]> de.git.xonotic.org Git - xonotic/gmqcc.git/blob - main.c
Print off the documentation when -[f/W/O]help, also fixed some of the descriptions...
[xonotic/gmqcc.git] / main.c
1 /*
2  * Copyright (C) 2012, 2013
3  *     Dale Weiler
4  *     Wolfgang Bumiller
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy of
7  * this software and associated documentation files (the "Software"), to deal in
8  * the Software without restriction, including without limitation the rights to
9  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10  * of the Software, and to permit persons to whom the Software is furnished to do
11  * so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #include "gmqcc.h"
25 #include "lexer.h"
26 #include <time.h>
27
28 /* TODO: cleanup this whole file .. it's a fuckign mess */
29
30 /* set by the standard */
31 const oper_info *operators      = NULL;
32 size_t           operator_count = 0;
33 static bool      opts_output_wasset = false;
34
35 typedef struct { char *filename; int   type;  } argitem;
36 typedef struct { char *name;     char *value; } ppitem;
37 static argitem *items = NULL;
38 static ppitem  *ppems = NULL;
39
40 #define TYPE_QC  0
41 #define TYPE_ASM 1
42 #define TYPE_SRC 2
43
44 static const char *app_name;
45
46 static void version() {
47     con_out("GMQCC %d.%d.%d Built %s %s\n",
48         GMQCC_VERSION_MAJOR,
49         GMQCC_VERSION_MINOR,
50         GMQCC_VERSION_PATCH,
51         __DATE__,
52         __TIME__
53     );
54 #ifdef GMQCC_GITINFO
55     con_out("git build: %s\n", GMQCC_GITINFO);
56 #elif defined(GMQCC_VERION_TYPE_DEVEL)
57     con_out("development build\n");
58 #endif
59 }
60
61 static int usage() {
62     con_out("usage: %s [options] [files...]", app_name);
63     con_out("options:\n"
64             "  -h, --help             show this help message\n"
65             "  -debug                 turns on compiler debug messages\n"
66             "  -memchk                turns on compiler memory leak check\n");
67     con_out("  -o, --output=file      output file, defaults to progs.dat\n"
68             "  -s filename            add a progs.src file to be used\n");
69     con_out("  -E                     stop after preprocessing\n");
70     con_out("  -q, --quiet            be less verbose\n");
71     con_out("  -config file           use the specified ini file\n");
72     con_out("  -std=standard          select one of the following standards\n"
73             "       -std=qcc          original QuakeC\n"
74             "       -std=fteqcc       fteqcc QuakeC\n"
75             "       -std=gmqcc        this compiler (default)\n");
76     con_out("  -f<flag>               enable a flag\n"
77             "  -fno-<flag>            disable a flag\n"
78             "  -fhelp                 list possible flags\n");
79     con_out("  -W<warning>            enable a warning\n"
80             "  -Wno-<warning>         disable a warning\n"
81             "  -Wall                  enable all warnings\n");
82     con_out("  -Werror                treat warnings as errors\n"
83             "  -Werror-<warning>      treat a warning as error\n"
84             "  -Wno-error-<warning>   opposite of the above\n");
85     con_out("  -Whelp                 list possible warnings\n");
86     con_out("  -O<number>             optimization level\n"
87             "  -O<name>               enable specific optimization\n"
88             "  -Ono-<name>            disable specific optimization\n"
89             "  -Ohelp                 list optimizations\n");
90     con_out("  -force-crc=num         force a specific checksum into the header\n");
91     return -1;
92 }
93
94 /* command line parsing */
95 static bool options_witharg(int *argc_, char ***argv_, char **out) {
96     int  argc   = *argc_;
97     char **argv = *argv_;
98
99     if (argv[0][2]) {
100         *out = argv[0]+2;
101         return true;
102     }
103     /* eat up the next */
104     if (argc < 2) /* no parameter was provided */
105         return false;
106
107     *out = argv[1];
108     --*argc_;
109     ++*argv_;
110     return true;
111 }
112
113 static bool options_long_witharg_all(const char *optname, int *argc_, char ***argv_, char **out, int ds, bool split) {
114     int  argc   = *argc_;
115     char **argv = *argv_;
116
117     size_t len = strlen(optname);
118
119     if (strncmp(argv[0]+ds, optname, len))
120         return false;
121
122     /* it's --optname, check how the parameter is supplied */
123     if (argv[0][ds+len] == '=') {
124         /* using --opt=param */
125         *out = argv[0]+ds+len+1;
126         return true;
127     }
128
129     if (!split || argc < ds) /* no parameter was provided, or only single-arg form accepted */
130         return false;
131
132     /* using --opt param */
133     *out = argv[1];
134     --*argc_;
135     ++*argv_;
136     return true;
137 }
138 static bool options_long_witharg(const char *optname, int *argc_, char ***argv_, char **out) {
139     return options_long_witharg_all(optname, argc_, argv_, out, 2, true);
140 }
141 static bool options_long_gcc(const char *optname, int *argc_, char ***argv_, char **out) {
142     return options_long_witharg_all(optname, argc_, argv_, out, 1, false);
143 }
144
145 static bool options_parse(int argc, char **argv) {
146     bool argend = false;
147     size_t itr;
148     char  buffer[1024];
149     char *redirout = NULL;
150     char *redirerr = NULL;
151     char *config   = NULL;
152
153     while (!argend && argc > 1) {
154         char *argarg;
155         argitem item;
156         ppitem  macro;
157
158         ++argv;
159         --argc;
160
161         if (argv[0][0] == '-') {
162             /* All gcc-type long options */
163             if (options_long_gcc("std", &argc, &argv, &argarg)) {
164                 if (!strcmp(argarg, "gmqcc") || !strcmp(argarg, "default")) {
165
166                     opts_set(opts.flags, ADJUST_VECTOR_FIELDS,          true);
167                     opts_set(opts.flags, CORRECT_LOGIC,                 true);
168                     opts_set(opts.flags, FALSE_EMPTY_STRINGS,           false);
169                     opts_set(opts.flags, TRUE_EMPTY_STRINGS,            true);
170                     opts_set(opts.flags, LOOP_LABELS,                   true);
171                     opts_set(opts.flags, TRANSLATABLE_STRINGS,          true);
172                     opts_set(opts.flags, INITIALIZED_NONCONSTANTS,      true);
173                     opts_set(opts.werror, WARN_INVALID_PARAMETER_COUNT, true);
174                     opts_set(opts.werror, WARN_MISSING_RETURN_VALUES,   true);
175                     opts.standard = COMPILER_GMQCC;
176
177                 } else if (!strcmp(argarg, "qcc")) {
178
179                     opts_set(opts.flags, ADJUST_VECTOR_FIELDS,  false);
180                     opts_set(opts.flags, ASSIGN_FUNCTION_TYPES, true);
181                     opts.standard = COMPILER_QCC;
182
183                 } else if (!strcmp(argarg, "fte") || !strcmp(argarg, "fteqcc")) {
184
185                     opts_set(opts.flags, FTEPP,                    true);
186                     opts_set(opts.flags, TRANSLATABLE_STRINGS,     true);
187                     opts_set(opts.flags, ADJUST_VECTOR_FIELDS,     false);
188                     opts_set(opts.flags, ASSIGN_FUNCTION_TYPES,    true);
189                     opts_set(opts.flags, CORRECT_TERNARY,          false);
190                     opts_set(opts.warn, WARN_TERNARY_PRECEDENCE,   true);
191                     opts.standard = COMPILER_FTEQCC;
192
193                 } else if (!strcmp(argarg, "qccx")) {
194
195                     opts_set(opts.flags, ADJUST_VECTOR_FIELDS,  false);
196                     opts.standard = COMPILER_QCCX;
197
198                 } else {
199                     con_out("Unknown standard: %s\n", argarg);
200                     return false;
201                 }
202                 continue;
203             }
204             if (options_long_gcc("force-crc", &argc, &argv, &argarg)) {
205                 opts.forcecrc   = true;
206                 opts.forced_crc = strtol(argarg, NULL, 0);
207                 continue;
208             }
209             if (options_long_gcc("redirout", &argc, &argv, &redirout)) {
210                 con_change(redirout, redirerr);
211                 continue;
212             }
213             if (options_long_gcc("redirerr", &argc, &argv, &redirerr)) {
214                 con_change(redirout, redirerr);
215                 continue;
216             }
217             if (options_long_gcc("config", &argc, &argv, &argarg)) {
218                 config = argarg;
219                 continue;
220             }
221
222             /* show defaults (like pathscale) */
223             if (!strcmp(argv[0]+1, "show-defaults")) {
224                 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
225                     if (!OPTS_FLAG(itr))
226                         continue;
227
228                     memset(buffer, 0, sizeof(buffer));
229                     util_strtononcmd(opts_flag_list[itr].name, buffer, strlen(opts_flag_list[itr].name) + 1);
230
231                     con_out("-f%s ", buffer);
232                 }
233                 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
234                     if (!OPTS_WARN(itr))
235                         continue;
236
237                     memset(buffer, 0, sizeof(buffer));
238                     util_strtononcmd(opts_warn_list[itr].name, buffer, strlen(opts_warn_list[itr].name) + 1);
239                     con_out("-W%s ", buffer);
240                 }
241                 con_out("\n");
242                 exit(0);
243             }
244
245             if (!strcmp(argv[0]+1, "debug")) {
246                 opts.debug = true;
247                 continue;
248             }
249             if (!strcmp(argv[0]+1, "dump")) {
250                 opts.dump = true;
251                 continue;
252             }
253             if (!strcmp(argv[0]+1, "dumpfin")) {
254                 opts.dumpfin = true;
255                 continue;
256             }
257             if (!strcmp(argv[0]+1, "memchk")) {
258                 opts.memchk = true;
259                 continue;
260             }
261             if (!strcmp(argv[0]+1, "nocolor")) {
262                 con_color(0);
263                 continue;
264             }
265
266             switch (argv[0][1]) {
267                 /* -h, show usage but exit with 0 */
268                 case 'h':
269                     usage();
270                     exit(0);
271                     /* break; never reached because of exit(0) */
272
273                 case 'v':
274                     version();
275                     exit(0);
276
277                 case 'E':
278                     opts.pp_only = true;
279                     opts_set(opts.flags, FTEPP_PREDEFS, true); /* predefs on for -E */
280                     break;
281
282                 /* debug turns on -flno */
283                 case 'g':
284                     opts_setflag("LNO", true);
285                     opts.g = true;
286                     break;
287
288                 case 'q':
289                     opts.quiet = true;
290                     break;
291
292                 case 'D':
293                     if (!strlen(argv[0]+2)) {
294                         con_err("expected name after -D\n");
295                         exit(0);
296                     }
297
298                     if (!(argarg = strchr(argv[0] + 2, '='))) {
299                         macro.name  = util_strdup(argv[0]+2);
300                         macro.value = NULL;
301                     } else {
302                         *argarg='\0'; /* terminate for name */
303                         macro.name  = util_strdup(argv[0]+2);
304                         macro.value = util_strdup(argarg+1);
305                     }
306                     vec_push(ppems, macro);
307                     break;
308
309                 /* handle all -fflags */
310                 case 'f':
311                     util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
312                     if (!strcmp(argv[0]+2, "HELP") || *(argv[0]+2) == '?') {
313                         con_out("Possible flags:\n\n");
314                         for (itr = 0; itr < COUNT_FLAGS; ++itr) {
315                             util_strtononcmd(opts_flag_list[itr].name, buffer, sizeof(buffer));
316                             con_out(" -f%s:\n%s\n\n", buffer, opts_flag_list[itr].description);
317                         }
318                         exit(0);
319                     }
320                     else if (!strncmp(argv[0]+2, "NO_", 3)) {
321                         if (!opts_setflag(argv[0]+5, false)) {
322                             con_out("unknown flag: %s\n", argv[0]+2);
323                             return false;
324                         }
325                     }
326                     else if (!opts_setflag(argv[0]+2, true)) {
327                         con_out("unknown flag: %s\n", argv[0]+2);
328                         return false;
329                     }
330                     break;
331                 case 'W':
332                     util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
333                     if (!strcmp(argv[0]+2, "HELP") || *(argv[0]+2) == '?') {
334                         con_out("Possible warnings:\n");
335                         for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
336                             util_strtononcmd(opts_warn_list[itr].name, buffer, sizeof(buffer));
337                             con_out(" -W%s:\n%s\n\n\n", buffer, opts_warn_list[itr].description);
338                         }
339                         exit(0);
340                     }
341                     else if (!strcmp(argv[0]+2, "NO_ERROR") ||
342                              !strcmp(argv[0]+2, "NO_ERROR_ALL"))
343                     {
344                         for (itr = 0; itr < sizeof(opts.werror)/sizeof(opts.werror[0]); ++itr)
345                             opts.werror[itr] = 0;
346                         break;
347                     }
348                     else if (!strcmp(argv[0]+2, "ERROR") ||
349                              !strcmp(argv[0]+2, "ERROR_ALL"))
350                     {
351                         opts_backup_non_Werror_all();
352                         for (itr = 0; itr < sizeof(opts.werror)/sizeof(opts.werror[0]); ++itr)
353                             opts.werror[itr] = 0xFFFFFFFFL;
354                         opts_restore_non_Werror_all();
355                         break;
356                     }
357                     else if (!strcmp(argv[0]+2, "NONE")) {
358                         for (itr = 0; itr < sizeof(opts.warn)/sizeof(opts.warn[0]); ++itr)
359                             opts.warn[itr] = 0;
360                         break;
361                     }
362                     else if (!strcmp(argv[0]+2, "ALL")) {
363                         opts_backup_non_Wall();
364                         for (itr = 0; itr < sizeof(opts.warn)/sizeof(opts.warn[0]); ++itr)
365                             opts.warn[itr] = 0xFFFFFFFFL;
366                         opts_restore_non_Wall();
367                         break;
368                     }
369                     else if (!strncmp(argv[0]+2, "ERROR_", 6)) {
370                         if (!opts_setwerror(argv[0]+8, true)) {
371                             con_out("unknown warning: %s\n", argv[0]+2);
372                             return false;
373                         }
374                     }
375                     else if (!strncmp(argv[0]+2, "NO_ERROR_", 9)) {
376                         if (!opts_setwerror(argv[0]+11, false)) {
377                             con_out("unknown warning: %s\n", argv[0]+2);
378                             return false;
379                         }
380                     }
381                     else if (!strncmp(argv[0]+2, "NO_", 3)) {
382                         if (!opts_setwarn(argv[0]+5, false)) {
383                             con_out("unknown warning: %s\n", argv[0]+2);
384                             return false;
385                         }
386                     }
387                     else if (!opts_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                         opts_setoptimlevel(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%s\n\n", buffer, opts_opt_oflag[itr], opts_opt_list[itr].description);
408                             }
409                             exit(0);
410                         }
411                         else if (!strcmp(argarg, "ALL"))
412                             opts_setoptimlevel(opts.O = 9999);
413                         else if (!strncmp(argarg, "NO_", 3)) {
414                             if (!opts_setoptim(argarg+3, false)) {
415                                 con_out("unknown optimization: %s\n", argarg+3);
416                                 return false;
417                             }
418                         }
419                         else {
420                             if (!opts_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 if (!strcmp(argv[0]+2, "quiet")) {
465                         opts.quiet = true;
466                         break;
467                     }
468                     else if (!strcmp(argv[0]+2, "correct")) {
469                         opts.correction = true;
470                         break;
471                     }
472                     else if (!strcmp(argv[0]+2, "no-correct")) {
473                         opts.correction = false;
474                         break;
475                     }
476                     else if (!strcmp(argv[0]+2, "add-info")) {
477                         opts.add_info = true;
478                         break;
479                     }
480                     else {
481             /* All long options with arguments */
482                         if (options_long_witharg("output", &argc, &argv, &argarg)) {
483                             opts.output = argarg;
484                             opts_output_wasset = true;
485                         } else {
486                             con_out("Unknown parameter: %s\n", argv[0]);
487                             return false;
488                         }
489                     }
490                     break;
491
492                 default:
493                     con_out("Unknown parameter: %s\n", argv[0]);
494                     return false;
495             }
496         }
497         else
498         {
499             /* it's a QC filename */
500             item.filename = argv[0];
501             item.type     = TYPE_QC;
502             vec_push(items, item);
503         }
504     }
505     opts_ini_init(config);
506     return true;
507 }
508
509 /* returns the line number, or -1 on error */
510 static bool progs_nextline(char **out, size_t *alen,FILE *src) {
511     int    len;
512     char  *line;
513     char  *start;
514     char  *end;
515
516     line = *out;
517     len  = file_getline(&line, alen, src);
518     if (len == -1)
519         return false;
520
521     /* start at first non-blank */
522     for (start = line; isspace(*start); ++start) {}
523     /* end at the first non-blank */
524     for (end = start;  *end && !isspace(*end);  ++end)   {}
525
526     *out = line;
527     /* move the actual filename to the beginning */
528     while (start != end) {
529         *line++ = *start++;
530     }
531     *line = 0;
532     return true;
533 }
534
535 int main(int argc, char **argv) {
536     size_t itr;
537     int    retval           = 0;
538     bool   opts_output_free = false;
539     bool   operators_free   = false;
540     bool   progs_src        = false;
541     FILE  *outfile          = NULL;
542
543     app_name = argv[0];
544     con_init ();
545     opts_init("progs.dat", COMPILER_GMQCC, (1024 << 3));
546
547     util_seed(time(0));
548
549     if (!options_parse(argc, argv)) {
550         return usage();
551     }
552
553     if (OPTS_FLAG(TRUE_EMPTY_STRINGS) && OPTS_FLAG(FALSE_EMPTY_STRINGS)) {
554         con_err("-ftrue-empty-strings and -ffalse-empty-strings are mutually exclusive");
555         exit(EXIT_FAILURE);
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(EXIT_FAILURE);
579             }
580             operators_free = true;
581             newops = (oper_info*)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         for (itr = 0; itr < COUNT_WARNINGS; ++itr)
594             con_out("Warning %s = %i\n", opts_warn_list[itr].name, OPTS_WARN(itr));
595
596         con_out("output             = %s\n", opts.output);
597         con_out("optimization level = %d\n", opts.O);
598         con_out("standard           = %i\n", opts.standard);
599     }
600
601     if (opts.pp_only) {
602         if (opts_output_wasset) {
603             outfile = file_open(opts.output, "wb");
604             if (!outfile) {
605                 con_err("failed to open `%s` for writing\n", opts.output);
606                 retval = 1;
607                 goto cleanup;
608             }
609         }
610         else {
611             outfile = con_default_out();
612         }
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         }
629     }
630
631     if (OPTS_FLAG(TRUE_EMPTY_STRINGS))
632         type_not_instr[TYPE_STRING] = INSTR_NOT_F;
633
634     util_debug("COM", "starting ...\n");
635
636     /* add macros */
637     if (opts.pp_only || OPTS_FLAG(FTEPP)) {
638         for (itr = 0; itr < vec_size(ppems); itr++) {
639             ftepp_add_macro(ppems[itr].name, ppems[itr].value);
640             mem_d(ppems[itr].name);
641
642             /* can be null */
643             if (ppems[itr].value)
644                 mem_d(ppems[itr].value);
645         }
646     }
647
648     if (!vec_size(items)) {
649         FILE *src;
650         char *line;
651         size_t linelen = 0;
652
653         progs_src = true;
654
655         src = file_open("progs.src", "rb");
656         if (!src) {
657             con_err("failed to open `progs.src` for reading\n");
658             retval = 1;
659             goto cleanup;
660         }
661
662         line = NULL;
663         if (!progs_nextline(&line, &linelen, src) || !line[0]) {
664             con_err("illformatted progs.src file: expected output filename in first line\n");
665             retval = 1;
666             goto srcdone;
667         }
668
669         if (!opts_output_wasset) {
670             opts.output = util_strdup(line);
671             opts_output_free = true;
672         }
673
674         while (progs_nextline(&line, &linelen, src)) {
675             argitem item;
676             if (!line[0] || (line[0] == '/' && line[1] == '/'))
677                 continue;
678             item.filename = util_strdup(line);
679             item.type     = TYPE_QC;
680             vec_push(items, item);
681         }
682
683 srcdone:
684         file_close(src);
685         mem_d(line);
686     }
687
688     if (retval)
689         goto cleanup;
690
691     if (vec_size(items)) {
692         if (!opts.quiet && !opts.pp_only) {
693             con_out("Mode: %s\n", (progs_src ? "progs.src" : "manual"));
694             con_out("There are %lu items to compile:\n", (unsigned long)vec_size(items));
695         }
696         for (itr = 0; itr < vec_size(items); ++itr) {
697             if (!opts.quiet && !opts.pp_only) {
698                 con_out("  item: %s (%s)\n",
699                        items[itr].filename,
700                        ( (items[itr].type == TYPE_QC ? "qc" :
701                          (items[itr].type == TYPE_ASM ? "asm" :
702                          (items[itr].type == TYPE_SRC ? "progs.src" :
703                          ("unknown"))))));
704             }
705
706             if (opts.pp_only) {
707                 const char *out;
708                 if (!ftepp_preprocess_file(items[itr].filename)) {
709                     retval = 1;
710                     goto cleanup;
711                 }
712                 out = ftepp_get();
713                 if (out)
714                     file_printf(outfile, "%s", out);
715                 ftepp_flush();
716             }
717             else {
718                 if (OPTS_FLAG(FTEPP)) {
719                     const char *data;
720                     if (!ftepp_preprocess_file(items[itr].filename)) {
721                         retval = 1;
722                         goto cleanup;
723                     }
724                     data = ftepp_get();
725                     if (vec_size(data)) {
726                         if (!parser_compile_string(items[itr].filename, data, vec_size(data))) {
727                             retval = 1;
728                             goto cleanup;
729                         }
730                     }
731                     ftepp_flush();
732                 }
733                 else {
734                     if (!parser_compile_file(items[itr].filename)) {
735                         retval = 1;
736                         goto cleanup;
737                     }
738                 }
739             }
740
741             if (progs_src) {
742                 mem_d(items[itr].filename);
743                 items[itr].filename = NULL;
744             }
745         }
746
747         ftepp_finish();
748         if (!opts.pp_only) {
749             if (!parser_finish(opts.output)) {
750                 retval = 1;
751                 goto cleanup;
752             }
753         }
754     }
755
756     /* stuff */
757     if (!opts.quiet && !opts.pp_only) {
758         for (itr = 0; itr < COUNT_OPTIMIZATIONS; ++itr) {
759             if (opts_optimizationcount[itr]) {
760                 con_out("%s: %u\n", opts_opt_list[itr].name, (unsigned int)opts_optimizationcount[itr]);
761             }
762         }
763     }
764
765 cleanup:
766     util_debug("COM", "cleaning ...\n");
767     ftepp_finish();
768     con_close();
769     vec_free(items);
770     vec_free(ppems);
771
772     if (!opts.pp_only)
773         parser_cleanup();
774     if (opts_output_free)
775         mem_d((char*)opts.output);
776     if (operators_free)
777         mem_d((void*)operators);
778
779     lex_cleanup();
780     util_meminfo();
781     return retval;
782 }