]> Chaos Git - misc/ysh.git/commitdiff
Add command line options
authorJon Feldman <chaos.kagami@gmail.com>
Wed, 26 Jul 2017 00:41:32 +0000 (20:41 -0400)
committerJon Feldman <chaos.kagami@gmail.com>
Wed, 26 Jul 2017 00:41:32 +0000 (20:41 -0400)
gsh_main.c

index 2d7d96e6347837814a4ddf869c9eeccc76366818..53ae0ed42c7aea102d87db4750c165ac48006be0 100644 (file)
@@ -3,11 +3,16 @@
 #include <string.h>
 #include <assert.h>
 #include <ctype.h>
+#include <getopt.h>
 
 #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);
+        }
     }
 }