]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/unifdef/unifdef.c
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r305575, and update
[FreeBSD/FreeBSD.git] / usr.bin / unifdef / unifdef.c
1 /*
2  * Copyright (c) 2002 - 2015 Tony Finch <dot@dotat.at>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25
26 /*
27  * unifdef - remove ifdef'ed lines
28  *
29  * This code was derived from software contributed to Berkeley by Dave Yost.
30  * It was rewritten to support ANSI C by Tony Finch. The original version
31  * of unifdef carried the 4-clause BSD copyright licence. None of its code
32  * remains in this version (though some of the names remain) so it now
33  * carries a more liberal licence.
34  *
35  *  Wishlist:
36  *      provide an option which will append the name of the
37  *        appropriate symbol after #else's and #endif's
38  *      provide an option which will check symbols after
39  *        #else's and #endif's to see that they match their
40  *        corresponding #ifdef or #ifndef
41  *
42  *   These require better buffer handling, which would also make
43  *   it possible to handle all "dodgy" directives correctly.
44  */
45
46 #include "unifdef.h"
47
48 static const char copyright[] =
49     "@(#) $Version: unifdef-2.11 $\n"
50     "@(#) $FreeBSD$\n"
51     "@(#) $Author: Tony Finch (dot@dotat.at) $\n"
52     "@(#) $URL: http://dotat.at/prog/unifdef $\n"
53 ;
54
55 /* types of input lines: */
56 typedef enum {
57         LT_TRUEI,               /* a true #if with ignore flag */
58         LT_FALSEI,              /* a false #if with ignore flag */
59         LT_IF,                  /* an unknown #if */
60         LT_TRUE,                /* a true #if */
61         LT_FALSE,               /* a false #if */
62         LT_ELIF,                /* an unknown #elif */
63         LT_ELTRUE,              /* a true #elif */
64         LT_ELFALSE,             /* a false #elif */
65         LT_ELSE,                /* #else */
66         LT_ENDIF,               /* #endif */
67         LT_DODGY,               /* flag: directive is not on one line */
68         LT_DODGY_LAST = LT_DODGY + LT_ENDIF,
69         LT_PLAIN,               /* ordinary line */
70         LT_EOF,                 /* end of file */
71         LT_ERROR,               /* unevaluable #if */
72         LT_COUNT
73 } Linetype;
74
75 static char const * const linetype_name[] = {
76         "TRUEI", "FALSEI", "IF", "TRUE", "FALSE",
77         "ELIF", "ELTRUE", "ELFALSE", "ELSE", "ENDIF",
78         "DODGY TRUEI", "DODGY FALSEI",
79         "DODGY IF", "DODGY TRUE", "DODGY FALSE",
80         "DODGY ELIF", "DODGY ELTRUE", "DODGY ELFALSE",
81         "DODGY ELSE", "DODGY ENDIF",
82         "PLAIN", "EOF", "ERROR"
83 };
84
85 #define linetype_if2elif(lt) ((Linetype)(lt - LT_IF + LT_ELIF))
86 #define linetype_2dodgy(lt) ((Linetype)(lt + LT_DODGY))
87
88 /* state of #if processing */
89 typedef enum {
90         IS_OUTSIDE,
91         IS_FALSE_PREFIX,        /* false #if followed by false #elifs */
92         IS_TRUE_PREFIX,         /* first non-false #(el)if is true */
93         IS_PASS_MIDDLE,         /* first non-false #(el)if is unknown */
94         IS_FALSE_MIDDLE,        /* a false #elif after a pass state */
95         IS_TRUE_MIDDLE,         /* a true #elif after a pass state */
96         IS_PASS_ELSE,           /* an else after a pass state */
97         IS_FALSE_ELSE,          /* an else after a true state */
98         IS_TRUE_ELSE,           /* an else after only false states */
99         IS_FALSE_TRAILER,       /* #elifs after a true are false */
100         IS_COUNT
101 } Ifstate;
102
103 static char const * const ifstate_name[] = {
104         "OUTSIDE", "FALSE_PREFIX", "TRUE_PREFIX",
105         "PASS_MIDDLE", "FALSE_MIDDLE", "TRUE_MIDDLE",
106         "PASS_ELSE", "FALSE_ELSE", "TRUE_ELSE",
107         "FALSE_TRAILER"
108 };
109
110 /* state of comment parser */
111 typedef enum {
112         NO_COMMENT = false,     /* outside a comment */
113         C_COMMENT,              /* in a comment like this one */
114         CXX_COMMENT,            /* between // and end of line */
115         STARTING_COMMENT,       /* just after slash-backslash-newline */
116         FINISHING_COMMENT,      /* star-backslash-newline in a C comment */
117         CHAR_LITERAL,           /* inside '' */
118         STRING_LITERAL          /* inside "" */
119 } Comment_state;
120
121 static char const * const comment_name[] = {
122         "NO", "C", "CXX", "STARTING", "FINISHING", "CHAR", "STRING"
123 };
124
125 /* state of preprocessor line parser */
126 typedef enum {
127         LS_START,               /* only space and comments on this line */
128         LS_HASH,                /* only space, comments, and a hash */
129         LS_DIRTY                /* this line can't be a preprocessor line */
130 } Line_state;
131
132 static char const * const linestate_name[] = {
133         "START", "HASH", "DIRTY"
134 };
135
136 /*
137  * Minimum translation limits from ISO/IEC 9899:1999 5.2.4.1
138  */
139 #define MAXDEPTH        64                      /* maximum #if nesting */
140 #define MAXLINE         4096                    /* maximum length of line */
141 #define MAXSYMS         16384                   /* maximum number of symbols */
142
143 /*
144  * Sometimes when editing a keyword the replacement text is longer, so
145  * we leave some space at the end of the tline buffer to accommodate this.
146  */
147 #define EDITSLOP        10
148
149 /*
150  * Globals.
151  */
152
153 static bool             compblank;              /* -B: compress blank lines */
154 static bool             lnblank;                /* -b: blank deleted lines */
155 static bool             complement;             /* -c: do the complement */
156 static bool             debugging;              /* -d: debugging reports */
157 static bool             inplace;                /* -m: modify in place */
158 static bool             iocccok;                /* -e: fewer IOCCC errors */
159 static bool             strictlogic;            /* -K: keep ambiguous #ifs */
160 static bool             killconsts;             /* -k: eval constant #ifs */
161 static bool             lnnum;                  /* -n: add #line directives */
162 static bool             symlist;                /* -s: output symbol list */
163 static bool             symdepth;               /* -S: output symbol depth */
164 static bool             text;                   /* -t: this is a text file */
165
166 static const char      *symname[MAXSYMS];       /* symbol name */
167 static const char      *value[MAXSYMS];         /* -Dsym=value */
168 static bool             ignore[MAXSYMS];        /* -iDsym or -iUsym */
169 static int              nsyms;                  /* number of symbols */
170
171 static FILE            *input;                  /* input file pointer */
172 static const char      *filename;               /* input file name */
173 static int              linenum;                /* current line number */
174 static const char      *linefile;               /* file name for #line */
175 static FILE            *output;                 /* output file pointer */
176 static const char      *ofilename;              /* output file name */
177 static const char      *backext;                /* backup extension */
178 static char            *tempname;               /* avoid splatting input */
179
180 static char             tline[MAXLINE+EDITSLOP];/* input buffer plus space */
181 static char            *keyword;                /* used for editing #elif's */
182
183 /*
184  * When processing a file, the output's newline style will match the
185  * input's, and unifdef correctly handles CRLF or LF endings whatever
186  * the platform's native style. The stdio streams are opened in binary
187  * mode to accommodate platforms whose native newline style is CRLF.
188  * When the output isn't a processed input file (when it is error /
189  * debug / diagnostic messages) then unifdef uses native line endings.
190  */
191
192 static const char      *newline;                /* input file format */
193 static const char       newline_unix[] = "\n";
194 static const char       newline_crlf[] = "\r\n";
195
196 static Comment_state    incomment;              /* comment parser state */
197 static Line_state       linestate;              /* #if line parser state */
198 static Ifstate          ifstate[MAXDEPTH];      /* #if processor state */
199 static bool             ignoring[MAXDEPTH];     /* ignore comments state */
200 static int              stifline[MAXDEPTH];     /* start of current #if */
201 static int              depth;                  /* current #if nesting */
202 static int              delcount;               /* count of deleted lines */
203 static unsigned         blankcount;             /* count of blank lines */
204 static unsigned         blankmax;               /* maximum recent blankcount */
205 static bool             constexpr;              /* constant #if expression */
206 static bool             zerosyms;               /* to format symdepth output */
207 static bool             firstsym;               /* ditto */
208
209 static int              exitmode;               /* exit status mode */
210 static int              exitstat;               /* program exit status */
211 static bool             altered;                /* was this file modified? */
212
213 static void             addsym1(bool, bool, char *);
214 static void             addsym2(bool, const char *, const char *);
215 static char            *astrcat(const char *, const char *);
216 static void             cleantemp(void);
217 static void             closeio(void);
218 static void             debug(const char *, ...);
219 static void             debugsym(const char *, int);
220 static bool             defundef(void);
221 static void             defundefile(const char *);
222 static void             done(void);
223 static void             error(const char *);
224 static int              findsym(const char **);
225 static void             flushline(bool);
226 static void             hashline(void);
227 static void             help(void);
228 static Linetype         ifeval(const char **);
229 static void             ignoreoff(void);
230 static void             ignoreon(void);
231 static void             indirectsym(void);
232 static void             keywordedit(const char *);
233 static const char      *matchsym(const char *, const char *);
234 static void             nest(void);
235 static Linetype         parseline(void);
236 static void             process(void);
237 static void             processinout(const char *, const char *);
238 static const char      *skipargs(const char *);
239 static const char      *skipcomment(const char *);
240 static const char      *skiphash(void);
241 static const char      *skipline(const char *);
242 static const char      *skipsym(const char *);
243 static void             state(Ifstate);
244 static void             unnest(void);
245 static void             usage(void);
246 static void             version(void);
247 static const char      *xstrdup(const char *, const char *);
248
249 #define endsym(c) (!isalnum((unsigned char)c) && c != '_')
250
251 /*
252  * The main program.
253  */
254 int
255 main(int argc, char *argv[])
256 {
257         int opt;
258
259         while ((opt = getopt(argc, argv, "i:D:U:f:I:M:o:x:bBcdehKklmnsStV")) != -1)
260                 switch (opt) {
261                 case 'i': /* treat stuff controlled by these symbols as text */
262                         /*
263                          * For strict backwards-compatibility the U or D
264                          * should be immediately after the -i but it doesn't
265                          * matter much if we relax that requirement.
266                          */
267                         opt = *optarg++;
268                         if (opt == 'D')
269                                 addsym1(true, true, optarg);
270                         else if (opt == 'U')
271                                 addsym1(true, false, optarg);
272                         else
273                                 usage();
274                         break;
275                 case 'D': /* define a symbol */
276                         addsym1(false, true, optarg);
277                         break;
278                 case 'U': /* undef a symbol */
279                         addsym1(false, false, optarg);
280                         break;
281                 case 'I': /* no-op for compatibility with cpp */
282                         break;
283                 case 'b': /* blank deleted lines instead of omitting them */
284                 case 'l': /* backwards compatibility */
285                         lnblank = true;
286                         break;
287                 case 'B': /* compress blank lines around removed section */
288                         compblank = true;
289                         break;
290                 case 'c': /* treat -D as -U and vice versa */
291                         complement = true;
292                         break;
293                 case 'd':
294                         debugging = true;
295                         break;
296                 case 'e': /* fewer errors from dodgy lines */
297                         iocccok = true;
298                         break;
299                 case 'f': /* definitions file */
300                         defundefile(optarg);
301                         break;
302                 case 'h':
303                         help();
304                         break;
305                 case 'K': /* keep ambiguous #ifs */
306                         strictlogic = true;
307                         break;
308                 case 'k': /* process constant #ifs */
309                         killconsts = true;
310                         break;
311                 case 'm': /* modify in place */
312                         inplace = true;
313                         break;
314                 case 'M': /* modify in place and keep backup */
315                         inplace = true;
316                         if (strlen(optarg) > 0)
317                                 backext = optarg;
318                         break;
319                 case 'n': /* add #line directive after deleted lines */
320                         lnnum = true;
321                         break;
322                 case 'o': /* output to a file */
323                         ofilename = optarg;
324                         break;
325                 case 's': /* only output list of symbols that control #ifs */
326                         symlist = true;
327                         break;
328                 case 'S': /* list symbols with their nesting depth */
329                         symlist = symdepth = true;
330                         break;
331                 case 't': /* don't parse C comments */
332                         text = true;
333                         break;
334                 case 'V':
335                         version();
336                         break;
337                 case 'x':
338                         exitmode = atoi(optarg);
339                         if(exitmode < 0 || exitmode > 2)
340                                 usage();
341                         break;
342                 default:
343                         usage();
344                 }
345         argc -= optind;
346         argv += optind;
347         if (compblank && lnblank)
348                 errx(2, "-B and -b are mutually exclusive");
349         if (symlist && (ofilename != NULL || inplace || argc > 1))
350                 errx(2, "-s only works with one input file");
351         if (argc > 1 && ofilename != NULL)
352                 errx(2, "-o cannot be used with multiple input files");
353         if (argc > 1 && !inplace)
354                 errx(2, "multiple input files require -m or -M");
355         if (argc == 0 && inplace)
356                 errx(2, "-m requires an input file");
357         if (argc == 0)
358                 argc = 1;
359         if (argc == 1 && !inplace && ofilename == NULL)
360                 ofilename = "-";
361         indirectsym();
362
363         atexit(cleantemp);
364         if (ofilename != NULL)
365                 processinout(*argv, ofilename);
366         else while (argc-- > 0) {
367                 processinout(*argv, *argv);
368                 argv++;
369         }
370         switch(exitmode) {
371         case(0): exit(exitstat);
372         case(1): exit(!exitstat);
373         case(2): exit(0);
374         default: abort(); /* bug */
375         }
376 }
377
378 /*
379  * File logistics.
380  */
381 static void
382 processinout(const char *ifn, const char *ofn)
383 {
384         struct stat st;
385
386         if (ifn == NULL || strcmp(ifn, "-") == 0) {
387                 filename = "[stdin]";
388                 linefile = NULL;
389                 input = fbinmode(stdin);
390         } else {
391                 filename = ifn;
392                 linefile = ifn;
393                 input = fopen(ifn, "rb");
394                 if (input == NULL)
395                         err(2, "can't open %s", ifn);
396         }
397         if (strcmp(ofn, "-") == 0) {
398                 output = fbinmode(stdout);
399                 process();
400                 return;
401         }
402         if (stat(ofn, &st) < 0) {
403                 output = fopen(ofn, "wb");
404                 if (output == NULL)
405                         err(2, "can't create %s", ofn);
406                 process();
407                 return;
408         }
409
410         tempname = astrcat(ofn, ".XXXXXX");
411         output = mktempmode(tempname, st.st_mode);
412         if (output == NULL)
413                 err(2, "can't create %s", tempname);
414
415         process();
416
417         if (backext != NULL) {
418                 char *backname = astrcat(ofn, backext);
419                 if (rename(ofn, backname) < 0)
420                         err(2, "can't rename \"%s\" to \"%s\"", ofn, backname);
421                 free(backname);
422         }
423         /* leave file unmodified if unifdef made no changes */
424         if (!altered && backext == NULL) {
425                 if (remove(tempname) < 0)
426                         warn("can't remove \"%s\"", tempname);
427         } else if (replace(tempname, ofn) < 0)
428                 err(2, "can't rename \"%s\" to \"%s\"", tempname, ofn);
429         free(tempname);
430         tempname = NULL;
431 }
432
433 /*
434  * For cleaning up if there is an error.
435  */
436 static void
437 cleantemp(void)
438 {
439         if (tempname != NULL)
440                 remove(tempname);
441 }
442
443 /*
444  * Self-identification functions.
445  */
446
447 static void
448 version(void)
449 {
450         const char *c = copyright;
451         for (;;) {
452                 while (*++c != '$')
453                         if (*c == '\0')
454                                 exit(0);
455                 while (*++c != '$')
456                         putc(*c, stderr);
457                 putc('\n', stderr);
458         }
459 }
460
461 static void
462 synopsis(FILE *fp)
463 {
464         fprintf(fp,
465             "usage:     unifdef [-bBcdehKkmnsStV] [-x{012}] [-Mext] [-opath] \\\n"
466             "           [-[i]Dsym[=val]] [-[i]Usym] [-fpath] ... [file] ...\n");
467 }
468
469 static void
470 usage(void)
471 {
472         synopsis(stderr);
473         exit(2);
474 }
475
476 static void
477 help(void)
478 {
479         synopsis(stdout);
480         printf(
481             "   -Dsym=val  define preprocessor symbol with given value\n"
482             "   -Dsym      define preprocessor symbol with value 1\n"
483             "   -Usym      preprocessor symbol is undefined\n"
484             "   -iDsym=val \\  ignore C strings and comments\n"
485             "   -iDsym      ) in sections controlled by these\n"
486             "   -iUsym     /  preprocessor symbols\n"
487             "   -fpath  file containing #define and #undef directives\n"
488             "   -b      blank lines instead of deleting them\n"
489             "   -B      compress blank lines around deleted section\n"
490             "   -c      complement (invert) keep vs. delete\n"
491             "   -d      debugging mode\n"
492             "   -e      ignore multiline preprocessor directives\n"
493             "   -h      print help\n"
494             "   -Ipath  extra include file path (ignored)\n"
495             "   -K      disable && and || short-circuiting\n"
496             "   -k      process constant #if expressions\n"
497             "   -Mext   modify in place and keep backups\n"
498             "   -m      modify input files in place\n"
499             "   -n      add #line directives to output\n"
500             "   -opath  output file name\n"
501             "   -S      list #if control symbols with nesting\n"
502             "   -s      list #if control symbols\n"
503             "   -t      ignore C strings and comments\n"
504             "   -V      print version\n"
505             "   -x{012} exit status mode\n"
506         );
507         exit(0);
508 }
509
510 /*
511  * A state transition function alters the global #if processing state
512  * in a particular way. The table below is indexed by the current
513  * processing state and the type of the current line.
514  *
515  * Nesting is handled by keeping a stack of states; some transition
516  * functions increase or decrease the depth. They also maintain the
517  * ignore state on a stack. In some complicated cases they have to
518  * alter the preprocessor directive, as follows.
519  *
520  * When we have processed a group that starts off with a known-false
521  * #if/#elif sequence (which has therefore been deleted) followed by a
522  * #elif that we don't understand and therefore must keep, we edit the
523  * latter into a #if to keep the nesting correct. We use memcpy() to
524  * overwrite the 4 byte token "elif" with "if  " without a '\0' byte.
525  *
526  * When we find a true #elif in a group, the following block will
527  * always be kept and the rest of the sequence after the next #elif or
528  * #else will be discarded. We edit the #elif into a #else and the
529  * following directive to #endif since this has the desired behaviour.
530  *
531  * "Dodgy" directives are split across multiple lines, the most common
532  * example being a multi-line comment hanging off the right of the
533  * directive. We can handle them correctly only if there is no change
534  * from printing to dropping (or vice versa) caused by that directive.
535  * If the directive is the first of a group we have a choice between
536  * failing with an error, or passing it through unchanged instead of
537  * evaluating it. The latter is not the default to avoid questions from
538  * users about unifdef unexpectedly leaving behind preprocessor directives.
539  */
540 typedef void state_fn(void);
541
542 /* report an error */
543 static void Eelif (void) { error("Inappropriate #elif"); }
544 static void Eelse (void) { error("Inappropriate #else"); }
545 static void Eendif(void) { error("Inappropriate #endif"); }
546 static void Eeof  (void) { error("Premature EOF"); }
547 static void Eioccc(void) { error("Obfuscated preprocessor control line"); }
548 /* plain line handling */
549 static void print (void) { flushline(true); }
550 static void drop  (void) { flushline(false); }
551 /* output lacks group's start line */
552 static void Strue (void) { drop();  ignoreoff(); state(IS_TRUE_PREFIX); }
553 static void Sfalse(void) { drop();  ignoreoff(); state(IS_FALSE_PREFIX); }
554 static void Selse (void) { drop();               state(IS_TRUE_ELSE); }
555 /* print/pass this block */
556 static void Pelif (void) { print(); ignoreoff(); state(IS_PASS_MIDDLE); }
557 static void Pelse (void) { print();              state(IS_PASS_ELSE); }
558 static void Pendif(void) { print(); unnest(); }
559 /* discard this block */
560 static void Dfalse(void) { drop();  ignoreoff(); state(IS_FALSE_TRAILER); }
561 static void Delif (void) { drop();  ignoreoff(); state(IS_FALSE_MIDDLE); }
562 static void Delse (void) { drop();               state(IS_FALSE_ELSE); }
563 static void Dendif(void) { drop();  unnest(); }
564 /* first line of group */
565 static void Fdrop (void) { nest();  Dfalse(); }
566 static void Fpass (void) { nest();  Pelif(); }
567 static void Ftrue (void) { nest();  Strue(); }
568 static void Ffalse(void) { nest();  Sfalse(); }
569 /* variable pedantry for obfuscated lines */
570 static void Oiffy (void) { if (!iocccok) Eioccc(); Fpass(); ignoreon(); }
571 static void Oif   (void) { if (!iocccok) Eioccc(); Fpass(); }
572 static void Oelif (void) { if (!iocccok) Eioccc(); Pelif(); }
573 /* ignore comments in this block */
574 static void Idrop (void) { Fdrop();  ignoreon(); }
575 static void Itrue (void) { Ftrue();  ignoreon(); }
576 static void Ifalse(void) { Ffalse(); ignoreon(); }
577 /* modify this line */
578 static void Mpass (void) { memcpy(keyword, "if  ", 4); Pelif(); }
579 static void Mtrue (void) { keywordedit("else");  state(IS_TRUE_MIDDLE); }
580 static void Melif (void) { keywordedit("endif"); state(IS_FALSE_TRAILER); }
581 static void Melse (void) { keywordedit("endif"); state(IS_FALSE_ELSE); }
582
583 static state_fn * const trans_table[IS_COUNT][LT_COUNT] = {
584 /* IS_OUTSIDE */
585 { Itrue, Ifalse,Fpass, Ftrue, Ffalse,Eelif, Eelif, Eelif, Eelse, Eendif,
586   Oiffy, Oiffy, Fpass, Oif,   Oif,   Eelif, Eelif, Eelif, Eelse, Eendif,
587   print, done,  abort },
588 /* IS_FALSE_PREFIX */
589 { Idrop, Idrop, Fdrop, Fdrop, Fdrop, Mpass, Strue, Sfalse,Selse, Dendif,
590   Idrop, Idrop, Fdrop, Fdrop, Fdrop, Mpass, Eioccc,Eioccc,Eioccc,Eioccc,
591   drop,  Eeof,  abort },
592 /* IS_TRUE_PREFIX */
593 { Itrue, Ifalse,Fpass, Ftrue, Ffalse,Dfalse,Dfalse,Dfalse,Delse, Dendif,
594   Oiffy, Oiffy, Fpass, Oif,   Oif,   Eioccc,Eioccc,Eioccc,Eioccc,Eioccc,
595   print, Eeof,  abort },
596 /* IS_PASS_MIDDLE */
597 { Itrue, Ifalse,Fpass, Ftrue, Ffalse,Pelif, Mtrue, Delif, Pelse, Pendif,
598   Oiffy, Oiffy, Fpass, Oif,   Oif,   Pelif, Oelif, Oelif, Pelse, Pendif,
599   print, Eeof,  abort },
600 /* IS_FALSE_MIDDLE */
601 { Idrop, Idrop, Fdrop, Fdrop, Fdrop, Pelif, Mtrue, Delif, Pelse, Pendif,
602   Idrop, Idrop, Fdrop, Fdrop, Fdrop, Eioccc,Eioccc,Eioccc,Eioccc,Eioccc,
603   drop,  Eeof,  abort },
604 /* IS_TRUE_MIDDLE */
605 { Itrue, Ifalse,Fpass, Ftrue, Ffalse,Melif, Melif, Melif, Melse, Pendif,
606   Oiffy, Oiffy, Fpass, Oif,   Oif,   Eioccc,Eioccc,Eioccc,Eioccc,Pendif,
607   print, Eeof,  abort },
608 /* IS_PASS_ELSE */
609 { Itrue, Ifalse,Fpass, Ftrue, Ffalse,Eelif, Eelif, Eelif, Eelse, Pendif,
610   Oiffy, Oiffy, Fpass, Oif,   Oif,   Eelif, Eelif, Eelif, Eelse, Pendif,
611   print, Eeof,  abort },
612 /* IS_FALSE_ELSE */
613 { Idrop, Idrop, Fdrop, Fdrop, Fdrop, Eelif, Eelif, Eelif, Eelse, Dendif,
614   Idrop, Idrop, Fdrop, Fdrop, Fdrop, Eelif, Eelif, Eelif, Eelse, Eioccc,
615   drop,  Eeof,  abort },
616 /* IS_TRUE_ELSE */
617 { Itrue, Ifalse,Fpass, Ftrue, Ffalse,Eelif, Eelif, Eelif, Eelse, Dendif,
618   Oiffy, Oiffy, Fpass, Oif,   Oif,   Eelif, Eelif, Eelif, Eelse, Eioccc,
619   print, Eeof,  abort },
620 /* IS_FALSE_TRAILER */
621 { Idrop, Idrop, Fdrop, Fdrop, Fdrop, Dfalse,Dfalse,Dfalse,Delse, Dendif,
622   Idrop, Idrop, Fdrop, Fdrop, Fdrop, Dfalse,Dfalse,Dfalse,Delse, Eioccc,
623   drop,  Eeof,  abort }
624 /*TRUEI  FALSEI IF     TRUE   FALSE  ELIF   ELTRUE ELFALSE ELSE  ENDIF
625   TRUEI  FALSEI IF     TRUE   FALSE  ELIF   ELTRUE ELFALSE ELSE  ENDIF (DODGY)
626   PLAIN  EOF    ERROR */
627 };
628
629 /*
630  * State machine utility functions
631  */
632 static void
633 ignoreoff(void)
634 {
635         if (depth == 0)
636                 abort(); /* bug */
637         ignoring[depth] = ignoring[depth-1];
638 }
639 static void
640 ignoreon(void)
641 {
642         ignoring[depth] = true;
643 }
644 static void
645 keywordedit(const char *replacement)
646 {
647         snprintf(keyword, tline + sizeof(tline) - keyword,
648             "%s%s", replacement, newline);
649         altered = true;
650         print();
651 }
652 static void
653 nest(void)
654 {
655         if (depth > MAXDEPTH-1)
656                 abort(); /* bug */
657         if (depth == MAXDEPTH-1)
658                 error("Too many levels of nesting");
659         depth += 1;
660         stifline[depth] = linenum;
661 }
662 static void
663 unnest(void)
664 {
665         if (depth == 0)
666                 abort(); /* bug */
667         depth -= 1;
668 }
669 static void
670 state(Ifstate is)
671 {
672         ifstate[depth] = is;
673 }
674
675 /*
676  * The last state transition function. When this is called,
677  * lineval == LT_EOF, so the process() loop will terminate.
678  */
679 static void
680 done(void)
681 {
682         if (incomment)
683                 error("EOF in comment");
684         closeio();
685 }
686
687 /*
688  * Write a line to the output or not, according to command line options.
689  * If writing fails, closeio() will print the error and exit.
690  */
691 static void
692 flushline(bool keep)
693 {
694         if (symlist)
695                 return;
696         if (keep ^ complement) {
697                 bool blankline = tline[strspn(tline, " \t\r\n")] == '\0';
698                 if (blankline && compblank && blankcount != blankmax) {
699                         delcount += 1;
700                         blankcount += 1;
701                 } else {
702                         if (lnnum && delcount > 0)
703                                 hashline();
704                         if (fputs(tline, output) == EOF)
705                                 closeio();
706                         delcount = 0;
707                         blankmax = blankcount = blankline ? blankcount + 1 : 0;
708                 }
709         } else {
710                 if (lnblank && fputs(newline, output) == EOF)
711                         closeio();
712                 altered = true;
713                 delcount += 1;
714                 blankcount = 0;
715         }
716         if (debugging && fflush(output) == EOF)
717                 closeio();
718 }
719
720 /*
721  * Format of #line directives depends on whether we know the input filename.
722  */
723 static void
724 hashline(void)
725 {
726         int e;
727
728         if (linefile == NULL)
729                 e = fprintf(output, "#line %d%s", linenum, newline);
730         else
731                 e = fprintf(output, "#line %d \"%s\"%s",
732                     linenum, linefile, newline);
733         if (e < 0)
734                 closeio();
735 }
736
737 /*
738  * Flush the output and handle errors.
739  */
740 static void
741 closeio(void)
742 {
743         /* Tidy up after findsym(). */
744         if (symdepth && !zerosyms)
745                 printf("\n");
746         if (output != NULL && (ferror(output) || fclose(output) == EOF))
747                         err(2, "%s: can't write to output", filename);
748         fclose(input);
749 }
750
751 /*
752  * The driver for the state machine.
753  */
754 static void
755 process(void)
756 {
757         Linetype lineval = LT_PLAIN;
758         /* When compressing blank lines, act as if the file
759            is preceded by a large number of blank lines. */
760         blankmax = blankcount = 1000;
761         zerosyms = true;
762         newline = NULL;
763         linenum = 0;
764         altered = false;
765         while (lineval != LT_EOF) {
766                 lineval = parseline();
767                 trans_table[ifstate[depth]][lineval]();
768                 debug("process line %d %s -> %s depth %d",
769                     linenum, linetype_name[lineval],
770                     ifstate_name[ifstate[depth]], depth);
771         }
772         exitstat |= altered;
773 }
774
775 /*
776  * Parse a line and determine its type. We keep the preprocessor line
777  * parser state between calls in the global variable linestate, with
778  * help from skipcomment().
779  */
780 static Linetype
781 parseline(void)
782 {
783         const char *cp;
784         int cursym;
785         Linetype retval;
786         Comment_state wascomment;
787
788         wascomment = incomment;
789         cp = skiphash();
790         if (cp == NULL)
791                 return (LT_EOF);
792         if (newline == NULL) {
793                 if (strrchr(tline, '\n') == strrchr(tline, '\r') + 1)
794                         newline = newline_crlf;
795                 else
796                         newline = newline_unix;
797         }
798         if (*cp == '\0') {
799                 retval = LT_PLAIN;
800                 goto done;
801         }
802         keyword = tline + (cp - tline);
803         if ((cp = matchsym("ifdef", keyword)) != NULL ||
804             (cp = matchsym("ifndef", keyword)) != NULL) {
805                 cp = skipcomment(cp);
806                 if ((cursym = findsym(&cp)) < 0)
807                         retval = LT_IF;
808                 else {
809                         retval = (keyword[2] == 'n')
810                             ? LT_FALSE : LT_TRUE;
811                         if (value[cursym] == NULL)
812                                 retval = (retval == LT_TRUE)
813                                     ? LT_FALSE : LT_TRUE;
814                         if (ignore[cursym])
815                                 retval = (retval == LT_TRUE)
816                                     ? LT_TRUEI : LT_FALSEI;
817                 }
818         } else if ((cp = matchsym("if", keyword)) != NULL)
819                 retval = ifeval(&cp);
820         else if ((cp = matchsym("elif", keyword)) != NULL)
821                 retval = linetype_if2elif(ifeval(&cp));
822         else if ((cp = matchsym("else", keyword)) != NULL)
823                 retval = LT_ELSE;
824         else if ((cp = matchsym("endif", keyword)) != NULL)
825                 retval = LT_ENDIF;
826         else {
827                 cp = skipsym(keyword);
828                 /* no way can we deal with a continuation inside a keyword */
829                 if (strncmp(cp, "\\\r\n", 3) == 0 ||
830                     strncmp(cp, "\\\n", 2) == 0)
831                         Eioccc();
832                 cp = skipline(cp);
833                 retval = LT_PLAIN;
834                 goto done;
835         }
836         cp = skipcomment(cp);
837         if (*cp != '\0') {
838                 cp = skipline(cp);
839                 if (retval == LT_TRUE || retval == LT_FALSE ||
840                     retval == LT_TRUEI || retval == LT_FALSEI)
841                         retval = LT_IF;
842                 if (retval == LT_ELTRUE || retval == LT_ELFALSE)
843                         retval = LT_ELIF;
844         }
845         /* the following can happen if the last line of the file lacks a
846            newline or if there is too much whitespace in a directive */
847         if (linestate == LS_HASH) {
848                 long len = cp - tline;
849                 if (fgets(tline + len, MAXLINE - len, input) == NULL) {
850                         if (ferror(input))
851                                 err(2, "can't read %s", filename);
852                         /* append the missing newline at eof */
853                         strcpy(tline + len, newline);
854                         cp += strlen(newline);
855                         linestate = LS_START;
856                 } else {
857                         linestate = LS_DIRTY;
858                 }
859         }
860         if (retval != LT_PLAIN && (wascomment || linestate != LS_START)) {
861                 retval = linetype_2dodgy(retval);
862                 linestate = LS_DIRTY;
863         }
864 done:
865         debug("parser line %d state %s comment %s line", linenum,
866             comment_name[incomment], linestate_name[linestate]);
867         return (retval);
868 }
869
870 /*
871  * These are the binary operators that are supported by the expression
872  * evaluator.
873  */
874 static Linetype op_strict(long *p, long v, Linetype at, Linetype bt) {
875         if(at == LT_IF || bt == LT_IF) return (LT_IF);
876         return (*p = v, v ? LT_TRUE : LT_FALSE);
877 }
878 static Linetype op_lt(long *p, Linetype at, long a, Linetype bt, long b) {
879         return op_strict(p, a < b, at, bt);
880 }
881 static Linetype op_gt(long *p, Linetype at, long a, Linetype bt, long b) {
882         return op_strict(p, a > b, at, bt);
883 }
884 static Linetype op_le(long *p, Linetype at, long a, Linetype bt, long b) {
885         return op_strict(p, a <= b, at, bt);
886 }
887 static Linetype op_ge(long *p, Linetype at, long a, Linetype bt, long b) {
888         return op_strict(p, a >= b, at, bt);
889 }
890 static Linetype op_eq(long *p, Linetype at, long a, Linetype bt, long b) {
891         return op_strict(p, a == b, at, bt);
892 }
893 static Linetype op_ne(long *p, Linetype at, long a, Linetype bt, long b) {
894         return op_strict(p, a != b, at, bt);
895 }
896 static Linetype op_or(long *p, Linetype at, long a, Linetype bt, long b) {
897         if (!strictlogic && (at == LT_TRUE || bt == LT_TRUE))
898                 return (*p = 1, LT_TRUE);
899         return op_strict(p, a || b, at, bt);
900 }
901 static Linetype op_and(long *p, Linetype at, long a, Linetype bt, long b) {
902         if (!strictlogic && (at == LT_FALSE || bt == LT_FALSE))
903                 return (*p = 0, LT_FALSE);
904         return op_strict(p, a && b, at, bt);
905 }
906 static Linetype op_blsh(long *p, Linetype at, long a, Linetype bt, long b) {
907         return op_strict(p, a << b, at, bt);
908 }
909 static Linetype op_brsh(long *p, Linetype at, long a, Linetype bt, long b) {
910         return op_strict(p, a >> b, at, bt);
911 }
912 static Linetype op_add(long *p, Linetype at, long a, Linetype bt, long b) {
913         return op_strict(p, a + b, at, bt);
914 }
915 static Linetype op_sub(long *p, Linetype at, long a, Linetype bt, long b) {
916         return op_strict(p, a - b, at, bt);
917 }
918 static Linetype op_mul(long *p, Linetype at, long a, Linetype bt, long b) {
919         return op_strict(p, a * b, at, bt);
920 }
921 static Linetype op_div(long *p, Linetype at, long a, Linetype bt, long b) {
922         if (bt != LT_TRUE) {
923                 debug("eval division by zero");
924                 return (LT_ERROR);
925         }
926         return op_strict(p, a / b, at, bt);
927 }
928 static Linetype op_mod(long *p, Linetype at, long a, Linetype bt, long b) {
929         return op_strict(p, a % b, at, bt);
930 }
931 static Linetype op_bor(long *p, Linetype at, long a, Linetype bt, long b) {
932         return op_strict(p, a | b, at, bt);
933 }
934 static Linetype op_bxor(long *p, Linetype at, long a, Linetype bt, long b) {
935         return op_strict(p, a ^ b, at, bt);
936 }
937 static Linetype op_band(long *p, Linetype at, long a, Linetype bt, long b) {
938         return op_strict(p, a & b, at, bt);
939 }
940
941 /*
942  * An evaluation function takes three arguments, as follows: (1) a pointer to
943  * an element of the precedence table which lists the operators at the current
944  * level of precedence; (2) a pointer to an integer which will receive the
945  * value of the expression; and (3) a pointer to a char* that points to the
946  * expression to be evaluated and that is updated to the end of the expression
947  * when evaluation is complete. The function returns LT_FALSE if the value of
948  * the expression is zero, LT_TRUE if it is non-zero, LT_IF if the expression
949  * depends on an unknown symbol, or LT_ERROR if there is a parse failure.
950  */
951 struct ops;
952
953 typedef Linetype eval_fn(const struct ops *, long *, const char **);
954
955 static eval_fn eval_table, eval_unary;
956
957 /*
958  * The precedence table. Expressions involving binary operators are evaluated
959  * in a table-driven way by eval_table. When it evaluates a subexpression it
960  * calls the inner function with its first argument pointing to the next
961  * element of the table. Innermost expressions have special non-table-driven
962  * handling.
963  *
964  * The stop characters help with lexical analysis: an operator is not
965  * recognized if it is followed by one of the stop characters because
966  * that would make it a different operator.
967  */
968 struct op {
969         const char *str;
970         Linetype (*fn)(long *, Linetype, long, Linetype, long);
971         const char *stop;
972 };
973 struct ops {
974         eval_fn *inner;
975         struct op op[5];
976 };
977 static const struct ops eval_ops[] = {
978         { eval_table, { { "||", op_or, NULL } } },
979         { eval_table, { { "&&", op_and, NULL } } },
980         { eval_table, { { "|", op_bor, "|" } } },
981         { eval_table, { { "^", op_bxor, NULL } } },
982         { eval_table, { { "&", op_band, "&" } } },
983         { eval_table, { { "==", op_eq, NULL },
984                         { "!=", op_ne, NULL } } },
985         { eval_table, { { "<=", op_le, NULL },
986                         { ">=", op_ge, NULL },
987                         { "<", op_lt, "<=" },
988                         { ">", op_gt, ">=" } } },
989         { eval_table, { { "<<", op_blsh, NULL },
990                         { ">>", op_brsh, NULL } } },
991         { eval_table, { { "+", op_add, NULL },
992                         { "-", op_sub, NULL } } },
993         { eval_unary, { { "*", op_mul, NULL },
994                         { "/", op_div, NULL },
995                         { "%", op_mod, NULL } } },
996 };
997
998 /* Current operator precedence level */
999 static long prec(const struct ops *ops)
1000 {
1001         return (ops - eval_ops);
1002 }
1003
1004 /*
1005  * Function for evaluating the innermost parts of expressions,
1006  * viz. !expr (expr) number defined(symbol) symbol
1007  * We reset the constexpr flag in the last two cases.
1008  */
1009 static Linetype
1010 eval_unary(const struct ops *ops, long *valp, const char **cpp)
1011 {
1012         const char *cp;
1013         char *ep;
1014         int sym;
1015         bool defparen;
1016         Linetype lt;
1017
1018         cp = skipcomment(*cpp);
1019         if (*cp == '!') {
1020                 debug("eval%d !", prec(ops));
1021                 cp++;
1022                 lt = eval_unary(ops, valp, &cp);
1023                 if (lt == LT_ERROR)
1024                         return (LT_ERROR);
1025                 if (lt != LT_IF) {
1026                         *valp = !*valp;
1027                         lt = *valp ? LT_TRUE : LT_FALSE;
1028                 }
1029         } else if (*cp == '~') {
1030                 debug("eval%d ~", prec(ops));
1031                 cp++;
1032                 lt = eval_unary(ops, valp, &cp);
1033                 if (lt == LT_ERROR)
1034                         return (LT_ERROR);
1035                 if (lt != LT_IF) {
1036                         *valp = ~(*valp);
1037                         lt = *valp ? LT_TRUE : LT_FALSE;
1038                 }
1039         } else if (*cp == '-') {
1040                 debug("eval%d -", prec(ops));
1041                 cp++;
1042                 lt = eval_unary(ops, valp, &cp);
1043                 if (lt == LT_ERROR)
1044                         return (LT_ERROR);
1045                 if (lt != LT_IF) {
1046                         *valp = -(*valp);
1047                         lt = *valp ? LT_TRUE : LT_FALSE;
1048                 }
1049         } else if (*cp == '(') {
1050                 cp++;
1051                 debug("eval%d (", prec(ops));
1052                 lt = eval_table(eval_ops, valp, &cp);
1053                 if (lt == LT_ERROR)
1054                         return (LT_ERROR);
1055                 cp = skipcomment(cp);
1056                 if (*cp++ != ')')
1057                         return (LT_ERROR);
1058         } else if (isdigit((unsigned char)*cp)) {
1059                 debug("eval%d number", prec(ops));
1060                 *valp = strtol(cp, &ep, 0);
1061                 if (ep == cp)
1062                         return (LT_ERROR);
1063                 lt = *valp ? LT_TRUE : LT_FALSE;
1064                 cp = ep;
1065         } else if (matchsym("defined", cp) != NULL) {
1066                 cp = skipcomment(cp+7);
1067                 if (*cp == '(') {
1068                         cp = skipcomment(cp+1);
1069                         defparen = true;
1070                 } else {
1071                         defparen = false;
1072                 }
1073                 sym = findsym(&cp);
1074                 cp = skipcomment(cp);
1075                 if (defparen && *cp++ != ')') {
1076                         debug("eval%d defined missing ')'", prec(ops));
1077                         return (LT_ERROR);
1078                 }
1079                 if (sym < 0) {
1080                         debug("eval%d defined unknown", prec(ops));
1081                         lt = LT_IF;
1082                 } else {
1083                         debug("eval%d defined %s", prec(ops), symname[sym]);
1084                         *valp = (value[sym] != NULL);
1085                         lt = *valp ? LT_TRUE : LT_FALSE;
1086                 }
1087                 constexpr = false;
1088         } else if (!endsym(*cp)) {
1089                 debug("eval%d symbol", prec(ops));
1090                 sym = findsym(&cp);
1091                 if (sym < 0) {
1092                         lt = LT_IF;
1093                         cp = skipargs(cp);
1094                 } else if (value[sym] == NULL) {
1095                         *valp = 0;
1096                         lt = LT_FALSE;
1097                 } else {
1098                         *valp = strtol(value[sym], &ep, 0);
1099                         if (*ep != '\0' || ep == value[sym])
1100                                 return (LT_ERROR);
1101                         lt = *valp ? LT_TRUE : LT_FALSE;
1102                         cp = skipargs(cp);
1103                 }
1104                 constexpr = false;
1105         } else {
1106                 debug("eval%d bad expr", prec(ops));
1107                 return (LT_ERROR);
1108         }
1109
1110         *cpp = cp;
1111         debug("eval%d = %d", prec(ops), *valp);
1112         return (lt);
1113 }
1114
1115 /*
1116  * Table-driven evaluation of binary operators.
1117  */
1118 static Linetype
1119 eval_table(const struct ops *ops, long *valp, const char **cpp)
1120 {
1121         const struct op *op;
1122         const char *cp;
1123         long val = 0;
1124         Linetype lt, rt;
1125
1126         debug("eval%d", prec(ops));
1127         cp = *cpp;
1128         lt = ops->inner(ops+1, valp, &cp);
1129         if (lt == LT_ERROR)
1130                 return (LT_ERROR);
1131         for (;;) {
1132                 cp = skipcomment(cp);
1133                 for (op = ops->op; op->str != NULL; op++) {
1134                         if (strncmp(cp, op->str, strlen(op->str)) == 0) {
1135                                 /* assume only one-char operators have stop chars */
1136                                 if (op->stop != NULL && cp[1] != '\0' &&
1137                                     strchr(op->stop, cp[1]) != NULL)
1138                                         continue;
1139                                 else
1140                                         break;
1141                         }
1142                 }
1143                 if (op->str == NULL)
1144                         break;
1145                 cp += strlen(op->str);
1146                 debug("eval%d %s", prec(ops), op->str);
1147                 rt = ops->inner(ops+1, &val, &cp);
1148                 if (rt == LT_ERROR)
1149                         return (LT_ERROR);
1150                 lt = op->fn(valp, lt, *valp, rt, val);
1151         }
1152
1153         *cpp = cp;
1154         debug("eval%d = %d", prec(ops), *valp);
1155         debug("eval%d lt = %s", prec(ops), linetype_name[lt]);
1156         return (lt);
1157 }
1158
1159 /*
1160  * Evaluate the expression on a #if or #elif line. If we can work out
1161  * the result we return LT_TRUE or LT_FALSE accordingly, otherwise we
1162  * return just a generic LT_IF.
1163  */
1164 static Linetype
1165 ifeval(const char **cpp)
1166 {
1167         Linetype ret;
1168         long val = 0;
1169
1170         debug("eval %s", *cpp);
1171         constexpr = killconsts ? false : true;
1172         ret = eval_table(eval_ops, &val, cpp);
1173         debug("eval = %d", val);
1174         return (constexpr ? LT_IF : ret == LT_ERROR ? LT_IF : ret);
1175 }
1176
1177 /*
1178  * Read a line and examine its initial part to determine if it is a
1179  * preprocessor directive. Returns NULL on EOF, or a pointer to a
1180  * preprocessor directive name, or a pointer to the zero byte at the
1181  * end of the line.
1182  */
1183 static const char *
1184 skiphash(void)
1185 {
1186         const char *cp;
1187
1188         linenum++;
1189         if (fgets(tline, MAXLINE, input) == NULL) {
1190                 if (ferror(input))
1191                         err(2, "can't read %s", filename);
1192                 else
1193                         return (NULL);
1194         }
1195         cp = skipcomment(tline);
1196         if (linestate == LS_START && *cp == '#') {
1197                 linestate = LS_HASH;
1198                 return (skipcomment(cp + 1));
1199         } else if (*cp == '\0') {
1200                 return (cp);
1201         } else {
1202                 return (skipline(cp));
1203         }
1204 }
1205
1206 /*
1207  * Mark a line dirty and consume the rest of it, keeping track of the
1208  * lexical state.
1209  */
1210 static const char *
1211 skipline(const char *cp)
1212 {
1213         const char *pcp;
1214         if (*cp != '\0')
1215                 linestate = LS_DIRTY;
1216         while (*cp != '\0') {
1217                 cp = skipcomment(pcp = cp);
1218                 if (pcp == cp)
1219                         cp++;
1220         }
1221         return (cp);
1222 }
1223
1224 /*
1225  * Skip over comments, strings, and character literals and stop at the
1226  * next character position that is not whitespace. Between calls we keep
1227  * the comment state in the global variable incomment, and we also adjust
1228  * the global variable linestate when we see a newline.
1229  * XXX: doesn't cope with the buffer splitting inside a state transition.
1230  */
1231 static const char *
1232 skipcomment(const char *cp)
1233 {
1234         if (text || ignoring[depth]) {
1235                 for (; isspace((unsigned char)*cp); cp++)
1236                         if (*cp == '\n')
1237                                 linestate = LS_START;
1238                 return (cp);
1239         }
1240         while (*cp != '\0')
1241                 /* don't reset to LS_START after a line continuation */
1242                 if (strncmp(cp, "\\\r\n", 3) == 0)
1243                         cp += 3;
1244                 else if (strncmp(cp, "\\\n", 2) == 0)
1245                         cp += 2;
1246                 else switch (incomment) {
1247                 case NO_COMMENT:
1248                         if (strncmp(cp, "/\\\r\n", 4) == 0) {
1249                                 incomment = STARTING_COMMENT;
1250                                 cp += 4;
1251                         } else if (strncmp(cp, "/\\\n", 3) == 0) {
1252                                 incomment = STARTING_COMMENT;
1253                                 cp += 3;
1254                         } else if (strncmp(cp, "/*", 2) == 0) {
1255                                 incomment = C_COMMENT;
1256                                 cp += 2;
1257                         } else if (strncmp(cp, "//", 2) == 0) {
1258                                 incomment = CXX_COMMENT;
1259                                 cp += 2;
1260                         } else if (strncmp(cp, "\'", 1) == 0) {
1261                                 incomment = CHAR_LITERAL;
1262                                 linestate = LS_DIRTY;
1263                                 cp += 1;
1264                         } else if (strncmp(cp, "\"", 1) == 0) {
1265                                 incomment = STRING_LITERAL;
1266                                 linestate = LS_DIRTY;
1267                                 cp += 1;
1268                         } else if (strncmp(cp, "\n", 1) == 0) {
1269                                 linestate = LS_START;
1270                                 cp += 1;
1271                         } else if (strchr(" \r\t", *cp) != NULL) {
1272                                 cp += 1;
1273                         } else
1274                                 return (cp);
1275                         continue;
1276                 case CXX_COMMENT:
1277                         if (strncmp(cp, "\n", 1) == 0) {
1278                                 incomment = NO_COMMENT;
1279                                 linestate = LS_START;
1280                         }
1281                         cp += 1;
1282                         continue;
1283                 case CHAR_LITERAL:
1284                 case STRING_LITERAL:
1285                         if ((incomment == CHAR_LITERAL && cp[0] == '\'') ||
1286                             (incomment == STRING_LITERAL && cp[0] == '\"')) {
1287                                 incomment = NO_COMMENT;
1288                                 cp += 1;
1289                         } else if (cp[0] == '\\') {
1290                                 if (cp[1] == '\0')
1291                                         cp += 1;
1292                                 else
1293                                         cp += 2;
1294                         } else if (strncmp(cp, "\n", 1) == 0) {
1295                                 if (incomment == CHAR_LITERAL)
1296                                         error("Unterminated char literal");
1297                                 else
1298                                         error("Unterminated string literal");
1299                         } else
1300                                 cp += 1;
1301                         continue;
1302                 case C_COMMENT:
1303                         if (strncmp(cp, "*\\\r\n", 4) == 0) {
1304                                 incomment = FINISHING_COMMENT;
1305                                 cp += 4;
1306                         } else if (strncmp(cp, "*\\\n", 3) == 0) {
1307                                 incomment = FINISHING_COMMENT;
1308                                 cp += 3;
1309                         } else if (strncmp(cp, "*/", 2) == 0) {
1310                                 incomment = NO_COMMENT;
1311                                 cp += 2;
1312                         } else
1313                                 cp += 1;
1314                         continue;
1315                 case STARTING_COMMENT:
1316                         if (*cp == '*') {
1317                                 incomment = C_COMMENT;
1318                                 cp += 1;
1319                         } else if (*cp == '/') {
1320                                 incomment = CXX_COMMENT;
1321                                 cp += 1;
1322                         } else {
1323                                 incomment = NO_COMMENT;
1324                                 linestate = LS_DIRTY;
1325                         }
1326                         continue;
1327                 case FINISHING_COMMENT:
1328                         if (*cp == '/') {
1329                                 incomment = NO_COMMENT;
1330                                 cp += 1;
1331                         } else
1332                                 incomment = C_COMMENT;
1333                         continue;
1334                 default:
1335                         abort(); /* bug */
1336                 }
1337         return (cp);
1338 }
1339
1340 /*
1341  * Skip macro arguments.
1342  */
1343 static const char *
1344 skipargs(const char *cp)
1345 {
1346         const char *ocp = cp;
1347         int level = 0;
1348         cp = skipcomment(cp);
1349         if (*cp != '(')
1350                 return (cp);
1351         do {
1352                 if (*cp == '(')
1353                         level++;
1354                 if (*cp == ')')
1355                         level--;
1356                 cp = skipcomment(cp+1);
1357         } while (level != 0 && *cp != '\0');
1358         if (level == 0)
1359                 return (cp);
1360         else
1361         /* Rewind and re-detect the syntax error later. */
1362                 return (ocp);
1363 }
1364
1365 /*
1366  * Skip over an identifier.
1367  */
1368 static const char *
1369 skipsym(const char *cp)
1370 {
1371         while (!endsym(*cp))
1372                 ++cp;
1373         return (cp);
1374 }
1375
1376 /*
1377  * Skip whitespace and take a copy of any following identifier.
1378  */
1379 static const char *
1380 getsym(const char **cpp)
1381 {
1382         const char *cp = *cpp, *sym;
1383
1384         cp = skipcomment(cp);
1385         cp = skipsym(sym = cp);
1386         if (cp == sym)
1387                 return NULL;
1388         *cpp = cp;
1389         return (xstrdup(sym, cp));
1390 }
1391
1392 /*
1393  * Check that s (a symbol) matches the start of t, and that the
1394  * following character in t is not a symbol character. Returns a
1395  * pointer to the following character in t if there is a match,
1396  * otherwise NULL.
1397  */
1398 static const char *
1399 matchsym(const char *s, const char *t)
1400 {
1401         while (*s != '\0' && *t != '\0')
1402                 if (*s != *t)
1403                         return (NULL);
1404                 else
1405                         ++s, ++t;
1406         if (*s == '\0' && endsym(*t))
1407                 return(t);
1408         else
1409                 return(NULL);
1410 }
1411
1412 /*
1413  * Look for the symbol in the symbol table. If it is found, we return
1414  * the symbol table index, else we return -1.
1415  */
1416 static int
1417 findsym(const char **strp)
1418 {
1419         const char *str;
1420         int symind;
1421
1422         str = *strp;
1423         *strp = skipsym(str);
1424         if (symlist) {
1425                 if (*strp == str)
1426                         return (-1);
1427                 if (symdepth && firstsym)
1428                         printf("%s%3d", zerosyms ? "" : "\n", depth);
1429                 firstsym = zerosyms = false;
1430                 printf("%s%.*s%s",
1431                        symdepth ? " " : "",
1432                        (int)(*strp-str), str,
1433                        symdepth ? "" : "\n");
1434                 /* we don't care about the value of the symbol */
1435                 return (0);
1436         }
1437         for (symind = 0; symind < nsyms; ++symind) {
1438                 if (matchsym(symname[symind], str) != NULL) {
1439                         debugsym("findsym", symind);
1440                         return (symind);
1441                 }
1442         }
1443         return (-1);
1444 }
1445
1446 /*
1447  * Resolve indirect symbol values to their final definitions.
1448  */
1449 static void
1450 indirectsym(void)
1451 {
1452         const char *cp;
1453         int changed, sym, ind;
1454
1455         do {
1456                 changed = 0;
1457                 for (sym = 0; sym < nsyms; ++sym) {
1458                         if (value[sym] == NULL)
1459                                 continue;
1460                         cp = value[sym];
1461                         ind = findsym(&cp);
1462                         if (ind == -1 || ind == sym ||
1463                             *cp != '\0' ||
1464                             value[ind] == NULL ||
1465                             value[ind] == value[sym])
1466                                 continue;
1467                         debugsym("indir...", sym);
1468                         value[sym] = value[ind];
1469                         debugsym("...ectsym", sym);
1470                         changed++;
1471                 }
1472         } while (changed);
1473 }
1474
1475 /*
1476  * Add a symbol to the symbol table, specified with the format sym=val
1477  */
1478 static void
1479 addsym1(bool ignorethis, bool definethis, char *symval)
1480 {
1481         const char *sym, *val;
1482
1483         sym = symval;
1484         val = skipsym(sym);
1485         if (definethis && *val == '=') {
1486                 symval[val - sym] = '\0';
1487                 val = val + 1;
1488         } else if (*val == '\0') {
1489                 val = definethis ? "1" : NULL;
1490         } else {
1491                 usage();
1492         }
1493         addsym2(ignorethis, sym, val);
1494 }
1495
1496 /*
1497  * Add a symbol to the symbol table.
1498  */
1499 static void
1500 addsym2(bool ignorethis, const char *sym, const char *val)
1501 {
1502         const char *cp = sym;
1503         int symind;
1504
1505         symind = findsym(&cp);
1506         if (symind < 0) {
1507                 if (nsyms >= MAXSYMS)
1508                         errx(2, "too many symbols");
1509                 symind = nsyms++;
1510         }
1511         ignore[symind] = ignorethis;
1512         symname[symind] = sym;
1513         value[symind] = val;
1514         debugsym("addsym", symind);
1515 }
1516
1517 static void
1518 debugsym(const char *why, int symind)
1519 {
1520         debug("%s %s%c%s", why, symname[symind],
1521             value[symind] ? '=' : ' ',
1522             value[symind] ? value[symind] : "undef");
1523 }
1524
1525 /*
1526  * Add symbols to the symbol table from a file containing
1527  * #define and #undef preprocessor directives.
1528  */
1529 static void
1530 defundefile(const char *fn)
1531 {
1532         filename = fn;
1533         input = fopen(fn, "rb");
1534         if (input == NULL)
1535                 err(2, "can't open %s", fn);
1536         linenum = 0;
1537         while (defundef())
1538                 ;
1539         if (ferror(input))
1540                 err(2, "can't read %s", filename);
1541         else
1542                 fclose(input);
1543         if (incomment)
1544                 error("EOF in comment");
1545 }
1546
1547 /*
1548  * Read and process one #define or #undef directive
1549  */
1550 static bool
1551 defundef(void)
1552 {
1553         const char *cp, *kw, *sym, *val, *end;
1554
1555         cp = skiphash();
1556         if (cp == NULL)
1557                 return (false);
1558         if (*cp == '\0')
1559                 goto done;
1560         /* strip trailing whitespace, and do a fairly rough check to
1561            avoid unsupported multi-line preprocessor directives */
1562         end = cp + strlen(cp);
1563         while (end > tline && strchr(" \t\n\r", end[-1]) != NULL)
1564                 --end;
1565         if (end > tline && end[-1] == '\\')
1566                 Eioccc();
1567
1568         kw = cp;
1569         if ((cp = matchsym("define", kw)) != NULL) {
1570                 sym = getsym(&cp);
1571                 if (sym == NULL)
1572                         error("Missing macro name in #define");
1573                 if (*cp == '(') {
1574                         val = "1";
1575                 } else {
1576                         cp = skipcomment(cp);
1577                         val = (cp < end) ? xstrdup(cp, end) : "";
1578                 }
1579                 debug("#define");
1580                 addsym2(false, sym, val);
1581         } else if ((cp = matchsym("undef", kw)) != NULL) {
1582                 sym = getsym(&cp);
1583                 if (sym == NULL)
1584                         error("Missing macro name in #undef");
1585                 cp = skipcomment(cp);
1586                 debug("#undef");
1587                 addsym2(false, sym, NULL);
1588         } else {
1589                 error("Unrecognized preprocessor directive");
1590         }
1591         skipline(cp);
1592 done:
1593         debug("parser line %d state %s comment %s line", linenum,
1594             comment_name[incomment], linestate_name[linestate]);
1595         return (true);
1596 }
1597
1598 /*
1599  * Concatenate two strings into new memory, checking for failure.
1600  */
1601 static char *
1602 astrcat(const char *s1, const char *s2)
1603 {
1604         char *s;
1605         int len;
1606         size_t size;
1607
1608         len = snprintf(NULL, 0, "%s%s", s1, s2);
1609         if (len < 0)
1610                 err(2, "snprintf");
1611         size = (size_t)len + 1;
1612         s = (char *)malloc(size);
1613         if (s == NULL)
1614                 err(2, "malloc");
1615         snprintf(s, size, "%s%s", s1, s2);
1616         return (s);
1617 }
1618
1619 /*
1620  * Duplicate a segment of a string, checking for failure.
1621  */
1622 static const char *
1623 xstrdup(const char *start, const char *end)
1624 {
1625         size_t n;
1626         char *s;
1627
1628         if (end < start) abort(); /* bug */
1629         n = (size_t)(end - start) + 1;
1630         s = malloc(n);
1631         if (s == NULL)
1632                 err(2, "malloc");
1633         snprintf(s, n, "%s", start);
1634         return (s);
1635 }
1636
1637 /*
1638  * Diagnostics.
1639  */
1640 static void
1641 debug(const char *msg, ...)
1642 {
1643         va_list ap;
1644
1645         if (debugging) {
1646                 va_start(ap, msg);
1647                 vwarnx(msg, ap);
1648                 va_end(ap);
1649         }
1650 }
1651
1652 static void
1653 error(const char *msg)
1654 {
1655         if (depth == 0)
1656                 warnx("%s: %d: %s", filename, linenum, msg);
1657         else
1658                 warnx("%s: %d: %s (#if line %d depth %d)",
1659                     filename, linenum, msg, stifline[depth], depth);
1660         closeio();
1661         errx(2, "Output may be truncated");
1662 }