/* Copyright (c) 1982 Regents of the University of California */
static char sccsid
[] = "@(#)unifdef.c 4.2 (Berkeley) %G%";
unifdef
- remove ifdef
'ed lines
char text BSS; /* -t option in effect: this is a text file */
char lnblank BSS; /* -l option in effect: blank deleted lines */
char complement BSS; /* -c option in effect: complement the operation */
char ignore[MAXSYMS] BSS;
progname = argv[0][0] ? argv[0] : "unifdef";
for (curarg = &argv[1]; --argc > 0; curarg++) {
if (*(cp1 = cp = *curarg) != '-')
fprintf (stderr
, "too many symbols.\n");
ignore
[nsyms
] = ignorethis
;
true[nsyms
] = *cp1
== 'd' ? YES
: NO
;
else if (strcmp (&cp
[1], "t") == 0)
else if (strcmp (&cp
[1], "l") == 0)
else if (strcmp (&cp
[1], "c") == 0)
fprintf (stderr
, "unrecognized option: %s\n", cp
);
Usage: %s [-l] [-t] [-c] [[-dsym] [-usym] [-idsym] [-iusym]]... [file]\n\
At least one arg from [-d -u -id -iu] is required\n", progname
);
fprintf (stderr
, "can only do one file.\n");
if ((input
= fopen (filename
, "r")) != NULL
) {
/* types of input lines: */
#define PLAIN 0 /* ordinary line */
#define TRUE 1 /* a true #ifdef of a symbol known to us */
#define FALSE 2 /* a false #ifdef of a symbol known to us */
#define OTHER 3 /* an #ifdef of a symbol not known to us */
#define ELSE 4 /* #else */
#define ENDIF 5 /* #endif */
#define LEOF 6 /* end of file */
char reject BSS
; /* 0 or 1: pass thru; 1 or 2: ignore comments */
int linenum BSS
; /* current line number */
int stqcline BSS
; /* start of current coment or quote */
"Premature EOF in ifdef",
"Premature EOF in comment",
"Premature EOF in quoted character",
"Premature EOF in quoted string"
doif (-1, NO
, reject
, 0);
doif (thissym
, inif
, prevreject
, depth
)
register int thissym
; /* index of the symbol who was last ifdef'ed */
int inif
; /* YES or NO we are inside an ifdef */
int prevreject
; /* previous value of reject */
int depth
; /* depth of ifdef's */
int doret
; /* tmp return valud]e of doif */
int cursym
; /* index of the symbol returned by checkline */
int stline
; /* line number when called this time */
switch (lineval
= checkline (&cursym
)) {
reject
= ignore
[cursym
] ? 1 : 2;
if ((doret
= doif (cursym
, YES
, thisreject
, depth
+ 1)) != NO_ERR
)
return error (doret
, stline
, depth
);
if ((doret
= doif (-1, YES
, reject
, depth
+ 1)) != NO_ERR
)
return error (doret
, stline
, depth
);
return error (ELSE_ERR
, linenum
, depth
);
if ((insym
[thissym
] = -insym
[thissym
]) < 0)
reject
= ignore
[thissym
] ? 1 : 2;
return error (ENDIF_ERR
, linenum
, depth
);
error (err
, stqcline
, depth
);
return error (IEOF_ERR
, stline
, depth
);
return error (err
, stqcline
, depth
);
#define endsym(c) (!isalpha (c) && !isdigit (c) && c != '_')
if (getlin (tline
, sizeof tline
, input
, NO
) == EOF
)
if ( *(cp
= tline
) != '#'
if (++symp
>= &keyword
[KWSIZE
])
if (strcmp (keyword
, "ifdef") == 0) {
else if (strcmp (keyword
, "ifndef") == 0) {
scp
= cp
= skipcomment (++cp
);
if (insym
[symind
] == 0) {
for ( symp
= sym
[symind
], cp
= scp
if (*symp
== '\0' && endsym (chr
)) {
retval
= (retval
^ true[symind
]) ? FALSE
: TRUE
;
else if (strcmp (keyword
, "if") == 0)
else if (strcmp (keyword
, "else") == 0)
else if (strcmp (keyword
, "endif") == 0)
else if (inquote
[QUOTE1
])
cp
= skipquote (cp
, QUOTE1
);
else if (inquote
[QUOTE2
])
cp
= skipquote (cp
, QUOTE2
);
else if (*cp
== '/' && cp
[1] == '*')
cp
= skipquote (cp
, QUOTE1
);
cp
= skipquote (cp
, QUOTE2
);
/* Skip over comments and stop at the next charaacter
/* position that is not whitespace.
while (*cp
== ' ' || *cp
== '\t')
/* Skip over a quoted string or character and stop at the next charaacter
/* position that is not whitespace.
qchar
= type
== QUOTE1
? '\'' : '"';
/* special getlin - treats form-feed as an end-of-line
/* and expands tabs if asked for
getlin (line
, maxline
, inp
, expandtabs
)
static char havechar
= NO
; /* have leftover char from last time */
while (num
+ 8 < maxline
) { /* leave room for tab */
num
+= tmp
= 8 - (num
& 7);
if ((keep
&& reject
< 2) ^ complement
)
fprintf (stderr
, "%s: ", progname
);
fprintf (stderr
, "Error in %s line %d: %s.\n",
filename
, line
, errs
[err
]);
fprintf (stderr
, "Error in %s line %d: %s. ",
filename
, line
, errs
[err
]);
fprintf (stderr
, "ifdef depth: %d\n", depth
);
return depth
> 1 ? IEOF_ERR
: END_ERR
;