2 * Copyright (c) 2002 - 2015 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.11 $\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 16384 /* 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 */
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.
192 static const char *newline
; /* input file format */
193 static const char newline_unix
[] = "\n";
194 static const char newline_crlf
[] = "\r\n";
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 */
209 static int exitmode
; /* exit status mode */
210 static int exitstat
; /* program exit status */
211 static bool altered
; /* was this file modified? */
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 *);
249 #define endsym(c) (!isalnum((unsigned char)c) && c != '_')
255 main(int argc
, char *argv
[])
259 while ((opt
= getopt(argc
, argv
, "i:D:U:f:I:M:o:x:bBcdehKklmnsStV")) != -1)
261 case 'i': /* treat stuff controlled by these symbols as text */
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.
269 addsym1(true, true, optarg
);
271 addsym1(true, false, optarg
);
275 case 'D': /* define a symbol */
276 addsym1(false, true, optarg
);
278 case 'U': /* undef a symbol */
279 addsym1(false, false, optarg
);
281 case 'I': /* no-op for compatibility with cpp */
283 case 'b': /* blank deleted lines instead of omitting them */
284 case 'l': /* backwards compatibility */
287 case 'B': /* compress blank lines around removed section */
290 case 'c': /* treat -D as -U and vice versa */
296 case 'e': /* fewer errors from dodgy lines */
299 case 'f': /* definitions file */
305 case 'K': /* keep ambiguous #ifs */
308 case 'k': /* process constant #ifs */
311 case 'm': /* modify in place */
314 case 'M': /* modify in place and keep backup */
316 if (strlen(optarg
) > 0)
319 case 'n': /* add #line directive after deleted lines */
322 case 'o': /* output to a file */
325 case 's': /* only output list of symbols that control #ifs */
328 case 'S': /* list symbols with their nesting depth */
329 symlist
= symdepth
= true;
331 case 't': /* don't parse C comments */
338 exitmode
= atoi(optarg
);
339 if(exitmode
< 0 || exitmode
> 2)
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");
359 if (argc
== 1 && !inplace
&& ofilename
== NULL
)
364 if (ofilename
!= NULL
)
365 processinout(*argv
, ofilename
);
366 else while (argc
-- > 0) {
367 processinout(*argv
, *argv
);
371 case(0): exit(exitstat
);
372 case(1): exit(!exitstat
);
374 default: abort(); /* bug */
382 processinout(const char *ifn
, const char *ofn
)
386 if (ifn
== NULL
|| strcmp(ifn
, "-") == 0) {
387 filename
= "[stdin]";
389 input
= fbinmode(stdin
);
393 input
= fopen(ifn
, "rb");
395 err(2, "can't open %s", ifn
);
397 if (strcmp(ofn
, "-") == 0) {
398 output
= fbinmode(stdout
);
402 if (stat(ofn
, &st
) < 0) {
403 output
= fopen(ofn
, "wb");
405 err(2, "can't create %s", ofn
);
410 tempname
= astrcat(ofn
, ".XXXXXX");
411 output
= mktempmode(tempname
, st
.st_mode
);
413 err(2, "can't create %s", tempname
);
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
);
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
);
434 * For cleaning up if there is an error.
439 if (tempname
!= NULL
)
444 * Self-identification functions.
450 const char *c
= copyright
;
465 "usage: unifdef [-bBcdehKkmnsStV] [-x{012}] [-Mext] [-opath] \\\n"
466 " [-[i]Dsym[=val]] [-[i]Usym] [-fpath] ... [file] ...\n");
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"
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"
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.
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.
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.
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.
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.
540 typedef void state_fn(void);
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
); }
583 static state_fn
* const trans_table
[IS_COUNT
][LT_COUNT
] = {
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
,
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
},
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
,
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
},
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
},
613 { Idrop
, Idrop
, Fdrop
, Fdrop
, Fdrop
, Eelif
, Eelif
, Eelif
, Eelse
, Dendif
,
614 Idrop
, Idrop
, Fdrop
, Fdrop
, Fdrop
, Eelif
, Eelif
, Eelif
, Eelse
, Eioccc
,
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
,
624 /*TRUEI FALSEI IF TRUE FALSE ELIF ELTRUE ELFALSE ELSE ENDIF
625 TRUEI FALSEI IF TRUE FALSE ELIF ELTRUE ELFALSE ELSE ENDIF (DODGY)
630 * State machine utility functions
637 ignoring
[depth
] = ignoring
[depth
-1];
642 ignoring
[depth
] = true;
645 keywordedit(const char *replacement
)
647 snprintf(keyword
, tline
+ sizeof(tline
) - keyword
,
648 "%s%s", replacement
, newline
);
655 if (depth
> MAXDEPTH
-1)
657 if (depth
== MAXDEPTH
-1)
658 error("Too many levels of nesting");
660 stifline
[depth
] = linenum
;
676 * The last state transition function. When this is called,
677 * lineval == LT_EOF, so the process() loop will terminate.
683 error("EOF in comment");
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.
696 if (keep
^ complement
) {
697 bool blankline
= tline
[strspn(tline
, " \t\r\n")] == '\0';
698 if (blankline
&& compblank
&& blankcount
!= blankmax
) {
702 if (lnnum
&& delcount
> 0)
704 if (fputs(tline
, output
) == EOF
)
707 blankmax
= blankcount
= blankline
? blankcount
+ 1 : 0;
710 if (lnblank
&& fputs(newline
, output
) == EOF
)
716 if (debugging
&& fflush(output
) == EOF
)
721 * Format of #line directives depends on whether we know the input filename.
728 if (linefile
== NULL
)
729 e
= fprintf(output
, "#line %d%s", linenum
, newline
);
731 e
= fprintf(output
, "#line %d \"%s\"%s",
732 linenum
, linefile
, newline
);
738 * Flush the output and handle errors.
743 /* Tidy up after findsym(). */
744 if (symdepth
&& !zerosyms
)
746 if (output
!= NULL
&& (ferror(output
) || fclose(output
) == EOF
))
747 err(2, "%s: can't write to output", filename
);
752 * The driver for the state machine.
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;
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
);
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().
786 Comment_state wascomment
;
788 wascomment
= incomment
;
792 if (newline
== NULL
) {
793 if (strrchr(tline
, '\n') == strrchr(tline
, '\r') + 1)
794 newline
= newline_crlf
;
796 newline
= newline_unix
;
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)
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
;
815 retval
= (retval
== LT_TRUE
)
816 ? LT_TRUEI
: LT_FALSEI
;
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
)
824 else if ((cp
= matchsym("endif", keyword
)) != NULL
)
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)
836 cp
= skipcomment(cp
);
839 if (retval
== LT_TRUE
|| retval
== LT_FALSE
||
840 retval
== LT_TRUEI
|| retval
== LT_FALSEI
)
842 if (retval
== LT_ELTRUE
|| retval
== LT_ELFALSE
)
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
) {
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
;
857 linestate
= LS_DIRTY
;
860 if (retval
!= LT_PLAIN
&& (wascomment
|| linestate
!= LS_START
)) {
861 retval
= linetype_2dodgy(retval
);
862 linestate
= LS_DIRTY
;
865 debug("parser line %d state %s comment %s line", linenum
,
866 comment_name
[incomment
], linestate_name
[linestate
]);
871 * These are the binary operators that are supported by the expression
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
);
878 static Linetype
op_lt(long *p
, Linetype at
, long a
, Linetype bt
, long b
) {
879 return op_strict(p
, a
< b
, at
, bt
);
881 static Linetype
op_gt(long *p
, Linetype at
, long a
, Linetype bt
, long b
) {
882 return op_strict(p
, a
> b
, at
, bt
);
884 static Linetype
op_le(long *p
, Linetype at
, long a
, Linetype bt
, long b
) {
885 return op_strict(p
, a
<= b
, at
, bt
);
887 static Linetype
op_ge(long *p
, Linetype at
, long a
, Linetype bt
, long b
) {
888 return op_strict(p
, a
>= b
, at
, bt
);
890 static Linetype
op_eq(long *p
, Linetype at
, long a
, Linetype bt
, long b
) {
891 return op_strict(p
, a
== b
, at
, bt
);
893 static Linetype
op_ne(long *p
, Linetype at
, long a
, Linetype bt
, long b
) {
894 return op_strict(p
, a
!= b
, at
, bt
);
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
);
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
);
906 static Linetype
op_blsh(long *p
, Linetype at
, long a
, Linetype bt
, long b
) {
907 return op_strict(p
, a
<< b
, at
, bt
);
909 static Linetype
op_brsh(long *p
, Linetype at
, long a
, Linetype bt
, long b
) {
910 return op_strict(p
, a
>> b
, at
, bt
);
912 static Linetype
op_add(long *p
, Linetype at
, long a
, Linetype bt
, long b
) {
913 return op_strict(p
, a
+ b
, at
, bt
);
915 static Linetype
op_sub(long *p
, Linetype at
, long a
, Linetype bt
, long b
) {
916 return op_strict(p
, a
- b
, at
, bt
);
918 static Linetype
op_mul(long *p
, Linetype at
, long a
, Linetype bt
, long b
) {
919 return op_strict(p
, a
* b
, at
, bt
);
921 static Linetype
op_div(long *p
, Linetype at
, long a
, Linetype bt
, long b
) {
923 debug("eval division by zero");
926 return op_strict(p
, a
/ b
, at
, bt
);
928 static Linetype
op_mod(long *p
, Linetype at
, long a
, Linetype bt
, long b
) {
929 return op_strict(p
, a
% b
, at
, bt
);
931 static Linetype
op_bor(long *p
, Linetype at
, long a
, Linetype bt
, long b
) {
932 return op_strict(p
, a
| b
, at
, bt
);
934 static Linetype
op_bxor(long *p
, Linetype at
, long a
, Linetype bt
, long b
) {
935 return op_strict(p
, a
^ b
, at
, bt
);
937 static Linetype
op_band(long *p
, Linetype at
, long a
, Linetype bt
, long b
) {
938 return op_strict(p
, a
& b
, at
, bt
);
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.
953 typedef Linetype
eval_fn(const struct ops
*, long *, const char **);
955 static eval_fn eval_table
, eval_unary
;
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
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.
970 Linetype (*fn
)(long *, Linetype
, long, Linetype
, long);
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
} } },
998 /* Current operator precedence level */
999 static long prec(const struct ops
*ops
)
1001 return (ops
- eval_ops
);
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.
1010 eval_unary(const struct ops
*ops
, long *valp
, const char **cpp
)
1018 cp
= skipcomment(*cpp
);
1020 debug("eval%d !", prec(ops
));
1022 lt
= eval_unary(ops
, valp
, &cp
);
1027 lt
= *valp
? LT_TRUE
: LT_FALSE
;
1029 } else if (*cp
== '~') {
1030 debug("eval%d ~", prec(ops
));
1032 lt
= eval_unary(ops
, valp
, &cp
);
1037 lt
= *valp
? LT_TRUE
: LT_FALSE
;
1039 } else if (*cp
== '-') {
1040 debug("eval%d -", prec(ops
));
1042 lt
= eval_unary(ops
, valp
, &cp
);
1047 lt
= *valp
? LT_TRUE
: LT_FALSE
;
1049 } else if (*cp
== '(') {
1051 debug("eval%d (", prec(ops
));
1052 lt
= eval_table(eval_ops
, valp
, &cp
);
1055 cp
= skipcomment(cp
);
1058 } else if (isdigit((unsigned char)*cp
)) {
1059 debug("eval%d number", prec(ops
));
1060 *valp
= strtol(cp
, &ep
, 0);
1063 lt
= *valp
? LT_TRUE
: LT_FALSE
;
1065 } else if (matchsym("defined", cp
) != NULL
) {
1066 cp
= skipcomment(cp
+7);
1068 cp
= skipcomment(cp
+1);
1074 cp
= skipcomment(cp
);
1075 if (defparen
&& *cp
++ != ')') {
1076 debug("eval%d defined missing ')'", prec(ops
));
1080 debug("eval%d defined unknown", prec(ops
));
1083 debug("eval%d defined %s", prec(ops
), symname
[sym
]);
1084 *valp
= (value
[sym
] != NULL
);
1085 lt
= *valp
? LT_TRUE
: LT_FALSE
;
1088 } else if (!endsym(*cp
)) {
1089 debug("eval%d symbol", prec(ops
));
1094 } else if (value
[sym
] == NULL
) {
1098 *valp
= strtol(value
[sym
], &ep
, 0);
1099 if (*ep
!= '\0' || ep
== value
[sym
])
1101 lt
= *valp
? LT_TRUE
: LT_FALSE
;
1106 debug("eval%d bad expr", prec(ops
));
1111 debug("eval%d = %d", prec(ops
), *valp
);
1116 * Table-driven evaluation of binary operators.
1119 eval_table(const struct ops
*ops
, long *valp
, const char **cpp
)
1121 const struct op
*op
;
1126 debug("eval%d", prec(ops
));
1128 lt
= ops
->inner(ops
+1, valp
, &cp
);
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
)
1143 if (op
->str
== NULL
)
1145 cp
+= strlen(op
->str
);
1146 debug("eval%d %s", prec(ops
), op
->str
);
1147 rt
= ops
->inner(ops
+1, &val
, &cp
);
1150 lt
= op
->fn(valp
, lt
, *valp
, rt
, val
);
1154 debug("eval%d = %d", prec(ops
), *valp
);
1155 debug("eval%d lt = %s", prec(ops
), linetype_name
[lt
]);
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.
1165 ifeval(const char **cpp
)
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
);
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
1189 if (fgets(tline
, MAXLINE
, input
) == NULL
) {
1191 err(2, "can't read %s", filename
);
1195 cp
= skipcomment(tline
);
1196 if (linestate
== LS_START
&& *cp
== '#') {
1197 linestate
= LS_HASH
;
1198 return (skipcomment(cp
+ 1));
1199 } else if (*cp
== '\0') {
1202 return (skipline(cp
));
1207 * Mark a line dirty and consume the rest of it, keeping track of the
1211 skipline(const char *cp
)
1215 linestate
= LS_DIRTY
;
1216 while (*cp
!= '\0') {
1217 cp
= skipcomment(pcp
= cp
);
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.
1232 skipcomment(const char *cp
)
1234 if (text
|| ignoring
[depth
]) {
1235 for (; isspace((unsigned char)*cp
); cp
++)
1237 linestate
= LS_START
;
1241 /* don't reset to LS_START after a line continuation */
1242 if (strncmp(cp
, "\\\r\n", 3) == 0)
1244 else if (strncmp(cp
, "\\\n", 2) == 0)
1246 else switch (incomment
) {
1248 if (strncmp(cp
, "/\\\r\n", 4) == 0) {
1249 incomment
= STARTING_COMMENT
;
1251 } else if (strncmp(cp
, "/\\\n", 3) == 0) {
1252 incomment
= STARTING_COMMENT
;
1254 } else if (strncmp(cp
, "/*", 2) == 0) {
1255 incomment
= C_COMMENT
;
1257 } else if (strncmp(cp
, "//", 2) == 0) {
1258 incomment
= CXX_COMMENT
;
1260 } else if (strncmp(cp
, "\'", 1) == 0) {
1261 incomment
= CHAR_LITERAL
;
1262 linestate
= LS_DIRTY
;
1264 } else if (strncmp(cp
, "\"", 1) == 0) {
1265 incomment
= STRING_LITERAL
;
1266 linestate
= LS_DIRTY
;
1268 } else if (strncmp(cp
, "\n", 1) == 0) {
1269 linestate
= LS_START
;
1271 } else if (strchr(" \r\t", *cp
) != NULL
) {
1277 if (strncmp(cp
, "\n", 1) == 0) {
1278 incomment
= NO_COMMENT
;
1279 linestate
= LS_START
;
1284 case STRING_LITERAL
:
1285 if ((incomment
== CHAR_LITERAL
&& cp
[0] == '\'') ||
1286 (incomment
== STRING_LITERAL
&& cp
[0] == '\"')) {
1287 incomment
= NO_COMMENT
;
1289 } else if (cp
[0] == '\\') {
1294 } else if (strncmp(cp
, "\n", 1) == 0) {
1295 if (incomment
== CHAR_LITERAL
)
1296 error("Unterminated char literal");
1298 error("Unterminated string literal");
1303 if (strncmp(cp
, "*\\\r\n", 4) == 0) {
1304 incomment
= FINISHING_COMMENT
;
1306 } else if (strncmp(cp
, "*\\\n", 3) == 0) {
1307 incomment
= FINISHING_COMMENT
;
1309 } else if (strncmp(cp
, "*/", 2) == 0) {
1310 incomment
= NO_COMMENT
;
1315 case STARTING_COMMENT
:
1317 incomment
= C_COMMENT
;
1319 } else if (*cp
== '/') {
1320 incomment
= CXX_COMMENT
;
1323 incomment
= NO_COMMENT
;
1324 linestate
= LS_DIRTY
;
1327 case FINISHING_COMMENT
:
1329 incomment
= NO_COMMENT
;
1332 incomment
= C_COMMENT
;
1341 * Skip macro arguments.
1344 skipargs(const char *cp
)
1346 const char *ocp
= cp
;
1348 cp
= skipcomment(cp
);
1356 cp
= skipcomment(cp
+1);
1357 } while (level
!= 0 && *cp
!= '\0');
1361 /* Rewind and re-detect the syntax error later. */
1366 * Skip over an identifier.
1369 skipsym(const char *cp
)
1371 while (!endsym(*cp
))
1377 * Skip whitespace and take a copy of any following identifier.
1380 getsym(const char **cpp
)
1382 const char *cp
= *cpp
, *sym
;
1384 cp
= skipcomment(cp
);
1385 cp
= skipsym(sym
= cp
);
1389 return (xstrdup(sym
, cp
));
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,
1399 matchsym(const char *s
, const char *t
)
1401 while (*s
!= '\0' && *t
!= '\0')
1406 if (*s
== '\0' && endsym(*t
))
1413 * Look for the symbol in the symbol table. If it is found, we return
1414 * the symbol table index, else we return -1.
1417 findsym(const char **strp
)
1423 *strp
= skipsym(str
);
1427 if (symdepth
&& firstsym
)
1428 printf("%s%3d", zerosyms
? "" : "\n", depth
);
1429 firstsym
= zerosyms
= false;
1431 symdepth
? " " : "",
1432 (int)(*strp
-str
), str
,
1433 symdepth
? "" : "\n");
1434 /* we don't care about the value of the symbol */
1437 for (symind
= 0; symind
< nsyms
; ++symind
) {
1438 if (matchsym(symname
[symind
], str
) != NULL
) {
1439 debugsym("findsym", symind
);
1447 * Resolve indirect symbol values to their final definitions.
1453 int changed
, sym
, ind
;
1457 for (sym
= 0; sym
< nsyms
; ++sym
) {
1458 if (value
[sym
] == NULL
)
1462 if (ind
== -1 || ind
== sym
||
1464 value
[ind
] == NULL
||
1465 value
[ind
] == value
[sym
])
1467 debugsym("indir...", sym
);
1468 value
[sym
] = value
[ind
];
1469 debugsym("...ectsym", sym
);
1476 * Add a symbol to the symbol table, specified with the format sym=val
1479 addsym1(bool ignorethis
, bool definethis
, char *symval
)
1481 const char *sym
, *val
;
1485 if (definethis
&& *val
== '=') {
1486 symval
[val
- sym
] = '\0';
1488 } else if (*val
== '\0') {
1489 val
= definethis
? "1" : NULL
;
1493 addsym2(ignorethis
, sym
, val
);
1497 * Add a symbol to the symbol table.
1500 addsym2(bool ignorethis
, const char *sym
, const char *val
)
1502 const char *cp
= sym
;
1505 symind
= findsym(&cp
);
1507 if (nsyms
>= MAXSYMS
)
1508 errx(2, "too many symbols");
1511 ignore
[symind
] = ignorethis
;
1512 symname
[symind
] = sym
;
1513 value
[symind
] = val
;
1514 debugsym("addsym", symind
);
1518 debugsym(const char *why
, int symind
)
1520 debug("%s %s%c%s", why
, symname
[symind
],
1521 value
[symind
] ? '=' : ' ',
1522 value
[symind
] ? value
[symind
] : "undef");
1526 * Add symbols to the symbol table from a file containing
1527 * #define and #undef preprocessor directives.
1530 defundefile(const char *fn
)
1533 input
= fopen(fn
, "rb");
1535 err(2, "can't open %s", fn
);
1540 err(2, "can't read %s", filename
);
1544 error("EOF in comment");
1548 * Read and process one #define or #undef directive
1553 const char *cp
, *kw
, *sym
, *val
, *end
;
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
)
1565 if (end
> tline
&& end
[-1] == '\\')
1569 if ((cp
= matchsym("define", kw
)) != NULL
) {
1572 error("Missing macro name in #define");
1576 cp
= skipcomment(cp
);
1577 val
= (cp
< end
) ? xstrdup(cp
, end
) : "";
1580 addsym2(false, sym
, val
);
1581 } else if ((cp
= matchsym("undef", kw
)) != NULL
) {
1584 error("Missing macro name in #undef");
1585 cp
= skipcomment(cp
);
1587 addsym2(false, sym
, NULL
);
1589 error("Unrecognized preprocessor directive");
1593 debug("parser line %d state %s comment %s line", linenum
,
1594 comment_name
[incomment
], linestate_name
[linestate
]);
1599 * Concatenate two strings into new memory, checking for failure.
1602 astrcat(const char *s1
, const char *s2
)
1608 len
= snprintf(NULL
, 0, "%s%s", s1
, s2
);
1611 size
= (size_t)len
+ 1;
1612 s
= (char *)malloc(size
);
1615 snprintf(s
, size
, "%s%s", s1
, s2
);
1620 * Duplicate a segment of a string, checking for failure.
1623 xstrdup(const char *start
, const char *end
)
1628 if (end
< start
) abort(); /* bug */
1629 n
= (size_t)(end
- start
) + 1;
1633 snprintf(s
, n
, "%s", start
);
1641 debug(const char *msg
, ...)
1653 error(const char *msg
)
1656 warnx("%s: %d: %s", filename
, linenum
, msg
);
1658 warnx("%s: %d: %s (#if line %d depth %d)",
1659 filename
, linenum
, msg
, stifline
[depth
], depth
);
1661 errx(2, "Output may be truncated");