-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
351 lines (327 loc) · 9.71 KB
/
main.c
File metadata and controls
351 lines (327 loc) · 9.71 KB
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "libscary.h"
#include "schaf.h"
#include "utils.h"
typedef struct {
const char *name;
int ch; // declare an alias of short option
bool has_arg;
} OptLonger;
typedef struct {
const char *name;
const char *value;
} OptLongerData;
#define eprintf(fmt, ...) fprintf(stderr, "error: " fmt "\n" __VA_OPT__(,) __VA_ARGS__)
static int getopt_longer(int argc, char *const *argv, const char *optstrorig,
const OptLonger longopts[], OptLongerData *val)
{
if (longopts == NULL)
return getopt(argc, argv, optstrorig);
static char optstring[0x100] = { '\0', };
if (optstring[0] == '\0') {
if (strlen(optstrorig) >= sizeof(optstring) - 2 ||
strchr(optstrorig, '-') != NULL)
return '?';
strcpy(optstring, optstrorig);
strcat(optstring, "-:");
}
int opt = getopt(argc, argv, optstring);
if (opt != '-')
return opt; // as is
const char *flag = argv[optind - 1];
if (strncmp(flag, "--", 2) != 0)
goto invalid;
const char *name = flag + 2;
for (size_t i = 0; longopts[i].name != NULL; i++) {
const OptLonger *e = &longopts[i];
size_t len = strlen(e->name);
if (!(strncmp(e->name, name, len) == 0 && (name[len] == '\0' || name[len] == '=')))
continue;
const char *value = NULL;
if (!e->has_arg && name[len] == '\0')
; // accepted
else if (e->has_arg && name[len] == '=' && name[len + 1] != '\0')
value = name + len + 1;
else {
eprintf("option requires %s argument '%s'",
e->has_arg ? "an" : "no", argv[optind - 1]);
return '?';
}
if (val != NULL)
*val = (OptLongerData) { .name = e->name, .value = value };
return e->ch;
}
invalid:
eprintf("invalid option '%s'", argv[optind - 1]);
return '?';
}
static void usage_opt(FILE *out, const char *opt, const char *desc)
{
fprintf(out, " %-18s%s\n", opt, desc);
}
[[noreturn]]
static void usage(FILE *out)
{
fprintf(out, "Usage: schaf [-HMpPsSTh] [-e <source>] <file>\n");
usage_opt(out, "<file>", "Path or - (stdin).");
usage_opt(out, "-e <source>", "Evaluate <source> string directly instead of <file>.");
usage_opt(out, "-H <MiB>", "Specify initial heap size.");
usage_opt(out, "-l <file>", "Load specified file in advance. Can be specified multiple.");
usage_opt(out, "-M", "Print memory usage (VmHWM) at exit.");
usage_opt(out, "-p", "Print the last expression before exit.");
usage_opt(out, "-P", "Only parse and print syntax list without evaluation.");
usage_opt(out, "-s", "Print heap statistics before/after GC.");
usage_opt(out, "--gc=<algorithm>", "Specify GC algorithm: mark-sweep, mark-sweep+bitmap, epsilon.");
usage_opt(out, "-S, --gc-stress", "Put stress on GC.");
usage_opt(out, "-T", "Print consumed CPU time at exit.");
usage_opt(out, "-h, --help", "Print this help.");
exit(out == stdout ? 0 : 2);
}
#define opt_error(fmt, ...) do { \
eprintf(fmt __VA_OPT__(,) __VA_ARGS__); \
usage(stderr); \
} while (0)
typedef struct {
const char *path;
const char *script;
const char **loaded_files;
double init_heap_size_mib;
int gc_algorithm;
bool print;
bool parse_only;
bool cputime;
bool memory;
bool heap_stat;
bool stress_gc;
bool interacitve;
} SchOption;
enum {
SCH_GC_ALGORITHM_INVALID = 0xFF,
};
static int get_gc_algorithm(const char *s)
{
static const struct {
const char *name;
SchGCAlgorithm algorithm;
} algos[] = {
{ "mark-sweep", SCH_GC_ALGORITHM_MARK_SWEEP },
{ "mark-sweep+bitmap", SCH_GC_ALGORITHM_MARK_SWEEP_BITMAP },
{ "epsilon", SCH_GC_ALGORITHM_EPSILON },
};
for (size_t i = 0; i < sizeof(algos) / sizeof(*algos); i++) {
if (strcmp(s, algos[i].name) == 0)
return algos[i].algorithm;
}
return SCH_GC_ALGORITHM_INVALID;
}
static void parse_opt_longer(SchOption *o, const char *name, const char *value)
{
if (strcmp(name, "gc") == 0) {
int s = get_gc_algorithm(value);
if (s == SCH_GC_ALGORITHM_INVALID)
opt_error("invalid value for --gc: %s", value);
o->gc_algorithm = s;
}
}
static double parse_posnum(const char *s)
{
char *ep;
double val = strtod(s, &ep);
if (val <= 0 || ep[0] != '\0')
opt_error("invalid positive number '%s'", s);
return val;
}
static void append_load_file(SchOption *o, const char *load_file)
{
if (o->loaded_files == NULL)
o->loaded_files = scary_new(sizeof(const char *));
scary_push((char ***) &o->loaded_files, load_file);
}
static SchOption parse_opt(int argc, char *const *argv)
{
const OptLonger opts[] = {
{ "help", 'h', false },
{ "gc", 0, true },
{ "gc-stress", 'S', false },
{}
};
SchOption o = {
.init_heap_size_mib = 0.0,
.gc_algorithm = SCH_GC_ALGORITHM_INVALID,
};
OptLongerData data = { 0 };
int opt;
while ((opt = getopt_longer(argc, argv, "e:H:l:MPpSsTh", opts, &data)) != -1) {
switch (opt) {
case 'e':
o.script = optarg;
break;
case 'H':
o.init_heap_size_mib = parse_posnum(optarg);
break;
case 'l':
append_load_file(&o, optarg);
break;
case 'M':
o.memory = true;
break;
case 'P':
o.parse_only = o.print = true;
break;
case 'p':
o.print = true;
break;
case 'S':
o.stress_gc = true;
break;
case 's':
o.heap_stat = true;
break;
case 'T':
o.cputime = true;
break;
case 'h':
usage(stdout);
case 0:
parse_opt_longer(&o, data.name, data.value);
break;
case '?':
usage(stderr);
}
}
o.path = argv[optind];
if (o.path == NULL && o.script == NULL)
o.interacitve = true;
if (o.path != NULL && o.script != NULL)
opt_error("filename %s given while option '-e' passed", o.path);
return o;
}
static double cputime_ms(void)
{
struct timespec ts;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
return ts.tv_sec * 1000.0 + ts.tv_nsec / (1000.0*1000.0);
}
static void print_cputime(void)
{
fprintf(stderr, "CPU: %.3lf ms\n", cputime_ms());
}
static void print_vmhwm(void)
{
static const char *const path = "/proc/self/status",
*const pat = "VmHWM";
FILE *status = fopen(path, "r");
if (status == NULL)
error("cannot open file %s", path);
char buf[BUFSIZ];
bool printed = false;
while (fgets(buf, sizeof(buf), status) != NULL) {
if (strncmp(buf, pat, strlen(pat)) == 0) {
fprintf(stderr, "%s", buf);
printed = true;
break;
}
}
fclose(status);
if (!printed)
error("memory usage not printed");
}
static bool to_be_continued(SchValue ret)
{
return ret == SCH_UNDEF &&
strstr(sch_error_message(), "expected ')' but got EOF") != NULL;
}
static bool is_empty(const char *line)
{
const char *p = line;
while (isspace(*p))
p++;
return *p == '\0';
}
static int repl(void)
{
char buf[BUFSIZ] = { '\0' }, line[BUFSIZ];
for (;;) { // Loop of..
printf(buf[0] ? ".....> " : "schaf$ ");
if (fgets(line, sizeof(line), stdin) == NULL) // Read,
break;
if (is_empty(line))
continue;// ignore
size_t nextlen = strlen(buf) + strlen(line);
if (nextlen >= sizeof(buf)) {
printf("error: input too large\n");
buf[0] = '\0';// shrink
continue;
}
strcat(buf, line);
SchValue v = sch_eval_string(buf); // Eval,
if (to_be_continued(v))
continue;
buf[0] = '\0';
if (v == SCH_UNDEF) {
printf("error: %s\n", sch_error_message());
continue;
}
sch_display(v); // Print!
printf("\n");
}
printf("\n");
fflush(stdout);
return 0;
}
static SchValue load_files(const char **files)
{
SchValue v = SCH_UNDEF;
for (size_t i = 0, len = scary_length(files); i < len; i++) {
v = sch_load(files[i]);
if (v == SCH_UNDEF)
break;
}
scary_free(files);
return v;
}
int main(int argc, char **argv)
{
SchOption o = parse_opt(argc, argv);
sch_set_gc_stress(o.stress_gc);
sch_set_gc_print_stat(o.heap_stat);
if (o.init_heap_size_mib > 0.0)
sch_set_gc_init_size(o.init_heap_size_mib);
if (o.gc_algorithm != SCH_GC_ALGORITHM_INVALID)
sch_set_gc_algorithm(o.gc_algorithm);
SCH_INIT();
SchValue v;
if (o.loaded_files != NULL) {
v = load_files(o.loaded_files);
if (v == SCH_UNDEF)
error("%s", sch_error_message()); // error while loading
}
if (o.interacitve)
return repl();
if (o.script)
v = o.parse_only ? sch_parse_string(o.script) : sch_eval_string(o.script);
else {
if (strcmp(o.path, "-") == 0)
v = o.parse_only ?
sch_parse_file(stdin, "<stdin>") : sch_load_file(stdin, "<stdin>");
else
v = o.parse_only ? sch_parse(o.path) : sch_load(o.path);
}
if (v == SCH_UNDEF)
error("%s", sch_error_message()); // runtime error occurred
if (o.print) {
sch_display(v);
printf("\n");
}
if (o.cputime)
print_cputime();
if (o.memory)
print_vmhwm();
return sch_fin();
}