]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - main.c
AUTHORS
[xonotic/gmqcc.git] / main.c
diff --git a/main.c b/main.c
index 5b9828832743bc85e2fabc0116374fd8fb10803c..88481e388d2c0fa004f678c27c4bcd70fdc6287a 100644 (file)
--- a/main.c
+++ b/main.c
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2012 
- *     Dale Weiler
+ *     Dale Weiler
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy of
  * this software and associated documentation files (the "Software"), to deal in
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  * SOFTWARE.
  */
-#include <stdlib.h>
-#include <string.h>
-#include <limits.h>
-#include  "gmqcc.h"
+#include "gmqcc.h"
+// 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;
+int opts_compiler                  = COMPILER_GMQCC;
+int opts_darkplaces_stringtablebug = 0;
+int opts_omit_nullcode             = 0;
+
+static const int usage(const char *const app) {
+    printf("usage:\n");
+    printf("    %s -c<file>          -oprog.dat -- compile file\n"     , app);
+    printf("    %s -a<file>          -oprog.dat -- assemble file\n"    , app);
+    printf("    %s -c<file> -i<file> -oprog.dat -- compile together (allowed multiple -i<file>)\n" , app);
+    printf("    %s -a<file> -i<file> -oprog.dat -- assemble together(allowed multiple -i<file>)\n", app);
+    printf("    example:\n");
+    printf("    %s -cfoo.qc -ibar.qc -oqc.dat -afoo.qs -ibar.qs -oqs.dat\n", app);
+    printf("    additional flags:\n");
+    printf("        -debug           -- turns on compiler debug messages\n");
+    printf("        -memchk          -- turns on compiler memory leak check\n");
+    printf("        -help            -- prints this help/usage text\n");
+    printf("        -std             -- select the QuakeC compile type (types below):\n");
+    printf("            -std=qcc     -- original QuakeC\n");
+    printf("            -std=ftqecc  -- fteqcc QuakeC\n");
+    printf("            -std=qccx    -- qccx QuakeC\n");
+    printf("            -std=gmqcc   -- this compiler QuakeC (default selection)\n");
+    printf("    codegen flags:\n");
+    printf("        -fdarkplaces-string-table-bug -- patches the string table to work with bugged versions of darkplaces\n");
+    printf("        -fomit-nullcode               -- omits the generation of null code (will break everywhere see propsal.txt)\n");
+    return -1;
+}
+
 int main(int argc, char **argv) {
-       argc--;
-       argv++;
-       
-       const char *ifile = argv[0];
-       
-       FILE *fp = fopen(ifile, "r");
-       if  (!fp) {
-               fclose(fp);
-               return error(ERROR_COMPILER, "Source file: %s not found\n", ifile);
-       } else {
-               struct lex_file *lex = lex_open(fp);
-               if (!lex) {
-                       fclose(fp);
-                       return 0;
-               }
-               parse_tree(lex);
-               lex_close (lex);
-       }
-       
-       code_write();
-       return 0;
+    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 */
+            case 'i': items_add((argitem){util_strdup(&argv[1][2]), 2}); break; /* includes */
+            default:
+                if (!strncmp(&argv[1][1], "debug" , 5)) { opts_debug  = 1; break; }
+                if (!strncmp(&argv[1][1], "memchk", 6)) { opts_memchk = 1; break; }
+                if (!strncmp(&argv[1][1], "help",   4)) {
+                    return usage(app);
+                    break;
+                }
+                /* compiler type selection */
+                if (!strncmp(&argv[1][1], "std=qcc"   , 7 )) { opts_compiler = COMPILER_QCC;    break; }
+                if (!strncmp(&argv[1][1], "std=fteqcc", 10)) { opts_compiler = COMPILER_FTEQCC; break; }
+                if (!strncmp(&argv[1][1], "std=qccx",   8 )) { opts_compiler = COMPILER_QCCX;   break; }
+                if (!strncmp(&argv[1][1], "std=gmqcc",  9 )) { opts_compiler = COMPILER_GMQCC;  break; }
+                if (!strncmp(&argv[1][1], "std=",       4 )) {
+                    printf("invalid std selection, supported types:\n");
+                    printf("    -std=qcc     -- original QuakeC\n");
+                    printf("    -std=ftqecc  -- fteqcc QuakeC\n");
+                    printf("    -std=qccx    -- qccx QuakeC\n");
+                    printf("    -std=gmqcc   -- this compiler QuakeC (default selection)\n");
+                    return 0;
+                }
+
+                /* code specific switches */
+                if (!strcmp(&argv[1][1], "fdarkplaces-stringtablebug")) {
+                    opts_darkplaces_stringtablebug = 1;
+                    break;
+                }
+                if (!strcmp(&argv[1][1], "fomit-nullcode")) {
+                    opts_omit_nullcode = 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");
+
+    util_debug("COM", "starting ...\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;
+        }
+    }
+
+    util_debug("COM", "cleaning ...\n"); 
+    /* clean list */
+    for (itr = 0; itr < items_elements; itr++)
+        mem_d(items_data[itr].name);
+    mem_d(items_data);
+
+    util_meminfo();
+    return 0;
 }