]> git.xonotic.org Git - xonotic/gmqcc.git/blob - main.c
33bde557176bf0d3a5c6ab9a4077551334f37606
[xonotic/gmqcc.git] / main.c
1 /*
2  * Copyright (C) 2012, 2013
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 <time.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include "gmqcc.h"
30 #include "lexer.h"
31
32 /* TODO: cleanup this whole file .. it's a fuckign mess */
33
34 /* set by the standard */
35 const oper_info *operators      = NULL;
36 size_t           operator_count = 0;
37 static bool      opts_output_wasset = false;
38
39 typedef struct { char *filename; int   type;  } argitem;
40 typedef struct { char *name;     char *value; } ppitem;
41 static argitem *items = NULL;
42 static ppitem  *ppems = NULL;
43
44 #define TYPE_QC  0
45 #define TYPE_ASM 1
46 #define TYPE_SRC 2
47
48 static const char *app_name;
49
50 static void version(void) {
51     con_out("GMQCC %d.%d.%d Built %s %s\n" GMQCC_DEV_VERSION_STRING,
52         GMQCC_VERSION_MAJOR,
53         GMQCC_VERSION_MINOR,
54         GMQCC_VERSION_PATCH,
55         __DATE__,
56         __TIME__
57     );
58 }
59
60 static int usage(void) {
61     con_out("usage: %s [options] [files...]", app_name);
62     con_out("options:\n"
63             "  -h, --help             show this help message\n"
64             "  -debug                 turns on compiler debug messages\n"
65             "  -memchk                turns on compiler memory leak check\n");
66     con_out("  -o, --output=file      output file, defaults to progs.dat\n"
67             "  -s filename            add a progs.src file to be used\n");
68     con_out("  -E                     stop after preprocessing\n");
69     con_out("  -q, --quiet            be less verbose\n");
70     con_out("  -config file           use the specified ini file\n");
71     con_out("  -std=standard          select one of the following standards\n"
72             "       -std=qcc          original QuakeC\n"
73             "       -std=fteqcc       fteqcc QuakeC\n"
74             "       -std=gmqcc        this compiler (default)\n");
75     con_out("  -f<flag>               enable a flag\n"
76             "  -fno-<flag>            disable a flag\n"
77             "  -fhelp                 list possible flags\n");
78     con_out("  -W<warning>            enable a warning\n"
79             "  -Wno-<warning>         disable a warning\n"
80             "  -Wall                  enable all warnings\n");
81     con_out("  -Werror                treat warnings as errors\n"
82             "  -Werror-<warning>      treat a warning as error\n"
83             "  -Wno-error-<warning>   opposite of the above\n");
84     con_out("  -Whelp                 list possible warnings\n");
85     con_out("  -O<number>             optimization level\n"
86             "  -O<name>               enable specific optimization\n"
87             "  -Ono-<name>            disable specific optimization\n"
88             "  -Ohelp                 list optimizations\n");
89     con_out("  -force-crc=num         force a specific checksum into the header\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("redirout", &argc, &argv, &redirout)) {
222                 con_change(redirout, redirerr);
223                 continue;
224             }
225             if (options_long_gcc("redirerr", &argc, &argv, &redirerr)) {
226                 con_change(redirout, redirerr);
227                 continue;
228             }
229             if (options_long_gcc("config", &argc, &argv, &argarg)) {
230                 config = argarg;
231                 continue;
232             }
233             if (options_long_gcc("memdumpcols", &argc, &argv, &memdumpcols)) {
234                 OPTS_OPTION_U16(OPTION_MEMDUMPCOLS) = (uint16_t)strtol(memdumpcols, NULL, 10);
235                 continue;
236             }
237
238             /* show defaults (like pathscale) */
239             if (!strcmp(argv[0]+1, "show-defaults")) {
240                 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
241                     if (!OPTS_FLAG(itr))
242                         continue;
243
244                     memset(buffer, 0, sizeof(buffer));
245                     util_strtononcmd(opts_flag_list[itr].name, buffer, strlen(opts_flag_list[itr].name) + 1);
246
247                     con_out("-f%s ", buffer);
248                 }
249                 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
250                     if (!OPTS_WARN(itr))
251                         continue;
252
253                     memset(buffer, 0, sizeof(buffer));
254                     util_strtononcmd(opts_warn_list[itr].name, buffer, strlen(opts_warn_list[itr].name) + 1);
255                     con_out("-W%s ", buffer);
256                 }
257                 con_out("\n");
258                 exit(0);
259             }
260
261             if (!strcmp(argv[0]+1, "debug")) {
262                 OPTS_OPTION_BOOL(OPTION_DEBUG) = true;
263                 continue;
264             }
265             if (!strcmp(argv[0]+1, "dump")) {
266                 OPTS_OPTION_BOOL(OPTION_DUMP)  = true;
267                 continue;
268             }
269             if (!strcmp(argv[0]+1, "dumpfin")) {
270                 OPTS_OPTION_BOOL(OPTION_DUMPFIN) = true;
271                 continue;
272             }
273             if (!strcmp(argv[0]+1, "memchk")) {
274                 OPTS_OPTION_BOOL(OPTION_MEMCHK) = true;
275                 continue;
276             }
277             if (!strcmp(argv[0]+1, "nocolor")) {
278                 con_color(0);
279                 continue;
280             }
281
282             switch (argv[0][1]) {
283                 /* -h, show usage but exit with 0 */
284                 case 'h':
285                     usage();
286                     exit(0);
287                     /* break; never reached because of exit(0) */
288
289                 case 'v':
290                     version();
291                     exit(0);
292
293                 case 'E':
294                     OPTS_OPTION_BOOL(OPTION_PP_ONLY) = true;
295                     opts_set(opts.flags, FTEPP_PREDEFS, true); /* predefs on for -E */
296                     break;
297
298                 /* debug turns on -flno */
299                 case 'g':
300                     opts_setflag("LNO", true);
301                     OPTS_OPTION_BOOL(OPTION_G) = true;
302                     break;
303
304                 case 'q':
305                     OPTS_OPTION_BOOL(OPTION_QUIET) = true;
306                     break;
307
308                 case 'D':
309                     if (!strlen(argv[0]+2)) {
310                         con_err("expected name after -D\n");
311                         exit(0);
312                     }
313
314                     if (!(argarg = strchr(argv[0] + 2, '='))) {
315                         macro.name  = util_strdup(argv[0]+2);
316                         macro.value = NULL;
317                     } else {
318                         *argarg='\0'; /* terminate for name */
319                         macro.name  = util_strdup(argv[0]+2);
320                         macro.value = util_strdup(argarg+1);
321                     }
322                     vec_push(ppems, macro);
323                     break;
324
325                 /* handle all -fflags */
326                 case 'f':
327                     util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
328                     if (!strcmp(argv[0]+2, "HELP") || *(argv[0]+2) == '?') {
329                         con_out("Possible flags:\n\n");
330                         for (itr = 0; itr < COUNT_FLAGS; ++itr) {
331                             util_strtononcmd(opts_flag_list[itr].name, buffer, sizeof(buffer));
332                             con_out(" -f%s\n", buffer);
333                         }
334                         exit(0);
335                     }
336                     else if (!strncmp(argv[0]+2, "NO_", 3)) {
337                         if (!opts_setflag(argv[0]+5, false)) {
338                             con_out("unknown flag: %s\n", argv[0]+2);
339                             return false;
340                         }
341                     }
342                     else if (!opts_setflag(argv[0]+2, true)) {
343                         con_out("unknown flag: %s\n", argv[0]+2);
344                         return false;
345                     }
346                     break;
347                 case 'W':
348                     util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
349                     if (!strcmp(argv[0]+2, "HELP") || *(argv[0]+2) == '?') {
350                         con_out("Possible warnings:\n");
351                         for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
352                             util_strtononcmd(opts_warn_list[itr].name, buffer, sizeof(buffer));
353                             con_out(" -W%s\n", buffer);
354                             if (itr == WARN_DEBUG)
355                                 con_out("   Warnings included by -Wall:\n");
356                         }
357                         exit(0);
358                     }
359                     else if (!strcmp(argv[0]+2, "NO_ERROR") ||
360                              !strcmp(argv[0]+2, "NO_ERROR_ALL"))
361                     {
362                         for (itr = 0; itr < GMQCC_ARRAY_COUNT(opts.werror); ++itr)
363                             opts.werror[itr] = 0;
364                         break;
365                     }
366                     else if (!strcmp(argv[0]+2, "ERROR") ||
367                              !strcmp(argv[0]+2, "ERROR_ALL"))
368                     {
369                         opts_backup_non_Werror_all();
370                         for (itr = 0; itr < GMQCC_ARRAY_COUNT(opts.werror); ++itr)
371                             opts.werror[itr] = 0xFFFFFFFFL;
372                         opts_restore_non_Werror_all();
373                         break;
374                     }
375                     else if (!strcmp(argv[0]+2, "NONE")) {
376                         for (itr = 0; itr < GMQCC_ARRAY_COUNT(opts.warn); ++itr)
377                             opts.warn[itr] = 0;
378                         break;
379                     }
380                     else if (!strcmp(argv[0]+2, "ALL")) {
381                         opts_backup_non_Wall();
382                         for (itr = 0; itr < GMQCC_ARRAY_COUNT(opts.warn); ++itr)
383                             opts.warn[itr] = 0xFFFFFFFFL;
384                         opts_restore_non_Wall();
385                         break;
386                     }
387                     else if (!strncmp(argv[0]+2, "ERROR_", 6)) {
388                         if (!opts_setwerror(argv[0]+8, true)) {
389                             con_out("unknown warning: %s\n", argv[0]+2);
390                             return false;
391                         }
392                     }
393                     else if (!strncmp(argv[0]+2, "NO_ERROR_", 9)) {
394                         if (!opts_setwerror(argv[0]+11, false)) {
395                             con_out("unknown warning: %s\n", argv[0]+2);
396                             return false;
397                         }
398                     }
399                     else if (!strncmp(argv[0]+2, "NO_", 3)) {
400                         if (!opts_setwarn(argv[0]+5, false)) {
401                             con_out("unknown warning: %s\n", argv[0]+2);
402                             return false;
403                         }
404                     }
405                     else if (!opts_setwarn(argv[0]+2, true)) {
406                         con_out("unknown warning: %s\n", argv[0]+2);
407                         return false;
408                     }
409                     break;
410
411                 case 'O':
412                     if (!options_witharg(&argc, &argv, &argarg)) {
413                         con_out("option -O requires a numerical argument, or optimization name with an optional 'no-' prefix\n");
414                         return false;
415                     }
416                     if (util_isdigit(argarg[0])) {
417                         uint32_t val = (uint32_t)strtol(argarg, NULL, 10);
418                         OPTS_OPTION_U32(OPTION_O) = val;
419                         opts_setoptimlevel(val);
420                     } else {
421                         util_strtocmd(argarg, argarg, strlen(argarg)+1);
422                         if (!strcmp(argarg, "HELP")) {
423                             con_out("Possible optimizations:\n");
424                             for (itr = 0; itr < COUNT_OPTIMIZATIONS; ++itr) {
425                                 util_strtononcmd(opts_opt_list[itr].name, buffer, sizeof(buffer));
426                                 con_out(" -O%-20s (-O%u)\n", buffer, opts_opt_oflag[itr]);
427                             }
428                             exit(0);
429                         }
430                         else if (!strcmp(argarg, "ALL"))
431                             opts_setoptimlevel(OPTS_OPTION_U32(OPTION_O) = 9999);
432                         else if (!strncmp(argarg, "NO_", 3)) {
433                             /* constant folding cannot be turned off for obvious reasons */
434                             if (!strcmp(argarg, "NO_CONST_FOLD") || !opts_setoptim(argarg+3, false)) {
435                                 con_out("unknown optimization: %s\n", argarg+3);
436                                 return false;
437                             }
438                         }
439                         else {
440                             if (!opts_setoptim(argarg, true)) {
441                                 con_out("unknown optimization: %s\n", argarg);
442                                 return false;
443                             }
444                         }
445                     }
446                     break;
447
448                 case 'o':
449                     if (!options_witharg(&argc, &argv, &argarg)) {
450                         con_out("option -o requires an argument: the output file name\n");
451                         return false;
452                     }
453                     OPTS_OPTION_STR(OPTION_OUTPUT) = argarg;
454                     opts_output_wasset = true;
455                     break;
456
457                 case 'a':
458                 case 's':
459                     item.type = argv[0][1] == 'a' ? TYPE_ASM : TYPE_SRC;
460                     if (!options_witharg(&argc, &argv, &argarg)) {
461                         con_out("option -a requires a filename %s\n",
462                                 (argv[0][1] == 'a' ? "containing QC-asm" : "containing a progs.src formatted list"));
463                         return false;
464                     }
465                     item.filename = argarg;
466                     vec_push(items, item);
467                     break;
468
469                 case '-':
470                     if (!argv[0][2]) {
471                         /* anything following -- is considered a non-option argument */
472                         argend = true;
473                         break;
474                     }
475             /* All long options without arguments */
476                     else if (!strcmp(argv[0]+2, "help")) {
477                         usage();
478                         exit(0);
479                     }
480                     else if (!strcmp(argv[0]+2, "version")) {
481                         version();
482                         exit(0);
483                     }
484                     else if (!strcmp(argv[0]+2, "quiet")) {
485                         OPTS_OPTION_BOOL(OPTION_QUIET) = true;
486                         break;
487                     }
488                     else if (!strcmp(argv[0]+2, "correct")) {
489                         OPTS_OPTION_BOOL(OPTION_CORRECTION) = true;
490                         break;
491                     }
492                     else if (!strcmp(argv[0]+2, "no-correct")) {
493                         OPTS_OPTION_BOOL(OPTION_CORRECTION) = false;
494                         break;
495                     }
496                     else if (!strcmp(argv[0]+2, "add-info")) {
497                         OPTS_OPTION_BOOL(OPTION_ADD_INFO) = true;
498                         break;
499                     }
500                     else {
501             /* All long options with arguments */
502                         if (options_long_witharg("output", &argc, &argv, &argarg)) {
503                             OPTS_OPTION_STR(OPTION_OUTPUT) = argarg;
504                             opts_output_wasset = true;
505                         } else {
506                             con_out("Unknown parameter: %s\n", argv[0]);
507                             return false;
508                         }
509                     }
510                     break;
511
512                 default:
513                     con_out("Unknown parameter: %s\n", argv[0]);
514                     return false;
515             }
516         }
517         else
518         {
519             /* it's a QC filename */
520             item.filename = argv[0];
521             item.type     = TYPE_QC;
522             vec_push(items, item);
523         }
524     }
525     opts_ini_init(config);
526     return true;
527 }
528
529 /* returns the line number, or -1 on error */
530 static bool progs_nextline(char **out, size_t *alen,FILE *src) {
531     int    len;
532     char  *line;
533     char  *start;
534     char  *end;
535
536     line = *out;
537     len  = fs_file_getline(&line, alen, src);
538     if (len == -1)
539         return false;
540
541     /* start at first non-blank */
542     for (start = line; util_isspace(*start); ++start) {}
543     /* end at the first non-blank */
544     for (end = start; *end && !util_isspace(*end);  ++end)   {}
545
546     *out = line;
547     /* move the actual filename to the beginning */
548     while (start != end) {
549         *line++ = *start++;
550     }
551     *line = 0;
552     return true;
553 }
554
555 int main(int argc, char **argv) {
556     size_t          itr;
557     int             retval           = 0;
558     bool            opts_output_free = false;
559     bool            operators_free   = false;
560     bool            progs_src        = false;
561     FILE            *outfile         = NULL;
562     struct parser_s *parser          = NULL;
563     struct ftepp_s  *ftepp           = NULL;
564
565     app_name = argv[0];
566     con_init ();
567     opts_init("progs.dat", COMPILER_QCC, (1024 << 3));
568
569     util_seed(time(0));
570
571     if (!options_parse(argc, argv)) {
572         return usage();
573     }
574
575     if (OPTS_FLAG(TRUE_EMPTY_STRINGS) && OPTS_FLAG(FALSE_EMPTY_STRINGS)) {
576         con_err("-ftrue-empty-strings and -ffalse-empty-strings are mutually exclusive");
577         exit(EXIT_FAILURE);
578     }
579
580     /* the standard decides which set of operators to use */
581     if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_GMQCC) {
582         operators      = c_operators;
583         operator_count = GMQCC_ARRAY_COUNT(c_operators);
584     } else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_FTEQCC) {
585         operators      = fte_operators;
586         operator_count = GMQCC_ARRAY_COUNT(fte_operators);
587     } else {
588         operators      = qcc_operators;
589         operator_count = GMQCC_ARRAY_COUNT(qcc_operators);
590     }
591
592     if (operators == fte_operators) {
593         /* fix ternary? */
594         if (OPTS_FLAG(CORRECT_TERNARY)) {
595             oper_info *newops;
596             if (operators[operator_count-2].id != opid1(',') ||
597                 operators[operator_count-1].id != opid2(':','?'))
598             {
599                 con_err("internal error: operator precedence table wasn't updated correctly!\n");
600                 exit(EXIT_FAILURE);
601             }
602             operators_free = true;
603             newops = (oper_info*)mem_a(sizeof(operators[0]) * operator_count);
604             memcpy(newops, operators, sizeof(operators[0]) * operator_count);
605             memcpy(&newops[operator_count-2], &operators[operator_count-1], sizeof(newops[0]));
606             memcpy(&newops[operator_count-1], &operators[operator_count-2], sizeof(newops[0]));
607             newops[operator_count-2].prec = newops[operator_count-1].prec+1;
608             operators = newops;
609         }
610     }
611
612     if (OPTS_OPTION_BOOL(OPTION_DUMP)) {
613         for (itr = 0; itr < COUNT_FLAGS; ++itr)
614             con_out("Flag %s = %i\n",    opts_flag_list[itr].name, OPTS_FLAG(itr));
615         for (itr = 0; itr < COUNT_WARNINGS; ++itr)
616             con_out("Warning %s = %i\n", opts_warn_list[itr].name, OPTS_WARN(itr));
617
618         con_out("output             = %s\n", OPTS_OPTION_STR(OPTION_OUTPUT));
619         con_out("optimization level = %u\n", OPTS_OPTION_U32(OPTION_O));
620         con_out("standard           = %u\n", OPTS_OPTION_U32(OPTION_STANDARD));
621     }
622
623     if (OPTS_OPTION_BOOL(OPTION_PP_ONLY)) {
624         if (opts_output_wasset) {
625             outfile = fs_file_open(OPTS_OPTION_STR(OPTION_OUTPUT), "wb");
626             if (!outfile) {
627                 con_err("failed to open `%s` for writing\n", OPTS_OPTION_STR(OPTION_OUTPUT));
628                 retval = 1;
629                 goto cleanup;
630             }
631         }
632         else {
633             outfile = con_default_out();
634         }
635     }
636
637     if (!OPTS_OPTION_BOOL(OPTION_PP_ONLY)) {
638         if (!(parser = parser_create())) {
639             con_err("failed to initialize parser\n");
640             retval = 1;
641             goto cleanup;
642         }
643     }
644
645     if (OPTS_OPTION_BOOL(OPTION_PP_ONLY) || OPTS_FLAG(FTEPP)) {
646         if (!(ftepp = ftepp_create())) {
647             con_err("failed to initialize parser\n");
648             retval = 1;
649             goto cleanup;
650         }
651     }
652
653     /* add macros */
654     if (OPTS_OPTION_BOOL(OPTION_PP_ONLY) || OPTS_FLAG(FTEPP)) {
655         for (itr = 0; itr < vec_size(ppems); itr++) {
656             ftepp_add_macro(ftepp, ppems[itr].name, ppems[itr].value);
657             mem_d(ppems[itr].name);
658
659             /* can be null */
660             if (ppems[itr].value)
661                 mem_d(ppems[itr].value);
662         }
663     }
664
665     if (!vec_size(items)) {
666         FILE  *src;
667         char  *line    = NULL;
668         size_t linelen = 0;
669         bool   hasline = false;
670
671         progs_src = true;
672
673         src = fs_file_open("progs.src", "rb");
674         if (!src) {
675             con_err("failed to open `progs.src` for reading\n");
676             retval = 1;
677             goto cleanup;
678         }
679
680         while (progs_nextline(&line, &linelen, src)) {
681             argitem item;
682
683             if (!line[0] || (line[0] == '/' && line[1] == '/'))
684                 continue;
685
686             if (hasline) {
687                 item.filename = util_strdup(line);
688                 item.type     = TYPE_QC;
689                 vec_push(items, item);
690             } else if (!opts_output_wasset) {
691                 OPTS_OPTION_STR(OPTION_OUTPUT) = util_strdup(line);
692                 opts_output_free               = true;
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     if (opts_output_free)
782         mem_d(OPTS_OPTION_STR(OPTION_OUTPUT));
783     if (operators_free)
784         mem_d((void*)operators);
785
786     lex_cleanup();
787     stat_info();
788
789     return retval;
790 }