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