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