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