tisp

tiny lisp
git clone git://edryd.org/tisp
Log | Files | Refs | README | LICENSE

main.c (1717B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include <libgen.h>
      3 #include <signal.h>
      4 #include <stdarg.h>
      5 #include <stdio.h>
      6 #include <stdlib.h>
      7 #include <string.h>
      8 
      9 #include "tisp.h"
     10 #ifndef TIB_DYNAMIC
     11 #  include "tibs.tsp.h"
     12 	tsp_include_tib(math);
     13 	tsp_include_tib(io);
     14 	tsp_include_tib(os);
     15 	tsp_include_tib(string);
     16 #endif
     17 
     18 int
     19 main(int argc, char *argv[])
     20 {
     21 	int i = 1;
     22 	Val v = NULL;
     23 
     24 	Tsp st = tisp_env_init(64);
     25 #ifndef TIB_DYNAMIC
     26 	tib_env_math(st);
     27 	tib_env_io(st);
     28 	tib_env_os(st);
     29 	tib_env_string(st);
     30 	tisp_env_lib(st, tibs);
     31 #endif
     32 
     33 	/* TODO SIGTERM to handle garbage collection */
     34 	struct sigaction sigint;
     35 	sigint.sa_handler = SIG_IGN;
     36 	sigaction(SIGINT, &sigint, NULL);
     37 
     38 	if (argc == 1) {
     39 		st->file = "(repl)";
     40 		goto readstr;
     41 	}
     42 
     43 	for (; i < argc; i++, v = NULL) {
     44 		if (argv[i][0] == '-') {
     45 			if (argv[i][1] == 'c') { /* run next argument as tisp command */
     46 				if (!(st->file = argv[++i])) {
     47 					fputs("tisp: expected command after -c\n", stderr);
     48 					exit(2);
     49 				}
     50 readstr:
     51 				if ((v = tisp_read(st)))
     52 					v = tisp_eval(st, st->global, v);
     53 			} else if (argv[i][1] == 'v') { /* version and copyright info */
     54 				fprintf(stderr, "tisp v%s (c) 2017-2022 Ed van Bruggen\n", VERSION);
     55 				exit(0);
     56 			} else if (argv[i][1]) { /* unsupported argument or help */
     57 				fputs("usage: tisp [-hv] [-c COMMAND] [-] [FILE ...]\n", stderr);
     58 				exit(argv[i][1] == 'h' ? 0 : 1);
     59 			} else { /* single hypen read from stdin */
     60 				v = tisp_eval_seq(st, st->global, tisp_parse_file(st, NULL));
     61 			}
     62 		} else { /* otherwise read as file */
     63 			v = tisp_eval_seq(st, st->global, tisp_parse_file(st, argv[i]));
     64 		}
     65 		if (v && v->t != TSP_NONE) tisp_print(stdout, v);
     66 	}
     67 
     68 	puts("");
     69 
     70 	return 0;
     71 }