]> git.xonotic.org Git - xonotic/gmqcc.git/commitdiff
Commandline handling first draft
authorWolfgang Bumiller <wolfgang.linux@bumiller.com>
Sat, 28 Jul 2012 10:36:20 +0000 (12:36 +0200)
committerWolfgang Bumiller <wolfgang.linux@bumiller.com>
Sat, 28 Jul 2012 10:36:20 +0000 (12:36 +0200)
gmqcc.h
main.c

diff --git a/gmqcc.h b/gmqcc.h
index 90d1e05b98e0830763d74d9ed116d4d39efd947a..072048d4e20de340e88ec49ff2f8ac5c10086ca1 100644 (file)
--- a/gmqcc.h
+++ b/gmqcc.h
@@ -960,4 +960,50 @@ prog_section_def* prog_getdef    (qc_program *prog, qcint off);
 qcany*            prog_getedict  (qc_program *prog, qcint e);
 qcint             prog_tempstring(qc_program *prog, const char *_str);
 
+/*===================================================================*/
+/*======================= main.c commandline ========================*/
+/*===================================================================*/
+
+#if 0
+/* Helpers to allow for a whole lot of flags. Otherwise we'd limit
+ * to 32 or 64 -f options...
+ */
+typedef struct {
+    size_t  idx; /* index into an array of 32 bit words */
+    uint8_t bit; /* index _into_ the 32 bit word, thus just uint8 */
+} longbit;
+#define LONGBIT(bit) { ((bit)/32), ((bit)%32) }
+#else
+typedef uint32_t longbit;
+#define LONGBIT(bit) (bit)
+#endif
+
+/* Used to store the list of flags with names */
+typedef struct {
+    const char *name;
+    longbit    bit;
+} opt_flag_def;
+
+/*===================================================================*/
+/* list of -f flags, like -fdarkplaces-string-table-bug */
+enum {
+    DP_STRING_TABLE_BUG,
+    OMIT_NULLBYTES,
+
+    NUM_F_FLAGS
+};
+static const opt_flag_def opt_flag_list[] = {
+    { "darkplaces-string-table-bug", LONGBIT(DP_STRING_TABLE_BUG) },
+    { "omit-nullbytes",              LONGBIT(OMIT_NULLBYTES)      },
+};
+static const size_t opt_flag_list_count = sizeof(opt_flag_list) / sizeof(opt_flag_list[0]);
+
+/* other options: */
+extern uint32_t    opt_O;      /* -Ox */
+extern const char *opt_output; /* -o file */
+
+/*===================================================================*/
+#define OPT_FLAG(i) (!! (opt_flags[(i)/32] & (1<< ((i)%32))))
+extern uint32_t opt_flags[1 + (NUM_F_FLAGS / 32)];
+
 #endif
diff --git a/main.c b/main.c
index 1ffcd998927a437459041cd79a53195b72a38465..60aa8bc4e1f8e76ea4430221d995a507e598572e 100644 (file)
--- a/main.c
+++ b/main.c
  * SOFTWARE.
  */
 #include "gmqcc.h"
