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