]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - main.c
Merge branch 'master' into ast-and-ir
[xonotic/gmqcc.git] / main.c
diff --git a/main.c b/main.c
index 89090ba22246f6e00292e3c1e4089c8b49b5a93f..4bdde47506d883b80e00f4b1241efe7af8808fad 100644 (file)
--- a/main.c
+++ b/main.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2012 
+ * Copyright (C) 2012
  *     Dale Weiler
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy of
@@ -33,32 +33,35 @@ bool opts_omit_nullcode             = false;
 int  opts_compiler                  = COMPILER_GMQCC;
 
 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");
+    printf("usage:\n"
+           "    %s -c<file>          -oprog.dat -- compile file\n"
+           "    %s -a<file>          -oprog.dat -- assemble file\n"
+           "    %s -c<file> -i<file> -oprog.dat -- compile together (allowed multiple -i<file>)\n"
+           "    %s -a<file> -i<file> -oprog.dat -- assemble together(allowed multiple -i<file>)\n"
+           "    example:\n"
+           "    %s -cfoo.qc -ibar.qc -oqc.dat -afoo.qs -ibar.qs -oqs.dat\n"
+           "    additional flags:\n"
+           "        -debug           -- turns on compiler debug messages\n"
+           "        -memchk          -- turns on compiler memory leak check\n"
+           "        -help            -- prints this help/usage text\n"
+           "        -std             -- select the QuakeC compile type (types below):\n"
+           "            -std=qcc     -- original QuakeC\n"
+           "            -std=ftqecc  -- fteqcc QuakeC\n"
+           "            -std=qccx    -- qccx QuakeC\n"
+           "            -std=gmqcc   -- this compiler QuakeC (default selection)\n"
+           "    codegen flags:\n"
+           "        -fdarkplaces-string-table-bug -- patches the string table to work with bugged versions of darkplaces\n"
+           "        -fomit-nullcode               -- omits the generation of null code (will break everywhere see propsal.txt)\n",
+            app,app,app,app,app
+    );
     return -1;
 }
 
 int main(int argc, char **argv) {
-    size_t itr = 0;
-    char  *app = &argv[0][0];
-    FILE  *fpp = NULL;
+    size_t    itr = 0;
+    char     *app = &argv[0][0];
+    FILE     *fpp = NULL;
+    lex_file *lex = NULL;
 
     /*
      * Parse all command line arguments.  This is rather annoying to do
@@ -69,9 +72,36 @@ int main(int argc, char **argv) {
 
     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 */
+            case 'v': {
+                printf("GMQCC:\n"
+                       "    version:    %d.%d.%d (0x%08X)\n"
+                       "    build date: %s\n"
+                       "    build time: %s\n",
+                    (GMQCC_VERSION >> 16) & 0xFF,
+                    (GMQCC_VERSION >>  8) & 0xFF,
+                    (GMQCC_VERSION >>  0) & 0xFF,
+                    (GMQCC_VERSION),
+                    __DATE__,
+                    __TIME__
+                );
+                return 0;
+            }
+            #define param_argument(argtype) do {                             \
+                if (argv[1][2]) {                                            \
+                    items_add((argitem){util_strdup(&argv[1][2]), argtype}); \
+                } else {                                                     \
+                    ++argv;                                                  \
+                    --argc;                                                  \
+                    if (argc <= 1)                                           \
+                        goto clean_params_usage;                             \
+                    items_add((argitem){util_strdup(&argv[1][0]), argtype}); \
+                }                                                            \
+            } while (0)
+
+            case 'c': param_argument(0); break; /* compile  */
+            case 'a': param_argument(1); break; /* assemble */
+            case 'i': param_argument(2); break; /* includes */
+            #undef parm_argument
             default:
                 if (!strncmp(&argv[1][1], "debug" , 5)) { opts_debug  = true; break; }
                 if (!strncmp(&argv[1][1], "memchk", 6)) { opts_memchk = true; break; }
@@ -85,11 +115,11 @@ int main(int argc, char **argv) {
                 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");
+                    printf("invalid std selection, supported types:\n"
+                           "    -std=qcc     -- original QuakeC\n"
+                           "    -std=ftqecc  -- fteqcc QuakeC\n"
+                           "    -std=qccx    -- qccx QuakeC\n"
+                           "    -std=gmqcc   -- this compiler QuakeC (default selection)\n");
                     return 0;
                 }
 
@@ -103,7 +133,7 @@ int main(int argc, char **argv) {
                     break;
                 }
                 return usage(app);
-                
+
         }
         ++argv;
         --argc;
@@ -113,7 +143,7 @@ int main(int argc, char **argv) {
      * options could depend on another option, this is where option
      * validity checking like that would take place.
      */
-    if (opts_memchk && !opts_debug) 
+    if (opts_memchk && !opts_debug)
         printf("Warning: cannot enable -memchk, without -debug.\n");
 
     util_debug("COM", "starting ...\n");
@@ -121,9 +151,8 @@ int main(int argc, char **argv) {
     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_init (items_data[itr].name, &lex);
+                lex_parse(lex);
                 lex_close(lex);
                 break;
             case 1:
@@ -134,7 +163,7 @@ int main(int argc, char **argv) {
         }
     }
 
-    util_debug("COM", "cleaning ...\n"); 
+    util_debug("COM", "cleaning ...\n");
     /* clean list */
     for (itr = 0; itr < items_elements; itr++)
         mem_d(items_data[itr].name);
@@ -142,4 +171,9 @@ int main(int argc, char **argv) {
 
     util_meminfo();
     return 0;
+clean_params_usage:
+    for (itr = 0; itr < items_elements; itr++)
+        mem_d(items_data[itr].name);
+    mem_d(items_data);
+    return usage(app);
 }