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