bitwise util.c
  1#include <errno.h>
  2#include <stdio.h>
  3#include <stdlib.h>
  4#include <string.h>
  5#include <fcntl.h>
  6#include <unistd.h>
  7
  8#include "util.h"
  9#include "kav.h"
 10
 11#define CHUNK 4096
 12
 13const char *PROG;
 14
 15void
 16diev(int sys, const char *const *v, usize n) {
 17	int e = errno;
 18	usize i;
 19
 20	fputs(PROG, stderr);
 21	fputs(": fatal: ", stderr);
 22	for(i = 0; i < n; i++)
 23		fputs(v[i], stderr);
 24	if(sys) {
 25		fputs(": ", stderr);
 26		fputs(strerror(e), stderr);
 27	}
 28	fputs("\n", stderr);
 29	exit(1);
 30}
 31
 32void *
 33alloc(usize n) {
 34	void *p = malloc(n);
 35
 36	if(!p)
 37		die("out of memory");
 38	return p;
 39}
 40
 41
 42void *
 43zalloc(usize n) {
 44	void *p;
 45
 46	p = alloc(n);
 47	memset(p, 0, n);
 48	return p;
 49}
 50
 51void *
 52allocre(void *p, usize n) {
 53	void *q = realloc(p, n);
 54
 55	if(!q)
 56		die("out of memory");
 57	return q;
 58}
 59
 60void
 61sagrow(stralloc *sa, usize n) {
 62	usize size;
 63	usize extra;
 64
 65	if(n > SIZE_MAX - sa->len)
 66		die("out of memory");
 67	size = sa->len + n;
 68	if(size <= sa->a)
 69		return;
 70	extra = size / 8 + 30;
 71	if(extra <= SIZE_MAX - size)
 72		size += extra;
 73	sa->s = allocre(sa->s, size);
 74	sa->a = size;
 75}
 76
 77/* append n bytes of s */
 78void
 79sacatb(stralloc *sa, const char *s, usize n) {
 80	sagrow(sa, n);
 81	memcpy(sa->s + sa->len, s, n);
 82	sa->len += n;
 83}
 84
 85void
 86sa0(stralloc *sa) {
 87	sagrow(sa, 1);
 88	sa->s[sa->len] = '\0';
 89}
 90
 91int
 92openreadclose(const char *fn, stralloc *sa) {
 93	int fd = open(fn, O_RDONLY);
 94	isize r;
 95
 96	if(fd == -1)
 97		return 0;
 98	for(;;) {
 99		sagrow(sa, CHUNK);
100		r = read(fd, sa->s + sa->len, CHUNK);
101		if(r == -1 && errno == EINTR)
102			continue;
103		if(r == -1) {
104			close(fd);
105			return 0;
106		}
107		if(r == 0) {
108			close(fd);
109			return 1;
110		}
111		sa->len = sa->len + r;
112	}
113}
114
115usize
116fmtusize(char *s, usize u) {
117	usize len;
118	usize q;
119
120	len = 1;
121	q = u;
122	while(q > 9) {
123		len++;
124		q = q / 10;
125	}
126	if(s) {
127		s = s + len;
128		do {
129			s = s - 1;
130			*s = '0' + (u % 10);
131			u = u / 10;
132		} while(u);
133	}
134	return len;
135}
136
137void
138putnum(usize u) {
139	char num[FMTUSIZE];
140
141	fwrite(num, 1, fmtusize(num, u), stdout);
142}
143
144/* the value of a number token */
145i64
146tonum(struct token t) {
147	u64 n = 0;
148	usize i;
149
150	if(t.kind == TCHAR) {
151		if(t.str[1] != '\\')
152			return (u8)t.str[1];
153		switch(t.str[2]) {
154		case 'n': return '\n';
155		case 't': return '\t';
156		case 'r': return '\r';
157		case '0': return 0;
158		default:  return t.str[2];
159		}
160	}
161	for(i = 0; i < t.length; i++)
162		n = n * 10 + (t.str[i] - '0');
163	return (i64)n;
164}
165
166char *
167tokdup(struct token t) {
168	char *s;
169
170	s = alloc(t.length + 1);
171	memcpy(s, t.str, t.length);
172	s[t.length] = '\0';
173	return s;
174}
175
176int
177tokeqb(struct token t, const char *s, usize n) {
178	return t.length == n && memcmp(t.str, s, n) == 0;
179}
180
181void
182erroratv(const struct location *loc, const char *const *v, usize n) {
183	char num[FMTUSIZE];
184	usize i;
185
186	fputs(loc->file, stderr);
187	fputs(":", stderr);
188	fwrite(num, 1, fmtusize(num, loc->line), stderr);
189	fputs(":", stderr);
190	fwrite(num, 1, fmtusize(num, loc->col), stderr);
191	fputs(": error: ", stderr);
192	for(i = 0; i < n; i++)
193		fputs(v[i], stderr);
194	fputs("\n", stderr);
195	exit(1);
196}