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