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