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 char copyright[] =
39
"@(#) Copyright (c) 1985, 1993\n\
40
The Regents of the University of California. All rights reserved.\n";
44
static char sccsid[] = "@(#)unifdef.c 8.1 (Berkeley) 6/6/93";
48
* unifdef - remove ifdef'ed lines
50
* Warning: will not work correctly if input contains null characters.
53
* provide an option which will append the name of the
54
* appropriate symbol after #else's and #endif's
55
* provide an option which will check symbols after
56
* #else's and #endif's to see that they match their
57
* corresponding #ifdef or #ifndef
73
char text BSS; /* -t option in effect: this is a text file */
74
char lnblank BSS; /* -l option in effect: blank deleted lines */
75
char complement BSS; /* -c option in effect: complement the operation */
78
char *symname[MAXSYMS] BSS; /* symbol name */
79
char true[MAXSYMS] BSS; /* -Dsym */
80
char ignore[MAXSYMS] BSS; /* -iDsym or -iUsym */
81
char insym[MAXSYMS] BSS; /* state: false, inactive, true */
82
#define SYM_INACTIVE 0 /* symbol is currently inactive */
83
#define SYM_FALSE 1 /* symbol is currently false */
84
#define SYM_TRUE 2 /* symbol is currently true */
87
char incomment BSS; /* inside C comment */
88
char inCPPcomment BSS; /* inside C++ comment */
91
#define QUOTE_SINGLE 1
92
#define QUOTE_DOUBLE 2
93
char inquote BSS; /* inside single or double quotes */
108
progname = argv[0][0] ? argv[0] : "unifdef";
110
for (curarg = &argv[1]; --argc > 0; curarg++) {
111
if (*(cp1 = cp = *curarg) != '-')
125
if ((symind = findsym (&cp1[1])) < 0) {
126
if (nsyms >= MAXSYMS) {
128
fprintf (stderr, "too many symbols.\n");
132
symname[symind] = &cp1[1];
133
insym[symind] = SYM_INACTIVE;
135
ignore[symind] = ignorethis;
136
true[symind] = *cp1 == 'D' ? YES : NO;
137
} else if (ignorethis)
139
else if (strcmp (&cp[1], "t") == 0)
141
else if (strcmp (&cp[1], "l") == 0)
143
else if (strcmp (&cp[1], "c") == 0)
148
fprintf (stderr, "unrecognized option: %s\n", cp);
155
Usage: %s [-l] [-t] [-c] [[-Dsym] [-Usym] [-iDsym] [-iUsym]]... [file]\n\
156
At least one arg from [-D -U -iD -iU] is required\n", progname);
162
fprintf (stderr, "can only do one file.\n");
163
} else if (argc == 1) {
165
if ((input = fopen (filename, "r")) != NULL) {
167
(void) fclose (input);
170
fprintf (stderr, "can't open ");
174
filename = "[stdin]";
179
(void) fflush (stdout);
183
/* types of input lines: */
184
typedef int Linetype;
185
#define LT_PLAIN 0 /* ordinary line */
186
#define LT_TRUE 1 /* a true #ifdef of a symbol known to us */
187
#define LT_FALSE 2 /* a false #ifdef of a symbol known to us */
188
#define LT_OTHER 3 /* an #ifdef of a symbol not known to us */
189
#define LT_IF 4 /* an #ifdef of a symbol not known to us */
190
#define LT_ELSE 5 /* #else */
191
#define LT_ENDIF 6 /* #endif */
192
#define LT_LEOF 7 /* end of file */
193
extern Linetype checkline ();
195
typedef int Reject_level;
196
Reject_level reject BSS; /* 0 or 1: pass thru; 1 or 2: ignore comments */
201
int linenum BSS; /* current line number */
202
int stqcline BSS; /* start of current coment or quote */
209
"Inappropriate else",
211
"Inappropriate endif",
213
"Premature EOF in ifdef",
215
"Premature EOF in comment",
217
"Premature EOF in quoted character",
219
"Premature EOF in quoted string"
222
/* States for inif arg to doif */
230
(void) doif (-1, IN_NONE, reject, 0);
235
doif (thissym, inif, prevreject, depth)
236
register int thissym; /* index of the symbol who was last ifdef'ed */
237
int inif; /* YES or NO we are inside an ifdef */
238
Reject_level prevreject;/* previous value of reject */
239
int depth; /* depth of ifdef's */
241
register Linetype lineval;
242
register Reject_level thisreject;
243
int doret; /* tmp return value of doif */
244
int cursym; /* index of the symbol returned by checkline */
245
int stline; /* line number when called this time */
249
switch (lineval = checkline (&cursym)) {
257
if (lineval == LT_TRUE)
258
insym[cursym] = SYM_TRUE;
260
if (reject != REJ_YES)
261
reject = ignore[cursym] ? REJ_IGNORE : REJ_YES;
262
insym[cursym] = SYM_FALSE;
270
if ((doret = doif (cursym, IN_IF, thisreject, depth + 1)) != NO_ERR)
271
return error (doret, stline, depth);
277
if ((doret = doif (-1, IN_IF, reject, depth + 1)) != NO_ERR)
278
return error (doret, stline, depth);
283
return error (ELSE_ERR, linenum, depth);
286
if (insym[thissym] == SYM_TRUE) {
287
reject = ignore[thissym] ? REJ_IGNORE : REJ_YES;
288
insym[thissym] = SYM_FALSE;
289
} else { /* (insym[thissym] == SYM_FALSE) */
291
insym[thissym] = SYM_TRUE;
293
if (!ignore[thissym]) {
303
return error (ENDIF_ERR, linenum, depth);
305
insym[thissym] = SYM_INACTIVE;
307
if (!ignore[thissym]) {
319
: inquote == QUOTE_SINGLE
321
: inquote == QUOTE_DOUBLE
324
if (inif != IN_NONE) {
326
(void) error (err, stqcline, depth);
327
return error (IEOF_ERR, stline, depth);
328
} else if (err != NO_ERR)
329
return error (err, stqcline, depth);
337
#define endsym(c) (!isalpha (c) && !isdigit (c) && c != '_')
340
char tline[MAXLINE] BSS;
344
int *cursym; /* if LT_TRUE or LT_FALSE returned, set this to sym index */
351
char keyword[KWSIZE];
354
if (getlin (tline, sizeof tline, input, NO) == EOF)
358
if ( *(cp = tline) != '#'
360
|| inquote == QUOTE_SINGLE
361
|| inquote == QUOTE_DOUBLE
365
cp = skipcomment (++cp);
367
while (!endsym (*cp)) {
369
if (++symp >= &keyword[KWSIZE])
374
if (strcmp (keyword, "ifdef") == 0) {
377
} else if (strcmp (keyword, "ifndef") == 0) {
380
scp = cp = skipcomment (++cp);
388
if ((symind = findsym (scp)) >= 0)
389
retval = (retval ^ true[*cursym = symind])
390
? LT_FALSE : LT_TRUE;
394
} else if (strcmp (keyword, "if") == 0)
396
else if (strcmp (keyword, "else") == 0)
398
else if (strcmp (keyword, "endif") == 0)
402
if (!text && reject != REJ_IGNORE)
405
cp = skipcomment (cp);
406
else if (inquote == QUOTE_SINGLE)
407
cp = skipquote (cp, QUOTE_SINGLE);
408
else if (inquote == QUOTE_DOUBLE)
409
cp = skipquote (cp, QUOTE_DOUBLE);
410
else if (*cp == '/' && (cp[1] == '*' || cp[1] == '/'))
411
cp = skipcomment (cp);
412
else if (*cp == '\'')
413
cp = skipquote (cp, QUOTE_SINGLE);
415
cp = skipquote (cp, QUOTE_DOUBLE);
423
* Skip over comments and stop at the next charaacter
424
* position that is not whitespace.
433
while (*cp == ' ' || *cp == '\t')
438
|| (cp[1] != '*' && cp[1] != '/')
443
inCPPcomment = (cp[1] == '/');
449
for (; *cp != '*'; cp++)
465
* Skip over a quoted string or character and stop at the next charaacter
466
* position that is not whitespace.
475
qchar = type == QUOTE_SINGLE ? '\'' : '"';
490
|| *cp == '\\' && *++cp == '\0'
494
inquote = QUOTE_NONE;
499
* findsym - look for the symbol in the symbol table.
500
* if found, return symbol table index,
512
for (symind = 0; symind < nsyms; ++symind) {
513
if (insym[symind] == SYM_INACTIVE) {
514
for ( symp = symname[symind], cp = str
515
; *symp && *cp == *symp
520
if (*symp == '\0' && endsym (chr))
528
* getlin - expands tabs if asked for
529
* and (if compiled in) treats form-feed as an end-of-line
532
getlin (line, maxline, inp, expandtabs)
542
static char havechar = NO; /* have leftover char from last time */
543
static char svchar BSS;
554
while (num + 8 < maxline) { /* leave room for tab */
569
num += tmp = 8 - (num & 7);
606
if ((keep && reject != REJ_YES) ^ complement) {
607
register char *line = tline;
608
register FILE *out = stdout;
611
while (chr = *line++)
620
fprintf (stderr, "%s: ", progname);
625
error (err, line, depth)
626
int err; /* type of error & index into error string array */
627
int line; /* line number */
628
int depth; /* how many ifdefs we are inside */
636
fprintf (stderr, "Error in %s line %d: %s.\n", filename, line, errs[err]);
638
fprintf (stderr, "Error in %s line %d: %s. ", filename, line, errs[err]);
639
fprintf (stderr, "ifdef depth: %d\n", depth);
643
return depth > 1 ? IEOF_ERR : END_ERR;