]> git.xonotic.org Git - xonotic/gmqcc.git/blob - main.c
More cleanup
[xonotic/gmqcc.git] / main.c
1 /*
2  * Copyright (C) 2012, 2013, 2014, 2015
3  *     Dale Weiler
4  *     Wolfgang Bumiller
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy of
7  * this software and associated documentation files (the "Software"), to deal in
8  * the Software without restriction, including without limitation the rights to
9  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10  * of the Software, and to permit persons to whom the Software is furnished to do
11  * so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "gmqcc.h"
28 #include "lexer.h"
29
30 /* TODO: cleanup this whole file .. it's a fuckign mess */
31
32 /* set by the standard */
33 const oper_info *operators      = NULL;
34 size_t           operator_count = 0;
35 static bool      opts_output_wasset = false;
36
37 typedef struct { char *filename; int   type;  } argitem;
38 typedef struct { char *name;     char *value; } ppitem;
39 static argitem *items = NULL;
40 static ppitem  *ppems = NULL;
41
42 #define TYPE_QC  0
43 #define TYPE_ASM 1
44 #define TYPE_SRC 2
45
46 static const char *app_name;
47
48 static void version(void) {
49     con_out("GMQCC %d.%d.%d Built %s %s\n" GMQCC_DEV_VERSION_STRING,
50         GMQCC_VERSION_MAJOR,
51         GMQCC_VERSION_MINOR,
52         GMQCC_VERSION_PATCH,
53         __DATE__,
54         __TIME__
55     );
56 }
57
58 static int usage(void) {
59     con_out("usage: %s [options] [files...]", app_name);
60     con_out("options:\n"
61             "  -h, --help             show this help message\n"
62             "  -debug                 turns on compiler debug messages\n"
63             "  -memchk                turns on compiler memory leak check\n");
64     con_out("  -o, --output=file      output file, defaults to progs.dat\n"
65             "  -s filename            add a progs.src file to be used\n");
66     con_out("  -E                     stop after preprocessing\n");
67     con_out("  -q, --quiet            be less verbose\n");
68     con_out("  -config file           use the specified ini file\n");
69     con_out("  -std=standard          select one of the following standards\n"
70             "       -std=qcc          original QuakeC\n"
71             "       -std=fteqcc       fteqcc QuakeC\n"
72             "       -std=gmqcc        this compiler (default)\n");
73     con_out("  -f<flag>               enable a flag\n"
74             "  -fno-<flag>            disable a flag\n"
75             "  -fhelp                 list possible flags\n");
76     con_out("  -W<warning>            enable a warning\n"
77             "  -Wno-<warning>         disable a warning\n"
78             "  -Wall                  enable all warnings\n");
79     con_out("  -Werror                treat warnings as errors\n"
80             "  -Werror-<warning>      treat a warning as error\n"
81             "  -Wno-error-<warning>   opposite of the above\n");
82     con_out("  -Whelp                 list possible warnings\n");
83     con_out("  -O<number>             optimization level\n"
84             "  -O<name>               enable specific optimization\n"
85             "  -Ono-<name>            disable specific optimization\n"
86             "  -Ohelp                 list optimizations\n");
87     con_out("  -force-crc=num         force a specific checksum into the header\n");
88     con_out("  -state-fps=num         emulate OP_STATE with the specified FPS\n");
89     con_out("  -coverage              add coverage support\n");
90     return -1;
91 }
92
93 /* command line parsing */
94 static bool options_witharg(int *argc_, char ***argv_, char **out) {
95     int  argc   = *argc_;
96     char **argv = *argv_;
97
98     if (argv[0][2]) {
99         *out = argv[0]+2;
100         return true;
101     }
102     /* eat up the next */
103     if (argc < 2) /* no parameter was provided */
104         return false;
105
106     *out = argv[1];
107     --*argc_;
108     ++*argv_;
109     return true;
110 }
111
112 static bool options_long_witharg_all(const char *optname, int *argc_, char ***argv_, char **out, int ds, bool split) {
113     int  argc   = *argc_;
114     char **argv = *argv_;
115
116     size_t len = strlen(optname);
117
118     if (strncmp(argv[0]+ds, optname, len))
119         return false;
120
121     /* it's --optname, check how the parameter is supplied */
122     if (argv[0][ds+len] == '=') {
123         /* using --opt=param */
124         *out = argv[0]+ds+len+1;
125         return true;
126     }
127
128     if (!split || argc < ds) /* no parameter was provided, or only single-arg form accepted */
129         return false;
130
131     /* using --opt param */
132     *out = argv[1];
133     --*argc_;
134     ++*argv_;
135     return true;
136 }
137 static bool options_long_witharg(const char *optname, int *argc_, char ***argv_, char **out) {
138     return options_long_witharg_all(optname, argc_, argv_, out, 2, true);
139 }
140 static bool options_long_gcc(const char *optname, int *argc_, char ***argv_, char **out) {
141     return options_long_witharg_all(optname, argc_, argv_, out, 1, false);
142 }
143
144 static bool options_parse(int argc, char **argv) {
145     bool argend = false;
146     size_t itr;
147     char  buffer[1024];
148     char *config      = NULL;
149     char *memdumpcols = NULL;
150
151     while (!argend && argc > 1) {
152         char *argarg;
153         argitem item;
154         ppitem  macro;
155
156         ++argv;
157         --argc;
158
159         if (argv[0][0] == '-') {
160             /* All gcc-type long options */
161             if (options_long_gcc("std", &argc, &argv, &argarg)) {
162                 if (!strcmp(argarg, "gmqcc") || !strcmp(argarg, "default")) {
163
164                     opts_set(opts.flags, ADJUST_VECTOR_FIELDS,          true);
165                     opts_set(opts.flags, CORRECT_LOGIC,                 true);
166                     opts_set(opts.flags, SHORT_LOGIC,                   true);
167                     opts_set(opts.flags, UNTYPED_NIL,                   true);
168                     opts_set(opts.flags, VARIADIC_ARGS,                 true);
169                     opts_set(opts.flags, FALSE_EMPTY_STRINGS,           false);
170                     opts_set(opts.flags, TRUE_EMPTY_STRINGS,            true);
171                     opts_set(opts.flags, LOOP_LABELS,                   true);
172                     opts_set(opts.flags, TRANSLATABLE_STRINGS,          true);
173                     opts_set(opts.flags, INITIALIZED_NONCONSTANTS,      true);
174                     opts_set(opts.werror, WARN_INVALID_PARAMETER_COUNT, true);
175                     opts_set(opts.werror, WARN_MISSING_RETURN_VALUES,   true);
176                     opts_set(opts.flags,  EXPRESSIONS_FOR_BUILTINS,     true);
177                     opts_set(opts.warn,   WARN_BREAKDEF,                true);
178
179
180
181                     OPTS_OPTION_U32(OPTION_STANDARD) = COMPILER_GMQCC;
182
183                 } else if (!strcmp(argarg, "qcc")) {
184
185                     opts_set(opts.flags, ADJUST_VECTOR_FIELDS,  false);
186                     opts_set(opts.flags, ASSIGN_FUNCTION_TYPES, true);
187
188                     OPTS_OPTION_U32(OPTION_STANDARD) = COMPILER_QCC;
189
190                 } else if (!strcmp(argarg, "fte") || !strcmp(argarg, "fteqcc")) {
191
192                     opts_set(opts.flags, FTEPP,                    true);
193                     opts_set(opts.flags, TRANSLATABLE_STRINGS,     true);
194                     opts_set(opts.flags, ADJUST_VECTOR_FIELDS,     false);
195                     opts_set(opts.flags, ASSIGN_FUNCTION_TYPES,    true);
196                     opts_set(opts.flags, CORRECT_TERNARY,          false);
197                     opts_set(opts.warn, WARN_TERNARY_PRECEDENCE,   true);
198                     opts_set(opts.warn, WARN_BREAKDEF,             true);
199
200                     OPTS_OPTION_U32(OPTION_STANDARD) = COMPILER_FTEQCC;
201
202                 } else if (!strcmp(argarg, "qccx")) {
203
204                     opts_set(opts.flags, ADJUST_VECTOR_FIELDS,  false);
205                     OPTS_OPTION_U32(OPTION_STANDARD) = COMPILER_QCCX;
206
207                 } else {
208                     con_out("Unknown standard: %s\n", argarg);
209                     return false;
210                 }
211                 continue;
212             }
213             if (options_long_gcc("force-crc", &argc, &argv, &argarg)) {
214
215                 OPTS_OPTION_BOOL(OPTION_FORCECRC)   = true;
216                 OPTS_OPTION_U16 (OPTION_FORCED_CRC) = strtol(argarg, NULL, 0);
217                 continue;
218             }
219             if (options_long_gcc("state-fps", &argc, &argv, &argarg)) {
220                 OPTS_OPTION_U32(OPTION_STATE_FPS) = strtol(argarg, NULL, 0);
221                 opts_set(opts.flags, EMULATE_STATE, true);
222                 continue;
223             }
224             if (options_long_gcc("config", &argc, &argv, &argarg)) {
225                 config = argarg;
226                 continue;
227             }
228             if (options_long_gcc("memdumpcols", &argc, &argv, &memdumpcols)) {
229                 OPTS_OPTION_U16(OPTION_MEMDUMPCOLS) = (uint16_t)strtol(memdumpcols, NULL, 10);
230                 continue;
231             }
232             if (options_long_gcc("progsrc", &argc, &argv, &argarg)) {
233                 OPTS_OPTION_STR(OPTION_PROGSRC) = argarg;
234                 continue;
235             }
236
237             /* show defaults (like pathscale) */
238             if (!strcmp(argv[0]+1, "show-defaults")) {
239                 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
240                     if (!OPTS_FLAG(itr))
241                         continue;
242
243                     memset(buffer, 0, sizeof(buffer));
244                     util_strtononcmd(opts_flag_list[itr].name, buffer, strlen(opts_flag_list[itr].name) + 1);
245
246                     con_out("-f%s ", buffer);
247                 }
248                 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
249                     if (!OPTS_WARN(itr))
250                         continue;
251
252                     memset(buffer, 0, sizeof(buffer));
253                     util_strtononcmd(opts_warn_list[itr].name, buffer, strlen(opts_warn_list[itr].name) + 1);
254                     con_out("-W%s ", buffer);
255                 }
256                 con_out("\n");
257                 exit(0);
258             }
259
260             if (!strcmp(argv[0]+1, "debug")) {
261                 OPTS_OPTION_BOOL(OPTION_DEBUG) = true;
262                 continue;
263             }
264             if (!strcmp(argv[0]+1, "dump")) {
265                 OPTS_OPTION_BOOL(OPTION_DUMP)  = true;
266                 continue;
267             }
268             if (!strcmp(argv[0]+1, "dumpfin")) {
269                 OPTS_OPTION_BOOL(OPTION_DUMPFIN) = true;
270                 continue;
271             }
272             if (!strcmp(argv[0]+1, "memchk")) {
273                 OPTS_OPTION_BOOL(OPTION_MEMCHK) = true;
274                 continue;
275             }
276             if (!strcmp(argv[0]+1, "nocolor")) {
277                 con_color(0);
278                 continue;
279             }
280             if (!strcmp(argv[0]+1, "coverage")) {
281                 OPTS_OPTION_BOOL(OPTION_COVERAGE) = true;
282                 continue;
283             }
284
285             switch (argv[0][1]) {
286                 /* -h, show usage but exit with 0 */
287                 case 'h':
288                     usage();
289                     exit(0);
290                     /* break; never reached because of exit(0) */
291
292                 case 'v':
293                     version();
294                     exit(0);
295
296                 case 'E':
297                     OPTS_OPTION_BOOL(OPTION_PP_ONLY) = true;
298                     opts_set(opts.flags, FTEPP_PREDEFS, true); /* predefs on for -E */
299                     break;
300
301                 /* debug turns on -flno */
302                 case 'g':
303                     opts_setflag("LNO", true);
304                     OPTS_OPTION_BOOL(OPTION_G) = true;
305                     break;
306
307                 case 'q':
308                     OPTS_OPTION_BOOL(OPTION_QUIET) = true;
309                     break;
310
311                 case 'D':
312                     if (!strlen(argv[0]+2)) {
313                         con_err("expected name after -D\n");
314                         exit(0);
315                     }
316
317                     if (!(argarg = strchr(argv[0] + 2, '='))) {
318                         macro.name  = util_strdup(argv[0]+2);
319                         macro.value = NULL;
320                     } else {
321                         *argarg='\0'; /* terminate for name */
322                         macro.name  = util_strdup(argv[0]+2);
323                         macro.value = util_strdup(argarg+1);
324                     }
325                     vec_push(ppems, macro);
326                     break;
327
328                 /* handle all -fflags */
329                 case 'f':
330                     util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
331                     if (!strcmp(argv[0]+2, "HELP") || *(argv[0]+2) == '?') {
332                         con_out("Possible flags:\n\n");
333                         for (itr = 0; itr < COUNT_FLAGS; ++itr) {
334                             util_strtononcmd(opts_flag_list[itr].name, buffer, sizeof(buffer));
335                             con_out(" -f%s\n", buffer);
336                         }
337                         exit(0);
338                     }
339                     else if (!strncmp(argv[0]+2, "NO_", 3)) {
340                         if (!opts_setflag(argv[0]+5, false)) {
341                             con_out("unknown flag: %s\n", argv[0]+2);
342                             return false;
343                         }
344                     }
345                     else if (!opts_setflag(argv[0]+2, true)) {
346                         con_out("unknown flag: %s\n", argv[0]+2);
347                         return false;
348                     }
349                     break;
350                 case 'W':
351                     util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
352                     if (!strcmp(argv[0]+2, "HELP") || *(argv[0]+2) == '?') {
353                         con_out("Possible warnings:\n");
354                         for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
355                             util_strtononcmd(opts_warn_list[itr].name, buffer, sizeof(buffer));
356                             con_out(" -W%s\n", buffer);
357                             if (itr == WARN_DEBUG)
358                                 con_out("   Warnings included by -Wall:\n");
359                         }
360                         exit(0);
361                     }
362                     else if (!strcmp(argv[0]+2, "NO_ERROR") ||
363                              !strcmp(argv[0]+2, "NO_ERROR_ALL"))
364                     {
365                         for (itr = 0; itr < GMQCC_ARRAY_COUNT(opts.werror); ++itr)
366                             opts.werror[itr] = 0;
367                         break;
368                     }
369                     else if (!strcmp(argv[0]+2, "ERROR") ||
370                              !strcmp(argv[0]+2, "ERROR_ALL"))
371                     {
372                         opts_backup_non_Werror_all();
373                         for (itr = 0; itr < GMQCC_ARRAY_COUNT(opts.werror); ++itr)
374                             opts.werror[itr] = 0xFFFFFFFFL;
375                         opts_restore_non_Werror_all();
376                         break;
377                     }
378                     else if (!strcmp(argv[0]+2, "NONE")) {
379                         for (itr = 0; itr < GMQCC_ARRAY_COUNT(opts.warn); ++itr)
380                             opts.warn[itr] = 0;
381                         break;
382                     }
383                     else if (!strcmp(argv[0]+2, "ALL")) {
384                         opts_backup_non_Wall();
385                         for (itr = 0; itr < GMQCC_ARRAY_COUNT(opts.warn); ++itr)
386                             opts.warn[itr] = 0xFFFFFFFFL;
387                         opts_restore_non_Wall();
388                         break;
389                     }
390                     else if (!strncmp(argv[0]+2, "ERROR_", 6)) {
391                         if (!opts_setwerror(argv[0]+8, true)) {
392                             con_out("unknown warning: %s\n", argv[0]+2);
393                             return false;
394                         }
395                     }
396                     else if (!strncmp(argv[0]+2, "NO_ERROR_", 9)) {
397                         if (!opts_setwerror(argv[0]+11, false)) {
398                             con_out("unknown warning: %s\n", argv[0]+2);
399                             return false;
400                         }
401                     }
402                     else if (!strncmp(argv[0]+2, "NO_", 3)) {
403                         if (!opts_setwarn(argv[0]+5, false)) {
404                             con_out("unknown warning: %s\n", argv[0]+2);
405                             return false;
406                         }
407                     }
408                     else if (!opts_setwarn(argv[0]+2, true)) {
409                         con_out("unknown warning: %s\n", argv[0]+2);
410                         return false;
411                     }
412                     break;
413
414                 case 'O':
415                     if (!options_witharg(&argc, &argv, &argarg)) {
416                         con_out("option -O requires a numerical argument, or optimization name with an optional 'no-' prefix\n");
417                         return false;
418                     }
419                     if (util_isdigit(argarg[0])) {
420                         uint32_t val = (uint32_t)strtol(argarg, NULL, 10);
421                         OPTS_OPTION_U32(OPTION_O) = val;
422                         opts_setoptimlevel(val);
423                     } else {
424                         util_strtocmd(argarg, argarg, strlen(argarg)+1);
425                         if (!strcmp(argarg, "HELP")) {
426                             con_out("Possible optimizations:\n");
427                             for (itr = 0; itr < COUNT_OPTIMIZATIONS; ++itr) {
428                                 util_strtononcmd(opts_opt_list[itr].name, buffer, sizeof(buffer));
429                                 con_out(" -O%-20s (-O%u)\n", buffer, opts_opt_oflag[itr]);
430                             }
431                             exit(0);
432                         }
433                         else if (!strcmp(argarg, "ALL"))
434                             opts_setoptimlevel(OPTS_OPTION_U32(OPTION_O) = 9999);
435                         else if (!strncmp(argarg, "NO_", 3)) {
436                             /* constant folding cannot be turned off for obvious reasons */
437                             if (!strcmp(argarg, "NO_CONST_FOLD") || !opts_setoptim(argarg+3, false)) {
438                                 con_out("unknown optimization: %s\n", argarg+3);
439                                 return false;
440                             }
441                         }
442                         else {
443                             if (!opts_setoptim(argarg, true)) {
444                                 con_out("unknown optimization: %s\n", argarg);
445                                 return false;
446                             }
447                         }
448                     }
449                     break;
450
451                 case 'o':
452                     if (!options_witharg(&argc, &argv, &argarg)) {
453                         con_out("option -o requires an argument: the output file name\n");
454                         return false;
455                     }
456                     OPTS_OPTION_STR(OPTION_OUTPUT) = argarg;
457                     opts_output_wasset = true;
458                     break;
459
460                 case 'a':
461                 case 's':
462                     item.type = argv[0][1] == 'a' ? TYPE_ASM : TYPE_SRC;
463                     if (!options_witharg(&argc, &argv, &argarg)) {
464                         con_out("option -a requires a filename %s\n",
465                                 (argv[0][1] == 'a' ? "containing QC-asm" : "containing a progs.src formatted list"));
466                         return false;
467                     }
468                     item.filename = argarg;
469                     vec_push(items, item);
470                     break;
471
472                 case '-':
473                     if (!argv[0][2]) {
474                         /* anything following -- is considered a non-option argument */
475                         argend = true;
476                         break;
477                     }
478             /* All long options without arguments */
479                     else if (!strcmp(argv[0]+2, "help")) {
480                         usage();
481                         exit(0);
482                     }
483                     else if (!strcmp(argv[0]+2, "version")) {
484                         version();
485                         exit(0);
486                     }
487                     else if (!strcmp(argv[0]+2, "quiet")) {
488                         OPTS_OPTION_BOOL(OPTION_QUIET) = true;
489                         break;
490                     }
491                     else if (!strcmp(argv[0]+2, "add-info")) {
492                         OPTS_OPTION_BOOL(OPTION_ADD_INFO) = true;
493                         break;
494                     }
495                     else {
496             /* All long options with arguments */
497                         if (options_long_witharg("output", &argc, &argv, &argarg)) {
498                             OPTS_OPTION_STR(OPTION_OUTPUT) = argarg;
499                             opts_output_wasset = true;
500                         } else {
501                             con_out("Unknown parameter: %s\n", argv[0]);
502                             return false;
503                         }
504                     }
505                     break;
506
507                 default:
508                     con_out("Unknown parameter: %s\n", argv[0]);
509                     return false;
510             }
511         }
512         else
513         {
514             /* it's a QC filename */
515             item.filename = argv[0];
516             item.type     = TYPE_QC;
517             vec_push(items, item);
518         }
519     }
520     opts_ini_init(config);
521     return true;
522 }
523
524 /* returns the line number, or -1 on error */
525 static bool progs_nextline(char **out, size_t *alen, FILE *src) {
526     int    len;
527     char  *line;
528     char  *start;
529     char  *end;
530
531     line = *out;
532     len  = util_getline(&line, alen, src);
533     if (len == -1)
534         return false;
535
536     /* start at first non-blank */
537     for (start = line; util_isspace(*start); ++start) {}
538     /* end at the first non-blank */
539     for (end = start; *end && !util_isspace(*end);  ++end)   {}
540
541     *out = line;
542     /* move the actual filename to the beginning */
543     while (start != end) {
544         *line++ = *start++;
545     }
546     *line = 0;
547     return true;
548 }
549
550 int main(int argc, char **argv) {
551     size_t          itr;
552     int             retval           = 0;
553     bool            operators_free   = false;
554     bool            progs_src        = false;
555     FILE       *outfile         = NULL;
556     struct parser_s *parser          = NULL;
557     struct ftepp_s  *ftepp           = NULL;
558
559     app_name = argv[0];
560     con_init ();
561     opts_init("progs.dat", COMPILER_QCC, (1024 << 3));
562
563     util_seed(time(0));
564
565     if (!options_parse(argc, argv)) {
566         return usage();
567     }
568
569     if (OPTS_FLAG(TRUE_EMPTY_STRINGS) && OPTS_FLAG(FALSE_EMPTY_STRINGS)) {
570         con_err("-ftrue-empty-strings and -ffalse-empty-strings are mutually exclusive");
571         exit(EXIT_FAILURE);
572     }
573
574     /* the standard decides which set of operators to use */
575     if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_GMQCC) {
576         operators      = c_operators;
577         operator_count = GMQCC_ARRAY_COUNT(c_operators);
578     } else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_FTEQCC) {
579         operators      = fte_operators;
580         operator_count = GMQCC_ARRAY_COUNT(fte_operators);
581     } else {
582         operators      = qcc_operators;
583         operator_count = GMQCC_ARRAY_COUNT(qcc_operators);
584     }
585
586     if (operators == fte_operators) {
587         /* fix ternary? */
588         if (OPTS_FLAG(CORRECT_TERNARY)) {
589             oper_info *newops;
590             if (operators[operator_count-2].id != opid1(',') ||
591                 operators[operator_count-1].id != opid2(':','?'))
592             {
593                 con_err("internal error: operator precedence table wasn't updated correctly!\n");
594                 exit(EXIT_FAILURE);
595             }
596             operators_free = true;
597             newops = (oper_info*)mem_a(sizeof(operators[0]) * operator_count);
598             memcpy(newops, operators, sizeof(operators[0]) * operator_count);
599             memcpy(&newops[operator_count-2], &operators[operator_count-1], sizeof(newops[0]));
600             memcpy(&newops[operator_count-1], &operators[operator_count-2], sizeof(newops[0]));
601             newops[operator_count-2].prec = newops[operator_count-1].prec+1;
602             operators = newops;
603         }
604     }
605
606     if (OPTS_OPTION_BOOL(OPTION_DUMP)) {
607         for (itr = 0; itr < COUNT_FLAGS; ++itr)
608             con_out("Flag %s = %i\n",    opts_flag_list[itr].name, OPTS_FLAG(itr));
609         for (itr = 0; itr < COUNT_WARNINGS; ++itr)
610             con_out("Warning %s = %i\n", opts_warn_list[itr].name, OPTS_WARN(itr));
611
612         con_out("output             = %s\n", OPTS_OPTION_STR(OPTION_OUTPUT));
613         con_out("optimization level = %u\n", OPTS_OPTION_U32(OPTION_O));
614         con_out("standard           = %u\n", OPTS_OPTION_U32(OPTION_STANDARD));
615     }
616
617     if (OPTS_OPTION_BOOL(OPTION_PP_ONLY)) {
618         if (opts_output_wasset) {
619             outfile = fopen(OPTS_OPTION_STR(OPTION_OUTPUT), "wb");
620             if (!outfile) {
621                 con_err("failed to open `%s` for writing\n", OPTS_OPTION_STR(OPTION_OUTPUT));
622                 retval = 1;
623                 goto cleanup;
624             }
625         }
626         else {
627             outfile = con_default_out();
628         }
629     }
630
631     if (!OPTS_OPTION_BOOL(OPTION_PP_ONLY)) {
632         if (!(parser = parser_create())) {
633             con_err("failed to initialize parser\n");
634             retval = 1;
635             goto cleanup;
636         }
637     }
638
639     if (OPTS_OPTION_BOOL(OPTION_PP_ONLY) || OPTS_FLAG(FTEPP)) {
640         if (!(ftepp = ftepp_create())) {
641             con_err("failed to initialize parser\n");
642             retval = 1;
643             goto cleanup;
644         }
645     }
646
647     /* add macros */
648     if (OPTS_OPTION_BOOL(OPTION_PP_ONLY) || OPTS_FLAG(FTEPP)) {
649         for (itr = 0; itr < vec_size(ppems); itr++) {
650             ftepp_add_macro(ftepp, ppems[itr].name, ppems[itr].value);
651             mem_d(ppems[itr].name);
652
653             /* can be null */
654             if (ppems[itr].value)
655                 mem_d(ppems[itr].value);
656         }
657     }
658
659     if (!vec_size(items)) {
660         FILE *src;
661         char      *line    = NULL;
662         size_t     linelen = 0;
663         bool       hasline = false;
664
665         progs_src = true;
666
667         src = fopen(OPTS_OPTION_STR(OPTION_PROGSRC), "rb");
668         if (!src) {
669             con_err("failed to open `%s` for reading\n", OPTS_OPTION_STR(OPTION_PROGSRC));
670             retval = 1;
671             goto cleanup;
672         }
673
674         while (progs_nextline(&line, &linelen, src)) {
675             argitem item;
676
677             if (!line[0] || (line[0] == '/' && line[1] == '/'))
678                 continue;
679
680             if (hasline) {
681                 item.filename = util_strdup(line);
682                 item.type     = TYPE_QC;
683                 vec_push(items, item);
684             } else if (!opts_output_wasset) {
685                 OPTS_OPTION_DUP(OPTION_OUTPUT) = util_strdup(line);
686                 hasline                        = true;
687             }
688         }
689
690         fclose(src);
691         mem_d(line);
692     }
693
694     if (vec_size(items)) {
695         if (!OPTS_OPTION_BOOL(OPTION_QUIET) &&
696             !OPTS_OPTION_BOOL(OPTION_PP_ONLY))
697         {
698             con_out("Mode: %s\n", (progs_src ? "progs.src" : "manual"));
699             con_out("There are %lu items to compile:\n", (unsigned long)vec_size(items));
700         }
701
702         for (itr = 0; itr < vec_size(items); ++itr) {
703             if (!OPTS_OPTION_BOOL(OPTION_QUIET) &&
704                 !OPTS_OPTION_BOOL(OPTION_PP_ONLY))
705             {
706                 con_out("  item: %s (%s)\n",
707                        items[itr].filename,
708                        ( (items[itr].type == TYPE_QC ? "qc" :
709                          (items[itr].type == TYPE_ASM ? "asm" :
710                          (items[itr].type == TYPE_SRC ? "progs.src" :
711                          ("unknown"))))));
712             }
713
714             if (OPTS_OPTION_BOOL(OPTION_PP_ONLY)) {
715                 const char *out;
716                 if (!ftepp_preprocess_file(ftepp, items[itr].filename)) {
717                     retval = 1;
718                     goto cleanup;
719                 }
720                 out = ftepp_get(ftepp);
721                 if (out)
722                     fprintf(outfile, "%s", out);
723                 ftepp_flush(ftepp);
724             }
725             else {
726                 if (OPTS_FLAG(FTEPP)) {
727                     const char *data;
728                     if (!ftepp_preprocess_file(ftepp, items[itr].filename)) {
729                         retval = 1;
730                         goto cleanup;
731                     }
732                     data = ftepp_get(ftepp);
733                     if (vec_size(data)) {
734                         if (!parser_compile_string(parser, items[itr].filename, data, vec_size(data))) {
735                             retval = 1;
736                             goto cleanup;
737                         }
738                     }
739                     ftepp_flush(ftepp);
740                 }
741                 else {
742                     if (!parser_compile_file(parser, items[itr].filename)) {
743                         retval = 1;
744                         goto cleanup;
745                     }
746                 }
747             }
748
749             if (progs_src) {
750                 mem_d(items[itr].filename);
751                 items[itr].filename = NULL;
752             }
753         }
754
755         ftepp_finish(ftepp);
756         ftepp = NULL;
757         if (!OPTS_OPTION_BOOL(OPTION_PP_ONLY)) {
758             if (!parser_finish(parser, OPTS_OPTION_STR(OPTION_OUTPUT))) {
759                 retval = 1;
760                 goto cleanup;
761             }
762         }
763     }
764
765 cleanup:
766     if (ftepp)
767         ftepp_finish(ftepp);
768     con_close();
769     vec_free(items);
770     vec_free(ppems);
771
772     if (!OPTS_OPTION_BOOL(OPTION_PP_ONLY))
773         if(parser) parser_cleanup(parser);
774
775     /* free allocated option strings */
776     for (itr = 0; itr < OPTION_COUNT; itr++)
777         if (OPTS_OPTION_DUPED(itr))
778             mem_d(OPTS_OPTION_STR(itr));
779
780     if (operators_free)
781         mem_d((void*)operators);
782
783     lex_cleanup();
784
785     if (!retval && compile_errors)
786         retval = 1;
787     return retval;
788 }