-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.c
327 lines (253 loc) · 8.63 KB
/
context.c
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#if __STDC_VERSION__ >= 199901L
#define _XOPEN_SOURCE 600
#else
#define _XOPEN_SOURCE 500
#endif /* __STDC_VERSION__ */
#include <geode.h>
#include <context.h>
#include <config.h>
#include <builtins.h>
#include <libshard.h>
#include <assert.h>
#include <errno.h>
#include <libgen.h>
#include <memory.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifdef __x86_64__
#define HOST_ARCH GEODE_ARCH_X86_64
#else
#define HOST_ARCH GEODE_ARCH_UNKNOWN
#endif
static const char* arch_strings[] = {
"unknown",
"x86_64"
};
static_assert(__len(arch_strings) == __GEODE_ARCH_NUM);
static int _getc(struct shard_source* src) {
return fgetc(src->userp);
}
static int _ungetc(int c, struct shard_source* src) {
return ungetc(c, src->userp);
}
static int _tell(struct shard_source* src) {
return ftell(src->userp);
}
static int _close(struct shard_source* src) {
return fclose(src->userp);
}
int geode_open_shard_file(const char* path, struct shard_source* dest, const char* restrict mode) {
FILE* fd = fopen(path, mode);
if(!fd)
return errno;
*dest = (struct shard_source) {
.userp = (void*) fd,
.origin = path,
.getc = _getc,
.ungetc = _ungetc,
.tell = _tell,
.close = _close,
.line = 1
};
return 0;
}
static void print_basic_error(struct shard_error* error) {
fprintf(stderr,
C_BLD C_RED "error: " C_RST C_BLD " %s:%u:" C_NOBLD " %s\n" C_RST,
error->loc.src->origin, error->loc.line, EITHER(error->err, strerror(error->_errno))
);
}
void geode_print_shard_error(struct shard_error* error) {
if(error->loc.src->getc != _getc) {
print_basic_error(error);
return;
}
FILE* fd = error->loc.src->userp;
assert(fd);
size_t fd_pos = ftell(fd), line_start = error->loc.offset;
while(line_start > 0) {
fseek(fd, --line_start - 1, SEEK_SET);
if(fgetc(fd) == '\n')
break;
}
size_t line_end = error->loc.offset + error->loc.width - 1;
fseek(fd, line_end, SEEK_SET);
char c;
while(((c = fgetc(fd)) != '\n') && c != EOF)
line_end++;
fseek(fd, line_start, SEEK_SET);
char* line_str = calloc(line_end - line_start + 1, sizeof(char));
fread(line_str, line_end - line_start, sizeof(char), fd);
size_t column = error->loc.offset - line_start;
fprintf(stderr,
C_BLD C_RED "error:" C_RST C_RST " %s\n " C_BLD C_BLUE " at " C_PURPLE "%s:%u:%zu:\n" C_RST,
EITHER(error->err, strerror(error->_errno)),
error->loc.src->origin, error->loc.line, column
);
fprintf(stderr,
C_BLD C_BLACK " %4d " C_NOBLD "| " C_RST,
error->loc.line
);
fwrite(line_str, sizeof(char), column, stderr);
fprintf(stderr, C_RED C_RST);
fwrite(line_str + column, sizeof(char), error->loc.width, stderr);
fprintf(stderr, C_RST "%s\n", line_str + column + error->loc.width);
fprintf(stderr, C_BLACK " |" C_RST " %*s" C_RED, (int) column, "");
for(size_t i = 0; i < MAX(error->loc.width, 1); i++)
fputc('^', stderr);
fprintf(stderr, C_RST "\n");
fseek(fd, fd_pos, SEEK_SET);
free(line_str);
}
int geode_context_init(struct geode_context* ctx, const char* prog_name, geode_error_handler_t error_handler) {
if(!ctx)
return EINVAL;
memset(ctx, 0, sizeof(struct geode_context));
ctx->prog_name = prog_name;
ctx->error_handler = error_handler;
ctx->main_config_path = GEODE_DEFAULT_PREFIX GEODE_DEFAULT_CONFIG_FILE;
ctx->store_path = GEODE_DEFAULT_PREFIX GEODE_DEFAULT_STORE_PATH;
ctx->prefix = GEODE_DEFAULT_PREFIX;
ctx->arch = HOST_ARCH;
ctx->cross_arch = HOST_ARCH;
ctx->nproc = 1;
ctx->shard_ctx = (struct shard_context){
.malloc = malloc,
.realloc = realloc,
.free = free,
.realpath = realpath,
.dirname = dirname,
.access = access,
.open = geode_open_shard_file,
.home_dir = nullptr,
.userp = ctx
};
int err = shard_init(&ctx->shard_ctx);
if(err)
geode_throw(ctx, LIBSHARD_INIT, .err_no=err);
ctx->shard_initialized = true;
return 0;
}
void geode_context_free(struct geode_context* ctx) {
if(ctx->shard_initialized) {
shard_deinit(&ctx->shard_ctx);
ctx->shard_initialized = false;
}
// free all heap buckets
struct heap_bucket* bucket = ctx->heap;
while(bucket) {
struct heap_bucket* next = bucket->next;
free(bucket);
bucket = next;
}
}
char* geode_strdup(struct geode_context* ctx, const char* str) {
size_t size = strlen(str) + 1;
char* new = geode_malloc(ctx, size);
memcpy(new, str, size);
return new;
}
void* geode_malloc(struct geode_context* ctx, size_t size) {
struct heap_bucket* bucket = malloc(sizeof(struct heap_bucket) + size);
if(!bucket)
return NULL;
bucket->next = NULL;
bucket->prev = NULL;
if(ctx->heap) {
struct heap_bucket* prev = ctx->heap;
while(prev->next)
prev = prev->next;
prev->next = bucket;
bucket->prev = prev;
}
else {
ctx->heap = bucket;
}
return bucket + 1;
}
void* geode_calloc(struct geode_context* ctx, size_t nmemb, size_t size) {
void* ptr = geode_malloc(ctx, nmemb * size);
memset(ptr, 0, nmemb * size);
return ptr;
}
void* geode_realloc(struct geode_context* ctx, void* ptr, size_t size) {
if(!ptr)
return geode_malloc(ctx, size);
struct heap_bucket* bucket = ((struct heap_bucket*) ptr) - 1;
bucket = realloc(bucket, sizeof(struct heap_bucket) + size);
if(bucket->prev)
bucket->prev->next = bucket;
else
ctx->heap = bucket;
if(bucket->next)
bucket->next->prev = bucket;
return bucket + 1;
}
void geode_free(struct geode_context* ctx, void* ptr) {
if(!ptr)
return;
struct heap_bucket* bucket = ((struct heap_bucket*) ptr) - 1;
if(bucket->prev)
bucket->prev->next = bucket->next;
if(bucket->next)
bucket->next->prev = bucket->prev;
if(bucket == ctx->heap)
ctx->heap = NULL;
free(bucket);
}
void geode_context_set_prefix(struct geode_context* ctx, const char* prefix) {
ctx->prefix = prefix;
if(strcmp(ctx->main_config_path, GEODE_DEFAULT_PREFIX GEODE_DEFAULT_CONFIG_FILE) == 0) {
size_t len = strlen(ctx->prefix) + strlen(GEODE_DEFAULT_CONFIG_FILE) + 2;
ctx->main_config_path = geode_malloc(ctx, len);
snprintf(ctx->main_config_path, len, "%s/%s", ctx->prefix, GEODE_DEFAULT_CONFIG_FILE);
}
if(strcmp(ctx->store_path, GEODE_DEFAULT_PREFIX GEODE_DEFAULT_STORE_PATH) == 0) {
size_t len = strlen(ctx->prefix) + strlen(GEODE_DEFAULT_STORE_PATH) + 2;
ctx->main_config_path = geode_malloc(ctx, len);
snprintf(ctx->main_config_path, len, "%s/%s", ctx->prefix, GEODE_DEFAULT_STORE_PATH);
}
}
void geode_context_set_config_file(struct geode_context* ctx, char* config_file) {
ctx->main_config_path = config_file;
}
void geode_context_set_store_path(struct geode_context* ctx, char* store_path) {
ctx->store_path = store_path;
}
int geode_context_set_nproc(struct geode_context* ctx, const char* nproc_arg) {
errno = 0;
ctx->nproc = (int) strtol(nproc_arg, NULL, 10);
return errno;
}
_Noreturn void geode_throw_err(struct geode_context* ctx, struct geode_error err) {
ctx->error_handler(ctx, err);
unreachable();
}
struct shard_value geode_call_file(struct geode_context* ctx, const char* script_path) {
struct shard_open_source* source = shard_open(&ctx->shard_ctx, script_path);
if(!source)
geode_throw(ctx, FILE_IO, .file=TUPLE(.err_no=errno,.path=script_path));
int err = shard_eval(&ctx->shard_ctx, source);
if(err)
geode_throw(ctx, SHARD, .shard=TUPLE(.num=err,.errs=shard_get_errors(&ctx->shard_ctx)));
infof(ctx, "Loaded script `%s`.\n", script_path);
struct shard_value result = {0};
err = shard_call(&ctx->shard_ctx, source->result, ctx->configuration, &result);
if(err)
geode_throw(ctx, SHARD, .shard=TUPLE(.num=err, .errs=shard_get_errors(&ctx->shard_ctx)));
return result;
}
const char* geode_arch_to_string(enum geode_architecture arch) {
return arch >= 0 && arch < __GEODE_ARCH_NUM
? EITHER(arch_strings[arch], arch_strings[GEODE_ARCH_UNKNOWN])
: arch_strings[GEODE_ARCH_UNKNOWN];
}
enum geode_architecture geode_string_to_arch(const char* str) {
for(enum geode_architecture a = 0; a < __GEODE_ARCH_NUM; a++) {
if(strcmp(str, arch_strings[a]) == 0)
return a;
}
return GEODE_ARCH_UNKNOWN;
}