diff options
author | Preston Pan <preston@nullring.xyz> | 2024-01-11 12:10:45 -0800 |
---|---|---|
committer | Preston Pan <preston@nullring.xyz> | 2024-01-11 12:10:45 -0800 |
commit | 8d9709720b8b7007d7178723c93feb6c881e546a (patch) | |
tree | 2b01251d40266b2df796860282e96701f4c5dfa1 /src/main.c | |
parent | 8569af05d8111654f1839f1cf50175a32b0bc547 (diff) |
add more documentation
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 31 |
1 files changed, 24 insertions, 7 deletions
@@ -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; } |