From: Jon Feldman Date: Wed, 26 Jul 2017 00:41:32 +0000 (-0400) Subject: Add command line options X-Git-Url: https://chaos.moe/g/?a=commitdiff_plain;h=9909ccdb25e651049682e50fb4787b4d28eb740c;p=misc%2Fysh.git Add command line options --- diff --git a/gsh_main.c b/gsh_main.c index 2d7d96e..53ae0ed 100644 --- a/gsh_main.c +++ b/gsh_main.c @@ -3,11 +3,16 @@ #include #include #include +#include #define BUF_CHUNKSIZ 64 +// Exit the main interactive loop int shell_do_exit = 0; +// Various flags. +int obscene_debug = 0; + void* malloc_trap(size_t malloc_size) { void* ret = malloc(malloc_size); if (!ret) { @@ -104,6 +109,9 @@ char *read_input() { // evaluated such that no AST_ROOT elements remain aside from the tree's // primary element. +// Postnote; implementation is a bit different as one can see in --obscene +// mode. + typedef struct ast_s { int type; size_t size; // Number of elements for AST_ROOT|AST_GRP, @@ -278,17 +286,42 @@ ast_t* parse(char* data) { ast_t *ptr = ast->ptr; - ast_dump_print(ast, 0); + if (obscene_debug) ast_dump_print(ast, 0); return ast; } -int main(int c, char *v[]) { - while (!shell_do_exit) { - // Read a command in. - char *input = read_input(); - ast_t *toks = parse(input); - free(toks); - free(input); +int main(int argc, char **argv) { + char* run_str = NULL; + // Options. + int c; + while ((c = getopt (argc, argv, "Dc:")) != -1) { + switch(c) { + case 'D': + obscene_debug = 1; + break; + case 'c': + run_str = optarg; + break; + case '?': + printf("Invalid invocation.\n"); + return 1; + break; + default: + assert(0); // Seriously, how? + break; + } + } + + if (run_str) { + parse(run_str); + } else { + while (!shell_do_exit) { + // Read a command in. + char *input = read_input(); + ast_t *toks = parse(input); + // FIXME: free the damn toks memory, this requires traversing to the bottom tho + free(input); + } } }