]> git.xonotic.org Git - xonotic/gmqcc.git/blob - main.c
manpage: -Oglobal-temps
[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.standard = COMPILER_GMQCC;
163
164                 } else if (!strcmp(argarg, "qcc")) {
165
166                     opts_set(opts.flags, ADJUST_VECTOR_FIELDS,  false);
167                     opts_set(opts.flags, ASSIGN_FUNCTION_TYPES, true);
168                     opts.standard = COMPILER_QCC;
169
170                 } else if (!strcmp(argarg, "fte") || !strcmp(argarg, "fteqcc")) {
171
172                     opts_set(opts.flags, FTEPP,                    true);
173                     opts_set(opts.flags, TRANSLATABLE_STRINGS,     true);
174                     opts_set(opts.flags, ADJUST_VECTOR_FIELDS,     false);
175                     opts_set(opts.flags, ASSIGN_FUNCTION_TYPES,    true);
176                     opts_set(opts.warn, WARN_TERNARY_PRECEDENCE,   true);
177                     opts_set(opts.flags, CORRECT_TERNARY,          false);
178                     opts.standard = COMPILER_FTEQCC;
179
180                 } else if (!strcmp(argarg, "qccx")) {
181
182                     opts_set(opts.flags, ADJUST_VECTOR_FIELDS,  false);
183                     opts.standard = COMPILER_QCCX;
184
185                 } else {
186                     con_out("Unknown standard: %s\n", argarg);
187                     return false;
188                 }
189                 continue;
190             }
191             if (options_long_gcc("force-crc", &argc, &argv, &argarg)) {
192                 opts.forcecrc   = true;
193                 opts.forced_crc = strtol(argarg, NULL, 0);
194                 continue;
195             }
196             if (options_long_gcc("redirout", &argc, &argv, &redirout)) {
197                 con_change(redirout, redirerr);
198                 continue;
199             }
200             if (options_long_gcc("redirerr", &argc, &argv, &redirerr)) {
201                 con_change(redirout, redirerr);
202                 continue;
203             }
204             if (options_long_gcc("config", &argc, &argv, &argarg)) {
205                 config = argarg;
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.debug = true;
234                 continue;
235             }
236             if (!strcmp(argv[0]+1, "dump")) {
237                 opts.dump = true;
238                 continue;
239             }
240             if (!strcmp(argv[0]+1, "dumpfin")) {
241                 opts.dumpfin = true;
242                 continue;
243             }
244             if (!strcmp(argv[0]+1, "memchk")) {
245                 opts.memchk = true;
246                 continue;
247             }
248             if (!strcmp(argv[0]+1, "nocolor")) {
249                 con_color(0);
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.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.g = true;
273                     break;
274
275                 case 'q':
276                     opts.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 = NULL;
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");
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                         }
326                         exit(0);
327                     }
328                     else if (!strcmp(argv[0]+2, "NO_ERROR") ||
329                              !strcmp(argv[0]+2, "NO_ERROR_ALL"))
330                     {
331                         for (itr = 0; itr < sizeof(opts.werror)/sizeof(opts.werror[0]); ++itr)
332                             opts.werror[itr] = 0;
333                         break;
334                     }
335                     else if (!strcmp(argv[0]+2, "ERROR") ||
336                              !strcmp(argv[0]+2, "ERROR_ALL"))
337                     {
338                         for (itr = 0; itr < sizeof(opts.werror)/sizeof(opts.werror[0]); ++itr)
339                             opts.werror[itr] = 0xFFFFFFFFL;
340                         break;
341                     }
342                     else if (!strcmp(argv[0]+2, "NONE")) {
343                         for (itr = 0; itr < sizeof(opts.warn)/sizeof(opts.warn[0]); ++itr)
344                             opts.warn[itr] = 0;
345                         break;
346                     }
347                     else if (!strcmp(argv[0]+2, "ALL")) {
348                         for (itr = 0; itr < sizeof(opts.warn)/sizeof(opts.warn[0]); ++itr)
349                             opts.warn[itr] = 0xFFFFFFFFL;
350                         break;
351                     }
352                     else if (!strncmp(argv[0]+2, "ERROR_", 6)) {
353                         if (!opts_setwerror(argv[0]+8, true)) {
354                             con_out("unknown warning: %s\n", argv[0]+2);
355                             return false;
356                         }
357                     }
358                     else if (!strncmp(argv[0]+2, "NO_ERROR_", 9)) {
359                         if (!opts_setwerror(argv[0]+11, false)) {
360                             con_out("unknown warning: %s\n", argv[0]+2);
361                             return false;
362                         }
363                     }
364                     else if (!strncmp(argv[0]+2, "NO_", 3)) {
365                         if (!opts_setwarn(argv[0]+5, false)) {
366                             con_out("unknown warning: %s\n", argv[0]+2);
367                             return false;
368                         }
369                     }
370                     else if (!opts_setwarn(argv[0]+2, true)) {
371                         con_out("unknown warning: %s\n", argv[0]+2);
372                         return false;
373                     }
374                     break;
375
376                 case 'O':
377                     if (!options_witharg(&argc, &argv, &argarg)) {
378                         con_out("option -O requires a numerical argument, or optimization name with an optional 'no-' prefix\n");
379                         return false;
380                     }
381                     if (isdigit(argarg[0])) {
382                         opts.O = atoi(argarg);
383                         opts_setoptimlevel(opts.O);
384                     } else {
385                         util_strtocmd(argarg, argarg, strlen(argarg)+1);
386                         if (!strcmp(argarg, "HELP")) {
387                             con_out("Possible optimizations:\n");
388                             for (itr = 0; itr < COUNT_OPTIMIZATIONS; ++itr) {
389                                 util_strtononcmd(opts_opt_list[itr].name, buffer, sizeof(buffer));
390                                 con_out(" -O%-20s (-O%u)\n", buffer, opts_opt_oflag[itr]);
391                             }
392                             exit(0);
393                         }
394                         else if (!strcmp(argarg, "ALL"))
395                             opts_setoptimlevel(opts.O = 9999);
396                         else if (!strncmp(argarg, "NO_", 3)) {
397                             if (!opts_setoptim(argarg+3, false)) {
398                                 con_out("unknown optimization: %s\n", argarg+3);
399                                 return false;
400                             }
401                         }
402                         else {
403                             if (!opts_setoptim(argarg, true)) {
404                                 con_out("unknown optimization: %s\n", argarg);
405                                 return false;
406                             }
407                         }
408                     }
409                     break;
410
411                 case 'o':
412                     if (!options_witharg(&argc, &argv, &argarg)) {
413                         con_out("option -o requires an argument: the output file name\n");
414                         return false;
415                     }
416                     opts.output = argarg;
417                     opts_output_wasset = true;
418                     break;
419
420                 case 'a':
421                 case 's':
422                     item.type = argv[0][1] == 'a' ? TYPE_ASM : TYPE_SRC;
423                     if (!options_witharg(&argc, &argv, &argarg)) {
424                         con_out("option -a requires a filename %s\n",
425                                 (argv[0][1] == 'a' ? "containing QC-asm" : "containing a progs.src formatted list"));
426                         return false;
427                     }
428                     item.filename = argarg;
429                     vec_push(items, item);
430                     break;
431
432                 case '-':
433                     if (!argv[0][2]) {
434                         /* anything following -- is considered a non-option argument */
435                         argend = true;
436                         break;
437                     }
438             /* All long options without arguments */
439                     else if (!strcmp(argv[0]+2, "help")) {
440                         usage();
441                         exit(0);
442                     }
443                     else if (!strcmp(argv[0]+2, "version")) {
444                         version();
445                         exit(0);
446                     }
447                     else if (!strcmp(argv[0]+2, "quiet")) {
448                         opts.quiet = true;
449                         break;
450                     }
451                     else {
452             /* All long options with arguments */
453                         if (options_long_witharg("output", &argc, &argv, &argarg)) {
454                             opts.output = argarg;
455                             opts_output_wasset = true;
456                         } else {
457                             con_out("Unknown parameter: %s\n", argv[0]);
458                             return false;
459                         }
460                     }
461                     break;
462
463                 default:
464                     con_out("Unknown parameter: %s\n", argv[0]);
465                     return false;
466             }
467         }
468         else
469         {
470             /* it's a QC filename */
471             item.filename = argv[0];
472             item.type     = TYPE_QC;
473             vec_push(items, item);
474         }
475     }
476     opts_ini_init(config);
477     return true;
478 }
479
480 /* returns the line number, or -1 on error */
481 static bool progs_nextline(char **out, size_t *alen,FILE *src) {
482     int    len;
483     char  *line;
484     char  *start;
485     char  *end;
486
487     line = *out;
488     len  = file_getline(&line, alen, src);
489     if (len == -1)
490         return false;
491
492     /* start at first non-blank */
493     for (start = line; isspace(*start); ++start) {}
494     /* end at the first non-blank */
495     for (end = start;  *end && !isspace(*end);  ++end)   {}
496
497     *out = line;
498     /* move the actual filename to the beginning */
499     while (start != end) {
500         *line++ = *start++;
501     }
502     *line = 0;
503     return true;
504 }
505
506 int main(int argc, char **argv) {
507     size_t itr;
508     int    retval           = 0;
509     bool   opts_output_free = false;
510     bool   operators_free   = false;
511     bool   progs_src        = false;
512     FILE  *outfile          = NULL;
513
514     app_name = argv[0];
515     con_init ();
516     opts_init("progs.dat", COMPILER_GMQCC, (1024 << 3));
517
518     util_seed(time(0));
519
520     if (!options_parse(argc, argv)) {
521         return usage();
522     }
523
524     if (OPTS_FLAG(TRUE_EMPTY_STRINGS) && OPTS_FLAG(FALSE_EMPTY_STRINGS)) {
525         con_err("-ftrue-empty-strings and -ffalse-empty-strings are mutually exclusive");
526         exit(1);
527     }
528
529     /* the standard decides which set of operators to use */
530     if (opts.standard == COMPILER_GMQCC) {
531         operators      = c_operators;
532         operator_count = c_operator_count;
533     } else if (opts.standard == COMPILER_FTEQCC) {
534         operators      = fte_operators;
535         operator_count = fte_operator_count;
536     } else {
537         operators      = qcc_operators;
538         operator_count = qcc_operator_count;
539     }
540
541     if (operators == fte_operators) {
542         /* fix ternary? */
543         if (OPTS_FLAG(CORRECT_TERNARY)) {
544             oper_info *newops;
545             if (operators[operator_count-2].id != opid1(',') ||
546                 operators[operator_count-1].id != opid2(':','?'))
547             {
548                 con_err("internal error: operator precedence table wasn't updated correctly!\n");
549                 exit(1);
550             }
551             operators_free = true;
552             newops = (oper_info*)mem_a(sizeof(operators[0]) * operator_count);
553             memcpy(newops, operators, sizeof(operators[0]) * operator_count);
554             memcpy(&newops[operator_count-2], &operators[operator_count-1], sizeof(newops[0]));
555             memcpy(&newops[operator_count-1], &operators[operator_count-2], sizeof(newops[0]));
556             newops[operator_count-2].prec = newops[operator_count-1].prec+1;
557             operators = newops;
558         }
559     }
560
561     if (opts.dump) {
562         for (itr = 0; itr < COUNT_FLAGS; ++itr)
563             con_out("Flag %s = %i\n",    opts_flag_list[itr].name, OPTS_FLAG(itr));
564         for (itr = 0; itr < COUNT_WARNINGS; ++itr)
565             con_out("Warning %s = %i\n", opts_warn_list[itr].name, OPTS_WARN(itr));
566         
567         con_out("output             = %s\n", opts.output);
568         con_out("optimization level = %d\n", opts.O);
569         con_out("standard           = %i\n", opts.standard);
570     }
571
572     if (opts.pp_only) {
573         if (opts_output_wasset) {
574             outfile = file_open(opts.output, "wb");
575             if (!outfile) {
576                 con_err("failed to open `%s` for writing\n", opts.output);
577                 retval = 1;
578                 goto cleanup;
579             }
580         }
581         else {
582             outfile = con_default_out();
583         }
584     }
585
586     if (!opts.pp_only) {
587         if (!parser_init()) {
588             con_err("failed to initialize parser\n");
589             retval = 1;
590             goto cleanup;
591         }
592     }
593
594     if (opts.pp_only || OPTS_FLAG(FTEPP)) {
595         if (!ftepp_init()) {
596             con_err("failed to initialize parser\n");
597             retval = 1;
598             goto cleanup;
599         }
600     }
601
602     if (OPTS_FLAG(TRUE_EMPTY_STRINGS))
603         type_not_instr[TYPE_STRING] = INSTR_NOT_F;
604
605     util_debug("COM", "starting ...\n");
606
607     /* add macros */
608     if (opts.pp_only || OPTS_FLAG(FTEPP)) {
609         for (itr = 0; itr < vec_size(ppems); itr++) {
610             ftepp_add_macro(ppems[itr].name, ppems[itr].value);
611             mem_d(ppems[itr].name);
612
613             /* can be null */
614             if (ppems[itr].value)
615                 mem_d(ppems[itr].value);
616         }
617     }
618
619     if (!vec_size(items)) {
620         FILE *src;
621         char *line;
622         size_t linelen = 0;
623
624         progs_src = true;
625
626         src = file_open("progs.src", "rb");
627         if (!src) {
628             con_err("failed to open `progs.src` for reading\n");
629             retval = 1;
630             goto cleanup;
631         }
632
633         line = NULL;
634         if (!progs_nextline(&line, &linelen, src) || !line[0]) {
635             con_err("illformatted progs.src file: expected output filename in first line\n");
636             retval = 1;
637             goto srcdone;
638         }
639
640         if (!opts_output_wasset) {
641             opts.output = util_strdup(line);
642             opts_output_free = true;
643         }
644
645         while (progs_nextline(&line, &linelen, src)) {
646             argitem item;
647             if (!line[0] || (line[0] == '/' && line[1] == '/'))
648                 continue;
649             item.filename = util_strdup(line);
650             item.type     = TYPE_QC;
651             vec_push(items, item);
652         }
653
654 srcdone:
655         file_close(src);
656         mem_d(line);
657     }
658
659     if (retval)
660         goto cleanup;
661
662     if (vec_size(items)) {
663         if (!opts.quiet && !opts.pp_only) {
664             con_out("Mode: %s\n", (progs_src ? "progs.src" : "manual"));
665             con_out("There are %lu items to compile:\n", (unsigned long)vec_size(items));
666         }
667         for (itr = 0; itr < vec_size(items); ++itr) {
668             if (!opts.quiet && !opts.pp_only) {
669                 con_out("  item: %s (%s)\n",
670                        items[itr].filename,
671                        ( (items[itr].type == TYPE_QC ? "qc" :
672                          (items[itr].type == TYPE_ASM ? "asm" :
673                          (items[itr].type == TYPE_SRC ? "progs.src" :
674                          ("unknown"))))));
675             }
676
677             if (opts.pp_only) {
678                 const char *out;
679                 if (!ftepp_preprocess_file(items[itr].filename)) {
680                     retval = 1;
681                     goto cleanup;
682                 }
683                 out = ftepp_get();
684                 if (out)
685                     file_printf(outfile, "%s", out);
686                 ftepp_flush();
687             }
688             else {
689                 if (OPTS_FLAG(FTEPP)) {
690                     const char *data;
691                     if (!ftepp_preprocess_file(items[itr].filename)) {
692                         retval = 1;
693                         goto cleanup;
694                     }
695                     data = ftepp_get();
696                     if (vec_size(data)) {
697                         if (!parser_compile_string(items[itr].filename, data, vec_size(data))) {
698                             retval = 1;
699                             goto cleanup;
700                         }
701                     }
702                     ftepp_flush();
703                 }
704                 else {
705                     if (!parser_compile_file(items[itr].filename)) {
706                         retval = 1;
707                         goto cleanup;
708                     }
709                 }
710             }
711
712             if (progs_src) {
713                 mem_d(items[itr].filename);
714                 items[itr].filename = NULL;
715             }
716         }
717
718         ftepp_finish();
719         if (!opts.pp_only) {
720             if (!parser_finish(opts.output)) {
721                 retval = 1;
722                 goto cleanup;
723             }
724         }
725     }
726
727     /* stuff */
728     if (!opts.quiet && !opts.pp_only) {
729         for (itr = 0; itr < COUNT_OPTIMIZATIONS; ++itr) {
730             if (opts_optimizationcount[itr]) {
731                 con_out("%s: %u\n", opts_opt_list[itr].name, (unsigned int)opts_optimizationcount[itr]);
732             }
733         }
734     }
735
736 cleanup:
737     util_debug("COM", "cleaning ...\n");
738     ftepp_finish();
739     con_close();
740     vec_free(items);
741     vec_free(ppems);
742
743     if (!opts.pp_only)
744         parser_cleanup();
745     if (opts_output_free)
746         mem_d((char*)opts.output);
747     if (operators_free)
748         mem_d((void*)operators);
749
750     lex_cleanup();
751     util_meminfo();
752     return retval;
753 }