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