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