]> git.xonotic.org Git - xonotic/gmqcc.git/commitdiff
Make -E print to stdout by default and honor -o, changed handling of source list...
authorWolfgang (Blub) Bumiller <blub@speed.at>
Sun, 18 Nov 2012 11:51:38 +0000 (12:51 +0100)
committerWolfgang (Blub) Bumiller <blub@speed.at>
Sun, 18 Nov 2012 11:51:38 +0000 (12:51 +0100)
ftepp.c
gmqcc.h
main.c

diff --git a/ftepp.c b/ftepp.c
index 865cd4e6f5e0867160de6bda34a7030099002d41..4318633eb22226f66a70d1a7d013b6e1446eff93 100644 (file)
--- a/ftepp.c
+++ b/ftepp.c
@@ -62,8 +62,9 @@ typedef struct {
     ppcondition *conditions;
     ppmacro    **macros;
 
-    bool         output_string;
+    bool         to_string;
     char        *output;
+    FILE        *output_file;
 } ftepp_t;
 
 #define ftepp_tokval(f) ((f)->lex->tok.value)
@@ -171,7 +172,10 @@ static void ftepp_delete(ftepp_t *self)
         ppmacro_delete(self->macros[i]);
     vec_free(self->macros);
     vec_free(self->conditions);
-    lex_close(self->lex);
+    if (self->lex)
+        lex_close(self->lex);
+    if (self->output_file)
+        fclose(self->output_file);
     mem_d(self);
 }
 
