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