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