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