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