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