Home | History | Annotate | Line # | Download | only in unifdef
unifdef.c revision 1.5
      1  1.5  lukem /*	$NetBSD: unifdef.c,v 1.5 1997/10/20 02:23:14 lukem Exp $	*/
      2  1.3    jtc 
      3  1.1    cgd /*
      4  1.3    jtc  * Copyright (c) 1985, 1993
      5  1.3    jtc  *	The Regents of the University of California.  All rights reserved.
      6  1.1    cgd  *
      7  1.1    cgd  * This code is derived from software contributed to Berkeley by
      8  1.1    cgd  * Dave Yost.
      9  1.1    cgd  *
     10  1.1    cgd  * Redistribution and use in source and binary forms, with or without
     11  1.1    cgd  * modification, are permitted provided that the following conditions
     12  1.1    cgd  * are met:
     13  1.1    cgd  * 1. Redistributions of source code must retain the above copyright
     14  1.1    cgd  *    notice, this list of conditions and the following disclaimer.
     15  1.1    cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1    cgd  *    notice, this list of conditions and the following disclaimer in the
     17  1.1    cgd  *    documentation and/or other materials provided with the distribution.
     18  1.1    cgd  * 3. All advertising materials mentioning features or use of this software
     19  1.1    cgd  *    must display the following acknowledgement:
     20  1.1    cgd  *	This product includes software developed by the University of
     21  1.1    cgd  *	California, Berkeley and its contributors.
     22  1.1    cgd  * 4. Neither the name of the University nor the names of its contributors
     23  1.1    cgd  *    may be used to endorse or promote products derived from this software
     24  1.1    cgd  *    without specific prior written permission.
     25  1.1    cgd  *
     26  1.1    cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  1.1    cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  1.1    cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  1.1    cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  1.1    cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  1.1    cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  1.1    cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  1.1    cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  1.1    cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  1.1    cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  1.1    cgd  * SUCH DAMAGE.
     37  1.1    cgd  */
     38  1.1    cgd 
     39  1.5  lukem #include <sys/cdefs.h>
     40  1.1    cgd #ifndef lint
     41  1.5  lukem __COPYRIGHT("@(#) Copyright (c) 1985, 1993\n\
     42  1.5  lukem 	The Regents of the University of California.  All rights reserved.\n");
     43  1.5  lukem #endif				/* not lint */
     44  1.1    cgd 
     45  1.1    cgd #ifndef lint
     46  1.3    jtc #if 0
     47  1.3    jtc static char sccsid[] = "@(#)unifdef.c	8.1 (Berkeley) 6/6/93";
     48  1.3    jtc #endif
     49  1.5  lukem __RCSID("$NetBSD: unifdef.c,v 1.5 1997/10/20 02:23:14 lukem Exp $");
     50  1.5  lukem #endif				/* not lint */
     51  1.1    cgd 
     52  1.1    cgd /*
     53  1.1    cgd  * unifdef - remove ifdef'ed lines
     54  1.1    cgd  *
     55  1.1    cgd  *  Warning: will not work correctly if input contains null characters.
     56  1.1    cgd  *
     57  1.1    cgd  *  Wishlist:
     58  1.1    cgd  *      provide an option which will append the name of the
     59  1.1    cgd  *        appropriate symbol after #else's and #endif's
     60  1.1    cgd  *      provide an option which will check symbols after
     61  1.1    cgd  *        #else's and #endif's to see that they match their
     62  1.1    cgd  *        corresponding #ifdef or #ifndef
     63  1.1    cgd  */
     64  1.1    cgd 
     65  1.1    cgd #include <stdio.h>
     66  1.1    cgd #include <ctype.h>
     67  1.1    cgd 
     68  1.1    cgd #define BSS
     69  1.5  lukem FILE   *input;
     70  1.1    cgd #ifndef YES
     71  1.1    cgd #define YES 1
     72  1.1    cgd #define NO  0
     73  1.5  lukem #endif				/* YES */
     74  1.1    cgd typedef int Bool;
     75  1.1    cgd 
     76  1.5  lukem char   *progname BSS;
     77  1.5  lukem char   *filename BSS;
     78  1.5  lukem char text BSS;			/* -t option in effect: this is a text file */
     79  1.5  lukem char lnblank BSS;		/* -l option in effect: blank deleted lines */
     80  1.5  lukem char complement BSS;		/* -c option in effect: complement the
     81  1.5  lukem 				 * operation */
     82  1.1    cgd 
     83  1.1    cgd #define MAXSYMS 100
     84  1.5  lukem char   *symname[MAXSYMS] BSS;	/* symbol name */
     85  1.5  lukem char    true[MAXSYMS] BSS;	/* -Dsym */
     86  1.5  lukem char    ignore[MAXSYMS] BSS;	/* -iDsym or -iUsym */
     87  1.5  lukem char    insym[MAXSYMS] BSS;	/* state: false, inactive, true */
     88  1.5  lukem #define SYM_INACTIVE 0		/* symbol is currently inactive */
     89  1.5  lukem #define SYM_FALSE    1		/* symbol is currently false */
     90  1.5  lukem #define SYM_TRUE     2		/* symbol is currently true  */
     91  1.1    cgd 
     92  1.1    cgd char nsyms BSS;
     93  1.5  lukem char incomment BSS;		/* inside C comment */
     94  1.1    cgd 
     95  1.1    cgd #define QUOTE_NONE   0
     96  1.1    cgd #define QUOTE_SINGLE 1
     97  1.1    cgd #define QUOTE_DOUBLE 2
     98  1.5  lukem char inquote BSS;		/* inside single or double quotes */
     99  1.4    jtc int exitstat BSS;
    100  1.1    cgd 
    101  1.5  lukem int error __P((int, int, int));
    102  1.5  lukem int findsym __P((char *));
    103  1.5  lukem void flushline __P((Bool));
    104  1.5  lukem int getlin __P((char *, int, FILE *, int));
    105  1.5  lukem int main __P((int, char **));
    106  1.5  lukem void pfile __P((void));
    107  1.5  lukem void prname __P((void));
    108  1.4    jtc char   *skipcomment __P((char *));
    109  1.4    jtc char   *skipquote __P((char *, int));
    110  1.1    cgd 
    111  1.4    jtc int
    112  1.5  lukem main(argc, argv)
    113  1.5  lukem 	int     argc;
    114  1.5  lukem 	char  **argv;
    115  1.5  lukem {
    116  1.5  lukem 	char  **curarg;
    117  1.5  lukem 	char   *cp;
    118  1.5  lukem 	char   *cp1;
    119  1.5  lukem 	char    ignorethis;
    120  1.5  lukem 
    121  1.5  lukem 	progname = argv[0][0] ? argv[0] : "unifdef";
    122  1.5  lukem 
    123  1.5  lukem 	for (curarg = &argv[1]; --argc > 0; curarg++) {
    124  1.5  lukem 		if (*(cp1 = cp = *curarg) != '-')
    125  1.5  lukem 			break;
    126  1.5  lukem 		if (*++cp1 == 'i') {
    127  1.5  lukem 			ignorethis = YES;
    128  1.5  lukem 			cp1++;
    129  1.5  lukem 		} else
    130  1.5  lukem 			ignorethis = NO;
    131  1.5  lukem 		if ((*cp1 == 'D'
    132  1.5  lukem 			|| *cp1 == 'U'
    133  1.5  lukem 		    )
    134  1.5  lukem 		    && cp1[1] != '\0'
    135  1.5  lukem 		    ) {
    136  1.5  lukem 			int     symind;
    137  1.5  lukem 
    138  1.5  lukem 			if ((symind = findsym(&cp1[1])) < 0) {
    139  1.5  lukem 				if (nsyms >= MAXSYMS) {
    140  1.5  lukem 					prname();
    141  1.5  lukem 					fprintf(stderr, "too many symbols.\n");
    142  1.5  lukem 					exit(2);
    143  1.5  lukem 				}
    144  1.5  lukem 				symind = nsyms++;
    145  1.5  lukem 				symname[symind] = &cp1[1];
    146  1.5  lukem 				insym[symind] = SYM_INACTIVE;
    147  1.5  lukem 			}
    148  1.5  lukem 			ignore[symind] = ignorethis;
    149  1.5  lukem 			true[symind] = *cp1 == 'D' ? YES : NO;
    150  1.5  lukem 		} else
    151  1.5  lukem 			if (ignorethis)
    152  1.5  lukem 				goto unrec;
    153  1.5  lukem 			else
    154  1.5  lukem 				if (strcmp(&cp[1], "t") == 0)
    155  1.5  lukem 					text = YES;
    156  1.5  lukem 				else
    157  1.5  lukem 					if (strcmp(&cp[1], "l") == 0)
    158  1.5  lukem 						lnblank = YES;
    159  1.5  lukem 					else
    160  1.5  lukem 						if (strcmp(&cp[1], "c") == 0)
    161  1.5  lukem 							complement = YES;
    162  1.5  lukem 						else {
    163  1.5  lukem 					unrec:
    164  1.5  lukem 							prname();
    165  1.5  lukem 							fprintf(stderr, "unrecognized option: %s\n", cp);
    166  1.5  lukem 							goto usage;
    167  1.5  lukem 						}
    168  1.1    cgd 	}
    169  1.5  lukem 	if (nsyms == 0) {
    170  1.5  lukem usage:
    171  1.5  lukem 		fprintf(stderr, "\
    172  1.1    cgd Usage: %s [-l] [-t] [-c] [[-Dsym] [-Usym] [-iDsym] [-iUsym]]... [file]\n\
    173  1.1    cgd     At least one arg from [-D -U -iD -iU] is required\n", progname);
    174  1.5  lukem 		exit(2);
    175  1.1    cgd 	}
    176  1.5  lukem 	if (argc > 1) {
    177  1.5  lukem 		prname();
    178  1.5  lukem 		fprintf(stderr, "can only do one file.\n");
    179  1.5  lukem 	} else
    180  1.5  lukem 		if (argc == 1) {
    181  1.5  lukem 			filename = *curarg;
    182  1.5  lukem 			if ((input = fopen(filename, "r")) != NULL) {
    183  1.5  lukem 				pfile();
    184  1.5  lukem 				(void) fclose(input);
    185  1.5  lukem 			} else {
    186  1.5  lukem 				prname();
    187  1.5  lukem 				fprintf(stderr, "can't open ");
    188  1.5  lukem 				perror(*curarg);
    189  1.5  lukem 			}
    190  1.5  lukem 		} else {
    191  1.5  lukem 			filename = "[stdin]";
    192  1.5  lukem 			input = stdin;
    193  1.5  lukem 			pfile();
    194  1.5  lukem 		}
    195  1.1    cgd 
    196  1.5  lukem 	(void) fflush(stdout);
    197  1.5  lukem 	exit(exitstat);
    198  1.1    cgd }
    199  1.1    cgd /* types of input lines: */
    200  1.1    cgd typedef int Linetype;
    201  1.5  lukem #define LT_PLAIN       0	/* ordinary line */
    202  1.5  lukem #define LT_TRUE        1	/* a true  #ifdef of a symbol known to us */
    203  1.5  lukem #define LT_FALSE       2	/* a false #ifdef of a symbol known to us */
    204  1.5  lukem #define LT_OTHER       3	/* an #ifdef of a symbol not known to us */
    205  1.5  lukem #define LT_IF          4	/* an #ifdef of a symbol not known to us */
    206  1.5  lukem #define LT_ELSE        5	/* #else */
    207  1.5  lukem #define LT_ENDIF       6	/* #endif */
    208  1.5  lukem #define LT_LEOF        7	/* end of file */
    209  1.4    jtc Linetype checkline __P((int *));
    210  1.1    cgd 
    211  1.1    cgd typedef int Reject_level;
    212  1.5  lukem Reject_level reject BSS;	/* 0 or 1: pass thru; 1 or 2: ignore comments */
    213  1.1    cgd #define REJ_NO          0
    214  1.1    cgd #define REJ_IGNORE      1
    215  1.1    cgd #define REJ_YES         2
    216  1.4    jtc int doif __P((int, int, Reject_level, int));
    217  1.1    cgd 
    218  1.5  lukem int linenum BSS;		/* current line number */
    219  1.5  lukem int stqcline BSS;		/* start of current coment or quote */
    220  1.5  lukem char   *errs[] = {
    221  1.1    cgd #define NO_ERR      0
    222  1.5  lukem 	"",
    223  1.1    cgd #define END_ERR     1
    224  1.5  lukem 	"",
    225  1.1    cgd #define ELSE_ERR    2
    226  1.5  lukem 	"Inappropriate else",
    227  1.1    cgd #define ENDIF_ERR   3
    228  1.5  lukem 	"Inappropriate endif",
    229  1.1    cgd #define IEOF_ERR    4
    230  1.5  lukem 	"Premature EOF in ifdef",
    231  1.1    cgd #define CEOF_ERR    5
    232  1.5  lukem 	"Premature EOF in comment",
    233  1.1    cgd #define Q1EOF_ERR   6
    234  1.5  lukem 	"Premature EOF in quoted character",
    235  1.1    cgd #define Q2EOF_ERR   7
    236  1.5  lukem 	"Premature EOF in quoted string"
    237  1.1    cgd };
    238  1.1    cgd /* States for inif arg to doif */
    239  1.1    cgd #define IN_NONE 0
    240  1.1    cgd #define IN_IF   1
    241  1.1    cgd #define IN_ELSE 2
    242  1.1    cgd 
    243  1.4    jtc void
    244  1.5  lukem pfile()
    245  1.1    cgd {
    246  1.5  lukem 	reject = REJ_NO;
    247  1.5  lukem 	(void) doif(-1, IN_NONE, reject, 0);
    248  1.5  lukem 	return;
    249  1.1    cgd }
    250  1.1    cgd 
    251  1.1    cgd int
    252  1.5  lukem doif(thissym, inif, prevreject, depth)
    253  1.5  lukem 	int     thissym;	/* index of the symbol who was last ifdef'ed */
    254  1.5  lukem 	int     inif;		/* YES or NO we are inside an ifdef */
    255  1.5  lukem 	Reject_level prevreject;/* previous value of reject */
    256  1.5  lukem 	int     depth;		/* depth of ifdef's */
    257  1.5  lukem {
    258  1.5  lukem 	Linetype lineval;
    259  1.5  lukem 	Reject_level thisreject;
    260  1.5  lukem 	int     doret;		/* tmp return value of doif */
    261  1.5  lukem 	int     cursym;		/* index of the symbol returned by checkline */
    262  1.5  lukem 	int     stline;		/* line number when called this time */
    263  1.5  lukem 
    264  1.5  lukem 	stline = linenum;
    265  1.5  lukem 	for (;;) {
    266  1.5  lukem 		switch (lineval = checkline(&cursym)) {
    267  1.5  lukem 		case LT_PLAIN:
    268  1.5  lukem 			flushline(YES);
    269  1.5  lukem 			break;
    270  1.5  lukem 
    271  1.5  lukem 		case LT_TRUE:
    272  1.5  lukem 		case LT_FALSE:
    273  1.5  lukem 			thisreject = reject;
    274  1.5  lukem 			if (lineval == LT_TRUE)
    275  1.5  lukem 				insym[cursym] = SYM_TRUE;
    276  1.5  lukem 			else {
    277  1.5  lukem 				if (reject != REJ_YES)
    278  1.5  lukem 					reject = ignore[cursym] ? REJ_IGNORE : REJ_YES;
    279  1.5  lukem 				insym[cursym] = SYM_FALSE;
    280  1.5  lukem 			}
    281  1.5  lukem 			if (ignore[cursym])
    282  1.5  lukem 				flushline(YES);
    283  1.5  lukem 			else {
    284  1.5  lukem 				exitstat = 1;
    285  1.5  lukem 				flushline(NO);
    286  1.5  lukem 			}
    287  1.5  lukem 			if ((doret = doif(cursym, IN_IF, thisreject, depth + 1)) != NO_ERR)
    288  1.5  lukem 				return error(doret, stline, depth);
    289  1.5  lukem 			break;
    290  1.5  lukem 
    291  1.5  lukem 		case LT_IF:
    292  1.5  lukem 		case LT_OTHER:
    293  1.5  lukem 			flushline(YES);
    294  1.5  lukem 			if ((doret = doif(-1, IN_IF, reject, depth + 1)) != NO_ERR)
    295  1.5  lukem 				return error(doret, stline, depth);
    296  1.5  lukem 			break;
    297  1.5  lukem 
    298  1.5  lukem 		case LT_ELSE:
    299  1.5  lukem 			if (inif != IN_IF)
    300  1.5  lukem 				return error(ELSE_ERR, linenum, depth);
    301  1.5  lukem 			inif = IN_ELSE;
    302  1.5  lukem 			if (thissym >= 0) {
    303  1.5  lukem 				if (insym[thissym] == SYM_TRUE) {
    304  1.5  lukem 					reject = ignore[thissym] ? REJ_IGNORE : REJ_YES;
    305  1.5  lukem 					insym[thissym] = SYM_FALSE;
    306  1.5  lukem 				} else {	/* (insym[thissym] ==
    307  1.5  lukem 						 * SYM_FALSE) */
    308  1.5  lukem 					reject = prevreject;
    309  1.5  lukem 					insym[thissym] = SYM_TRUE;
    310  1.5  lukem 				}
    311  1.5  lukem 				if (!ignore[thissym]) {
    312  1.5  lukem 					flushline(NO);
    313  1.5  lukem 					break;
    314  1.5  lukem 				}
    315  1.5  lukem 			}
    316  1.5  lukem 			flushline(YES);
    317  1.5  lukem 			break;
    318  1.5  lukem 
    319  1.5  lukem 		case LT_ENDIF:
    320  1.5  lukem 			if (inif == IN_NONE)
    321  1.5  lukem 				return error(ENDIF_ERR, linenum, depth);
    322  1.5  lukem 			if (thissym >= 0) {
    323  1.5  lukem 				insym[thissym] = SYM_INACTIVE;
    324  1.5  lukem 				reject = prevreject;
    325  1.5  lukem 				if (!ignore[thissym]) {
    326  1.5  lukem 					flushline(NO);
    327  1.5  lukem 					return NO_ERR;
    328  1.5  lukem 				}
    329  1.5  lukem 			}
    330  1.5  lukem 			flushline(YES);
    331  1.5  lukem 			return NO_ERR;
    332  1.5  lukem 
    333  1.5  lukem 		case LT_LEOF:{
    334  1.5  lukem 				int     err;
    335  1.5  lukem 				err = incomment
    336  1.5  lukem 				    ? CEOF_ERR
    337  1.5  lukem 				    : inquote == QUOTE_SINGLE
    338  1.5  lukem 				    ? Q1EOF_ERR
    339  1.5  lukem 				    : inquote == QUOTE_DOUBLE
    340  1.5  lukem 				    ? Q2EOF_ERR
    341  1.5  lukem 				    : NO_ERR;
    342  1.5  lukem 				if (inif != IN_NONE) {
    343  1.5  lukem 					if (err != NO_ERR)
    344  1.5  lukem 						(void) error(err, stqcline, depth);
    345  1.5  lukem 					return error(IEOF_ERR, stline, depth);
    346  1.5  lukem 				} else
    347  1.5  lukem 					if (err != NO_ERR)
    348  1.5  lukem 						return error(err, stqcline, depth);
    349  1.5  lukem 					else
    350  1.5  lukem 						return NO_ERR;
    351  1.5  lukem 			}
    352  1.1    cgd 		}
    353  1.1    cgd 	}
    354  1.1    cgd }
    355  1.1    cgd #define endsym(c) (!isalpha (c) && !isdigit (c) && c != '_')
    356  1.1    cgd 
    357  1.1    cgd #define MAXLINE 256
    358  1.5  lukem char    tline[MAXLINE] BSS;
    359  1.1    cgd 
    360  1.1    cgd Linetype
    361  1.5  lukem checkline(cursym)
    362  1.5  lukem 	int    *cursym;		/* if LT_TRUE or LT_FALSE returned, set this
    363  1.5  lukem 				 * to sym index */
    364  1.5  lukem {
    365  1.5  lukem 	char   *cp;
    366  1.5  lukem 	char   *symp;
    367  1.5  lukem 	char   *scp;
    368  1.5  lukem 	Linetype retval;
    369  1.5  lukem #define KWSIZE 8
    370  1.5  lukem 	char    keyword[KWSIZE];
    371  1.5  lukem 
    372  1.5  lukem 	linenum++;
    373  1.5  lukem 	if (getlin(tline, sizeof tline, input, NO) == EOF)
    374  1.5  lukem 		return LT_LEOF;
    375  1.5  lukem 
    376  1.5  lukem 	retval = LT_PLAIN;
    377  1.5  lukem 	if (*(cp = tline) != '#'
    378  1.5  lukem 	    || incomment
    379  1.5  lukem 	    || inquote == QUOTE_SINGLE
    380  1.5  lukem 	    || inquote == QUOTE_DOUBLE
    381  1.5  lukem 	    )
    382  1.5  lukem 		goto eol;
    383  1.5  lukem 
    384  1.5  lukem 	cp = skipcomment(++cp);
    385  1.5  lukem 	symp = keyword;
    386  1.5  lukem 	while (!endsym(*cp)) {
    387  1.5  lukem 		*symp = *cp++;
    388  1.5  lukem 		if (++symp >= &keyword[KWSIZE])
    389  1.5  lukem 			goto eol;
    390  1.1    cgd 	}
    391  1.5  lukem 	*symp = '\0';
    392  1.1    cgd 
    393  1.5  lukem 	if (strcmp(keyword, "ifdef") == 0) {
    394  1.5  lukem 		retval = YES;
    395  1.5  lukem 		goto ifdef;
    396  1.5  lukem 	} else
    397  1.5  lukem 		if (strcmp(keyword, "ifndef") == 0) {
    398  1.5  lukem 			retval = NO;
    399  1.5  lukem 	ifdef:
    400  1.5  lukem 			scp = cp = skipcomment(++cp);
    401  1.5  lukem 			if (incomment) {
    402  1.5  lukem 				retval = LT_PLAIN;
    403  1.5  lukem 				goto eol;
    404  1.5  lukem 			} {
    405  1.5  lukem 				int     symind;
    406  1.5  lukem 
    407  1.5  lukem 				if ((symind = findsym(scp)) >= 0)
    408  1.5  lukem 					retval = (retval ^ true[*cursym = symind])
    409  1.5  lukem 					    ? LT_FALSE : LT_TRUE;
    410  1.5  lukem 				else
    411  1.5  lukem 					retval = LT_OTHER;
    412  1.5  lukem 			}
    413  1.5  lukem 		} else
    414  1.5  lukem 			if (strcmp(keyword, "if") == 0)
    415  1.5  lukem 				retval = LT_IF;
    416  1.5  lukem 			else
    417  1.5  lukem 				if (strcmp(keyword, "else") == 0)
    418  1.5  lukem 					retval = LT_ELSE;
    419  1.5  lukem 				else
    420  1.5  lukem 					if (strcmp(keyword, "endif") == 0)
    421  1.5  lukem 						retval = LT_ENDIF;
    422  1.5  lukem 
    423  1.5  lukem eol:
    424  1.5  lukem 	if (!text && reject != REJ_IGNORE)
    425  1.5  lukem 		for (; *cp;) {
    426  1.5  lukem 			if (incomment)
    427  1.5  lukem 				cp = skipcomment(cp);
    428  1.5  lukem 			else
    429  1.5  lukem 				if (inquote == QUOTE_SINGLE)
    430  1.5  lukem 					cp = skipquote(cp, QUOTE_SINGLE);
    431  1.5  lukem 				else
    432  1.5  lukem 					if (inquote == QUOTE_DOUBLE)
    433  1.5  lukem 						cp = skipquote(cp, QUOTE_DOUBLE);
    434  1.5  lukem 					else
    435  1.5  lukem 						if (*cp == '/' && cp[1] == '*')
    436  1.5  lukem 							cp = skipcomment(cp);
    437  1.5  lukem 						else
    438  1.5  lukem 							if (*cp == '\'')
    439  1.5  lukem 								cp = skipquote(cp, QUOTE_SINGLE);
    440  1.5  lukem 							else
    441  1.5  lukem 								if (*cp == '"')
    442  1.5  lukem 									cp = skipquote(cp, QUOTE_DOUBLE);
    443  1.5  lukem 								else
    444  1.5  lukem 									cp++;
    445  1.5  lukem 		}
    446  1.5  lukem 	return retval;
    447  1.1    cgd }
    448  1.1    cgd /*
    449  1.1    cgd  *  Skip over comments and stop at the next charaacter
    450  1.1    cgd  *  position that is not whitespace.
    451  1.1    cgd  */
    452  1.5  lukem char   *
    453  1.5  lukem skipcomment(cp)
    454  1.5  lukem 	char   *cp;
    455  1.5  lukem {
    456  1.5  lukem 	if (incomment)
    457  1.5  lukem 		goto inside;
    458  1.5  lukem 	for (;; cp++) {
    459  1.5  lukem 		while (*cp == ' ' || *cp == '\t')
    460  1.5  lukem 			cp++;
    461  1.5  lukem 		if (text)
    462  1.5  lukem 			return cp;
    463  1.5  lukem 		if (cp[0] != '/'
    464  1.5  lukem 		    || cp[1] != '*'
    465  1.5  lukem 		    )
    466  1.5  lukem 			return cp;
    467  1.5  lukem 		cp += 2;
    468  1.5  lukem 		if (!incomment) {
    469  1.5  lukem 			incomment = YES;
    470  1.5  lukem 			stqcline = linenum;
    471  1.5  lukem 		}
    472  1.5  lukem inside:
    473  1.5  lukem 		for (;;) {
    474  1.5  lukem 			for (; *cp != '*'; cp++)
    475  1.5  lukem 				if (*cp == '\0')
    476  1.5  lukem 					return cp;
    477  1.5  lukem 			if (*++cp == '/') {
    478  1.5  lukem 				incomment = NO;
    479  1.5  lukem 				break;
    480  1.5  lukem 			}
    481  1.5  lukem 		}
    482  1.1    cgd 	}
    483  1.1    cgd }
    484  1.1    cgd /*
    485  1.1    cgd  *  Skip over a quoted string or character and stop at the next charaacter
    486  1.1    cgd  *  position that is not whitespace.
    487  1.1    cgd  */
    488  1.5  lukem char   *
    489  1.5  lukem skipquote(cp, type)
    490  1.5  lukem 	char   *cp;
    491  1.5  lukem 	int     type;
    492  1.5  lukem {
    493  1.5  lukem 	char    qchar;
    494  1.5  lukem 
    495  1.5  lukem 	qchar = type == QUOTE_SINGLE ? '\'' : '"';
    496  1.5  lukem 
    497  1.5  lukem 	if (inquote == type)
    498  1.5  lukem 		goto inside;
    499  1.5  lukem 	for (;; cp++) {
    500  1.5  lukem 		if (*cp != qchar)
    501  1.5  lukem 			return cp;
    502  1.5  lukem 		cp++;
    503  1.5  lukem 		inquote = type;
    504  1.5  lukem 		stqcline = linenum;
    505  1.5  lukem inside:
    506  1.5  lukem 		for (;; cp++) {
    507  1.5  lukem 			if (*cp == qchar)
    508  1.5  lukem 				break;
    509  1.5  lukem 			if (*cp == '\0' || (*cp == '\\' && *++cp == '\0'))
    510  1.5  lukem 				return cp;
    511  1.5  lukem 		}
    512  1.5  lukem 		inquote = QUOTE_NONE;
    513  1.1    cgd 	}
    514  1.1    cgd }
    515  1.1    cgd /*
    516  1.1    cgd  *  findsym - look for the symbol in the symbol table.
    517  1.1    cgd  *            if found, return symbol table index,
    518  1.1    cgd  *            else return -1.
    519  1.1    cgd  */
    520  1.1    cgd int
    521  1.5  lukem findsym(str)
    522  1.5  lukem 	char   *str;
    523  1.1    cgd {
    524  1.5  lukem 	char   *cp;
    525  1.5  lukem 	char   *symp;
    526  1.5  lukem 	int     symind;
    527  1.5  lukem 	char    chr;
    528  1.5  lukem 
    529  1.5  lukem 	for (symind = 0; symind < nsyms; ++symind) {
    530  1.5  lukem 		if (insym[symind] == SYM_INACTIVE) {
    531  1.5  lukem 			for (symp = symname[symind], cp = str
    532  1.5  lukem 			    ; *symp && *cp == *symp
    533  1.5  lukem 			    ; cp++, symp++
    534  1.5  lukem 			    )
    535  1.5  lukem 				continue;
    536  1.5  lukem 			chr = *cp;
    537  1.5  lukem 			if (*symp == '\0' && endsym(chr))
    538  1.5  lukem 				return symind;
    539  1.5  lukem 		}
    540  1.1    cgd 	}
    541  1.5  lukem 	return -1;
    542  1.1    cgd }
    543  1.1    cgd /*
    544  1.1    cgd  *   getlin - expands tabs if asked for
    545  1.1    cgd  *            and (if compiled in) treats form-feed as an end-of-line
    546  1.1    cgd  */
    547  1.1    cgd int
    548  1.5  lukem getlin(line, maxline, inp, expandtabs)
    549  1.5  lukem 	char   *line;
    550  1.5  lukem 	int     maxline;
    551  1.5  lukem 	FILE   *inp;
    552  1.5  lukem 	int     expandtabs;
    553  1.5  lukem {
    554  1.5  lukem 	int     tmp;
    555  1.5  lukem 	int     num;
    556  1.5  lukem 	int     chr;
    557  1.1    cgd #ifdef  FFSPECIAL
    558  1.5  lukem 	static char havechar = NO;	/* have leftover char from last time */
    559  1.5  lukem 	static char svchar BSS;
    560  1.5  lukem #endif				/* FFSPECIAL */
    561  1.1    cgd 
    562  1.5  lukem 	num = 0;
    563  1.1    cgd #ifdef  FFSPECIAL
    564  1.5  lukem 	if (havechar) {
    565  1.5  lukem 		havechar = NO;
    566  1.5  lukem 		chr = svchar;
    567  1.5  lukem 		goto ent;
    568  1.5  lukem 	}
    569  1.5  lukem #endif				/* FFSPECIAL */
    570  1.5  lukem 	while (num + 8 < maxline) {	/* leave room for tab */
    571  1.5  lukem 		chr = getc(inp);
    572  1.5  lukem 		if (isprint(chr)) {
    573  1.1    cgd #ifdef  FFSPECIAL
    574  1.5  lukem 	ent:
    575  1.5  lukem #endif				/* FFSPECIAL */
    576  1.5  lukem 			*line++ = chr;
    577  1.5  lukem 			num++;
    578  1.5  lukem 		} else
    579  1.5  lukem 			switch (chr) {
    580  1.5  lukem 			case EOF:
    581  1.5  lukem 				return EOF;
    582  1.5  lukem 
    583  1.5  lukem 			case '\t':
    584  1.5  lukem 				if (expandtabs) {
    585  1.5  lukem 					num += tmp = 8 - (num & 7);
    586  1.5  lukem 					do
    587  1.5  lukem 						*line++ = ' ';
    588  1.5  lukem 					while (--tmp);
    589  1.5  lukem 					break;
    590  1.5  lukem 				}
    591  1.5  lukem 			default:
    592  1.5  lukem 				*line++ = chr;
    593  1.5  lukem 				num++;
    594  1.5  lukem 				break;
    595  1.5  lukem 
    596  1.5  lukem 			case '\n':
    597  1.5  lukem 				*line = '\n';
    598  1.5  lukem 				num++;
    599  1.5  lukem 				goto end;
    600  1.1    cgd 
    601  1.1    cgd #ifdef  FFSPECIAL
    602  1.5  lukem 			case '\f':
    603  1.5  lukem 				if (++num == 1)
    604  1.5  lukem 					*line = '\f';
    605  1.5  lukem 				else {
    606  1.5  lukem 					*line = '\n';
    607  1.5  lukem 					havechar = YES;
    608  1.5  lukem 					svchar = chr;
    609  1.5  lukem 				}
    610  1.5  lukem 				goto end;
    611  1.5  lukem #endif				/* FFSPECIAL */
    612  1.5  lukem 			}
    613  1.5  lukem 	}
    614  1.5  lukem end:
    615  1.5  lukem 	*++line = '\0';
    616  1.5  lukem 	return num;
    617  1.1    cgd }
    618  1.1    cgd 
    619  1.4    jtc void
    620  1.5  lukem flushline(keep)
    621  1.5  lukem 	Bool    keep;
    622  1.1    cgd {
    623  1.5  lukem 	if ((keep && reject != REJ_YES) ^ complement) {
    624  1.5  lukem 		char   *line = tline;
    625  1.5  lukem 		FILE   *out = stdout;
    626  1.5  lukem 		char    chr;
    627  1.5  lukem 
    628  1.5  lukem 		while ((chr = *line++) != 0)
    629  1.5  lukem 			putc(chr, out);
    630  1.5  lukem 	} else
    631  1.5  lukem 		if (lnblank)
    632  1.5  lukem 			putc('\n', stdout);
    633  1.5  lukem 	return;
    634  1.1    cgd }
    635  1.1    cgd 
    636  1.4    jtc void
    637  1.5  lukem prname()
    638  1.1    cgd {
    639  1.5  lukem 	fprintf(stderr, "%s: ", progname);
    640  1.5  lukem 	return;
    641  1.1    cgd }
    642  1.1    cgd 
    643  1.1    cgd int
    644  1.5  lukem error(err, line, depth)
    645  1.5  lukem 	int     err;		/* type of error & index into error string
    646  1.5  lukem 				 * array */
    647  1.5  lukem 	int     line;		/* line number */
    648  1.5  lukem 	int     depth;		/* how many ifdefs we are inside */
    649  1.1    cgd {
    650  1.5  lukem 	if (err == END_ERR)
    651  1.5  lukem 		return err;
    652  1.1    cgd 
    653  1.5  lukem 	prname();
    654  1.1    cgd 
    655  1.1    cgd #ifndef TESTING
    656  1.5  lukem 	fprintf(stderr, "Error in %s line %d: %s.\n", filename, line, errs[err]);
    657  1.5  lukem #else				/* TESTING */
    658  1.5  lukem 	fprintf(stderr, "Error in %s line %d: %s. ", filename, line, errs[err]);
    659  1.5  lukem 	fprintf(stderr, "ifdef depth: %d\n", depth);
    660  1.5  lukem #endif				/* TESTING */
    661  1.1    cgd 
    662  1.5  lukem 	exitstat = 2;
    663  1.5  lukem 	return depth > 1 ? IEOF_ERR : END_ERR;
    664  1.1    cgd }
    665