]> de.git.xonotic.org Git - xonotic/gmqcc.git/blob - ftepp.c
added -Wcpp (for turning off cpp warnings defined with #warning like GCC/clang/pathsc...
[xonotic/gmqcc.git] / ftepp.c
1 /*
2  * Copyright (C) 2012
3  *     Wolfgang Bumiller
4  *     Dale Weiler 
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 typedef struct {
28     bool on;
29     bool was_on;
30     bool had_else;
31 } ppcondition;
32
33 typedef struct {
34     int   token;
35     char *value;
36     /* a copy from the lexer */
37     union {
38         vector v;
39         int    i;
40         double f;
41         int    t; /* type */
42     } constval;
43 } pptoken;
44
45 typedef struct {
46     lex_ctx ctx;
47
48     char   *name;
49     char  **params;
50     /* yes we need an extra flag since `#define FOO x` is not the same as `#define FOO() x` */
51     bool    has_params;
52
53     pptoken **output;
54 } ppmacro;
55
56 typedef struct {
57     lex_file    *lex;
58     int          token;
59     unsigned int errors;
60
61     bool         output_on;
62     ppcondition *conditions;
63     ppmacro    **macros;
64
65     char        *output_string;
66
67     char        *itemname;
68     char        *includename;
69 } ftepp_t;
70
71 #define ftepp_tokval(f) ((f)->lex->tok.value)
72 #define ftepp_ctx(f)    ((f)->lex->tok.ctx)
73
74 static void ftepp_errorat(ftepp_t *ftepp, lex_ctx ctx, const char *fmt, ...)
75 {
76     va_list ap;
77
78     ftepp->errors++;
79
80     va_start(ap, fmt);
81     con_cvprintmsg((void*)&ctx, LVL_ERROR, "error", fmt, ap);
82     va_end(ap);
83 }
84
85 static void ftepp_error(ftepp_t *ftepp, const char *fmt, ...)
86 {
87     va_list ap;
88
89     ftepp->errors++;
90
91     va_start(ap, fmt);
92     con_cvprintmsg((void*)&ftepp->lex->tok.ctx, LVL_ERROR, "error", fmt, ap);
93     va_end(ap);
94 }
95
96 static bool GMQCC_WARN ftepp_warn(ftepp_t *ftepp, int warntype, const char *fmt, ...)
97 {
98     bool    r;
99     va_list ap;
100
101     va_start(ap, fmt);
102     r = vcompile_warning(ftepp->lex->tok.ctx, warntype, fmt, ap);
103     va_end(ap);
104     return r;
105 }
106
107 static pptoken *pptoken_make(ftepp_t *ftepp)
108 {
109     pptoken *token = (pptoken*)mem_a(sizeof(pptoken));
110     token->token = ftepp->token;
111 #if 0
112     if (token->token == TOKEN_WHITE)
113         token->value = util_strdup(" ");
114     else
115 #else
116         token->value = util_strdup(ftepp_tokval(ftepp));
117 #endif
118     memcpy(&token->constval, &ftepp->lex->tok.constval, sizeof(token->constval));
119     return token;
120 }
121
122 static void pptoken_delete(pptoken *self)
123 {
124     mem_d(self->value);
125     mem_d(self);
126 }
127
128 static ppmacro *ppmacro_new(lex_ctx ctx, const char *name)
129 {
130     ppmacro *macro = (ppmacro*)mem_a(sizeof(ppmacro));
131
132     (void)ctx;
133     memset(macro, 0, sizeof(*macro));
134     macro->name = util_strdup(name);
135     return macro;
136 }
137
138 static void ppmacro_delete(ppmacro *self)
139 {
140     size_t i;
141     for (i = 0; i < vec_size(self->params); ++i)
142         mem_d(self->params[i]);
143     vec_free(self->params);
144     for (i = 0; i < vec_size(self->output); ++i)
145         pptoken_delete(self->output[i]);
146     vec_free(self->output);
147     mem_d(self->name);
148     mem_d(self);
149 }
150
151 static ftepp_t* ftepp_new()
152 {
153     ftepp_t *ftepp;
154
155     ftepp = (ftepp_t*)mem_a(sizeof(*ftepp));
156     memset(ftepp, 0, sizeof(*ftepp));
157
158     ftepp->output_on = true;
159
160     return ftepp;
161 }
162
163 static void ftepp_delete(ftepp_t *self)
164 {
165     size_t i;
166     if (self->itemname)
167         mem_d(self->itemname);
168     if (self->includename)
169         vec_free(self->includename);
170     for (i = 0; i < vec_size(self->macros); ++i)
171         ppmacro_delete(self->macros[i]);
172     vec_free(self->macros);
173     vec_free(self->conditions);
174     if (self->lex)
175         lex_close(self->lex);
176     mem_d(self);
177 }
178
179 static void ftepp_out(ftepp_t *ftepp, const char *str, bool ignore_cond)
180 {
181     if (ignore_cond || ftepp->output_on)
182     {
183         size_t len;
184         char  *data;
185         len = strlen(str);
186         data = vec_add(ftepp->output_string, len);
187         memcpy(data, str, len);
188     }
189 }
190
191 static void ftepp_update_output_condition(ftepp_t *ftepp)
192 {
193     size_t i;
194     ftepp->output_on = true;
195     for (i = 0; i < vec_size(ftepp->conditions); ++i)
196         ftepp->output_on = ftepp->output_on && ftepp->conditions[i].on;
197 }
198
199 static ppmacro* ftepp_macro_find(ftepp_t *ftepp, const char *name)
200 {
201     size_t i;
202     for (i = 0; i < vec_size(ftepp->macros); ++i) {
203         if (!strcmp(name, ftepp->macros[i]->name))
204             return ftepp->macros[i];
205     }
206     return NULL;
207 }
208
209 static void ftepp_macro_delete(ftepp_t *ftepp, const char *name)
210 {
211     size_t i;
212     for (i = 0; i < vec_size(ftepp->macros); ++i) {
213         if (!strcmp(name, ftepp->macros[i]->name)) {
214             vec_remove(ftepp->macros, i, 1);
215             return;
216         }
217     }
218 }
219
220 static inline int ftepp_next(ftepp_t *ftepp)
221 {
222     return (ftepp->token = lex_do(ftepp->lex));
223 }
224
225 /* Important: this does not skip newlines! */
226 static bool ftepp_skipspace(ftepp_t *ftepp)
227 {
228     if (ftepp->token != TOKEN_WHITE)
229         return true;
230     while (ftepp_next(ftepp) == TOKEN_WHITE) {}
231     if (ftepp->token >= TOKEN_EOF) {
232         ftepp_error(ftepp, "unexpected end of preprocessor directive");
233         return false;
234     }
235     return true;
236 }
237
238 /* this one skips EOLs as well */
239 static bool ftepp_skipallwhite(ftepp_t *ftepp)
240 {
241     if (ftepp->token != TOKEN_WHITE && ftepp->token != TOKEN_EOL)
242         return true;
243     do {
244         ftepp_next(ftepp);
245     } while (ftepp->token == TOKEN_WHITE || ftepp->token == TOKEN_EOL);
246     if (ftepp->token >= TOKEN_EOF) {
247         ftepp_error(ftepp, "unexpected end of preprocessor directive");
248         return false;
249     }
250     return true;
251 }
252
253 /**
254  * The huge macro parsing code...
255  */
256 static bool ftepp_define_params(ftepp_t *ftepp, ppmacro *macro)
257 {
258     do {
259         ftepp_next(ftepp);
260         if (!ftepp_skipspace(ftepp))
261             return false;
262         if (ftepp->token == ')')
263             break;
264         switch (ftepp->token) {
265             case TOKEN_IDENT:
266             case TOKEN_TYPENAME:
267             case TOKEN_KEYWORD:
268                 break;
269             default:
270                 ftepp_error(ftepp, "unexpected token in parameter list");
271                 return false;
272         }
273         vec_push(macro->params, util_strdup(ftepp_tokval(ftepp)));
274         ftepp_next(ftepp);
275         if (!ftepp_skipspace(ftepp))
276             return false;
277     } while (ftepp->token == ',');
278     if (ftepp->token != ')') {
279         ftepp_error(ftepp, "expected closing paren after macro parameter list");
280         return false;
281     }
282     ftepp_next(ftepp);
283     /* skipspace happens in ftepp_define */
284     return true;
285 }
286
287 static bool ftepp_define_body(ftepp_t *ftepp, ppmacro *macro)
288 {
289     pptoken *ptok;
290     while (ftepp->token != TOKEN_EOL && ftepp->token < TOKEN_EOF) {
291         ptok = pptoken_make(ftepp);
292         vec_push(macro->output, ptok);
293         ftepp_next(ftepp);
294     }
295     /* recursive expansion can cause EOFs here */
296     if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
297         ftepp_error(ftepp, "unexpected junk after macro or unexpected end of file");
298         return false;
299     }
300     return true;
301 }
302
303 static bool ftepp_define(ftepp_t *ftepp)
304 {
305     ppmacro *macro;
306     size_t l = ftepp_ctx(ftepp).line;
307
308     (void)ftepp_next(ftepp);
309     if (!ftepp_skipspace(ftepp))
310         return false;
311
312     switch (ftepp->token) {
313         case TOKEN_IDENT:
314         case TOKEN_TYPENAME:
315         case TOKEN_KEYWORD:
316             macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
317             if (macro && ftepp->output_on) {
318                 if (ftepp_warn(ftepp, WARN_PREPROCESSOR, "redefining `%s`", ftepp_tokval(ftepp)))
319                     return false;
320                 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
321             }
322             macro = ppmacro_new(ftepp_ctx(ftepp), ftepp_tokval(ftepp));
323             break;
324         default:
325             ftepp_error(ftepp, "expected macro name");
326             return false;
327     }
328
329     (void)ftepp_next(ftepp);
330
331     if (ftepp->token == '(') {
332         macro->has_params = true;
333         if (!ftepp_define_params(ftepp, macro))
334             return false;
335     }
336
337     if (!ftepp_skipspace(ftepp))
338         return false;
339
340     if (!ftepp_define_body(ftepp, macro))
341         return false;
342
343     if (ftepp->output_on)
344         vec_push(ftepp->macros, macro);
345     else {
346         ppmacro_delete(macro);
347     }
348
349     for (; l < ftepp_ctx(ftepp).line; ++l)
350         ftepp_out(ftepp, "\n", true);
351     return true;
352 }
353
354 /**
355  * When a macro is used we have to handle parameters as well
356  * as special-concatenation via ## or stringification via #
357  *
358  * Note: parenthesis can nest, so FOO((a),b) is valid, but only
359  * this kind of parens. Curly braces or [] don't count towards the
360  * paren-level.
361  */
362 typedef struct {
363     pptoken **tokens;
364 } macroparam;
365
366 static void macroparam_clean(macroparam *self)
367 {
368     size_t i;
369     for (i = 0; i < vec_size(self->tokens); ++i)
370         pptoken_delete(self->tokens[i]);
371     vec_free(self->tokens);
372 }
373
374 /* need to leave the last token up */
375 static bool ftepp_macro_call_params(ftepp_t *ftepp, macroparam **out_params)
376 {
377     macroparam *params = NULL;
378     pptoken    *ptok;
379     macroparam  mp;
380     size_t      parens = 0;
381     size_t      i;
382
383     if (!ftepp_skipallwhite(ftepp))
384         return false;
385     while (ftepp->token != ')') {
386         mp.tokens = NULL;
387         if (!ftepp_skipallwhite(ftepp))
388             return false;
389         while (parens || ftepp->token != ',') {
390             if (ftepp->token == '(')
391                 ++parens;
392             else if (ftepp->token == ')') {
393                 if (!parens)
394                     break;
395                 --parens;
396             }
397             ptok = pptoken_make(ftepp);
398             vec_push(mp.tokens, ptok);
399             if (ftepp_next(ftepp) >= TOKEN_EOF) {
400                 ftepp_error(ftepp, "unexpected EOF in macro call");
401                 goto on_error;
402             }
403         }
404         vec_push(params, mp);
405         mp.tokens = NULL;
406         if (ftepp->token == ')')
407             break;
408         if (ftepp->token != ',') {
409             ftepp_error(ftepp, "expected closing paren or comma in macro call");
410             goto on_error;
411         }
412         if (ftepp_next(ftepp) >= TOKEN_EOF) {
413             ftepp_error(ftepp, "unexpected EOF in macro call");
414             goto on_error;
415         }
416     }
417     /* need to leave that up
418     if (ftepp_next(ftepp) >= TOKEN_EOF) {
419         ftepp_error(ftepp, "unexpected EOF in macro call");
420         goto on_error;
421     }
422     */
423     *out_params = params;
424     return true;
425
426 on_error:
427     if (mp.tokens)
428         macroparam_clean(&mp);
429     for (i = 0; i < vec_size(params); ++i)
430         macroparam_clean(&params[i]);
431     vec_free(params);
432     return false;
433 }
434
435 static bool macro_params_find(ppmacro *macro, const char *name, size_t *idx)
436 {
437     size_t i;
438     for (i = 0; i < vec_size(macro->params); ++i) {
439         if (!strcmp(macro->params[i], name)) {
440             *idx = i;
441             return true;
442         }
443     }
444     return false;
445 }
446
447 static void ftepp_stringify_token(ftepp_t *ftepp, pptoken *token)
448 {
449     char        chs[2];
450     const char *ch;
451     chs[1] = 0;
452     switch (token->token) {
453         case TOKEN_STRINGCONST:
454             ch = token->value;
455             while (*ch) {
456                 /* in preprocessor mode strings already are string,
457                  * so we don't get actual newline bytes here.
458                  * Still need to escape backslashes and quotes.
459                  */
460                 switch (*ch) {
461                     case '\\': ftepp_out(ftepp, "\\\\", false); break;
462                     case '"':  ftepp_out(ftepp, "\\\"", false); break;
463                     default:
464                         chs[0] = *ch;
465                         ftepp_out(ftepp, chs, false);
466                         break;
467                 }
468                 ++ch;
469             }
470             break;
471         case TOKEN_WHITE:
472             ftepp_out(ftepp, " ", false);
473             break;
474         case TOKEN_EOL:
475             ftepp_out(ftepp, "\\n", false);
476             break;
477         default:
478             ftepp_out(ftepp, token->value, false);
479             break;
480     }
481 }
482
483 static void ftepp_stringify(ftepp_t *ftepp, macroparam *param)
484 {
485     size_t i;
486     ftepp_out(ftepp, "\"", false);
487     for (i = 0; i < vec_size(param->tokens); ++i)
488         ftepp_stringify_token(ftepp, param->tokens[i]);
489     ftepp_out(ftepp, "\"", false);
490 }
491
492 static void ftepp_recursion_header(ftepp_t *ftepp)
493 {
494     ftepp_out(ftepp, "\n#pragma push(line)\n", false);
495 }
496
497 static void ftepp_recursion_footer(ftepp_t *ftepp)
498 {
499     ftepp_out(ftepp, "\n#pragma pop(line)\n", false);
500 }
501
502 static bool ftepp_preprocess(ftepp_t *ftepp);
503 static bool ftepp_macro_expand(ftepp_t *ftepp, ppmacro *macro, macroparam *params)
504 {
505     char     *old_string = ftepp->output_string;
506     lex_file *old_lexer = ftepp->lex;
507     bool retval = true;
508
509     size_t    o, pi, pv;
510     lex_file *inlex;
511
512     int nextok;
513
514     /* really ... */
515     if (!vec_size(macro->output))
516         return true;
517
518     ftepp->output_string = NULL;
519     for (o = 0; o < vec_size(macro->output); ++o) {
520         pptoken *out = macro->output[o];
521         switch (out->token) {
522             case TOKEN_IDENT:
523             case TOKEN_TYPENAME:
524             case TOKEN_KEYWORD:
525                 if (!macro_params_find(macro, out->value, &pi)) {
526                     ftepp_out(ftepp, out->value, false);
527                     break;
528                 } else {
529                     for (pv = 0; pv < vec_size(params[pi].tokens); ++pv) {
530                         out = params[pi].tokens[pv];
531                         if (out->token == TOKEN_EOL)
532                             ftepp_out(ftepp, "\n", false);
533                         else
534                             ftepp_out(ftepp, out->value, false);
535                     }
536                 }
537                 break;
538             case '#':
539                 if (o + 1 < vec_size(macro->output)) {
540                     nextok = macro->output[o+1]->token;
541                     if (nextok == '#') {
542                         /* raw concatenation */
543                         ++o;
544                         break;
545                     }
546                     if ( (nextok == TOKEN_IDENT    ||
547                           nextok == TOKEN_KEYWORD  ||
548                           nextok == TOKEN_TYPENAME) &&
549                         macro_params_find(macro, macro->output[o+1]->value, &pi))
550                     {
551                         ++o;
552                         ftepp_stringify(ftepp, &params[pi]);
553                         break;
554                     }
555                 }
556                 ftepp_out(ftepp, "#", false);
557                 break;
558             case TOKEN_EOL:
559                 ftepp_out(ftepp, "\n", false);
560                 break;
561             default:
562                 ftepp_out(ftepp, out->value, false);
563                 break;
564         }
565     }
566     vec_push(ftepp->output_string, 0);
567     /* Now run the preprocessor recursively on this string buffer */
568     /*
569     printf("__________\n%s\n=========\n", ftepp->output_string);
570     */
571     inlex = lex_open_string(ftepp->output_string, vec_size(ftepp->output_string)-1, ftepp->lex->name);
572     if (!inlex) {
573         ftepp_error(ftepp, "internal error: failed to instantiate lexer");
574         retval = false;
575         goto cleanup;
576     }
577     ftepp->output_string = old_string;
578     ftepp->lex = inlex;
579     ftepp_recursion_header(ftepp);
580     if (!ftepp_preprocess(ftepp)) {
581         lex_close(ftepp->lex);
582         retval = false;
583         goto cleanup;
584     }
585     ftepp_recursion_footer(ftepp);
586     old_string = ftepp->output_string;
587
588 cleanup:
589     ftepp->lex           = old_lexer;
590     ftepp->output_string = old_string;
591     return retval;
592 }
593
594 static bool ftepp_macro_call(ftepp_t *ftepp, ppmacro *macro)
595 {
596     size_t     o;
597     macroparam *params = NULL;
598     bool        retval = true;
599
600     if (!macro->has_params) {
601         if (!ftepp_macro_expand(ftepp, macro, NULL))
602             return false;
603         ftepp_next(ftepp);
604         return true;
605     }
606     ftepp_next(ftepp);
607
608     if (!ftepp_skipallwhite(ftepp))
609         return false;
610
611     if (ftepp->token != '(') {
612         ftepp_error(ftepp, "expected macro parameters in parenthesis");
613         return false;
614     }
615
616     ftepp_next(ftepp);
617     if (!ftepp_macro_call_params(ftepp, &params))
618         return false;
619
620     if (vec_size(params) != vec_size(macro->params)) {
621         ftepp_error(ftepp, "macro %s expects %u paramteters, %u provided", macro->name,
622                     (unsigned int)vec_size(macro->params),
623                     (unsigned int)vec_size(params));
624         retval = false;
625         goto cleanup;
626     }
627
628     if (!ftepp_macro_expand(ftepp, macro, params))
629         retval = false;
630     ftepp_next(ftepp);
631
632 cleanup:
633     for (o = 0; o < vec_size(params); ++o)
634         macroparam_clean(&params[o]);
635     vec_free(params);
636     return retval;
637 }
638
639 /**
640  * #if - the FTEQCC way:
641  *    defined(FOO) => true if FOO was #defined regardless of parameters or contents
642  *    <numbers>    => True if the number is not 0
643  *    !<factor>    => True if the factor yields false
644  *    !!<factor>   => ERROR on 2 or more unary nots
645  *    <macro>      => becomes the macro's FIRST token regardless of parameters
646  *    <e> && <e>   => True if both expressions are true
647  *    <e> || <e>   => True if either expression is true
648  *    <string>     => False
649  *    <ident>      => False (remember for macros the <macro> rule applies instead)
650  * Unary + and - are weird and wrong in fteqcc so we don't allow them
651  * parenthesis in expressions are allowed
652  * parameter lists on macros are errors
653  * No mathematical calculations are executed
654  */
655 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out, double *value_out);
656 static bool ftepp_if_op(ftepp_t *ftepp)
657 {
658     ftepp->lex->flags.noops = false;
659     ftepp_next(ftepp);
660     if (!ftepp_skipspace(ftepp))
661         return false;
662     ftepp->lex->flags.noops = true;
663     return true;
664 }
665 static bool ftepp_if_value(ftepp_t *ftepp, bool *out, double *value_out)
666 {
667     ppmacro *macro;
668     bool     wasnot = false;
669
670     if (!ftepp_skipspace(ftepp))
671         return false;
672
673     while (ftepp->token == '!') {
674         wasnot = true;
675         ftepp_next(ftepp);
676         if (!ftepp_skipspace(ftepp))
677             return false;
678     }
679
680     switch (ftepp->token) {
681         case TOKEN_IDENT:
682         case TOKEN_TYPENAME:
683         case TOKEN_KEYWORD:
684             if (!strcmp(ftepp_tokval(ftepp), "defined")) {
685                 ftepp_next(ftepp);
686                 if (!ftepp_skipspace(ftepp))
687                     return false;
688                 if (ftepp->token != '(') {
689                     ftepp_error(ftepp, "`defined` keyword in #if requires a macro name in parenthesis");
690                     return false;
691                 }
692                 ftepp_next(ftepp);
693                 if (!ftepp_skipspace(ftepp))
694                     return false;
695                 if (ftepp->token != TOKEN_IDENT &&
696                     ftepp->token != TOKEN_TYPENAME &&
697                     ftepp->token != TOKEN_KEYWORD)
698                 {
699                     ftepp_error(ftepp, "defined() used on an unexpected token type");
700                     return false;
701                 }
702                 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
703                 *out = !!macro;
704                 ftepp_next(ftepp);
705                 if (!ftepp_skipspace(ftepp))
706                     return false;
707                 if (ftepp->token != ')') {
708                     ftepp_error(ftepp, "expected closing paren");
709                     return false;
710                 }
711                 break;
712             }
713
714             macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
715             if (!macro || !vec_size(macro->output)) {
716                 *out = false;
717                 *value_out = 0;
718             } else {
719                 /* This does not expand recursively! */
720                 switch (macro->output[0]->token) {
721                     case TOKEN_INTCONST:
722                         *value_out = macro->output[0]->constval.i;
723                         *out = !!(macro->output[0]->constval.i);
724                         break;
725                     case TOKEN_FLOATCONST:
726                         *value_out = macro->output[0]->constval.f;
727                         *out = !!(macro->output[0]->constval.f);
728                         break;
729                     default:
730                         *out = false;
731                         break;
732                 }
733             }
734             break;
735         case TOKEN_STRINGCONST:
736             *out = false;
737             break;
738         case TOKEN_INTCONST:
739             *value_out = ftepp->lex->tok.constval.i;
740             *out = !!(ftepp->lex->tok.constval.i);
741             break;
742         case TOKEN_FLOATCONST:
743             *value_out = ftepp->lex->tok.constval.f;
744             *out = !!(ftepp->lex->tok.constval.f);
745             break;
746
747         case '(':
748             ftepp_next(ftepp);
749             if (!ftepp_if_expr(ftepp, out, value_out))
750                 return false;
751             if (ftepp->token != ')') {
752                 ftepp_error(ftepp, "expected closing paren in #if expression");
753                 return false;
754             }
755             break;
756
757         default:
758             ftepp_error(ftepp, "junk in #if: `%s` ...", ftepp_tokval(ftepp));
759             return false;
760     }
761     if (wasnot) {
762         *out = !*out;
763         *value_out = (*out ? 1 : 0);
764     }
765     return true;
766 }
767
768 /*
769 static bool ftepp_if_nextvalue(ftepp_t *ftepp, bool *out, double *value_out)
770 {
771     if (!ftepp_next(ftepp))
772         return false;
773     return ftepp_if_value(ftepp, out, value_out);
774 }
775 */
776
777 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out, double *value_out)
778 {
779     if (!ftepp_if_value(ftepp, out, value_out))
780         return false;
781
782     if (!ftepp_if_op(ftepp))
783         return false;
784
785     if (ftepp->token == ')' || ftepp->token != TOKEN_OPERATOR)
786         return true;
787
788     /* FTEQCC is all right-associative and no precedence here */
789     if (!strcmp(ftepp_tokval(ftepp), "&&") ||
790         !strcmp(ftepp_tokval(ftepp), "||"))
791     {
792         bool next = false;
793         char opc  = ftepp_tokval(ftepp)[0];
794         double nextvalue;
795
796         (void)nextvalue;
797         if (!ftepp_next(ftepp))
798             return false;
799         if (!ftepp_if_expr(ftepp, &next, &nextvalue))
800             return false;
801
802         if (opc == '&')
803             *out = *out && next;
804         else
805             *out = *out || next;
806
807         *value_out = (*out ? 1 : 0);
808         return true;
809     }
810     else if (!strcmp(ftepp_tokval(ftepp), "==") ||
811              !strcmp(ftepp_tokval(ftepp), "!=") ||
812              !strcmp(ftepp_tokval(ftepp), ">=") ||
813              !strcmp(ftepp_tokval(ftepp), "<=") ||
814              !strcmp(ftepp_tokval(ftepp), ">") ||
815              !strcmp(ftepp_tokval(ftepp), "<"))
816     {
817         bool next = false;
818         const char opc0 = ftepp_tokval(ftepp)[0];
819         const char opc1 = ftepp_tokval(ftepp)[1];
820         double other;
821
822         if (!ftepp_next(ftepp))
823             return false;
824         if (!ftepp_if_expr(ftepp, &next, &other))
825             return false;
826
827         if (opc0 == '=')
828             *out = (*value_out == other);
829         else if (opc0 == '!')
830             *out = (*value_out != other);
831         else if (opc0 == '>') {
832             if (opc1 == '=') *out = (*value_out >= other);
833             else             *out = (*value_out > other);
834         }
835         else if (opc0 == '<') {
836             if (opc1 == '=') *out = (*value_out <= other);
837             else             *out = (*value_out < other);
838         }
839         *value_out = (*out ? 1 : 0);
840
841         return true;
842     }
843     else {
844         ftepp_error(ftepp, "junk after #if");
845         return false;
846     }
847 }
848
849 static bool ftepp_if(ftepp_t *ftepp, ppcondition *cond)
850 {
851     bool result = false;
852     double dummy = 0;
853
854     memset(cond, 0, sizeof(*cond));
855     (void)ftepp_next(ftepp);
856
857     if (!ftepp_skipspace(ftepp))
858         return false;
859     if (ftepp->token == TOKEN_EOL) {
860         ftepp_error(ftepp, "expected expression for #if-directive");
861         return false;
862     }
863
864     if (!ftepp_if_expr(ftepp, &result, &dummy))
865         return false;
866
867     cond->on = result;
868     return true;
869 }
870
871 /**
872  * ifdef is rather simple
873  */
874 static bool ftepp_ifdef(ftepp_t *ftepp, ppcondition *cond)
875 {
876     ppmacro *macro;
877     memset(cond, 0, sizeof(*cond));
878     (void)ftepp_next(ftepp);
879     if (!ftepp_skipspace(ftepp))
880         return false;
881
882     switch (ftepp->token) {
883         case TOKEN_IDENT:
884         case TOKEN_TYPENAME:
885         case TOKEN_KEYWORD:
886             macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
887             break;
888         default:
889             ftepp_error(ftepp, "expected macro name");
890             return false;
891     }
892
893     (void)ftepp_next(ftepp);
894     if (!ftepp_skipspace(ftepp))
895         return false;
896     /* relaxing this condition
897     if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
898         ftepp_error(ftepp, "stray tokens after #ifdef");
899         return false;
900     }
901     */
902     cond->on = !!macro;
903     return true;
904 }
905
906 /**
907  * undef is also simple
908  */
909 static bool ftepp_undef(ftepp_t *ftepp)
910 {
911     (void)ftepp_next(ftepp);
912     if (!ftepp_skipspace(ftepp))
913         return false;
914
915     if (ftepp->output_on) {
916         switch (ftepp->token) {
917             case TOKEN_IDENT:
918             case TOKEN_TYPENAME:
919             case TOKEN_KEYWORD:
920                 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
921                 break;
922             default:
923                 ftepp_error(ftepp, "expected macro name");
924                 return false;
925         }
926     }
927
928     (void)ftepp_next(ftepp);
929     if (!ftepp_skipspace(ftepp))
930         return false;
931     /* relaxing this condition
932     if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
933         ftepp_error(ftepp, "stray tokens after #ifdef");
934         return false;
935     }
936     */
937     return true;
938 }
939
940 /* Special unescape-string function which skips a leading quote
941  * and stops at a quote, not just at \0
942  */
943 static void unescape(const char *str, char *out) {
944     ++str;
945     while (*str && *str != '"') {
946         if (*str == '\\') {
947             ++str;
948             switch (*str) {
949                 case '\\': *out++ = *str; break;
950                 case '"':  *out++ = *str; break;
951                 case 'a':  *out++ = '\a'; break;
952                 case 'b':  *out++ = '\b'; break;
953                 case 'r':  *out++ = '\r'; break;
954                 case 'n':  *out++ = '\n'; break;
955                 case 't':  *out++ = '\t'; break;
956                 case 'f':  *out++ = '\f'; break;
957                 case 'v':  *out++ = '\v'; break;
958                 default:
959                     *out++ = '\\';
960                     *out++ = *str;
961                     break;
962             }
963             ++str;
964             continue;
965         }
966
967         *out++ = *str++;
968     }
969     *out = 0;
970 }
971
972 static char *ftepp_include_find_path(const char *file, const char *pathfile)
973 {
974     FILE       *fp;
975     char       *filename = NULL;
976     const char *last_slash;
977     size_t      len;
978
979     if (!pathfile)
980         return NULL;
981
982     last_slash = strrchr(pathfile, '/');
983
984     if (last_slash) {
985         len = last_slash - pathfile;
986         memcpy(vec_add(filename, len), pathfile, len);
987         vec_push(filename, '/');
988     }
989
990     len = strlen(file);
991     memcpy(vec_add(filename, len+1), file, len);
992     vec_last(filename) = 0;
993
994     fp = util_fopen(filename, "rb");
995     if (fp) {
996         fclose(fp);
997         return filename;
998     }
999     vec_free(filename);
1000     return NULL;
1001 }
1002
1003 static char *ftepp_include_find(ftepp_t *ftepp, const char *file)
1004 {
1005     char *filename = NULL;
1006
1007     filename = ftepp_include_find_path(file, ftepp->includename);
1008     if (!filename)
1009         filename = ftepp_include_find_path(file, ftepp->itemname);
1010     return filename;
1011 }
1012
1013 static void ftepp_directive_warning(ftepp_t *ftepp) {
1014     char *message = NULL;
1015
1016     if (!ftepp_skipspace(ftepp))
1017         return;
1018
1019     /* handle the odd non string constant case so it works like C */
1020     if (ftepp->token != TOKEN_STRINGCONST) {
1021         vec_upload(message, "#warning", 8);
1022         ftepp_next(ftepp);
1023         while (ftepp->token != TOKEN_EOL) {
1024             vec_upload(message, ftepp_tokval(ftepp), strlen(ftepp_tokval(ftepp)));
1025             ftepp_next(ftepp);
1026         }
1027         vec_push(message, '\0');
1028         (void)!!ftepp_warn(ftepp, WARN_CPP, message);
1029         vec_free(message);
1030         return;
1031     }
1032
1033     unescape  (ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1034     (void)!!ftepp_warn(ftepp, WARN_CPP, "#warning %s", ftepp_tokval(ftepp));
1035 }
1036
1037 static void ftepp_directive_error(ftepp_t *ftepp) {
1038     char *message = NULL;
1039
1040     if (!ftepp_skipspace(ftepp))
1041         return;
1042
1043     /* handle the odd non string constant case so it works like C */
1044     if (ftepp->token != TOKEN_STRINGCONST) {
1045         vec_upload(message, "#error", 6);
1046         ftepp_next(ftepp);
1047         while (ftepp->token != TOKEN_EOL) {
1048             vec_upload(message, ftepp_tokval(ftepp), strlen(ftepp_tokval(ftepp)));
1049             ftepp_next(ftepp);
1050         }
1051         vec_push(message, '\0');
1052         ftepp_error(ftepp, message);
1053         vec_free(message);
1054         return;
1055     }
1056
1057     unescape  (ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1058     ftepp_error(ftepp, "#error %s", ftepp_tokval(ftepp));
1059 }
1060
1061 /**
1062  * Include a file.
1063  * FIXME: do we need/want a -I option?
1064  * FIXME: what about when dealing with files in subdirectories coming from a progs.src?
1065  */
1066 static bool ftepp_include(ftepp_t *ftepp)
1067 {
1068     lex_file *old_lexer = ftepp->lex;
1069     lex_file *inlex;
1070     lex_ctx  ctx;
1071     char     lineno[128];
1072     char     *filename;
1073     char     *old_includename;
1074
1075     (void)ftepp_next(ftepp);
1076     if (!ftepp_skipspace(ftepp))
1077         return false;
1078
1079     if (ftepp->token != TOKEN_STRINGCONST) {
1080         ftepp_error(ftepp, "expected filename to include");
1081         return false;
1082     }
1083
1084     ctx = ftepp_ctx(ftepp);
1085
1086     unescape(ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1087
1088     ftepp_out(ftepp, "\n#pragma file(", false);
1089     ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1090     ftepp_out(ftepp, ")\n#pragma line(1)\n", false);
1091
1092     filename = ftepp_include_find(ftepp, ftepp_tokval(ftepp));
1093     if (!filename) {
1094         ftepp_error(ftepp, "failed to open include file `%s`", ftepp_tokval(ftepp));
1095         return false;
1096     }
1097     inlex = lex_open(filename);
1098     if (!inlex) {
1099         ftepp_error(ftepp, "open failed on include file `%s`", filename);
1100         vec_free(filename);
1101         return false;
1102     }
1103     ftepp->lex = inlex;
1104     old_includename = ftepp->includename;
1105     ftepp->includename = filename;
1106     if (!ftepp_preprocess(ftepp)) {
1107         vec_free(ftepp->includename);
1108         ftepp->includename = old_includename;
1109         lex_close(ftepp->lex);
1110         ftepp->lex = old_lexer;
1111         return false;
1112     }
1113     vec_free(ftepp->includename);
1114     ftepp->includename = old_includename;
1115     lex_close(ftepp->lex);
1116     ftepp->lex = old_lexer;
1117
1118     ftepp_out(ftepp, "\n#pragma file(", false);
1119     ftepp_out(ftepp, ctx.file, false);
1120     snprintf(lineno, sizeof(lineno), ")\n#pragma line(%lu)\n", (unsigned long)(ctx.line+1));
1121     ftepp_out(ftepp, lineno, false);
1122
1123     /* skip the line */
1124     (void)ftepp_next(ftepp);
1125     if (!ftepp_skipspace(ftepp))
1126         return false;
1127     if (ftepp->token != TOKEN_EOL) {
1128         ftepp_error(ftepp, "stray tokens after #include");
1129         return false;
1130     }
1131     (void)ftepp_next(ftepp);
1132
1133     return true;
1134 }
1135
1136 /* Basic structure handlers */
1137 static bool ftepp_else_allowed(ftepp_t *ftepp)
1138 {
1139     if (!vec_size(ftepp->conditions)) {
1140         ftepp_error(ftepp, "#else without #if");
1141         return false;
1142     }
1143     if (vec_last(ftepp->conditions).had_else) {
1144         ftepp_error(ftepp, "multiple #else for a single #if");
1145         return false;
1146     }
1147     return true;
1148 }
1149
1150 static bool ftepp_hash(ftepp_t *ftepp)
1151 {
1152     ppcondition cond;
1153     ppcondition *pc;
1154
1155     lex_ctx ctx = ftepp_ctx(ftepp);
1156
1157     if (!ftepp_skipspace(ftepp))
1158         return false;
1159
1160     switch (ftepp->token) {
1161         case TOKEN_KEYWORD:
1162         case TOKEN_IDENT:
1163         case TOKEN_TYPENAME:
1164             if (!strcmp(ftepp_tokval(ftepp), "define")) {
1165                 return ftepp_define(ftepp);
1166             }
1167             else if (!strcmp(ftepp_tokval(ftepp), "undef")) {
1168                 return ftepp_undef(ftepp);
1169             }
1170             else if (!strcmp(ftepp_tokval(ftepp), "ifdef")) {
1171                 if (!ftepp_ifdef(ftepp, &cond))
1172                     return false;
1173                 cond.was_on = cond.on;
1174                 vec_push(ftepp->conditions, cond);
1175                 ftepp->output_on = ftepp->output_on && cond.on;
1176                 break;
1177             }
1178             else if (!strcmp(ftepp_tokval(ftepp), "ifndef")) {
1179                 if (!ftepp_ifdef(ftepp, &cond))
1180                     return false;
1181                 cond.on = !cond.on;
1182                 cond.was_on = cond.on;
1183                 vec_push(ftepp->conditions, cond);
1184                 ftepp->output_on = ftepp->output_on && cond.on;
1185                 break;
1186             }
1187             else if (!strcmp(ftepp_tokval(ftepp), "elifdef")) {
1188                 if (!ftepp_else_allowed(ftepp))
1189                     return false;
1190                 if (!ftepp_ifdef(ftepp, &cond))
1191                     return false;
1192                 pc = &vec_last(ftepp->conditions);
1193                 pc->on     = !pc->was_on && cond.on;
1194                 pc->was_on = pc->was_on || pc->on;
1195                 ftepp_update_output_condition(ftepp);
1196                 break;
1197             }
1198             else if (!strcmp(ftepp_tokval(ftepp), "elifndef")) {
1199                 if (!ftepp_else_allowed(ftepp))
1200                     return false;
1201                 if (!ftepp_ifdef(ftepp, &cond))
1202                     return false;
1203                 cond.on = !cond.on;
1204                 pc = &vec_last(ftepp->conditions);
1205                 pc->on     = !pc->was_on && cond.on;
1206                 pc->was_on = pc->was_on || pc->on;
1207                 ftepp_update_output_condition(ftepp);
1208                 break;
1209             }
1210             else if (!strcmp(ftepp_tokval(ftepp), "elif")) {
1211                 if (!ftepp_else_allowed(ftepp))
1212                     return false;
1213                 if (!ftepp_if(ftepp, &cond))
1214                     return false;
1215                 pc = &vec_last(ftepp->conditions);
1216                 pc->on     = !pc->was_on && cond.on;
1217                 pc->was_on = pc->was_on  || pc->on;
1218                 ftepp_update_output_condition(ftepp);
1219                 break;
1220             }
1221             else if (!strcmp(ftepp_tokval(ftepp), "if")) {
1222                 if (!ftepp_if(ftepp, &cond))
1223                     return false;
1224                 cond.was_on = cond.on;
1225                 vec_push(ftepp->conditions, cond);
1226                 ftepp->output_on = ftepp->output_on && cond.on;
1227                 break;
1228             }
1229             else if (!strcmp(ftepp_tokval(ftepp), "else")) {
1230                 if (!ftepp_else_allowed(ftepp))
1231                     return false;
1232                 pc = &vec_last(ftepp->conditions);
1233                 pc->on = !pc->was_on;
1234                 pc->had_else = true;
1235                 ftepp_next(ftepp);
1236                 ftepp_update_output_condition(ftepp);
1237                 break;
1238             }
1239             else if (!strcmp(ftepp_tokval(ftepp), "endif")) {
1240                 if (!vec_size(ftepp->conditions)) {
1241                     ftepp_error(ftepp, "#endif without #if");
1242                     return false;
1243                 }
1244                 vec_pop(ftepp->conditions);
1245                 ftepp_next(ftepp);
1246                 ftepp_update_output_condition(ftepp);
1247                 break;
1248             }
1249             else if (!strcmp(ftepp_tokval(ftepp), "include")) {
1250                 return ftepp_include(ftepp);
1251             }
1252             else if (!strcmp(ftepp_tokval(ftepp), "pragma")) {
1253                 ftepp_out(ftepp, "#", false);
1254                 break;
1255             }
1256             else if (!strcmp(ftepp_tokval(ftepp), "warning")) {
1257                 ftepp_directive_warning(ftepp);
1258                 break;
1259             }
1260             else if (!strcmp(ftepp_tokval(ftepp), "error")) {
1261                 ftepp_directive_error(ftepp);
1262                 break;
1263             }
1264             else {
1265                 if (ftepp->output_on) {
1266                     ftepp_error(ftepp, "unrecognized preprocessor directive: `%s`", ftepp_tokval(ftepp));
1267                     return false;
1268                 } else {
1269                     ftepp_next(ftepp);
1270                     break;
1271                 }
1272             }
1273             /* break; never reached */
1274         default:
1275             ftepp_error(ftepp, "unexpected preprocessor token: `%s`", ftepp_tokval(ftepp));
1276             return false;
1277         case TOKEN_EOL:
1278             ftepp_errorat(ftepp, ctx, "empty preprocessor directive");
1279             return false;
1280         case TOKEN_EOF:
1281             ftepp_error(ftepp, "missing newline at end of file", ftepp_tokval(ftepp));
1282             return false;
1283
1284         /* Builtins! Don't forget the builtins! */
1285         case TOKEN_INTCONST:
1286         case TOKEN_FLOATCONST:
1287             ftepp_out(ftepp, "#", false);
1288             return true;
1289     }
1290     if (!ftepp_skipspace(ftepp))
1291         return false;
1292     return true;
1293 }
1294
1295 static bool ftepp_preprocess(ftepp_t *ftepp)
1296 {
1297     ppmacro *macro;
1298     bool     newline = true;
1299
1300     ftepp->lex->flags.preprocessing = true;
1301     ftepp->lex->flags.mergelines    = false;
1302     ftepp->lex->flags.noops         = true;
1303
1304     ftepp_next(ftepp);
1305     do
1306     {
1307         if (ftepp->token >= TOKEN_EOF)
1308             break;
1309 #if 0
1310         newline = true;
1311 #endif
1312
1313         switch (ftepp->token) {
1314             case TOKEN_KEYWORD:
1315             case TOKEN_IDENT:
1316             case TOKEN_TYPENAME:
1317                 if (ftepp->output_on)
1318                     macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1319                 else
1320                     macro = NULL;
1321                 if (!macro) {
1322                     ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1323                     ftepp_next(ftepp);
1324                     break;
1325                 }
1326                 if (!ftepp_macro_call(ftepp, macro))
1327                     ftepp->token = TOKEN_ERROR;
1328                 break;
1329             case '#':
1330                 if (!newline) {
1331                     ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1332                     ftepp_next(ftepp);
1333                     break;
1334                 }
1335                 ftepp->lex->flags.mergelines = true;
1336                 if (ftepp_next(ftepp) >= TOKEN_EOF) {
1337                     ftepp_error(ftepp, "error in preprocessor directive");
1338                     ftepp->token = TOKEN_ERROR;
1339                     break;
1340                 }
1341                 if (!ftepp_hash(ftepp))
1342                     ftepp->token = TOKEN_ERROR;
1343                 ftepp->lex->flags.mergelines = false;
1344                 break;
1345             case TOKEN_EOL:
1346                 newline = true;
1347                 ftepp_out(ftepp, "\n", true);
1348                 ftepp_next(ftepp);
1349                 break;
1350             case TOKEN_WHITE:
1351                 /* same as default but don't set newline=false */
1352                 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1353                 ftepp_next(ftepp);
1354                 break;
1355             default:
1356                 newline = false;
1357                 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1358                 ftepp_next(ftepp);
1359                 break;
1360         }
1361     } while (!ftepp->errors && ftepp->token < TOKEN_EOF);
1362
1363     /* force a 0 at the end but don't count it as added to the output */
1364     vec_push(ftepp->output_string, 0);
1365     vec_shrinkby(ftepp->output_string, 1);
1366
1367     return (ftepp->token == TOKEN_EOF);
1368 }
1369
1370 /* Like in parser.c - files keep the previous state so we have one global
1371  * preprocessor. Except here we will want to warn about dangling #ifs.
1372  */
1373 static ftepp_t *ftepp;
1374
1375 static bool ftepp_preprocess_done()
1376 {
1377     bool retval = true;
1378     if (vec_size(ftepp->conditions)) {
1379         if (ftepp_warn(ftepp, WARN_MULTIFILE_IF, "#if spanning multiple files, is this intended?"))
1380             retval = false;
1381     }
1382     lex_close(ftepp->lex);
1383     ftepp->lex = NULL;
1384     if (ftepp->itemname) {
1385         mem_d(ftepp->itemname);
1386         ftepp->itemname = NULL;
1387     }
1388     return retval;
1389 }
1390
1391 bool ftepp_preprocess_file(const char *filename)
1392 {
1393     ftepp->lex = lex_open(filename);
1394     ftepp->itemname = util_strdup(filename);
1395     if (!ftepp->lex) {
1396         con_out("failed to open file \"%s\"\n", filename);
1397         return false;
1398     }
1399     if (!ftepp_preprocess(ftepp))
1400         return false;
1401     return ftepp_preprocess_done();
1402 }
1403
1404 bool ftepp_preprocess_string(const char *name, const char *str)
1405 {
1406     ftepp->lex = lex_open_string(str, strlen(str), name);
1407     ftepp->itemname = util_strdup(name);
1408     if (!ftepp->lex) {
1409         con_out("failed to create lexer for string \"%s\"\n", name);
1410         return false;
1411     }
1412     if (!ftepp_preprocess(ftepp))
1413         return false;
1414     return ftepp_preprocess_done();
1415 }
1416
1417
1418 void ftepp_add_macro(const char *name, const char *value) {
1419     char *create = NULL;
1420
1421     /* use saner path for empty macros */
1422     if (!value) {
1423         ftepp_add_define("__builtin__", name);
1424         return;
1425     }
1426
1427     vec_upload(create, "#define ", 8);
1428     vec_upload(create, name,  strlen(name));
1429     vec_push  (create, ' ');
1430     vec_upload(create, value, strlen(value));
1431     vec_push  (create, 0);
1432
1433     ftepp_preprocess_string("__builtin__", create);
1434     vec_free  (create);
1435 }
1436
1437 bool ftepp_init()
1438 {
1439     char minor[32];
1440     char major[32];
1441
1442     ftepp = ftepp_new();
1443     if (!ftepp)
1444         return false;
1445
1446     memset(minor, 0, sizeof(minor));
1447     memset(major, 0, sizeof(major));
1448
1449     /* set the right macro based on the selected standard */
1450     ftepp_add_define(NULL, "GMQCC");
1451     if (opts.standard == COMPILER_FTEQCC) {
1452         ftepp_add_define(NULL, "__STD_FTEQCC__");
1453         /* 1.00 */
1454         major[0] = '"';
1455         major[1] = '1';
1456         major[2] = '"';
1457
1458         minor[0] = '"';
1459         minor[1] = '0';
1460         minor[2] = '"';
1461     } else if (opts.standard == COMPILER_GMQCC) {
1462         ftepp_add_define(NULL, "__STD_GMQCC__");
1463         sprintf(major, "\"%d\"", GMQCC_VERSION_MAJOR);
1464         sprintf(minor, "\"%d\"", GMQCC_VERSION_MINOR);
1465     } else if (opts.standard == COMPILER_QCC) {
1466         ftepp_add_define(NULL, "__STD_QCC__");
1467         /* 1.0 */
1468         major[0] = '"';
1469         major[1] = '1';
1470         major[2] = '"';
1471
1472         minor[0] = '"';
1473         minor[1] = '0';
1474         minor[2] = '"';
1475     }
1476
1477     ftepp_add_macro("__STD_VERSION_MINOR__", minor);
1478     ftepp_add_macro("__STD_VERSION_MAJOR__", major);
1479
1480     return true;
1481 }
1482
1483 void ftepp_add_define(const char *source, const char *name)
1484 {
1485     ppmacro *macro;
1486     lex_ctx ctx = { "__builtin__", 0 };
1487     ctx.file = source;
1488     macro = ppmacro_new(ctx, name);
1489     vec_push(ftepp->macros, macro);
1490 }
1491
1492 const char *ftepp_get()
1493 {
1494     return ftepp->output_string;
1495 }
1496
1497 void ftepp_flush()
1498 {
1499     vec_free(ftepp->output_string);
1500 }
1501
1502 void ftepp_finish()
1503 {
1504     if (!ftepp)
1505         return;
1506     ftepp_delete(ftepp);
1507     ftepp = NULL;
1508 }