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