2 * Copyright (c) 1985, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 static const char copyright[] =
39 "@(#) Copyright (c) 1985, 1993\n\
40 The Regents of the University of California. All rights reserved.\n";
45 static char sccsid[] = "@(#)unifdef.c 8.1 (Berkeley) 6/6/93";
47 static const char rcsid[] =
52 * unifdef - remove ifdef'ed lines
54 * Warning: will not work correctly if input contains null characters.
57 * provide an option which will append the name of the
58 * appropriate symbol after #else's and #endif's
59 * provide an option which will check symbols after
60 * #else's and #endif's to see that they match their
61 * corresponding #ifdef or #ifndef
70 typedef int Reject_level;
81 char text BSS; /* -t option in effect: this is a text file */
82 char lnblank BSS; /* -l option in effect: blank deleted lines */
83 char complement BSS; /* -c option in effect: complement the operation */
86 char *symname[MAXSYMS] BSS; /* symbol name */
87 char true[MAXSYMS] BSS; /* -Dsym */
88 char ignore[MAXSYMS] BSS; /* -iDsym or -iUsym */
89 char insym[MAXSYMS] BSS; /* state: false, inactive, true */
90 #define SYM_INACTIVE 0 /* symbol is currently inactive */
91 #define SYM_FALSE 1 /* symbol is currently false */
92 #define SYM_TRUE 2 /* symbol is currently true */
95 char incomment BSS; /* inside C comment */
98 #define QUOTE_SINGLE 1
99 #define QUOTE_DOUBLE 2
100 char inquote BSS; /* inside single or double quotes */
103 char *skipcomment ();
105 static void usage __P((void));
106 void flushline __P((Bool));
107 int getlin __P((char *, int, FILE *, int));
108 int error __P((int, int, int));
109 void pfile __P((void));
110 int doif __P((int, int, Reject_level, int));
111 int findsym __P((char *));
123 for (curarg = &argv[1]; --argc > 0; curarg++) {
124 if (*(cp1 = cp = *curarg) != '-')
138 if ((symind = findsym (&cp1[1])) < 0) {
139 if (nsyms >= MAXSYMS)
140 errx(2, "too many symbols");
142 symname[symind] = &cp1[1];
143 insym[symind] = SYM_INACTIVE;
145 ignore[symind] = ignorethis;
146 true[symind] = *cp1 == 'D' ? YES : NO;
147 } else if (ignorethis)
149 else if (strcmp (&cp[1], "t") == 0)
151 else if (strcmp (&cp[1], "l") == 0)
153 else if (strcmp (&cp[1], "c") == 0)
157 warnx("unrecognized option: %s", cp);
165 warnx("can only do one file");
166 } else if (argc == 1) {
168 if ((input = fopen (filename, "r")) != NULL) {
170 (void) fclose (input);
172 warn("can't open %s", *curarg);
175 filename = "[stdin]";
180 (void) fflush (stdout);
187 fprintf (stderr, "usage: %s",
188 "unifdef [-l] [-t] [-c] [[-Dsym] [-Usym] [-iDsym] [-iUsym]] ... [file]\n");
192 /* types of input lines: */
193 typedef int Linetype;
194 #define LT_PLAIN 0 /* ordinary line */
195 #define LT_TRUE 1 /* a true #ifdef of a symbol known to us */
196 #define LT_FALSE 2 /* a false #ifdef of a symbol known to us */
197 #define LT_OTHER 3 /* an #ifdef of a symbol not known to us */
198 #define LT_IF 4 /* an #ifdef of a symbol not known to us */
199 #define LT_ELSE 5 /* #else */
200 #define LT_ENDIF 6 /* #endif */
201 #define LT_LEOF 7 /* end of file */
202 extern Linetype checkline ();
204 Reject_level reject BSS; /* 0 or 1: pass thru; 1 or 2: ignore comments */
209 int linenum BSS; /* current line number */
210 int stqcline BSS; /* start of current coment or quote */
217 "Inappropriate else",
219 "Inappropriate endif",
221 "Premature EOF in ifdef",
223 "Premature EOF in comment",
225 "Premature EOF in quoted character",
227 "Premature EOF in quoted string"
230 /* States for inif arg to doif */
239 (void) doif (-1, IN_NONE, reject, 0);
243 doif (thissym, inif, prevreject, depth)
244 register int thissym; /* index of the symbol who was last ifdef'ed */
245 int inif; /* YES or NO we are inside an ifdef */
246 Reject_level prevreject;/* previous value of reject */
247 int depth; /* depth of ifdef's */
249 register Linetype lineval;
250 register Reject_level thisreject;
251 int doret; /* tmp return value of doif */
252 int cursym; /* index of the symbol returned by checkline */
253 int stline; /* line number when called this time */
257 switch (lineval = checkline (&cursym)) {
265 if (lineval == LT_TRUE)
266 insym[cursym] = SYM_TRUE;
268 if (reject != REJ_YES)
269 reject = ignore[cursym] ? REJ_IGNORE : REJ_YES;
270 insym[cursym] = SYM_FALSE;
278 if ((doret = doif (cursym, IN_IF, thisreject, depth + 1)) != NO_ERR)
279 return error (doret, stline, depth);
285 if ((doret = doif (-1, IN_IF, reject, depth + 1)) != NO_ERR)
286 return error (doret, stline, depth);
291 return error (ELSE_ERR, linenum, depth);
294 if (insym[thissym] == SYM_TRUE) {
295 reject = ignore[thissym] ? REJ_IGNORE : REJ_YES;
296 insym[thissym] = SYM_FALSE;
297 } else { /* (insym[thissym] == SYM_FALSE) */
299 insym[thissym] = SYM_TRUE;
301 if (!ignore[thissym]) {
311 return error (ENDIF_ERR, linenum, depth);
313 insym[thissym] = SYM_INACTIVE;
315 if (!ignore[thissym]) {
327 : inquote == QUOTE_SINGLE
329 : inquote == QUOTE_DOUBLE
332 if (inif != IN_NONE) {
334 (void) error (err, stqcline, depth);
335 return error (IEOF_ERR, stline, depth);
336 } else if (err != NO_ERR)
337 return error (err, stqcline, depth);
345 #define endsym(c) (!isalpha (c) && !isdigit (c) && c != '_')
348 char tline[MAXLINE] BSS;
352 int *cursym; /* if LT_TRUE or LT_FALSE returned, set this to sym index */
359 char keyword[KWSIZE];
362 if (getlin (tline, sizeof tline, input, NO) == EOF)
366 if ( *(cp = tline) != '#'
368 || inquote == QUOTE_SINGLE
369 || inquote == QUOTE_DOUBLE
373 cp = skipcomment (++cp);
375 while (!endsym (*cp)) {
377 if (++symp >= &keyword[KWSIZE])
382 if (strcmp (keyword, "ifdef") == 0) {
385 } else if (strcmp (keyword, "ifndef") == 0) {
388 scp = cp = skipcomment (++cp);
396 if ((symind = findsym (scp)) >= 0)
397 retval = (retval ^ true[*cursym = symind])
398 ? LT_FALSE : LT_TRUE;
402 } else if (strcmp (keyword, "if") == 0)
404 else if (strcmp (keyword, "else") == 0)
406 else if (strcmp (keyword, "endif") == 0)
410 if (!text && reject != REJ_IGNORE)
413 cp = skipcomment (cp);
414 else if (inquote == QUOTE_SINGLE)
415 cp = skipquote (cp, QUOTE_SINGLE);
416 else if (inquote == QUOTE_DOUBLE)
417 cp = skipquote (cp, QUOTE_DOUBLE);
418 else if (*cp == '/' && cp[1] == '*')
419 cp = skipcomment (cp);
420 else if (*cp == '\'')
421 cp = skipquote (cp, QUOTE_SINGLE);
423 cp = skipquote (cp, QUOTE_DOUBLE);
431 * Skip over comments and stop at the next charaacter
432 * position that is not whitespace.
441 while (*cp == ' ' || *cp == '\t')
456 for (; *cp != '*'; cp++)
468 * Skip over a quoted string or character and stop at the next charaacter
469 * position that is not whitespace.
478 qchar = type == QUOTE_SINGLE ? '\'' : '"';
493 || (*cp == '\\' && *++cp == '\0')
497 inquote = QUOTE_NONE;
502 * findsym - look for the symbol in the symbol table.
503 * if found, return symbol table index,
515 for (symind = 0; symind < nsyms; ++symind) {
516 if (insym[symind] == SYM_INACTIVE) {
517 for ( symp = symname[symind], cp = str
518 ; *symp && *cp == *symp
523 if (*symp == '\0' && endsym (chr))
531 * getlin - expands tabs if asked for
532 * and (if compiled in) treats form-feed as an end-of-line
535 getlin (line, maxline, inp, expandtabs)
545 static char havechar = NO; /* have leftover char from last time */
546 static char svchar BSS;
557 while (num + 8 < maxline) { /* leave room for tab */
572 num += tmp = 8 - (num & 7);
610 if ((keep && reject != REJ_YES) ^ complement) {
611 register char *line = tline;
612 register FILE *out = stdout;
615 while ((chr = *line++))
622 error (err, line, depth)
623 int err; /* type of error & index into error string array */
624 int line; /* line number */
625 int depth; /* how many ifdefs we are inside */
631 warnx("error in %s line %d: %s", filename, line, errs[err]);
633 warnx("error in %s line %d: %s. ifdef depth: %d",
634 filename, line, errs[err], depth);
638 return depth > 1 ? IEOF_ERR : END_ERR;