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