]> git.xonotic.org Git - xonotic/gmqcc.git/blob - ftepp.c
recursively call the preprocessor on expanded macros
[xonotic/gmqcc.git] / ftepp.c
1 /*
2  * Copyright (C) 2012
3  *     Wolfgang Bumiller
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9  * of the Software, and to permit persons to whom the Software is furnished to do
10  * so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23 #include "gmqcc.h"
24 #include "lexer.h"
25
26 typedef struct {
27     bool on;
28     bool was_on;
29     bool had_else;
30 } ppcondition;
31
32 typedef struct {
33     int   token;
34     char *value;
35     /* a copy from the lexer */
36     union {
37         vector v;
38         int    i;
39         double f;
40         int    t; /* type */
41     } constval;
42 } pptoken;
43
44 typedef struct {
45     lex_ctx ctx;
46
47     char   *name;
48     char  **params;
49     /* yes we need an extra flag since `#define FOO x` is not the same as `#define FOO() x` */
50     bool    has_params;
51
52     pptoken **output;
53 } ppmacro;
54
55 typedef struct {
56     lex_file    *lex;
57     int          token;
58     bool         newline;
59     unsigned int errors;
60
61     bool         output_on;
62     ppcondition *conditions;
63     ppmacro    **macros;
64
65     bool         output_string;
66     char        *output;
67 } ftepp_t;
68
69 #define ftepp_tokval(f) ((f)->lex->tok.value)
70 #define ftepp_ctx(f)    ((f)->lex->tok.ctx)
71
72 static void ftepp_errorat(ftepp_t *ftepp, lex_ctx ctx, const char *fmt, ...)
73 {
74     va_list ap;
75
76     ftepp->errors++;
77
78     va_start(ap, fmt);
79     con_vprintmsg(LVL_ERROR, ctx.file, ctx.line, "error", fmt, ap);
80     va_end(ap);
81 }
82
83 static void ftepp_error(ftepp_t *ftepp, const char *fmt, ...)
84 {
85     va_list ap;
86
87     ftepp->errors++;
88
89     va_start(ap, fmt);
90     con_vprintmsg(LVL_ERROR, ftepp->lex->tok.ctx.file, ftepp->lex->tok.ctx.line, "error", fmt, ap);
91     va_end(ap);
92 }
93
94 static pptoken *pptoken_make(ftepp_t *ftepp)
95 {
96     pptoken *token = (pptoken*)mem_a(sizeof(pptoken));
97     token->token = ftepp->token;
98     if (token->token == TOKEN_WHITE)
99         token->value = util_strdup(" ");
100     else
101         token->value = util_strdup(ftepp_tokval(ftepp));
102     memcpy(&token->constval, &ftepp->lex->tok.constval, sizeof(token->constval));
103     return token;
104 }
105
106 static void pptoken_delete(pptoken *self)
107 {
108     mem_d(self->value);
109     mem_d(self);
110 }
111
112 static ppmacro *ppmacro_new(lex_ctx ctx, const char *name)
113 {
114     ppmacro *macro = (ppmacro*)mem_a(sizeof(ppmacro));
115     memset(macro, 0, sizeof(*macro));
116     macro->name = util_strdup(name);
117     return macro;
118 }
119
120 static void ppmacro_delete(ppmacro *self)
121 {
122     size_t i;
123     for (i = 0; i < vec_size(self->params); ++i)
124         mem_d(self->params[i]);
125     vec_free(self->params);
126     for (i = 0; i < vec_size(self->output); ++i)
127         pptoken_delete(self->output[i]);
128     vec_free(self->output);
129     mem_d(self->name);
130     mem_d(self);
131 }
132
133 static ftepp_t* ftepp_init()
134 {
135     ftepp_t *ftepp;
136
137     ftepp = (ftepp_t*)mem_a(sizeof(*ftepp));
138     memset(ftepp, 0, sizeof(*ftepp));
139
140     ftepp->output_on = true;
141
142     return ftepp;
143 }
144
145 static void ftepp_delete(ftepp_t *self)
146 {
147     size_t i;
148     for (i = 0; i < vec_size(self->macros); ++i)
149         ppmacro_delete(self->macros[i]);
150     vec_free(self->macros);
151     vec_free(self->conditions);
152     lex_close(self->lex);
153     mem_d(self);
154 }
155
156 static void ftepp_out(ftepp_t *ftepp, const char *str, bool ignore_cond)
157 {
158     if (ignore_cond || ftepp->output_on)
159     {
160         size_t len;
161         char  *data;
162         if (!ftepp->output_string) {
163             printf("%s", str);
164             return;
165         }
166         len = strlen(str);
167         data = vec_add(ftepp->output, len);
168         memcpy(data, str, len);
169     }
170 }
171
172 static void ftepp_update_output_condition(ftepp_t *ftepp)
173 {
174     size_t i;
175     ftepp->output_on = true;
176     for (i = 0; i < vec_size(ftepp->conditions); ++i)
177         ftepp->output_on = ftepp->output_on && ftepp->conditions[i].on;
178 }
179
180 static ppmacro* ftepp_macro_find(ftepp_t *ftepp, const char *name)
181 {
182     size_t i;
183     for (i = 0; i < vec_size(ftepp->macros); ++i) {
184         if (!strcmp(name, ftepp->macros[i]->name))
185             return ftepp->macros[i];
186     }
187     return NULL;
188 }
189
190 static inline int ftepp_next(ftepp_t *ftepp)
191 {
192     return (ftepp->token = lex_do(ftepp->lex));
193 }
194
195 /* Important: this does not skip newlines! */
196 static bool ftepp_skipspace(ftepp_t *ftepp)
197 {
198     if (ftepp->token != TOKEN_WHITE)
199         return true;
200     while (ftepp_next(ftepp) == TOKEN_WHITE) {}
201     if (ftepp->token >= TOKEN_EOF) {
202         ftepp_error(ftepp, "unexpected end of preprocessor directive");
203         return false;
204     }
205     return true;
206 }
207
208 /* this one skips EOLs as well */
209 static bool ftepp_skipallwhite(ftepp_t *ftepp)
210 {
211     if (ftepp->token != TOKEN_WHITE && ftepp->token != TOKEN_EOL)
212         return true;
213     do {
214         ftepp_next(ftepp);
215     } while (ftepp->token == TOKEN_WHITE || ftepp->token == TOKEN_EOL);
216     if (ftepp->token >= TOKEN_EOF) {
217         ftepp_error(ftepp, "unexpected end of preprocessor directive");
218         return false;
219     }
220     return true;
221 }
222
223 /**
224  * The huge macro parsing code...
225  */
226 static bool ftepp_define_params(ftepp_t *ftepp, ppmacro *macro)
227 {
228     do {
229         ftepp_next(ftepp);
230         if (!ftepp_skipspace(ftepp))
231             return false;
232         if (ftepp->token == ')')
233             break;
234         switch (ftepp->token) {
235             case TOKEN_IDENT:
236             case TOKEN_TYPENAME:
237             case TOKEN_KEYWORD:
238                 break;
239             default:
240                 ftepp_error(ftepp, "unexpected token in parameter list");
241                 return false;
242         }
243         vec_push(macro->params, util_strdup(ftepp_tokval(ftepp)));
244         ftepp_next(ftepp);
245         if (!ftepp_skipspace(ftepp))
246             return false;
247     } while (ftepp->token == ',');
248     if (ftepp->token != ')') {
249         ftepp_error(ftepp, "expected closing paren after macro parameter list");
250         return false;
251     }
252     ftepp_next(ftepp);
253     /* skipspace happens in ftepp_define */
254     return true;
255 }
256
257 static bool ftepp_define_body(ftepp_t *ftepp, ppmacro *macro)
258 {
259     pptoken *ptok;
260     while (ftepp->token != TOKEN_EOL && ftepp->token < TOKEN_EOF) {
261         ptok = pptoken_make(ftepp);
262         vec_push(macro->output, ptok);
263         ftepp_next(ftepp);
264     }
265     if (ftepp->token != TOKEN_EOL) {
266         ftepp_error(ftepp, "unexpected junk after macro or unexpected end of file");
267         return false;
268     }
269     return true;
270 }
271
272 static bool ftepp_define(ftepp_t *ftepp)
273 {
274     ppmacro *macro;
275     (void)ftepp_next(ftepp);
276     if (!ftepp_skipspace(ftepp))
277         return false;
278
279     switch (ftepp->token) {
280         case TOKEN_IDENT:
281         case TOKEN_TYPENAME:
282         case TOKEN_KEYWORD:
283             macro = ppmacro_new(ftepp_ctx(ftepp), ftepp_tokval(ftepp));
284             break;
285         default:
286             ftepp_error(ftepp, "expected macro name");
287             return false;
288     }
289
290     (void)ftepp_next(ftepp);
291
292     if (ftepp->token == '(') {
293         macro->has_params = true;
294         if (!ftepp_define_params(ftepp, macro))
295             return false;
296     }
297
298     if (!ftepp_skipspace(ftepp))
299         return false;
300
301     if (!ftepp_define_body(ftepp, macro))
302         return false;
303
304     vec_push(ftepp->macros, macro);
305     return true;
306 }
307
308 /**
309  * When a macro is used we have to handle parameters as well
310  * as special-concatenation via ## or stringification via #
311  *
312  * Note: parenthesis can nest, so FOO((a),b) is valid, but only
313  * this kind of parens. Curly braces or [] don't count towards the
314  * paren-level.
315  */
316 typedef struct {
317     pptoken **tokens;
318 } macroparam;
319
320 static void macroparam_clean(macroparam *self)
321 {
322     size_t i;
323     for (i = 0; i < vec_size(self->tokens); ++i)
324         pptoken_delete(self->tokens[i]);
325     vec_free(self->tokens);
326 }
327
328 /* need to leave the last token up */
329 static bool ftepp_macro_call_params(ftepp_t *ftepp, macroparam **out_params)
330 {
331     macroparam *params = NULL;
332     pptoken    *ptok;
333     macroparam  mp;
334     size_t      parens = 0;
335     size_t      i;
336
337     if (!ftepp_skipallwhite(ftepp))
338         return false;
339     while (ftepp->token != ')') {
340         mp.tokens = NULL;
341         if (!ftepp_skipallwhite(ftepp))
342             return false;
343         while (parens || ftepp->token != ',') {
344             if (ftepp->token == '(')
345                 ++parens;
346             else if (ftepp->token == ')') {
347                 if (!parens)
348                     break;
349                 --parens;
350             }
351             ptok = pptoken_make(ftepp);
352             vec_push(mp.tokens, ptok);
353             if (ftepp_next(ftepp) >= TOKEN_EOF) {
354                 ftepp_error(ftepp, "unexpected EOF in macro call");
355                 goto on_error;
356             }
357         }
358         vec_push(params, mp);
359         mp.tokens = NULL;
360         if (ftepp->token == ')')
361             break;
362         if (ftepp->token != ',') {
363             ftepp_error(ftepp, "expected closing paren or comma in macro call");
364             goto on_error;
365         }
366         if (ftepp_next(ftepp) >= TOKEN_EOF) {
367             ftepp_error(ftepp, "unexpected EOF in macro call");
368             goto on_error;
369         }
370     }
371     /* need to leave that up
372     if (ftepp_next(ftepp) >= TOKEN_EOF) {
373         ftepp_error(ftepp, "unexpected EOF in macro call");
374         goto on_error;
375     }
376     */
377     *out_params = params;
378     return true;
379
380 on_error:
381     if (mp.tokens)
382         macroparam_clean(&mp);
383     for (i = 0; i < vec_size(params); ++i)
384         macroparam_clean(&params[i]);
385     vec_free(params);
386     return false;
387 }
388
389 static bool macro_params_find(ppmacro *macro, const char *name, size_t *idx)
390 {
391     size_t i;
392     for (i = 0; i < vec_size(macro->params); ++i) {
393         if (!strcmp(macro->params[i], name)) {
394             *idx = i;
395             return true;
396         }
397     }
398     return false;
399 }
400
401 static bool ftepp_preprocess(ftepp_t *ftepp);
402 static bool ftepp_macro_expand(ftepp_t *ftepp, ppmacro *macro, macroparam *params)
403 {
404     char     *old_string = ftepp->output;
405     bool      old_string_flag = ftepp->output_string;
406     lex_file *old_lexer = ftepp->lex;
407     bool retval = true;
408
409     size_t    o, pi, pv;
410     lex_file *inlex;
411
412     /* really ... */
413     if (!vec_size(macro->output))
414         return true;
415
416     ftepp->output = NULL;
417     ftepp->output_string = true;
418     for (o = 0; o < vec_size(macro->output); ++o) {
419         pptoken *out = macro->output[o];
420         switch (out->token) {
421             case TOKEN_IDENT:
422             case TOKEN_TYPENAME:
423             case TOKEN_KEYWORD:
424                 if (!macro_params_find(macro, out->value, &pi)) {
425                     ftepp_out(ftepp, out->value, false);
426                     break;
427                 } else {
428                     for (pv = 0; pv < vec_size(params[pi].tokens); ++pv) {
429                         out = params[pi].tokens[pv];
430                         if (out->token == TOKEN_EOL)
431                             ftepp_out(ftepp, "\n", false);
432                         else
433                             ftepp_out(ftepp, out->value, false);
434                     }
435                 }
436                 break;
437             case TOKEN_EOL:
438                 ftepp_out(ftepp, "\n", false);
439                 break;
440             default:
441                 ftepp_out(ftepp, out->value, false);
442                 break;
443         }
444     }
445     vec_push(ftepp->output, 0);
446     /* Now run the preprocessor recursively on this string buffer */
447     inlex = lex_open_string(ftepp->output, vec_size(ftepp->output)-1, ftepp->lex->name);
448     if (!inlex) {
449         ftepp_error(ftepp, "internal error: failed to instantiate lexer");
450         retval = false;
451         goto cleanup;
452     }
453     ftepp->output        = old_string;
454     ftepp->output_string = old_string_flag;
455     ftepp->lex = inlex;
456     if (!ftepp_preprocess(ftepp)) {
457         retval = false;
458         goto cleanup;
459     }
460
461 cleanup:
462     ftepp->lex           = old_lexer;
463     ftepp->output        = old_string;
464     ftepp->output_string = old_string_flag;
465     return retval;
466 }
467
468 static bool ftepp_macro_call(ftepp_t *ftepp, ppmacro *macro)
469 {
470     size_t     o;
471     macroparam *params = NULL;
472     bool        retval = true;
473
474     ftepp_next(ftepp);
475
476     if (!macro->has_params) {
477         for (o = 0; o < vec_size(macro->output); ++o) {
478             ftepp_out(ftepp, macro->output[o]->value, false);
479         }
480         return true;
481     }
482
483     if (!ftepp_skipallwhite(ftepp))
484         return false;
485
486     if (ftepp->token != '(') {
487         ftepp_error(ftepp, "expected macro parameters in parenthesis");
488         return false;
489     }
490
491     ftepp_next(ftepp);
492     if (!ftepp_macro_call_params(ftepp, &params))
493         return false;
494
495     if (vec_size(params) != vec_size(macro->params)) {
496         ftepp_error(ftepp, "macro %s expects %u paramteters, %u provided", macro->name,
497                     (unsigned int)vec_size(macro->params),
498                     (unsigned int)vec_size(params));
499         retval = false;
500         goto cleanup;
501     }
502
503     if (!ftepp_macro_expand(ftepp, macro, params))
504         retval = false;
505     ftepp_next(ftepp);
506
507 cleanup:
508     for (o = 0; o < vec_size(params); ++o)
509         macroparam_clean(&params[o]);
510     vec_free(params);
511     return retval;
512 }
513
514 /**
515  * #if - the FTEQCC way:
516  *    defined(FOO) => true if FOO was #defined regardless of parameters or contents
517  *    <numbers>    => True if the number is not 0
518  *    !<factor>    => True if the factor yields false
519  *    !!<factor>   => ERROR on 2 or more unary nots
520  *    <macro>      => becomes the macro's FIRST token regardless of parameters
521  *    <e> && <e>   => True if both expressions are true
522  *    <e> || <e>   => True if either expression is true
523  *    <string>     => False
524  *    <ident>      => False (remember for macros the <macro> rule applies instead)
525  * Unary + and - are weird and wrong in fteqcc so we don't allow them
526  * parenthesis in expressions are allowed
527  * parameter lists on macros are errors
528  * No mathematical calculations are executed
529  */
530 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out)
531 {
532     ppmacro *macro;
533     bool     wasnot = false;
534
535     if (!ftepp_skipspace(ftepp))
536         return false;
537
538     while (ftepp->token == '!') {
539         wasnot = true;
540         ftepp_next(ftepp);
541         if (!ftepp_skipspace(ftepp))
542             return false;
543     }
544
545     switch (ftepp->token) {
546         case TOKEN_IDENT:
547         case TOKEN_TYPENAME:
548         case TOKEN_KEYWORD:
549             if (!strcmp(ftepp_tokval(ftepp), "defined")) {
550                 ftepp_next(ftepp);
551                 if (!ftepp_skipspace(ftepp))
552                     return false;
553                 if (ftepp->token != '(') {
554                     ftepp_error(ftepp, "`defined` keyword in #if requires a macro name in parenthesis");
555                     return false;
556                 }
557                 ftepp_next(ftepp);
558                 if (!ftepp_skipspace(ftepp))
559                     return false;
560                 if (ftepp->token != TOKEN_IDENT &&
561                     ftepp->token != TOKEN_TYPENAME &&
562                     ftepp->token != TOKEN_KEYWORD)
563                 {
564                     ftepp_error(ftepp, "defined() used on an unexpected token type");
565                     return false;
566                 }
567                 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
568                 *out = !!macro;
569                 ftepp_next(ftepp);
570                 if (!ftepp_skipspace(ftepp))
571                     return false;
572                 if (ftepp->token != ')') {
573                     ftepp_error(ftepp, "expected closing paren");
574                     return false;
575                 }
576                 break;
577             }
578
579             macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
580             if (!macro || !vec_size(macro->output)) {
581                 *out = false;
582             } else {
583                 /* This does not expand recursively! */
584                 switch (macro->output[0]->token) {
585                     case TOKEN_INTCONST:
586                         *out = !!(macro->output[0]->constval.f);
587                         break;
588                     case TOKEN_FLOATCONST:
589                         *out = !!(macro->output[0]->constval.f);
590                         break;
591                     default:
592                         *out = false;
593                         break;
594                 }
595             }
596             break;
597         case TOKEN_STRINGCONST:
598             *out = false;
599             break;
600         case TOKEN_INTCONST:
601             *out = !!(ftepp->lex->tok.constval.i);
602             break;
603         case TOKEN_FLOATCONST:
604             *out = !!(ftepp->lex->tok.constval.f);
605             break;
606
607         case '(':
608             ftepp_next(ftepp);
609             if (!ftepp_if_expr(ftepp, out))
610                 return false;
611             if (ftepp->token != ')') {
612                 ftepp_error(ftepp, "expected closing paren in #if expression");
613                 return false;
614             }
615             break;
616
617         default:
618             ftepp_error(ftepp, "junk in #if");
619             return false;
620     }
621     if (wasnot)
622         *out = !*out;
623
624     ftepp->lex->flags.noops = false;
625     ftepp_next(ftepp);
626     if (!ftepp_skipspace(ftepp))
627         return false;
628     ftepp->lex->flags.noops = true;
629
630     if (ftepp->token == ')')
631         return true;
632
633     if (ftepp->token != TOKEN_OPERATOR)
634         return true;
635
636     if (!strcmp(ftepp_tokval(ftepp), "&&") ||
637         !strcmp(ftepp_tokval(ftepp), "||"))
638     {
639         bool next = false;
640         char opc  = ftepp_tokval(ftepp)[0];
641
642         ftepp_next(ftepp);
643         if (!ftepp_if_expr(ftepp, &next))
644             return false;
645
646         if (opc == '&')
647             *out = *out && next;
648         else
649             *out = *out || next;
650         return true;
651     }
652     else {
653         ftepp_error(ftepp, "junk after #if");
654         return false;
655     }
656 }
657
658 static bool ftepp_if(ftepp_t *ftepp, ppcondition *cond)
659 {
660     bool result = false;
661
662     memset(cond, 0, sizeof(*cond));
663     (void)ftepp_next(ftepp);
664
665     if (!ftepp_skipspace(ftepp))
666         return false;
667     if (ftepp->token == TOKEN_EOL) {
668         ftepp_error(ftepp, "expected expression for #if-directive");
669         return false;
670     }
671
672     if (!ftepp_if_expr(ftepp, &result))
673         return false;
674
675     cond->on = result;
676     return true;
677 }
678
679 /**
680  * ifdef is rather simple
681  */
682 static bool ftepp_ifdef(ftepp_t *ftepp, ppcondition *cond)
683 {
684     ppmacro *macro;
685     memset(cond, 0, sizeof(*cond));
686     (void)ftepp_next(ftepp);
687     if (!ftepp_skipspace(ftepp))
688         return false;
689
690     switch (ftepp->token) {
691         case TOKEN_IDENT:
692         case TOKEN_TYPENAME:
693         case TOKEN_KEYWORD:
694             macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
695             break;
696         default:
697             ftepp_error(ftepp, "expected macro name");
698             return false;
699     }
700
701     (void)ftepp_next(ftepp);
702     if (!ftepp_skipspace(ftepp))
703         return false;
704     if (ftepp->token != TOKEN_EOL) {
705         ftepp_error(ftepp, "stray tokens after #ifdef");
706         return false;
707     }
708     cond->on = !!macro;
709     return true;
710 }
711
712 /* Basic structure handlers */
713 static bool ftepp_else_allowed(ftepp_t *ftepp)
714 {
715     if (!vec_size(ftepp->conditions)) {
716         ftepp_error(ftepp, "#else without #if");
717         return false;
718     }
719     if (vec_last(ftepp->conditions).had_else) {
720         ftepp_error(ftepp, "multiple #else for a single #if");
721         return false;
722     }
723     return true;
724 }
725
726 static bool ftepp_hash(ftepp_t *ftepp)
727 {
728     ppcondition cond;
729     ppcondition *pc;
730
731     lex_ctx ctx = ftepp_ctx(ftepp);
732
733     if (!ftepp_skipspace(ftepp))
734         return false;
735
736     switch (ftepp->token) {
737         case TOKEN_KEYWORD:
738         case TOKEN_IDENT:
739         case TOKEN_TYPENAME:
740             if (!strcmp(ftepp_tokval(ftepp), "define")) {
741                 return ftepp_define(ftepp);
742             }
743             else if (!strcmp(ftepp_tokval(ftepp), "ifdef")) {
744                 if (!ftepp_ifdef(ftepp, &cond))
745                     return false;
746                 cond.was_on = cond.on;
747                 vec_push(ftepp->conditions, cond);
748                 ftepp->output_on = ftepp->output_on && cond.on;
749                 break;
750             }
751             else if (!strcmp(ftepp_tokval(ftepp), "ifndef")) {
752                 if (!ftepp_ifdef(ftepp, &cond))
753                     return false;
754                 cond.on = !cond.on;
755                 cond.was_on = cond.on;
756                 vec_push(ftepp->conditions, cond);
757                 ftepp->output_on = ftepp->output_on && cond.on;
758                 break;
759             }
760             else if (!strcmp(ftepp_tokval(ftepp), "elifdef")) {
761                 if (!ftepp_else_allowed(ftepp))
762                     return false;
763                 if (!ftepp_ifdef(ftepp, &cond))
764                     return false;
765                 pc = &vec_last(ftepp->conditions);
766                 pc->on     = !pc->was_on && cond.on;
767                 pc->was_on = pc->was_on || pc->on;
768                 ftepp_update_output_condition(ftepp);
769                 break;
770             }
771             else if (!strcmp(ftepp_tokval(ftepp), "elifndef")) {
772                 if (!ftepp_else_allowed(ftepp))
773                     return false;
774                 if (!ftepp_ifdef(ftepp, &cond))
775                     return false;
776                 cond.on = !cond.on;
777                 pc = &vec_last(ftepp->conditions);
778                 pc->on     = !pc->was_on && cond.on;
779                 pc->was_on = pc->was_on || pc->on;
780                 ftepp_update_output_condition(ftepp);
781                 break;
782             }
783             else if (!strcmp(ftepp_tokval(ftepp), "elif")) {
784                 if (!ftepp_else_allowed(ftepp))
785                     return false;
786                 if (!ftepp_if(ftepp, &cond))
787                     return false;
788                 pc = &vec_last(ftepp->conditions);
789                 pc->on     = !pc->was_on && cond.on;
790                 pc->was_on = pc->was_on  || pc->on;
791                 ftepp_update_output_condition(ftepp);
792                 break;
793             }
794             else if (!strcmp(ftepp_tokval(ftepp), "if")) {
795                 if (!ftepp_if(ftepp, &cond))
796                     return false;
797                 cond.was_on = cond.on;
798                 vec_push(ftepp->conditions, cond);
799                 ftepp->output_on = ftepp->output_on && cond.on;
800                 break;
801             }
802             else if (!strcmp(ftepp_tokval(ftepp), "else")) {
803                 if (!ftepp_else_allowed(ftepp))
804                     return false;
805                 pc = &vec_last(ftepp->conditions);
806                 pc->on = !pc->was_on;
807                 pc->had_else = true;
808                 ftepp_next(ftepp);
809                 ftepp_update_output_condition(ftepp);
810                 break;
811             }
812             else if (!strcmp(ftepp_tokval(ftepp), "endif")) {
813                 if (!vec_size(ftepp->conditions)) {
814                     ftepp_error(ftepp, "#endif without #if");
815                     return false;
816                 }
817                 vec_pop(ftepp->conditions);
818                 ftepp_next(ftepp);
819                 ftepp_update_output_condition(ftepp);
820                 break;
821             }
822             else {
823                 ftepp_error(ftepp, "unrecognized preprocessor directive: `%s`", ftepp_tokval(ftepp));
824                 return false;
825             }
826             break;
827         default:
828             ftepp_error(ftepp, "unexpected preprocessor token: `%s`", ftepp_tokval(ftepp));
829             return false;
830         case TOKEN_EOL:
831             ftepp_errorat(ftepp, ctx, "empty preprocessor directive");
832             return false;
833         case TOKEN_EOF:
834             ftepp_error(ftepp, "missing newline at end of file", ftepp_tokval(ftepp));
835             return false;
836
837         /* Builtins! Don't forget the builtins! */
838         case TOKEN_INTCONST:
839         case TOKEN_FLOATCONST:
840             ftepp_out(ftepp, "#", false);
841             return true;
842     }
843     if (!ftepp_skipspace(ftepp))
844         return false;
845     return true;
846 }
847
848 static bool ftepp_preprocess(ftepp_t *ftepp)
849 {
850     ppmacro *macro;
851     bool     newline = true;
852
853     ftepp->lex->flags.preprocessing = true;
854     ftepp->lex->flags.mergelines    = false;
855     ftepp->lex->flags.noops         = true;
856
857     ftepp_next(ftepp);
858     do
859     {
860         if (ftepp->token >= TOKEN_EOF)
861             break;
862 #if 0
863         ftepp->newline = newline;
864         newline = false;
865 #else
866         /* For the sake of FTE compatibility... FU, really */
867         ftepp->newline = newline = true;
868 #endif
869
870         switch (ftepp->token) {
871             case TOKEN_KEYWORD:
872             case TOKEN_IDENT:
873             case TOKEN_TYPENAME:
874                 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
875                 if (!macro) {
876                     ftepp_out(ftepp, ftepp_tokval(ftepp), false);
877                     ftepp_next(ftepp);
878                     break;
879                 }
880                 if (!ftepp_macro_call(ftepp, macro))
881                     ftepp->token = TOKEN_ERROR;
882                 break;
883             case '#':
884                 if (!ftepp->newline) {
885                     ftepp_out(ftepp, ftepp_tokval(ftepp), false);
886                     ftepp_next(ftepp);
887                     break;
888                 }
889                 ftepp->lex->flags.mergelines = true;
890                 if (ftepp_next(ftepp) >= TOKEN_EOF) {
891                     ftepp_error(ftepp, "error in preprocessor directive");
892                     ftepp->token = TOKEN_ERROR;
893                     break;
894                 }
895                 if (!ftepp_hash(ftepp))
896                     ftepp->token = TOKEN_ERROR;
897                 ftepp->lex->flags.mergelines = false;
898                 break;
899             case TOKEN_EOL:
900                 newline = true;
901                 ftepp_out(ftepp, "\n", true);
902                 ftepp_next(ftepp);
903                 break;
904             default:
905                 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
906                 ftepp_next(ftepp);
907                 break;
908         }
909     } while (!ftepp->errors && ftepp->token < TOKEN_EOF);
910
911     newline = ftepp->token == TOKEN_EOF;
912     return newline;
913 }
914
915 bool ftepp_preprocess_file(const char *filename)
916 {
917     ftepp_t *ftepp = ftepp_init();
918     ftepp->lex = lex_open(filename);
919     if (!ftepp->lex) {
920         con_out("failed to open file \"%s\"\n", filename);
921         return false;
922     }
923     if (!ftepp_preprocess(ftepp)) {
924         ftepp_delete(ftepp);
925         return false;
926     }
927     ftepp_delete(ftepp);
928     return true;
929 }
930
931 bool ftepp_preprocess_string(const char *name, const char *str)
932 {
933     ftepp_t *ftepp = ftepp_init();
934     ftepp->lex = lex_open_string(str, strlen(str), name);
935     if (!ftepp->lex) {
936         con_out("failed to create lexer for string \"%s\"\n", name);
937         return false;
938     }
939     if (!ftepp_preprocess(ftepp)) {
940         ftepp_delete(ftepp);
941         return false;
942     }
943     ftepp_delete(ftepp);
944     return true;
945 }