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