]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - main.c
even in non-qcc mode we need to search the variables for fields because of field...
[xonotic/gmqcc.git] / main.c
diff --git a/main.c b/main.c
index ca404f9db25f68027fc986416719260efcc44e5f..272c493d3c54056c1bad2348dc294b2f59129d94 100644 (file)
--- a/main.c
+++ b/main.c
@@ -30,6 +30,9 @@ const char *opts_output   = "progs.dat";
 int         opts_standard = COMPILER_GMQCC;
 bool        opts_debug    = false;
 bool        opts_memchk   = false;
+bool        opts_dump     = false;
+
+static bool opts_output_wasset = false;
 
 typedef struct { char *filename; int type; } argitem;
 VECTOR_MAKE(argitem, items);
@@ -68,36 +71,6 @@ static int usage() {
     return -1;
 }
 
-static void strtocmd(char *str)
-{
-    for(; *str; ++str) {
-        if (*str == '-') {
-            *str = '_';
-            continue;
-        }
-        if (isalpha(*str) && !isupper(*str)) {
-            *str += 'A' - 'a';
-            continue;
-        }
-    }
-}
-
-static void strtononcmd(char *buf, const char *str)
-{
-    for(; *str; ++buf, ++str) {
-        if (*str == '_') {
-            *buf = '-';
-            continue;
-        }
-        if (isalpha(*str) && isupper(*str)) {
-            *buf = *str + 'a' - 'A';
-            continue;
-        }
-        *buf = *str;
-    }
-    *buf = 0;
-}
-
 static bool options_setflag_all(const char *name, bool on, uint32_t *flags, const opts_flag_def *list, size_t listsize) {
     size_t i;
 
@@ -209,6 +182,10 @@ static bool options_parse(int argc, char **argv) {
                 opts_debug = true;
                 continue;
             }
+            if (!strcmp(argv[0]+1, "dump")) {
+                opts_dump = true;
+                continue;
+            }
             if (!strcmp(argv[0]+1, "memchk")) {
                 opts_memchk = true;
                 continue;
@@ -223,16 +200,16 @@ static bool options_parse(int argc, char **argv) {
 
                 /* handle all -fflags */
                 case 'f':
-                    strtocmd(argv[0]+2);
+                    util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
                     if (!strcmp(argv[0]+2, "HELP")) {
                         printf("Possible flags:\n");
                         for (itr = 0; itr < COUNT_FLAGS; ++itr) {
-                            strtononcmd(buffer, opts_flag_list[itr].name);
+                            util_strtononcmd(opts_flag_list[itr].name, buffer, sizeof(buffer));
                             printf(" -f%s\n", buffer);
                         }
                         exit(0);
                     }
-                    else if (!strncmp(argv[0]+2, "NO-", 3)) {
+                    else if (!strncmp(argv[0]+2, "NO_", 3)) {
                         if (!options_setflag(argv[0]+5, false)) {
                             printf("unknown flag: %s\n", argv[0]+2);
                             return false;
@@ -244,21 +221,21 @@ static bool options_parse(int argc, char **argv) {
                     }
                     break;
                 case 'W':
-                    strtocmd(argv[0]+2);
+                    util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
                     if (!strcmp(argv[0]+2, "HELP")) {
                         printf("Possible warnings:\n");
                         for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
-                            strtononcmd(buffer, opts_warn_list[itr].name);
+                            util_strtononcmd(opts_warn_list[itr].name, buffer, sizeof(buffer));
                             printf(" -W%s\n", buffer);
                         }
                         exit(0);
                     }
-                    else if (!strcmp(argv[0]+2, "all")) {
+                    else if (!strcmp(argv[0]+2, "ALL")) {
                         for (itr = 0; itr < sizeof(opts_warn)/sizeof(opts_warn[0]); ++itr)
                             opts_warn[itr] = 0xFFFFFFFFL;
                         break;
                     }
-                    if (!strncmp(argv[0]+2, "no-", 3)) {
+                    if (!strncmp(argv[0]+2, "NO_", 3)) {
                         if (!options_setwarn(argv[0]+5, false)) {
                             printf("unknown warning: %s\n", argv[0]+2);
                             return false;
@@ -284,6 +261,7 @@ static bool options_parse(int argc, char **argv) {
                         return false;
                     }
                     opts_output = argarg;
+                    opts_output_wasset = true;
                     break;
 
                 case 'a':
@@ -311,10 +289,10 @@ static bool options_parse(int argc, char **argv) {
                     }
                     else {
             /* All long options with arguments */
-                        if (options_long_witharg("output", &argc, &argv, &argarg))
+                        if (options_long_witharg("output", &argc, &argv, &argarg)) {
                             opts_output = argarg;
-                        else
-                        {
+                            opts_output_wasset = true;
+                        } else {
                             printf("Unknown parameter: %s\n", argv[0]);
                             return false;
                         }
@@ -338,23 +316,85 @@ static bool options_parse(int argc, char **argv) {
     return true;
 }
 
+static void options_set(uint32_t *flags, size_t idx, bool on)
+{
+    longbit lb = LONGBIT(idx);
+#if 0
+    if (on)
+        flags[lb.idx] |= (1<<(lb.bit));
+    else
+        flags[lb.idx] &= ~(1<<(lb.bit));
+#else
+    if (on)
+        flags[0] |= (1<<(lb));
+    else
+        flags[0] &= ~(1<<(lb));
+#endif
+}
+
+/* returns the line number, or -1 on error */
+static bool progs_nextline(char **out, FILE *src)
+{
+    size_t alen;
+    int    len;
+    char  *line;
+    char  *start;
+    char  *end;
+
+    line = *out;
+    len = util_getline(&line, &alen, src);
+    if (len == -1)
+        return false;
+
+    /* start at first non-blank */
+    for (start = line; isspace(*start); ++start) {}
+    /* end at the first non-blank */
+    for (end = start;  *end && !isspace(*end);  ++end)   {}
+
+    *out = line;
+    /* move the actual filename to the beginning */
+    while (start != end) {
+        *line++ = *start++;
+    }
+    *line = 0;
+    return true;
+}
+
 int main(int argc, char **argv) {
     size_t itr;
+    int retval = 0;
+    bool opts_output_free = false;
+
     app_name = argv[0];
 
+    /* default options / warn flags */
+    options_set(opts_warn, WARN_UNKNOWN_CONTROL_SEQUENCE, true);
+    options_set(opts_warn, WARN_EXTENSIONS, true);
+    options_set(opts_warn, WARN_FIELD_REDECLARED, true);
+
     if (!options_parse(argc, argv)) {
         return usage();
     }
 
-    for (itr = 0; itr < COUNT_FLAGS; ++itr) {
-        printf("Flag %s = %i\n", opts_flag_list[itr].name, OPTS_FLAG(itr));
+    if (opts_dump) {
+        for (itr = 0; itr < COUNT_FLAGS; ++itr) {
+            printf("Flag %s = %i\n", opts_flag_list[itr].name, OPTS_FLAG(itr));
+        }
+        for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
+            printf("Warning %s = %i\n", opts_warn_list[itr].name, OPTS_WARN(itr));
+        }
+        printf("output = %s\n", opts_output);
+        printf("optimization level = %i\n", (int)opts_O);
+        printf("standard = %i\n", opts_standard);
     }
-    for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
-        printf("Warning %s = %i\n", opts_warn_list[itr].name, OPTS_WARN(itr));
+
+    if (!parser_init()) {
+        printf("failed to initialize parser\n");
+        retval = 1;
+        goto cleanup;
     }
-    printf("output = %s\n", opts_output);
-    printf("optimization level = %i\n", (int)opts_O);
-    printf("standard = %i\n", opts_standard);
+
+    util_debug("COM", "starting ...\n");
 
     if (items_elements) {
         printf("Mode: manual\n");
@@ -366,17 +406,64 @@ int main(int argc, char **argv) {
                      (items_data[itr].type == TYPE_ASM ? "asm" :
                      (items_data[itr].type == TYPE_SRC ? "progs.src" :
                      ("unknown"))))));
+
+            if (!parser_compile(items_data[itr].filename)) {
+                retval = 1;
+                goto cleanup;
+            }
         }
+
+        parser_finish(opts_output);
     } else {
+        FILE *src;
+        char *line;
+
         printf("Mode: progs.src\n");
-    }
+        src = util_fopen("progs.src", "rb");
+        if (!src) {
+            printf("failed to open `progs.src` for reading\n");
+            retval = 1;
+            goto cleanup;
+        }
 
-    util_debug("COM", "starting ...\n");
+        line = NULL;
+        if (!progs_nextline(&line, src) || !line[0]) {
+            printf("illformatted progs.src file: expected output filename in first line\n");
+            retval = 1;
+            goto srcdone;
+        }
+
+        if (!opts_output_wasset) {
+            opts_output = util_strdup(line);
+            opts_output_free = true;
+        }
+
+        while (progs_nextline(&line, src)) {
+            if (!line[0] || (line[0] == '/' && line[1] == '/'))
+                continue;
+            printf("  src: %s\n", line);
+            if (!parser_compile(line)) {
+                retval = 1;
+                goto srcdone;
+            }
+        }
+
+        parser_finish(opts_output);
+
+srcdone:
+        fclose(src);
+        mem_d(line);
+    }
 
     /* stuff */
 
+cleanup:
     util_debug("COM", "cleaning ...\n");
 
+    parser_cleanup();
+    if (opts_output_free)
+        mem_d((char*)opts_output);
+
     util_meminfo();
-    return 0;
+    return retval;
 }