]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - main.cpp
fixes for progs.src
[xonotic/gmqcc.git] / main.cpp
index ac715d5b4186bc1d18cff1e6a2b16a154dff1dc3..1080f8f7fde7f1cf20619c8a1bdb7f3682f246af 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -3,18 +3,18 @@
 
 #include "gmqcc.h"
 #include "lexer.h"
+#include "parser.h"
 
 /* TODO: cleanup this whole file .. it's a fuckign mess */
 
 /* set by the standard */
-const oper_info *operators      = NULL;
-size_t           operator_count = 0;
-static bool      opts_output_wasset = false;
-
-typedef struct { char *filename; int   type;  } argitem;
-typedef struct { char *name;     char *value; } ppitem;
-static argitem *items = NULL;
-static ppitem  *ppems = NULL;
+const oper_info *operators = nullptr;
+size_t operator_count = 0;
+static bool opts_output_wasset = false;
+struct argitem { char *filename; int type; };
+struct ppitem { char *name; char *value; };
+static argitem *items = nullptr;
+static ppitem  *ppems = nullptr;
 
 #define TYPE_QC  0
 #define TYPE_ASM 1
@@ -117,11 +117,11 @@ static bool options_long_gcc(const char *optname, int *argc_, char ***argv_, cha
     return options_long_witharg_all(optname, argc_, argv_, out, 1, false);
 }
 
