]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/unifdef/unifdef.c
Remove extra ;
[FreeBSD/FreeBSD.git] / usr.bin / unifdef / unifdef.c
1 /*
2  * Copyright (c) 2002 - 2010 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 <sys/types.h>
47 #include <sys/stat.h>
48
49 #include <ctype.h>
50 #include <err.h>
51 #include <errno.h>
52 #include <stdarg.h>
53 #include <stdbool.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58
59 const char copyright[] =
60     "@(#) $Version: unifdef-2.3 $\n"
61     "@(#) $FreeBSD$\n"
62     "@(#) $Author: Tony Finch (dot@dotat.at) $\n"
63     "@(#) $URL: http://dotat.at/prog/unifdef $\n"
64 ;
65
66 /* types of input lines: */
67 typedef enum {
68         LT_TRUEI,               /* a true #if with ignore flag */
69         LT_FALSEI,              /* a false #if with ignore flag */
70         LT_IF,                  /* an unknown #if */
71         LT_TRUE,                /* a true #if */
72         LT_FALSE,               /* a false #if */
73         LT_ELIF,                /* an unknown #elif */
74         LT_ELTRUE,              /* a true #elif */
75         LT_ELFALSE,             /* a false #elif */
76         LT_ELSE,                /* #else */
77         LT_ENDIF,               /* #endif */
78         LT_DODGY,               /* flag: directive is not on one line */
79         LT_DODGY_LAST = LT_DODGY + LT_ENDIF,
80         LT_PLAIN,               /* ordinary line */
81         LT_EOF,                 /* end of file */
82         LT_ERROR,               /* unevaluable #if */
83         LT_COUNT
84 } Linetype;
85
86 static char const * const linetype_name[] = {
87         "TRUEI", "FALSEI", "IF", "TRUE", "FALSE",
88         "ELIF", "ELTRUE", "ELFALSE", "ELSE", "ENDIF",
89         "DODGY TRUEI", "DODGY FALSEI",
90         "DODGY IF", "DODGY TRUE", "DODGY FALSE",
91         "DODGY ELIF", "DODGY ELTRUE", "DODGY ELFALSE",
92         "DODGY ELSE", "DODGY ENDIF",
93         "PLAIN", "EOF", "ERROR"
94 };
95
96 /* state of #if processing */
97 typedef enum {
98         IS_OUTSIDE,
99         IS_FALSE_PREFIX,        /* false #if followed by false #elifs */
100         IS_TRUE_PREFIX,         /* first non-false #(el)if is true */
101         IS_PASS_MIDDLE,         /* first non-false #(el)if is unknown */
102         IS_FALSE_MIDDLE,        /* a false #elif after a pass state */
103         IS_TRUE_MIDDLE,         /* a true #elif after a pass state */
104         IS_PASS_ELSE,           /* an else after a pass state */
105         IS_FALSE_ELSE,          /* an else after a true state */
106         IS_TRUE_ELSE,           /* an else after only false states */
107         IS_FALSE_TRAILER,       /* #elifs after a true are false */
108         IS_COUNT
109 } Ifstate;
110
111 static char const * const ifstate_name[] = {
112         "OUTSIDE", "FALSE_PREFIX", "TRUE_PREFIX",
113         "PASS_MIDDLE", "FALSE_MIDDLE", "TRUE_MIDDLE",
114         "PASS_ELSE", "FALSE_ELSE", "TRUE_ELSE",
115         "FALSE_TRAILER"
116 };
117
118 /* state of comment parser */
119 typedef enum {
120         NO_COMMENT = false,     /* outside a comment */
121         C_COMMENT,              /* in a comment like this one */
122         CXX_COMMENT,            /* between // and end of line */
123         STARTING_COMMENT,       /* just after slash-backslash-newline */
124         FINISHING_COMMENT,      /* star-backslash-newline in a C comment */
125         CHAR_LITERAL,           /* inside '' */
126         STRING_LITERAL          /* inside "" */
127 } Comment_state;
128
129 static char const * const comment_name[] = {
130         "NO", "C", "CXX", "STARTING", "FINISHING", "CHAR", "STRING"
131 };
132
133 /* state of preprocessor line parser */
134 typedef enum {
135         LS_START,               /* only space and comments on this line */
136         LS_HASH,                /* only space, comments, and a hash */
137         LS_DIRTY                /* this line can't be a preprocessor line */
138 } Line_state;
139
140 static char const * const linestate_name[] = {
141         "START", "HASH", "DIRTY"
142 };
143
144 /*
145  * Minimum translation limits from ISO/IEC 9899:1999 5.2.4.1
146  */
147 #define MAXDEPTH        64                      /* maximum #if nesting */
148 #define MAXLINE         4096                    /* maximum length of line */
149 #define MAXSYMS         4096                    /* maximum number of symbols */
150
151 /*
152  * Sometimes when editing a keyword the replacement text is longer, so
153  * we leave some space at the end of the tline buffer to accommodate this.
154  */
155 #define EDITSLOP        10
156
157 /*
158  * For temporary filenames
159  */
160 #define TEMPLATE        "unifdef.XXXXXX"
161
162 /*
163  * Globals.
164  */
165
166 static bool             compblank;              /* -B: compress blank lines */
167 static bool             lnblank;                /* -b: blank deleted lines */
168 static bool             complement;             /* -c: do the complement */
169 static bool             debugging;              /* -d: debugging reports */
170 static bool             iocccok;                /* -e: fewer IOCCC errors */
171 static bool             strictlogic;            /* -K: keep ambiguous #ifs */
172 static bool             killconsts;             /* -k: eval constant #ifs */
173 static bool             lnnum;                  /* -n: add #line directives */
174 static bool             symlist;                /* -s: output symbol list */
175 static bool             symdepth;               /* -S: output symbol depth */
176 static bool             text;                   /* -t: this is a text file */
177
178 static const char      *symname[MAXSYMS];       /* symbol name */
179 static const char      *value[MAXSYMS];         /* -Dsym=value */
180 static bool             ignore[MAXSYMS];        /* -iDsym or -iUsym */
181 static int              nsyms;                  /* number of symbols */
182
183 static FILE            *input;                  /* input file pointer */
184 static const char      *filename;               /* input file name */
185 static int              linenum;                /* current line number */
186 static FILE            *output;                 /* output file pointer */
187 static const char      *ofilename;              /* output file name */
188 static bool             overwriting;            /* output overwrites input */
189 static char             tempname[FILENAME_MAX]; /* used when overwriting */
190
191 static char             tline[MAXLINE+EDITSLOP];/* input buffer plus space */
192 static char            *keyword;                /* used for editing #elif's */
193
194 static const char      *newline;                /* input file format */
195 static const char       newline_unix[] = "\n";
196 static const char       newline_crlf[] = "\r\n";
197
198 static Comment_state    incomment;              /* comment parser state */
199 static Line_state       linestate;              /* #if line parser state */
200 static Ifstate          ifstate[MAXDEPTH];      /* #if processor state */
201 static bool             ignoring[MAXDEPTH];     /* ignore comments state */
202 static int              stifline[MAXDEPTH];     /* start of current #if */
203 static int              depth;                  /* current #if nesting */
204 static int              delcount;               /* count of deleted lines */
205 static unsigned         blankcount;             /* count of blank lines */
206 static unsigned         blankmax;               /* maximum recent blankcount */
207 static bool             constexpr;              /* constant #if expression */
208 static bool             zerosyms = true;        /* to format symdepth output */
209 static bool             firstsym;               /* ditto */
210
211 static int              exitstat;               /* program exit status */
212
213 static void             addsym(bool, bool, char *);
214 static void             closeout(void);
215 static void             debug(const char *, ...);
216 static void             done(void);
217 static void             error(const char *);
218 static int              findsym(const char *);
219 static void             flushline(bool);
220 static Linetype         parseline(void);
221 static Linetype         ifeval(const char **);
222 static void             ignoreoff(void);
223 static void             ignoreon(void);
224 static void             keywordedit(const char *);
225 static void             nest(void);
226 static void             process(void);
227 static const char      *skipargs(const char *);
228 static const char      *skipcomment(const char *);
229 static const char      *skipsym(const char *);
230 static void             state(Ifstate);
231 static int              strlcmp(const char *, const char *, size_t);
232 static void             unnest(void);
233 static void             usage(void);
234 static void             version(void);
235
236 #define endsym(c) (!isalnum((unsigned char)c) && c != '_')
237
238 /*
239  * The main program.
240  */
241 int
242 main(int argc, char *argv[])
243 {
244         int opt;
245
246         while ((opt = getopt(argc, argv, "i:D:U:I:o:bBcdeKklnsStV")) != -1)
247                 switch (opt) {
248                 case 'i': /* treat stuff controlled by these symbols as text */
249                         /*
250                          * For strict backwards-compatibility the U or D
251                          * should be immediately after the -i but it doesn't
252                          * matter much if we relax that requirement.
253                          */
254                         opt = *optarg++;
255                         if (opt == 'D')
256                                 addsym(true, true, optarg);
257                         else if (opt == 'U')
258                                 addsym(true, false, optarg);
259                         else
260                                 usage();
261                         break;
262                 case 'D': /* define a symbol */
263                         addsym(false, true, optarg);
264                         break;
265                 case 'U': /* undef a symbol */
266                         addsym(false, false, optarg);
267                         break;
268                 case 'I': /* no-op for compatibility with cpp */
269                         break;
270                 case 'b': /* blank deleted lines instead of omitting them */
271                 case 'l': /* backwards compatibility */
272                         lnblank = true;
273                         break;
274                 case 'B': /* compress blank lines around removed section */
275                         compblank = true;
276                         break;
277                 case 'c': /* treat -D as -U and vice versa */
278                         complement = true;
279                         break;
280                 case 'd':
281                         debugging = true;
282                         break;
283                 case 'e': /* fewer errors from dodgy lines */
284                         iocccok = true;
285                         break;
286                 case 'K': /* keep ambiguous #ifs */
287                         strictlogic = true;
288                         break;
289                 case 'k': /* process constant #ifs */
290                         killconsts = true;
291                         break;
292                 case 'n': /* add #line directive after deleted lines */
293                         lnnum = true;
294                         break;
295                 case 'o': /* output to a file */
296                         ofilename = optarg;
297                         break;
298                 case 's': /* only output list of symbols that control #ifs */
299                         symlist = true;
300                         break;
301                 case 'S': /* list symbols with their nesting depth */
302                         symlist = symdepth = true;
303                         break;
304                 case 't': /* don't parse C comments */
305                         text = true;
306                         break;
307                 case 'V': /* print version */
308                         version();
309                 default:
310                         usage();
311                 }
312         argc -= optind;
313         argv += optind;
314         if (compblank && lnblank)
315                 errx(2, "-B and -b are mutually exclusive");
316         if (argc > 1) {
317                 errx(2, "can only do one file");
318         } else if (argc == 1 && strcmp(*argv, "-") != 0) {
319                 filename = *argv;
320                 input = fopen(filename, "rb");
321                 if (input == NULL)
322                         err(2, "can't open %s", filename);
323         } else {
324                 filename = "[stdin]";
325                 input = stdin;
326         }
327         if (ofilename == NULL) {
328                 ofilename = "[stdout]";
329                 output = stdout;
330         } else {
331                 struct stat ist, ost;
332                 memset(&ist, 0, sizeof(ist));
333                 memset(&ost, 0, sizeof(ost));
334
335                 if (fstat(fileno(input), &ist) != 0)
336                         err(2, "can't fstat %s", filename);
337                 if (stat(ofilename, &ost) != 0 && errno != ENOENT)
338                         warn("can't stat %s", ofilename);
339
340                 overwriting = (ist.st_dev == ost.st_dev
341                             && ist.st_ino == ost.st_ino);
342                 if (overwriting) {
343                         const char *dirsep;
344                         int ofd;
345
346                         dirsep = strrchr(ofilename, '/');
347                         if (dirsep != NULL)
348                                 snprintf(tempname, sizeof(tempname),
349                                     "%.*s/" TEMPLATE,
350                                     (int)(dirsep - ofilename), ofilename);
351                         else
352                                 snprintf(tempname, sizeof(tempname),
353                                     TEMPLATE);
354                         ofd = mkstemp(tempname);
355                         if (ofd != -1)
356                                 output = fdopen(ofd, "wb+");
357                         if (output == NULL)
358                                 err(2, "can't create temporary file");
359                         fchmod(ofd, ist.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO));
360                 } else {
361                         output = fopen(ofilename, "wb");
362                         if (output == NULL)
363                                 err(2, "can't open %s", ofilename);
364                 }
365         }
366         process();
367         abort(); /* bug */
368 }
369
370 static void
371 version(void)
372 {
373         const char *c = copyright;
374         for (;;) {
375                 while (*++c != '$')
376                         if (*c == '\0')
377                                 exit(0);
378                 while (*++c != '$')
379                         putc(*c, stderr);
380                 putc('\n', stderr);
381         }
382 }
383
384 static void
385 usage(void)
386 {
387         fprintf(stderr, "usage: unifdef [-bBcdeKknsStV] [-Ipath]"
388             " [-Dsym[=val]] [-Usym] [-iDsym[=val]] [-iUsym] ... [file]\n");
389         exit(2);
390 }
391
392 /*
393  * A state transition function alters the global #if processing state
394  * in a particular way. The table below is indexed by the current
395  * processing state and the type of the current line.
396  *
397  * Nesting is handled by keeping a stack of states; some transition
398  * functions increase or decrease the depth. They also maintain the
399  * ignore state on a stack. In some complicated cases they have to
400  * alter the preprocessor directive, as follows.
401  *
402  * When we have processed a group that starts off with a known-false
403  * #if/#elif sequence (which has therefore been deleted) followed by a
404  * #elif that we don't understand and therefore must keep, we edit the
405  * latter into a #if to keep the nesting correct.
406  *
407  * When we find a true #elif in a group, the following block will
408  * always be kept and the rest of the sequence after the next #elif or
409  * #else will be discarded. We edit the #elif into a #else and the
410  * following directive to #endif since this has the desired behaviour.
411  *
412  * "Dodgy" directives are split across multiple lines, the most common
413  * example being a multi-line comment hanging off the right of the
414  * directive. We can handle them correctly only if there is no change
415  * from printing to dropping (or vice versa) caused by that directive.
416  * If the directive is the first of a group we have a choice between
417  * failing with an error, or passing it through unchanged instead of
418  * evaluating it. The latter is not the default to avoid questions from
419  * users about unifdef unexpectedly leaving behind preprocessor directives.
420  */
421 typedef void state_fn(void);
422
423 /* report an error */
424 static void Eelif (void) { error("Inappropriate #elif"); }
425 static void Eelse (void) { error("Inappropriate #else"); }
426 static void Eendif(void) { error("Inappropriate #endif"); }
427 static void Eeof  (void) { error("Premature EOF"); }
428 static void Eioccc(void) { error("Obfuscated preprocessor control line"); }
429 /* plain line handling */
430 static void print (void) { flushline(true); }
431 static void drop  (void) { flushline(false); }
432 /* output lacks group's start line */
433 static void Strue (void) { drop();  ignoreoff(); state(IS_TRUE_PREFIX); }
434 static void Sfalse(void) { drop();  ignoreoff(); state(IS_FALSE_PREFIX); }
435 static void Selse (void) { drop();               state(IS_TRUE_ELSE); }
436 /* print/pass this block */
437 static void Pelif (void) { print(); ignoreoff(); state(IS_PASS_MIDDLE); }
438 static void Pelse (void) { print();              state(IS_PASS_ELSE); }
439 static void Pendif(void) { print(); unnest(); }
440 /* discard this block */
441 static void Dfalse(void) { drop();  ignoreoff(); state(IS_FALSE_TRAILER); }
442 static void Delif (void) { drop();  ignoreoff(); state(IS_FALSE_MIDDLE); }
443 static void Delse (void) { drop();               state(IS_FALSE_ELSE); }
444 static void Dendif(void) { drop();  unnest(); }
445 /* first line of group */
446 static void Fdrop (void) { nest();  Dfalse(); }
447 static void Fpass (void) { nest();  Pelif(); }
448 static void Ftrue (void) { nest();  Strue(); }
449 static void Ffalse(void) { nest();  Sfalse(); }
450 /* variable pedantry for obfuscated lines */
451 static void Oiffy (void) { if (!iocccok) Eioccc(); Fpass(); ignoreon(); }
452 static void Oif   (void) { if (!iocccok) Eioccc(); Fpass(); }
453 static void Oelif (void) { if (!iocccok) Eioccc(); Pelif(); }
454 /* ignore comments in this block */
455 static void Idrop (void) { Fdrop();  ignoreon(); }
456 static void Itrue (void) { Ftrue();  ignoreon(); }
457 static void Ifalse(void) { Ffalse(); ignoreon(); }
458 /* edit this line */
459 static void Mpass (void) { strncpy(keyword, "if  ", 4); Pelif(); }
460 static void Mtrue (void) { keywordedit("else");  state(IS_TRUE_MIDDLE); }
461 static void Melif (void) { keywordedit("endif"); state(IS_FALSE_TRAILER); }
462 static void Melse (void) { keywordedit("endif"); state(IS_FALSE_ELSE); }
463
464 static state_fn * const trans_table[IS_COUNT][LT_COUNT] = {
465 /* IS_OUTSIDE */
466 { Itrue, Ifalse,Fpass, Ftrue, Ffalse,Eelif, Eelif, Eelif, Eelse, Eendif,
467   Oiffy, Oiffy, Fpass, Oif,   Oif,   Eelif, Eelif, Eelif, Eelse, Eendif,
468   print, done,  abort },
469 /* IS_FALSE_PREFIX */
470 { Idrop, Idrop, Fdrop, Fdrop, Fdrop, Mpass, Strue, Sfalse,Selse, Dendif,
471   Idrop, Idrop, Fdrop, Fdrop, Fdrop, Mpass, Eioccc,Eioccc,Eioccc,Eioccc,
472   drop,  Eeof,  abort },
473 /* IS_TRUE_PREFIX */
474 { Itrue, Ifalse,Fpass, Ftrue, Ffalse,Dfalse,Dfalse,Dfalse,Delse, Dendif,
475   Oiffy, Oiffy, Fpass, Oif,   Oif,   Eioccc,Eioccc,Eioccc,Eioccc,Eioccc,
476   print, Eeof,  abort },
477 /* IS_PASS_MIDDLE */
478 { Itrue, Ifalse,Fpass, Ftrue, Ffalse,Pelif, Mtrue, Delif, Pelse, Pendif,
479   Oiffy, Oiffy, Fpass, Oif,   Oif,   Pelif, Oelif, Oelif, Pelse, Pendif,
480   print, Eeof,  abort },
481 /* IS_FALSE_MIDDLE */
482 { Idrop, Idrop, Fdrop, Fdrop, Fdrop, Pelif, Mtrue, Delif, Pelse, Pendif,
483   Idrop, Idrop, Fdrop, Fdrop, Fdrop, Eioccc,Eioccc,Eioccc,Eioccc,Eioccc,
484   drop,  Eeof,  abort },
485 /* IS_TRUE_MIDDLE */
486 { Itrue, Ifalse,Fpass, Ftrue, Ffalse,Melif, Melif, Melif, Melse, Pendif,
487   Oiffy, Oiffy, Fpass, Oif,   Oif,   Eioccc,Eioccc,Eioccc,Eioccc,Pendif,
488   print, Eeof,  abort },
489 /* IS_PASS_ELSE */
490 { Itrue, Ifalse,Fpass, Ftrue, Ffalse,Eelif, Eelif, Eelif, Eelse, Pendif,
491   Oiffy, Oiffy, Fpass, Oif,   Oif,   Eelif, Eelif, Eelif, Eelse, Pendif,
492   print, Eeof,  abort },
493 /* IS_FALSE_ELSE */
494 { Idrop, Idrop, Fdrop, Fdrop, Fdrop, Eelif, Eelif, Eelif, Eelse, Dendif,
495   Idrop, Idrop, Fdrop, Fdrop, Fdrop, Eelif, Eelif, Eelif, Eelse, Eioccc,
496   drop,  Eeof,  abort },
497 /* IS_TRUE_ELSE */
498 { Itrue, Ifalse,Fpass, Ftrue, Ffalse,Eelif, Eelif, Eelif, Eelse, Dendif,
499   Oiffy, Oiffy, Fpass, Oif,   Oif,   Eelif, Eelif, Eelif, Eelse, Eioccc,
500   print, Eeof,  abort },
501 /* IS_FALSE_TRAILER */
502 { Idrop, Idrop, Fdrop, Fdrop, Fdrop, Dfalse,Dfalse,Dfalse,Delse, Dendif,
503   Idrop, Idrop, Fdrop, Fdrop, Fdrop, Dfalse,Dfalse,Dfalse,Delse, Eioccc,
504   drop,  Eeof,  abort }
505 /*TRUEI  FALSEI IF     TRUE   FALSE  ELIF   ELTRUE ELFALSE ELSE  ENDIF
506   TRUEI  FALSEI IF     TRUE   FALSE  ELIF   ELTRUE ELFALSE ELSE  ENDIF (DODGY)
507   PLAIN  EOF    ERROR */
508 };
509
510 /*
511  * State machine utility functions
512  */
513 static void
514 ignoreoff(void)
515 {
516         if (depth == 0)
517                 abort(); /* bug */
518         ignoring[depth] = ignoring[depth-1];
519 }
520 static void
521 ignoreon(void)
522 {
523         ignoring[depth] = true;
524 }
525 static void
526 keywordedit(const char *replacement)
527 {
528         snprintf(keyword, tline + sizeof(tline) - keyword,
529             "%s%s", replacement, newline);
530         print();
531 }
532 static void
533 nest(void)
534 {
535         if (depth > MAXDEPTH-1)
536                 abort(); /* bug */
537         if (depth == MAXDEPTH-1)
538                 error("Too many levels of nesting");
539         depth += 1;
540         stifline[depth] = linenum;
541 }
542 static void
543 unnest(void)
544 {
545         if (depth == 0)
546                 abort(); /* bug */
547         depth -= 1;
548 }
549 static void
550 state(Ifstate is)
551 {
552         ifstate[depth] = is;
553 }
554
555 /*
556  * Write a line to the output or not, according to command line options.
557  */
558 static void
559 flushline(bool keep)
560 {
561         if (symlist)
562                 return;
563         if (keep ^ complement) {
564                 bool blankline = tline[strspn(tline, " \t\r\n")] == '\0';
565                 if (blankline && compblank && blankcount != blankmax) {
566                         delcount += 1;
567                         blankcount += 1;
568                 } else {
569                         if (lnnum && delcount > 0)
570                                 printf("#line %d%s", linenum, newline);
571                         fputs(tline, output);
572                         delcount = 0;
573                         blankmax = blankcount = blankline ? blankcount + 1 : 0;
574                 }
575         } else {
576                 if (lnblank)
577                         fputs(newline, output);
578                 exitstat = 1;
579                 delcount += 1;
580                 blankcount = 0;
581         }
582         if (debugging)
583                 fflush(output);
584 }
585
586 /*
587  * The driver for the state machine.
588  */
589 static void
590 process(void)
591 {
592         /* When compressing blank lines, act as if the file
593            is preceded by a large number of blank lines. */
594         blankmax = blankcount = 1000;
595         for (;;) {
596                 Linetype lineval = parseline();
597                 trans_table[ifstate[depth]][lineval]();
598                 debug("process line %d %s -> %s depth %d",
599                     linenum, linetype_name[lineval],
600                     ifstate_name[ifstate[depth]], depth);
601         }
602 }
603
604 /*
605  * Flush the output and handle errors.
606  */
607 static void
608 closeout(void)
609 {
610         if (symdepth && !zerosyms)
611                 printf("\n");
612         if (fclose(output) == EOF) {
613                 warn("couldn't write to %s", ofilename);
614                 if (overwriting) {
615                         unlink(tempname);
616                         errx(2, "%s unchanged", filename);
617                 } else {
618                         exit(2);
619                 }
620         }
621 }
622
623 /*
624  * Clean up and exit.
625  */
626 static void
627 done(void)
628 {
629         if (incomment)
630                 error("EOF in comment");
631         closeout();
632         if (overwriting && rename(tempname, filename) == -1) {
633                 warn("couldn't rename temporary file");
634                 unlink(tempname);
635                 errx(2, "%s unchanged", filename);
636         }
637         exit(exitstat);
638 }
639
640 /*
641  * Parse a line and determine its type. We keep the preprocessor line
642  * parser state between calls in the global variable linestate, with
643  * help from skipcomment().
644  */
645 static Linetype
646 parseline(void)
647 {
648         const char *cp;
649         int cursym;
650         int kwlen;
651         Linetype retval;
652         Comment_state wascomment;
653
654         linenum++;
655         if (fgets(tline, MAXLINE, input) == NULL)
656                 return (LT_EOF);
657         if (newline == NULL) {
658                 if (strrchr(tline, '\n') == strrchr(tline, '\r') + 1)
659                         newline = newline_crlf;
660                 else
661                         newline = newline_unix;
662         }
663         retval = LT_PLAIN;
664         wascomment = incomment;
665         cp = skipcomment(tline);
666         if (linestate == LS_START) {
667                 if (*cp == '#') {
668                         linestate = LS_HASH;
669                         firstsym = true;
670                         cp = skipcomment(cp + 1);
671                 } else if (*cp != '\0')
672                         linestate = LS_DIRTY;
673         }
674         if (!incomment && linestate == LS_HASH) {
675                 keyword = tline + (cp - tline);
676                 cp = skipsym(cp);
677                 kwlen = cp - keyword;
678                 /* no way can we deal with a continuation inside a keyword */
679                 if (strncmp(cp, "\\\r\n", 3) == 0 ||
680                     strncmp(cp, "\\\n", 2) == 0)
681                         Eioccc();
682                 if (strlcmp("ifdef", keyword, kwlen) == 0 ||
683                     strlcmp("ifndef", keyword, kwlen) == 0) {
684                         cp = skipcomment(cp);
685                         if ((cursym = findsym(cp)) < 0)
686                                 retval = LT_IF;
687                         else {
688                                 retval = (keyword[2] == 'n')
689                                     ? LT_FALSE : LT_TRUE;
690                                 if (value[cursym] == NULL)
691                                         retval = (retval == LT_TRUE)
692                                             ? LT_FALSE : LT_TRUE;
693                                 if (ignore[cursym])
694                                         retval = (retval == LT_TRUE)
695                                             ? LT_TRUEI : LT_FALSEI;
696                         }
697                         cp = skipsym(cp);
698                 } else if (strlcmp("if", keyword, kwlen) == 0)
699                         retval = ifeval(&cp);
700                 else if (strlcmp("elif", keyword, kwlen) == 0)
701                         retval = ifeval(&cp) - LT_IF + LT_ELIF;
702                 else if (strlcmp("else", keyword, kwlen) == 0)
703                         retval = LT_ELSE;
704                 else if (strlcmp("endif", keyword, kwlen) == 0)
705                         retval = LT_ENDIF;
706                 else {
707                         linestate = LS_DIRTY;
708                         retval = LT_PLAIN;
709                 }
710                 cp = skipcomment(cp);
711                 if (*cp != '\0') {
712                         linestate = LS_DIRTY;
713                         if (retval == LT_TRUE || retval == LT_FALSE ||
714                             retval == LT_TRUEI || retval == LT_FALSEI)
715                                 retval = LT_IF;
716                         if (retval == LT_ELTRUE || retval == LT_ELFALSE)
717                                 retval = LT_ELIF;
718                 }
719                 if (retval != LT_PLAIN && (wascomment || incomment)) {
720                         retval += LT_DODGY;
721                         if (incomment)
722                                 linestate = LS_DIRTY;
723                 }
724                 /* skipcomment normally changes the state, except
725                    if the last line of the file lacks a newline, or
726                    if there is too much whitespace in a directive */
727                 if (linestate == LS_HASH) {
728                         size_t len = cp - tline;
729                         if (fgets(tline + len, MAXLINE - len, input) == NULL) {
730                                 /* append the missing newline */
731                                 strcpy(tline + len, newline);
732                                 cp += strlen(newline);
733                                 linestate = LS_START;
734                         } else {
735                                 linestate = LS_DIRTY;
736                         }
737                 }
738         }
739         if (linestate == LS_DIRTY) {
740                 while (*cp != '\0')
741                         cp = skipcomment(cp + 1);
742         }
743         debug("parser line %d state %s comment %s line", linenum,
744             comment_name[incomment], linestate_name[linestate]);
745         return (retval);
746 }
747
748 /*
749  * These are the binary operators that are supported by the expression
750  * evaluator.
751  */
752 static Linetype op_strict(int *p, int v, Linetype at, Linetype bt) {
753         if(at == LT_IF || bt == LT_IF) return (LT_IF);
754         return (*p = v, v ? LT_TRUE : LT_FALSE);
755 }
756 static Linetype op_lt(int *p, Linetype at, int a, Linetype bt, int b) {
757         return op_strict(p, a < b, at, bt);
758 }
759 static Linetype op_gt(int *p, Linetype at, int a, Linetype bt, int b) {
760         return op_strict(p, a > b, at, bt);
761 }
762 static Linetype op_le(int *p, Linetype at, int a, Linetype bt, int b) {
763         return op_strict(p, a <= b, at, bt);
764 }
765 static Linetype op_ge(int *p, Linetype at, int a, Linetype bt, int b) {
766         return op_strict(p, a >= b, at, bt);
767 }
768 static Linetype op_eq(int *p, Linetype at, int a, Linetype bt, int b) {
769         return op_strict(p, a == b, at, bt);
770 }
771 static Linetype op_ne(int *p, Linetype at, int a, Linetype bt, int b) {
772         return op_strict(p, a != b, at, bt);
773 }
774 static Linetype op_or(int *p, Linetype at, int a, Linetype bt, int b) {
775         if (!strictlogic && (at == LT_TRUE || bt == LT_TRUE))
776                 return (*p = 1, LT_TRUE);
777         return op_strict(p, a || b, at, bt);
778 }
779 static Linetype op_and(int *p, Linetype at, int a, Linetype bt, int b) {
780         if (!strictlogic && (at == LT_FALSE || bt == LT_FALSE))
781                 return (*p = 0, LT_FALSE);
782         return op_strict(p, a && b, at, bt);
783 }
784
785 /*
786  * An evaluation function takes three arguments, as follows: (1) a pointer to
787  * an element of the precedence table which lists the operators at the current
788  * level of precedence; (2) a pointer to an integer which will receive the
789  * value of the expression; and (3) a pointer to a char* that points to the
790  * expression to be evaluated and that is updated to the end of the expression
791  * when evaluation is complete. The function returns LT_FALSE if the value of
792  * the expression is zero, LT_TRUE if it is non-zero, LT_IF if the expression
793  * depends on an unknown symbol, or LT_ERROR if there is a parse failure.
794  */
795 struct ops;
796
797 typedef Linetype eval_fn(const struct ops *, int *, const char **);
798
799 static eval_fn eval_table, eval_unary;
800
801 /*
802  * The precedence table. Expressions involving binary operators are evaluated
803  * in a table-driven way by eval_table. When it evaluates a subexpression it
804  * calls the inner function with its first argument pointing to the next
805  * element of the table. Innermost expressions have special non-table-driven
806  * handling.
807  */
808 static const struct ops {
809         eval_fn *inner;
810         struct op {
811                 const char *str;
812                 Linetype (*fn)(int *, Linetype, int, Linetype, int);
813         } op[5];
814 } eval_ops[] = {
815         { eval_table, { { "||", op_or } } },
816         { eval_table, { { "&&", op_and } } },
817         { eval_table, { { "==", op_eq },
818                         { "!=", op_ne } } },
819         { eval_unary, { { "<=", op_le },
820                         { ">=", op_ge },
821                         { "<", op_lt },
822                         { ">", op_gt } } }
823 };
824
825 /*
826  * Function for evaluating the innermost parts of expressions,
827  * viz. !expr (expr) number defined(symbol) symbol
828  * We reset the constexpr flag in the last two cases.
829  */
830 static Linetype
831 eval_unary(const struct ops *ops, int *valp, const char **cpp)
832 {
833         const char *cp;
834         char *ep;
835         int sym;
836         bool defparen;
837         Linetype lt;
838
839         cp = skipcomment(*cpp);
840         if (*cp == '!') {
841                 debug("eval%d !", ops - eval_ops);
842                 cp++;
843                 lt = eval_unary(ops, valp, &cp);
844                 if (lt == LT_ERROR)
845                         return (LT_ERROR);
846                 if (lt != LT_IF) {
847                         *valp = !*valp;
848                         lt = *valp ? LT_TRUE : LT_FALSE;
849                 }
850         } else if (*cp == '(') {
851                 cp++;
852                 debug("eval%d (", ops - eval_ops);
853                 lt = eval_table(eval_ops, valp, &cp);
854                 if (lt == LT_ERROR)
855                         return (LT_ERROR);
856                 cp = skipcomment(cp);
857                 if (*cp++ != ')')
858                         return (LT_ERROR);
859         } else if (isdigit((unsigned char)*cp)) {
860                 debug("eval%d number", ops - eval_ops);
861                 *valp = strtol(cp, &ep, 0);
862                 if (ep == cp)
863                         return (LT_ERROR);
864                 lt = *valp ? LT_TRUE : LT_FALSE;
865                 cp = skipsym(cp);
866         } else if (strncmp(cp, "defined", 7) == 0 && endsym(cp[7])) {
867                 cp = skipcomment(cp+7);
868                 debug("eval%d defined", ops - eval_ops);
869                 if (*cp == '(') {
870                         cp = skipcomment(cp+1);
871                         defparen = true;
872                 } else {
873                         defparen = false;
874                 }
875                 sym = findsym(cp);
876                 if (sym < 0) {
877                         lt = LT_IF;
878                 } else {
879                         *valp = (value[sym] != NULL);
880                         lt = *valp ? LT_TRUE : LT_FALSE;
881                 }
882                 cp = skipsym(cp);
883                 cp = skipcomment(cp);
884                 if (defparen && *cp++ != ')')
885                         return (LT_ERROR);
886                 constexpr = false;
887         } else if (!endsym(*cp)) {
888                 debug("eval%d symbol", ops - eval_ops);
889                 sym = findsym(cp);
890                 cp = skipsym(cp);
891                 if (sym < 0) {
892                         lt = LT_IF;
893                         cp = skipargs(cp);
894                 } else if (value[sym] == NULL) {
895                         *valp = 0;
896                         lt = LT_FALSE;
897                 } else {
898                         *valp = strtol(value[sym], &ep, 0);
899                         if (*ep != '\0' || ep == value[sym])
900                                 return (LT_ERROR);
901                         lt = *valp ? LT_TRUE : LT_FALSE;
902                         cp = skipargs(cp);
903                 }
904                 constexpr = false;
905         } else {
906                 debug("eval%d bad expr", ops - eval_ops);
907                 return (LT_ERROR);
908         }
909
910         *cpp = cp;
911         debug("eval%d = %d", ops - eval_ops, *valp);
912         return (lt);
913 }
914
915 /*
916  * Table-driven evaluation of binary operators.
917  */
918 static Linetype
919 eval_table(const struct ops *ops, int *valp, const char **cpp)
920 {
921         const struct op *op;
922         const char *cp;
923         int val;
924         Linetype lt, rt;
925
926         debug("eval%d", ops - eval_ops);
927         cp = *cpp;
928         lt = ops->inner(ops+1, valp, &cp);
929         if (lt == LT_ERROR)
930                 return (LT_ERROR);
931         for (;;) {
932                 cp = skipcomment(cp);
933                 for (op = ops->op; op->str != NULL; op++)
934                         if (strncmp(cp, op->str, strlen(op->str)) == 0)
935                                 break;
936                 if (op->str == NULL)
937                         break;
938                 cp += strlen(op->str);
939                 debug("eval%d %s", ops - eval_ops, op->str);
940                 rt = ops->inner(ops+1, &val, &cp);
941                 if (rt == LT_ERROR)
942                         return (LT_ERROR);
943                 lt = op->fn(valp, lt, *valp, rt, val);
944         }
945
946         *cpp = cp;
947         debug("eval%d = %d", ops - eval_ops, *valp);
948         debug("eval%d lt = %s", ops - eval_ops, linetype_name[lt]);
949         return (lt);
950 }
951
952 /*
953  * Evaluate the expression on a #if or #elif line. If we can work out
954  * the result we return LT_TRUE or LT_FALSE accordingly, otherwise we
955  * return just a generic LT_IF.
956  */
957 static Linetype
958 ifeval(const char **cpp)
959 {
960         int ret;
961         int val = 0;
962
963         debug("eval %s", *cpp);
964         constexpr = killconsts ? false : true;
965         ret = eval_table(eval_ops, &val, cpp);
966         debug("eval = %d", val);
967         return (constexpr ? LT_IF : ret == LT_ERROR ? LT_IF : ret);
968 }
969
970 /*
971  * Skip over comments, strings, and character literals and stop at the
972  * next character position that is not whitespace. Between calls we keep
973  * the comment state in the global variable incomment, and we also adjust
974  * the global variable linestate when we see a newline.
975  * XXX: doesn't cope with the buffer splitting inside a state transition.
976  */
977 static const char *
978 skipcomment(const char *cp)
979 {
980         if (text || ignoring[depth]) {
981                 for (; isspace((unsigned char)*cp); cp++)
982                         if (*cp == '\n')
983                                 linestate = LS_START;
984                 return (cp);
985         }
986         while (*cp != '\0')
987                 /* don't reset to LS_START after a line continuation */
988                 if (strncmp(cp, "\\\r\n", 3) == 0)
989                         cp += 3;
990                 else if (strncmp(cp, "\\\n", 2) == 0)
991                         cp += 2;
992                 else switch (incomment) {
993                 case NO_COMMENT:
994                         if (strncmp(cp, "/\\\r\n", 4) == 0) {
995                                 incomment = STARTING_COMMENT;
996                                 cp += 4;
997                         } else if (strncmp(cp, "/\\\n", 3) == 0) {
998                                 incomment = STARTING_COMMENT;
999                                 cp += 3;
1000                         } else if (strncmp(cp, "/*", 2) == 0) {
1001                                 incomment = C_COMMENT;
1002                                 cp += 2;
1003                         } else if (strncmp(cp, "//", 2) == 0) {
1004                                 incomment = CXX_COMMENT;
1005                                 cp += 2;
1006                         } else if (strncmp(cp, "\'", 1) == 0) {
1007                                 incomment = CHAR_LITERAL;
1008                                 linestate = LS_DIRTY;
1009                                 cp += 1;
1010                         } else if (strncmp(cp, "\"", 1) == 0) {
1011                                 incomment = STRING_LITERAL;
1012                                 linestate = LS_DIRTY;
1013                                 cp += 1;
1014                         } else if (strncmp(cp, "\n", 1) == 0) {
1015                                 linestate = LS_START;
1016                                 cp += 1;
1017                         } else if (strchr(" \r\t", *cp) != NULL) {
1018                                 cp += 1;
1019                         } else
1020                                 return (cp);
1021                         continue;
1022                 case CXX_COMMENT:
1023                         if (strncmp(cp, "\n", 1) == 0) {
1024                                 incomment = NO_COMMENT;
1025                                 linestate = LS_START;
1026                         }
1027                         cp += 1;
1028                         continue;
1029                 case CHAR_LITERAL:
1030                 case STRING_LITERAL:
1031                         if ((incomment == CHAR_LITERAL && cp[0] == '\'') ||
1032                             (incomment == STRING_LITERAL && cp[0] == '\"')) {
1033                                 incomment = NO_COMMENT;
1034                                 cp += 1;
1035                         } else if (cp[0] == '\\') {
1036                                 if (cp[1] == '\0')
1037                                         cp += 1;
1038                                 else
1039                                         cp += 2;
1040                         } else if (strncmp(cp, "\n", 1) == 0) {
1041                                 if (incomment == CHAR_LITERAL)
1042                                         error("unterminated char literal");
1043                                 else
1044                                         error("unterminated string literal");
1045                         } else
1046                                 cp += 1;
1047                         continue;
1048                 case C_COMMENT:
1049                         if (strncmp(cp, "*\\\r\n", 4) == 0) {
1050                                 incomment = FINISHING_COMMENT;
1051                                 cp += 4;
1052                         } else if (strncmp(cp, "*\\\n", 3) == 0) {
1053                                 incomment = FINISHING_COMMENT;
1054                                 cp += 3;
1055                         } else if (strncmp(cp, "*/", 2) == 0) {
1056                                 incomment = NO_COMMENT;
1057                                 cp += 2;
1058                         } else
1059                                 cp += 1;
1060                         continue;
1061                 case STARTING_COMMENT:
1062                         if (*cp == '*') {
1063                                 incomment = C_COMMENT;
1064                                 cp += 1;
1065                         } else if (*cp == '/') {
1066                                 incomment = CXX_COMMENT;
1067                                 cp += 1;
1068                         } else {
1069                                 incomment = NO_COMMENT;
1070                                 linestate = LS_DIRTY;
1071                         }
1072                         continue;
1073                 case FINISHING_COMMENT:
1074                         if (*cp == '/') {
1075                                 incomment = NO_COMMENT;
1076                                 cp += 1;
1077                         } else
1078                                 incomment = C_COMMENT;
1079                         continue;
1080                 default:
1081                         abort(); /* bug */
1082                 }
1083         return (cp);
1084 }
1085
1086 /*
1087  * Skip macro arguments.
1088  */
1089 static const char *
1090 skipargs(const char *cp)
1091 {
1092         const char *ocp = cp;
1093         int level = 0;
1094         cp = skipcomment(cp);
1095         if (*cp != '(')
1096                 return (cp);
1097         do {
1098                 if (*cp == '(')
1099                         level++;
1100                 if (*cp == ')')
1101                         level--;
1102                 cp = skipcomment(cp+1);
1103         } while (level != 0 && *cp != '\0');
1104         if (level == 0)
1105                 return (cp);
1106         else
1107         /* Rewind and re-detect the syntax error later. */
1108                 return (ocp);
1109 }
1110
1111 /*
1112  * Skip over an identifier.
1113  */
1114 static const char *
1115 skipsym(const char *cp)
1116 {
1117         while (!endsym(*cp))
1118                 ++cp;
1119         return (cp);
1120 }
1121
1122 /*
1123  * Look for the symbol in the symbol table. If it is found, we return
1124  * the symbol table index, else we return -1.
1125  */
1126 static int
1127 findsym(const char *str)
1128 {
1129         const char *cp;
1130         int symind;
1131
1132         cp = skipsym(str);
1133         if (cp == str)
1134                 return (-1);
1135         if (symlist) {
1136                 if (symdepth && firstsym)
1137                         printf("%s%3d", zerosyms ? "" : "\n", depth);
1138                 firstsym = zerosyms = false;
1139                 printf("%s%.*s%s",
1140                     symdepth ? " " : "",
1141                     (int)(cp-str), str,
1142                     symdepth ? "" : "\n");
1143                 /* we don't care about the value of the symbol */
1144                 return (0);
1145         }
1146         for (symind = 0; symind < nsyms; ++symind) {
1147                 if (strlcmp(symname[symind], str, cp-str) == 0) {
1148                         debug("findsym %s %s", symname[symind],
1149                             value[symind] ? value[symind] : "");
1150                         return (symind);
1151                 }
1152         }
1153         return (-1);
1154 }
1155
1156 /*
1157  * Add a symbol to the symbol table.
1158  */
1159 static void
1160 addsym(bool ignorethis, bool definethis, char *sym)
1161 {
1162         int symind;
1163         char *val;
1164
1165         symind = findsym(sym);
1166         if (symind < 0) {
1167                 if (nsyms >= MAXSYMS)
1168                         errx(2, "too many symbols");
1169                 symind = nsyms++;
1170         }
1171         symname[symind] = sym;
1172         ignore[symind] = ignorethis;
1173         val = sym + (skipsym(sym) - sym);
1174         if (definethis) {
1175                 if (*val == '=') {
1176                         value[symind] = val+1;
1177                         *val = '\0';
1178                 } else if (*val == '\0')
1179                         value[symind] = "1";
1180                 else
1181                         usage();
1182         } else {
1183                 if (*val != '\0')
1184                         usage();
1185                 value[symind] = NULL;
1186         }
1187         debug("addsym %s=%s", symname[symind],
1188             value[symind] ? value[symind] : "undef");
1189 }
1190
1191 /*
1192  * Compare s with n characters of t.
1193  * The same as strncmp() except that it checks that s[n] == '\0'.
1194  */
1195 static int
1196 strlcmp(const char *s, const char *t, size_t n)
1197 {
1198         while (n-- && *t != '\0')
1199                 if (*s != *t)
1200                         return ((unsigned char)*s - (unsigned char)*t);
1201                 else
1202                         ++s, ++t;
1203         return ((unsigned char)*s);
1204 }
1205
1206 /*
1207  * Diagnostics.
1208  */
1209 static void
1210 debug(const char *msg, ...)
1211 {
1212         va_list ap;
1213
1214         if (debugging) {
1215                 va_start(ap, msg);
1216                 vwarnx(msg, ap);
1217                 va_end(ap);
1218         }
1219 }
1220
1221 static void
1222 error(const char *msg)
1223 {
1224         if (depth == 0)
1225                 warnx("%s: %d: %s", filename, linenum, msg);
1226         else
1227                 warnx("%s: %d: %s (#if line %d depth %d)",
1228                     filename, linenum, msg, stifline[depth], depth);
1229         closeout();
1230         errx(2, "output may be truncated");
1231 }