-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
71 lines (63 loc) · 1.68 KB
/
main.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
/* See LICENSE file for copyright and license details. */
#include <libgen.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "tisp.h"
#ifndef TIB_DYNAMIC
# include "tibs.tsp.h"
tsp_include_tib(math);
tsp_include_tib(io);
tsp_include_tib(os);
tsp_include_tib(string);
#endif
int
main(int argc, char *argv[])
{
int i = 1;
Val v = NULL;
Tsp st = tisp_env_init(64);
#ifndef TIB_DYNAMIC
tib_env_math(st);
tib_env_io(st);
tib_env_os(st);
tib_env_string(st);
tisp_env_lib(st, tibs);
#endif
/* TODO SIGTERM to handle garbage collection */
struct sigaction sigint;
sigint.sa_handler = SIG_IGN;
sigaction(SIGINT, &sigint, NULL);
if (argc == 1) {
st->file = "(repl)";
goto readstr;
}
for (; i < argc; i++, v = NULL) {
if (argv[i][0] == '-') {
if (argv[i][1] == 'c') { /* run next argument as tisp command */
if (!(st->file = argv[++i])) {
fputs("tisp: expected command after -c\n", stderr);
exit(2);
}
readstr:
if ((v = tisp_read(st)))
v = tisp_eval(st, st->global, v);
} else if (argv[i][1] == 'v') { /* version and copyright info */
fprintf(stderr, "tisp v%s (c) 2017-2022 Ed van Bruggen\n", VERSION);
exit(0);
} else if (argv[i][1]) { /* unsupported argument or help */
fputs("usage: tisp [-hv] [-c COMMAND] [-] [FILE ...]\n", stderr);
exit(argv[i][1] == 'h' ? 0 : 1);
} else { /* single hypen read from stdin */
v = tisp_eval_seq(st, st->global, tisp_parse_file(st, NULL));
}
} else { /* otherwise read as file */
v = tisp_eval_seq(st, st->global, tisp_parse_file(st, argv[i]));
}
if (v && v->t != TSP_NONE) tisp_print(stdout, v);
}
puts("");
return 0;
}