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