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