@@ -181,8 +185,8 @@ static void ftepp_out(ftepp_t *ftepp, const char *str, bool ignore_cond)
     {
         size_t len;
         char  *data;
-        if (!ftepp->output_string) {
-            printf("%s", str);
+        if (!ftepp->to_string) {
+            fprintf((ftepp->output_file ? ftepp->output_file : stdout), "%s", str);
             return;
         }
         len = strlen(str);
@@ -446,7 +450,7 @@ static bool ftepp_preprocess(ftepp_t *ftepp);
 static bool ftepp_macro_expand(ftepp_t *ftepp, ppmacro *macro, macroparam *params)
 {
     char     *old_string = ftepp->output;
-    bool      old_string_flag = ftepp->output_string;
+    bool      old_string_flag = ftepp->to_string;
     lex_file *old_lexer = ftepp->lex;
     bool retval = true;
 
@@ -457,8 +461,8 @@ static bool ftepp_macro_expand(ftepp_t *ftepp, ppmacro *macro, macroparam *param
     if (!vec_size(macro->output))
         return true;
 
-    ftepp->output = NULL;
-    ftepp->output_string = true;
+    ftepp->output    = NULL;
+    ftepp->to_string = true;
     for (o = 0; o < vec_size(macro->output); ++o) {
         pptoken *out = macro->output[o];
         switch (out->token) {
@@ -505,8 +509,8 @@ static bool ftepp_macro_expand(ftepp_t *ftepp, ppmacro *macro, macroparam *param
         retval = false;
         goto cleanup;
     }
-    ftepp->output        = old_string;
-    ftepp->output_string = old_string_flag;
+    ftepp->output    = old_string;
+    ftepp->to_string = old_string_flag;
     ftepp->lex = inlex;
     if (!ftepp_preprocess(ftepp)) {
         lex_close(ftepp->lex);
@@ -515,9 +519,9 @@ static bool ftepp_macro_expand(ftepp_t *ftepp, ppmacro *macro, macroparam *param
     }
 
 cleanup:
-    ftepp->lex           = old_lexer;
-    ftepp->output        = old_string;
-    ftepp->output_string = old_string_flag;
+    ftepp->lex       = old_lexer;
+    ftepp->output    = old_string;
+    ftepp->to_string = old_string_flag;
     return retval;
 }
 
@@ -1052,13 +1056,17 @@ bool ftepp_preprocess_string(const char *name, const char *str)
     return ftepp_preprocess_done();
 }
 
-bool ftepp_init()
+bool ftepp_init(FILE *out)
 {
     ftepp = ftepp_new();
+    ftepp->output_file = out;
     return !!ftepp;
 }
 
 void ftepp_finish()
 {
+    if (!ftepp)
+        return;
     ftepp_delete(ftepp);
+    ftepp = NULL;
 }
diff --git a/gmqcc.h b/gmqcc.h
index d99a778ed174e507f175394b5b9029c518646cf7..c42197011c2617449aba7d2429e5b6184e8f99d0 100644 (file)
--- a/gmqcc.h
+++ b/gmqcc.h
@@ -755,7 +755,7 @@ void parser_cleanup       ();
 /*===================================================================*/
 /*====================== ftepp.c commandline ========================*/
 /*===================================================================*/
-bool ftepp_init             ();
+bool ftepp_init             (FILE *out);
 bool ftepp_preprocess_file  (const char *filename);
 bool ftepp_preprocess_string(const char *name, const char *str);
 void ftepp_finish           ();
diff --git a/main.c b/main.c
index d1ac7f12e46560dde15d5d94bd50e7dd23f9b733..1301e7549b70da667312bf89e2e27f6acb0e5731 100644 (file)
--- a/main.c
+++ b/main.c
@@ -415,6 +415,7 @@ int main(int argc, char **argv) {
     size_t itr;
     int retval = 0;
     bool opts_output_free = false;
+    bool progs_src = false;
 
     app_name = argv[0];
     con_init();
@@ -473,54 +474,31 @@ int main(int argc, char **argv) {
         }
     }
     if (opts_pp_only || opts_standard == COMPILER_FTEQCC) {
-        if (!ftepp_init()) {
-            con_err("failed to initialize parser\n");
-            retval = 1;
-            goto cleanup;
-        }
-    }
-
-    util_debug("COM", "starting ...\n");
-
-    if (vec_size(items)) {
-        if (!opts_pp_only) {
-            con_out("Mode: manual\n");
-            con_out("There are %lu items to compile:\n", (unsigned long)vec_size(items));
-        }
-        for (itr = 0; itr < vec_size(items); ++itr) {
-            if (!opts_pp_only) {
-                con_out("  item: %s (%s)\n",
-                       items[itr].filename,
-                       ( (items[itr].type == TYPE_QC ? "qc" :
-                         (items[itr].type == TYPE_ASM ? "asm" :
-                         (items[itr].type == TYPE_SRC ? "progs.src" :
-                         ("unknown"))))));
-            }
-
-            if (opts_pp_only) {
-                if (!ftepp_preprocess_file(items[itr].filename)) {
-                    retval = 1;
-                    goto cleanup;
-                }
-            }
-            else if (!parser_compile_file(items[itr].filename)) {
+        FILE *out = NULL;
+        if (opts_output_wasset) {
+            out = util_fopen(opts_output, "wb");
+            if (!out) {
+                con_err("failed to open `%s` for writing\n", opts_output);
                 retval = 1;
                 goto cleanup;
             }
         }
-
-        if (!parser_finish(opts_output)) {
+        if (!ftepp_init(out)) {
+            con_err("failed to initialize parser\n");
             retval = 1;
             goto cleanup;
         }
+    }
 
-    } else {
+    util_debug("COM", "starting ...\n");
+
+    if (!vec_size(items)) {
         FILE *src;
         char *line;
         size_t linelen = 0;
 
-        if (!opts_pp_only)
-            con_out("Mode: progs.src\n");
+        progs_src = true;
+
         src = util_fopen("progs.src", "rb");
         if (!src) {
             con_err("failed to open `progs.src` for reading\n");
@@ -541,27 +519,68 @@ int main(int argc, char **argv) {
         }
 
         while (progs_nextline(&line, &linelen, src)) {
+            argitem item;
             if (!line[0] || (line[0] == '/' && line[1] == '/'))
                 continue;
-            if (!opts_pp_only)
-                con_out("  src: %s\n", line);
-            if (!parser_compile_file(line)) {
-                retval = 1;
-                goto srcdone;
-            }
+            item.filename = util_strdup(line);
+            item.type     = TYPE_QC;
+            vec_push(items, item);
         }
 
-        parser_finish(opts_output);
-
 srcdone:
         fclose(src);
         mem_d(line);
     }
 
+    if (retval)
+        goto cleanup;
+
+    if (vec_size(items)) {
+        if (!opts_pp_only) {
+            con_out("Mode: %s\n", (progs_src ? "progs.src" : "manual"));
+            con_out("There are %lu items to compile:\n", (unsigned long)vec_size(items));
+        }
+        for (itr = 0; itr < vec_size(items); ++itr) {
+            if (!opts_pp_only) {
+                con_out("  item: %s (%s)\n",
+                       items[itr].filename,
+                       ( (items[itr].type == TYPE_QC ? "qc" :
+                         (items[itr].type == TYPE_ASM ? "asm" :
+                         (items[itr].type == TYPE_SRC ? "progs.src" :
+                         ("unknown"))))));
+            }
+
+            if (opts_pp_only) {
+                if (!ftepp_preprocess_file(items[itr].filename)) {
+                    retval = 1;
+                    goto cleanup;
+                }
+            }
+            else if (!parser_compile_file(items[itr].filename)) {
+                retval = 1;
+                goto cleanup;
+            }
+
+            if (progs_src) {
+                mem_d(items[itr].filename);
+                items[itr].filename = NULL;
+            }
+        }
+
+        ftepp_finish();
+        if (!opts_pp_only) {
+            if (!parser_finish(opts_output)) {
+                retval = 1;
+                goto cleanup;
+            }
+        }
+    }
+
     /* stuff */
 
 cleanup:
     util_debug("COM", "cleaning ...\n");
+    ftepp_finish();
     con_close();
     vec_free(items);