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