-static bool options_parse(int argc, char **argv) {
+static bool options_parse(int argc, char **argv, bool *has_progs_src) {
     bool argend = false;
     size_t itr;
     char buffer[1024];
-    char *config = NULL;
+    char *config = nullptr;
 
     while (!argend && argc > 1) {
         char *argarg;
@@ -188,11 +188,11 @@ static bool options_parse(int argc, char **argv) {
             if (options_long_gcc("force-crc", &argc, &argv, &argarg)) {
 
                 OPTS_OPTION_BOOL(OPTION_FORCECRC)   = true;
-                OPTS_OPTION_U16 (OPTION_FORCED_CRC) = strtol(argarg, NULL, 0);
+                OPTS_OPTION_U16 (OPTION_FORCED_CRC) = strtol(argarg, nullptr, 0);
                 continue;
             }
             if (options_long_gcc("state-fps", &argc, &argv, &argarg)) {
-                OPTS_OPTION_U32(OPTION_STATE_FPS) = strtol(argarg, NULL, 0);
+                OPTS_OPTION_U32(OPTION_STATE_FPS) = strtol(argarg, nullptr, 0);
                 opts_set(opts.flags, EMULATE_STATE, true);
                 continue;
             }
@@ -202,6 +202,7 @@ static bool options_parse(int argc, char **argv) {
             }
             if (options_long_gcc("progsrc", &argc, &argv, &argarg)) {
                 OPTS_OPTION_STR(OPTION_PROGSRC) = argarg;
+                *has_progs_src = true;
                 continue;
             }
 
@@ -283,7 +284,7 @@ static bool options_parse(int argc, char **argv) {
 
                     if (!(argarg = strchr(argv[0] + 2, '='))) {
                         macro.name  = util_strdup(argv[0]+2);
-                        macro.value = NULL;
+                        macro.value = nullptr;
                     } else {
                         *argarg='\0'; /* terminate for name */
                         macro.name  = util_strdup(argv[0]+2);
@@ -384,7 +385,7 @@ static bool options_parse(int argc, char **argv) {
                         return false;
                     }
                     if (util_isdigit(argarg[0])) {
-                        uint32_t val = (uint32_t)strtol(argarg, NULL, 10);
+                        uint32_t val = (uint32_t)strtol(argarg, nullptr, 10);
                         OPTS_OPTION_U32(OPTION_O) = val;
                         opts_setoptimlevel(val);
                     } else {
@@ -434,6 +435,9 @@ static bool options_parse(int argc, char **argv) {
                     }
                     item.filename = argarg;
                     vec_push(items, item);
+                    if (item.type == TYPE_SRC) {
+                        *has_progs_src = true;
+                    }
                     break;
 
                 case '-':
@@ -515,13 +519,13 @@ static bool progs_nextline(char **out, size_t *alen, FILE *src) {
 }
 
 int main(int argc, char **argv) {
-    size_t          itr;
-    int             retval           = 0;
-    bool            operators_free   = false;
-    bool            progs_src        = false;
-    FILE       *outfile         = NULL;
-    struct parser_s *parser          = NULL;
-    struct ftepp_s  *ftepp           = NULL;
+    size_t itr;
+    int retval = 0;
+    bool operators_free = false;
+    bool has_progs_src = false;
+    FILE *outfile = nullptr;
+    parser_t *parser = nullptr;
+    ftepp_t *ftepp = nullptr;
 
     app_name = argv[0];
     con_init ();
@@ -529,7 +533,7 @@ int main(int argc, char **argv) {
 
     util_seed(time(0));
 
-    if (!options_parse(argc, argv)) {
+    if (!options_parse(argc, argv, &has_progs_src)) {
         return usage();
     }
 
@@ -623,13 +627,19 @@ int main(int argc, char **argv) {
         }
     }
 
-    if (!vec_size(items)) {
-        FILE *src;
-        char      *line    = NULL;
-        size_t     linelen = 0;
-        bool       hasline = false;
+    if (!vec_size(items) && !has_progs_src) {
+      FILE *fp = fopen(OPTS_OPTION_STR(OPTION_PROGSRC), "rb");
+      if (fp) {
+        has_progs_src = true;
+      }
+      fclose(fp);
+    }
 
-        progs_src = true;
+    if (has_progs_src) {
+        FILE *src;
+        char *line = nullptr;
+        size_t linelen = 0;
+        bool has_first_line = false;
 
         src = fopen(OPTS_OPTION_STR(OPTION_PROGSRC), "rb");
         if (!src) {
@@ -641,16 +651,19 @@ int main(int argc, char **argv) {
         while (progs_nextline(&line, &linelen, src)) {
             argitem item;
 
-            if (!line[0] || (line[0] == '/' && line[1] == '/'))
+            if (!line[0] || (line[0] == '/' && line[1] == '/')) {
                 continue;
+            }
 
-            if (hasline) {
+            if (has_first_line) {
                 item.filename = util_strdup(line);
-                item.type     = TYPE_QC;
+                item.type = TYPE_QC;
                 vec_push(items, item);
-            } else if (!opts_output_wasset) {
-                OPTS_OPTION_DUP(OPTION_OUTPUT) = util_strdup(line);
-                hasline                        = true;
+            } else {
+                if (!opts_output_wasset) {
+                    OPTS_OPTION_DUP(OPTION_OUTPUT) = util_strdup(line);
+                }
+                has_first_line = true;
             }
         }
 
@@ -662,7 +675,7 @@ int main(int argc, char **argv) {
         if (!OPTS_OPTION_BOOL(OPTION_QUIET) &&
             !OPTS_OPTION_BOOL(OPTION_PP_ONLY))
         {
-            con_out("Mode: %s\n", (progs_src ? "progs.src" : "manual"));
+            con_out("Mode: %s\n", (has_progs_src ? "progs.src" : "manual"));
             con_out("There are %lu items to compile:\n", (unsigned long)vec_size(items));
         }
 
@@ -678,6 +691,10 @@ int main(int argc, char **argv) {
                          ("unknown"))))));
             }
 
+            if (items[itr].type == TYPE_SRC) {
+                continue;
+            }
+
             if (OPTS_OPTION_BOOL(OPTION_PP_ONLY)) {
                 const char *out;
                 if (!ftepp_preprocess_file(ftepp, items[itr].filename)) {
@@ -713,14 +730,14 @@ int main(int argc, char **argv) {
                 }
             }
 
-            if (progs_src) {
+            if (has_progs_src) {
                 mem_d(items[itr].filename);
-                items[itr].filename = NULL;
+                items[itr].filename = nullptr;
             }
         }
 
         ftepp_finish(ftepp);
-        ftepp = NULL;
+        ftepp = nullptr;
         if (!OPTS_OPTION_BOOL(OPTION_PP_ONLY)) {
             if (!parser_finish(parser, OPTS_OPTION_STR(OPTION_OUTPUT))) {
                 retval = 1;
@@ -737,7 +754,7 @@ cleanup:
     vec_free(ppems);
 
     if (!OPTS_OPTION_BOOL(OPTION_PP_ONLY))
-        if(parser) parser_cleanup(parser);
+        delete parser;
 
     /* free allocated option strings */
     for (itr = 0; itr < OPTION_COUNT; itr++)