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