Debian Codegraph @

rts/c/util.h

raw
1 // Start of util.h.
2 //
3 // Various helper functions that are useful in all generated C code.
4
5 #include <errno.h>
6 #include <string.h>
7
8 static const char *fut_progname = "(embedded Futhark)";
9
10 static void futhark_panic(int eval, const char *fmt, ...) __attribute__((noreturn));
11 static char* msgprintf(const char *s, ...);
12 static void* slurp_file(const char *filename, size_t *size);
13 static int dump_file(const char *file, const void *buf, size_t n);
14 struct str_builder;
15 static void str_builder_init(struct str_builder *b);
16 static void str_builder(struct str_builder *b, const char *s, ...);
17 static char *strclone(const char *str);
18
19 static void futhark_panic(int eval, const char *fmt, ...) {
20 va_list ap;
21 va_start(ap, fmt);
22 fprintf(stderr, "%s: ", fut_progname);
23 vfprintf(stderr, fmt, ap);
24 va_end(ap);
25 exit(eval);
26 }
27
28 // For generating arbitrary-sized error messages. It is the callers
29 // responsibility to free the buffer at some point.
30 static char* msgprintf(const char *s, ...) {
31 va_list vl;
32 va_start(vl, s);
33 size_t needed = 1 + (size_t)vsnprintf(NULL, 0, s, vl);
34 char *buffer = (char*) malloc(needed);
35 va_start(vl, s); // Must re-init.
36 vsnprintf(buffer, needed, s, vl);
37 return buffer;
38 }
39
40 static inline void check_err(int errval, int sets_errno, const char *fun, int line,
41 const char *msg, ...) {
42 if (errval) {
43 char errnum[10];
44
45 va_list vl;
46 va_start(vl, msg);
47
48 fprintf(stderr, "ERROR: ");
49 vfprintf(stderr, msg, vl);
50 fprintf(stderr, " in %s() at line %d with error code %s\n",
51 fun, line,
52 sets_errno ? strerror(errno) : errnum);
53 exit(errval);
54 }
55 }
56
57 #define CHECK_ERR(err, ...) check_err(err, 0, __func__, __LINE__, __VA_ARGS__)
58 #define CHECK_ERRNO(err, ...) check_err(err, 1, __func__, __LINE__, __VA_ARGS__)
59
60 // Read the rest of an open file into a NUL-terminated string; returns
61 // NULL on error.
62 static void* fslurp_file(FILE *f, size_t *size) {
63 long start = ftell(f);
64 fseek(f, 0, SEEK_END);
65 long src_size = ftell(f)-start;
66 fseek(f, start, SEEK_SET);
67 unsigned char *s = (unsigned char*) malloc((size_t)src_size + 1);
68 if (fread(s, 1, (size_t)src_size, f) != (size_t)src_size) {
69 free(s);
70 s = NULL;
71 } else {
72 s[src_size] = '\0';
73 }
74
75 if (size) {
76 *size = (size_t)src_size;
77 }
78
79 return s;
80 }
81
82 // Read a file into a NUL-terminated string; returns NULL on error.
83 static void* slurp_file(const char *filename, size_t *size) {
84 FILE *f = fopen(filename, "rb"); // To avoid Windows messing with linebreaks.
85 if (f == NULL) return NULL;
86 unsigned char *s = fslurp_file(f, size);
87 fclose(f);
88 return s;
89 }
90
91 // Dump 'n' bytes from 'buf' into the file at the designated location.
92 // Returns 0 on success.
93 static int dump_file(const char *file, const void *buf, size_t n) {
94 FILE *f = fopen(file, "w");
95
96 if (f == NULL) {
97 return 1;
98 }
99
100 if (fwrite(buf, sizeof(char), n, f) != n) {
101 return 1;
102 }
103
104 if (fclose(f) != 0) {
105 return 1;
106 }
107
108 return 0;
109 }
110
111 struct str_builder {
112 char *str;
113 size_t capacity; // Size of buffer.
114 size_t used; // Bytes used, *not* including final zero.
115 };
116
117 static void str_builder_init(struct str_builder *b) {
118 b->capacity = 10;
119 b->used = 0;
120 b->str = malloc(b->capacity);
121 b->str[0] = 0;
122 }
123
124 static void str_builder(struct str_builder *b, const char *s, ...) {
125 va_list vl;
126 va_start(vl, s);
127 size_t needed = (size_t)vsnprintf(NULL, 0, s, vl);
128
129 while (b->capacity < b->used + needed + 1) {
130 b->capacity *= 2;
131 b->str = realloc(b->str, b->capacity);
132 }
133
134 va_start(vl, s); // Must re-init.
135 vsnprintf(b->str+b->used, b->capacity-b->used, s, vl);
136 b->used += needed;
137 }
138
139 static void str_builder_str(struct str_builder *b, const char *s) {
140 size_t needed = strlen(s);
141 if (b->capacity < b->used + needed + 1) {
142 b->capacity *= 2;
143 b->str = realloc(b->str, b->capacity);
144 }
145 strcpy(b->str+b->used, s);
146 b->used += needed;
147 }
148
149 static void str_builder_char(struct str_builder *b, char c) {
150 size_t needed = 1;
151 if (b->capacity < b->used + needed + 1) {
152 b->capacity *= 2;
153 b->str = realloc(b->str, b->capacity);
154 }
155 b->str[b->used] = c;
156 b->str[b->used+1] = 0;
157 b->used += needed;
158 }
159
160 static void str_builder_json_str(struct str_builder* sb, const char* s) {
161 str_builder_char(sb, '"');
162 for (int j = 0; s[j]; j++) {
163 char c = s[j];
164 switch (c) {
165 case '\n':
166 str_builder_str(sb, "\\n");
167 break;
168 case '"':
169 str_builder_str(sb, "\\\"");
170 break;
171 default:
172 str_builder_char(sb, c);
173 }
174 }
175 str_builder_char(sb, '"');
176 }
177
178 static char *strclone(const char *str) {
179 size_t size = strlen(str) + 1;
180 char *copy = (char*) malloc(size);
181 if (copy == NULL) {
182 return NULL;
183 }
184
185 memcpy(copy, str, size);
186 return copy;
187 }
188
189 // Assumes NULL-terminated.
190 static char *strconcat(const char *src_fragments[]) {
191 size_t src_len = 0;
192 const char **p;
193
194 for (p = src_fragments; *p; p++) {
195 src_len += strlen(*p);
196 }
197
198 char *src = (char*) malloc(src_len + 1);
199 size_t n = 0;
200 for (p = src_fragments; *p; p++) {
201 strcpy(src + n, *p);
202 n += strlen(*p);
203 }
204
205 return src;
206 }
207
208 // End of util.h.