xref: /csrg-svn/old/cpp/cpp.c (revision 10083)
17978Srrh #ifndef lint
2*10083Ssam static char sccsid[] = "@(#)cpp.c 1.4 01/02/83";
37978Srrh #endif lint
47982Srrh 
57978Srrh #ifdef FLEXNAMES
67978Srrh #define	NCPS	128
77978Srrh #else
87978Srrh #define	NCPS	8
97978Srrh #endif
107978Srrh 
117978Srrh # include "stdio.h"
127982Srrh # include "ctype.h"
137978Srrh /* C command
147978Srrh /* written by John F. Reiser
157978Srrh /* July/August 1978
167978Srrh */
177978Srrh 
187978Srrh #define STATIC
197978Srrh 
207978Srrh #define STDIN 0
217978Srrh #define STDOUT 1
227978Srrh #define STDERR 2
237978Srrh #define READ 0
247978Srrh #define WRITE 1
257978Srrh #define SALT '#'
267978Srrh #ifndef BUFSIZ
277978Srrh #define BUFSIZ 512
287978Srrh #endif
297978Srrh 
307978Srrh char *pbeg,*pbuf,*pend;
317978Srrh char *outp,*inp;
327978Srrh char *newp;
337978Srrh char cinit;
347978Srrh 
357978Srrh /* some code depends on whether characters are sign or zero extended */
367978Srrh /*	#if '\377' < 0		not used here, old cpp doesn't understand */
377978Srrh #if pdp11 | vax
387978Srrh #define COFF 128
397978Srrh #else
407978Srrh #define COFF 0
417978Srrh #endif
427978Srrh 
437978Srrh # if gcos
447978Srrh #define ALFSIZ 512	/* alphabet size */
457978Srrh # else
467978Srrh #define ALFSIZ 256	/* alphabet size */
477978Srrh # endif
487978Srrh char macbit[ALFSIZ+11];
497978Srrh char toktyp[ALFSIZ];
507978Srrh #define BLANK 1
517978Srrh #define IDENT 2
527978Srrh #define NUMBR 3
537978Srrh 
547978Srrh /* a superimposed code is used to reduce the number of calls to the
557978Srrh /* symbol table lookup routine.  (if the kth character of an identifier
567978Srrh /* is 'a' and there are no macro names whose kth character is 'a'
577978Srrh /* then the identifier cannot be a macro name, hence there is no need
587978Srrh /* to look in the symbol table.)  'scw1' enables the test based on
597978Srrh /* single characters and their position in the identifier.  'scw2'
607978Srrh /* enables the test based on adjacent pairs of characters and their
617978Srrh /* position in the identifier.  scw1 typically costs 1 indexed fetch,
627978Srrh /* an AND, and a jump per character of identifier, until the identifier
637978Srrh /* is known as a non-macro name or until the end of the identifier.
647978Srrh /* scw1 is inexpensive.  scw2 typically costs 4 indexed fetches,
657978Srrh /* an add, an AND, and a jump per character of identifier, but it is also
667978Srrh /* slightly more effective at reducing symbol table searches.
677978Srrh /* scw2 usually costs too much because the symbol table search is
687978Srrh /* usually short; but if symbol table search should become expensive,
697978Srrh /* the code is here.
707978Srrh /* using both scw1 and scw2 is of dubious value.
717978Srrh */
727978Srrh #define scw1 1
737978Srrh #define scw2 0
747978Srrh 
757978Srrh #if scw2
767978Srrh char t21[ALFSIZ],t22[ALFSIZ],t23[ALFSIZ+NCPS];
777978Srrh #endif
787978Srrh 
797978Srrh #if scw1
807978Srrh #define b0 1
817978Srrh #define b1 2
827978Srrh #define b2 4
837978Srrh #define b3 8
847978Srrh #define b4 16
857978Srrh #define b5 32
867978Srrh #define b6 64
877978Srrh #define b7 128
887978Srrh #endif
897978Srrh 
907978Srrh #define IB 1
917978Srrh #define SB 2
927978Srrh #define NB 4
937978Srrh #define CB 8
947978Srrh #define QB 16
957978Srrh #define WB 32
967978Srrh char fastab[ALFSIZ];
977978Srrh char slotab[ALFSIZ];
987978Srrh char *ptrtab;
997978Srrh #define isslo (ptrtab==(slotab+COFF))
1007978Srrh #define isid(a)  ((fastab+COFF)[a]&IB)
1017978Srrh #define isspc(a) (ptrtab[a]&SB)
1027978Srrh #define isnum(a) ((fastab+COFF)[a]&NB)
1037978Srrh #define iscom(a) ((fastab+COFF)[a]&CB)
1047978Srrh #define isquo(a) ((fastab+COFF)[a]&QB)
1057978Srrh #define iswarn(a) ((fastab+COFF)[a]&WB)
1067978Srrh 
1077978Srrh #define eob(a) ((a)>=pend)
1087978Srrh #define bob(a) (pbeg>=(a))
1097978Srrh 
1107978Srrh char buffer[NCPS+BUFSIZ+BUFSIZ+NCPS];
1117978Srrh 
1128242Srrh # define SBSIZE 60000		/* std = 12000, wnj aug 1979 */
1137978Srrh char	sbf[SBSIZE];
1147978Srrh char	*savch	= sbf;
1157978Srrh 
1167978Srrh # define DROP 0xFE	/* special character not legal ASCII or EBCDIC */
1177978Srrh # define WARN DROP
1187978Srrh # define SAME 0
1197978Srrh # define MAXINC 10
1207978Srrh # define MAXFRE 14	/* max buffers of macro pushback */
1217978Srrh # define MAXFRM 31	/* max number of formals/actuals to a macro */
1227978Srrh 
1237978Srrh static char warnc = WARN;
1247978Srrh 
1257978Srrh int mactop,fretop;
1267978Srrh char *instack[MAXFRE],*bufstack[MAXFRE],*endbuf[MAXFRE];
1277978Srrh 
1287978Srrh int plvl;	/* parenthesis level during scan for macro actuals */
1297978Srrh int maclin;	/* line number of macro call requiring actuals */
1307978Srrh char *macfil;	/* file name of macro call requiring actuals */
1317978Srrh char *macnam;	/* name of macro requiring actuals */
1327978Srrh int maclvl;	/* # calls since last decrease in nesting level */
1337978Srrh char *macforw;	/* pointer which must be exceeded to decrease nesting level */
1347978Srrh int macdam;	/* offset to macforw due to buffer shifting */
1357978Srrh 
1367978Srrh #if tgp
1377978Srrh int tgpscan;	/* flag for dump(); */
1387978Srrh #endif
1397978Srrh 
1407978Srrh STATIC	int	inctop[MAXINC];
1417978Srrh STATIC	char	*fnames[MAXINC];
1427978Srrh STATIC	char	*dirnams[MAXINC];	/* actual directory of #include files */
1437978Srrh STATIC	int	fins[MAXINC];
1447978Srrh STATIC	int	lineno[MAXINC];
1457978Srrh 
1467978Srrh STATIC	char	*dirs[10];	/* -I and <> directories */
1477978Srrh char *strdex(), *copy(), *subst(), *trmdir();
1487978Srrh struct symtab *stsym();
1497978Srrh STATIC	int	fin	= STDIN;
1507978Srrh STATIC	FILE	*fout	= stdout;
1517978Srrh STATIC	int	nd	= 1;
1527978Srrh STATIC	int	pflag;	/* don't put out lines "# 12 foo.c" */
1537978Srrh STATIC	int	passcom;	/* don't delete comments */
1547978Srrh STATIC	int rflag;	/* allow macro recursion */
1557978Srrh STATIC	int	ifno;
1567978Srrh # define NPREDEF 20
1577978Srrh STATIC	char *prespc[NPREDEF];
1587978Srrh STATIC	char **predef = prespc;
1597978Srrh STATIC	char *punspc[NPREDEF];
1607978Srrh STATIC	char **prund = punspc;
1617978Srrh STATIC	int	exfail;
1627978Srrh struct symtab {
1637978Srrh 	char	*name;
1647978Srrh 	char	*value;
1657978Srrh } *lastsym, *lookup(), *slookup();
1667978Srrh 
1677978Srrh # if gcos
1687978Srrh #include <setjmp.h>
1697978Srrh static jmp_buf env;
1707978Srrh # define main	mainpp
1717978Srrh # undef exit
1727978Srrh # define exit(S)	longjmp(env, 1)
1737978Srrh # define open(S,D)	fileno(fopen(S, "r"))
1747978Srrh # define close(F)	fclose(_f[F])
1757978Srrh extern FILE *_f[];
1767978Srrh # define symsiz 500
1777978Srrh # else
1788242Srrh # define symsiz 1500		/* std = 500, wnj aug 1979 */
1797978Srrh # endif
1807978Srrh STATIC	struct symtab stab[symsiz];
1817978Srrh 
1827978Srrh STATIC	struct symtab *defloc;
1837978Srrh STATIC	struct symtab *udfloc;
1847978Srrh STATIC	struct symtab *incloc;
1857978Srrh STATIC	struct symtab *ifloc;
1867978Srrh STATIC	struct symtab *elsloc;
1877978Srrh STATIC	struct symtab *eifloc;
1887978Srrh STATIC	struct symtab *ifdloc;
1897978Srrh STATIC	struct symtab *ifnloc;
1907978Srrh STATIC	struct symtab *ysysloc;
1917978Srrh STATIC	struct symtab *varloc;
1927978Srrh STATIC	struct symtab *lneloc;
1937978Srrh STATIC	struct symtab *ulnloc;
1947978Srrh STATIC	struct symtab *uflloc;
1957978Srrh STATIC	int	trulvl;
1967978Srrh STATIC	int	flslvl;
1977978Srrh 
1987978Srrh sayline() {
1997978Srrh 	if (pflag==0) fprintf(fout,"# %d \"%s\"\n", lineno[ifno], fnames[ifno]);
2007978Srrh }
2017978Srrh 
2027978Srrh /* data structure guide
2037978Srrh /*
2047978Srrh /* most of the scanning takes place in the buffer:
2057978Srrh /*
2067978Srrh /*  (low address)                                             (high address)
2077978Srrh /*  pbeg                           pbuf                                 pend
2087978Srrh /*  |      <-- BUFSIZ chars -->      |         <-- BUFSIZ chars -->        |
2097978Srrh /*  _______________________________________________________________________
2107978Srrh /* |_______________________________________________________________________|
2117978Srrh /*          |               |               |
2127978Srrh /*          |<-- waiting -->|               |<-- waiting -->
2137978Srrh /*          |    to be      |<-- current -->|    to be
2147978Srrh /*          |    written    |    token      |    scanned
2157978Srrh /*          |               |               |
2167978Srrh /*          outp            inp             p
2177978Srrh /*
2187978Srrh /*  *outp   first char not yet written to output file
2197978Srrh /*  *inp    first char of current token
2207978Srrh /*  *p      first char not yet scanned
2217978Srrh /*
2227978Srrh /* macro expansion: write from *outp to *inp (chars waiting to be written),
2237978Srrh /* ignore from *inp to *p (chars of the macro call), place generated
2247978Srrh /* characters in front of *p (in reverse order), update pointers,
2257978Srrh /* resume scanning.
2267978Srrh /*
2277978Srrh /* symbol table pointers point to just beyond the end of macro definitions;
2287978Srrh /* the first preceding character is the number of formal parameters.
2297978Srrh /* the appearance of a formal in the body of a definition is marked by
2307978Srrh /* 2 chars: the char WARN, and a char containing the parameter number.
2317978Srrh /* the first char of a definition is preceded by a zero character.
2327978Srrh /*
2337978Srrh /* when macro expansion attempts to back up over the beginning of the
2347978Srrh /* buffer, some characters preceding *pend are saved in a side buffer,
2357978Srrh /* the address of the side buffer is put on 'instack', and the rest
2367978Srrh /* of the main buffer is moved to the right.  the end of the saved buffer
2377978Srrh /* is kept in 'endbuf' since there may be nulls in the saved buffer.
2387978Srrh /*
2397978Srrh /* similar action is taken when an 'include' statement is processed,
2407978Srrh /* except that the main buffer must be completely emptied.  the array
2417978Srrh /* element 'inctop[ifno]' records the last side buffer saved when
2427978Srrh /* file 'ifno' was included.  these buffers remain dormant while
2437978Srrh /* the file is being read, and are reactivated at end-of-file.
2447978Srrh /*
2457978Srrh /* instack[0 : mactop] holds the addresses of all pending side buffers.
2467978Srrh /* instack[inctop[ifno]+1 : mactop-1] holds the addresses of the side
2477978Srrh /* buffers which are "live"; the side buffers instack[0 : inctop[ifno]]
2487978Srrh /* are dormant, waiting for end-of-file on the current file.
2497978Srrh /*
2507978Srrh /* space for side buffers is obtained from 'savch' and is never returned.
2517978Srrh /* bufstack[0:fretop-1] holds addresses of side buffers which
2527978Srrh /* are available for use.
2537978Srrh */
2547978Srrh 
2557978Srrh dump() {
2567978Srrh /* write part of buffer which lies between  outp  and  inp .
2577978Srrh /* this should be a direct call to 'write', but the system slows to a crawl
2587978Srrh /* if it has to do an unaligned copy.  thus we buffer.  this silly loop
2597978Srrh /* is 15% of the total time, thus even the 'putc' macro is too slow.
2607978Srrh */
2617978Srrh 	register char *p1,*p2; register FILE *f;
2627978Srrh 	if ((p1=outp)==inp || flslvl!=0) return;
2637978Srrh #if tgp
2647978Srrh #define MAXOUT 80
2657978Srrh 	if (!tgpscan) {/* scan again to insure <= MAXOUT chars between linefeeds */
2667978Srrh 		register char c,*pblank; char savc,stopc,brk;
2677978Srrh 		tgpscan=1; brk=stopc=pblank=0; p2=inp; savc= *p2; *p2='\0';
2687978Srrh 		while (c= *p1++) {
2697978Srrh 			if (c=='\\') c= *p1++;
2707978Srrh 			if (stopc==c) stopc=0;
2717978Srrh 			else if (c=='"' || c=='\'') stopc=c;
2727978Srrh 			if (p1-outp>MAXOUT && pblank!=0) {
2737978Srrh 				*pblank++='\n'; inp=pblank; dump(); brk=1; pblank=0;
2747978Srrh 			}
2757978Srrh 			if (c==' ' && stopc==0) pblank=p1-1;
2767978Srrh 		}
2777978Srrh 		if (brk) sayline();
2787978Srrh 		*p2=savc; inp=p2; p1=outp; tgpscan=0;
2797978Srrh 	}
2807978Srrh #endif
2817978Srrh 	f=fout;
2827978Srrh # if gcos
2837978Srrh /* filter out "$ program c" card if first line of input */
2847978Srrh /* gmatch is a simple pattern matcher in the GCOS Standard Library */
2857978Srrh {	static int gmfirst = 0;
2867978Srrh 	if (!gmfirst) {
2877978Srrh 		++gmfirst;
2887978Srrh 		if (gmatch(p1, "^$*program[ \t]*c*"))
2897978Srrh 			p1 = strdex(p1, '\n');
2907978Srrh 	}
2917978Srrh }
2927978Srrh # endif
2937978Srrh 	while (p1<inp) putc(*p1++,f);
2947978Srrh 	outp=p1;
2957978Srrh }
2967978Srrh 
2977978Srrh char *
2987978Srrh refill(p) register char *p; {
2997978Srrh /* dump buffer.  save chars from inp to p.  read into buffer at pbuf,
3007978Srrh /* contiguous with p.  update pointers, return new p.
3017978Srrh */
3027978Srrh 	register char *np,*op; register int ninbuf;
3037978Srrh 	dump(); np=pbuf-(p-inp); op=inp;
3047978Srrh 	if (bob(np+1)) {pperror("token too long"); np=pbeg; p=inp+BUFSIZ;}
3057978Srrh 	macdam += np-inp; outp=inp=np;
3067978Srrh 	while (op<p) *np++= *op++;
3077978Srrh 	p=np;
3087978Srrh 	for (;;) {
3097978Srrh 		if (mactop>inctop[ifno]) {/* retrieve hunk of pushed-back macro text */
3107978Srrh 			op=instack[--mactop]; np=pbuf;
3117978Srrh 			do {while (*np++= *op++);} while (op<endbuf[mactop]); pend=np-1;
3127978Srrh 			/* make buffer space avail for 'include' processing */
3137978Srrh 			if (fretop<MAXFRE) bufstack[fretop++]=instack[mactop];
3147978Srrh 			return(p);
3157978Srrh 		} else {/* get more text from file(s) */
3167978Srrh 			maclvl=0;
3177978Srrh 			if (0<(ninbuf=read(fin,pbuf,BUFSIZ))) {
3187978Srrh 				pend=pbuf+ninbuf; *pend='\0';
3197978Srrh 				return(p);
3207978Srrh 			}
3217978Srrh 			/* end of #include file */
3227978Srrh 			if (ifno==0) {/* end of input */
3237978Srrh 				if (plvl!=0) {
3247978Srrh 					int n=plvl,tlin=lineno[ifno]; char *tfil=fnames[ifno];
3257978Srrh 					lineno[ifno]=maclin; fnames[ifno]=macfil;
3267978Srrh 					pperror("%s: unterminated macro call",macnam);
3277978Srrh 					lineno[ifno]=tlin; fnames[ifno]=tfil;
3287978Srrh 					np=p; *np++='\n';	/* shut off unterminated quoted string */
3297978Srrh 					while (--n>=0) *np++=')';	/* supply missing parens */
3307978Srrh 					pend=np; *np='\0'; if (plvl<0) plvl=0;
3317978Srrh 					return(p);
3327978Srrh 				}
3337978Srrh 				inp=p; dump(); exit(exfail);
3347978Srrh 			}
3357978Srrh 			close(fin); fin=fins[--ifno]; dirs[0]=dirnams[ifno]; sayline();
3367978Srrh 		}
3377978Srrh 	}
3387978Srrh }
3397978Srrh 
3407978Srrh #define BEG 0
3417978Srrh #define LF 1
3427978Srrh 
3437978Srrh char *
3447978Srrh cotoken(p) register char *p; {
3457978Srrh 	register int c,i; char quoc;
3467978Srrh 	static int state = BEG;
3477978Srrh 
3487978Srrh 	if (state!=BEG) goto prevlf;
3497978Srrh for (;;) {
3507978Srrh again:
3517978Srrh 	while (!isspc(*p++));
3527978Srrh 	switch (*(inp=p-1)) {
3537978Srrh 	case 0: {
3547978Srrh 		if (eob(--p)) {p=refill(p); goto again;}
3557978Srrh 		else ++p; /* ignore null byte */
3567978Srrh 	} break;
3577978Srrh 	case '|': case '&': for (;;) {/* sloscan only */
3587978Srrh 		if (*p++== *inp) break;
3597978Srrh 		if (eob(--p)) p=refill(p);
3607978Srrh 		else break;
3617978Srrh 	} break;
3627978Srrh 	case '=': case '!': for (;;) {/* sloscan only */
3637978Srrh 		if (*p++=='=') break;
3647978Srrh 		if (eob(--p)) p=refill(p);
3657978Srrh 		else break;
3667978Srrh 	} break;
3677978Srrh 	case '<': case '>': for (;;) {/* sloscan only */
3687978Srrh 		if (*p++=='=' || p[-2]==p[-1]) break;
3697978Srrh 		if (eob(--p)) p=refill(p);
3707978Srrh 		else break;
3717978Srrh 	} break;
3727978Srrh 	case '\\': for (;;) {
3737978Srrh 		if (*p++=='\n') {++lineno[ifno]; break;}
3747978Srrh 		if (eob(--p)) p=refill(p);
3757978Srrh 		else {++p; break;}
3767978Srrh 	} break;
3777978Srrh 	case '/': for (;;) {
3787978Srrh 		if (*p++=='*') {/* comment */
3797978Srrh 			if (!passcom) {inp=p-2; dump(); ++flslvl;}
3807978Srrh 			for (;;) {
3817978Srrh 				while (!iscom(*p++));
3827978Srrh 				if (p[-1]=='*') for (;;) {
3837978Srrh 					if (*p++=='/') goto endcom;
3847978Srrh 					if (eob(--p)) {
3857978Srrh 						if (!passcom) {inp=p; p=refill(p);}
3867978Srrh 						else if ((p-inp)>=BUFSIZ) {/* split long comment */
3877978Srrh 							inp=p; p=refill(p);	/* last char written is '*' */
3887978Srrh 							putc('/',fout);	/* terminate first part */
3897978Srrh 							/* and fake start of 2nd */
3907978Srrh 							outp=inp=p-=3; *p++='/'; *p++='*'; *p++='*';
3917978Srrh 						} else p=refill(p);
3927978Srrh 					} else break;
3937978Srrh 				} else if (p[-1]=='\n') {
3947978Srrh 					++lineno[ifno]; if (!passcom) putc('\n',fout);
3957978Srrh 				} else if (eob(--p)) {
3967978Srrh 					if (!passcom) {inp=p; p=refill(p);}
3977978Srrh 					else if ((p-inp)>=BUFSIZ) {/* split long comment */
3987978Srrh 						inp=p; p=refill(p);
3997978Srrh 						putc('*',fout); putc('/',fout);
4007978Srrh 						outp=inp=p-=2; *p++='/'; *p++='*';
4017978Srrh 					} else p=refill(p);
4027978Srrh 				} else ++p; /* ignore null byte */
4037978Srrh 			}
4047978Srrh 		endcom:
4057978Srrh 			if (!passcom) {outp=inp=p; --flslvl; goto again;}
4067978Srrh 			break;
4077978Srrh 		}
4087978Srrh 		if (eob(--p)) p=refill(p);
4097978Srrh 		else break;
4107978Srrh 	} break;
4117978Srrh # if gcos
4127978Srrh 	case '`':
4137978Srrh # endif
4147978Srrh 	case '"': case '\'': {
4157978Srrh 		quoc=p[-1];
4167978Srrh 		for (;;) {
4177978Srrh 			while (!isquo(*p++));
4187978Srrh 			if (p[-1]==quoc) break;
4197978Srrh 			if (p[-1]=='\n') {--p; break;} /* bare \n terminates quotation */
4207978Srrh 			if (p[-1]=='\\') for (;;) {
4217978Srrh 				if (*p++=='\n') {++lineno[ifno]; break;} /* escaped \n ignored */
4227978Srrh 				if (eob(--p)) p=refill(p);
4237978Srrh 				else {++p; break;}
4247978Srrh 			} else if (eob(--p)) p=refill(p);
4257978Srrh 			else ++p;	/* it was a different quote character */
4267978Srrh 		}
4277978Srrh 	} break;
4287978Srrh 	case '\n': {
4297978Srrh 		++lineno[ifno]; if (isslo) {state=LF; return(p);}
4307978Srrh prevlf:
4317978Srrh 		state=BEG;
4327978Srrh 		for (;;) {
4337978Srrh 			if (*p++=='#') return(p);
4347978Srrh 			if (eob(inp= --p)) p=refill(p);
4357978Srrh 			else goto again;
4367978Srrh 		}
4377978Srrh 	} break;
4387978Srrh 	case '0': case '1': case '2': case '3': case '4':
4397978Srrh 	case '5': case '6': case '7': case '8': case '9':
4407978Srrh 	for (;;) {
4417978Srrh 		while (isnum(*p++));
4427978Srrh 		if (eob(--p)) p=refill(p);
4437978Srrh 		else break;
4447978Srrh 	} break;
4457978Srrh 	case 'A': case 'B': case 'C': case 'D': case 'E':
4467978Srrh 	case 'F': case 'G': case 'H': case 'I': case 'J':
4477978Srrh 	case 'K': case 'L': case 'M': case 'N': case 'O':
4487978Srrh 	case 'P': case 'Q': case 'R': case 'S': case 'T':
4497978Srrh 	case 'U': case 'V': case 'W': case 'X': case 'Y':
4507978Srrh 	case 'Z': case '_':
4517978Srrh 	case 'a': case 'b': case 'c': case 'd': case 'e':
4527978Srrh 	case 'f': case 'g': case 'h': case 'i': case 'j':
4537978Srrh 	case 'k': case 'l': case 'm': case 'n': case 'o':
4547978Srrh 	case 'p': case 'q': case 'r': case 's': case 't':
4557978Srrh 	case 'u': case 'v': case 'w': case 'x': case 'y':
4567978Srrh 	case 'z':
4577978Srrh #if scw1
4587978Srrh #define tmac1(c,bit) if (!xmac1(c,bit,&)) goto nomac
4597978Srrh #define xmac1(c,bit,op) ((macbit+COFF)[c] op (bit))
4607978Srrh #else
4617978Srrh #define tmac1(c,bit)
4627978Srrh #define xmac1(c,bit,op)
4637978Srrh #endif
4647978Srrh 
4657978Srrh #if scw2
4667978Srrh #define tmac2(c0,c1,cpos) if (!xmac2(c0,c1,cpos,&)) goto nomac
4677978Srrh #define xmac2(c0,c1,cpos,op)\
4687978Srrh 	((macbit+COFF)[(t21+COFF)[c0]+(t22+COFF)[c1]] op (t23+COFF+cpos)[c0])
4697978Srrh #else
4707978Srrh #define tmac2(c0,c1,cpos)
4717978Srrh #define xmac2(c0,c1,cpos,op)
4727978Srrh #endif
4737978Srrh 
4747978Srrh 	if (flslvl) goto nomac;
4757978Srrh 	for (;;) {
4767978Srrh 		c= p[-1];                          tmac1(c,b0);
4777978Srrh 		i= *p++; if (!isid(i)) goto endid; tmac1(i,b1); tmac2(c,i,0);
4787978Srrh 		c= *p++; if (!isid(c)) goto endid; tmac1(c,b2); tmac2(i,c,1);
4797978Srrh 		i= *p++; if (!isid(i)) goto endid; tmac1(i,b3); tmac2(c,i,2);
4807978Srrh 		c= *p++; if (!isid(c)) goto endid; tmac1(c,b4); tmac2(i,c,3);
4817978Srrh 		i= *p++; if (!isid(i)) goto endid; tmac1(i,b5); tmac2(c,i,4);
4827978Srrh 		c= *p++; if (!isid(c)) goto endid; tmac1(c,b6); tmac2(i,c,5);
4837978Srrh 		i= *p++; if (!isid(i)) goto endid; tmac1(i,b7); tmac2(c,i,6);
4847978Srrh 		                                                tmac2(i,0,7);
4857978Srrh 		while (isid(*p++));
4867978Srrh 		if (eob(--p)) {refill(p); p=inp+1; continue;}
4877978Srrh 		goto lokid;
4887978Srrh 	endid:
4897978Srrh 		if (eob(--p)) {refill(p); p=inp+1; continue;}
4907978Srrh 		tmac2(p[-1],0,-1+(p-inp));
4917978Srrh 	lokid:
4927978Srrh 		slookup(inp,p,0); if (newp) {p=newp; goto again;}
4937978Srrh 		else break;
4947978Srrh 	nomac:
4957978Srrh 		while (isid(*p++));
4967978Srrh 		if (eob(--p)) {p=refill(p); goto nomac;}
4977978Srrh 		else break;
4987978Srrh 	} break;
4997978Srrh 	} /* end of switch */
5007978Srrh 
5017978Srrh 	if (isslo) return(p);
5027978Srrh } /* end of infinite loop */
5037978Srrh }
5047978Srrh 
5057978Srrh char *
5067978Srrh skipbl(p) register char *p; {/* get next non-blank token */
5077978Srrh 	do {outp=inp=p; p=cotoken(p);} while ((toktyp+COFF)[*inp]==BLANK);
5087978Srrh 	return(p);
5097978Srrh }
5107978Srrh 
5117978Srrh char *
5127978Srrh unfill(p) register char *p; {
5137978Srrh /* take <= BUFSIZ chars from right end of buffer and put them on instack .
5147978Srrh /* slide rest of buffer to the right, update pointers, return new p.
5157978Srrh */
5167978Srrh 	register char *np,*op; register int d;
5177978Srrh 	if (mactop>=MAXFRE) {
5187978Srrh 		pperror("%s: too much pushback",macnam);
5197978Srrh 		p=inp=pend; dump();	/* begin flushing pushback */
5207978Srrh 		while (mactop>inctop[ifno]) {p=refill(p); p=inp=pend; dump();}
5217978Srrh 	}
5227978Srrh 	if (fretop>0) np=bufstack[--fretop];
5237978Srrh 	else {
5247978Srrh 		np=savch; savch+=BUFSIZ;
5257978Srrh 		if (savch>=sbf+SBSIZE) {pperror("no space"); exit(exfail);}
5267978Srrh 		*savch++='\0';
5277978Srrh 	}
5287978Srrh 	instack[mactop]=np; op=pend-BUFSIZ; if (op<p) op=p;
5297978Srrh 	for (;;) {while (*np++= *op++); if (eob(op)) break;} /* out with old */
5307978Srrh 	endbuf[mactop++]=np;	/* mark end of saved text */
5317978Srrh 	np=pbuf+BUFSIZ; op=pend-BUFSIZ; pend=np; if (op<p) op=p;
5327978Srrh 	while (outp<op) *--np= *--op; /* slide over new */
5337978Srrh 	if (bob(np)) pperror("token too long");
5347978Srrh 	d=np-outp; outp+=d; inp+=d; macdam+=d; return(p+d);
5357978Srrh }
5367978Srrh 
5377978Srrh char *
5387978Srrh doincl(p) register char *p; {
5397978Srrh 	int filok,inctype;
5407978Srrh 	register char *cp; char **dirp,*nfil; char filname[BUFSIZ];
5417978Srrh 
5427978Srrh 	p=skipbl(p); cp=filname;
5437978Srrh 	if (*inp++=='<') {/* special <> syntax */
5447978Srrh 		inctype=1;
5457978Srrh 		++flslvl;	/* prevent macro expansion */
5467978Srrh 		for (;;) {
5477978Srrh 			outp=inp=p; p=cotoken(p);
5487978Srrh 			if (*inp=='\n') {--p; *cp='\0'; break;}
5497978Srrh 			if (*inp=='>') {      *cp='\0'; break;}
5507978Srrh # ifdef gimpel
5517978Srrh 			if (*inp=='.' && !intss()) *inp='#';
5527978Srrh # endif
5537978Srrh 			while (inp<p) *cp++= *inp++;
5547978Srrh 		}
5557978Srrh 		--flslvl;	/* reenable macro expansion */
5567978Srrh 	} else if (inp[-1]=='"') {/* regular "" syntax */
5577978Srrh 		inctype=0;
5587978Srrh # ifdef gimpel
5597978Srrh 		while (inp<p) {if (*inp=='.' && !intss()) *inp='#'; *cp++= *inp++;}
5607978Srrh # else
5617978Srrh 		while (inp<p) *cp++= *inp++;
5627978Srrh # endif
5637978Srrh 		if (*--cp=='"') *cp='\0';
5647978Srrh 	} else {pperror("bad include syntax",0); inctype=2;}
5657978Srrh 	/* flush current file to \n , then write \n */
5667978Srrh 	++flslvl; do {outp=inp=p; p=cotoken(p);} while (*inp!='\n'); --flslvl;
5677978Srrh 	inp=p; dump(); if (inctype==2) return(p);
5687978Srrh 	/* look for included file */
5697978Srrh 	if (ifno+1 >=MAXINC) {
5707978Srrh 		pperror("Unreasonable include nesting",0); return(p);
5717978Srrh 	}
5727978Srrh 	if((nfil=savch)>sbf+SBSIZE-BUFSIZ) {pperror("no space"); exit(exfail);}
5737978Srrh 	filok=0;
5747978Srrh 	for (dirp=dirs+inctype; *dirp; ++dirp) {
5757978Srrh 		if (
5767978Srrh # if gcos
5777978Srrh 			strdex(filname, '/')
5787978Srrh # else
5797978Srrh 			filname[0]=='/'
5807978Srrh # endif
5817978Srrh 				|| **dirp=='\0') strcpy(nfil,filname);
5827978Srrh 		else {
5837978Srrh 			strcpy(nfil,*dirp);
5847978Srrh # if unix || gcos
5857978Srrh 			strcat(nfil,"/");
5867978Srrh # endif
5877978Srrh #ifdef ibm
5887978Srrh #ifndef gimpel
5897978Srrh 			strcat(nfil,".");
5907978Srrh #endif
5917978Srrh #endif
5927978Srrh 			strcat(nfil,filname);
5937978Srrh 		}
5947978Srrh 		if (0<(fins[ifno+1]=open(nfil,READ))) {
5957978Srrh 			filok=1; fin=fins[++ifno]; break;
5967978Srrh 		}
5977978Srrh 	}
5987978Srrh 	if (filok==0) pperror("Can't find include file %s",filname);
5997978Srrh 	else {
6007978Srrh 		lineno[ifno]=1; fnames[ifno]=cp=nfil; while (*cp++); savch=cp;
6017978Srrh 		dirnams[ifno]=dirs[0]=trmdir(copy(nfil));
6027978Srrh 		sayline();
6037978Srrh 		/* save current contents of buffer */
6047978Srrh 		while (!eob(p)) p=unfill(p);
6057978Srrh 		inctop[ifno]=mactop;
6067978Srrh 	}
6077978Srrh 	return(p);
6087978Srrh }
6097978Srrh 
6107978Srrh equfrm(a,p1,p2) register char *a,*p1,*p2; {
6117978Srrh 	register char c; int flag;
6127978Srrh 	c= *p2; *p2='\0';
6137978Srrh 	flag=strcmp(a,p1); *p2=c; return(flag==SAME);
6147978Srrh }
6157978Srrh 
6167978Srrh char *
6177978Srrh dodef(p) char *p; {/* process '#define' */
6187978Srrh 	register char *pin,*psav,*cf;
6197978Srrh 	char **pf,**qf; int b,c,params; struct symtab *np;
6207978Srrh 	char *oldval,*oldsavch;
6217978Srrh 	char *formal[MAXFRM]; /* formal[n] is name of nth formal */
6227978Srrh 	char formtxt[BUFSIZ]; /* space for formal names */
6237978Srrh 
6247978Srrh 	if (savch>sbf+SBSIZE-BUFSIZ) {pperror("too much defining"); return(p);}
6257978Srrh 	oldsavch=savch; /* to reclaim space if redefinition */
6267978Srrh 	++flslvl; /* prevent macro expansion during 'define' */
6277978Srrh 	p=skipbl(p); pin=inp;
6287978Srrh 	if ((toktyp+COFF)[*pin]!=IDENT) {
6297978Srrh 		ppwarn("illegal macro name"); while (*inp!='\n') p=skipbl(p); return(p);
6307978Srrh 	}
6317978Srrh 	np=slookup(pin,p,1);
6327978Srrh 	if (oldval=np->value) savch=oldsavch;	/* was previously defined */
6337978Srrh 	b=1; cf=pin;
6347978Srrh 	while (cf<p) {/* update macbit */
6357978Srrh 		c= *cf++; xmac1(c,b,|=); b=(b+b)&0xFF;
6367978Srrh 		if (cf!=p) xmac2(c,*cf,-1+(cf-pin),|=);
6377978Srrh 		else xmac2(c,0,-1+(cf-pin),|=);
6387978Srrh 	}
6397978Srrh 	params=0; outp=inp=p; p=cotoken(p); pin=inp;
6407978Srrh 	if (*pin=='(') {/* with parameters; identify the formals */
6417978Srrh 		cf=formtxt; pf=formal;
6427978Srrh 		for (;;) {
6437978Srrh 			p=skipbl(p); pin=inp;
6447978Srrh 			if (*pin=='\n') {
6457978Srrh 				--lineno[ifno]; --p; pperror("%s: missing )",np->name); break;
6467978Srrh 			}
6477978Srrh 			if (*pin==')') break;
6487978Srrh 			if (*pin==',') continue;
6497978Srrh 			if ((toktyp+COFF)[*pin]!=IDENT) {
6507978Srrh 				c= *p; *p='\0'; pperror("bad formal: %s",pin); *p=c;
6517978Srrh 			} else if (pf>= &formal[MAXFRM]) {
6527978Srrh 				c= *p; *p='\0'; pperror("too many formals: %s",pin); *p=c;
6537978Srrh 			} else {
6547978Srrh 				*pf++=cf; while (pin<p) *cf++= *pin++; *cf++='\0'; ++params;
6557978Srrh 			}
6567978Srrh 		}
6577978Srrh 		if (params==0) --params; /* #define foo() ... */
6587978Srrh 	} else if (*pin=='\n') {--lineno[ifno]; --p;}
6597978Srrh 	/* remember beginning of macro body, so that we can
6607978Srrh 	/* warn if a redefinition is different from old value.
6617978Srrh 	*/
6627978Srrh 	oldsavch=psav=savch;
6637978Srrh 	for (;;) {/* accumulate definition until linefeed */
6647978Srrh 		outp=inp=p; p=cotoken(p); pin=inp;
6657978Srrh 		if (*pin=='\\' && pin[1]=='\n') {putc('\n',fout); continue;}	/* ignore escaped lf */
6667978Srrh 		if (*pin=='\n') break;
6677978Srrh 		if (params) {/* mark the appearance of formals in the definiton */
6687978Srrh 			if ((toktyp+COFF)[*pin]==IDENT) {
6697978Srrh 				for (qf=pf; --qf>=formal; ) {
6707978Srrh 					if (equfrm(*qf,pin,p)) {
6717978Srrh 						*psav++=qf-formal+1; *psav++=WARN; pin=p; break;
6727978Srrh 					}
6737978Srrh 				}
6747978Srrh 			} else if (*pin=='"' || *pin=='\''
6757978Srrh # if gcos
6767978Srrh 					|| *pin=='`'
6777978Srrh # endif
6787978Srrh 						) {/* inside quotation marks, too */
6797978Srrh 				char quoc= *pin;
6807978Srrh 				for (*psav++= *pin++; pin<p && *pin!=quoc; ) {
6817978Srrh 					while (pin<p && !isid(*pin)) *psav++= *pin++;
6827978Srrh 					cf=pin; while (cf<p && isid(*cf)) ++cf;
6837978Srrh 					for (qf=pf; --qf>=formal; ) {
6847978Srrh 						if (equfrm(*qf,pin,cf)) {
6857978Srrh 							*psav++=qf-formal+1; *psav++=WARN; pin=cf; break;
6867978Srrh 						}
6877978Srrh 					}
6887978Srrh 					while (pin<cf) *psav++= *pin++;
6897978Srrh 				}
6907978Srrh 			}
6917978Srrh 		}
6927978Srrh 		while (pin<p) *psav++= *pin++;
6937978Srrh 	}
6947978Srrh 	*psav++=params; *psav++='\0';
6957978Srrh 	if ((cf=oldval)!=NULL) {/* redefinition */
6967978Srrh 		--cf;	/* skip no. of params, which may be zero */
6977978Srrh 		while (*--cf);	/* go back to the beginning */
6987978Srrh 		if (0!=strcmp(++cf,oldsavch)) {/* redefinition different from old */
6997978Srrh 			--lineno[ifno]; ppwarn("%s redefined",np->name); ++lineno[ifno];
7007978Srrh 			np->value=psav-1;
7017978Srrh 		} else psav=oldsavch; /* identical redef.; reclaim space */
7027978Srrh 	} else np->value=psav-1;
7037978Srrh 	--flslvl; inp=pin; savch=psav; return(p);
7047978Srrh }
7057978Srrh 
7067978Srrh #define fasscan() ptrtab=fastab+COFF
7077978Srrh #define sloscan() ptrtab=slotab+COFF
7087978Srrh 
7097978Srrh char *
7107978Srrh control(p) register char *p; {/* find and handle preprocessor control lines */
7117978Srrh 	register struct symtab *np;
7127978Srrh for (;;) {
7137978Srrh 	fasscan(); p=cotoken(p); if (*inp=='\n') ++inp; dump();
7147978Srrh 	sloscan(); p=skipbl(p);
7157978Srrh 	*--inp=SALT; outp=inp; ++flslvl; np=slookup(inp,p,0); --flslvl;
7167978Srrh 	if (np==defloc) {/* define */
7177978Srrh 		if (flslvl==0) {p=dodef(p); continue;}
7187978Srrh 	} else if (np==incloc) {/* include */
7197978Srrh 		if (flslvl==0) {p=doincl(p); continue;}
7207978Srrh 	} else if (np==ifnloc) {/* ifndef */
7217978Srrh 		++flslvl; p=skipbl(p); np=slookup(inp,p,0); --flslvl;
7227978Srrh 		if (flslvl==0 && np->value==0) ++trulvl;
7237978Srrh 		else ++flslvl;
7247978Srrh 	} else if (np==ifdloc) {/* ifdef */
7257978Srrh 		++flslvl; p=skipbl(p); np=slookup(inp,p,0); --flslvl;
7267978Srrh 		if (flslvl==0 && np->value!=0) ++trulvl;
7277978Srrh 		else ++flslvl;
7287978Srrh 	} else if (np==eifloc) {/* endif */
7297978Srrh 		if (flslvl) {if (--flslvl==0) sayline();}
7307978Srrh 		else if (trulvl) --trulvl;
7317978Srrh 		else pperror("If-less endif",0);
7327978Srrh 	} else if (np==elsloc) {/* else */
7337978Srrh 		if (flslvl) {
7347978Srrh 			if (--flslvl!=0) ++flslvl;
7357978Srrh 			else {++trulvl; sayline();}
7367978Srrh 		}
7377978Srrh 		else if (trulvl) {++flslvl; --trulvl;}
7387978Srrh 		else pperror("If-less else",0);
7397978Srrh 	} else if (np==udfloc) {/* undefine */
7407978Srrh 		if (flslvl==0) {
7417978Srrh 			++flslvl; p=skipbl(p); slookup(inp,p,DROP); --flslvl;
7427978Srrh 		}
7437978Srrh 	} else if (np==ifloc) {/* if */
7447978Srrh #if tgp
7457978Srrh 		pperror(" IF not implemented, true assumed", 0);
7467978Srrh 		if (flslvl==0) ++trulvl; else ++flslvl;
7477978Srrh #else
7487978Srrh 		newp=p;
7497978Srrh 		if (flslvl==0 && yyparse()) ++trulvl; else ++flslvl;
7507978Srrh 		p=newp;
7517978Srrh #endif
7527978Srrh 	} else if (np==lneloc) {/* line */
7537978Srrh 		if (flslvl==0 && pflag==0) {
7547982Srrh 			char *cp, *cp2, *savestring();
7557978Srrh 			outp=inp=p; *--outp='#'; while (*inp!='\n') p=cotoken(p);
7567982Srrh 			cp = outp + 1;
7577982Srrh 			while (isspace(*cp) && cp < inp)
7587982Srrh 				cp++;
7597982Srrh 			while (isdigit(*cp) && cp < inp)
7607982Srrh 				cp++;
7617982Srrh 			while (*cp != '"' && cp < inp)
7627982Srrh 				cp++;
7637982Srrh 			if (cp < inp) {
7647982Srrh 				cp++;
7657982Srrh 				cp2 = cp;
7667982Srrh 				while (*cp2 != '"' && cp2 < inp)
7677982Srrh 					cp2++;
7687982Srrh 				fnames[ifno] = savestring(cp, cp2);
7697982Srrh 			}
7707978Srrh 			continue;
7717978Srrh 		}
7727978Srrh 	} else if (*++inp=='\n') outp=inp;	/* allows blank line after # */
7737978Srrh 	else pperror("undefined control",0);
7747978Srrh 	/* flush to lf */
7757978Srrh 	++flslvl; while (*inp!='\n') {outp=inp=p; p=cotoken(p);} --flslvl;
7767978Srrh }
7777978Srrh }
7787978Srrh 
7797982Srrh char *
7807982Srrh savestring(start, finish)
7817982Srrh 	register char *start, *finish;
7827982Srrh {
7837982Srrh 	char *retbuf;
7847982Srrh 	register char *cp;
7857982Srrh 
7867982Srrh 	retbuf = (char *) calloc(finish - start + 1, sizeof (char));
7877982Srrh 	cp = retbuf;
7887982Srrh 	while (start < finish)
7897982Srrh 		*cp++ = *start++;
7907982Srrh 	*cp = 0;
7917982Srrh 	return(retbuf);
7927982Srrh }
7937982Srrh 
7947978Srrh struct symtab *
7957978Srrh stsym(s) register char *s; {
7967978Srrh 	char buf[BUFSIZ]; register char *p;
7977978Srrh 
7987978Srrh 	/* make definition look exactly like end of #define line */
7997978Srrh 	/* copy to avoid running off end of world when param list is at end */
8007978Srrh 	p=buf; while (*p++= *s++);
8017978Srrh 	p=buf; while (isid(*p++)); /* skip first identifier */
8027978Srrh 	if (*--p=='=') {*p++=' '; while (*p++);}
8037978Srrh 	else {s=" 1"; while (*p++= *s++);}
8047978Srrh 	pend=p; *--p='\n';
8057978Srrh 	sloscan(); dodef(buf); return(lastsym);
8067978Srrh }
8077978Srrh 
8087978Srrh struct symtab *
8097978Srrh ppsym(s) char *s; {/* kluge */
8107978Srrh 	register struct symtab *sp;
8117978Srrh 	cinit=SALT; *savch++=SALT; sp=stsym(s); --sp->name; cinit=0; return(sp);
8127978Srrh }
8137978Srrh 
8147978Srrh /* VARARGS1 */
8157978Srrh pperror(s,x,y) char *s; {
8167978Srrh 	if (fnames[ifno][0]) fprintf(stderr,
8177978Srrh # if gcos
8187978Srrh 			"*%c*   \"%s\", line ", exfail >= 0 ? 'F' : 'W',
8197978Srrh # else
8207978Srrh 			"%s: ",
8217978Srrh # endif
8227978Srrh 				 fnames[ifno]);
8237978Srrh 	fprintf(stderr, "%d: ",lineno[ifno]);
8247978Srrh 	fprintf(stderr, s, x, y);
8257978Srrh 	fprintf(stderr,"\n");
8267978Srrh 	++exfail;
8277978Srrh }
8287978Srrh 
8297978Srrh yyerror(s,a,b) char *s; {
8307978Srrh 	pperror(s,a,b);
8317978Srrh }
8327978Srrh 
8337978Srrh ppwarn(s,x) char *s; {
8347978Srrh 	int fail = exfail;
8357978Srrh 	exfail = -1;
8367978Srrh 	pperror(s,x);
8377978Srrh 	exfail = fail;
8387978Srrh }
8397978Srrh 
8407978Srrh struct symtab *
8417978Srrh lookup(namep, enterf)
8427978Srrh char *namep;
8437978Srrh {
8447978Srrh 	register char *np, *snp;
8457978Srrh 	register int c, i; int around;
8467978Srrh 	register struct symtab *sp;
8477978Srrh 
8487978Srrh 	/* namep had better not be too long (currently, <=NCPS chars) */
8497978Srrh 	np=namep; around=0; i=cinit;
8507978Srrh 	while (c= *np++) i += i+c; c=i;	/* c=i for register usage on pdp11 */
8517978Srrh 	c %= symsiz; if (c<0) c += symsiz;
8527978Srrh 	sp = &stab[c];
8537978Srrh 	while (snp=sp->name) {
8547978Srrh 		np = namep;
8557978Srrh 		while (*snp++ == *np) if (*np++ == '\0') {
8567978Srrh 				if (enterf==DROP) {sp->name[0]= DROP; sp->value=0;}
8577978Srrh 				return(lastsym=sp);
8587978Srrh 			}
8597978Srrh 		if (--sp < &stab[0])
8607978Srrh 			if (around) {pperror("too many defines", 0); exit(exfail);}
8617978Srrh 			else {++around; sp = &stab[symsiz-1];}
8627978Srrh 	}
8637978Srrh 	if (enterf==1) sp->name=namep;
8647978Srrh 	return(lastsym=sp);
8657978Srrh }
8667978Srrh 
8677978Srrh struct symtab *
8687978Srrh slookup(p1,p2,enterf) register char *p1,*p2; int enterf;{
8697978Srrh 	register char *p3; char c2,c3; struct symtab *np;
8707978Srrh 	         c2= *p2; *p2='\0';	/* mark end of token */
8717978Srrh 	if ((p2-p1)>NCPS) p3=p1+NCPS; else p3=p2;
8727978Srrh 			 c3= *p3; *p3='\0';	/* truncate to NCPS chars or less */
8737978Srrh 	if (enterf==1) p1=copy(p1);
8747978Srrh 	np=lookup(p1,enterf); *p3=c3; *p2=c2;
8757978Srrh 	if (np->value!=0 && flslvl==0) newp=subst(p2,np);
8767978Srrh 	else newp=0;
8777978Srrh 	return(np);
8787978Srrh }
8797978Srrh 
8807978Srrh char *
8817978Srrh subst(p,sp) register char *p; struct symtab *sp; {
8827978Srrh 	static char match[]="%s: argument mismatch";
8837978Srrh 	register char *ca,*vp; int params;
8847978Srrh 	char *actual[MAXFRM]; /* actual[n] is text of nth actual */
8857978Srrh 	char acttxt[BUFSIZ]; /* space for actuals */
8867978Srrh 
8877978Srrh 	if (0==(vp=sp->value)) return(p);
8887978Srrh 	if ((p-macforw)<=macdam) {
8897978Srrh 		if (++maclvl>symsiz && !rflag) {
8907978Srrh 			pperror("%s: macro recursion",sp->name); return(p);
8917978Srrh 		}
8927978Srrh 	} else maclvl=0;	/* level decreased */
8937978Srrh 	macforw=p; macdam=0;	/* new target for decrease in level */
8947978Srrh 	macnam=sp->name;
8957978Srrh 	dump();
8967978Srrh 	if (sp==ulnloc) {
8977978Srrh 		vp=acttxt; *vp++='\0';
8987978Srrh 		sprintf(vp,"%d",lineno[ifno]); while (*vp++);
8997978Srrh 	} else if (sp==uflloc) {
9007978Srrh 		vp=acttxt; *vp++='\0';
9017978Srrh 		sprintf(vp,"\"%s\"",fnames[ifno]); while (*vp++);
9027978Srrh 	}
9037978Srrh 	if (0!=(params= *--vp&0xFF)) {/* definition calls for params */
9047978Srrh 		register char **pa;
9057978Srrh 		ca=acttxt; pa=actual;
9067978Srrh 		if (params==0xFF) params=1;	/* #define foo() ... */
9077978Srrh 		sloscan(); ++flslvl; /* no expansion during search for actuals */
9087978Srrh 		plvl= -1;
9097978Srrh 		do p=skipbl(p); while (*inp=='\n');	/* skip \n too */
9107978Srrh 		if (*inp=='(') {
9117978Srrh 			maclin=lineno[ifno]; macfil=fnames[ifno];
9127978Srrh 			for (plvl=1; plvl!=0; ) {
9137978Srrh 				*ca++='\0';
9147978Srrh 				for (;;) {
9157978Srrh 					outp=inp=p; p=cotoken(p);
9167978Srrh 					if (*inp=='(') ++plvl;
9177978Srrh 					if (*inp==')' && --plvl==0) {--params; break;}
9187978Srrh 					if (plvl==1 && *inp==',') {--params; break;}
9197978Srrh 					while (inp<p) *ca++= *inp++;
9207978Srrh 					if (ca> &acttxt[BUFSIZ])
9217978Srrh 						pperror("%s: actuals too long",sp->name);
9227978Srrh 				}
9237978Srrh 				if (pa>= &actual[MAXFRM]) ppwarn(match,sp->name);
9247978Srrh 				else *pa++=ca;
9257978Srrh 			}
9267978Srrh 		}
9277978Srrh 		if (params!=0) ppwarn(match,sp->name);
9287978Srrh 		while (--params>=0) *pa++=""+1;	/* null string for missing actuals */
9297978Srrh 		--flslvl; fasscan();
9307978Srrh 	}
9317978Srrh 	for (;;) {/* push definition onto front of input stack */
9327978Srrh 		while (!iswarn(*--vp)) {
9337978Srrh 			if (bob(p)) {outp=inp=p; p=unfill(p);}
9347978Srrh 			*--p= *vp;
9357978Srrh 		}
9367978Srrh 		if (*vp==warnc) {/* insert actual param */
9377978Srrh 			ca=actual[*--vp-1];
9387978Srrh 			while (*--ca) {
9397978Srrh 				if (bob(p)) {outp=inp=p; p=unfill(p);}
9407978Srrh 				*--p= *ca;
9417978Srrh 			}
9427978Srrh 		} else break;
9437978Srrh 	}
9447978Srrh 	outp=inp=p;
9457978Srrh 	return(p);
9467978Srrh }
9477978Srrh 
9487978Srrh 
9497978Srrh 
9507978Srrh 
9517978Srrh char *
9527978Srrh trmdir(s) register char *s; {
9537978Srrh 	register char *p = s;
9547978Srrh 	while (*p++); --p; while (p>s && *--p!='/');
9557978Srrh # if unix
9567978Srrh 	if (p==s) *p++='.';
9577978Srrh # endif
9587978Srrh 	*p='\0';
9597978Srrh 	return(s);
9607978Srrh }
9617978Srrh 
9627978Srrh STATIC char *
9637978Srrh copy(s) register char *s; {
9647978Srrh 	register char *old;
9657978Srrh 
9667978Srrh 	old = savch; while (*savch++ = *s++);
9677978Srrh 	return(old);
9687978Srrh }
9697978Srrh 
9707978Srrh char *
9717978Srrh strdex(s,c) char *s,c; {
9727978Srrh 	while (*s) if (*s++==c) return(--s);
9737978Srrh 	return(0);
9747978Srrh }
9757978Srrh 
9767978Srrh yywrap(){ return(1); }
9777978Srrh 
9787978Srrh main(argc,argv)
9797978Srrh 	char *argv[];
9807978Srrh {
9817978Srrh 	register int i,c;
9827978Srrh 	register char *p;
9837978Srrh 	char *tf,**cp2;
9847978Srrh 
9857978Srrh # if gcos
9867978Srrh 	if (setjmp(env)) return (exfail);
9877978Srrh # endif
9887978Srrh 	p="_$ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
9897978Srrh 		i=0;
9907978Srrh 		while (c= *p++) {
9917978Srrh 			(fastab+COFF)[c] |= IB|NB|SB; (toktyp+COFF)[c]=IDENT;
9927978Srrh #if scw2
9937978Srrh 			/* 53 == 63-10; digits rarely appear in identifiers,
9947978Srrh 			/* and can never be the first char of an identifier.
9957978Srrh 			/* 11 == 53*53/sizeof(macbit) .
9967978Srrh 			*/
9977978Srrh 			++i; (t21+COFF)[c]=(53*i)/11; (t22+COFF)[c]=i%11;
9987978Srrh #endif
9997978Srrh 		}
10007978Srrh 	p="0123456789.";
10017978Srrh 		while (c= *p++) {(fastab+COFF)[c] |= NB|SB; (toktyp+COFF)[c]=NUMBR;}
10027978Srrh # if gcos
10037978Srrh 	p="\n\"'`/\\";
10047978Srrh # else
10057978Srrh 	p="\n\"'/\\";
10067978Srrh # endif
10077978Srrh 		while (c= *p++) (fastab+COFF)[c] |= SB;
10087978Srrh # if gcos
10097978Srrh 	p="\n\"'`\\";
10107978Srrh # else
10117978Srrh 	p="\n\"'\\";
10127978Srrh # endif
10137978Srrh 		while (c= *p++) (fastab+COFF)[c] |= QB;
10147978Srrh 	p="*\n"; while (c= *p++) (fastab+COFF)[c] |= CB;
10157978Srrh 	(fastab+COFF)[warnc] |= WB;
10167978Srrh 	(fastab+COFF)['\0'] |= CB|QB|SB|WB;
10177978Srrh 	for (i=ALFSIZ; --i>=0; ) slotab[i]=fastab[i]|SB;
10187978Srrh 	p=" \t\013\f\r";	/* note no \n;	\v not legal for vertical tab? */
10197978Srrh 		while (c= *p++) (toktyp+COFF)[c]=BLANK;
10207978Srrh #if scw2
10217978Srrh 	for ((t23+COFF)[i=ALFSIZ+7-COFF]=1; --i>=-COFF; )
10227978Srrh 		if (((t23+COFF)[i]=(t23+COFF+1)[i]<<1)==0) (t23+COFF)[i]=1;
10237978Srrh #endif
10247978Srrh 
10257978Srrh # if unix
10267978Srrh 	fnames[ifno=0] = ""; dirnams[0]=dirs[0]=".";
10277978Srrh # endif
10287978Srrh # if ibm
10297978Srrh 	fnames[ifno=0] = "";
10307978Srrh # endif
10317978Srrh # if gcos
10327978Srrh 	if (inquire(stdin, _TTY)) freopen("*src", "rt", stdin);
10337978Srrh # endif
10347978Srrh # if gimpel || gcos
10357978Srrh 	fnames[ifno=0] = (char *)inquire(stdin, _FILENAME);
10367978Srrh 	dirnams[0] = dirs[0] = trmdir(copy(fnames[0]));
10377978Srrh # endif
10387978Srrh 	for(i=1; i<argc; i++)
10397978Srrh 		{
10407978Srrh 		switch(argv[i][0])
10417978Srrh 			{
10427978Srrh 			case '-':
10437978Srrh # if gcos
10447978Srrh 			switch(toupper(argv[i][1])) { /* case-independent on GCOS */
10457978Srrh # else
10467978Srrh 			switch(argv[i][1]) {
10477978Srrh # endif
10487978Srrh 				case 'P': pflag++;
10497978Srrh 				case 'E': continue;
10507978Srrh 				case 'R': ++rflag; continue;
10517978Srrh 				case 'C': passcom++; continue;
10527978Srrh 				case 'D':
10537978Srrh 					if (predef>prespc+NPREDEF) {
10547978Srrh 						pperror("too many -D options, ignoring %s",argv[i]);
10557978Srrh 						continue;
10567978Srrh 					}
10577978Srrh 					/* ignore plain "-D" (no argument) */
10587978Srrh 					if (*(argv[i]+2)) *predef++ = argv[i]+2;
10597978Srrh 					continue;
10607978Srrh 				case 'U':
10617978Srrh 					if (prund>punspc+NPREDEF) {
10627978Srrh 						pperror("too many -U options, ignoring %s",argv[i]);
10637978Srrh 						continue;
10647978Srrh 					}
10657978Srrh 					*prund++ = argv[i]+2;
10667978Srrh 					continue;
10677978Srrh 				case 'I':
10687978Srrh 					if (nd>8) pperror("excessive -I file (%s) ignored",argv[i]);
10697978Srrh 					else dirs[nd++] = argv[i]+2;
10707978Srrh 					continue;
10717978Srrh 				case '\0': continue;
10727978Srrh 				default:
10737978Srrh 					pperror("unknown flag %s", argv[i]);
10747978Srrh 					continue;
10757978Srrh 				}
10767978Srrh 			default:
10777978Srrh 				if (fin==STDIN) {
10787978Srrh 					if (0>(fin=open(argv[i], READ))) {
10797978Srrh 						pperror("No source file %s",argv[i]); exit(8);
10807978Srrh 					}
10817978Srrh 					fnames[ifno]=copy(argv[i]);
10827982Srrh 					dirs[0]=dirnams[ifno]=trmdir(argv[i]);
10837978Srrh # ifndef gcos
10847978Srrh /* too dangerous to have file name in same syntactic position
10857978Srrh    be input or output file depending on file redirections,
10867978Srrh    so force output to stdout, willy-nilly
10877978Srrh 	[i don't see what the problem is.  jfr]
10887978Srrh */
10897978Srrh 				} else if (fout==stdout) {
10907978Srrh 					extern char _sobuf[BUFSIZ];
10917978Srrh 					if (NULL==(fout=fopen(argv[i], "w"))) {
10927978Srrh 						pperror("Can't create %s", argv[i]); exit(8);
10937978Srrh 					} else {fclose(stdout); setbuf(fout,_sobuf);}
10947978Srrh # endif
10957978Srrh 				} else pperror("extraneous name %s", argv[i]);
10967978Srrh 			}
10977978Srrh 		}
10987978Srrh 
10997978Srrh 	fins[ifno]=fin;
11007978Srrh 	exfail = 0;
11017978Srrh 		/* after user -I files here are the standard include libraries */
11027978Srrh # if unix
11037978Srrh 	dirs[nd++] = "/usr/include";
11047978Srrh # endif
11057978Srrh # if gcos
11067978Srrh 	dirs[nd++] = "cc/include";
11077978Srrh # endif
11087978Srrh # if ibm
11097978Srrh # ifndef gimpel
11107978Srrh 	dirs[nd++] = "BTL$CLIB";
11117978Srrh # endif
11127978Srrh # endif
11137978Srrh # ifdef gimpel
11147978Srrh 	dirs[nd++] = intss() ?  "SYS3.C." : "" ;
11157978Srrh # endif
11167978Srrh 	/* dirs[nd++] = "/compool"; */
11177978Srrh 	dirs[nd++] = 0;
11187978Srrh 	defloc=ppsym("define");
11197978Srrh 	udfloc=ppsym("undef");
11207978Srrh 	incloc=ppsym("include");
11217978Srrh 	elsloc=ppsym("else");
11227978Srrh 	eifloc=ppsym("endif");
11237978Srrh 	ifdloc=ppsym("ifdef");
11247978Srrh 	ifnloc=ppsym("ifndef");
11257978Srrh 	ifloc=ppsym("if");
11267978Srrh 	lneloc=ppsym("line");
11277978Srrh 	for (i=sizeof(macbit)/sizeof(macbit[0]); --i>=0; ) macbit[i]=0;
11287978Srrh # if unix
11297978Srrh 	ysysloc=stsym("unix");
11307978Srrh # endif
11317978Srrh # if gcos
11327978Srrh 	ysysloc=stsym ("gcos");
11337978Srrh # endif
11347978Srrh # if ibm
11357978Srrh 	ysysloc=stsym ("ibm");
11367978Srrh # endif
11377978Srrh # if pdp11
11387978Srrh 	varloc=stsym("pdp11");
11397978Srrh # endif
11407978Srrh # if vax
11417978Srrh 	varloc=stsym("vax");
11427978Srrh # endif
11437978Srrh # if interdata
11447978Srrh 	varloc=stsym ("interdata");
11457978Srrh # endif
11467978Srrh # if tss
11477978Srrh 	varloc=stsym ("tss");
11487978Srrh # endif
11497978Srrh # if os
11507978Srrh 	varloc=stsym ("os");
11517978Srrh # endif
11527978Srrh # if mert
11537978Srrh 	varloc=stsym ("mert");
11547978Srrh # endif
1155*10083Ssam # if mc68000
1156*10083Ssam 	varloc=stsym("mc68000");
1157*10083Ssam # endif
1158*10083Ssam # if sun
1159*10083Ssam 	varloc=stsym("sun");
1160*10083Ssam # endif
11617978Srrh 	ulnloc=stsym ("__LINE__");
11627978Srrh 	uflloc=stsym ("__FILE__");
11637978Srrh 
11647978Srrh 	tf=fnames[ifno]; fnames[ifno]="command line"; lineno[ifno]=1;
11657978Srrh 	cp2=prespc;
11667978Srrh 	while (cp2<predef) stsym(*cp2++);
11677978Srrh 	cp2=punspc;
11687978Srrh 	while (cp2<prund) {
11697978Srrh 		if (p=strdex(*cp2, '=')) *p++='\0';
11707978Srrh 		lookup(*cp2++, DROP);
11717978Srrh 	}
11727978Srrh 	fnames[ifno]=tf;
11737978Srrh 	pbeg=buffer+NCPS; pbuf=pbeg+BUFSIZ; pend=pbuf+BUFSIZ;
11747978Srrh 
11757978Srrh 	trulvl = 0; flslvl = 0;
11767978Srrh 	lineno[0] = 1; sayline();
11777978Srrh 	outp=inp=pend;
11787978Srrh 	control(pend);
11797978Srrh 	return (exfail);
11807978Srrh }
1181