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