-typedef struct { char *name, type; } argitem;
-VECTOR_MAKE(argitem, items);
-
-static int usage(const char *app) {
-    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", app, app, app, app, app);
-
-    printf("    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");
-
-    printf("            -std=qcc     -- original QuakeC\n"
-           "            -std=ftqecc  -- fteqcc QuakeC\n"
-           "            -std=qccx    -- qccx QuakeC\n"
-           "            -std=gmqcc   -- this compiler QuakeC (default selection)\n");
-
-    printf("    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");
+
+uint32_t    opt_flags[1 + (NUM_F_FLAGS / 32)];
+uint32_t    opt_O = 1;
+const char *opt_output = "progs.dat";
+
+static int usage() {
+    printf("Usage:\n");
     return -1;
 }
 
-int main(int argc, char **argv) {
-    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
-     * 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 '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 {                             \
-                argitem item;                                                \
-                if (argv[1][2]) {                                            \
-                    item.name = util_strdup(&argv[1][2]);                    \
-                    item.type = argtype;                                     \
-                    items_add(item);                                         \
-                } else {                                                     \
-                    ++argv;                                                  \
-                    --argc;                                                  \
-                    if (argc <= 1)                                           \
-                        goto clean_params_usage;                             \
-                    item.name = util_strdup(argv[1]);                        \
-                    item.type = argtype;                                     \
-                    items_add(item);                                         \
-                }                                                            \
-            } 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 (util_strncmpexact(&argv[1][1], "debug" , 5)) { opts_debug  = true; break; }
-                if (util_strncmpexact(&argv[1][1], "memchk", 6)) { opts_memchk = true; break; }
-                if (util_strncmpexact(&argv[1][1], "help",   4)) {
-                    return usage(app);
-                }
-                /* compiler type selection */
-                if (util_strncmpexact(&argv[1][1], "std=qcc"   , 7 )) { opts_compiler = COMPILER_QCC;    break; }
-                if (util_strncmpexact(&argv[1][1], "std=fteqcc", 10)) { opts_compiler = COMPILER_FTEQCC; break; }
-                if (util_strncmpexact(&argv[1][1], "std=qccx",   8 )) { opts_compiler = COMPILER_QCCX;   break; }
-                if (util_strncmpexact(&argv[1][1], "std=gmqcc",  9 )) { opts_compiler = COMPILER_GMQCC;  break; }
-                if (util_strncmpexact(&argv[1][1], "std=",       4 )) {
-                    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;
-                }
-
-                /* code specific switches */
-                if (util_strncmpexact(&argv[1][1], "fdarkplaces-stringtablebug", 26)) {
-                    opts_darkplaces_stringtablebug = true;
+static bool options_setflag(const char *name, bool on) {
+    size_t i;
+
+    for (i = 0; i < opt_flag_list_count; ++i) {
+        if (!strcmp(name, opt_flag_list[i].name)) {
+            longbit lb = opt_flag_list[i].bit;
+#if 0
+            if (on)
+                opt_flags[lb.idx] |= (1<<(lb.bit));
+            else
+                opt_flags[lb.idx] &= ~(1<<(lb.bit));
+#else
+            if (on)
+                opt_flags[0] |= (1<<lb);
+            else
+                opt_flags[0] &= ~(1<<(lb));
+#endif
+            return true;
+        }
+    }
+    return false;
+}
+
+static bool options_witharg(int *argc_, char ***argv_, char **out) {
+    int  argc   = *argc_;
+    char **argv = *argv_;
+
+    if (argv[0][2]) {
+        *out = argv[0]+2;
+        return true;
+    }
+    /* eat up the next */
+    if (argc < 2) /* no parameter was provided */
+        return false;
+
+    *out = argv[1];
+    --*argc_;
+    ++*argv_;
+    return true;
+}
+
+static bool options_long_witharg(const char *optname, int *argc_, char ***argv_, char **out) {
+    int  argc   = *argc_;
+    char **argv = *argv_;
+
+    size_t len = strlen(optname);
+
+    if (strncmp(argv[0]+2, optname, len))
+        return false;
+
+    /* it's --optname, check how the parameter is supplied */
+    if (argv[0][2+len] == '=') {
+        /* using --opt=param */
+        *out = argv[0]+2+len+1;
+        return true;
+    }
+
+    if (argc < 2) /* no parameter was provided */
+        return false;
+
+    /* using --opt param */
+    *out = argv[1];
+    --*argc_;
+    ++*argv_;
+    return true;
+}
+
+static bool options_parse(int argc, char **argv) {
+    bool argend = false;
+    while (!argend && argc) {
+        char *argarg;
+        if (argv[0][0] == '-') {
+            switch (argv[0][1]) {
+                /* -h, show usage but exit with 0 */
+                case 'h':
+                    usage();
+                    exit(0);
+                    break;
+
+                /* handle all -fflags */
+                case 'f':
+                    if (!strncmp(argv[0]+2, "no-", 3)) {
+                        if (!options_setflag(argv[0]+5, false)) {
+                            printf("unknown flag: %s\n", argv[0]+2);
+                            return false;
+                        }
+                    }
+                    else if (!options_setflag(argv[0]+2, true)) {
+                        printf("unknown flag: %s\n", argv[0]+2);
+                        return false;
+                    }
+                    break;
+
+                case 'O':
+                    if (!options_witharg(&argc, &argv, &argarg)) {
+                        printf("option -O requires a numerical argument\n");
+                        return false;
+                    }
+                    opt_O = atoi(argarg);
                     break;
-                }
-                if (util_strncmpexact(&argv[1][1], "fomit-nullcode", 14)) {
-                    opts_omit_nullcode = true;
+
+                case 'o':
+                    if (!options_witharg(&argc, &argv, &argarg)) {
+                        printf("option -o requires an argument: the output file name\n");
+                        return false;
+                    }
+                    opt_output = argarg;
+                    break;
+
+                case '-':
+                    if (!argv[0][2]) {
+                        /* anything following -- is considered a non-option argument */
+                        argend = true;
+                        break;
+                    }
+            /* All long options without arguments */
+                    else if (!strcmp(argv[0]+2, "help")) {
+                        usage();
+                        exit(0);
+                    }
+                    else {
+            /* All long options with arguments */
+                        if (options_long_witharg("output", &argc, &argv, &argarg))
+                            opt_output = argarg;
+                        else
+                        {
+                            printf("Unknown parameter: %s\n", argv[0]);
+                            return false;
+                        }
+                    }
                     break;
-                }
-                return printf("invalid command line argument: %s\n",argv[1]);
 
+                default:
+                    break;
+            }
         }
         ++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:
-                lex_init (items_data[itr].name, &lex);
-                if (lex) {
-                    lex_parse(lex);
-                    lex_close(lex);
-                }
-                break;
-            case 1:
-                asm_init (items_data[itr].name, &fpp);
-                if (fpp) {
-                    asm_parse(fpp);
-                    asm_close(fpp);
-                }
-                break;
-        }
+    return true;
+}
+
+int main(int argc, char **argv) {
+    /* char     *app = &argv[0][0]; */
+
+    if (!options_parse(argc-1, argv+1)) {
+        return usage();
     }
 
+    util_debug("COM", "starting ...\n");
+
+    /* stuff */
+
     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;
-
-clean_params_usage:
-    for (itr = 0; itr < items_elements; itr++)
-        mem_d(items_data[itr].name);
-    mem_d(items_data);
-    return usage(app);
 }