Home | History | Annotate | Line # | Download | only in unifdef
unifdef.c revision 1.10
      1  1.10    itojun /*	$NetBSD: unifdef.c,v 1.10 2003/07/04 04:26:47 itojun 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.10    itojun __RCSID("$NetBSD: unifdef.c,v 1.10 2003/07/04 04:26:47 itojun 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.8      matt #include <stdlib.h>
     67   1.8      matt #include <string.h>
     68   1.1       cgd #include <ctype.h>
     69   1.1       cgd 
     70   1.1       cgd #define BSS
     71   1.5     lukem FILE   *input;
     72   1.1       cgd #ifndef YES
     73   1.1       cgd #define YES 1
     74   1.1       cgd #define NO  0
     75   1.5     lukem #endif				/* YES */
     76   1.6  wsanchez #define C_COMMENT   1
     77   1.6  wsanchez #define CXX_COMMENT 2
     78   1.1       cgd typedef int Bool;
     79   1.1       cgd 
     80   1.5     lukem char   *progname BSS;
     81   1.5     lukem char   *filename BSS;
     82   1.5     lukem char text BSS;			/* -t option in effect: this is a text file */
     83   1.5     lukem char lnblank BSS;		/* -l option in effect: blank deleted lines */
     84   1.5     lukem char complement BSS;		/* -c option in effect: complement the
     85   1.5     lukem 				 * operation */
     86   1.1       cgd 
     87   1.1       cgd #define MAXSYMS 100
     88   1.5     lukem char   *symname[MAXSYMS] BSS;	/* symbol name */
     89   1.5     lukem char    true[MAXSYMS] BSS;	/* -Dsym */
     90   1.5     lukem char    ignore[MAXSYMS] BSS;	/* -iDsym or -iUsym */
     91   1.5     lukem char    insym[MAXSYMS] BSS;	/* state: false, inactive, true */
     92   1.5     lukem #define SYM_INACTIVE 0		/* symbol is currently inactive */
     93   1.5     lukem #define SYM_FALSE    1		/* symbol is currently false */
     94   1.5     lukem #define SYM_TRUE     2		/* symbol is currently true  */
     95   1.1       cgd 
     96   1.1       cgd char nsyms BSS;
     97   1.5     lukem char incomment BSS;		/* inside C comment */
     98   1.1       cgd 
     99   1.1       cgd #define QUOTE_NONE   0
    100   1.1       cgd #define QUOTE_SINGLE 1
    101   1.1       cgd #define QUOTE_DOUBLE 2
    102   1.5     lukem char inquote BSS;		/* inside single or double quotes */
    103   1.4       jtc int exitstat BSS;
    104   1.1       cgd 
    105   1.5     lukem int error __P((int, int, int));
    106   1.5     lukem int findsym __P((char *));
    107   1.5     lukem void flushline __P((Bool));
    108   1.5     lukem int getlin __P((char *, int, FILE *, int));
    109   1.5     lukem int main __P((int, char **));
    110   1.5     lukem void pfile __P((void));
    111   1.5     lukem void prname __P((void));
    112   1.4       jtc char   *skipcomment __P((char *));
    113   1.4       jtc char   *skipquote __P((char *, int));
    114   1.1       cgd 
    115   1.4       jtc int
    116   1.5     lukem main(argc, argv)
    117   1.5     lukem 	int     argc;
    118   1.5     lukem 	char  **argv;
    119   1.5     lukem {
    120   1.5     lukem 	char  **curarg;
    121   1.5     lukem 	char   *cp;
    122   1.5     lukem 	char   *cp1;
    123   1.5     lukem 	char    ignorethis;
    124   1.5     lukem 
    125   1.5     lukem 	progname = argv[0][0] ? argv[0] : "unifdef";
    126   1.5     lukem 
    127   1.5     lukem 	for (curarg = &argv[1]; --argc > 0; curarg++) {
    128   1.5     lukem 		if (*(cp1 = cp = *curarg) != '-')
    129   1.5     lukem 			break;
    130   1.5     lukem 		if (*++cp1 == 'i') {
    131   1.5     lukem 			ignorethis = YES;
    132   1.5     lukem 			cp1++;
    133   1.5     lukem 		} else
    134   1.5     lukem 			ignorethis = NO;
    135  1.10    itojun 		if ((*cp1 == 'D' || *cp1 == 'U') && cp1[1] != '\0') {
    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.10    itojun 		} else if (ignorethis)
    151  1.10    itojun 			goto unrec;
    152  1.10    itojun 		else if (strcmp(&cp[1], "t") == 0)
    153  1.10    itojun 			text = YES;
    154  1.10    itojun 		else if (strcmp(&cp[1], "l") == 0)
    155  1.10    itojun 			lnblank = YES;
    156  1.10    itojun 		else if (strcmp(&cp[1], "c") == 0)
    157  1.10    itojun 			complement = YES;
    158  1.10    itojun 		else {
    159  1.10    itojun 	unrec:
    160  1.10    itojun 			prname();
    161  1.10    itojun 			fprintf(stderr, "unrecognized option: %s\n", cp);
    162  1.10    itojun 			goto usage;
    163  1.10    itojun 		}
    164   1.1       cgd 	}
    165   1.5     lukem 	if (nsyms == 0) {
    166   1.5     lukem usage:
    167   1.5     lukem 		fprintf(stderr, "\
    168   1.1       cgd Usage: %s [-l] [-t] [-c] [[-Dsym] [-Usym] [-iDsym] [-iUsym]]... [file]\n\
    169   1.1       cgd     At least one arg from [-D -U -iD -iU] is required\n", progname);
    170   1.5     lukem 		exit(2);
    171   1.1       cgd 	}
    172   1.5     lukem 	if (argc > 1) {
    173   1.5     lukem 		prname();
    174   1.5     lukem 		fprintf(stderr, "can only do one file.\n");
    175   1.5     lukem 	} else
    176   1.5     lukem 		if (argc == 1) {
    177   1.5     lukem 			filename = *curarg;
    178   1.5     lukem 			if ((input = fopen(filename, "r")) != NULL) {
    179   1.5     lukem 				pfile();
    180   1.5     lukem 				(void) fclose(input);
    181   1.5     lukem 			} else {
    182   1.5     lukem 				prname();
    183   1.5     lukem 				fprintf(stderr, "can't open ");
    184   1.5     lukem 				perror(*curarg);
    185   1.5     lukem 			}
    186   1.5     lukem 		} else {
    187   1.5     lukem 			filename = "[stdin]";
    188   1.5     lukem 			input = stdin;
    189   1.5     lukem 			pfile();
    190   1.5     lukem 		}
    191   1.1       cgd 
    192   1.5     lukem 	(void) fflush(stdout);
    193   1.5     lukem 	exit(exitstat);
    194   1.1       cgd }
    195   1.1       cgd /* types of input lines: */
    196   1.1       cgd typedef int Linetype;
    197   1.5     lukem #define LT_PLAIN       0	/* ordinary line */
    198   1.5     lukem #define LT_TRUE        1	/* a true  #ifdef of a symbol known to us */
    199   1.5     lukem #define LT_FALSE       2	/* a false #ifdef of a symbol known to us */
    200   1.5     lukem #define LT_OTHER       3	/* an #ifdef of a symbol not known to us */
    201   1.5     lukem #define LT_IF          4	/* an #ifdef of a symbol not known to us */
    202   1.5     lukem #define LT_ELSE        5	/* #else */
    203   1.5     lukem #define LT_ENDIF       6	/* #endif */
    204   1.5     lukem #define LT_LEOF        7	/* end of file */
    205   1.4       jtc Linetype checkline __P((int *));
    206   1.1       cgd 
    207   1.1       cgd typedef int Reject_level;
    208   1.5     lukem Reject_level reject BSS;	/* 0 or 1: pass thru; 1 or 2: ignore comments */
    209   1.1       cgd #define REJ_NO          0
    210   1.1       cgd #define REJ_IGNORE      1
    211   1.1       cgd #define REJ_YES         2
    212   1.4       jtc int doif __P((int, int, Reject_level, int));
    213   1.1       cgd 
    214   1.5     lukem int linenum BSS;		/* current line number */
    215   1.5     lukem int stqcline BSS;		/* start of current coment or quote */
    216   1.5     lukem char   *errs[] = {
    217   1.1       cgd #define NO_ERR      0
    218   1.5     lukem 	"",
    219   1.1       cgd #define END_ERR     1
    220   1.5     lukem 	"",
    221   1.1       cgd #define ELSE_ERR    2
    222   1.5     lukem 	"Inappropriate else",
    223   1.1       cgd #define ENDIF_ERR   3
    224   1.5     lukem 	"Inappropriate endif",
    225   1.1       cgd #define IEOF_ERR    4
    226   1.5     lukem 	"Premature EOF in ifdef",
    227   1.1       cgd #define CEOF_ERR    5
    228   1.5     lukem 	"Premature EOF in comment",
    229   1.1       cgd #define Q1EOF_ERR   6
    230   1.5     lukem 	"Premature EOF in quoted character",
    231   1.1       cgd #define Q2EOF_ERR   7
    232   1.5     lukem 	"Premature EOF in quoted string"
    233   1.1       cgd };
    234   1.1       cgd /* States for inif arg to doif */
    235   1.1       cgd #define IN_NONE 0
    236   1.1       cgd #define IN_IF   1
    237   1.1       cgd #define IN_ELSE 2
    238   1.1       cgd 
    239   1.4       jtc void
    240   1.5     lukem pfile()
    241   1.1       cgd {
    242   1.5     lukem 	reject = REJ_NO;
    243   1.5     lukem 	(void) doif(-1, IN_NONE, reject, 0);
    244   1.5     lukem 	return;
    245   1.1       cgd }
    246   1.1       cgd 
    247   1.1       cgd int
    248   1.5     lukem doif(thissym, inif, prevreject, depth)
    249   1.5     lukem 	int     thissym;	/* index of the symbol who was last ifdef'ed */
    250   1.5     lukem 	int     inif;		/* YES or NO we are inside an ifdef */
    251   1.5     lukem 	Reject_level prevreject;/* previous value of reject */
    252   1.5     lukem 	int     depth;		/* depth of ifdef's */
    253   1.5     lukem {
    254   1.5     lukem 	Linetype lineval;
    255   1.5     lukem 	Reject_level thisreject;
    256   1.5     lukem 	int     doret;		/* tmp return value of doif */
    257   1.5     lukem 	int     cursym;		/* index of the symbol returned by checkline */
    258   1.5     lukem 	int     stline;		/* line number when called this time */
    259   1.5     lukem 
    260   1.5     lukem 	stline = linenum;
    261   1.5     lukem 	for (;;) {
    262   1.5     lukem 		switch (lineval = checkline(&cursym)) {
    263   1.5     lukem 		case LT_PLAIN:
    264   1.5     lukem 			flushline(YES);
    265   1.5     lukem 			break;
    266   1.5     lukem 
    267   1.5     lukem 		case LT_TRUE:
    268   1.5     lukem 		case LT_FALSE:
    269   1.5     lukem 			thisreject = reject;
    270   1.5     lukem 			if (lineval == LT_TRUE)
    271   1.5     lukem 				insym[cursym] = SYM_TRUE;
    272   1.5     lukem 			else {
    273   1.5     lukem 				if (reject != REJ_YES)
    274   1.5     lukem 					reject = ignore[cursym] ? REJ_IGNORE : REJ_YES;
    275   1.5     lukem 				insym[cursym] = SYM_FALSE;
    276   1.5     lukem 			}
    277   1.5     lukem 			if (ignore[cursym])
    278   1.5     lukem 				flushline(YES);
    279   1.5     lukem 			else {
    280   1.5     lukem 				exitstat = 1;
    281   1.5     lukem 				flushline(NO);
    282   1.5     lukem 			}
    283   1.5     lukem 			if ((doret = doif(cursym, IN_IF, thisreject, depth + 1)) != NO_ERR)
    284   1.5     lukem 				return error(doret, stline, depth);
    285   1.5     lukem 			break;
    286   1.5     lukem 
    287   1.5     lukem 		case LT_IF:
    288   1.5     lukem 		case LT_OTHER:
    289   1.5     lukem 			flushline(YES);
    290   1.5     lukem 			if ((doret = doif(-1, IN_IF, reject, depth + 1)) != NO_ERR)
    291   1.5     lukem 				return error(doret, stline, depth);
    292   1.5     lukem 			break;
    293   1.5     lukem 
    294   1.5     lukem 		case LT_ELSE:
    295   1.5     lukem 			if (inif != IN_IF)
    296   1.5     lukem 				return error(ELSE_ERR, linenum, depth);
    297   1.5     lukem 			inif = IN_ELSE;
    298   1.5     lukem 			if (thissym >= 0) {
    299   1.5     lukem 				if (insym[thissym] == SYM_TRUE) {
    300   1.5     lukem 					reject = ignore[thissym] ? REJ_IGNORE : REJ_YES;
    301   1.5     lukem 					insym[thissym] = SYM_FALSE;
    302   1.5     lukem 				} else {	/* (insym[thissym] ==
    303   1.5     lukem 						 * SYM_FALSE) */
    304   1.5     lukem 					reject = prevreject;
    305   1.5     lukem 					insym[thissym] = SYM_TRUE;
    306   1.5     lukem 				}
    307   1.5     lukem 				if (!ignore[thissym]) {
    308   1.5     lukem 					flushline(NO);
    309   1.5     lukem 					break;
    310   1.5     lukem 				}
    311   1.5     lukem 			}
    312   1.5     lukem 			flushline(YES);
    313   1.5     lukem 			break;
    314   1.5     lukem 
    315   1.5     lukem 		case LT_ENDIF:
    316   1.5     lukem 			if (inif == IN_NONE)
    317   1.5     lukem 				return error(ENDIF_ERR, linenum, depth);
    318   1.5     lukem 			if (thissym >= 0) {
    319   1.5     lukem 				insym[thissym] = SYM_INACTIVE;
    320   1.5     lukem 				reject = prevreject;
    321   1.5     lukem 				if (!ignore[thissym]) {
    322   1.5     lukem 					flushline(NO);
    323   1.5     lukem 					return NO_ERR;
    324   1.5     lukem 				}
    325   1.5     lukem 			}
    326   1.5     lukem 			flushline(YES);
    327   1.5     lukem 			return NO_ERR;
    328   1.5     lukem 
    329   1.5     lukem 		case LT_LEOF:{
    330   1.5     lukem 				int     err;
    331   1.5     lukem 				err = incomment
    332   1.5     lukem 				    ? CEOF_ERR
    333   1.5     lukem 				    : inquote == QUOTE_SINGLE
    334   1.5     lukem 				    ? Q1EOF_ERR
    335   1.5     lukem 				    : inquote == QUOTE_DOUBLE
    336   1.5     lukem 				    ? Q2EOF_ERR
    337   1.5     lukem 				    : NO_ERR;
    338   1.5     lukem 				if (inif != IN_NONE) {
    339   1.5     lukem 					if (err != NO_ERR)
    340   1.5     lukem 						(void) error(err, stqcline, depth);
    341   1.5     lukem 					return error(IEOF_ERR, stline, depth);
    342   1.5     lukem 				} else
    343   1.5     lukem 					if (err != NO_ERR)
    344   1.5     lukem 						return error(err, stqcline, depth);
    345   1.5     lukem 					else
    346   1.5     lukem 						return NO_ERR;
    347   1.5     lukem 			}
    348   1.1       cgd 		}
    349   1.1       cgd 	}
    350   1.1       cgd }
    351   1.7  christos #define endsym(c) (!isalpha ((unsigned char)c) && !isdigit ((unsigned char)c) && c != '_')
    352   1.1       cgd 
    353   1.1       cgd #define MAXLINE 256
    354   1.5     lukem char    tline[MAXLINE] BSS;
    355   1.1       cgd 
    356   1.1       cgd Linetype
    357   1.5     lukem checkline(cursym)
    358   1.5     lukem 	int    *cursym;		/* if LT_TRUE or LT_FALSE returned, set this
    359   1.5     lukem 				 * to sym index */
    360   1.5     lukem {
    361   1.5     lukem 	char   *cp;
    362   1.5     lukem 	char   *symp;
    363   1.5     lukem 	char   *scp;
    364   1.5     lukem 	Linetype retval;
    365   1.5     lukem #define KWSIZE 8
    366   1.5     lukem 	char    keyword[KWSIZE];
    367   1.5     lukem 
    368   1.5     lukem 	linenum++;
    369   1.5     lukem 	if (getlin(tline, sizeof tline, input, NO) == EOF)
    370   1.5     lukem 		return LT_LEOF;
    371   1.5     lukem 
    372   1.5     lukem 	retval = LT_PLAIN;
    373   1.9    itojun 	if (*(cp = tline) != '#' || incomment || inquote == QUOTE_SINGLE ||
    374   1.9    itojun 	    inquote == QUOTE_DOUBLE)
    375   1.5     lukem 		goto eol;
    376   1.5     lukem 
    377   1.5     lukem 	cp = skipcomment(++cp);
    378   1.5     lukem 	symp = keyword;
    379   1.5     lukem 	while (!endsym(*cp)) {
    380   1.5     lukem 		*symp = *cp++;
    381   1.5     lukem 		if (++symp >= &keyword[KWSIZE])
    382   1.5     lukem 			goto eol;
    383   1.1       cgd 	}
    384   1.5     lukem 	*symp = '\0';
    385   1.1       cgd 
    386   1.5     lukem 	if (strcmp(keyword, "ifdef") == 0) {
    387   1.5     lukem 		retval = YES;
    388   1.5     lukem 		goto ifdef;
    389   1.9    itojun 	} else if (strcmp(keyword, "ifndef") == 0) {
    390   1.9    itojun 		retval = NO;
    391   1.5     lukem 	ifdef:
    392   1.9    itojun 		scp = cp = skipcomment(++cp);
    393   1.9    itojun 		if (incomment) {
    394   1.9    itojun 			retval = LT_PLAIN;
    395   1.9    itojun 			goto eol;
    396   1.9    itojun 		}
    397   1.9    itojun 		{
    398   1.9    itojun 			int     symind;
    399   1.9    itojun 
    400   1.9    itojun 			if ((symind = findsym(scp)) >= 0)
    401   1.9    itojun 				retval = (retval ^ true[*cursym = symind])
    402   1.9    itojun 				    ? LT_FALSE : LT_TRUE;
    403   1.5     lukem 			else
    404   1.9    itojun 				retval = LT_OTHER;
    405   1.9    itojun 		}
    406   1.9    itojun 	} else if (strcmp(keyword, "if") == 0)
    407   1.9    itojun 		retval = LT_IF;
    408   1.9    itojun 	else if (strcmp(keyword, "else") == 0)
    409   1.9    itojun 		retval = LT_ELSE;
    410   1.9    itojun 	else if (strcmp(keyword, "endif") == 0)
    411   1.9    itojun 		retval = LT_ENDIF;
    412   1.5     lukem 
    413   1.5     lukem eol:
    414   1.5     lukem 	if (!text && reject != REJ_IGNORE)
    415   1.5     lukem 		for (; *cp;) {
    416   1.5     lukem 			if (incomment)
    417   1.5     lukem 				cp = skipcomment(cp);
    418   1.9    itojun 			else if (inquote == QUOTE_SINGLE)
    419   1.9    itojun 				cp = skipquote(cp, QUOTE_SINGLE);
    420   1.9    itojun 			else if (inquote == QUOTE_DOUBLE)
    421   1.9    itojun 				cp = skipquote(cp, QUOTE_DOUBLE);
    422   1.9    itojun 			else if (*cp == '/' && (cp[1] == '*' || cp[1] == '/'))
    423   1.9    itojun 				cp = skipcomment(cp);
    424   1.9    itojun 			else if (*cp == '\'')
    425   1.9    itojun 				cp = skipquote(cp, QUOTE_SINGLE);
    426   1.9    itojun 			else if (*cp == '"')
    427   1.9    itojun 				cp = skipquote(cp, QUOTE_DOUBLE);
    428   1.5     lukem 			else
    429   1.9    itojun 				cp++;
    430   1.5     lukem 		}
    431   1.5     lukem 	return retval;
    432   1.1       cgd }
    433   1.1       cgd /*
    434   1.1       cgd  *  Skip over comments and stop at the next charaacter
    435   1.1       cgd  *  position that is not whitespace.
    436   1.1       cgd  */
    437   1.5     lukem char   *
    438   1.5     lukem skipcomment(cp)
    439   1.5     lukem 	char   *cp;
    440   1.5     lukem {
    441   1.5     lukem 	if (incomment)
    442   1.5     lukem 		goto inside;
    443   1.5     lukem 	for (;; cp++) {
    444   1.5     lukem 		while (*cp == ' ' || *cp == '\t')
    445   1.5     lukem 			cp++;
    446   1.5     lukem 		if (text)
    447   1.5     lukem 			return cp;
    448   1.6  wsanchez 		if (cp[0] != '/')
    449   1.6  wsanchez 			return cp;
    450   1.6  wsanchez 
    451   1.6  wsanchez 		if (cp[1] == '*') {
    452   1.6  wsanchez 			if (!incomment) {
    453   1.6  wsanchez 				incomment = C_COMMENT;
    454   1.6  wsanchez 				stqcline = linenum;
    455   1.6  wsanchez 			}
    456   1.6  wsanchez 		} else if (cp[1] == '/') {
    457   1.6  wsanchez 			if (!incomment) {
    458   1.6  wsanchez 				incomment = CXX_COMMENT;
    459   1.6  wsanchez 				stqcline = linenum;
    460   1.6  wsanchez 			}
    461   1.6  wsanchez 		} else
    462   1.5     lukem 			return cp;
    463   1.6  wsanchez 
    464   1.5     lukem 		cp += 2;
    465   1.6  wsanchez inside:
    466   1.6  wsanchez 		if (incomment == C_COMMENT) {
    467   1.6  wsanchez 			for (;;) {
    468   1.6  wsanchez 				for (; *cp != '*'; cp++)
    469   1.6  wsanchez 					if (*cp == '\0')
    470   1.6  wsanchez 						return cp;
    471   1.6  wsanchez 				if (*++cp == '/') {
    472   1.6  wsanchez 					incomment = NO;
    473   1.6  wsanchez 					break;
    474   1.6  wsanchez 				}
    475   1.6  wsanchez 			}
    476   1.5     lukem 		}
    477   1.6  wsanchez 		else if (incomment == CXX_COMMENT) {
    478   1.6  wsanchez 			for (; *cp != '\n'; cp++)
    479   1.5     lukem 				if (*cp == '\0')
    480   1.5     lukem 					return cp;
    481   1.6  wsanchez 			incomment = NO;
    482   1.5     lukem 		}
    483   1.1       cgd 	}
    484   1.1       cgd }
    485   1.1       cgd /*
    486   1.1       cgd  *  Skip over a quoted string or character and stop at the next charaacter
    487   1.1       cgd  *  position that is not whitespace.
    488   1.1       cgd  */
    489   1.5     lukem char   *
    490   1.5     lukem skipquote(cp, type)
    491   1.5     lukem 	char   *cp;
    492   1.5     lukem 	int     type;
    493   1.5     lukem {
    494   1.5     lukem 	char    qchar;
    495   1.5     lukem 
    496   1.5     lukem 	qchar = type == QUOTE_SINGLE ? '\'' : '"';
    497   1.5     lukem 
    498   1.5     lukem 	if (inquote == type)
    499   1.5     lukem 		goto inside;
    500   1.5     lukem 	for (;; cp++) {
    501   1.5     lukem 		if (*cp != qchar)
    502   1.5     lukem 			return cp;
    503   1.5     lukem 		cp++;
    504   1.5     lukem 		inquote = type;
    505   1.5     lukem 		stqcline = linenum;
    506   1.5     lukem inside:
    507   1.5     lukem 		for (;; cp++) {
    508   1.5     lukem 			if (*cp == qchar)
    509   1.5     lukem 				break;
    510   1.5     lukem 			if (*cp == '\0' || (*cp == '\\' && *++cp == '\0'))
    511   1.5     lukem 				return cp;
    512   1.5     lukem 		}
    513   1.5     lukem 		inquote = QUOTE_NONE;
    514   1.1       cgd 	}
    515   1.1       cgd }
    516   1.1       cgd /*
    517   1.1       cgd  *  findsym - look for the symbol in the symbol table.
    518   1.1       cgd  *            if found, return symbol table index,
    519   1.1       cgd  *            else return -1.
    520   1.1       cgd  */
    521   1.1       cgd int
    522   1.5     lukem findsym(str)
    523   1.5     lukem 	char   *str;
    524   1.1       cgd {
    525   1.5     lukem 	char   *cp;
    526   1.5     lukem 	char   *symp;
    527   1.5     lukem 	int     symind;
    528   1.5     lukem 	char    chr;
    529   1.5     lukem 
    530   1.5     lukem 	for (symind = 0; symind < nsyms; ++symind) {
    531   1.5     lukem 		if (insym[symind] == SYM_INACTIVE) {
    532   1.5     lukem 			for (symp = symname[symind], cp = str
    533   1.5     lukem 			    ; *symp && *cp == *symp
    534   1.5     lukem 			    ; cp++, symp++
    535   1.5     lukem 			    )
    536   1.5     lukem 				continue;
    537   1.5     lukem 			chr = *cp;
    538   1.5     lukem 			if (*symp == '\0' && endsym(chr))
    539   1.5     lukem 				return symind;
    540   1.5     lukem 		}
    541   1.1       cgd 	}
    542   1.5     lukem 	return -1;
    543   1.1       cgd }
    544   1.1       cgd /*
    545   1.1       cgd  *   getlin - expands tabs if asked for
    546   1.1       cgd  *            and (if compiled in) treats form-feed as an end-of-line
    547   1.1       cgd  */
    548   1.1       cgd int
    549   1.5     lukem getlin(line, maxline, inp, expandtabs)
    550   1.5     lukem 	char   *line;
    551   1.5     lukem 	int     maxline;
    552   1.5     lukem 	FILE   *inp;
    553   1.5     lukem 	int     expandtabs;
    554   1.5     lukem {
    555   1.5     lukem 	int     tmp;
    556   1.5     lukem 	int     num;
    557   1.5     lukem 	int     chr;
    558   1.1       cgd #ifdef  FFSPECIAL
    559   1.5     lukem 	static char havechar = NO;	/* have leftover char from last time */
    560   1.5     lukem 	static char svchar BSS;
    561   1.5     lukem #endif				/* FFSPECIAL */
    562   1.1       cgd 
    563   1.5     lukem 	num = 0;
    564   1.1       cgd #ifdef  FFSPECIAL
    565   1.5     lukem 	if (havechar) {
    566   1.5     lukem 		havechar = NO;
    567   1.5     lukem 		chr = svchar;
    568   1.5     lukem 		goto ent;
    569   1.5     lukem 	}
    570   1.5     lukem #endif				/* FFSPECIAL */
    571   1.5     lukem 	while (num + 8 < maxline) {	/* leave room for tab */
    572   1.5     lukem 		chr = getc(inp);
    573   1.5     lukem 		if (isprint(chr)) {
    574   1.1       cgd #ifdef  FFSPECIAL
    575   1.5     lukem 	ent:
    576   1.5     lukem #endif				/* FFSPECIAL */
    577   1.5     lukem 			*line++ = chr;
    578   1.5     lukem 			num++;
    579   1.5     lukem 		} else
    580   1.5     lukem 			switch (chr) {
    581   1.5     lukem 			case EOF:
    582   1.5     lukem 				return EOF;
    583   1.5     lukem 
    584   1.5     lukem 			case '\t':
    585   1.5     lukem 				if (expandtabs) {
    586   1.5     lukem 					num += tmp = 8 - (num & 7);
    587   1.5     lukem 					do
    588   1.5     lukem 						*line++ = ' ';
    589   1.5     lukem 					while (--tmp);
    590   1.5     lukem 					break;
    591   1.5     lukem 				}
    592   1.5     lukem 			default:
    593   1.5     lukem 				*line++ = chr;
    594   1.5     lukem 				num++;
    595   1.5     lukem 				break;
    596   1.5     lukem 
    597   1.5     lukem 			case '\n':
    598   1.5     lukem 				*line = '\n';
    599   1.5     lukem 				num++;
    600   1.5     lukem 				goto end;
    601   1.1       cgd 
    602   1.1       cgd #ifdef  FFSPECIAL
    603   1.5     lukem 			case '\f':
    604   1.5     lukem 				if (++num == 1)
    605   1.5     lukem 					*line = '\f';
    606   1.5     lukem 				else {
    607   1.5     lukem 					*line = '\n';
    608   1.5     lukem 					havechar = YES;
    609   1.5     lukem 					svchar = chr;
    610   1.5     lukem 				}
    611   1.5     lukem 				goto end;
    612   1.5     lukem #endif				/* FFSPECIAL */
    613   1.5     lukem 			}
    614   1.5     lukem 	}
    615   1.5     lukem end:
    616   1.5     lukem 	*++line = '\0';
    617   1.5     lukem 	return num;
    618   1.1       cgd }
    619   1.1       cgd 
    620   1.4       jtc void
    621   1.5     lukem flushline(keep)
    622   1.5     lukem 	Bool    keep;
    623   1.1       cgd {
    624   1.5     lukem 	if ((keep && reject != REJ_YES) ^ complement) {
    625   1.5     lukem 		char   *line = tline;
    626   1.5     lukem 		FILE   *out = stdout;
    627   1.5     lukem 		char    chr;
    628   1.5     lukem 
    629   1.5     lukem 		while ((chr = *line++) != 0)
    630   1.5     lukem 			putc(chr, out);
    631   1.5     lukem 	} else
    632   1.5     lukem 		if (lnblank)
    633   1.5     lukem 			putc('\n', stdout);
    634   1.5     lukem 	return;
    635   1.1       cgd }
    636   1.1       cgd 
    637   1.4       jtc void
    638   1.5     lukem prname()
    639   1.1       cgd {
    640   1.5     lukem 	fprintf(stderr, "%s: ", progname);
    641   1.5     lukem 	return;
    642   1.1       cgd }
    643   1.1       cgd 
    644   1.1       cgd int
    645   1.5     lukem error(err, line, depth)
    646   1.5     lukem 	int     err;		/* type of error & index into error string
    647   1.5     lukem 				 * array */
    648   1.5     lukem 	int     line;		/* line number */
    649   1.5     lukem 	int     depth;		/* how many ifdefs we are inside */
    650   1.1       cgd {
    651   1.5     lukem 	if (err == END_ERR)
    652   1.5     lukem 		return err;
    653   1.1       cgd 
    654   1.5     lukem 	prname();
    655   1.1       cgd 
    656   1.1       cgd #ifndef TESTING
    657   1.5     lukem 	fprintf(stderr, "Error in %s line %d: %s.\n", filename, line, errs[err]);
    658   1.5     lukem #else				/* TESTING */
    659   1.5     lukem 	fprintf(stderr, "Error in %s line %d: %s. ", filename, line, errs[err]);
    660   1.5     lukem 	fprintf(stderr, "ifdef depth: %d\n", depth);
    661   1.5     lukem #endif				/* TESTING */
    662   1.1       cgd 
    663   1.5     lukem 	exitstat = 2;
    664   1.5     lukem 	return depth > 1 ? IEOF_ERR : END_ERR;
    665   1.1       cgd }
    666