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