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