xref: /csrg-svn/old/cpp/cpp.c (revision 45525)
17978Srrh #ifndef lint
2*45525Sbostic static char sccsid[] = "@(#)cpp.c	1.22 11/07/90";
37978Srrh #endif lint
47982Srrh 
57978Srrh #ifdef FLEXNAMES
67978Srrh #define	NCPS	128
77978Srrh #else
87978Srrh #define	NCPS	8
97978Srrh #endif
107978Srrh 
1137777Sbostic #include <sys/param.h>
1237777Sbostic #include <stdio.h>
1337777Sbostic #include <ctype.h>
1437777Sbostic #include "pathnames.h"
1537777Sbostic 
167978Srrh /* C command
177978Srrh /* written by John F. Reiser
187978Srrh /* July/August 1978
197978Srrh */
207978Srrh 
217978Srrh #define STATIC
227978Srrh 
2325241Smckusick #define FIRSTOPEN -2
2425422Smckusick #define STDIN 0
257978Srrh #define READ 0
267978Srrh #define WRITE 1
277978Srrh #define SALT '#'
2819915Smckusick #if !defined BUFSIZ || BUFSIZ < 8192
2919915Smckusick #undef BUFSIZ
3019915Smckusick #define BUFSIZ 8192
317978Srrh #endif
327978Srrh 
337978Srrh char *pbeg,*pbuf,*pend;
347978Srrh char *outp,*inp;
357978Srrh char *newp;
367978Srrh char cinit;
377978Srrh 
387978Srrh /* some code depends on whether characters are sign or zero extended */
397978Srrh /*	#if '\377' < 0		not used here, old cpp doesn't understand */
4029876Ssam #if pdp11 | vax | mc68000 | tahoe
417978Srrh #define COFF 128
427978Srrh #else
437978Srrh #define COFF 0
447978Srrh #endif
457978Srrh 
467978Srrh # if gcos
477978Srrh #define ALFSIZ 512	/* alphabet size */
487978Srrh # else
497978Srrh #define ALFSIZ 256	/* alphabet size */
507978Srrh # endif
517978Srrh char macbit[ALFSIZ+11];
527978Srrh char toktyp[ALFSIZ];
537978Srrh #define BLANK 1
547978Srrh #define IDENT 2
557978Srrh #define NUMBR 3
567978Srrh 
577978Srrh /* a superimposed code is used to reduce the number of calls to the
587978Srrh /* symbol table lookup routine.  (if the kth character of an identifier
597978Srrh /* is 'a' and there are no macro names whose kth character is 'a'
607978Srrh /* then the identifier cannot be a macro name, hence there is no need
617978Srrh /* to look in the symbol table.)  'scw1' enables the test based on
627978Srrh /* single characters and their position in the identifier.  'scw2'
637978Srrh /* enables the test based on adjacent pairs of characters and their
647978Srrh /* position in the identifier.  scw1 typically costs 1 indexed fetch,
657978Srrh /* an AND, and a jump per character of identifier, until the identifier
667978Srrh /* is known as a non-macro name or until the end of the identifier.
677978Srrh /* scw1 is inexpensive.  scw2 typically costs 4 indexed fetches,
687978Srrh /* an add, an AND, and a jump per character of identifier, but it is also
697978Srrh /* slightly more effective at reducing symbol table searches.
707978Srrh /* scw2 usually costs too much because the symbol table search is
717978Srrh /* usually short; but if symbol table search should become expensive,
727978Srrh /* the code is here.
737978Srrh /* using both scw1 and scw2 is of dubious value.
747978Srrh */
757978Srrh #define scw1 1
767978Srrh #define scw2 0
777978Srrh 
787978Srrh #if scw2
797978Srrh char t21[ALFSIZ],t22[ALFSIZ],t23[ALFSIZ+NCPS];
807978Srrh #endif
817978Srrh 
827978Srrh #if scw1
837978Srrh #define b0 1
847978Srrh #define b1 2
857978Srrh #define b2 4
867978Srrh #define b3 8
877978Srrh #define b4 16
887978Srrh #define b5 32
897978Srrh #define b6 64
907978Srrh #define b7 128
917978Srrh #endif
927978Srrh 
937978Srrh #define IB 1
947978Srrh #define SB 2
957978Srrh #define NB 4
967978Srrh #define CB 8
977978Srrh #define QB 16
987978Srrh #define WB 32
997978Srrh char fastab[ALFSIZ];
1007978Srrh char slotab[ALFSIZ];
1017978Srrh char *ptrtab;
1027978Srrh #define isslo (ptrtab==(slotab+COFF))
1037978Srrh #define isid(a)  ((fastab+COFF)[a]&IB)
1047978Srrh #define isspc(a) (ptrtab[a]&SB)
1057978Srrh #define isnum(a) ((fastab+COFF)[a]&NB)
1067978Srrh #define iscom(a) ((fastab+COFF)[a]&CB)
1077978Srrh #define isquo(a) ((fastab+COFF)[a]&QB)
1087978Srrh #define iswarn(a) ((fastab+COFF)[a]&WB)
1097978Srrh 
1107978Srrh #define eob(a) ((a)>=pend)
1117978Srrh #define bob(a) (pbeg>=(a))
1127978Srrh 
11313556Ssam # define cputc(a,b)	if(!flslvl) putc(a,b)
11413556Ssam 
1157978Srrh char buffer[NCPS+BUFSIZ+BUFSIZ+NCPS];
1167978Srrh 
11724202Smckusick char	*lastcopy;
1187978Srrh 
11924202Smckusick char *malloc(), *realloc();
12024202Smckusick 
1217978Srrh # define DROP 0xFE	/* special character not legal ASCII or EBCDIC */
1227978Srrh # define WARN DROP
1237978Srrh # define SAME 0
12444917Ssklower # define MAXINC 15
1257978Srrh # define MAXFRE 14	/* max buffers of macro pushback */
1267978Srrh # define MAXFRM 31	/* max number of formals/actuals to a macro */
1277978Srrh 
1287978Srrh static char warnc = WARN;
1297978Srrh 
1307978Srrh int mactop,fretop;
1317978Srrh char *instack[MAXFRE],*bufstack[MAXFRE],*endbuf[MAXFRE];
1327978Srrh 
1337978Srrh int plvl;	/* parenthesis level during scan for macro actuals */
1347978Srrh int maclin;	/* line number of macro call requiring actuals */
1357978Srrh char *macfil;	/* file name of macro call requiring actuals */
1367978Srrh char *macnam;	/* name of macro requiring actuals */
1377978Srrh int maclvl;	/* # calls since last decrease in nesting level */
1387978Srrh char *macforw;	/* pointer which must be exceeded to decrease nesting level */
1397978Srrh int macdam;	/* offset to macforw due to buffer shifting */
1407978Srrh 
1417978Srrh #if tgp
1427978Srrh int tgpscan;	/* flag for dump(); */
1437978Srrh #endif
1447978Srrh 
1457978Srrh STATIC	int	inctop[MAXINC];
1467978Srrh STATIC	char	*fnames[MAXINC];
1477978Srrh STATIC	char	*dirnams[MAXINC];	/* actual directory of #include files */
1487978Srrh STATIC	int	fins[MAXINC];
1497978Srrh STATIC	int	lineno[MAXINC];
1507978Srrh 
1517978Srrh STATIC	char	*dirs[10];	/* -I and <> directories */
1527978Srrh char *strdex(), *copy(), *subst(), *trmdir();
1537978Srrh struct symtab *stsym();
15425241Smckusick STATIC	int	fin	= FIRSTOPEN;
1557978Srrh STATIC	FILE	*fout	= stdout;
1567978Srrh STATIC	int	nd	= 1;
1577978Srrh STATIC	int	pflag;	/* don't put out lines "# 12 foo.c" */
15813556Ssam int	passcom;	/* don't delete comments */
15925422Smckusick int	incomment;	/* True if parsing a comment */
1607978Srrh STATIC	int rflag;	/* allow macro recursion */
16116235Smckusick STATIC	int mflag;	/* generate makefile dependencies */
16216235Smckusick STATIC	char *infile;	/* name of .o file to build dependencies from */
16316235Smckusick STATIC 	FILE *mout;	/* file to place dependencies on */
16416235Smckusick #define START 1
16516235Smckusick #define CONT  2
16616235Smckusick #define BACK  3
1677978Srrh STATIC	int	ifno;
168*45525Sbostic # define NPREDEF 50
1697978Srrh STATIC	char *prespc[NPREDEF];
1707978Srrh STATIC	char **predef = prespc;
1717978Srrh STATIC	char *punspc[NPREDEF];
1727978Srrh STATIC	char **prund = punspc;
1737978Srrh STATIC	int	exfail;
1747978Srrh struct symtab {
1757978Srrh 	char	*name;
1767978Srrh 	char	*value;
1777978Srrh } *lastsym, *lookup(), *slookup();
1787978Srrh 
1797978Srrh # if gcos
1807978Srrh #include <setjmp.h>
1817978Srrh static jmp_buf env;
1827978Srrh # define main	mainpp
1837978Srrh # undef exit
1847978Srrh # define exit(S)	longjmp(env, 1)
1857978Srrh # define open(S,D)	fileno(fopen(S, "r"))
1867978Srrh # define close(F)	fclose(_f[F])
1877978Srrh extern FILE *_f[];
1887978Srrh # define symsiz 500
1897978Srrh # else
19044945Ssklower # define symsiz 5000		/* std = 500, wnj aug 1979 */
1917978Srrh # endif
1927978Srrh STATIC	struct symtab stab[symsiz];
1937978Srrh 
1947978Srrh STATIC	struct symtab *defloc;
1957978Srrh STATIC	struct symtab *udfloc;
1967978Srrh STATIC	struct symtab *incloc;
1977978Srrh STATIC	struct symtab *ifloc;
1987978Srrh STATIC	struct symtab *elsloc;
1997978Srrh STATIC	struct symtab *eifloc;
2007978Srrh STATIC	struct symtab *ifdloc;
2017978Srrh STATIC	struct symtab *ifnloc;
2027978Srrh STATIC	struct symtab *ysysloc;
2037978Srrh STATIC	struct symtab *varloc;
2047978Srrh STATIC	struct symtab *lneloc;
2057978Srrh STATIC	struct symtab *ulnloc;
2067978Srrh STATIC	struct symtab *uflloc;
20724202Smckusick STATIC	struct symtab *identloc;	/* Sys 5r3 compatibility */
2087978Srrh STATIC	int	trulvl;
2097978Srrh STATIC	int	flslvl;
2107978Srrh 
sayline(where)21116235Smckusick sayline(where)
21216235Smckusick 	int where;
21316235Smckusick {
21416235Smckusick 	if (mflag && where==START) fprintf(mout, "%s: %s\n", infile, fnames[ifno]);
2157978Srrh 	if (pflag==0) fprintf(fout,"# %d \"%s\"\n", lineno[ifno], fnames[ifno]);
2167978Srrh }
2177978Srrh 
2187978Srrh /* data structure guide
2197978Srrh /*
2207978Srrh /* most of the scanning takes place in the buffer:
2217978Srrh /*
2227978Srrh /*  (low address)                                             (high address)
2237978Srrh /*  pbeg                           pbuf                                 pend
2247978Srrh /*  |      <-- BUFSIZ chars -->      |         <-- BUFSIZ chars -->        |
2257978Srrh /*  _______________________________________________________________________
2267978Srrh /* |_______________________________________________________________________|
2277978Srrh /*          |               |               |
2287978Srrh /*          |<-- waiting -->|               |<-- waiting -->
2297978Srrh /*          |    to be      |<-- current -->|    to be
2307978Srrh /*          |    written    |    token      |    scanned
2317978Srrh /*          |               |               |
2327978Srrh /*          outp            inp             p
2337978Srrh /*
2347978Srrh /*  *outp   first char not yet written to output file
2357978Srrh /*  *inp    first char of current token
2367978Srrh /*  *p      first char not yet scanned
2377978Srrh /*
2387978Srrh /* macro expansion: write from *outp to *inp (chars waiting to be written),
2397978Srrh /* ignore from *inp to *p (chars of the macro call), place generated
2407978Srrh /* characters in front of *p (in reverse order), update pointers,
2417978Srrh /* resume scanning.
2427978Srrh /*
2437978Srrh /* symbol table pointers point to just beyond the end of macro definitions;
2447978Srrh /* the first preceding character is the number of formal parameters.
2457978Srrh /* the appearance of a formal in the body of a definition is marked by
2467978Srrh /* 2 chars: the char WARN, and a char containing the parameter number.
2477978Srrh /* the first char of a definition is preceded by a zero character.
2487978Srrh /*
2497978Srrh /* when macro expansion attempts to back up over the beginning of the
2507978Srrh /* buffer, some characters preceding *pend are saved in a side buffer,
2517978Srrh /* the address of the side buffer is put on 'instack', and the rest
2527978Srrh /* of the main buffer is moved to the right.  the end of the saved buffer
2537978Srrh /* is kept in 'endbuf' since there may be nulls in the saved buffer.
2547978Srrh /*
2557978Srrh /* similar action is taken when an 'include' statement is processed,
2567978Srrh /* except that the main buffer must be completely emptied.  the array
2577978Srrh /* element 'inctop[ifno]' records the last side buffer saved when
2587978Srrh /* file 'ifno' was included.  these buffers remain dormant while
2597978Srrh /* the file is being read, and are reactivated at end-of-file.
2607978Srrh /*
2617978Srrh /* instack[0 : mactop] holds the addresses of all pending side buffers.
2627978Srrh /* instack[inctop[ifno]+1 : mactop-1] holds the addresses of the side
2637978Srrh /* buffers which are "live"; the side buffers instack[0 : inctop[ifno]]
2647978Srrh /* are dormant, waiting for end-of-file on the current file.
2657978Srrh /*
26624202Smckusick /* space for side buffers is obtained from 'malloc' and is never returned.
2677978Srrh /* bufstack[0:fretop-1] holds addresses of side buffers which
2687978Srrh /* are available for use.
2697978Srrh */
2707978Srrh 
dump()2717978Srrh dump() {
2727978Srrh /* write part of buffer which lies between  outp  and  inp .
2737978Srrh /* this should be a direct call to 'write', but the system slows to a crawl
2747978Srrh /* if it has to do an unaligned copy.  thus we buffer.  this silly loop
2757978Srrh /* is 15% of the total time, thus even the 'putc' macro is too slow.
2767978Srrh */
2777978Srrh 	register char *p1,*p2; register FILE *f;
2787978Srrh 	if ((p1=outp)==inp || flslvl!=0) return;
2797978Srrh #if tgp
2807978Srrh #define MAXOUT 80
2817978Srrh 	if (!tgpscan) {/* scan again to insure <= MAXOUT chars between linefeeds */
2827978Srrh 		register char c,*pblank; char savc,stopc,brk;
2837978Srrh 		tgpscan=1; brk=stopc=pblank=0; p2=inp; savc= *p2; *p2='\0';
2847978Srrh 		while (c= *p1++) {
2857978Srrh 			if (c=='\\') c= *p1++;
2867978Srrh 			if (stopc==c) stopc=0;
2877978Srrh 			else if (c=='"' || c=='\'') stopc=c;
2887978Srrh 			if (p1-outp>MAXOUT && pblank!=0) {
2897978Srrh 				*pblank++='\n'; inp=pblank; dump(); brk=1; pblank=0;
2907978Srrh 			}
2917978Srrh 			if (c==' ' && stopc==0) pblank=p1-1;
2927978Srrh 		}
29316235Smckusick 		if (brk) sayline(CONT);
2947978Srrh 		*p2=savc; inp=p2; p1=outp; tgpscan=0;
2957978Srrh 	}
2967978Srrh #endif
2977978Srrh 	f=fout;
2987978Srrh # if gcos
2997978Srrh /* filter out "$ program c" card if first line of input */
3007978Srrh /* gmatch is a simple pattern matcher in the GCOS Standard Library */
3017978Srrh {	static int gmfirst = 0;
3027978Srrh 	if (!gmfirst) {
3037978Srrh 		++gmfirst;
3047978Srrh 		if (gmatch(p1, "^$*program[ \t]*c*"))
3057978Srrh 			p1 = strdex(p1, '\n');
3067978Srrh 	}
3077978Srrh }
3087978Srrh # endif
3097978Srrh 	while (p1<inp) putc(*p1++,f);
3107978Srrh 	outp=p1;
3117978Srrh }
3127978Srrh 
3137978Srrh char *
refill(p)3147978Srrh refill(p) register char *p; {
3157978Srrh /* dump buffer.  save chars from inp to p.  read into buffer at pbuf,
3167978Srrh /* contiguous with p.  update pointers, return new p.
3177978Srrh */
3187978Srrh 	register char *np,*op; register int ninbuf;
3197978Srrh 	dump(); np=pbuf-(p-inp); op=inp;
3207978Srrh 	if (bob(np+1)) {pperror("token too long"); np=pbeg; p=inp+BUFSIZ;}
3217978Srrh 	macdam += np-inp; outp=inp=np;
3227978Srrh 	while (op<p) *np++= *op++;
3237978Srrh 	p=np;
3247978Srrh 	for (;;) {
3257978Srrh 		if (mactop>inctop[ifno]) {/* retrieve hunk of pushed-back macro text */
3267978Srrh 			op=instack[--mactop]; np=pbuf;
3277978Srrh 			do {while (*np++= *op++);} while (op<endbuf[mactop]); pend=np-1;
3287978Srrh 			/* make buffer space avail for 'include' processing */
3297978Srrh 			if (fretop<MAXFRE) bufstack[fretop++]=instack[mactop];
3307978Srrh 			return(p);
3317978Srrh 		} else {/* get more text from file(s) */
3327978Srrh 			maclvl=0;
3337978Srrh 			if (0<(ninbuf=read(fin,pbuf,BUFSIZ))) {
3347978Srrh 				pend=pbuf+ninbuf; *pend='\0';
3357978Srrh 				return(p);
3367978Srrh 			}
3377978Srrh 			/* end of #include file */
3387978Srrh 			if (ifno==0) {/* end of input */
3397978Srrh 				if (plvl!=0) {
3407978Srrh 					int n=plvl,tlin=lineno[ifno]; char *tfil=fnames[ifno];
3417978Srrh 					lineno[ifno]=maclin; fnames[ifno]=macfil;
3427978Srrh 					pperror("%s: unterminated macro call",macnam);
3437978Srrh 					lineno[ifno]=tlin; fnames[ifno]=tfil;
3447978Srrh 					np=p; *np++='\n';	/* shut off unterminated quoted string */
3457978Srrh 					while (--n>=0) *np++=')';	/* supply missing parens */
3467978Srrh 					pend=np; *np='\0'; if (plvl<0) plvl=0;
3477978Srrh 					return(p);
3487978Srrh 				}
34912979Ssam 				if (trulvl || flslvl)
35025422Smckusick 					if (incomment)
35125422Smckusick 						pperror("unterminated comment");
35225422Smckusick 					else
35325422Smckusick 						pperror("missing endif");
3547978Srrh 				inp=p; dump(); exit(exfail);
3557978Srrh 			}
35616235Smckusick 			close(fin); fin=fins[--ifno]; dirs[0]=dirnams[ifno]; sayline(BACK);
3577978Srrh 		}
3587978Srrh 	}
3597978Srrh }
3607978Srrh 
3617978Srrh #define BEG 0
3627978Srrh #define LF 1
3637978Srrh 
3647978Srrh char *
cotoken(p)3657978Srrh cotoken(p) register char *p; {
3667978Srrh 	register int c,i; char quoc;
3677978Srrh 	static int state = BEG;
3687978Srrh 
3697978Srrh 	if (state!=BEG) goto prevlf;
3707978Srrh for (;;) {
3717978Srrh again:
3727978Srrh 	while (!isspc(*p++));
3737978Srrh 	switch (*(inp=p-1)) {
3747978Srrh 	case 0: {
3757978Srrh 		if (eob(--p)) {p=refill(p); goto again;}
3767978Srrh 		else ++p; /* ignore null byte */
3777978Srrh 	} break;
3787978Srrh 	case '|': case '&': for (;;) {/* sloscan only */
3797978Srrh 		if (*p++== *inp) break;
3807978Srrh 		if (eob(--p)) p=refill(p);
3817978Srrh 		else break;
3827978Srrh 	} break;
3837978Srrh 	case '=': case '!': for (;;) {/* sloscan only */
3847978Srrh 		if (*p++=='=') break;
3857978Srrh 		if (eob(--p)) p=refill(p);
3867978Srrh 		else break;
3877978Srrh 	} break;
3887978Srrh 	case '<': case '>': for (;;) {/* sloscan only */
3897978Srrh 		if (*p++=='=' || p[-2]==p[-1]) break;
3907978Srrh 		if (eob(--p)) p=refill(p);
3917978Srrh 		else break;
3927978Srrh 	} break;
3937978Srrh 	case '\\': for (;;) {
3947978Srrh 		if (*p++=='\n') {++lineno[ifno]; break;}
3957978Srrh 		if (eob(--p)) p=refill(p);
3967978Srrh 		else {++p; break;}
3977978Srrh 	} break;
3987978Srrh 	case '/': for (;;) {
3997978Srrh 		if (*p++=='*') {/* comment */
40025422Smckusick 			incomment++;
4017978Srrh 			if (!passcom) {inp=p-2; dump(); ++flslvl;}
4027978Srrh 			for (;;) {
4037978Srrh 				while (!iscom(*p++));
4047978Srrh 				if (p[-1]=='*') for (;;) {
4057978Srrh 					if (*p++=='/') goto endcom;
4067978Srrh 					if (eob(--p)) {
4077978Srrh 						if (!passcom) {inp=p; p=refill(p);}
4087978Srrh 						else if ((p-inp)>=BUFSIZ) {/* split long comment */
4097978Srrh 							inp=p; p=refill(p);	/* last char written is '*' */
41013556Ssam 							cputc('/',fout);	/* terminate first part */
4117978Srrh 							/* and fake start of 2nd */
4127978Srrh 							outp=inp=p-=3; *p++='/'; *p++='*'; *p++='*';
4137978Srrh 						} else p=refill(p);
4147978Srrh 					} else break;
4157978Srrh 				} else if (p[-1]=='\n') {
4167978Srrh 					++lineno[ifno]; if (!passcom) putc('\n',fout);
4177978Srrh 				} else if (eob(--p)) {
4187978Srrh 					if (!passcom) {inp=p; p=refill(p);}
4197978Srrh 					else if ((p-inp)>=BUFSIZ) {/* split long comment */
4207978Srrh 						inp=p; p=refill(p);
42113556Ssam 						cputc('*',fout); cputc('/',fout);
4227978Srrh 						outp=inp=p-=2; *p++='/'; *p++='*';
4237978Srrh 					} else p=refill(p);
4247978Srrh 				} else ++p; /* ignore null byte */
4257978Srrh 			}
4267978Srrh 		endcom:
42725422Smckusick 			incomment--;
4287978Srrh 			if (!passcom) {outp=inp=p; --flslvl; goto again;}
4297978Srrh 			break;
4307978Srrh 		}
4317978Srrh 		if (eob(--p)) p=refill(p);
4327978Srrh 		else break;
4337978Srrh 	} break;
4347978Srrh # if gcos
4357978Srrh 	case '`':
4367978Srrh # endif
4377978Srrh 	case '"': case '\'': {
4387978Srrh 		quoc=p[-1];
4397978Srrh 		for (;;) {
4407978Srrh 			while (!isquo(*p++));
4417978Srrh 			if (p[-1]==quoc) break;
4427978Srrh 			if (p[-1]=='\n') {--p; break;} /* bare \n terminates quotation */
4437978Srrh 			if (p[-1]=='\\') for (;;) {
4447978Srrh 				if (*p++=='\n') {++lineno[ifno]; break;} /* escaped \n ignored */
4457978Srrh 				if (eob(--p)) p=refill(p);
4467978Srrh 				else {++p; break;}
4477978Srrh 			} else if (eob(--p)) p=refill(p);
4487978Srrh 			else ++p;	/* it was a different quote character */
4497978Srrh 		}
4507978Srrh 	} break;
4517978Srrh 	case '\n': {
4527978Srrh 		++lineno[ifno]; if (isslo) {state=LF; return(p);}
4537978Srrh prevlf:
4547978Srrh 		state=BEG;
4557978Srrh 		for (;;) {
4567978Srrh 			if (*p++=='#') return(p);
4577978Srrh 			if (eob(inp= --p)) p=refill(p);
4587978Srrh 			else goto again;
4597978Srrh 		}
4607978Srrh 	} break;
4617978Srrh 	case '0': case '1': case '2': case '3': case '4':
4627978Srrh 	case '5': case '6': case '7': case '8': case '9':
4637978Srrh 	for (;;) {
4647978Srrh 		while (isnum(*p++));
4657978Srrh 		if (eob(--p)) p=refill(p);
4667978Srrh 		else break;
4677978Srrh 	} break;
4687978Srrh 	case 'A': case 'B': case 'C': case 'D': case 'E':
4697978Srrh 	case 'F': case 'G': case 'H': case 'I': case 'J':
4707978Srrh 	case 'K': case 'L': case 'M': case 'N': case 'O':
4717978Srrh 	case 'P': case 'Q': case 'R': case 'S': case 'T':
4727978Srrh 	case 'U': case 'V': case 'W': case 'X': case 'Y':
4737978Srrh 	case 'Z': case '_':
4747978Srrh 	case 'a': case 'b': case 'c': case 'd': case 'e':
4757978Srrh 	case 'f': case 'g': case 'h': case 'i': case 'j':
4767978Srrh 	case 'k': case 'l': case 'm': case 'n': case 'o':
4777978Srrh 	case 'p': case 'q': case 'r': case 's': case 't':
4787978Srrh 	case 'u': case 'v': case 'w': case 'x': case 'y':
4797978Srrh 	case 'z':
4807978Srrh #if scw1
4817978Srrh #define tmac1(c,bit) if (!xmac1(c,bit,&)) goto nomac
4827978Srrh #define xmac1(c,bit,op) ((macbit+COFF)[c] op (bit))
4837978Srrh #else
4847978Srrh #define tmac1(c,bit)
4857978Srrh #define xmac1(c,bit,op)
4867978Srrh #endif
4877978Srrh 
4887978Srrh #if scw2
4897978Srrh #define tmac2(c0,c1,cpos) if (!xmac2(c0,c1,cpos,&)) goto nomac
4907978Srrh #define xmac2(c0,c1,cpos,op)\
4917978Srrh 	((macbit+COFF)[(t21+COFF)[c0]+(t22+COFF)[c1]] op (t23+COFF+cpos)[c0])
4927978Srrh #else
4937978Srrh #define tmac2(c0,c1,cpos)
4947978Srrh #define xmac2(c0,c1,cpos,op)
4957978Srrh #endif
4967978Srrh 
4977978Srrh 	if (flslvl) goto nomac;
4987978Srrh 	for (;;) {
4997978Srrh 		c= p[-1];                          tmac1(c,b0);
5007978Srrh 		i= *p++; if (!isid(i)) goto endid; tmac1(i,b1); tmac2(c,i,0);
5017978Srrh 		c= *p++; if (!isid(c)) goto endid; tmac1(c,b2); tmac2(i,c,1);
5027978Srrh 		i= *p++; if (!isid(i)) goto endid; tmac1(i,b3); tmac2(c,i,2);
5037978Srrh 		c= *p++; if (!isid(c)) goto endid; tmac1(c,b4); tmac2(i,c,3);
5047978Srrh 		i= *p++; if (!isid(i)) goto endid; tmac1(i,b5); tmac2(c,i,4);
5057978Srrh 		c= *p++; if (!isid(c)) goto endid; tmac1(c,b6); tmac2(i,c,5);
5067978Srrh 		i= *p++; if (!isid(i)) goto endid; tmac1(i,b7); tmac2(c,i,6);
5077978Srrh 		                                                tmac2(i,0,7);
5087978Srrh 		while (isid(*p++));
5097978Srrh 		if (eob(--p)) {refill(p); p=inp+1; continue;}
5107978Srrh 		goto lokid;
5117978Srrh 	endid:
5127978Srrh 		if (eob(--p)) {refill(p); p=inp+1; continue;}
5137978Srrh 		tmac2(p[-1],0,-1+(p-inp));
5147978Srrh 	lokid:
5157978Srrh 		slookup(inp,p,0); if (newp) {p=newp; goto again;}
5167978Srrh 		else break;
5177978Srrh 	nomac:
5187978Srrh 		while (isid(*p++));
5197978Srrh 		if (eob(--p)) {p=refill(p); goto nomac;}
5207978Srrh 		else break;
5217978Srrh 	} break;
5227978Srrh 	} /* end of switch */
5237978Srrh 
5247978Srrh 	if (isslo) return(p);
5257978Srrh } /* end of infinite loop */
5267978Srrh }
5277978Srrh 
5287978Srrh char *
skipbl(p)5297978Srrh skipbl(p) register char *p; {/* get next non-blank token */
5307978Srrh 	do {outp=inp=p; p=cotoken(p);} while ((toktyp+COFF)[*inp]==BLANK);
5317978Srrh 	return(p);
5327978Srrh }
5337978Srrh 
5347978Srrh char *
unfill(p)5357978Srrh unfill(p) register char *p; {
5367978Srrh /* take <= BUFSIZ chars from right end of buffer and put them on instack .
5377978Srrh /* slide rest of buffer to the right, update pointers, return new p.
5387978Srrh */
5397978Srrh 	register char *np,*op; register int d;
5407978Srrh 	if (mactop>=MAXFRE) {
5417978Srrh 		pperror("%s: too much pushback",macnam);
5427978Srrh 		p=inp=pend; dump();	/* begin flushing pushback */
5437978Srrh 		while (mactop>inctop[ifno]) {p=refill(p); p=inp=pend; dump();}
5447978Srrh 	}
5457978Srrh 	if (fretop>0) np=bufstack[--fretop];
5467978Srrh 	else {
54724202Smckusick 		np=malloc(BUFSIZ+1);
54824202Smckusick 		if (np==NULL) {pperror("no space"); exit(exfail);}
54924202Smckusick 		np[BUFSIZ]='\0';
5507978Srrh 	}
5517978Srrh 	instack[mactop]=np; op=pend-BUFSIZ; if (op<p) op=p;
5527978Srrh 	for (;;) {while (*np++= *op++); if (eob(op)) break;} /* out with old */
5537978Srrh 	endbuf[mactop++]=np;	/* mark end of saved text */
5547978Srrh 	np=pbuf+BUFSIZ; op=pend-BUFSIZ; pend=np; if (op<p) op=p;
5557978Srrh 	while (outp<op) *--np= *--op; /* slide over new */
5567978Srrh 	if (bob(np)) pperror("token too long");
5577978Srrh 	d=np-outp; outp+=d; inp+=d; macdam+=d; return(p+d);
5587978Srrh }
5597978Srrh 
5607978Srrh char *
doincl(p)5617978Srrh doincl(p) register char *p; {
5627978Srrh 	int filok,inctype;
5637978Srrh 	register char *cp; char **dirp,*nfil; char filname[BUFSIZ];
5647978Srrh 
5657978Srrh 	p=skipbl(p); cp=filname;
5667978Srrh 	if (*inp++=='<') {/* special <> syntax */
5677978Srrh 		inctype=1;
5687978Srrh 		++flslvl;	/* prevent macro expansion */
5697978Srrh 		for (;;) {
5707978Srrh 			outp=inp=p; p=cotoken(p);
5717978Srrh 			if (*inp=='\n') {--p; *cp='\0'; break;}
5727978Srrh 			if (*inp=='>') {      *cp='\0'; break;}
5737978Srrh # ifdef gimpel
5747978Srrh 			if (*inp=='.' && !intss()) *inp='#';
5757978Srrh # endif
5767978Srrh 			while (inp<p) *cp++= *inp++;
5777978Srrh 		}
5787978Srrh 		--flslvl;	/* reenable macro expansion */
5797978Srrh 	} else if (inp[-1]=='"') {/* regular "" syntax */
5807978Srrh 		inctype=0;
5817978Srrh # ifdef gimpel
5827978Srrh 		while (inp<p) {if (*inp=='.' && !intss()) *inp='#'; *cp++= *inp++;}
5837978Srrh # else
5847978Srrh 		while (inp<p) *cp++= *inp++;
5857978Srrh # endif
5867978Srrh 		if (*--cp=='"') *cp='\0';
5877978Srrh 	} else {pperror("bad include syntax",0); inctype=2;}
5887978Srrh 	/* flush current file to \n , then write \n */
5897978Srrh 	++flslvl; do {outp=inp=p; p=cotoken(p);} while (*inp!='\n'); --flslvl;
5907978Srrh 	inp=p; dump(); if (inctype==2) return(p);
5917978Srrh 	/* look for included file */
5927978Srrh 	if (ifno+1 >=MAXINC) {
5937978Srrh 		pperror("Unreasonable include nesting",0); return(p);
5947978Srrh 	}
59524202Smckusick 	if((nfil=malloc(BUFSIZ))==NULL) {pperror("no space"); exit(exfail);}
5967978Srrh 	filok=0;
5977978Srrh 	for (dirp=dirs+inctype; *dirp; ++dirp) {
5987978Srrh 		if (
5997978Srrh # if gcos
6007978Srrh 			strdex(filname, '/')
6017978Srrh # else
6027978Srrh 			filname[0]=='/'
6037978Srrh # endif
6047978Srrh 				|| **dirp=='\0') strcpy(nfil,filname);
6057978Srrh 		else {
6067978Srrh 			strcpy(nfil,*dirp);
6077978Srrh # if unix || gcos
6087978Srrh 			strcat(nfil,"/");
6097978Srrh # endif
6107978Srrh #ifdef ibm
6117978Srrh #ifndef gimpel
6127978Srrh 			strcat(nfil,".");
6137978Srrh #endif
6147978Srrh #endif
6157978Srrh 			strcat(nfil,filname);
6167978Srrh 		}
6177978Srrh 		if (0<(fins[ifno+1]=open(nfil,READ))) {
6187978Srrh 			filok=1; fin=fins[++ifno]; break;
6197978Srrh 		}
6207978Srrh 	}
62124202Smckusick 	if(filok==0){pperror("Can't find include file %s",filname);free(nfil);}
6227978Srrh 	else {
62324202Smckusick 		nfil=realloc(nfil,strlen(nfil)+1);
62424202Smckusick 		lineno[ifno]=1; fnames[ifno]=nfil;
6257978Srrh 		dirnams[ifno]=dirs[0]=trmdir(copy(nfil));
62616235Smckusick 		sayline(START);
6277978Srrh 		/* save current contents of buffer */
6287978Srrh 		while (!eob(p)) p=unfill(p);
6297978Srrh 		inctop[ifno]=mactop;
6307978Srrh 	}
6317978Srrh 	return(p);
6327978Srrh }
6337978Srrh 
equfrm(a,p1,p2)6347978Srrh equfrm(a,p1,p2) register char *a,*p1,*p2; {
6357978Srrh 	register char c; int flag;
6367978Srrh 	c= *p2; *p2='\0';
6377978Srrh 	flag=strcmp(a,p1); *p2=c; return(flag==SAME);
6387978Srrh }
6397978Srrh 
6407978Srrh char *
dodef(p)6417978Srrh dodef(p) char *p; {/* process '#define' */
6427978Srrh 	register char *pin,*psav,*cf;
6437978Srrh 	char **pf,**qf; int b,c,params; struct symtab *np;
64424202Smckusick 	char *oldval;
64524202Smckusick 	char *space, *newspace;
6467978Srrh 	char *formal[MAXFRM]; /* formal[n] is name of nth formal */
6477978Srrh 	char formtxt[BUFSIZ]; /* space for formal names */
64827460Sdonn 	int opt_passcom=passcom;
6497978Srrh 
65027460Sdonn 	passcom=0;	/* don't put comments in macro expansions */
65127460Sdonn 
6527978Srrh 	++flslvl; /* prevent macro expansion during 'define' */
6537978Srrh 	p=skipbl(p); pin=inp;
6547978Srrh 	if ((toktyp+COFF)[*pin]!=IDENT) {
65527460Sdonn 		ppwarn("illegal macro name"); while (*inp!='\n') p=skipbl(p);
65627460Sdonn 		passcom=opt_passcom; return(p);
6577978Srrh 	}
6587978Srrh 	np=slookup(pin,p,1);
65924202Smckusick 	if (oldval=np->value) free(lastcopy);	/* was previously defined */
6607978Srrh 	b=1; cf=pin;
6617978Srrh 	while (cf<p) {/* update macbit */
6627978Srrh 		c= *cf++; xmac1(c,b,|=); b=(b+b)&0xFF;
6637978Srrh 		if (cf!=p) xmac2(c,*cf,-1+(cf-pin),|=);
6647978Srrh 		else xmac2(c,0,-1+(cf-pin),|=);
6657978Srrh 	}
6667978Srrh 	params=0; outp=inp=p; p=cotoken(p); pin=inp;
6677978Srrh 	if (*pin=='(') {/* with parameters; identify the formals */
6687978Srrh 		cf=formtxt; pf=formal;
6697978Srrh 		for (;;) {
6707978Srrh 			p=skipbl(p); pin=inp;
6717978Srrh 			if (*pin=='\n') {
6727978Srrh 				--lineno[ifno]; --p; pperror("%s: missing )",np->name); break;
6737978Srrh 			}
6747978Srrh 			if (*pin==')') break;
6757978Srrh 			if (*pin==',') continue;
6767978Srrh 			if ((toktyp+COFF)[*pin]!=IDENT) {
6777978Srrh 				c= *p; *p='\0'; pperror("bad formal: %s",pin); *p=c;
6787978Srrh 			} else if (pf>= &formal[MAXFRM]) {
6797978Srrh 				c= *p; *p='\0'; pperror("too many formals: %s",pin); *p=c;
6807978Srrh 			} else {
6817978Srrh 				*pf++=cf; while (pin<p) *cf++= *pin++; *cf++='\0'; ++params;
6827978Srrh 			}
6837978Srrh 		}
6847978Srrh 		if (params==0) --params; /* #define foo() ... */
6857978Srrh 	} else if (*pin=='\n') {--lineno[ifno]; --p;}
6867978Srrh 	/* remember beginning of macro body, so that we can
6877978Srrh 	/* warn if a redefinition is different from old value.
6887978Srrh 	*/
68924202Smckusick 	space=psav=malloc(BUFSIZ);
69027460Sdonn 	if (space==NULL) {
69127460Sdonn 		pperror("too much defining");
69227460Sdonn 		passcom=opt_passcom;
69327460Sdonn 		return(p);
69427460Sdonn 	}
69524202Smckusick 	*psav++ = '\0';
6967978Srrh 	for (;;) {/* accumulate definition until linefeed */
6977978Srrh 		outp=inp=p; p=cotoken(p); pin=inp;
6987978Srrh 		if (*pin=='\\' && pin[1]=='\n') {putc('\n',fout); continue;}	/* ignore escaped lf */
6997978Srrh 		if (*pin=='\n') break;
7007978Srrh 		if (params) {/* mark the appearance of formals in the definiton */
7017978Srrh 			if ((toktyp+COFF)[*pin]==IDENT) {
7027978Srrh 				for (qf=pf; --qf>=formal; ) {
7037978Srrh 					if (equfrm(*qf,pin,p)) {
7047978Srrh 						*psav++=qf-formal+1; *psav++=WARN; pin=p; break;
7057978Srrh 					}
7067978Srrh 				}
7077978Srrh 			} else if (*pin=='"' || *pin=='\''
7087978Srrh # if gcos
7097978Srrh 					|| *pin=='`'
7107978Srrh # endif
7117978Srrh 						) {/* inside quotation marks, too */
7127978Srrh 				char quoc= *pin;
7137978Srrh 				for (*psav++= *pin++; pin<p && *pin!=quoc; ) {
7147978Srrh 					while (pin<p && !isid(*pin)) *psav++= *pin++;
7157978Srrh 					cf=pin; while (cf<p && isid(*cf)) ++cf;
7167978Srrh 					for (qf=pf; --qf>=formal; ) {
7177978Srrh 						if (equfrm(*qf,pin,cf)) {
7187978Srrh 							*psav++=qf-formal+1; *psav++=WARN; pin=cf; break;
7197978Srrh 						}
7207978Srrh 					}
7217978Srrh 					while (pin<cf) *psav++= *pin++;
7227978Srrh 				}
7237978Srrh 			}
7247978Srrh 		}
7257978Srrh 		while (pin<p) *psav++= *pin++;
7267978Srrh 	}
7277978Srrh 	*psav++=params; *psav++='\0';
7287978Srrh 	if ((cf=oldval)!=NULL) {/* redefinition */
7297978Srrh 		--cf;	/* skip no. of params, which may be zero */
7307978Srrh 		while (*--cf);	/* go back to the beginning */
73124202Smckusick 		if (0!=strcmp(++cf,space+1)) {/* redefinition different from old */
7327978Srrh 			--lineno[ifno]; ppwarn("%s redefined",np->name); ++lineno[ifno];
7337978Srrh 			np->value=psav-1;
73424202Smckusick 		} else free(space); /* identical redef.; reclaim space */
7357978Srrh 	} else np->value=psav-1;
73624202Smckusick 	--flslvl; inp=pin;
73724202Smckusick 	if (np->value == psav-1) {
73824202Smckusick 		newspace = realloc(space, psav-space);
73924202Smckusick 		if (newspace==NULL) {pperror("no space"); exit(exfail);}
74024202Smckusick 		/*
74124202Smckusick 		 * Adjust pointer in case this moved.
74224202Smckusick 		 */
74324202Smckusick 		np->value += newspace-space;
74424202Smckusick 	}
74527460Sdonn 	passcom=opt_passcom;
74624202Smckusick 	return(p);
7477978Srrh }
7487978Srrh 
7497978Srrh #define fasscan() ptrtab=fastab+COFF
7507978Srrh #define sloscan() ptrtab=slotab+COFF
7517978Srrh 
7527978Srrh char *
control(p)7537978Srrh control(p) register char *p; {/* find and handle preprocessor control lines */
7547978Srrh 	register struct symtab *np;
7557978Srrh for (;;) {
7567978Srrh 	fasscan(); p=cotoken(p); if (*inp=='\n') ++inp; dump();
7577978Srrh 	sloscan(); p=skipbl(p);
7587978Srrh 	*--inp=SALT; outp=inp; ++flslvl; np=slookup(inp,p,0); --flslvl;
7597978Srrh 	if (np==defloc) {/* define */
7607978Srrh 		if (flslvl==0) {p=dodef(p); continue;}
7617978Srrh 	} else if (np==incloc) {/* include */
7627978Srrh 		if (flslvl==0) {p=doincl(p); continue;}
7637978Srrh 	} else if (np==ifnloc) {/* ifndef */
7647978Srrh 		++flslvl; p=skipbl(p); np=slookup(inp,p,0); --flslvl;
7657978Srrh 		if (flslvl==0 && np->value==0) ++trulvl;
7667978Srrh 		else ++flslvl;
7677978Srrh 	} else if (np==ifdloc) {/* ifdef */
7687978Srrh 		++flslvl; p=skipbl(p); np=slookup(inp,p,0); --flslvl;
7697978Srrh 		if (flslvl==0 && np->value!=0) ++trulvl;
7707978Srrh 		else ++flslvl;
7717978Srrh 	} else if (np==eifloc) {/* endif */
77216235Smckusick 		if (flslvl) {if (--flslvl==0) sayline(CONT);}
7737978Srrh 		else if (trulvl) --trulvl;
7747978Srrh 		else pperror("If-less endif",0);
7757978Srrh 	} else if (np==elsloc) {/* else */
7767978Srrh 		if (flslvl) {
7777978Srrh 			if (--flslvl!=0) ++flslvl;
77816235Smckusick 			else {++trulvl; sayline(CONT);}
7797978Srrh 		}
7807978Srrh 		else if (trulvl) {++flslvl; --trulvl;}
7817978Srrh 		else pperror("If-less else",0);
7827978Srrh 	} else if (np==udfloc) {/* undefine */
7837978Srrh 		if (flslvl==0) {
7847978Srrh 			++flslvl; p=skipbl(p); slookup(inp,p,DROP); --flslvl;
7857978Srrh 		}
7867978Srrh 	} else if (np==ifloc) {/* if */
7877978Srrh #if tgp
7887978Srrh 		pperror(" IF not implemented, true assumed", 0);
7897978Srrh 		if (flslvl==0) ++trulvl; else ++flslvl;
7907978Srrh #else
7917978Srrh 		newp=p;
7927978Srrh 		if (flslvl==0 && yyparse()) ++trulvl; else ++flslvl;
7937978Srrh 		p=newp;
7947978Srrh #endif
7957978Srrh 	} else if (np==lneloc) {/* line */
7967978Srrh 		if (flslvl==0 && pflag==0) {
79725422Smckusick 			char *savestring();
79825422Smckusick 			char filename[BUFSIZ], *cp = filename;
79925422Smckusick 			outp=inp=p; *--outp='#';
80025422Smckusick 			/* Find the line number.. */
80125422Smckusick 			do {
80225422Smckusick 				p = cotoken(p);
80325422Smckusick 			} while (!isnum(*inp) && *inp != '\n');
80425422Smckusick 			if (isnum(*inp))
80525422Smckusick 				lineno[ifno] = atoi(inp)-1;
80625422Smckusick 			/* Skip over the blank token */
80725422Smckusick 			inp = p;
80825422Smckusick 			if (*inp != '\n') {
80925422Smckusick 				p = cotoken(p); inp = p;
8107982Srrh 			}
81125422Smckusick 			/* Add a quote if missing..  */
81225422Smckusick 			if (*inp != '\n') {
81325422Smckusick 				p = cotoken(p);
81425422Smckusick 				/* Add a quote if missing..  */
81525422Smckusick 				if (*inp == '"')
81625422Smckusick 					inp++;
81725422Smckusick 				else {
81825422Smckusick 					dump();
81925422Smckusick 					*--outp = '"';
82025422Smckusick 				}
82125422Smckusick 				while (*inp != '\n') {
82225422Smckusick 					while (inp < p && *inp != '"' &&
82325422Smckusick 						cp < filename+sizeof(filename))
82425422Smckusick 						*cp++ = *inp++;
82525422Smckusick 					if (*inp == '"')
82625422Smckusick 						break;
82725422Smckusick 					inp = p; p = cotoken(p);
82825422Smckusick 				}
82925422Smckusick 				fnames[ifno] = savestring(filename, cp);
83025422Smckusick 				/* Add a quote if missing..  */
83125422Smckusick 				if (*inp != '"') {
83225422Smckusick 					dump();
83325422Smckusick 					*--outp = '"';
83425422Smckusick 				}
83525422Smckusick 			}
83625422Smckusick 			while (*inp != '\n')
83725422Smckusick 				p = cotoken(p);
8387978Srrh 			continue;
8397978Srrh 		}
84024202Smckusick 	} else if (np==identloc) {/* ident (for Sys 5r3 compat) */
84124202Smckusick 		while(*inp!='\n') p=cotoken(p);
8427978Srrh 	} else if (*++inp=='\n') outp=inp;	/* allows blank line after # */
8437978Srrh 	else pperror("undefined control",0);
8447978Srrh 	/* flush to lf */
8457978Srrh 	++flslvl; while (*inp!='\n') {outp=inp=p; p=cotoken(p);} --flslvl;
8467978Srrh }
8477978Srrh }
8487978Srrh 
8497982Srrh char *
savestring(start,finish)8507982Srrh savestring(start, finish)
8517982Srrh 	register char *start, *finish;
8527982Srrh {
8537982Srrh 	char *retbuf;
8547982Srrh 	register char *cp;
8557982Srrh 
8567982Srrh 	retbuf = (char *) calloc(finish - start + 1, sizeof (char));
8577982Srrh 	cp = retbuf;
8587982Srrh 	while (start < finish)
8597982Srrh 		*cp++ = *start++;
8607982Srrh 	*cp = 0;
8617982Srrh 	return(retbuf);
8627982Srrh }
8637982Srrh 
8647978Srrh struct symtab *
stsym(s)8657978Srrh stsym(s) register char *s; {
8667978Srrh 	char buf[BUFSIZ]; register char *p;
8677978Srrh 
8687978Srrh 	/* make definition look exactly like end of #define line */
8697978Srrh 	/* copy to avoid running off end of world when param list is at end */
8707978Srrh 	p=buf; while (*p++= *s++);
8717978Srrh 	p=buf; while (isid(*p++)); /* skip first identifier */
8727978Srrh 	if (*--p=='=') {*p++=' '; while (*p++);}
8737978Srrh 	else {s=" 1"; while (*p++= *s++);}
8747978Srrh 	pend=p; *--p='\n';
8757978Srrh 	sloscan(); dodef(buf); return(lastsym);
8767978Srrh }
8777978Srrh 
8787978Srrh struct symtab *
ppsym(s)8797978Srrh ppsym(s) char *s; {/* kluge */
8807978Srrh 	register struct symtab *sp;
88124202Smckusick 	register char *name;
88224202Smckusick 
88324202Smckusick 	cinit=SALT; sp=stsym(s); name = malloc(strlen(sp->name)+1+1);
88424202Smckusick 	name[0] = '#'; strcpy(name+1, sp->name); sp->name = name;
88524202Smckusick 	cinit=0; return(sp);
8867978Srrh }
8877978Srrh 
8887978Srrh /* VARARGS1 */
pperror(s,x,y)8897978Srrh pperror(s,x,y) char *s; {
8907978Srrh 	if (fnames[ifno][0]) fprintf(stderr,
8917978Srrh # if gcos
8927978Srrh 			"*%c*   \"%s\", line ", exfail >= 0 ? 'F' : 'W',
8937978Srrh # else
8947978Srrh 			"%s: ",
8957978Srrh # endif
8967978Srrh 				 fnames[ifno]);
8977978Srrh 	fprintf(stderr, "%d: ",lineno[ifno]);
8987978Srrh 	fprintf(stderr, s, x, y);
8997978Srrh 	fprintf(stderr,"\n");
9007978Srrh 	++exfail;
9017978Srrh }
9027978Srrh 
yyerror(s,a,b)9037978Srrh yyerror(s,a,b) char *s; {
9047978Srrh 	pperror(s,a,b);
9057978Srrh }
9067978Srrh 
ppwarn(s,x)9077978Srrh ppwarn(s,x) char *s; {
9087978Srrh 	int fail = exfail;
9097978Srrh 	exfail = -1;
9107978Srrh 	pperror(s,x);
9117978Srrh 	exfail = fail;
9127978Srrh }
9137978Srrh 
9147978Srrh struct symtab *
lookup(namep,enterf)9157978Srrh lookup(namep, enterf)
9167978Srrh char *namep;
9177978Srrh {
9187978Srrh 	register char *np, *snp;
9197978Srrh 	register int c, i; int around;
9207978Srrh 	register struct symtab *sp;
9217978Srrh 
9227978Srrh 	/* namep had better not be too long (currently, <=NCPS chars) */
9237978Srrh 	np=namep; around=0; i=cinit;
9247978Srrh 	while (c= *np++) i += i+c; c=i;	/* c=i for register usage on pdp11 */
9257978Srrh 	c %= symsiz; if (c<0) c += symsiz;
9267978Srrh 	sp = &stab[c];
9277978Srrh 	while (snp=sp->name) {
9287978Srrh 		np = namep;
9297978Srrh 		while (*snp++ == *np) if (*np++ == '\0') {
9307978Srrh 				if (enterf==DROP) {sp->name[0]= DROP; sp->value=0;}
9317978Srrh 				return(lastsym=sp);
9327978Srrh 			}
9337978Srrh 		if (--sp < &stab[0])
9347978Srrh 			if (around) {pperror("too many defines", 0); exit(exfail);}
9357978Srrh 			else {++around; sp = &stab[symsiz-1];}
9367978Srrh 	}
9377978Srrh 	if (enterf==1) sp->name=namep;
9387978Srrh 	return(lastsym=sp);
9397978Srrh }
9407978Srrh 
9417978Srrh struct symtab *
slookup(p1,p2,enterf)9427978Srrh slookup(p1,p2,enterf) register char *p1,*p2; int enterf;{
9437978Srrh 	register char *p3; char c2,c3; struct symtab *np;
9447978Srrh 	         c2= *p2; *p2='\0';	/* mark end of token */
9457978Srrh 	if ((p2-p1)>NCPS) p3=p1+NCPS; else p3=p2;
9467978Srrh 			 c3= *p3; *p3='\0';	/* truncate to NCPS chars or less */
9477978Srrh 	if (enterf==1) p1=copy(p1);
9487978Srrh 	np=lookup(p1,enterf); *p3=c3; *p2=c2;
9497978Srrh 	if (np->value!=0 && flslvl==0) newp=subst(p2,np);
9507978Srrh 	else newp=0;
9517978Srrh 	return(np);
9527978Srrh }
9537978Srrh 
9547978Srrh char *
subst(p,sp)9557978Srrh subst(p,sp) register char *p; struct symtab *sp; {
9567978Srrh 	static char match[]="%s: argument mismatch";
9577978Srrh 	register char *ca,*vp; int params;
95813556Ssam 	char *actual[MAXFRM]; /* actual[n] is text of nth actual   */
95913556Ssam 	char actused[MAXFRM]; /* for newline processing in actuals */
96013556Ssam 	char acttxt[BUFSIZ];  /* space for actuals */
96113556Ssam 	int  nlines = 0;
9627978Srrh 
9637978Srrh 	if (0==(vp=sp->value)) return(p);
9647978Srrh 	if ((p-macforw)<=macdam) {
9657978Srrh 		if (++maclvl>symsiz && !rflag) {
9667978Srrh 			pperror("%s: macro recursion",sp->name); return(p);
9677978Srrh 		}
9687978Srrh 	} else maclvl=0;	/* level decreased */
9697978Srrh 	macforw=p; macdam=0;	/* new target for decrease in level */
9707978Srrh 	macnam=sp->name;
9717978Srrh 	dump();
9727978Srrh 	if (sp==ulnloc) {
9737978Srrh 		vp=acttxt; *vp++='\0';
97432495Sbostic 		(void)sprintf(vp,"%d",lineno[ifno]); while (*vp++);
9757978Srrh 	} else if (sp==uflloc) {
9767978Srrh 		vp=acttxt; *vp++='\0';
97732495Sbostic 		(void)sprintf(vp,"\"%s\"",fnames[ifno]); while (*vp++);
9787978Srrh 	}
9797978Srrh 	if (0!=(params= *--vp&0xFF)) {/* definition calls for params */
9807978Srrh 		register char **pa;
9817978Srrh 		ca=acttxt; pa=actual;
9827978Srrh 		if (params==0xFF) params=1;	/* #define foo() ... */
9837978Srrh 		sloscan(); ++flslvl; /* no expansion during search for actuals */
9847978Srrh 		plvl= -1;
9857978Srrh 		do p=skipbl(p); while (*inp=='\n');	/* skip \n too */
9867978Srrh 		if (*inp=='(') {
9877978Srrh 			maclin=lineno[ifno]; macfil=fnames[ifno];
9887978Srrh 			for (plvl=1; plvl!=0; ) {
9897978Srrh 				*ca++='\0';
9907978Srrh 				for (;;) {
9917978Srrh 					outp=inp=p; p=cotoken(p);
9927978Srrh 					if (*inp=='(') ++plvl;
9937978Srrh 					if (*inp==')' && --plvl==0) {--params; break;}
9947978Srrh 					if (plvl==1 && *inp==',') {--params; break;}
9957978Srrh 					while (inp<p) *ca++= *inp++;
9967978Srrh 					if (ca> &acttxt[BUFSIZ])
9977978Srrh 						pperror("%s: actuals too long",sp->name);
9987978Srrh 				}
9997978Srrh 				if (pa>= &actual[MAXFRM]) ppwarn(match,sp->name);
100013556Ssam 				else { actused[pa-actual]=0; *pa++=ca; }
10017978Srrh 			}
100213556Ssam 			nlines = lineno[ifno] - maclin;
100313556Ssam 			lineno[ifno] = maclin; /* don't count newlines here */
10047978Srrh 		}
10057978Srrh 		if (params!=0) ppwarn(match,sp->name);
10067978Srrh 		while (--params>=0) *pa++=""+1;	/* null string for missing actuals */
10077978Srrh 		--flslvl; fasscan();
10087978Srrh 	}
10097978Srrh 	for (;;) {/* push definition onto front of input stack */
10107978Srrh 		while (!iswarn(*--vp)) {
10117978Srrh 			if (bob(p)) {outp=inp=p; p=unfill(p);}
10127978Srrh 			*--p= *vp;
10137978Srrh 		}
10147978Srrh 		if (*vp==warnc) {/* insert actual param */
10157978Srrh 			ca=actual[*--vp-1];
10167978Srrh 			while (*--ca) {
10177978Srrh 				if (bob(p)) {outp=inp=p; p=unfill(p);}
101813556Ssam 				/* Actuals with newlines confuse line numbering */
101913556Ssam 				if (*ca == '\n' && actused[*vp-1])
102013556Ssam 					if (*(ca-1) == '\\') ca--;
102113556Ssam 					else *--p = ' ';
102213556Ssam 				else { *--p= *ca; if (*ca == '\n') nlines--; }
10237978Srrh 			}
102413556Ssam 			actused[*vp-1] = 1;
102513556Ssam 		} else {
102613556Ssam 			if (nlines > 0 )
102713556Ssam 				while (nlines-- > 0)
102813556Ssam 					*--p = '\n';
102913556Ssam 			break;
103013556Ssam 		}
10317978Srrh 	}
10327978Srrh 	outp=inp=p;
10337978Srrh 	return(p);
10347978Srrh }
10357978Srrh 
10367978Srrh 
10377978Srrh 
10387978Srrh 
10397978Srrh char *
trmdir(s)10407978Srrh trmdir(s) register char *s; {
10417978Srrh 	register char *p = s;
10427978Srrh 	while (*p++); --p; while (p>s && *--p!='/');
10437978Srrh # if unix
10447978Srrh 	if (p==s) *p++='.';
10457978Srrh # endif
10467978Srrh 	*p='\0';
10477978Srrh 	return(s);
10487978Srrh }
10497978Srrh 
10507978Srrh STATIC char *
copy(s)10517978Srrh copy(s) register char *s; {
10527978Srrh 	register char *old;
10537978Srrh 
105424202Smckusick 	old = malloc(strlen(s)+1);
105524202Smckusick 	if (old==NULL) {pperror("no space"); exit(exfail);}
105624202Smckusick 	strcpy(old, s);
105724202Smckusick 	return(lastcopy=old);
10587978Srrh }
10597978Srrh 
10607978Srrh char *
strdex(s,c)10617978Srrh strdex(s,c) char *s,c; {
10627978Srrh 	while (*s) if (*s++==c) return(--s);
10637978Srrh 	return(0);
10647978Srrh }
10657978Srrh 
yywrap()10667978Srrh yywrap(){ return(1); }
10677978Srrh 
main(argc,argv)10687978Srrh main(argc,argv)
10697978Srrh 	char *argv[];
10707978Srrh {
10717978Srrh 	register int i,c;
10727978Srrh 	register char *p;
10737978Srrh 	char *tf,**cp2;
10747978Srrh 
10757978Srrh # if gcos
10767978Srrh 	if (setjmp(env)) return (exfail);
10777978Srrh # endif
10787978Srrh 	p="_$ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
10797978Srrh 		i=0;
10807978Srrh 		while (c= *p++) {
10817978Srrh 			(fastab+COFF)[c] |= IB|NB|SB; (toktyp+COFF)[c]=IDENT;
10827978Srrh #if scw2
10837978Srrh 			/* 53 == 63-10; digits rarely appear in identifiers,
10847978Srrh 			/* and can never be the first char of an identifier.
10857978Srrh 			/* 11 == 53*53/sizeof(macbit) .
10867978Srrh 			*/
10877978Srrh 			++i; (t21+COFF)[c]=(53*i)/11; (t22+COFF)[c]=i%11;
10887978Srrh #endif
10897978Srrh 		}
10907978Srrh 	p="0123456789.";
10917978Srrh 		while (c= *p++) {(fastab+COFF)[c] |= NB|SB; (toktyp+COFF)[c]=NUMBR;}
10927978Srrh # if gcos
10937978Srrh 	p="\n\"'`/\\";
10947978Srrh # else
10957978Srrh 	p="\n\"'/\\";
10967978Srrh # endif
10977978Srrh 		while (c= *p++) (fastab+COFF)[c] |= SB;
10987978Srrh # if gcos
10997978Srrh 	p="\n\"'`\\";
11007978Srrh # else
11017978Srrh 	p="\n\"'\\";
11027978Srrh # endif
11037978Srrh 		while (c= *p++) (fastab+COFF)[c] |= QB;
11047978Srrh 	p="*\n"; while (c= *p++) (fastab+COFF)[c] |= CB;
11057978Srrh 	(fastab+COFF)[warnc] |= WB;
11067978Srrh 	(fastab+COFF)['\0'] |= CB|QB|SB|WB;
11077978Srrh 	for (i=ALFSIZ; --i>=0; ) slotab[i]=fastab[i]|SB;
11087978Srrh 	p=" \t\013\f\r";	/* note no \n;	\v not legal for vertical tab? */
11097978Srrh 		while (c= *p++) (toktyp+COFF)[c]=BLANK;
11107978Srrh #if scw2
11117978Srrh 	for ((t23+COFF)[i=ALFSIZ+7-COFF]=1; --i>=-COFF; )
11127978Srrh 		if (((t23+COFF)[i]=(t23+COFF+1)[i]<<1)==0) (t23+COFF)[i]=1;
11137978Srrh #endif
11147978Srrh 
11157978Srrh # if unix
11167978Srrh 	fnames[ifno=0] = ""; dirnams[0]=dirs[0]=".";
11177978Srrh # endif
11187978Srrh # if ibm
11197978Srrh 	fnames[ifno=0] = "";
11207978Srrh # endif
11217978Srrh # if gcos
11227978Srrh 	if (inquire(stdin, _TTY)) freopen("*src", "rt", stdin);
11237978Srrh # endif
11247978Srrh # if gimpel || gcos
11257978Srrh 	fnames[ifno=0] = (char *)inquire(stdin, _FILENAME);
11267978Srrh 	dirnams[0] = dirs[0] = trmdir(copy(fnames[0]));
11277978Srrh # endif
11287978Srrh 	for(i=1; i<argc; i++)
11297978Srrh 		{
11307978Srrh 		switch(argv[i][0])
11317978Srrh 			{
11327978Srrh 			case '-':
11337978Srrh # if gcos
11347978Srrh 			switch(toupper(argv[i][1])) { /* case-independent on GCOS */
11357978Srrh # else
11367978Srrh 			switch(argv[i][1]) {
11377978Srrh # endif
113816235Smckusick 				case 'M': mflag++;
11397978Srrh 				case 'P': pflag++;
11407978Srrh 				case 'E': continue;
11417978Srrh 				case 'R': ++rflag; continue;
11427978Srrh 				case 'C': passcom++; continue;
11437978Srrh 				case 'D':
11447978Srrh 					if (predef>prespc+NPREDEF) {
11457978Srrh 						pperror("too many -D options, ignoring %s",argv[i]);
11467978Srrh 						continue;
11477978Srrh 					}
11487978Srrh 					/* ignore plain "-D" (no argument) */
11497978Srrh 					if (*(argv[i]+2)) *predef++ = argv[i]+2;
11507978Srrh 					continue;
11517978Srrh 				case 'U':
11527978Srrh 					if (prund>punspc+NPREDEF) {
11537978Srrh 						pperror("too many -U options, ignoring %s",argv[i]);
11547978Srrh 						continue;
11557978Srrh 					}
11567978Srrh 					*prund++ = argv[i]+2;
11577978Srrh 					continue;
11587978Srrh 				case 'I':
11597978Srrh 					if (nd>8) pperror("excessive -I file (%s) ignored",argv[i]);
11607978Srrh 					else dirs[nd++] = argv[i]+2;
11617978Srrh 					continue;
11627978Srrh 				case '\0': continue;
11637978Srrh 				default:
11647978Srrh 					pperror("unknown flag %s", argv[i]);
11657978Srrh 					continue;
11667978Srrh 				}
11677978Srrh 			default:
116825241Smckusick 				if (fin==FIRSTOPEN) {
11697978Srrh 					if (0>(fin=open(argv[i], READ))) {
11707978Srrh 						pperror("No source file %s",argv[i]); exit(8);
11717978Srrh 					}
11727978Srrh 					fnames[ifno]=copy(argv[i]);
117316235Smckusick 					infile=copy(argv[i]);
11747982Srrh 					dirs[0]=dirnams[ifno]=trmdir(argv[i]);
11757978Srrh # ifndef gcos
11767978Srrh /* too dangerous to have file name in same syntactic position
11777978Srrh    be input or output file depending on file redirections,
11787978Srrh    so force output to stdout, willy-nilly
11797978Srrh 	[i don't see what the problem is.  jfr]
11807978Srrh */
11817978Srrh 				} else if (fout==stdout) {
11827978Srrh 					if (NULL==(fout=fopen(argv[i], "w"))) {
11837978Srrh 						pperror("Can't create %s", argv[i]); exit(8);
118416495Sralph 					} else fclose(stdout);
11857978Srrh # endif
11867978Srrh 				} else pperror("extraneous name %s", argv[i]);
11877978Srrh 			}
11887978Srrh 		}
118925422Smckusick 	if (fin == FIRSTOPEN)
119025422Smckusick 		fin = STDIN;
11917978Srrh 
119216235Smckusick 	if (mflag) {
119316235Smckusick 		if (infile==(char *)0) {
119416235Smckusick 			fprintf(stderr,
119516235Smckusick 				"no input file specified with -M flag\n");
119616235Smckusick 			exit(8);
119716235Smckusick 		}
119816235Smckusick 		tf=(char *)rindex(infile, '.');
119916235Smckusick 		if (tf==0) {
120016235Smckusick 			fprintf(stderr, "missing component name on %s\n",
120116235Smckusick 				infile);
120216235Smckusick 			exit(8);
120316235Smckusick 		}
120416235Smckusick 		tf[1]='o';
120516235Smckusick 		tf=(char *)rindex(infile, '/');
120616235Smckusick 		if (tf!=(char *)0)
120716235Smckusick 			infile = tf + 1;
120816235Smckusick 		mout=fout;
120937777Sbostic 		if (NULL==(fout=fopen(_PATH_DEVNULL, "w"))) {
121037777Sbostic 			fprintf(stderr, "cpp: can't open %s\n", _PATH_DEVNULL);
121116235Smckusick 			exit(8);
121216235Smckusick 		}
121316235Smckusick 	}
12147978Srrh 	fins[ifno]=fin;
12157978Srrh 	exfail = 0;
12167978Srrh 		/* after user -I files here are the standard include libraries */
12177978Srrh # if unix
121837777Sbostic 	dirs[nd++] = _PATH_INCLUDES;
12197978Srrh # endif
12207978Srrh # if gcos
12217978Srrh 	dirs[nd++] = "cc/include";
12227978Srrh # endif
12237978Srrh # if ibm
12247978Srrh # ifndef gimpel
12257978Srrh 	dirs[nd++] = "BTL$CLIB";
12267978Srrh # endif
12277978Srrh # endif
12287978Srrh # ifdef gimpel
12297978Srrh 	dirs[nd++] = intss() ?  "SYS3.C." : "" ;
12307978Srrh # endif
12317978Srrh 	/* dirs[nd++] = "/compool"; */
12327978Srrh 	dirs[nd++] = 0;
12337978Srrh 	defloc=ppsym("define");
12347978Srrh 	udfloc=ppsym("undef");
12357978Srrh 	incloc=ppsym("include");
12367978Srrh 	elsloc=ppsym("else");
12377978Srrh 	eifloc=ppsym("endif");
12387978Srrh 	ifdloc=ppsym("ifdef");
12397978Srrh 	ifnloc=ppsym("ifndef");
12407978Srrh 	ifloc=ppsym("if");
12417978Srrh 	lneloc=ppsym("line");
124224202Smckusick 	identloc=ppsym("ident");	/* Sys 5r3 compatibility */
12437978Srrh 	for (i=sizeof(macbit)/sizeof(macbit[0]); --i>=0; ) macbit[i]=0;
12447978Srrh # if unix
12457978Srrh 	ysysloc=stsym("unix");
12467978Srrh # endif
124731186Sbostic 	ysysloc=stsym(MACHINE);
12487978Srrh 	ulnloc=stsym ("__LINE__");
12497978Srrh 	uflloc=stsym ("__FILE__");
12507978Srrh 
12517978Srrh 	tf=fnames[ifno]; fnames[ifno]="command line"; lineno[ifno]=1;
12527978Srrh 	cp2=prespc;
12537978Srrh 	while (cp2<predef) stsym(*cp2++);
12547978Srrh 	cp2=punspc;
12557978Srrh 	while (cp2<prund) {
12567978Srrh 		if (p=strdex(*cp2, '=')) *p++='\0';
12577978Srrh 		lookup(*cp2++, DROP);
12587978Srrh 	}
12597978Srrh 	fnames[ifno]=tf;
12607978Srrh 	pbeg=buffer+NCPS; pbuf=pbeg+BUFSIZ; pend=pbuf+BUFSIZ;
12617978Srrh 
12627978Srrh 	trulvl = 0; flslvl = 0;
126316235Smckusick 	lineno[0] = 1; sayline(START);
12647978Srrh 	outp=inp=pend;
12657978Srrh 	control(pend);
12667978Srrh 	return (exfail);
12677978Srrh }
1268