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