]> git.xonotic.org Git - xonotic/gmqcc.git/commitdiff
command line argument parsing.
authorDale Weiler <killfieldengine@gmail.com>
Fri, 20 Apr 2012 05:20:22 +0000 (01:20 -0400)
committerDale Weiler <killfieldengine@gmail.com>
Fri, 20 Apr 2012 05:20:22 +0000 (01:20 -0400)
assembler.c
gmqcc.h
main.c
util.c

index 6a4ec461a9f9848ec557d1946155dc04c27f7a3d..59ee27b4af1a5c4d018da3cda3af750169d94b4f 100644 (file)
@@ -45,12 +45,6 @@ static char *const asm_getline(size_t *byte, FILE *fp) {
     return line;
 }
 
-#define asm_rmnewline(L,S) *((L)+*(S)-1) = '\0'
-#define asm_skipwhite(L)             \
-    while((*(L)==' '||*(L)=='\t')) { \
-        (L)++;                       \
-    }
-    
 void asm_init(const char *file, FILE **fp) {
     *fp = fopen(file, "r");
     code_init();
diff --git a/gmqcc.h b/gmqcc.h
index f1004ebf3c96ac8d53fdc358bca7175b1a69d58f..6e7bb8b1e3a1d3f3ae7815bd1ea0a1efe6aa1b30 100644 (file)
--- a/gmqcc.h
+++ b/gmqcc.h
@@ -471,4 +471,9 @@ static const struct {
 void asm_init (const char *, FILE **);
 void asm_close(FILE *);
 void asm_parse(FILE *);
+//======================================================================
+//============================= main.c =================================
+//======================================================================
+extern int opts_debug;
+extern int opts_memchk;
 #endif
diff --git a/main.c b/main.c
index 2a86c32d6e3ee34f9ef0a7ada0452e2bcaad0060..183c3574832a40e6ba2e7c1543f13e5db4658dc8 100644 (file)
--- a/main.c
+++ b/main.c
  * SOFTWARE.
  */
 #include "gmqcc.h"
-int main(int argc, char **argv) {
-    argc--;
-    argv++;
-    
-    //const char *ifile = argv[0];
-    FILE *fp;
+// todo CLEANUP this argitem thing
+typedef struct { char *name, type; } argitem;
+VECTOR_MAKE(argitem, items);
+
+/* global options */
+int opts_debug  = 0;
+int opts_memchk = 0;
+
+static const int usage(const char *const app) {
+    printf("usage:\n");
+    printf("    %s -c<file> -- compile file\n" , app);
+    printf("    %s -a<file> -- assemble file\n", app);
+    printf("    additional flags:\n");
+    printf("        -debug  -- turns on compiler debug messages\n");
+    printf("        -memchk -- turns on compiler memory leak check\n");
     
-    /*TODO: proper interface swith switches*/
+    return -1;
+}
+
+int main(int argc, char **argv) {
+    size_t itr = 0;
+    char  *app = &argv[0][0];
+    FILE  *fpp = NULL;
+
+    /*
+     * Parse all command line arguments.  This is rather annoying to do
+     * because of all tiny corner cases.
+     */
+    if (argc <= 1 || (argv[1][0] != '-'))
+        return usage(app);
+
+    while ((argc > 1) && argv[1][0] == '-') {
+        switch (argv[1][1]) {
+            case 'c': items_add((argitem){util_strdup(&argv[1][2]), 0}); break; /* compile  */ 
+            case 'a': items_add((argitem){util_strdup(&argv[1][2]), 1}); break; /* assemble */
+            default:
+                if (!strncmp(&argv[1][1], "debug" , 5)) { opts_debug  = 1; break; }
+                if (!strncmp(&argv[1][1], "memchk", 6)) { opts_memchk = 1; break; }
+                return usage(app);
+                
+        }
+        ++argv;
+        --argc;
+    }
+
+    /*
+     * options could depend on another option, this is where option
+     * validity checking like that would take place.
+     */
+    if (opts_memchk && !opts_debug) 
+        printf("Warning: cannot enable -memchk, without -debug.\n");
+
+    /* multi file multi path compilation system */
+    for (; itr < items_elements; itr++) {
+        switch (items_data[itr].type) {
+            case 0:
+                fpp = fopen(items_data[itr].name, "r");
+                struct lex_file *lex = lex_open(fpp);
+                parse_gen(lex);
+                lex_close(lex);
+                break;
+            case 1:
+                asm_init (items_data[itr].name, &fpp);
+                asm_parse(fpp);
+                asm_close(fpp);
+                break;
+        }
+    }
     
-    asm_init ("test.qs", &fp);
-    asm_parse(fp);
-    asm_close(fp);
-    util_meminfo();
+    /* clean list */
+    for (itr = 0; itr < items_elements; itr++)
+        mem_d(items_data[itr].name);
+    mem_d(items_data);
+
+    if (opts_memchk)
+        util_meminfo();
     return 0;
 }
diff --git a/util.c b/util.c
index d46172096b2ab9cfaf6a84688a4670b4040310a4..3d069e18b8749687542ede403c2014cee7f60ca0 100644 (file)
--- a/util.c
+++ b/util.c
@@ -61,14 +61,17 @@ void util_memory_d(void *ptrn, unsigned int line, const char *file) {
 }
 
 void util_meminfo() {
-       util_debug("MEM", "Memory information:\n\
-       Total allocations:   %llu\n\
-       Total deallocations: %llu\n\
-       Total allocated:     %llu (bytes)\n\
-       Total deallocated:   %llu (bytes)\n",
-               mem_at, mem_dt,
-               mem_ab, mem_db
-       );
+    if (!opts_memchk)
+        return;
+        
+    util_debug("MEM", "Memory information:\n\
+        Total allocations:   %llu\n\
+        Total deallocations: %llu\n\
+        Total allocated:     %llu (bytes)\n\
+        Total deallocated:   %llu (bytes)\n",
+            mem_at, mem_dt,
+            mem_ab, mem_db
+    );
 }
 
 //#ifndef mem_d
@@ -134,6 +137,9 @@ char *util_strrnl(char *src) {
 }
 
 void util_debug(const char *area, const char *ms, ...) {
+    if (!opts_debug)
+        return;
+        
     va_list  va;
     va_start(va, ms);
     fprintf (stdout, "DEBUG: ");