xref: /minix3/usr.bin/unifdef/unifdef.c (revision b6f0c436268cdf3df867d9382f2423f5a36f7a4d)
1*b6f0c436SThomas Cort /*	$NetBSD: unifdef.c,v 1.22 2012/10/13 18:26:03 christos Exp $	*/
2*b6f0c436SThomas Cort 
3*b6f0c436SThomas Cort /*
4*b6f0c436SThomas Cort  * Copyright (c) 1985, 1993
5*b6f0c436SThomas Cort  *	The Regents of the University of California.  All rights reserved.
6*b6f0c436SThomas Cort  *
7*b6f0c436SThomas Cort  * This code is derived from software contributed to Berkeley by
8*b6f0c436SThomas Cort  * Dave Yost. It was rewritten to support ANSI C by Tony Finch.
9*b6f0c436SThomas Cort  *
10*b6f0c436SThomas Cort  * Redistribution and use in source and binary forms, with or without
11*b6f0c436SThomas Cort  * modification, are permitted provided that the following conditions
12*b6f0c436SThomas Cort  * are met:
13*b6f0c436SThomas Cort  * 1. Redistributions of source code must retain the above copyright
14*b6f0c436SThomas Cort  *    notice, this list of conditions and the following disclaimer.
15*b6f0c436SThomas Cort  * 2. Redistributions in binary form must reproduce the above copyright
16*b6f0c436SThomas Cort  *    notice, this list of conditions and the following disclaimer in the
17*b6f0c436SThomas Cort  *    documentation and/or other materials provided with the distribution.
18*b6f0c436SThomas Cort  * 3. Neither the name of the University nor the names of its contributors
19*b6f0c436SThomas Cort  *    may be used to endorse or promote products derived from this software
20*b6f0c436SThomas Cort  *    without specific prior written permission.
21*b6f0c436SThomas Cort  *
22*b6f0c436SThomas Cort  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23*b6f0c436SThomas Cort  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24*b6f0c436SThomas Cort  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25*b6f0c436SThomas Cort  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26*b6f0c436SThomas Cort  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27*b6f0c436SThomas Cort  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28*b6f0c436SThomas Cort  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29*b6f0c436SThomas Cort  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30*b6f0c436SThomas Cort  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31*b6f0c436SThomas Cort  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32*b6f0c436SThomas Cort  * SUCH DAMAGE.
33*b6f0c436SThomas Cort  */
34*b6f0c436SThomas Cort 
35*b6f0c436SThomas Cort /*
36*b6f0c436SThomas Cort  * Copyright (c) 2002, 2003 Tony Finch <dot@dotat.at>
37*b6f0c436SThomas Cort  *
38*b6f0c436SThomas Cort  * This code is derived from software contributed to Berkeley by
39*b6f0c436SThomas Cort  * Dave Yost. It was rewritten to support ANSI C by Tony Finch.
40*b6f0c436SThomas Cort  *
41*b6f0c436SThomas Cort  * Redistribution and use in source and binary forms, with or without
42*b6f0c436SThomas Cort  * modification, are permitted provided that the following conditions
43*b6f0c436SThomas Cort  * are met:
44*b6f0c436SThomas Cort  * 1. Redistributions of source code must retain the above copyright
45*b6f0c436SThomas Cort  *    notice, this list of conditions and the following disclaimer.
46*b6f0c436SThomas Cort  * 2. Redistributions in binary form must reproduce the above copyright
47*b6f0c436SThomas Cort  *    notice, this list of conditions and the following disclaimer in the
48*b6f0c436SThomas Cort  *    documentation and/or other materials provided with the distribution.
49*b6f0c436SThomas Cort  * 3. All advertising materials mentioning features or use of this software
50*b6f0c436SThomas Cort  *    must display the following acknowledgement:
51*b6f0c436SThomas Cort  *	This product includes software developed by the University of
52*b6f0c436SThomas Cort  *	California, Berkeley and its contributors.
53*b6f0c436SThomas Cort  * 4. Neither the name of the University nor the names of its contributors
54*b6f0c436SThomas Cort  *    may be used to endorse or promote products derived from this software
55*b6f0c436SThomas Cort  *    without specific prior written permission.
56*b6f0c436SThomas Cort  *
57*b6f0c436SThomas Cort  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
58*b6f0c436SThomas Cort  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59*b6f0c436SThomas Cort  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60*b6f0c436SThomas Cort  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
61*b6f0c436SThomas Cort  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62*b6f0c436SThomas Cort  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
63*b6f0c436SThomas Cort  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64*b6f0c436SThomas Cort  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65*b6f0c436SThomas Cort  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66*b6f0c436SThomas Cort  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67*b6f0c436SThomas Cort  * SUCH DAMAGE.
68*b6f0c436SThomas Cort  */
69*b6f0c436SThomas Cort 
70*b6f0c436SThomas Cort #include <sys/cdefs.h>
71*b6f0c436SThomas Cort 
72*b6f0c436SThomas Cort #ifndef lint
73*b6f0c436SThomas Cort #if 0
74*b6f0c436SThomas Cort static const char copyright[] =
75*b6f0c436SThomas Cort "@(#) Copyright (c) 1985, 1993\n\
76*b6f0c436SThomas Cort 	The Regents of the University of California.  All rights reserved.\n";
77*b6f0c436SThomas Cort #endif
78*b6f0c436SThomas Cort #ifdef __IDSTRING
79*b6f0c436SThomas Cort __IDSTRING(Berkeley, "@(#)unifdef.c	8.1 (Berkeley) 6/6/93");
80*b6f0c436SThomas Cort __IDSTRING(NetBSD, "$NetBSD: unifdef.c,v 1.22 2012/10/13 18:26:03 christos Exp $");
81*b6f0c436SThomas Cort __IDSTRING(dotat, "$dotat: things/unifdef.c,v 1.161 2003/07/01 15:32:48 fanf2 Exp $");
82*b6f0c436SThomas Cort #endif
83*b6f0c436SThomas Cort #endif /* not lint */
84*b6f0c436SThomas Cort #ifdef __FBSDID
85*b6f0c436SThomas Cort __FBSDID("$FreeBSD: src/usr.bin/unifdef/unifdef.c,v 1.18 2003/07/01 15:30:43 fanf Exp $");
86*b6f0c436SThomas Cort #endif
87*b6f0c436SThomas Cort 
88*b6f0c436SThomas Cort /*
89*b6f0c436SThomas Cort  * unifdef - remove ifdef'ed lines
90*b6f0c436SThomas Cort  *
91*b6f0c436SThomas Cort  *  Wishlist:
92*b6f0c436SThomas Cort  *      provide an option which will append the name of the
93*b6f0c436SThomas Cort  *        appropriate symbol after #else's and #endif's
94*b6f0c436SThomas Cort  *      provide an option which will check symbols after
95*b6f0c436SThomas Cort  *        #else's and #endif's to see that they match their
96*b6f0c436SThomas Cort  *        corresponding #ifdef or #ifndef
97*b6f0c436SThomas Cort  *      generate #line directives in place of deleted code
98*b6f0c436SThomas Cort  *
99*b6f0c436SThomas Cort  *   The first two items above require better buffer handling, which would
100*b6f0c436SThomas Cort  *     also make it possible to handle all "dodgy" directives correctly.
101*b6f0c436SThomas Cort  */
102*b6f0c436SThomas Cort 
103*b6f0c436SThomas Cort #include <ctype.h>
104*b6f0c436SThomas Cort #include <err.h>
105*b6f0c436SThomas Cort #include <libgen.h>
106*b6f0c436SThomas Cort #include <stdarg.h>
107*b6f0c436SThomas Cort #include <stdio.h>
108*b6f0c436SThomas Cort #include <stdlib.h>
109*b6f0c436SThomas Cort #include <string.h>
110*b6f0c436SThomas Cort #include <unistd.h>
111*b6f0c436SThomas Cort 
112*b6f0c436SThomas Cort #include <sys/param.h>
113*b6f0c436SThomas Cort #include <sys/stat.h>
114*b6f0c436SThomas Cort 
115*b6f0c436SThomas Cort #include "stdbool.h"
116*b6f0c436SThomas Cort 
117*b6f0c436SThomas Cort /* types of input lines: */
118*b6f0c436SThomas Cort typedef enum {
119*b6f0c436SThomas Cort 	LT_TRUEI,		/* a true #if with ignore flag */
120*b6f0c436SThomas Cort 	LT_FALSEI,		/* a false #if with ignore flag */
121*b6f0c436SThomas Cort 	LT_IF,			/* an unknown #if */
122*b6f0c436SThomas Cort 	LT_TRUE,		/* a true #if */
123*b6f0c436SThomas Cort 	LT_FALSE,		/* a false #if */
124*b6f0c436SThomas Cort 	LT_ELIF,		/* an unknown #elif */
125*b6f0c436SThomas Cort 	LT_ELTRUE,		/* a true #elif */
126*b6f0c436SThomas Cort 	LT_ELFALSE,		/* a false #elif */
127*b6f0c436SThomas Cort 	LT_ELSE,		/* #else */
128*b6f0c436SThomas Cort 	LT_ENDIF,		/* #endif */
129*b6f0c436SThomas Cort 	LT_DODGY,		/* flag: directive is not on one line */
130*b6f0c436SThomas Cort 	LT_DODGY_LAST = LT_DODGY + LT_ENDIF,
131*b6f0c436SThomas Cort 	LT_PLAIN,		/* ordinary line */
132*b6f0c436SThomas Cort 	LT_EOF,			/* end of file */
133*b6f0c436SThomas Cort 	LT_COUNT
134*b6f0c436SThomas Cort } Linetype;
135*b6f0c436SThomas Cort 
136*b6f0c436SThomas Cort static char const * const linetype_name[] = {
137*b6f0c436SThomas Cort 	"TRUEI", "FALSEI", "IF", "TRUE", "FALSE",
138*b6f0c436SThomas Cort 	"ELIF", "ELTRUE", "ELFALSE", "ELSE", "ENDIF",
139*b6f0c436SThomas Cort 	"DODGY TRUEI", "DODGY FALSEI",
140*b6f0c436SThomas Cort 	"DODGY IF", "DODGY TRUE", "DODGY FALSE",
141*b6f0c436SThomas Cort 	"DODGY ELIF", "DODGY ELTRUE", "DODGY ELFALSE",
142*b6f0c436SThomas Cort 	"DODGY ELSE", "DODGY ENDIF",
143*b6f0c436SThomas Cort 	"PLAIN", "EOF"
144*b6f0c436SThomas Cort };
145*b6f0c436SThomas Cort 
146*b6f0c436SThomas Cort /* state of #if processing */
147*b6f0c436SThomas Cort typedef enum {
148*b6f0c436SThomas Cort 	IS_OUTSIDE,
149*b6f0c436SThomas Cort 	IS_FALSE_PREFIX,	/* false #if followed by false #elifs */
150*b6f0c436SThomas Cort 	IS_TRUE_PREFIX,		/* first non-false #(el)if is true */
151*b6f0c436SThomas Cort 	IS_PASS_MIDDLE,		/* first non-false #(el)if is unknown */
152*b6f0c436SThomas Cort 	IS_FALSE_MIDDLE,	/* a false #elif after a pass state */
153*b6f0c436SThomas Cort 	IS_TRUE_MIDDLE,		/* a true #elif after a pass state */
154*b6f0c436SThomas Cort 	IS_PASS_ELSE,		/* an else after a pass state */
155*b6f0c436SThomas Cort 	IS_FALSE_ELSE,		/* an else after a true state */
156*b6f0c436SThomas Cort 	IS_TRUE_ELSE,		/* an else after only false states */
157*b6f0c436SThomas Cort 	IS_FALSE_TRAILER,	/* #elifs after a true are false */
158*b6f0c436SThomas Cort 	IS_COUNT
159*b6f0c436SThomas Cort } Ifstate;
160*b6f0c436SThomas Cort 
161*b6f0c436SThomas Cort static char const * const ifstate_name[] = {
162*b6f0c436SThomas Cort 	"OUTSIDE", "FALSE_PREFIX", "TRUE_PREFIX",
163*b6f0c436SThomas Cort 	"PASS_MIDDLE", "FALSE_MIDDLE", "TRUE_MIDDLE",
164*b6f0c436SThomas Cort 	"PASS_ELSE", "FALSE_ELSE", "TRUE_ELSE",
165*b6f0c436SThomas Cort 	"FALSE_TRAILER"
166*b6f0c436SThomas Cort };
167*b6f0c436SThomas Cort 
168*b6f0c436SThomas Cort /* state of comment parser */
169*b6f0c436SThomas Cort typedef enum {
170*b6f0c436SThomas Cort 	NO_COMMENT = false,	/* outside a comment */
171*b6f0c436SThomas Cort 	C_COMMENT,		/* in a comment like this one */
172*b6f0c436SThomas Cort 	CXX_COMMENT,		/* between // and end of line */
173*b6f0c436SThomas Cort 	STARTING_COMMENT,	/* just after slash-backslash-newline */
174*b6f0c436SThomas Cort 	FINISHING_COMMENT	/* star-backslash-newline in a C comment */
175*b6f0c436SThomas Cort } Comment_state;
176*b6f0c436SThomas Cort 
177*b6f0c436SThomas Cort static char const * const comment_name[] = {
178*b6f0c436SThomas Cort 	"NO", "C", "CXX", "STARTING", "FINISHING"
179*b6f0c436SThomas Cort };
180*b6f0c436SThomas Cort 
181*b6f0c436SThomas Cort /* state of preprocessor line parser */
182*b6f0c436SThomas Cort typedef enum {
183*b6f0c436SThomas Cort 	LS_START,		/* only space and comments on this line */
184*b6f0c436SThomas Cort 	LS_HASH,		/* only space, comments, and a hash */
185*b6f0c436SThomas Cort 	LS_DIRTY		/* this line can't be a preprocessor line */
186*b6f0c436SThomas Cort } Line_state;
187*b6f0c436SThomas Cort 
188*b6f0c436SThomas Cort static char const * const linestate_name[] = {
189*b6f0c436SThomas Cort 	"START", "HASH", "DIRTY"
190*b6f0c436SThomas Cort };
191*b6f0c436SThomas Cort 
192*b6f0c436SThomas Cort /*
193*b6f0c436SThomas Cort  * Minimum translation limits from ISO/IEC 9899:1999 5.2.4.1
194*b6f0c436SThomas Cort  */
195*b6f0c436SThomas Cort #define	MAXDEPTH        64			/* maximum #if nesting */
196*b6f0c436SThomas Cort #define	MAXLINE         4096			/* maximum length of line */
197*b6f0c436SThomas Cort #define	MAXSYMS         4096			/* maximum number of symbols */
198*b6f0c436SThomas Cort 
199*b6f0c436SThomas Cort /*
200*b6f0c436SThomas Cort  * Sometimes when editing a keyword the replacement text is longer, so
201*b6f0c436SThomas Cort  * we leave some space at the end of the tline buffer to accommodate this.
202*b6f0c436SThomas Cort  */
203*b6f0c436SThomas Cort #define	EDITSLOP        10
204*b6f0c436SThomas Cort 
205*b6f0c436SThomas Cort /*
206*b6f0c436SThomas Cort  * Globals.
207*b6f0c436SThomas Cort  */
208*b6f0c436SThomas Cort 
209*b6f0c436SThomas Cort static bool             complement;		/* -c: do the complement */
210*b6f0c436SThomas Cort static bool             debugging;		/* -d: debugging reports */
211*b6f0c436SThomas Cort static bool             iocccok;		/* -e: fewer IOCCC errors */
212*b6f0c436SThomas Cort static bool             killconsts;		/* -k: eval constant #ifs */
213*b6f0c436SThomas Cort static bool             lnblank;		/* -l: blank deleted lines */
214*b6f0c436SThomas Cort static bool             symlist;		/* -s: output symbol list */
215*b6f0c436SThomas Cort static bool             text;			/* -t: this is a text file */
216*b6f0c436SThomas Cort 
217*b6f0c436SThomas Cort static const char      *symname[MAXSYMS];	/* symbol name */
218*b6f0c436SThomas Cort static const char      *value[MAXSYMS];		/* -Dsym=value */
219*b6f0c436SThomas Cort static bool             ignore[MAXSYMS];	/* -iDsym or -iUsym */
220*b6f0c436SThomas Cort static int              nsyms;			/* number of symbols */
221*b6f0c436SThomas Cort 
222*b6f0c436SThomas Cort static FILE            *input;			/* input file pointer */
223*b6f0c436SThomas Cort static FILE            *output;			/* output file pointer */
224*b6f0c436SThomas Cort static const char      *filename;		/* input file name */
225*b6f0c436SThomas Cort static char            *ofilename;		/* output file name */
226*b6f0c436SThomas Cort static char             tmpname[MAXPATHLEN];	/* used when overwriting */
227*b6f0c436SThomas Cort static int              linenum;		/* current line number */
228*b6f0c436SThomas Cort static int              overwriting;		/* output overwrites input */
229*b6f0c436SThomas Cort 
230*b6f0c436SThomas Cort static char             tline[MAXLINE+EDITSLOP];/* input buffer plus space */
231*b6f0c436SThomas Cort static char            *keyword;		/* used for editing #elif's */
232*b6f0c436SThomas Cort 
233*b6f0c436SThomas Cort static Comment_state    incomment;		/* comment parser state */
234*b6f0c436SThomas Cort static Line_state       linestate;		/* #if line parser state */
235*b6f0c436SThomas Cort static Ifstate          ifstate[MAXDEPTH];	/* #if processor state */
236*b6f0c436SThomas Cort static bool             ignoring[MAXDEPTH];	/* ignore comments state */
237*b6f0c436SThomas Cort static int              stifline[MAXDEPTH];	/* start of current #if */
238*b6f0c436SThomas Cort static int              depth;			/* current #if nesting */
239*b6f0c436SThomas Cort static bool             keepthis;		/* don't delete constant #if */
240*b6f0c436SThomas Cort 
241*b6f0c436SThomas Cort static int              exitstat;		/* program exit status */
242*b6f0c436SThomas Cort 
243*b6f0c436SThomas Cort static void             addsym(bool, bool, char *);
244*b6f0c436SThomas Cort static void             debug(const char *, ...) __printflike(1, 2);
245*b6f0c436SThomas Cort __dead static void      done(void);
246*b6f0c436SThomas Cort __dead static void      error(const char *);
247*b6f0c436SThomas Cort static int              findsym(const char *);
248*b6f0c436SThomas Cort static void             flushline(bool);
249*b6f0c436SThomas Cort static Linetype         get_line(void);
250*b6f0c436SThomas Cort static Linetype         ifeval(const char **);
251*b6f0c436SThomas Cort static void             ignoreoff(void);
252*b6f0c436SThomas Cort static void             ignoreon(void);
253*b6f0c436SThomas Cort static void             keywordedit(const char *);
254*b6f0c436SThomas Cort static void             nest(void);
255*b6f0c436SThomas Cort __dead static void      process(void);
256*b6f0c436SThomas Cort static const char      *skipcomment(const char *);
257*b6f0c436SThomas Cort static const char      *skipsym(const char *);
258*b6f0c436SThomas Cort static void             state(Ifstate);
259*b6f0c436SThomas Cort static int              strlcmp(const char *, const char *, size_t);
260*b6f0c436SThomas Cort __dead static void      usage(void);
261*b6f0c436SThomas Cort 
262*b6f0c436SThomas Cort #define endsym(c) (!isalpha((unsigned char)c) && !isdigit((unsigned char)c) && c != '_')
263*b6f0c436SThomas Cort 
264*b6f0c436SThomas Cort /*
265*b6f0c436SThomas Cort  * The main program.
266*b6f0c436SThomas Cort  */
267*b6f0c436SThomas Cort int
main(int argc,char * argv[])268*b6f0c436SThomas Cort main(int argc, char *argv[])
269*b6f0c436SThomas Cort {
270*b6f0c436SThomas Cort 	int opt;
271*b6f0c436SThomas Cort 	struct stat isb, osb;
272*b6f0c436SThomas Cort 
273*b6f0c436SThomas Cort 	while ((opt = getopt(argc, argv, "i:D:U:I:o:cdeklst")) != -1)
274*b6f0c436SThomas Cort 		switch (opt) {
275*b6f0c436SThomas Cort 		case 'i': /* treat stuff controlled by these symbols as text */
276*b6f0c436SThomas Cort 			/*
277*b6f0c436SThomas Cort 			 * For strict backwards-compatibility the U or D
278*b6f0c436SThomas Cort 			 * should be immediately after the -i but it doesn't
279*b6f0c436SThomas Cort 			 * matter much if we relax that requirement.
280*b6f0c436SThomas Cort 			 */
281*b6f0c436SThomas Cort 			opt = *optarg++;
282*b6f0c436SThomas Cort 			if (opt == 'D')
283*b6f0c436SThomas Cort 				addsym(true, true, optarg);
284*b6f0c436SThomas Cort 			else if (opt == 'U')
285*b6f0c436SThomas Cort 				addsym(true, false, optarg);
286*b6f0c436SThomas Cort 			else
287*b6f0c436SThomas Cort 				usage();
288*b6f0c436SThomas Cort 			break;
289*b6f0c436SThomas Cort 		case 'D': /* define a symbol */
290*b6f0c436SThomas Cort 			addsym(false, true, optarg);
291*b6f0c436SThomas Cort 			break;
292*b6f0c436SThomas Cort 		case 'U': /* undef a symbol */
293*b6f0c436SThomas Cort 			addsym(false, false, optarg);
294*b6f0c436SThomas Cort 			break;
295*b6f0c436SThomas Cort 		case 'I':
296*b6f0c436SThomas Cort 			/* no-op for compatibility with cpp */
297*b6f0c436SThomas Cort 			break;
298*b6f0c436SThomas Cort 		case 'c': /* treat -D as -U and vice versa */
299*b6f0c436SThomas Cort 			complement = true;
300*b6f0c436SThomas Cort 			break;
301*b6f0c436SThomas Cort 		case 'd':
302*b6f0c436SThomas Cort 			debugging = true;
303*b6f0c436SThomas Cort 			break;
304*b6f0c436SThomas Cort 		case 'e': /* fewer errors from dodgy lines */
305*b6f0c436SThomas Cort 			iocccok = true;
306*b6f0c436SThomas Cort 			break;
307*b6f0c436SThomas Cort 		case 'k': /* process constant #ifs */
308*b6f0c436SThomas Cort 			killconsts = true;
309*b6f0c436SThomas Cort 			break;
310*b6f0c436SThomas Cort 		case 'l': /* blank deleted lines instead of omitting them */
311*b6f0c436SThomas Cort 			lnblank = true;
312*b6f0c436SThomas Cort 			break;
313*b6f0c436SThomas Cort 		case 'o': /* output to a file */
314*b6f0c436SThomas Cort 			ofilename = optarg;
315*b6f0c436SThomas Cort 			break;
316*b6f0c436SThomas Cort 		case 's': /* only output list of symbols that control #ifs */
317*b6f0c436SThomas Cort 			symlist = true;
318*b6f0c436SThomas Cort 			break;
319*b6f0c436SThomas Cort 		case 't': /* don't parse C comments */
320*b6f0c436SThomas Cort 			text = true;
321*b6f0c436SThomas Cort 			break;
322*b6f0c436SThomas Cort 		default:
323*b6f0c436SThomas Cort 			usage();
324*b6f0c436SThomas Cort 		}
325*b6f0c436SThomas Cort 	argc -= optind;
326*b6f0c436SThomas Cort 	argv += optind;
327*b6f0c436SThomas Cort 	if (nsyms == 0 && !symlist) {
328*b6f0c436SThomas Cort 		warnx("must -D or -U at least one symbol");
329*b6f0c436SThomas Cort 		usage();
330*b6f0c436SThomas Cort 	}
331*b6f0c436SThomas Cort 	if (argc > 1) {
332*b6f0c436SThomas Cort 		errx(2, "can only do one file");
333*b6f0c436SThomas Cort 	} else if (argc == 1 && strcmp(*argv, "-") != 0) {
334*b6f0c436SThomas Cort 		filename = *argv;
335*b6f0c436SThomas Cort 		input = fopen(filename, "r");
336*b6f0c436SThomas Cort 		if (input == NULL)
337*b6f0c436SThomas Cort 			err(2, "can't open %s", filename);
338*b6f0c436SThomas Cort 	} else {
339*b6f0c436SThomas Cort 		filename = "[stdin]";
340*b6f0c436SThomas Cort 		input = stdin;
341*b6f0c436SThomas Cort 	}
342*b6f0c436SThomas Cort 	if (ofilename == NULL) {
343*b6f0c436SThomas Cort 		output = stdout;
344*b6f0c436SThomas Cort 	} else {
345*b6f0c436SThomas Cort 		if (stat(ofilename, &osb) == 0) {
346*b6f0c436SThomas Cort 			if (fstat(fileno(input), &isb) != 0)
347*b6f0c436SThomas Cort 				err(2, "can't fstat %s", filename);
348*b6f0c436SThomas Cort 
349*b6f0c436SThomas Cort 			overwriting = (osb.st_dev == isb.st_dev &&
350*b6f0c436SThomas Cort 			    osb.st_ino == isb.st_ino);
351*b6f0c436SThomas Cort 		}
352*b6f0c436SThomas Cort 		if (overwriting) {
353*b6f0c436SThomas Cort 			int ofd;
354*b6f0c436SThomas Cort 
355*b6f0c436SThomas Cort 			snprintf(tmpname, sizeof(tmpname), "%s/unifdef.XXXXXX",
356*b6f0c436SThomas Cort 				 dirname(ofilename));
357*b6f0c436SThomas Cort 			if ((ofd = mkstemp(tmpname)) != -1)
358*b6f0c436SThomas Cort 				output = fdopen(ofd, "w+");
359*b6f0c436SThomas Cort 			if (output == NULL)
360*b6f0c436SThomas Cort 				err(2, "can't create temporary file");
361*b6f0c436SThomas Cort 			fchmod(ofd, isb.st_mode & ACCESSPERMS);
362*b6f0c436SThomas Cort 		} else {
363*b6f0c436SThomas Cort 			output = fopen(ofilename, "w");
364*b6f0c436SThomas Cort 			if (output == NULL)
365*b6f0c436SThomas Cort 				err(2, "can't open %s", ofilename);
366*b6f0c436SThomas Cort 		}
367*b6f0c436SThomas Cort 	}
368*b6f0c436SThomas Cort 	process();
369*b6f0c436SThomas Cort 	abort(); /* bug */
370*b6f0c436SThomas Cort }
371*b6f0c436SThomas Cort 
372*b6f0c436SThomas Cort static void
usage(void)373*b6f0c436SThomas Cort usage(void)
374*b6f0c436SThomas Cort {
375*b6f0c436SThomas Cort 	fprintf(stderr, "usage: unifdef [-cdeklst] [-o output]"
376*b6f0c436SThomas Cort 	    " [-Dsym[=val]] [-Usym] [-iDsym[=val]] [-iUsym] ... [file]\n");
377*b6f0c436SThomas Cort 	exit(2);
378*b6f0c436SThomas Cort }
379*b6f0c436SThomas Cort 
380*b6f0c436SThomas Cort /*
381*b6f0c436SThomas Cort  * A state transition function alters the global #if processing state
382*b6f0c436SThomas Cort  * in a particular way. The table below is indexed by the current
383*b6f0c436SThomas Cort  * processing state and the type of the current line.
384*b6f0c436SThomas Cort  *
385*b6f0c436SThomas Cort  * Nesting is handled by keeping a stack of states; some transition
386*b6f0c436SThomas Cort  * functions increase or decrease the depth. They also maintain the
387*b6f0c436SThomas Cort  * ignore state on a stack. In some complicated cases they have to
388*b6f0c436SThomas Cort  * alter the preprocessor directive, as follows.
389*b6f0c436SThomas Cort  *
390*b6f0c436SThomas Cort  * When we have processed a group that starts off with a known-false
391*b6f0c436SThomas Cort  * #if/#elif sequence (which has therefore been deleted) followed by a
392*b6f0c436SThomas Cort  * #elif that we don't understand and therefore must keep, we edit the
393*b6f0c436SThomas Cort  * latter into a #if to keep the nesting correct.
394*b6f0c436SThomas Cort  *
395*b6f0c436SThomas Cort  * When we find a true #elif in a group, the following block will
396*b6f0c436SThomas Cort  * always be kept and the rest of the sequence after the next #elif or
397*b6f0c436SThomas Cort  * #else will be discarded. We edit the #elif into a #else and the
398*b6f0c436SThomas Cort  * following directive to #endif since this has the desired behaviour.
399*b6f0c436SThomas Cort  *
400*b6f0c436SThomas Cort  * "Dodgy" directives are split across multiple lines, the most common
401*b6f0c436SThomas Cort  * example being a multi-line comment hanging off the right of the
402*b6f0c436SThomas Cort  * directive. We can handle them correctly only if there is no change
403*b6f0c436SThomas Cort  * from printing to dropping (or vice versa) caused by that directive.
404*b6f0c436SThomas Cort  * If the directive is the first of a group we have a choice between
405*b6f0c436SThomas Cort  * failing with an error, or passing it through unchanged instead of
406*b6f0c436SThomas Cort  * evaluating it. The latter is not the default to avoid questions from
407*b6f0c436SThomas Cort  * users about unifdef unexpectedly leaving behind preprocessor directives.
408*b6f0c436SThomas Cort  */
409*b6f0c436SThomas Cort typedef void state_fn(void);
410*b6f0c436SThomas Cort 
411*b6f0c436SThomas Cort /* report an error */
Eelif(void)412*b6f0c436SThomas Cort __dead static void Eelif (void) { error("Inappropriate #elif"); }
Eelse(void)413*b6f0c436SThomas Cort __dead static void Eelse (void) { error("Inappropriate #else"); }
Eendif(void)414*b6f0c436SThomas Cort __dead static void Eendif(void) { error("Inappropriate #endif"); }
Eeof(void)415*b6f0c436SThomas Cort __dead static void Eeof  (void) { error("Premature EOF"); }
Eioccc(void)416*b6f0c436SThomas Cort __dead static void Eioccc(void) { error("Obfuscated preprocessor control line"); }
417*b6f0c436SThomas Cort /* plain line handling */
print(void)418*b6f0c436SThomas Cort static void print (void) { flushline(true); }
drop(void)419*b6f0c436SThomas Cort static void drop  (void) { flushline(false); }
420*b6f0c436SThomas Cort /* output lacks group's start line */
Strue(void)421*b6f0c436SThomas Cort static void Strue (void) { drop();  ignoreoff(); state(IS_TRUE_PREFIX); }
Sfalse(void)422*b6f0c436SThomas Cort static void Sfalse(void) { drop();  ignoreoff(); state(IS_FALSE_PREFIX); }
Selse(void)423*b6f0c436SThomas Cort static void Selse (void) { drop();               state(IS_TRUE_ELSE); }
424*b6f0c436SThomas Cort /* print/pass this block */
Pelif(void)425*b6f0c436SThomas Cort static void Pelif (void) { print(); ignoreoff(); state(IS_PASS_MIDDLE); }
Pelse(void)426*b6f0c436SThomas Cort static void Pelse (void) { print();              state(IS_PASS_ELSE); }
Pendif(void)427*b6f0c436SThomas Cort static void Pendif(void) { print(); --depth; }
428*b6f0c436SThomas Cort /* discard this block */
Dfalse(void)429*b6f0c436SThomas Cort static void Dfalse(void) { drop();  ignoreoff(); state(IS_FALSE_TRAILER); }
Delif(void)430*b6f0c436SThomas Cort static void Delif (void) { drop();  ignoreoff(); state(IS_FALSE_MIDDLE); }
Delse(void)431*b6f0c436SThomas Cort static void Delse (void) { drop();               state(IS_FALSE_ELSE); }
Dendif(void)432*b6f0c436SThomas Cort static void Dendif(void) { drop();  --depth; }
433*b6f0c436SThomas Cort /* first line of group */
Fdrop(void)434*b6f0c436SThomas Cort static void Fdrop (void) { nest();  Dfalse(); }
Fpass(void)435*b6f0c436SThomas Cort static void Fpass (void) { nest();  Pelif(); }
Ftrue(void)436*b6f0c436SThomas Cort static void Ftrue (void) { nest();  Strue(); }
Ffalse(void)437*b6f0c436SThomas Cort static void Ffalse(void) { nest();  Sfalse(); }
438*b6f0c436SThomas Cort /* variable pedantry for obfuscated lines */
Oiffy(void)439*b6f0c436SThomas Cort static void Oiffy (void) { if (iocccok) Fpass(); else Eioccc(); ignoreon(); }
Oif(void)440*b6f0c436SThomas Cort static void Oif   (void) { if (iocccok) Fpass(); else Eioccc(); }
Oelif(void)441*b6f0c436SThomas Cort static void Oelif (void) { if (iocccok) Pelif(); else Eioccc(); }
442*b6f0c436SThomas Cort /* ignore comments in this block */
Idrop(void)443*b6f0c436SThomas Cort static void Idrop (void) { Fdrop();  ignoreon(); }
Itrue(void)444*b6f0c436SThomas Cort static void Itrue (void) { Ftrue();  ignoreon(); }
Ifalse(void)445*b6f0c436SThomas Cort static void Ifalse(void) { Ffalse(); ignoreon(); }
446*b6f0c436SThomas Cort /* edit this line */
Mpass(void)447*b6f0c436SThomas Cort static void Mpass (void) { strncpy(keyword, "if  ", 4); Pelif(); }
Mtrue(void)448*b6f0c436SThomas Cort static void Mtrue (void) { keywordedit("else\n");  state(IS_TRUE_MIDDLE); }
Melif(void)449*b6f0c436SThomas Cort static void Melif (void) { keywordedit("endif\n"); state(IS_FALSE_TRAILER); }
Melse(void)450*b6f0c436SThomas Cort static void Melse (void) { keywordedit("endif\n"); state(IS_FALSE_ELSE); }
451*b6f0c436SThomas Cort 
452*b6f0c436SThomas Cort static state_fn * const trans_table[IS_COUNT][LT_COUNT] = {
453*b6f0c436SThomas Cort /* IS_OUTSIDE */
454*b6f0c436SThomas Cort { Itrue, Ifalse,Fpass, Ftrue, Ffalse,Eelif, Eelif, Eelif, Eelse, Eendif,
455*b6f0c436SThomas Cort   Oiffy, Oiffy, Fpass, Oif,   Oif,   Eelif, Eelif, Eelif, Eelse, Eendif,
456*b6f0c436SThomas Cort   print, done },
457*b6f0c436SThomas Cort /* IS_FALSE_PREFIX */
458*b6f0c436SThomas Cort { Idrop, Idrop, Fdrop, Fdrop, Fdrop, Mpass, Strue, Sfalse,Selse, Dendif,
459*b6f0c436SThomas Cort   Idrop, Idrop, Fdrop, Fdrop, Fdrop, Mpass, Eioccc,Eioccc,Eioccc,Eioccc,
460*b6f0c436SThomas Cort   drop,  Eeof },
461*b6f0c436SThomas Cort /* IS_TRUE_PREFIX */
462*b6f0c436SThomas Cort { Itrue, Ifalse,Fpass, Ftrue, Ffalse,Dfalse,Dfalse,Dfalse,Delse, Dendif,
463*b6f0c436SThomas Cort   Oiffy, Oiffy, Fpass, Oif,   Oif,   Eioccc,Eioccc,Eioccc,Eioccc,Eioccc,
464*b6f0c436SThomas Cort   print, Eeof },
465*b6f0c436SThomas Cort /* IS_PASS_MIDDLE */
466*b6f0c436SThomas Cort { Itrue, Ifalse,Fpass, Ftrue, Ffalse,Pelif, Mtrue, Delif, Pelse, Pendif,
467*b6f0c436SThomas Cort   Oiffy, Oiffy, Fpass, Oif,   Oif,   Pelif, Oelif, Oelif, Pelse, Pendif,
468*b6f0c436SThomas Cort   print, Eeof },
469*b6f0c436SThomas Cort /* IS_FALSE_MIDDLE */
470*b6f0c436SThomas Cort { Idrop, Idrop, Fdrop, Fdrop, Fdrop, Pelif, Mtrue, Delif, Pelse, Pendif,
471*b6f0c436SThomas Cort   Idrop, Idrop, Fdrop, Fdrop, Fdrop, Eioccc,Eioccc,Eioccc,Eioccc,Eioccc,
472*b6f0c436SThomas Cort   drop,  Eeof },
473*b6f0c436SThomas Cort /* IS_TRUE_MIDDLE */
474*b6f0c436SThomas Cort { Itrue, Ifalse,Fpass, Ftrue, Ffalse,Melif, Melif, Melif, Melse, Pendif,
475*b6f0c436SThomas Cort   Oiffy, Oiffy, Fpass, Oif,   Oif,   Eioccc,Eioccc,Eioccc,Eioccc,Pendif,
476*b6f0c436SThomas Cort   print, Eeof },
477*b6f0c436SThomas Cort /* IS_PASS_ELSE */
478*b6f0c436SThomas Cort { Itrue, Ifalse,Fpass, Ftrue, Ffalse,Eelif, Eelif, Eelif, Eelse, Pendif,
479*b6f0c436SThomas Cort   Oiffy, Oiffy, Fpass, Oif,   Oif,   Eelif, Eelif, Eelif, Eelse, Pendif,
480*b6f0c436SThomas Cort   print, Eeof },
481*b6f0c436SThomas Cort /* IS_FALSE_ELSE */
482*b6f0c436SThomas Cort { Idrop, Idrop, Fdrop, Fdrop, Fdrop, Eelif, Eelif, Eelif, Eelse, Dendif,
483*b6f0c436SThomas Cort   Idrop, Idrop, Fdrop, Fdrop, Fdrop, Eelif, Eelif, Eelif, Eelse, Eioccc,
484*b6f0c436SThomas Cort   drop,  Eeof },
485*b6f0c436SThomas Cort /* IS_TRUE_ELSE */
486*b6f0c436SThomas Cort { Itrue, Ifalse,Fpass, Ftrue, Ffalse,Eelif, Eelif, Eelif, Eelse, Dendif,
487*b6f0c436SThomas Cort   Oiffy, Oiffy, Fpass, Oif,   Oif,   Eelif, Eelif, Eelif, Eelse, Eioccc,
488*b6f0c436SThomas Cort   print, Eeof },
489*b6f0c436SThomas Cort /* IS_FALSE_TRAILER */
490*b6f0c436SThomas Cort { Idrop, Idrop, Fdrop, Fdrop, Fdrop, Dfalse,Dfalse,Dfalse,Delse, Dendif,
491*b6f0c436SThomas Cort   Idrop, Idrop, Fdrop, Fdrop, Fdrop, Dfalse,Dfalse,Dfalse,Delse, Eioccc,
492*b6f0c436SThomas Cort   drop,  Eeof }
493*b6f0c436SThomas Cort /*TRUEI  FALSEI IF     TRUE   FALSE  ELIF   ELTRUE ELFALSE ELSE  ENDIF
494*b6f0c436SThomas Cort   TRUEI  FALSEI IF     TRUE   FALSE  ELIF   ELTRUE ELFALSE ELSE  ENDIF (DODGY)
495*b6f0c436SThomas Cort   PLAIN  EOF */
496*b6f0c436SThomas Cort };
497*b6f0c436SThomas Cort 
498*b6f0c436SThomas Cort /*
499*b6f0c436SThomas Cort  * State machine utility functions
500*b6f0c436SThomas Cort  */
501*b6f0c436SThomas Cort static void
done(void)502*b6f0c436SThomas Cort done(void)
503*b6f0c436SThomas Cort {
504*b6f0c436SThomas Cort 	if (incomment)
505*b6f0c436SThomas Cort 		error("EOF in comment");
506*b6f0c436SThomas Cort 	if (fclose(output)) {
507*b6f0c436SThomas Cort 		if (overwriting) {
508*b6f0c436SThomas Cort 			unlink(tmpname);
509*b6f0c436SThomas Cort 			errx(2, "%s unchanged", ofilename);
510*b6f0c436SThomas Cort 		}
511*b6f0c436SThomas Cort 	}
512*b6f0c436SThomas Cort 	if (overwriting && rename(tmpname, ofilename)) {
513*b6f0c436SThomas Cort 		unlink(tmpname);
514*b6f0c436SThomas Cort 		errx(2, "%s unchanged", ofilename);
515*b6f0c436SThomas Cort 	}
516*b6f0c436SThomas Cort 	exit(exitstat);
517*b6f0c436SThomas Cort }
518*b6f0c436SThomas Cort static void
ignoreoff(void)519*b6f0c436SThomas Cort ignoreoff(void)
520*b6f0c436SThomas Cort {
521*b6f0c436SThomas Cort 	ignoring[depth] = ignoring[depth-1];
522*b6f0c436SThomas Cort }
523*b6f0c436SThomas Cort static void
ignoreon(void)524*b6f0c436SThomas Cort ignoreon(void)
525*b6f0c436SThomas Cort {
526*b6f0c436SThomas Cort 	ignoring[depth] = true;
527*b6f0c436SThomas Cort }
528*b6f0c436SThomas Cort static void
keywordedit(const char * replacement)529*b6f0c436SThomas Cort keywordedit(const char *replacement)
530*b6f0c436SThomas Cort {
531*b6f0c436SThomas Cort 	strlcpy(keyword, replacement, tline + sizeof(tline) - keyword);
532*b6f0c436SThomas Cort 	print();
533*b6f0c436SThomas Cort }
534*b6f0c436SThomas Cort static void
nest(void)535*b6f0c436SThomas Cort nest(void)
536*b6f0c436SThomas Cort {
537*b6f0c436SThomas Cort 	depth += 1;
538*b6f0c436SThomas Cort 	if (depth >= MAXDEPTH)
539*b6f0c436SThomas Cort 		error("Too many levels of nesting");
540*b6f0c436SThomas Cort 	stifline[depth] = linenum;
541*b6f0c436SThomas Cort }
542*b6f0c436SThomas Cort static void
state(Ifstate is)543*b6f0c436SThomas Cort state(Ifstate is)
544*b6f0c436SThomas Cort {
545*b6f0c436SThomas Cort 	ifstate[depth] = is;
546*b6f0c436SThomas Cort }
547*b6f0c436SThomas Cort 
548*b6f0c436SThomas Cort /*
549*b6f0c436SThomas Cort  * Write a line to the output or not, according to command line options.
550*b6f0c436SThomas Cort  */
551*b6f0c436SThomas Cort static void
flushline(bool keep)552*b6f0c436SThomas Cort flushline(bool keep)
553*b6f0c436SThomas Cort {
554*b6f0c436SThomas Cort 	if (symlist)
555*b6f0c436SThomas Cort 		return;
556*b6f0c436SThomas Cort 	if (keep ^ complement)
557*b6f0c436SThomas Cort 		fputs(tline, output);
558*b6f0c436SThomas Cort 	else {
559*b6f0c436SThomas Cort 		if (lnblank)
560*b6f0c436SThomas Cort 			putc('\n', output);
561*b6f0c436SThomas Cort 		exitstat = 1;
562*b6f0c436SThomas Cort 	}
563*b6f0c436SThomas Cort }
564*b6f0c436SThomas Cort 
565*b6f0c436SThomas Cort /*
566*b6f0c436SThomas Cort  * The driver for the state machine.
567*b6f0c436SThomas Cort  */
568*b6f0c436SThomas Cort static void
process(void)569*b6f0c436SThomas Cort process(void)
570*b6f0c436SThomas Cort {
571*b6f0c436SThomas Cort 	Linetype lineval;
572*b6f0c436SThomas Cort 
573*b6f0c436SThomas Cort 	for (;;) {
574*b6f0c436SThomas Cort 		linenum++;
575*b6f0c436SThomas Cort 		lineval = get_line();
576*b6f0c436SThomas Cort 		trans_table[ifstate[depth]][lineval]();
577*b6f0c436SThomas Cort 		debug("process %s -> %s depth %d",
578*b6f0c436SThomas Cort 		    linetype_name[lineval],
579*b6f0c436SThomas Cort 		    ifstate_name[ifstate[depth]], depth);
580*b6f0c436SThomas Cort 	}
581*b6f0c436SThomas Cort }
582*b6f0c436SThomas Cort 
583*b6f0c436SThomas Cort /*
584*b6f0c436SThomas Cort  * Parse a line and determine its type. We keep the preprocessor line
585*b6f0c436SThomas Cort  * parser state between calls in the global variable linestate, with
586*b6f0c436SThomas Cort  * help from skipcomment().
587*b6f0c436SThomas Cort  */
588*b6f0c436SThomas Cort static Linetype
get_line(void)589*b6f0c436SThomas Cort get_line(void)
590*b6f0c436SThomas Cort {
591*b6f0c436SThomas Cort 	const char *cp;
592*b6f0c436SThomas Cort 	int cursym;
593*b6f0c436SThomas Cort 	int kwlen;
594*b6f0c436SThomas Cort 	Linetype retval;
595*b6f0c436SThomas Cort 	Comment_state wascomment;
596*b6f0c436SThomas Cort 
597*b6f0c436SThomas Cort 	if (fgets(tline, MAXLINE, input) == NULL)
598*b6f0c436SThomas Cort 		return (LT_EOF);
599*b6f0c436SThomas Cort 	retval = LT_PLAIN;
600*b6f0c436SThomas Cort 	wascomment = incomment;
601*b6f0c436SThomas Cort 	cp = skipcomment(tline);
602*b6f0c436SThomas Cort 	if (linestate == LS_START) {
603*b6f0c436SThomas Cort 		if (*cp == '#') {
604*b6f0c436SThomas Cort 			linestate = LS_HASH;
605*b6f0c436SThomas Cort 			cp = skipcomment(cp + 1);
606*b6f0c436SThomas Cort 		} else if (*cp != '\0')
607*b6f0c436SThomas Cort 			linestate = LS_DIRTY;
608*b6f0c436SThomas Cort 	}
609*b6f0c436SThomas Cort 	if (!incomment && linestate == LS_HASH) {
610*b6f0c436SThomas Cort 		keyword = tline + (cp - tline);
611*b6f0c436SThomas Cort 		cp = skipsym(cp);
612*b6f0c436SThomas Cort 		kwlen = cp - keyword;
613*b6f0c436SThomas Cort 		/* no way can we deal with a continuation inside a keyword */
614*b6f0c436SThomas Cort 		if (strncmp(cp, "\\\n", 2) == 0)
615*b6f0c436SThomas Cort 			Eioccc();
616*b6f0c436SThomas Cort 		if (strlcmp("ifdef", keyword, kwlen) == 0 ||
617*b6f0c436SThomas Cort 		    strlcmp("ifndef", keyword, kwlen) == 0) {
618*b6f0c436SThomas Cort 			cp = skipcomment(cp);
619*b6f0c436SThomas Cort 			if ((cursym = findsym(cp)) < 0)
620*b6f0c436SThomas Cort 				retval = LT_IF;
621*b6f0c436SThomas Cort 			else {
622*b6f0c436SThomas Cort 				retval = (keyword[2] == 'n')
623*b6f0c436SThomas Cort 				    ? LT_FALSE : LT_TRUE;
624*b6f0c436SThomas Cort 				if (value[cursym] == NULL)
625*b6f0c436SThomas Cort 					retval = (retval == LT_TRUE)
626*b6f0c436SThomas Cort 					    ? LT_FALSE : LT_TRUE;
627*b6f0c436SThomas Cort 				if (ignore[cursym])
628*b6f0c436SThomas Cort 					retval = (retval == LT_TRUE)
629*b6f0c436SThomas Cort 					    ? LT_TRUEI : LT_FALSEI;
630*b6f0c436SThomas Cort 			}
631*b6f0c436SThomas Cort 			cp = skipsym(cp);
632*b6f0c436SThomas Cort 		} else if (strlcmp("if", keyword, kwlen) == 0)
633*b6f0c436SThomas Cort 			retval = ifeval(&cp);
634*b6f0c436SThomas Cort 		else if (strlcmp("elif", keyword, kwlen) == 0)
635*b6f0c436SThomas Cort 			retval = ifeval(&cp) - LT_IF + LT_ELIF;
636*b6f0c436SThomas Cort 		else if (strlcmp("else", keyword, kwlen) == 0)
637*b6f0c436SThomas Cort 			retval = LT_ELSE;
638*b6f0c436SThomas Cort 		else if (strlcmp("endif", keyword, kwlen) == 0)
639*b6f0c436SThomas Cort 			retval = LT_ENDIF;
640*b6f0c436SThomas Cort 		else {
641*b6f0c436SThomas Cort 			linestate = LS_DIRTY;
642*b6f0c436SThomas Cort 			retval = LT_PLAIN;
643*b6f0c436SThomas Cort 		}
644*b6f0c436SThomas Cort 		cp = skipcomment(cp);
645*b6f0c436SThomas Cort 		if (*cp != '\0') {
646*b6f0c436SThomas Cort 			linestate = LS_DIRTY;
647*b6f0c436SThomas Cort 			if (retval == LT_TRUE || retval == LT_FALSE ||
648*b6f0c436SThomas Cort 			    retval == LT_TRUEI || retval == LT_FALSEI)
649*b6f0c436SThomas Cort 				retval = LT_IF;
650*b6f0c436SThomas Cort 			if (retval == LT_ELTRUE || retval == LT_ELFALSE)
651*b6f0c436SThomas Cort 				retval = LT_ELIF;
652*b6f0c436SThomas Cort 		}
653*b6f0c436SThomas Cort 		if (retval != LT_PLAIN && (wascomment || incomment)) {
654*b6f0c436SThomas Cort 			retval += LT_DODGY;
655*b6f0c436SThomas Cort 			if (incomment)
656*b6f0c436SThomas Cort 				linestate = LS_DIRTY;
657*b6f0c436SThomas Cort 		}
658*b6f0c436SThomas Cort 	}
659*b6f0c436SThomas Cort 	if (linestate == LS_DIRTY) {
660*b6f0c436SThomas Cort 		while (*cp != '\0')
661*b6f0c436SThomas Cort 			cp = skipcomment(cp + 1);
662*b6f0c436SThomas Cort 	}
663*b6f0c436SThomas Cort 	debug("parser %s comment %s line",
664*b6f0c436SThomas Cort 	    comment_name[incomment], linestate_name[linestate]);
665*b6f0c436SThomas Cort 	return (retval);
666*b6f0c436SThomas Cort }
667*b6f0c436SThomas Cort 
668*b6f0c436SThomas Cort /*
669*b6f0c436SThomas Cort  * These are the binary operators that are supported by the expression
670*b6f0c436SThomas Cort  * evaluator. Note that if support for division is added then we also
671*b6f0c436SThomas Cort  * need short-circuiting booleans because of divide-by-zero.
672*b6f0c436SThomas Cort  */
op_lt(int a,int b)673*b6f0c436SThomas Cort static int op_lt(int a, int b) { return (a < b); }
op_gt(int a,int b)674*b6f0c436SThomas Cort static int op_gt(int a, int b) { return (a > b); }
op_le(int a,int b)675*b6f0c436SThomas Cort static int op_le(int a, int b) { return (a <= b); }
op_ge(int a,int b)676*b6f0c436SThomas Cort static int op_ge(int a, int b) { return (a >= b); }
op_eq(int a,int b)677*b6f0c436SThomas Cort static int op_eq(int a, int b) { return (a == b); }
op_ne(int a,int b)678*b6f0c436SThomas Cort static int op_ne(int a, int b) { return (a != b); }
op_or(int a,int b)679*b6f0c436SThomas Cort static int op_or(int a, int b) { return (a || b); }
op_and(int a,int b)680*b6f0c436SThomas Cort static int op_and(int a, int b) { return (a && b); }
681*b6f0c436SThomas Cort 
682*b6f0c436SThomas Cort /*
683*b6f0c436SThomas Cort  * An evaluation function takes three arguments, as follows: (1) a pointer to
684*b6f0c436SThomas Cort  * an element of the precedence table which lists the operators at the current
685*b6f0c436SThomas Cort  * level of precedence; (2) a pointer to an integer which will receive the
686*b6f0c436SThomas Cort  * value of the expression; and (3) a pointer to a char* that points to the
687*b6f0c436SThomas Cort  * expression to be evaluated and that is updated to the end of the expression
688*b6f0c436SThomas Cort  * when evaluation is complete. The function returns LT_FALSE if the value of
689*b6f0c436SThomas Cort  * the expression is zero, LT_TRUE if it is non-zero, or LT_IF if the
690*b6f0c436SThomas Cort  * expression could not be evaluated.
691*b6f0c436SThomas Cort  */
692*b6f0c436SThomas Cort struct ops;
693*b6f0c436SThomas Cort 
694*b6f0c436SThomas Cort typedef Linetype eval_fn(const struct ops *, int *, const char **);
695*b6f0c436SThomas Cort 
696*b6f0c436SThomas Cort static eval_fn eval_table, eval_unary;
697*b6f0c436SThomas Cort 
698*b6f0c436SThomas Cort /*
699*b6f0c436SThomas Cort  * The precedence table. Expressions involving binary operators are evaluated
700*b6f0c436SThomas Cort  * in a table-driven way by eval_table. When it evaluates a subexpression it
701*b6f0c436SThomas Cort  * calls the inner function with its first argument pointing to the next
702*b6f0c436SThomas Cort  * element of the table. Innermost expressions have special non-table-driven
703*b6f0c436SThomas Cort  * handling.
704*b6f0c436SThomas Cort  */
705*b6f0c436SThomas Cort static const struct ops {
706*b6f0c436SThomas Cort 	eval_fn *inner;
707*b6f0c436SThomas Cort 	struct op {
708*b6f0c436SThomas Cort 		const char *str;
709*b6f0c436SThomas Cort 		int (*fn)(int, int);
710*b6f0c436SThomas Cort 	} op[5];
711*b6f0c436SThomas Cort } eval_ops[] = {
712*b6f0c436SThomas Cort 	{ eval_table, { { "||", op_or } } },
713*b6f0c436SThomas Cort 	{ eval_table, { { "&&", op_and } } },
714*b6f0c436SThomas Cort 	{ eval_table, { { "==", op_eq },
715*b6f0c436SThomas Cort 			{ "!=", op_ne } } },
716*b6f0c436SThomas Cort 	{ eval_unary, { { "<=", op_le },
717*b6f0c436SThomas Cort 			{ ">=", op_ge },
718*b6f0c436SThomas Cort 			{ "<", op_lt },
719*b6f0c436SThomas Cort 			{ ">", op_gt } } }
720*b6f0c436SThomas Cort };
721*b6f0c436SThomas Cort 
722*b6f0c436SThomas Cort /*
723*b6f0c436SThomas Cort  * Function for evaluating the innermost parts of expressions,
724*b6f0c436SThomas Cort  * viz. !expr (expr) defined(symbol) symbol number
725*b6f0c436SThomas Cort  * We reset the keepthis flag when we find a non-constant subexpression.
726*b6f0c436SThomas Cort  */
727*b6f0c436SThomas Cort static Linetype
eval_unary(const struct ops * ops,int * valp,const char ** cpp)728*b6f0c436SThomas Cort eval_unary(const struct ops *ops, int *valp, const char **cpp)
729*b6f0c436SThomas Cort {
730*b6f0c436SThomas Cort 	const char *cp;
731*b6f0c436SThomas Cort 	char *ep;
732*b6f0c436SThomas Cort 	int sym;
733*b6f0c436SThomas Cort 
734*b6f0c436SThomas Cort 	cp = skipcomment(*cpp);
735*b6f0c436SThomas Cort 	if (*cp == '!') {
736*b6f0c436SThomas Cort 		debug("eval%td !", ops - eval_ops);
737*b6f0c436SThomas Cort 		cp++;
738*b6f0c436SThomas Cort 		if (eval_unary(ops, valp, &cp) == LT_IF)
739*b6f0c436SThomas Cort 			return (LT_IF);
740*b6f0c436SThomas Cort 		*valp = !*valp;
741*b6f0c436SThomas Cort 	} else if (*cp == '(') {
742*b6f0c436SThomas Cort 		cp++;
743*b6f0c436SThomas Cort 		debug("eval%td (", ops - eval_ops);
744*b6f0c436SThomas Cort 		if (eval_table(eval_ops, valp, &cp) == LT_IF)
745*b6f0c436SThomas Cort 			return (LT_IF);
746*b6f0c436SThomas Cort 		cp = skipcomment(cp);
747*b6f0c436SThomas Cort 		if (*cp++ != ')')
748*b6f0c436SThomas Cort 			return (LT_IF);
749*b6f0c436SThomas Cort 	} else if (isdigit((unsigned char)*cp)) {
750*b6f0c436SThomas Cort 		debug("eval%td number", ops - eval_ops);
751*b6f0c436SThomas Cort 		*valp = strtol(cp, &ep, 0);
752*b6f0c436SThomas Cort 		cp = skipsym(cp);
753*b6f0c436SThomas Cort 	} else if (strncmp(cp, "defined", 7) == 0 && endsym(cp[7])) {
754*b6f0c436SThomas Cort 		cp = skipcomment(cp+7);
755*b6f0c436SThomas Cort 		debug("eval%td defined", ops - eval_ops);
756*b6f0c436SThomas Cort 		if (*cp++ != '(')
757*b6f0c436SThomas Cort 			return (LT_IF);
758*b6f0c436SThomas Cort 		cp = skipcomment(cp);
759*b6f0c436SThomas Cort 		sym = findsym(cp);
760*b6f0c436SThomas Cort 		if (sym < 0 || symlist)
761*b6f0c436SThomas Cort 			return (LT_IF);
762*b6f0c436SThomas Cort 		*valp = (value[sym] != NULL);
763*b6f0c436SThomas Cort 		cp = skipsym(cp);
764*b6f0c436SThomas Cort 		cp = skipcomment(cp);
765*b6f0c436SThomas Cort 		if (*cp++ != ')')
766*b6f0c436SThomas Cort 			return (LT_IF);
767*b6f0c436SThomas Cort 		keepthis = false;
768*b6f0c436SThomas Cort 	} else if (!endsym(*cp)) {
769*b6f0c436SThomas Cort 		debug("eval%td symbol", ops - eval_ops);
770*b6f0c436SThomas Cort 		sym = findsym(cp);
771*b6f0c436SThomas Cort 		if (sym < 0 || symlist)
772*b6f0c436SThomas Cort 			return (LT_IF);
773*b6f0c436SThomas Cort 		if (value[sym] == NULL)
774*b6f0c436SThomas Cort 			*valp = 0;
775*b6f0c436SThomas Cort 		else {
776*b6f0c436SThomas Cort 			*valp = strtol(value[sym], &ep, 0);
777*b6f0c436SThomas Cort 			if (*ep != '\0' || ep == value[sym])
778*b6f0c436SThomas Cort 				return (LT_IF);
779*b6f0c436SThomas Cort 		}
780*b6f0c436SThomas Cort 		cp = skipsym(cp);
781*b6f0c436SThomas Cort 		keepthis = false;
782*b6f0c436SThomas Cort 	} else {
783*b6f0c436SThomas Cort 		debug("eval%td bad expr", ops - eval_ops);
784*b6f0c436SThomas Cort 		return (LT_IF);
785*b6f0c436SThomas Cort 	}
786*b6f0c436SThomas Cort 
787*b6f0c436SThomas Cort 	*cpp = cp;
788*b6f0c436SThomas Cort 	debug("eval%td = %d", ops - eval_ops, *valp);
789*b6f0c436SThomas Cort 	return (*valp ? LT_TRUE : LT_FALSE);
790*b6f0c436SThomas Cort }
791*b6f0c436SThomas Cort 
792*b6f0c436SThomas Cort /*
793*b6f0c436SThomas Cort  * Table-driven evaluation of binary operators.
794*b6f0c436SThomas Cort  */
795*b6f0c436SThomas Cort static Linetype
eval_table(const struct ops * ops,int * valp,const char ** cpp)796*b6f0c436SThomas Cort eval_table(const struct ops *ops, int *valp, const char **cpp)
797*b6f0c436SThomas Cort {
798*b6f0c436SThomas Cort 	const struct op *op;
799*b6f0c436SThomas Cort 	const char *cp;
800*b6f0c436SThomas Cort 	int val;
801*b6f0c436SThomas Cort 
802*b6f0c436SThomas Cort 	debug("eval%td", ops - eval_ops);
803*b6f0c436SThomas Cort 	cp = *cpp;
804*b6f0c436SThomas Cort 	if (ops->inner(ops+1, valp, &cp) == LT_IF)
805*b6f0c436SThomas Cort 		return (LT_IF);
806*b6f0c436SThomas Cort 	for (;;) {
807*b6f0c436SThomas Cort 		cp = skipcomment(cp);
808*b6f0c436SThomas Cort 		for (op = ops->op; op->str != NULL; op++)
809*b6f0c436SThomas Cort 			if (strncmp(cp, op->str, strlen(op->str)) == 0)
810*b6f0c436SThomas Cort 				break;
811*b6f0c436SThomas Cort 		if (op->str == NULL)
812*b6f0c436SThomas Cort 			break;
813*b6f0c436SThomas Cort 		cp += strlen(op->str);
814*b6f0c436SThomas Cort 		debug("eval%td %s", ops - eval_ops, op->str);
815*b6f0c436SThomas Cort 		if (ops->inner(ops+1, &val, &cp) == LT_IF)
816*b6f0c436SThomas Cort 			return (LT_IF);
817*b6f0c436SThomas Cort 		*valp = op->fn(*valp, val);
818*b6f0c436SThomas Cort 	}
819*b6f0c436SThomas Cort 
820*b6f0c436SThomas Cort 	*cpp = cp;
821*b6f0c436SThomas Cort 	debug("eval%td = %d", ops - eval_ops, *valp);
822*b6f0c436SThomas Cort 	return (*valp ? LT_TRUE : LT_FALSE);
823*b6f0c436SThomas Cort }
824*b6f0c436SThomas Cort 
825*b6f0c436SThomas Cort /*
826*b6f0c436SThomas Cort  * Evaluate the expression on a #if or #elif line. If we can work out
827*b6f0c436SThomas Cort  * the result we return LT_TRUE or LT_FALSE accordingly, otherwise we
828*b6f0c436SThomas Cort  * return just a generic LT_IF.
829*b6f0c436SThomas Cort  */
830*b6f0c436SThomas Cort static Linetype
ifeval(const char ** cpp)831*b6f0c436SThomas Cort ifeval(const char **cpp)
832*b6f0c436SThomas Cort {
833*b6f0c436SThomas Cort 	int ret;
834*b6f0c436SThomas Cort 	int val;
835*b6f0c436SThomas Cort 
836*b6f0c436SThomas Cort 	debug("eval %s", *cpp);
837*b6f0c436SThomas Cort 	keepthis = killconsts ? false : true;
838*b6f0c436SThomas Cort 	ret = eval_table(eval_ops, &val, cpp);
839*b6f0c436SThomas Cort 	debug("eval = %d", val);
840*b6f0c436SThomas Cort 	return (keepthis ? LT_IF : ret);
841*b6f0c436SThomas Cort }
842*b6f0c436SThomas Cort 
843*b6f0c436SThomas Cort /*
844*b6f0c436SThomas Cort  * Skip over comments and stop at the next character position that is
845*b6f0c436SThomas Cort  * not whitespace. Between calls we keep the comment state in the
846*b6f0c436SThomas Cort  * global variable incomment, and we also adjust the global variable
847*b6f0c436SThomas Cort  * linestate when we see a newline.
848*b6f0c436SThomas Cort  * XXX: doesn't cope with the buffer splitting inside a state transition.
849*b6f0c436SThomas Cort  */
850*b6f0c436SThomas Cort static const char *
skipcomment(const char * cp)851*b6f0c436SThomas Cort skipcomment(const char *cp)
852*b6f0c436SThomas Cort {
853*b6f0c436SThomas Cort 	if (text || ignoring[depth]) {
854*b6f0c436SThomas Cort 		for (; isspace((unsigned char)*cp); cp++)
855*b6f0c436SThomas Cort 			if (*cp == '\n')
856*b6f0c436SThomas Cort 				linestate = LS_START;
857*b6f0c436SThomas Cort 		return (cp);
858*b6f0c436SThomas Cort 	}
859*b6f0c436SThomas Cort 	while (*cp != '\0')
860*b6f0c436SThomas Cort 		/* don't reset to LS_START after a line continuation */
861*b6f0c436SThomas Cort 		if (strncmp(cp, "\\\n", 2) == 0)
862*b6f0c436SThomas Cort 			cp += 2;
863*b6f0c436SThomas Cort 		else switch (incomment) {
864*b6f0c436SThomas Cort 		case NO_COMMENT:
865*b6f0c436SThomas Cort 			if (strncmp(cp, "/\\\n", 3) == 0) {
866*b6f0c436SThomas Cort 				incomment = STARTING_COMMENT;
867*b6f0c436SThomas Cort 				cp += 3;
868*b6f0c436SThomas Cort 			} else if (strncmp(cp, "/*", 2) == 0) {
869*b6f0c436SThomas Cort 				incomment = C_COMMENT;
870*b6f0c436SThomas Cort 				cp += 2;
871*b6f0c436SThomas Cort 			} else if (strncmp(cp, "//", 2) == 0) {
872*b6f0c436SThomas Cort 				incomment = CXX_COMMENT;
873*b6f0c436SThomas Cort 				cp += 2;
874*b6f0c436SThomas Cort 			} else if (strncmp(cp, "\n", 1) == 0) {
875*b6f0c436SThomas Cort 				linestate = LS_START;
876*b6f0c436SThomas Cort 				cp += 1;
877*b6f0c436SThomas Cort 			} else if (strchr(" \t", *cp) != NULL) {
878*b6f0c436SThomas Cort 				cp += 1;
879*b6f0c436SThomas Cort 			} else
880*b6f0c436SThomas Cort 				return (cp);
881*b6f0c436SThomas Cort 			continue;
882*b6f0c436SThomas Cort 		case CXX_COMMENT:
883*b6f0c436SThomas Cort 			if (strncmp(cp, "\n", 1) == 0) {
884*b6f0c436SThomas Cort 				incomment = NO_COMMENT;
885*b6f0c436SThomas Cort 				linestate = LS_START;
886*b6f0c436SThomas Cort 			}
887*b6f0c436SThomas Cort 			cp += 1;
888*b6f0c436SThomas Cort 			continue;
889*b6f0c436SThomas Cort 		case C_COMMENT:
890*b6f0c436SThomas Cort 			if (strncmp(cp, "*\\\n", 3) == 0) {
891*b6f0c436SThomas Cort 				incomment = FINISHING_COMMENT;
892*b6f0c436SThomas Cort 				cp += 3;
893*b6f0c436SThomas Cort 			} else if (strncmp(cp, "*/", 2) == 0) {
894*b6f0c436SThomas Cort 				incomment = NO_COMMENT;
895*b6f0c436SThomas Cort 				cp += 2;
896*b6f0c436SThomas Cort 			} else
897*b6f0c436SThomas Cort 				cp += 1;
898*b6f0c436SThomas Cort 			continue;
899*b6f0c436SThomas Cort 		case STARTING_COMMENT:
900*b6f0c436SThomas Cort 			if (*cp == '*') {
901*b6f0c436SThomas Cort 				incomment = C_COMMENT;
902*b6f0c436SThomas Cort 				cp += 1;
903*b6f0c436SThomas Cort 			} else if (*cp == '/') {
904*b6f0c436SThomas Cort 				incomment = CXX_COMMENT;
905*b6f0c436SThomas Cort 				cp += 1;
906*b6f0c436SThomas Cort 			} else {
907*b6f0c436SThomas Cort 				incomment = NO_COMMENT;
908*b6f0c436SThomas Cort 				linestate = LS_DIRTY;
909*b6f0c436SThomas Cort 			}
910*b6f0c436SThomas Cort 			continue;
911*b6f0c436SThomas Cort 		case FINISHING_COMMENT:
912*b6f0c436SThomas Cort 			if (*cp == '/') {
913*b6f0c436SThomas Cort 				incomment = NO_COMMENT;
914*b6f0c436SThomas Cort 				cp += 1;
915*b6f0c436SThomas Cort 			} else
916*b6f0c436SThomas Cort 				incomment = C_COMMENT;
917*b6f0c436SThomas Cort 			continue;
918*b6f0c436SThomas Cort 		default:
919*b6f0c436SThomas Cort 			abort(); /* bug */
920*b6f0c436SThomas Cort 		}
921*b6f0c436SThomas Cort 	return (cp);
922*b6f0c436SThomas Cort }
923*b6f0c436SThomas Cort 
924*b6f0c436SThomas Cort /*
925*b6f0c436SThomas Cort  * Skip over an identifier.
926*b6f0c436SThomas Cort  */
927*b6f0c436SThomas Cort static const char *
skipsym(const char * cp)928*b6f0c436SThomas Cort skipsym(const char *cp)
929*b6f0c436SThomas Cort {
930*b6f0c436SThomas Cort 	while (!endsym(*cp))
931*b6f0c436SThomas Cort 		++cp;
932*b6f0c436SThomas Cort 	return (cp);
933*b6f0c436SThomas Cort }
934*b6f0c436SThomas Cort 
935*b6f0c436SThomas Cort /*
936*b6f0c436SThomas Cort  * Look for the symbol in the symbol table. If it is found, we return
937*b6f0c436SThomas Cort  * the symbol table index, else we return -1.
938*b6f0c436SThomas Cort  */
939*b6f0c436SThomas Cort static int
findsym(const char * str)940*b6f0c436SThomas Cort findsym(const char *str)
941*b6f0c436SThomas Cort {
942*b6f0c436SThomas Cort 	const char *cp;
943*b6f0c436SThomas Cort 	int symind;
944*b6f0c436SThomas Cort 
945*b6f0c436SThomas Cort 	cp = skipsym(str);
946*b6f0c436SThomas Cort 	if (cp == str)
947*b6f0c436SThomas Cort 		return (-1);
948*b6f0c436SThomas Cort 	if (symlist)
949*b6f0c436SThomas Cort 		printf("%.*s\n", (int)(cp-str), str);
950*b6f0c436SThomas Cort 	for (symind = 0; symind < nsyms; ++symind) {
951*b6f0c436SThomas Cort 		if (strlcmp(symname[symind], str, cp-str) == 0) {
952*b6f0c436SThomas Cort 			debug("findsym %s %s", symname[symind],
953*b6f0c436SThomas Cort 			    value[symind] ? value[symind] : "");
954*b6f0c436SThomas Cort 			return (symind);
955*b6f0c436SThomas Cort 		}
956*b6f0c436SThomas Cort 	}
957*b6f0c436SThomas Cort 	return (-1);
958*b6f0c436SThomas Cort }
959*b6f0c436SThomas Cort 
960*b6f0c436SThomas Cort /*
961*b6f0c436SThomas Cort  * Add a symbol to the symbol table.
962*b6f0c436SThomas Cort  */
963*b6f0c436SThomas Cort static void
addsym(bool ignorethis,bool definethis,char * sym)964*b6f0c436SThomas Cort addsym(bool ignorethis, bool definethis, char *sym)
965*b6f0c436SThomas Cort {
966*b6f0c436SThomas Cort 	int symind;
967*b6f0c436SThomas Cort 	char *val;
968*b6f0c436SThomas Cort 
969*b6f0c436SThomas Cort 	symind = findsym(sym);
970*b6f0c436SThomas Cort 	if (symind < 0) {
971*b6f0c436SThomas Cort 		if (nsyms >= MAXSYMS)
972*b6f0c436SThomas Cort 			errx(2, "too many symbols");
973*b6f0c436SThomas Cort 		symind = nsyms++;
974*b6f0c436SThomas Cort 	}
975*b6f0c436SThomas Cort 	symname[symind] = sym;
976*b6f0c436SThomas Cort 	ignore[symind] = ignorethis;
977*b6f0c436SThomas Cort 	val = sym + (skipsym(sym) - sym);
978*b6f0c436SThomas Cort 	if (definethis) {
979*b6f0c436SThomas Cort 		if (*val == '=') {
980*b6f0c436SThomas Cort 			value[symind] = val+1;
981*b6f0c436SThomas Cort 			*val = '\0';
982*b6f0c436SThomas Cort 		} else if (*val == '\0')
983*b6f0c436SThomas Cort 			value[symind] = "";
984*b6f0c436SThomas Cort 		else
985*b6f0c436SThomas Cort 			usage();
986*b6f0c436SThomas Cort 	} else {
987*b6f0c436SThomas Cort 		if (*val != '\0')
988*b6f0c436SThomas Cort 			usage();
989*b6f0c436SThomas Cort 		value[symind] = NULL;
990*b6f0c436SThomas Cort 	}
991*b6f0c436SThomas Cort }
992*b6f0c436SThomas Cort 
993*b6f0c436SThomas Cort /*
994*b6f0c436SThomas Cort  * Compare s with n characters of t.
995*b6f0c436SThomas Cort  * The same as strncmp() except that it checks that s[n] == '\0'.
996*b6f0c436SThomas Cort  */
997*b6f0c436SThomas Cort static int
strlcmp(const char * s,const char * t,size_t n)998*b6f0c436SThomas Cort strlcmp(const char *s, const char *t, size_t n)
999*b6f0c436SThomas Cort {
1000*b6f0c436SThomas Cort 	while (n-- && *t != '\0')
1001*b6f0c436SThomas Cort 		if (*s != *t)
1002*b6f0c436SThomas Cort 			return ((unsigned char)*s - (unsigned char)*t);
1003*b6f0c436SThomas Cort 		else
1004*b6f0c436SThomas Cort 			++s, ++t;
1005*b6f0c436SThomas Cort 	return ((unsigned char)*s);
1006*b6f0c436SThomas Cort }
1007*b6f0c436SThomas Cort 
1008*b6f0c436SThomas Cort /*
1009*b6f0c436SThomas Cort  * Diagnostics.
1010*b6f0c436SThomas Cort  */
1011*b6f0c436SThomas Cort static void
debug(const char * msg,...)1012*b6f0c436SThomas Cort debug(const char *msg, ...)
1013*b6f0c436SThomas Cort {
1014*b6f0c436SThomas Cort 	va_list ap;
1015*b6f0c436SThomas Cort 
1016*b6f0c436SThomas Cort 	if (debugging) {
1017*b6f0c436SThomas Cort 		va_start(ap, msg);
1018*b6f0c436SThomas Cort 		vwarnx(msg, ap);
1019*b6f0c436SThomas Cort 		va_end(ap);
1020*b6f0c436SThomas Cort 	}
1021*b6f0c436SThomas Cort }
1022*b6f0c436SThomas Cort 
1023*b6f0c436SThomas Cort static void
error(const char * msg)1024*b6f0c436SThomas Cort error(const char *msg)
1025*b6f0c436SThomas Cort {
1026*b6f0c436SThomas Cort 	if (depth == 0)
1027*b6f0c436SThomas Cort 		warnx("%s: %d: %s", filename, linenum, msg);
1028*b6f0c436SThomas Cort 	else
1029*b6f0c436SThomas Cort 		warnx("%s: %d: %s (#if line %d depth %d)",
1030*b6f0c436SThomas Cort 		    filename, linenum, msg, stifline[depth], depth);
1031*b6f0c436SThomas Cort 	fclose(output);
1032*b6f0c436SThomas Cort 	if (overwriting) {
1033*b6f0c436SThomas Cort 		unlink(tmpname);
1034*b6f0c436SThomas Cort 		errx(2, "%s unchanged", ofilename);
1035*b6f0c436SThomas Cort 	}
1036*b6f0c436SThomas Cort 	errx(2, "output may be truncated");
1037*b6f0c436SThomas Cort }
1038