2 * Copyright (c) 2002 - 2013 Tony Finch <dot@dotat.at>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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
27 * unifdef - remove ifdef'ed lines
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.
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
42 * These require better buffer handling, which would also make
43 * it possible to handle all "dodgy" directives correctly.
48 static const char copyright[] =
49 "@(#) $Version: unifdef-2.7 $\n"
51 "@(#) $Author: Tony Finch (dot@dotat.at) $\n"
52 "@(#) $URL: http://dotat.at/prog/unifdef $\n"
55 /* types of input lines: */
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 */
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 */
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"
85 #define linetype_if2elif(lt) ((Linetype)(lt - LT_IF + LT_ELIF))
86 #define linetype_2dodgy(lt) ((Linetype)(lt + LT_DODGY))
88 /* state of #if processing */
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 */
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",
110 /* state of comment parser */
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 "" */
121 static char const * const comment_name[] = {
122 "NO", "C", "CXX", "STARTING", "FINISHING", "CHAR", "STRING"
125 /* state of preprocessor line parser */
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 */
132 static char const * const linestate_name[] = {
133 "START", "HASH", "DIRTY"
137 * Minimum translation limits from ISO/IEC 9899:1999 5.2.4.1
139 #define MAXDEPTH 64 /* maximum #if nesting */
140 #define MAXLINE 4096 /* maximum length of line */
141 #define MAXSYMS 4096 /* maximum number of symbols */
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.
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 */
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 */
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 */
180 static char tline[MAXLINE+EDITSLOP];/* input buffer plus space */
181 static char *keyword; /* used for editing #elif's */
183 static const char *newline; /* input file format */
184 static const char newline_unix[] = "\n";
185 static const char newline_crlf[] = "\r\n";
187 static Comment_state incomment; /* comment parser state */
188 static Line_state linestate; /* #if line parser state */
189 static Ifstate ifstate[MAXDEPTH]; /* #if processor state */
190 static bool ignoring[MAXDEPTH]; /* ignore comments state */
191 static int stifline[MAXDEPTH]; /* start of current #if */
192 static int depth; /* current #if nesting */
193 static int delcount; /* count of deleted lines */
194 static unsigned blankcount; /* count of blank lines */
195 static unsigned blankmax; /* maximum recent blankcount */
196 static bool constexpr; /* constant #if expression */
197 static bool zerosyms; /* to format symdepth output */
198 static bool firstsym; /* ditto */
200 static int exitmode; /* exit status mode */
201 static int exitstat; /* program exit status */
203 static void addsym(bool, bool, char *);
204 static char *astrcat(const char *, const char *);
205 static void cleantemp(void);
206 static void closeout(void);
207 static void debug(const char *, ...);
208 static void done(void);
209 static void error(const char *);
210 static int findsym(const char *);
211 static void flushline(bool);
212 static void hashline(void);
213 static void help(void);
214 static Linetype ifeval(const char **);
215 static void ignoreoff(void);
216 static void ignoreon(void);
217 static void keywordedit(const char *);
218 static void nest(void);
219 static Linetype parseline(void);
220 static void process(void);
221 static void processinout(const char *, const char *);
222 static const char *skipargs(const char *);
223 static const char *skipcomment(const char *);
224 static const char *skipsym(const char *);
225 static void state(Ifstate);
226 static int strlcmp(const char *, const char *, size_t);
227 static void unnest(void);
228 static void usage(void);
229 static void version(void);
231 #define endsym(c) (!isalnum((unsigned char)c) && c != '_')
237 main(int argc, char *argv[])
241 while ((opt = getopt(argc, argv, "i:D:U:I:M:o:x:bBcdehKklmnsStV")) != -1)
243 case 'i': /* treat stuff controlled by these symbols as text */
245 * For strict backwards-compatibility the U or D
246 * should be immediately after the -i but it doesn't
247 * matter much if we relax that requirement.
251 addsym(true, true, optarg);
253 addsym(true, false, optarg);
257 case 'D': /* define a symbol */
258 addsym(false, true, optarg);
260 case 'U': /* undef a symbol */
261 addsym(false, false, optarg);
263 case 'I': /* no-op for compatibility with cpp */
265 case 'b': /* blank deleted lines instead of omitting them */
266 case 'l': /* backwards compatibility */
269 case 'B': /* compress blank lines around removed section */
272 case 'c': /* treat -D as -U and vice versa */
278 case 'e': /* fewer errors from dodgy lines */
284 case 'K': /* keep ambiguous #ifs */
287 case 'k': /* process constant #ifs */
290 case 'm': /* modify in place */
293 case 'M': /* modify in place and keep backup */
297 case 'n': /* add #line directive after deleted lines */
300 case 'o': /* output to a file */
303 case 's': /* only output list of symbols that control #ifs */
306 case 'S': /* list symbols with their nesting depth */
307 symlist = symdepth = true;
309 case 't': /* don't parse C comments */
316 exitmode = atoi(optarg);
317 if(exitmode < 0 || exitmode > 2)
325 if (compblank && lnblank)
326 errx(2, "-B and -b are mutually exclusive");
327 if (symlist && (ofilename != NULL || inplace || argc > 1))
328 errx(2, "-s only works with one input file");
329 if (argc > 1 && ofilename != NULL)
330 errx(2, "-o cannot be used with multiple input files");
331 if (argc > 1 && !inplace)
332 errx(2, "multiple input files require -m or -M");
335 if (argc == 1 && !inplace && ofilename == NULL)
339 if (ofilename != NULL)
340 processinout(*argv, ofilename);
341 else while (argc-- > 0) {
342 processinout(*argv, *argv);
346 case(0): exit(exitstat);
347 case(1): exit(!exitstat);
349 default: abort(); /* bug */
357 processinout(const char *ifn, const char *ofn)
361 if (ifn == NULL || strcmp(ifn, "-") == 0) {
362 filename = "[stdin]";
364 input = fbinmode(stdin);
368 input = fopen(ifn, "rb");
370 err(2, "can't open %s", ifn);
372 if (strcmp(ofn, "-") == 0) {
373 output = fbinmode(stdout);
377 if (stat(ofn, &st) < 0) {
378 output = fopen(ofn, "wb");
380 err(2, "can't create %s", ofn);
385 tempname = astrcat(ofn, ".XXXXXX");
386 output = mktempmode(tempname, st.st_mode);
388 err(2, "can't create %s", tempname);
392 if (backext != NULL) {
393 char *backname = astrcat(ofn, backext);
394 if (rename(ofn, backname) < 0)
395 err(2, "can't rename \"%s\" to \"%s%s\"", ofn, ofn, backext);
398 if (rename(tempname, ofn) < 0)
399 err(2, "can't rename \"%s\" to \"%s\"", tempname, ofn);
405 * For cleaning up if there is an error.
410 if (tempname != NULL)
415 * Self-identification functions.
421 const char *c = copyright;
436 "usage: unifdef [-bBcdehKkmnsStV] [-x{012}] [-Mext] [-opath] \\\n"
437 " [-[i]Dsym[=val]] [-[i]Usym] ... [file] ...\n");
452 " -Dsym=val define preprocessor symbol with given value\n"
453 " -Dsym define preprocessor symbol with value 1\n"
454 " -Usym preprocessor symbol is undefined\n"
455 " -iDsym=val \\ ignore C strings and comments\n"
456 " -iDsym ) in sections controlled by these\n"
457 " -iUsym / preprocessor symbols\n"
458 " -b blank lines instead of deleting them\n"
459 " -B compress blank lines around deleted section\n"
460 " -c complement (invert) keep vs. delete\n"
461 " -d debugging mode\n"
462 " -e ignore multiline preprocessor directives\n"
464 " -Ipath extra include file path (ignored)\n"
465 " -K disable && and || short-circuiting\n"
466 " -k process constant #if expressions\n"
467 " -Mext modify in place and keep backups\n"
468 " -m modify input files in place\n"
469 " -n add #line directives to output\n"
470 " -opath output file name\n"
471 " -S list #if control symbols with nesting\n"
472 " -s list #if control symbols\n"
473 " -t ignore C strings and comments\n"
474 " -V print version\n"
475 " -x{012} exit status mode\n"
481 * A state transition function alters the global #if processing state
482 * in a particular way. The table below is indexed by the current
483 * processing state and the type of the current line.
485 * Nesting is handled by keeping a stack of states; some transition
486 * functions increase or decrease the depth. They also maintain the
487 * ignore state on a stack. In some complicated cases they have to
488 * alter the preprocessor directive, as follows.
490 * When we have processed a group that starts off with a known-false
491 * #if/#elif sequence (which has therefore been deleted) followed by a
492 * #elif that we don't understand and therefore must keep, we edit the
493 * latter into a #if to keep the nesting correct. We use memcpy() to
494 * overwrite the 4 byte token "elif" with "if " without a '\0' byte.
496 * When we find a true #elif in a group, the following block will
497 * always be kept and the rest of the sequence after the next #elif or
498 * #else will be discarded. We edit the #elif into a #else and the
499 * following directive to #endif since this has the desired behaviour.
501 * "Dodgy" directives are split across multiple lines, the most common
502 * example being a multi-line comment hanging off the right of the
503 * directive. We can handle them correctly only if there is no change
504 * from printing to dropping (or vice versa) caused by that directive.
505 * If the directive is the first of a group we have a choice between
506 * failing with an error, or passing it through unchanged instead of
507 * evaluating it. The latter is not the default to avoid questions from
508 * users about unifdef unexpectedly leaving behind preprocessor directives.
510 typedef void state_fn(void);
512 /* report an error */
513 static void Eelif (void) { error("Inappropriate #elif"); }
514 static void Eelse (void) { error("Inappropriate #else"); }
515 static void Eendif(void) { error("Inappropriate #endif"); }
516 static void Eeof (void) { error("Premature EOF"); }
517 static void Eioccc(void) { error("Obfuscated preprocessor control line"); }
518 /* plain line handling */
519 static void print (void) { flushline(true); }
520 static void drop (void) { flushline(false); }
521 /* output lacks group's start line */
522 static void Strue (void) { drop(); ignoreoff(); state(IS_TRUE_PREFIX); }
523 static void Sfalse(void) { drop(); ignoreoff(); state(IS_FALSE_PREFIX); }
524 static void Selse (void) { drop(); state(IS_TRUE_ELSE); }
525 /* print/pass this block */
526 static void Pelif (void) { print(); ignoreoff(); state(IS_PASS_MIDDLE); }
527 static void Pelse (void) { print(); state(IS_PASS_ELSE); }
528 static void Pendif(void) { print(); unnest(); }
529 /* discard this block */
530 static void Dfalse(void) { drop(); ignoreoff(); state(IS_FALSE_TRAILER); }
531 static void Delif (void) { drop(); ignoreoff(); state(IS_FALSE_MIDDLE); }
532 static void Delse (void) { drop(); state(IS_FALSE_ELSE); }
533 static void Dendif(void) { drop(); unnest(); }
534 /* first line of group */
535 static void Fdrop (void) { nest(); Dfalse(); }
536 static void Fpass (void) { nest(); Pelif(); }
537 static void Ftrue (void) { nest(); Strue(); }
538 static void Ffalse(void) { nest(); Sfalse(); }
539 /* variable pedantry for obfuscated lines */
540 static void Oiffy (void) { if (!iocccok) Eioccc(); Fpass(); ignoreon(); }
541 static void Oif (void) { if (!iocccok) Eioccc(); Fpass(); }
542 static void Oelif (void) { if (!iocccok) Eioccc(); Pelif(); }
543 /* ignore comments in this block */
544 static void Idrop (void) { Fdrop(); ignoreon(); }
545 static void Itrue (void) { Ftrue(); ignoreon(); }
546 static void Ifalse(void) { Ffalse(); ignoreon(); }
547 /* modify this line */
548 static void Mpass (void) { memcpy(keyword, "if ", 4); Pelif(); }
549 static void Mtrue (void) { keywordedit("else"); state(IS_TRUE_MIDDLE); }
550 static void Melif (void) { keywordedit("endif"); state(IS_FALSE_TRAILER); }
551 static void Melse (void) { keywordedit("endif"); state(IS_FALSE_ELSE); }
553 static state_fn * const trans_table[IS_COUNT][LT_COUNT] = {
555 { Itrue, Ifalse,Fpass, Ftrue, Ffalse,Eelif, Eelif, Eelif, Eelse, Eendif,
556 Oiffy, Oiffy, Fpass, Oif, Oif, Eelif, Eelif, Eelif, Eelse, Eendif,
557 print, done, abort },
558 /* IS_FALSE_PREFIX */
559 { Idrop, Idrop, Fdrop, Fdrop, Fdrop, Mpass, Strue, Sfalse,Selse, Dendif,
560 Idrop, Idrop, Fdrop, Fdrop, Fdrop, Mpass, Eioccc,Eioccc,Eioccc,Eioccc,
563 { Itrue, Ifalse,Fpass, Ftrue, Ffalse,Dfalse,Dfalse,Dfalse,Delse, Dendif,
564 Oiffy, Oiffy, Fpass, Oif, Oif, Eioccc,Eioccc,Eioccc,Eioccc,Eioccc,
565 print, Eeof, abort },
567 { Itrue, Ifalse,Fpass, Ftrue, Ffalse,Pelif, Mtrue, Delif, Pelse, Pendif,
568 Oiffy, Oiffy, Fpass, Oif, Oif, Pelif, Oelif, Oelif, Pelse, Pendif,
569 print, Eeof, abort },
570 /* IS_FALSE_MIDDLE */
571 { Idrop, Idrop, Fdrop, Fdrop, Fdrop, Pelif, Mtrue, Delif, Pelse, Pendif,
572 Idrop, Idrop, Fdrop, Fdrop, Fdrop, Eioccc,Eioccc,Eioccc,Eioccc,Eioccc,
575 { Itrue, Ifalse,Fpass, Ftrue, Ffalse,Melif, Melif, Melif, Melse, Pendif,
576 Oiffy, Oiffy, Fpass, Oif, Oif, Eioccc,Eioccc,Eioccc,Eioccc,Pendif,
577 print, Eeof, abort },
579 { Itrue, Ifalse,Fpass, Ftrue, Ffalse,Eelif, Eelif, Eelif, Eelse, Pendif,
580 Oiffy, Oiffy, Fpass, Oif, Oif, Eelif, Eelif, Eelif, Eelse, Pendif,
581 print, Eeof, abort },
583 { Idrop, Idrop, Fdrop, Fdrop, Fdrop, Eelif, Eelif, Eelif, Eelse, Dendif,
584 Idrop, Idrop, Fdrop, Fdrop, Fdrop, Eelif, Eelif, Eelif, Eelse, Eioccc,
587 { Itrue, Ifalse,Fpass, Ftrue, Ffalse,Eelif, Eelif, Eelif, Eelse, Dendif,
588 Oiffy, Oiffy, Fpass, Oif, Oif, Eelif, Eelif, Eelif, Eelse, Eioccc,
589 print, Eeof, abort },
590 /* IS_FALSE_TRAILER */
591 { Idrop, Idrop, Fdrop, Fdrop, Fdrop, Dfalse,Dfalse,Dfalse,Delse, Dendif,
592 Idrop, Idrop, Fdrop, Fdrop, Fdrop, Dfalse,Dfalse,Dfalse,Delse, Eioccc,
594 /*TRUEI FALSEI IF TRUE FALSE ELIF ELTRUE ELFALSE ELSE ENDIF
595 TRUEI FALSEI IF TRUE FALSE ELIF ELTRUE ELFALSE ELSE ENDIF (DODGY)
600 * State machine utility functions
607 ignoring[depth] = ignoring[depth-1];
612 ignoring[depth] = true;
615 keywordedit(const char *replacement)
617 snprintf(keyword, tline + sizeof(tline) - keyword,
618 "%s%s", replacement, newline);
624 if (depth > MAXDEPTH-1)
626 if (depth == MAXDEPTH-1)
627 error("Too many levels of nesting");
629 stifline[depth] = linenum;
645 * The last state transition function. When this is called,
646 * lineval == LT_EOF, so the process() loop will terminate.
652 error("EOF in comment");
657 * Write a line to the output or not, according to command line options.
658 * If writing fails, closeout() will print the error and exit.
665 if (keep ^ complement) {
666 bool blankline = tline[strspn(tline, " \t\r\n")] == '\0';
667 if (blankline && compblank && blankcount != blankmax) {
671 if (lnnum && delcount > 0)
673 if (fputs(tline, output) == EOF)
676 blankmax = blankcount = blankline ? blankcount + 1 : 0;
679 if (lnblank && fputs(newline, output) == EOF)
685 if (debugging && fflush(output) == EOF)
690 * Format of #line directives depends on whether we know the input filename.
697 if (linefile == NULL)
698 e = fprintf(output, "#line %d%s", linenum, newline);
700 e = fprintf(output, "#line %d \"%s\"%s",
701 linenum, linefile, newline);
707 * Flush the output and handle errors.
712 /* Tidy up after findsym(). */
713 if (symdepth && !zerosyms)
715 if (ferror(output) || fclose(output) == EOF)
716 err(2, "%s: can't write to output", filename);
720 * The driver for the state machine.
725 Linetype lineval = LT_PLAIN;
726 /* When compressing blank lines, act as if the file
727 is preceded by a large number of blank lines. */
728 blankmax = blankcount = 1000;
730 while (lineval != LT_EOF) {
731 lineval = parseline();
732 trans_table[ifstate[depth]][lineval]();
733 debug("process line %d %s -> %s depth %d",
734 linenum, linetype_name[lineval],
735 ifstate_name[ifstate[depth]], depth);
740 * Parse a line and determine its type. We keep the preprocessor line
741 * parser state between calls in the global variable linestate, with
742 * help from skipcomment().
751 Comment_state wascomment;
754 if (fgets(tline, MAXLINE, input) == NULL) {
756 err(2, "can't read %s", filename);
760 if (newline == NULL) {
761 if (strrchr(tline, '\n') == strrchr(tline, '\r') + 1)
762 newline = newline_crlf;
764 newline = newline_unix;
767 wascomment = incomment;
768 cp = skipcomment(tline);
769 if (linestate == LS_START) {
773 cp = skipcomment(cp + 1);
774 } else if (*cp != '\0')
775 linestate = LS_DIRTY;
777 if (!incomment && linestate == LS_HASH) {
778 keyword = tline + (cp - tline);
780 kwlen = cp - keyword;
781 /* no way can we deal with a continuation inside a keyword */
782 if (strncmp(cp, "\\\r\n", 3) == 0 ||
783 strncmp(cp, "\\\n", 2) == 0)
785 if (strlcmp("ifdef", keyword, kwlen) == 0 ||
786 strlcmp("ifndef", keyword, kwlen) == 0) {
787 cp = skipcomment(cp);
788 if ((cursym = findsym(cp)) < 0)
791 retval = (keyword[2] == 'n')
792 ? LT_FALSE : LT_TRUE;
793 if (value[cursym] == NULL)
794 retval = (retval == LT_TRUE)
795 ? LT_FALSE : LT_TRUE;
797 retval = (retval == LT_TRUE)
798 ? LT_TRUEI : LT_FALSEI;
801 } else if (strlcmp("if", keyword, kwlen) == 0)
802 retval = ifeval(&cp);
803 else if (strlcmp("elif", keyword, kwlen) == 0)
804 retval = linetype_if2elif(ifeval(&cp));
805 else if (strlcmp("else", keyword, kwlen) == 0)
807 else if (strlcmp("endif", keyword, kwlen) == 0)
810 linestate = LS_DIRTY;
813 cp = skipcomment(cp);
815 linestate = LS_DIRTY;
816 if (retval == LT_TRUE || retval == LT_FALSE ||
817 retval == LT_TRUEI || retval == LT_FALSEI)
819 if (retval == LT_ELTRUE || retval == LT_ELFALSE)
822 if (retval != LT_PLAIN && (wascomment || incomment)) {
823 retval = linetype_2dodgy(retval);
825 linestate = LS_DIRTY;
827 /* skipcomment normally changes the state, except
828 if the last line of the file lacks a newline, or
829 if there is too much whitespace in a directive */
830 if (linestate == LS_HASH) {
831 size_t len = cp - tline;
832 if (fgets(tline + len, MAXLINE - len, input) == NULL) {
834 err(2, "can't read %s", filename);
835 /* append the missing newline at eof */
836 strcpy(tline + len, newline);
837 cp += strlen(newline);
838 linestate = LS_START;
840 linestate = LS_DIRTY;
844 if (linestate == LS_DIRTY) {
846 cp = skipcomment(cp + 1);
848 debug("parser line %d state %s comment %s line", linenum,
849 comment_name[incomment], linestate_name[linestate]);
854 * These are the binary operators that are supported by the expression
857 static Linetype op_strict(int *p, int v, Linetype at, Linetype bt) {
858 if(at == LT_IF || bt == LT_IF) return (LT_IF);
859 return (*p = v, v ? LT_TRUE : LT_FALSE);
861 static Linetype op_lt(int *p, Linetype at, int a, Linetype bt, int b) {
862 return op_strict(p, a < b, at, bt);
864 static Linetype op_gt(int *p, Linetype at, int a, Linetype bt, int b) {
865 return op_strict(p, a > b, at, bt);
867 static Linetype op_le(int *p, Linetype at, int a, Linetype bt, int b) {
868 return op_strict(p, a <= b, at, bt);
870 static Linetype op_ge(int *p, Linetype at, int a, Linetype bt, int b) {
871 return op_strict(p, a >= b, at, bt);
873 static Linetype op_eq(int *p, Linetype at, int a, Linetype bt, int b) {
874 return op_strict(p, a == b, at, bt);
876 static Linetype op_ne(int *p, Linetype at, int a, Linetype bt, int b) {
877 return op_strict(p, a != b, at, bt);
879 static Linetype op_or(int *p, Linetype at, int a, Linetype bt, int b) {
880 if (!strictlogic && (at == LT_TRUE || bt == LT_TRUE))
881 return (*p = 1, LT_TRUE);
882 return op_strict(p, a || b, at, bt);
884 static Linetype op_and(int *p, Linetype at, int a, Linetype bt, int b) {
885 if (!strictlogic && (at == LT_FALSE || bt == LT_FALSE))
886 return (*p = 0, LT_FALSE);
887 return op_strict(p, a && b, at, bt);
891 * An evaluation function takes three arguments, as follows: (1) a pointer to
892 * an element of the precedence table which lists the operators at the current
893 * level of precedence; (2) a pointer to an integer which will receive the
894 * value of the expression; and (3) a pointer to a char* that points to the
895 * expression to be evaluated and that is updated to the end of the expression
896 * when evaluation is complete. The function returns LT_FALSE if the value of
897 * the expression is zero, LT_TRUE if it is non-zero, LT_IF if the expression
898 * depends on an unknown symbol, or LT_ERROR if there is a parse failure.
902 typedef Linetype eval_fn(const struct ops *, int *, const char **);
904 static eval_fn eval_table, eval_unary;
907 * The precedence table. Expressions involving binary operators are evaluated
908 * in a table-driven way by eval_table. When it evaluates a subexpression it
909 * calls the inner function with its first argument pointing to the next
910 * element of the table. Innermost expressions have special non-table-driven
915 Linetype (*fn)(int *, Linetype, int, Linetype, int);
921 static const struct ops eval_ops[] = {
922 { eval_table, { { "||", op_or } } },
923 { eval_table, { { "&&", op_and } } },
924 { eval_table, { { "==", op_eq },
926 { eval_unary, { { "<=", op_le },
932 /* Current operator precedence level */
933 static int prec(const struct ops *ops)
935 return (ops - eval_ops);
939 * Function for evaluating the innermost parts of expressions,
940 * viz. !expr (expr) number defined(symbol) symbol
941 * We reset the constexpr flag in the last two cases.
944 eval_unary(const struct ops *ops, int *valp, const char **cpp)
952 cp = skipcomment(*cpp);
954 debug("eval%d !", prec(ops));
956 lt = eval_unary(ops, valp, &cp);
961 lt = *valp ? LT_TRUE : LT_FALSE;
963 } else if (*cp == '(') {
965 debug("eval%d (", prec(ops));
966 lt = eval_table(eval_ops, valp, &cp);
969 cp = skipcomment(cp);
972 } else if (isdigit((unsigned char)*cp)) {
973 debug("eval%d number", prec(ops));
974 *valp = strtol(cp, &ep, 0);
977 lt = *valp ? LT_TRUE : LT_FALSE;
979 } else if (strncmp(cp, "defined", 7) == 0 && endsym(cp[7])) {
980 cp = skipcomment(cp+7);
981 debug("eval%d defined", prec(ops));
983 cp = skipcomment(cp+1);
992 *valp = (value[sym] != NULL);
993 lt = *valp ? LT_TRUE : LT_FALSE;
996 cp = skipcomment(cp);
997 if (defparen && *cp++ != ')')
1000 } else if (!endsym(*cp)) {
1001 debug("eval%d symbol", prec(ops));
1007 } else if (value[sym] == NULL) {
1011 *valp = strtol(value[sym], &ep, 0);
1012 if (*ep != '\0' || ep == value[sym])
1014 lt = *valp ? LT_TRUE : LT_FALSE;
1019 debug("eval%d bad expr", prec(ops));
1024 debug("eval%d = %d", prec(ops), *valp);
1029 * Table-driven evaluation of binary operators.
1032 eval_table(const struct ops *ops, int *valp, const char **cpp)
1034 const struct op *op;
1039 debug("eval%d", prec(ops));
1041 lt = ops->inner(ops+1, valp, &cp);
1045 cp = skipcomment(cp);
1046 for (op = ops->op; op->str != NULL; op++)
1047 if (strncmp(cp, op->str, strlen(op->str)) == 0)
1049 if (op->str == NULL)
1051 cp += strlen(op->str);
1052 debug("eval%d %s", prec(ops), op->str);
1053 rt = ops->inner(ops+1, &val, &cp);
1056 lt = op->fn(valp, lt, *valp, rt, val);
1060 debug("eval%d = %d", prec(ops), *valp);
1061 debug("eval%d lt = %s", prec(ops), linetype_name[lt]);
1066 * Evaluate the expression on a #if or #elif line. If we can work out
1067 * the result we return LT_TRUE or LT_FALSE accordingly, otherwise we
1068 * return just a generic LT_IF.
1071 ifeval(const char **cpp)
1076 debug("eval %s", *cpp);
1077 constexpr = killconsts ? false : true;
1078 ret = eval_table(eval_ops, &val, cpp);
1079 debug("eval = %d", val);
1080 return (constexpr ? LT_IF : ret == LT_ERROR ? LT_IF : ret);
1084 * Skip over comments, strings, and character literals and stop at the
1085 * next character position that is not whitespace. Between calls we keep
1086 * the comment state in the global variable incomment, and we also adjust
1087 * the global variable linestate when we see a newline.
1088 * XXX: doesn't cope with the buffer splitting inside a state transition.
1091 skipcomment(const char *cp)
1093 if (text || ignoring[depth]) {
1094 for (; isspace((unsigned char)*cp); cp++)
1096 linestate = LS_START;
1100 /* don't reset to LS_START after a line continuation */
1101 if (strncmp(cp, "\\\r\n", 3) == 0)
1103 else if (strncmp(cp, "\\\n", 2) == 0)
1105 else switch (incomment) {
1107 if (strncmp(cp, "/\\\r\n", 4) == 0) {
1108 incomment = STARTING_COMMENT;
1110 } else if (strncmp(cp, "/\\\n", 3) == 0) {
1111 incomment = STARTING_COMMENT;
1113 } else if (strncmp(cp, "/*", 2) == 0) {
1114 incomment = C_COMMENT;
1116 } else if (strncmp(cp, "//", 2) == 0) {
1117 incomment = CXX_COMMENT;
1119 } else if (strncmp(cp, "\'", 1) == 0) {
1120 incomment = CHAR_LITERAL;
1121 linestate = LS_DIRTY;
1123 } else if (strncmp(cp, "\"", 1) == 0) {
1124 incomment = STRING_LITERAL;
1125 linestate = LS_DIRTY;
1127 } else if (strncmp(cp, "\n", 1) == 0) {
1128 linestate = LS_START;
1130 } else if (strchr(" \r\t", *cp) != NULL) {
1136 if (strncmp(cp, "\n", 1) == 0) {
1137 incomment = NO_COMMENT;
1138 linestate = LS_START;
1143 case STRING_LITERAL:
1144 if ((incomment == CHAR_LITERAL && cp[0] == '\'') ||
1145 (incomment == STRING_LITERAL && cp[0] == '\"')) {
1146 incomment = NO_COMMENT;
1148 } else if (cp[0] == '\\') {
1153 } else if (strncmp(cp, "\n", 1) == 0) {
1154 if (incomment == CHAR_LITERAL)
1155 error("unterminated char literal");
1157 error("unterminated string literal");
1162 if (strncmp(cp, "*\\\r\n", 4) == 0) {
1163 incomment = FINISHING_COMMENT;
1165 } else if (strncmp(cp, "*\\\n", 3) == 0) {
1166 incomment = FINISHING_COMMENT;
1168 } else if (strncmp(cp, "*/", 2) == 0) {
1169 incomment = NO_COMMENT;
1174 case STARTING_COMMENT:
1176 incomment = C_COMMENT;
1178 } else if (*cp == '/') {
1179 incomment = CXX_COMMENT;
1182 incomment = NO_COMMENT;
1183 linestate = LS_DIRTY;
1186 case FINISHING_COMMENT:
1188 incomment = NO_COMMENT;
1191 incomment = C_COMMENT;
1200 * Skip macro arguments.
1203 skipargs(const char *cp)
1205 const char *ocp = cp;
1207 cp = skipcomment(cp);
1215 cp = skipcomment(cp+1);
1216 } while (level != 0 && *cp != '\0');
1220 /* Rewind and re-detect the syntax error later. */
1225 * Skip over an identifier.
1228 skipsym(const char *cp)
1230 while (!endsym(*cp))
1236 * Look for the symbol in the symbol table. If it is found, we return
1237 * the symbol table index, else we return -1.
1240 findsym(const char *str)
1249 if (symdepth && firstsym)
1250 printf("%s%3d", zerosyms ? "" : "\n", depth);
1251 firstsym = zerosyms = false;
1253 symdepth ? " " : "",
1255 symdepth ? "" : "\n");
1256 /* we don't care about the value of the symbol */
1259 for (symind = 0; symind < nsyms; ++symind) {
1260 if (strlcmp(symname[symind], str, cp-str) == 0) {
1261 debug("findsym %s %s", symname[symind],
1262 value[symind] ? value[symind] : "");
1270 * Add a symbol to the symbol table.
1273 addsym(bool ignorethis, bool definethis, char *sym)
1278 symind = findsym(sym);
1280 if (nsyms >= MAXSYMS)
1281 errx(2, "too many symbols");
1284 symname[symind] = sym;
1285 ignore[symind] = ignorethis;
1286 val = sym + (skipsym(sym) - sym);
1289 value[symind] = val+1;
1291 } else if (*val == '\0')
1292 value[symind] = "1";
1298 value[symind] = NULL;
1300 debug("addsym %s=%s", symname[symind],
1301 value[symind] ? value[symind] : "undef");
1305 * Compare s with n characters of t.
1306 * The same as strncmp() except that it checks that s[n] == '\0'.
1309 strlcmp(const char *s, const char *t, size_t n)
1311 while (n-- && *t != '\0')
1313 return ((unsigned char)*s - (unsigned char)*t);
1316 return ((unsigned char)*s);
1320 * Concatenate two strings into new memory, checking for failure.
1323 astrcat(const char *s1, const char *s2)
1328 len = 1 + snprintf(NULL, 0, "%s%s", s1, s2);
1329 s = (char *)malloc(len);
1332 snprintf(s, len, "%s%s", s1, s2);
1340 debug(const char *msg, ...)
1352 error(const char *msg)
1355 warnx("%s: %d: %s", filename, linenum, msg);
1357 warnx("%s: %d: %s (#if line %d depth %d)",
1358 filename, linenum, msg, stifline[depth], depth);
1360 errx(2, "output may be truncated");