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