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