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