aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPreston Pan <preston@nullring.xyz>2024-01-11 12:10:45 -0800
committerPreston Pan <preston@nullring.xyz>2024-01-11 12:10:45 -0800
commit8d9709720b8b7007d7178723c93feb6c881e546a (patch)
tree2b01251d40266b2df796860282e96701f4c5dfa1 /src
parent8569af05d8111654f1839f1cf50175a32b0bc547 (diff)
add more documentation
Diffstat (limited to 'src')
-rw-r--r--src/main.c31
1 files changed, 24 insertions, 7 deletions
diff --git a/src/main.c b/src/main.c
index 884a198..cbf7f34 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,5 +1,6 @@
#include <builtins.h>
#include <dlfcn.h>
+#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <stem.h>
@@ -13,17 +14,38 @@ extern array_t *EVAL_STACK;
extern ht_t *OBJ_TABLE;
extern ht_t *FLIT;
+/*! prints usage then exits */
void usage() {
printf("Usage: stem [-hv] [file]\n");
exit(1);
}
+/*! prints version and exits */
void version() {
printf("Author: Preston Pan, MIT License 2023\n");
printf("stem, version 1.2 alpha\n");
exit(0);
}
+/*! frees all global variables */
+void global_free() {
+ free(PARSER->source);
+ ht_free(WORD_TABLE, value_free);
+ ht_free(FLIT, func_free);
+ ht_free(OBJ_TABLE, custom_free);
+ array_free(STACK);
+ free(PARSER);
+ array_free(EVAL_STACK);
+}
+
+/*! handles SIGINT signal; frees memory before exit */
+void sigint_handler(int signum) {
+ signal(SIGINT, sigint_handler);
+ global_free();
+ fflush(stdout);
+ exit(1);
+}
+
int main(int argc, char **argv) {
value_t *v;
size_t len;
@@ -59,6 +81,7 @@ int main(int argc, char **argv) {
OBJ_TABLE = init_ht(500);
add_funcs();
+ signal(SIGINT, sigint_handler);
/* parse and eval loop */
while (1) {
@@ -69,12 +92,6 @@ int main(int argc, char **argv) {
}
/* Free all global variables */
- free(PARSER->source);
- ht_free(WORD_TABLE, value_free);
- ht_free(FLIT, func_free);
- ht_free(OBJ_TABLE, custom_free);
- array_free(STACK);
- free(PARSER);
- array_free(EVAL_STACK);
+ global_free();
return 0;
}