]> git.xonotic.org Git - xonotic/gmqcc.git/blob - main.c
Don't allocate globals for constants that aren't read
[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 *redirout    = NULL;
149     char *redirerr    = NULL;
150     char *config      = NULL;
151     char *memdumpcols = NULL;
152
153     while (!argend && argc > 1) {
154         char *argarg;
155         argitem item;
156         ppitem  macro;
157
158         ++argv;
159         --argc;
160
161         if (argv[0][0] == '-') {
162             /* All gcc-type long options */
163             if (options_long_gcc("std", &argc, &argv, &argarg)) {
164                 if (!strcmp(argarg, "gmqcc") || !strcmp(argarg, "default")) {
165
166                     opts_set(opts.flags, ADJUST_VECTOR_FIELDS,          true);
167                     opts_set(opts.flags, CORRECT_LOGIC,                 true);
168                     opts_set(opts.flags, SHORT_LOGIC,                   true);
169                     opts_set(opts.flags, UNTYPED_NIL,                   true);
170                     opts_set(opts.flags, VARIADIC_ARGS,                 true);
171                     opts_set(opts.flags, FALSE_EMPTY_STRINGS,           false);
172                     opts_set(opts.flags, TRUE_EMPTY_STRINGS,            true);
173                     opts_set(opts.flags, LOOP_LABELS,                   true);
174                     opts_set(opts.flags, TRANSLATABLE_STRINGS,          true);
175                     opts_set(opts.flags, INITIALIZED_NONCONSTANTS,      true);
176                     opts_set(opts.werror, WARN_INVALID_PARAMETER_COUNT, true);
177                     opts_set(opts.werror, WARN_MISSING_RETURN_VALUES,   true);
178                     opts_set(opts.flags,  EXPRESSIONS_FOR_BUILTINS,     true);
179                     opts_set(opts.warn,   WARN_BREAKDEF,                true);
180
181
182
183                     OPTS_OPTION_U32(OPTION_STANDARD) = COMPILER_GMQCC;
184
185                 } else if (!strcmp(argarg, "qcc")) {
186
187                     opts_set(opts.flags, ADJUST_VECTOR_FIELDS,  false);
188                     opts_set(opts.flags, ASSIGN_FUNCTION_TYPES, true);
189
190                     OPTS_OPTION_U32(OPTION_STANDARD) = COMPILER_QCC;
191
192                 } else if (!strcmp(argarg, "fte") || !strcmp(argarg, "fteqcc")) {
193
194                     opts_set(opts.flags, FTEPP,                    true);
195                     opts_set(opts.flags, TRANSLATABLE_STRINGS,     true);
196                     opts_set(opts.flags, ADJUST_VECTOR_FIELDS,     false);
197                     opts_set(opts.flags, ASSIGN_FUNCTION_TYPES,    true);
198                     opts_set(opts.flags, CORRECT_TERNARY,          false);
199                     opts_set(opts.warn, WARN_TERNARY_PRECEDENCE,   true);
200                     opts_set(opts.warn, WARN_BREAKDEF,             true);
201
202                     OPTS_OPTION_U32(OPTION_STANDARD) = COMPILER_FTEQCC;
203
204                 } else if (!strcmp(argarg, "qccx")) {
205
206                     opts_set(opts.flags, ADJUST_VECTOR_FIELDS,  false);
207                     OPTS_OPTION_U32(OPTION_STANDARD) = COMPILER_QCCX;
208
209                 } else {
210                     con_out("Unknown standard: %s\n", argarg);
211                     return false;
212                 }
213                 continue;
214             }
215             if (options_long_gcc("force-crc", &argc, &argv, &argarg)) {
216
217                 OPTS_OPTION_BOOL(OPTION_FORCECRC)   = true;
218                 OPTS_OPTION_U16 (OPTION_FORCED_CRC) = strtol(argarg, NULL, 0);
219                 continue;
220             }
221             if (options_long_gcc("state-fps", &argc, &argv, &argarg)) {
222                 OPTS_OPTION_U32(OPTION_STATE_FPS) = strtol(argarg, NULL, 0);
223                 opts_set(opts.flags, EMULATE_STATE, true);
224                 continue;
225             }
226             if (options_long_gcc("redirout", &argc, &argv, &redirout)) {
227                 con_change(redirout, redirerr);
228                 continue;
229             }
230             if (options_long_gcc("redirerr", &argc, &argv, &redirerr)) {
231                 con_change(redirout, redirerr);
232                 continue;
233             }
234             if (options_long_gcc("config", &argc, &argv, &argarg)) {
235                 config = argarg;
236                 continue;
237             }
238             if (options_long_gcc("memdumpcols", &argc, &argv, &memdumpcols)) {
239                 OPTS_OPTION_U16(OPTION_MEMDUMPCOLS) = (uint16_t)strtol(memdumpcols, NULL, 10);
240                 continue;
241             }
242             if (options_long_gcc("progsrc", &argc, &argv, &argarg)) {
243                 OPTS_OPTION_STR(OPTION_PROGSRC) = argarg;
244                 continue;
245             }
246
247             /* show defaults (like pathscale) */
248             if (!strcmp(argv[0]+1, "show-defaults")) {
249                 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
250                     if (!OPTS_FLAG(itr))
251                         continue;
252
253                     memset(buffer, 0, sizeof(buffer));
254                     util_strtononcmd(opts_flag_list[itr].name, buffer, strlen(opts_flag_list[itr].name) + 1);
255
256                     con_out("-f%s ", buffer);
257                 }
258                 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
259                     if (!OPTS_WARN(itr))
260                         continue;
261
262                     memset(buffer, 0, sizeof(buffer));
263                     util_strtononcmd(opts_warn_list[itr].name, buffer, strlen(opts_warn_list[itr].name) + 1);
264                     con_out("-W%s ", buffer);
265                 }
266                 con_out("\n");
267                 exit(0);
268             }
269
270             if (!strcmp(argv[0]+1, "debug")) {
271                 OPTS_OPTION_BOOL(OPTION_DEBUG) = true;
272                 continue;
273             }
274             if (!strcmp(argv[0]+1, "dump")) {
275                 OPTS_OPTION_BOOL(OPTION_DUMP)  = true;
276                 continue;
277             }
278             if (!strcmp(argv[0]+1, "dumpfin")) {
279                 OPTS_OPTION_BOOL(OPTION_DUMPFIN) = true;
280                 continue;
281             }
282             if (!strcmp(argv[0]+1, "memchk")) {
283                 OPTS_OPTION_BOOL(OPTION_MEMCHK) = true;
284                 continue;
285             }
286             if (!strcmp(argv[0]+1, "nocolor")) {
287                 con_color(0);
288                 continue;
289             }
290             if (!strcmp(argv[0]+1, "coverage")) {
291                 OPTS_OPTION_BOOL(OPTION_COVERAGE) = true;
292                 continue;
293             }
294
295             switch (argv[0][1]) {
296                 /* -h, show usage but exit with 0 */
297                 case 'h':
298                     usage();
299                     exit(0);
300                     /* break; never reached because of exit(0) */
301
302                 case 'v':
303                     version();
304                     exit(0);
305
306                 case 'E':
307                     OPTS_OPTION_BOOL(OPTION_PP_ONLY) = true;
308                     opts_set(opts.flags, FTEPP_PREDEFS, true); /* predefs on for -E */
309                     break;
310
311                 /* debug turns on -flno */
312                 case 'g':
313                     opts_setflag("LNO", true);
314                     OPTS_OPTION_BOOL(OPTION_G) = true;
315                     break;
316
317                 case 'q':
318                     OPTS_OPTION_BOOL(OPTION_QUIET) = true;
319                     break;
320
321                 case 'D':
322                     if (!strlen(argv[0]+2)) {
323                         con_err("expected name after -D\n");
324                         exit(0);
325                     }
326
327                     if (!(argarg = strchr(argv[0] + 2, '='))) {
328                         macro.name  = util_strdup(argv[0]+2);
329                         macro.value = NULL;
330                     } else {
331                         *argarg='\0'; /* terminate for name */
332                         macro.name  = util_strdup(argv[0]+2);
333                         macro.value = util_strdup(argarg+1);
334                     }
335                     vec_push(ppems, macro);
336                     break;
337
338                 /* handle all -fflags */
339                 case 'f':
340                     util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
341                     if (!strcmp(argv[0]+2, "HELP") || *(argv[0]+2) == '?') {
342                         con_out("Possible flags:\n\n");
343                         for (itr = 0; itr < COUNT_FLAGS; ++itr) {
344                             util_strtononcmd(opts_flag_list[itr].name, buffer, sizeof(buffer));
345                             con_out(" -f%s\n", buffer);
346                         }
347                         exit(0);
348                     }
349                     else if (!strncmp(argv[0]+2, "NO_", 3)) {
350                         if (!opts_setflag(argv[0]+5, false)) {
351                             con_out("unknown flag: %s\n", argv[0]+2);
352                             return false;
353                         }
354                     }
355                     else if (!opts_setflag(argv[0]+2, true)) {
356                         con_out("unknown flag: %s\n", argv[0]+2);
357                         return false;
358                     }
359                     break;
360                 case 'W':
361                     util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
362                     if (!strcmp(argv[0]+2, "HELP") || *(argv[0]+2) == '?') {
363                         con_out("Possible warnings:\n");
364                         for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
365                             util_strtononcmd(opts_warn_list[itr].name, buffer, sizeof(buffer));
366                             con_out(" -W%s\n", buffer);
367                             if (itr == WARN_DEBUG)
368                                 con_out("   Warnings included by -Wall:\n");
369                         }
370                         exit(0);
371                     }
372                     else if (!strcmp(argv[0]+2, "NO_ERROR") ||
373                              !strcmp(argv[0]+2, "NO_ERROR_ALL"))
374                     {
375                         for (itr = 0; itr < GMQCC_ARRAY_COUNT(opts.werror); ++itr)
376                             opts.werror[itr] = 0;
377                         break;
378                     }
379                     else if (!strcmp(argv[0]+2, "ERROR") ||
380                              !strcmp(argv[0]+2, "ERROR_ALL"))
381                     {
382                         opts_backup_non_Werror_all();
383                         for (itr = 0; itr < GMQCC_ARRAY_COUNT(opts.werror); ++itr)
384                             opts.werror[itr] = 0xFFFFFFFFL;
385                         opts_restore_non_Werror_all();
386                         break;
387                     }
388                     else if (!strcmp(argv[0]+2, "NONE")) {
389                         for (itr = 0; itr < GMQCC_ARRAY_COUNT(opts.warn); ++itr)
390                             opts.warn[itr] = 0;
391                         break;
392                     }
393                     else if (!strcmp(argv[0]+2, "ALL")) {
394                         opts_backup_non_Wall();
395                         for (itr = 0; itr < GMQCC_ARRAY_COUNT(opts.warn); ++itr)
396                             opts.warn[itr] = 0xFFFFFFFFL;
397                         opts_restore_non_Wall();
398                         break;
399                     }
400                     else if (!strncmp(argv[0]+2, "ERROR_", 6)) {
401                         if (!opts_setwerror(argv[0]+8, true)) {
402                             con_out("unknown warning: %s\n", argv[0]+2);
403                             return false;
404                         }
405                     }
406                     else if (!strncmp(argv[0]+2, "NO_ERROR_", 9)) {
407                         if (!opts_setwerror(argv[0]+11, false)) {
408                             con_out("unknown warning: %s\n", argv[0]+2);
409                             return false;
410                         }
411                     }
412                     else if (!strncmp(argv[0]+2, "NO_", 3)) {
413                         if (!opts_setwarn(argv[0]+5, false)) {
414                             con_out("unknown warning: %s\n", argv[0]+2);
415                             return false;
416                         }
417                     }
418                     else if (!opts_setwarn(argv[0]+2, true)) {
419                         con_out("unknown warning: %s\n", argv[0]+2);
420                         return false;
421                     }
422                     break;
423
424                 case 'O':
425                     if (!options_witharg(&argc, &argv, &argarg)) {
426                         con_out("option -O requires a numerical argument, or optimization name with an optional 'no-' prefix\n");
427                         return false;
428                     }
429                     if (util_isdigit(argarg[0])) {
430                         uint32_t val = (uint32_t)strtol(argarg, NULL, 10);
431                         OPTS_OPTION_U32(OPTION_O) = val;
432                         opts_setoptimlevel(val);
433                     } else {
434                         util_strtocmd(argarg, argarg, strlen(argarg)+1);
435                         if (!strcmp(argarg, "HELP")) {
436                             con_out("Possible optimizations:\n");
437                             for (itr = 0; itr < COUNT_OPTIMIZATIONS; ++itr) {
438                                 util_strtononcmd(opts_opt_list[itr].name, buffer, sizeof(buffer));
439                                 con_out(" -O%-20s (-O%u)\n", buffer, opts_opt_oflag[itr]);
440                             }
441                             exit(0);
442                         }
443                         else if (!strcmp(argarg, "ALL"))
444                             opts_setoptimlevel(OPTS_OPTION_U32(OPTION_O) = 9999);
445                         else if (!strncmp(argarg, "NO_", 3)) {
446                             /* constant folding cannot be turned off for obvious reasons */
447                             if (!strcmp(argarg, "NO_CONST_FOLD") || !opts_setoptim(argarg+3, false)) {
448                                 con_out("unknown optimization: %s\n", argarg+3);
449                                 return false;
450                             }
451                         }
452                         else {
453                             if (!opts_setoptim(argarg, true)) {
454                                 con_out("unknown optimization: %s\n", argarg);
455                                 return false;
456                             }
457                         }
458                     }
459                     break;
460
461                 case 'o':
462                     if (!options_witharg(&argc, &argv, &argarg)) {
463                         con_out("option -o requires an argument: the output file name\n");
464                         return false;
465                     }
466                     OPTS_OPTION_STR(OPTION_OUTPUT) = argarg;
467                     opts_output_wasset = true;
468                     break;
469
470                 case 'a':
471                 case 's':
472                     item.type = argv[0][1] == 'a' ? TYPE_ASM : TYPE_SRC;
473                     if (!options_witharg(&argc, &argv, &argarg)) {
474                         con_out("option -a requires a filename %s\n",
475                                 (argv[0][1] == 'a' ? "containing QC-asm" : "containing a progs.src formatted list"));
476                         return false;
477                     }
478                     item.filename = argarg;
479                     vec_push(items, item);
480                     break;
481
482                 case '-':
483                     if (!argv[0][2]) {
484                         /* anything following -- is considered a non-option argument */
485                         argend = true;
486                         break;
487                     }
488             /* All long options without arguments */
489                     else if (!strcmp(argv[0]+2, "help")) {
490                         usage();
491                         exit(0);
492                     }
493                     else if (!strcmp(argv[0]+2, "version")) {
494                         version();
495                         exit(0);
496                     }
497                     else if (!strcmp(argv[0]+2, "quiet")) {
498                         OPTS_OPTION_BOOL(OPTION_QUIET) = true;
499                         break;
500                     }
501                     else if (!strcmp(argv[0]+2, "correct")) {
502                         OPTS_OPTION_BOOL(OPTION_CORRECTION) = true;
503                         break;
504                     }
505                     else if (!strcmp(argv[0]+2, "no-correct")) {
506                         OPTS_OPTION_BOOL(OPTION_CORRECTION) = false;
507                         break;
508                     }
509                     else if (!strcmp(argv[0]+2, "add-info")) {
510                         OPTS_OPTION_BOOL(OPTION_ADD_INFO) = true;
511                         break;
512                     }
513                     else {
514             /* All long options with arguments */
515                         if (options_long_witharg("output", &argc, &argv, &argarg)) {
516                             OPTS_OPTION_STR(OPTION_OUTPUT) = argarg;
517                             opts_output_wasset = true;
518                         } else {
519                             con_out("Unknown parameter: %s\n", argv[0]);
520                             return false;
521                         }
522                     }
523                     break;
524
525                 default:
526                     con_out("Unknown parameter: %s\n", argv[0]);
527                     return false;
528             }
529         }
530         else
531         {
532             /* it's a QC filename */
533             item.filename = argv[0];
534             item.type     = TYPE_QC;
535             vec_push(items, item);
536         }
537     }
538     opts_ini_init(config);
539     return true;
540 }
541
542 /* returns the line number, or -1 on error */
543 static bool progs_nextline(char **out, size_t *alen, fs_file_t *src) {
544     int    len;
545     char  *line;
546     char  *start;
547     char  *end;
548
549     line = *out;
550     len  = fs_file_getline(&line, alen, src);
551     if (len == -1)
552         return false;
553
554     /* start at first non-blank */
555     for (start = line; util_isspace(*start); ++start) {}
556     /* end at the first non-blank */
557     for (end = start; *end && !util_isspace(*end);  ++end)   {}
558
559     *out = line;
560     /* move the actual filename to the beginning */
561     while (start != end) {
562         *line++ = *start++;
563     }
564     *line = 0;
565     return true;
566 }
567
568 int main(int argc, char **argv) {
569     size_t          itr;
570     int             retval           = 0;
571     bool            operators_free   = false;
572     bool            progs_src        = false;
573     fs_file_t       *outfile         = NULL;
574     struct parser_s *parser          = NULL;
575     struct ftepp_s  *ftepp           = NULL;
576
577     app_name = argv[0];
578     con_init ();
579     opts_init("progs.dat", COMPILER_QCC, (1024 << 3));
580
581     util_seed(time(0));
582
583     if (!options_parse(argc, argv)) {
584         return usage();
585     }
586
587     if (OPTS_FLAG(TRUE_EMPTY_STRINGS) && OPTS_FLAG(FALSE_EMPTY_STRINGS)) {
588         con_err("-ftrue-empty-strings and -ffalse-empty-strings are mutually exclusive");
589         exit(EXIT_FAILURE);
590     }
591
592     /* the standard decides which set of operators to use */
593     if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_GMQCC) {
594         operators      = c_operators;
595         operator_count = GMQCC_ARRAY_COUNT(c_operators);
596     } else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_FTEQCC) {
597         operators      = fte_operators;
598         operator_count = GMQCC_ARRAY_COUNT(fte_operators);
599     } else {
600         operators      = qcc_operators;
601         operator_count = GMQCC_ARRAY_COUNT(qcc_operators);
602     }
603
604     if (operators == fte_operators) {
605         /* fix ternary? */
606         if (OPTS_FLAG(CORRECT_TERNARY)) {
607             oper_info *newops;
608             if (operators[operator_count-2].id != opid1(',') ||
609                 operators[operator_count-1].id != opid2(':','?'))
610             {
611                 con_err("internal error: operator precedence table wasn't updated correctly!\n");
612                 exit(EXIT_FAILURE);
613             }
614             operators_free = true;
615             newops = (oper_info*)mem_a(sizeof(operators[0]) * operator_count);
616             memcpy(newops, operators, sizeof(operators[0]) * operator_count);
617             memcpy(&newops[operator_count-2], &operators[operator_count-1], sizeof(newops[0]));
618             memcpy(&newops[operator_count-1], &operators[operator_count-2], sizeof(newops[0]));
619             newops[operator_count-2].prec = newops[operator_count-1].prec+1;
620             operators = newops;
621         }
622     }
623
624     if (OPTS_OPTION_BOOL(OPTION_DUMP)) {
625         for (itr = 0; itr < COUNT_FLAGS; ++itr)
626             con_out("Flag %s = %i\n",    opts_flag_list[itr].name, OPTS_FLAG(itr));
627         for (itr = 0; itr < COUNT_WARNINGS; ++itr)
628             con_out("Warning %s = %i\n", opts_warn_list[itr].name, OPTS_WARN(itr));
629
630         con_out("output             = %s\n", OPTS_OPTION_STR(OPTION_OUTPUT));
631         con_out("optimization level = %u\n", OPTS_OPTION_U32(OPTION_O));
632         con_out("standard           = %u\n", OPTS_OPTION_U32(OPTION_STANDARD));
633     }
634
635     if (OPTS_OPTION_BOOL(OPTION_PP_ONLY)) {
636         if (opts_output_wasset) {
637             outfile = fs_file_open(OPTS_OPTION_STR(OPTION_OUTPUT), "wb");
638             if (!outfile) {
639                 con_err("failed to open `%s` for writing\n", OPTS_OPTION_STR(OPTION_OUTPUT));
640                 retval = 1;
641                 goto cleanup;
642             }
643         }
644         else {
645             outfile = con_default_out();
646         }
647     }
648
649     if (!OPTS_OPTION_BOOL(OPTION_PP_ONLY)) {
650         if (!(parser = parser_create())) {
651             con_err("failed to initialize parser\n");
652             retval = 1;
653             goto cleanup;
654         }
655     }
656
657     if (OPTS_OPTION_BOOL(OPTION_PP_ONLY) || OPTS_FLAG(FTEPP)) {
658         if (!(ftepp = ftepp_create())) {
659             con_err("failed to initialize parser\n");
660             retval = 1;
661             goto cleanup;
662         }
663     }
664
665     /* add macros */
666     if (OPTS_OPTION_BOOL(OPTION_PP_ONLY) || OPTS_FLAG(FTEPP)) {
667         for (itr = 0; itr < vec_size(ppems); itr++) {
668             ftepp_add_macro(ftepp, ppems[itr].name, ppems[itr].value);
669             mem_d(ppems[itr].name);
670
671             /* can be null */
672             if (ppems[itr].value)
673                 mem_d(ppems[itr].value);
674         }
675     }
676
677     if (!vec_size(items)) {
678         fs_file_t *src;
679         char      *line    = NULL;
680         size_t     linelen = 0;
681         bool       hasline = false;
682
683         progs_src = true;
684
685         src = fs_file_open(OPTS_OPTION_STR(OPTION_PROGSRC), "rb");
686         if (!src) {
687             con_err("failed to open `%s` for reading\n", OPTS_OPTION_STR(OPTION_PROGSRC));
688             retval = 1;
689             goto cleanup;
690         }
691
692         while (progs_nextline(&line, &linelen, src)) {
693             argitem item;
694
695             if (!line[0] || (line[0] == '/' && line[1] == '/'))
696                 continue;
697
698             if (hasline) {
699                 item.filename = util_strdup(line);
700                 item.type     = TYPE_QC;
701                 vec_push(items, item);
702             } else if (!opts_output_wasset) {
703                 OPTS_OPTION_DUP(OPTION_OUTPUT) = util_strdup(line);
704                 hasline                        = true;
705             }
706         }
707
708         fs_file_close(src);
709         mem_d(line);
710     }
711
712     if (vec_size(items)) {
713         if (!OPTS_OPTION_BOOL(OPTION_QUIET) &&
714             !OPTS_OPTION_BOOL(OPTION_PP_ONLY))
715         {
716             con_out("Mode: %s\n", (progs_src ? "progs.src" : "manual"));
717             con_out("There are %lu items to compile:\n", (unsigned long)vec_size(items));
718         }
719
720         for (itr = 0; itr < vec_size(items); ++itr) {
721             if (!OPTS_OPTION_BOOL(OPTION_QUIET) &&
722                 !OPTS_OPTION_BOOL(OPTION_PP_ONLY))
723             {
724                 con_out("  item: %s (%s)\n",
725                        items[itr].filename,
726                        ( (items[itr].type == TYPE_QC ? "qc" :
727                          (items[itr].type == TYPE_ASM ? "asm" :
728                          (items[itr].type == TYPE_SRC ? "progs.src" :
729                          ("unknown"))))));
730             }
731
732             if (OPTS_OPTION_BOOL(OPTION_PP_ONLY)) {
733                 const char *out;
734                 if (!ftepp_preprocess_file(ftepp, items[itr].filename)) {
735                     retval = 1;
736                     goto cleanup;
737                 }
738                 out = ftepp_get(ftepp);
739                 if (out)
740                     fs_file_printf(outfile, "%s", out);
741                 ftepp_flush(ftepp);
742             }
743             else {
744                 if (OPTS_FLAG(FTEPP)) {
745                     const char *data;
746                     if (!ftepp_preprocess_file(ftepp, items[itr].filename)) {
747                         retval = 1;
748                         goto cleanup;
749                     }
750                     data = ftepp_get(ftepp);
751                     if (vec_size(data)) {
752                         if (!parser_compile_string(parser, items[itr].filename, data, vec_size(data))) {
753                             retval = 1;
754                             goto cleanup;
755                         }
756                     }
757                     ftepp_flush(ftepp);
758                 }
759                 else {
760                     if (!parser_compile_file(parser, items[itr].filename)) {
761                         retval = 1;
762                         goto cleanup;
763                     }
764                 }
765             }
766
767             if (progs_src) {
768                 mem_d(items[itr].filename);
769                 items[itr].filename = NULL;
770             }
771         }
772
773         ftepp_finish(ftepp);
774         ftepp = NULL;
775         if (!OPTS_OPTION_BOOL(OPTION_PP_ONLY)) {
776             if (!parser_finish(parser, OPTS_OPTION_STR(OPTION_OUTPUT))) {
777                 retval = 1;
778                 goto cleanup;
779             }
780         }
781     }
782
783 cleanup:
784     if (ftepp)
785         ftepp_finish(ftepp);
786     con_close();
787     vec_free(items);
788     vec_free(ppems);
789
790     if (!OPTS_OPTION_BOOL(OPTION_PP_ONLY))
791         if(parser) parser_cleanup(parser);
792
793     /* free allocated option strings */
794     for (itr = 0; itr < OPTION_COUNT; itr++)
795         if (OPTS_OPTION_DUPED(itr))
796             mem_d(OPTS_OPTION_STR(itr));
797
798     if (operators_free)
799         mem_d((void*)operators);
800
801     lex_cleanup();
802     stat_info();
803
804     if (!retval && compile_errors)
805         retval = 1;
806     return retval;
807 }