# 1 "cpp.c"


static char sccsid[] = "@(#)cpp.c	1.7 3/24/84";


# 8 "cpp.c"





# 1 "/usr/include/stdio.h"





extern	struct	_iobuf {
	int	_cnt;
	unsigned char *_ptr;
	unsigned char *_base;
	int	_bufsiz;
	short	_flag;
	char	_file;		
} _iob[];



















# 36 "/usr/include/stdio.h"

















extern struct _iobuf	*fopen();
extern struct _iobuf	*fdopen();
extern struct _iobuf	*freopen();
extern struct _iobuf	*popen();
extern struct _iobuf	*tmpfile();
extern long	ftell();
extern char	*fgets();
extern char	*gets();
# 63 "/usr/include/stdio.h"

extern char	*ctermid();
extern char	*cuserid();
extern char	*tempnam();
extern char	*tmpnam();






# 13 "cpp.c"

# 1 "/usr/include/ctype.h"











extern	char	_ctype_[];
















# 14 "cpp.c"













# 29 "cpp.c"


char *pbeg,*pbuf,*pend;
char *outp,*inp;
char *newp;
char cinit;





# 42 "cpp.c"


# 46 "cpp.c"



char macbit[256	+11];
char toktyp[256	];

























# 78 "cpp.c"



















char fastab[256	];
char slotab[256	];
char *ptrtab;













char buffer[8+1024+1024+8];


char	sbf[60000		];
char	*savch	= sbf;








static char warnc = 0xFE	;

int mactop,fretop;
char *instack[14	],*bufstack[14	],*endbuf[14	];

int plvl;	
int maclin;	
char *macfil;	
char *macnam;	
int maclvl;	
char *macforw;	
int macdam;	

# 141 "cpp.c"


	int	inctop[10];
	char	*fnames[10];
	char	*dirnams[10];	
	int	fins[10];
	int	lineno[10];

	char	*dirs[10];	
char *strdex(), *copy(), *subst(), *trmdir();
struct symtab *stsym();
	int	fin	= 0;
	struct _iobuf	*fout	= (&_iob[1]);
	int	nd	= 1;
	int	pflag;	
int	passcom;	
	int rflag;	
	int mflag;	
	char *infile;	
 	struct _iobuf *mout;	



	int	ifno;

	char *prespc[20];
	char **predef = prespc;
	char *punspc[20];
	char **prund = punspc;
	int	exfail;
struct symtab {
	char	*name;
	char	*value;
} *lastsym, *lookup(), *slookup();

# 186 "cpp.c"



	struct symtab stab[2000		];

	struct symtab *defloc;
	struct symtab *udfloc;
	struct symtab *incloc;
	struct symtab *ifloc;
	struct symtab *elsloc;
	struct symtab *eifloc;
	struct symtab *ifdloc;
	struct symtab *ifnloc;
	struct symtab *ysysloc;
	struct symtab *varloc;
	struct symtab *lneloc;
	struct symtab *ulnloc;
	struct symtab *uflloc;
	int	trulvl;
	int	flslvl;

sayline(where)
	int where;
{
	if (mflag && where==1) fprintf(mout, "%s: %s\n", infile, fnames[ifno]);
	if (pflag==0) fprintf(fout,"# %d \"%s\"\n", lineno[ifno], fnames[ifno]);
}






















































dump() {





	register char *p1,*p2; register struct _iobuf *f;
	if ((p1=outp)==inp || flslvl!=0) return;
# 292 "cpp.c"

	f=fout;
# 304 "cpp.c"

	while (p1<inp) 	(--(f)->_cnt >= 0 ?	(int)(*(f)->_ptr++ = (unsigned char)(*p1++)) :	(((f)->_flag & 0200) && -(f)->_cnt < (f)->_bufsiz ?		((*(f)->_ptr = (unsigned char)(*p1++)) != '\n' ?			(int)(*(f)->_ptr++) :			_flsbuf(*(unsigned char *)(f)->_ptr, f)) :		_flsbuf((unsigned char)(*p1++), f)));
	outp=p1;
}

char *
refill(p) register char *p; {



	register char *np,*op; register int ninbuf;
	dump(); np=pbuf-(p-inp); op=inp;
	if ( (pbeg>=(np+1))) {pperror("token too long"); np=pbeg; p=inp+1024;}
	macdam += np-inp; outp=inp=np;
	while (op<p) *np++= *op++;
	p=np;
	for (;;) {
		if (mactop>inctop[ifno]) {
			op=instack[--mactop]; np=pbuf;
			do {while (*np++= *op++);} while (op<endbuf[mactop]); pend=np-1;
			
			if (fretop<14	) bufstack[fretop++]=instack[mactop];
			return(p);
		} else {
			maclvl=0;
			if (0<(ninbuf=read(fin,pbuf,1024))) {
				pend=pbuf+ninbuf; *pend='\0';
				return(p);
			}
			
			if (ifno==0) {
				if (plvl!=0) {
					int n=plvl,tlin=lineno[ifno]; char *tfil=fnames[ifno];
					lineno[ifno]=maclin; fnames[ifno]=macfil;
					pperror("%s: unterminated macro call",macnam);
					lineno[ifno]=tlin; fnames[ifno]=tfil;
					np=p; *np++='\n';	
					while (--n>=0) *np++=')';	
					pend=np; *np='\0'; if (plvl<0) plvl=0;
					return(p);
				}
				if (trulvl || flslvl)
					pperror("missing endif");
				inp=p; dump(); exit(exfail);
			}
			close(fin); fin=fins[--ifno]; dirs[0]=dirnams[ifno]; sayline( 3);
		}
	}
}




char *
cotoken(p) register char *p; {
	register int c,i; char quoc;
	static int state = 0;

	if (state!=0) goto prevlf;
for (;;) {
again:
	while (! (ptrtab[*p++]&2));
	switch (*(inp=p-1)) {
	case 0: {
		if ( ((--p)>=pend)) {p=refill(p); goto again;}
		else ++p; 
	} break;
	case '|': case '&': for (;;) {
		if (*p++== *inp) break;
		if ( ((--p)>=pend)) p=refill(p);
		else break;
	} break;
	case '=': case '!': for (;;) {
		if (*p++=='=') break;
		if ( ((--p)>=pend)) p=refill(p);
		else break;
	} break;
	case '<': case '>': for (;;) {
		if (*p++=='=' || p[-2]==p[-1]) break;
		if ( ((--p)>=pend)) p=refill(p);
		else break;
	} break;
	case '\\': for (;;) {
		if (*p++=='\n') {++lineno[ifno]; break;}
		if ( ((--p)>=pend)) p=refill(p);
		else {++p; break;}
	} break;
	case '/': for (;;) {
		if (*p++=='*') {
			if (!passcom) {inp=p-2; dump(); ++flslvl;}
			for (;;) {
				while (! ((fastab+128)[*p++]&8));
				if (p[-1]=='*') for (;;) {
					if (*p++=='/') goto endcom;
					if ( ((--p)>=pend)) {
						if (!passcom) {inp=p; p=refill(p);}
						else if ((p-inp)>=1024) {
							inp=p; p=refill(p);	
								if(!flslvl) 	(--(fout)->_cnt >= 0 ?	(int)(*(fout)->_ptr++ = (unsigned char)('/')) :	(((fout)->_flag & 0200) && -(fout)->_cnt < (fout)->_bufsiz ?		((*(fout)->_ptr = (unsigned char)('/')) != '\n' ?			(int)(*(fout)->_ptr++) :			_flsbuf(*(unsigned char *)(fout)->_ptr, fout)) :		_flsbuf((unsigned char)('/'), fout)));	
							
							outp=inp=p-=3; *p++='/'; *p++='*'; *p++='*';
						} else p=refill(p);
					} else break;
				} else if (p[-1]=='\n') {
					++lineno[ifno]; if (!passcom) 	(--(fout)->_cnt >= 0 ?	(int)(*(fout)->_ptr++ = (unsigned char)('\n')) :	(((fout)->_flag & 0200) && -(fout)->_cnt < (fout)->_bufsiz ?		((*(fout)->_ptr = (unsigned char)('\n')) != '\n' ?			(int)(*(fout)->_ptr++) :			_flsbuf(*(unsigned char *)(fout)->_ptr, fout)) :		_flsbuf((unsigned char)('\n'), fout)));
				} else if ( ((--p)>=pend)) {
					if (!passcom) {inp=p; p=refill(p);}
					else if ((p-inp)>=1024) {
						inp=p; p=refill(p);
							if(!flslvl) 	(--(fout)->_cnt >= 0 ?	(int)(*(fout)->_ptr++ = (unsigned char)('*')) :	(((fout)->_flag & 0200) && -(fout)->_cnt < (fout)->_bufsiz ?		((*(fout)->_ptr = (unsigned char)('*')) != '\n' ?			(int)(*(fout)->_ptr++) :			_flsbuf(*(unsigned char *)(fout)->_ptr, fout)) :		_flsbuf((unsigned char)('*'), fout))); 	if(!flslvl) 	(--(fout)->_cnt >= 0 ?	(int)(*(fout)->_ptr++ = (unsigned char)('/')) :	(((fout)->_flag & 0200) && -(fout)->_cnt < (fout)->_bufsiz ?		((*(fout)->_ptr = (unsigned char)('/')) != '\n' ?			(int)(*(fout)->_ptr++) :			_flsbuf(*(unsigned char *)(fout)->_ptr, fout)) :		_flsbuf((unsigned char)('/'), fout)));
						outp=inp=p-=2; *p++='/'; *p++='*';
					} else p=refill(p);
				} else ++p; 
			}
		endcom:
			if (!passcom) {outp=inp=p; --flslvl; goto again;}
			break;
		}
		if ( ((--p)>=pend)) p=refill(p);
		else break;
	} break;
# 427 "cpp.c"

	case '"': case '\'': {
		quoc=p[-1];
		for (;;) {
			while (! ((fastab+128)[*p++]&16));
			if (p[-1]==quoc) break;
			if (p[-1]=='\n') {--p; break;} 
			if (p[-1]=='\\') for (;;) {
				if (*p++=='\n') {++lineno[ifno]; break;} 
				if ( ((--p)>=pend)) p=refill(p);
				else {++p; break;}
			} else if ( ((--p)>=pend)) p=refill(p);
			else ++p;	
		}
	} break;
	case '\n': {
		++lineno[ifno]; if ((ptrtab==(slotab+128))) {state=1; return(p);}
prevlf:
		state=0;
		for (;;) {
			if (*p++=='#') return(p);
			if ( ((inp= --p)>=pend)) p=refill(p);
			else goto again;
		}
	} break;
	case '0': case '1': case '2': case '3': case '4':
	case '5': case '6': case '7': case '8': case '9':
	for (;;) {
		while ( ((fastab+128)[*p++]&4));
		if ( ((--p)>=pend)) p=refill(p);
		else break;
	} break;
	case 'A': case 'B': case 'C': case 'D': case 'E':
	case 'F': case 'G': case 'H': case 'I': case 'J':
	case 'K': case 'L': case 'M': case 'N': case 'O':
	case 'P': case 'Q': case 'R': case 'S': case 'T':
	case 'U': case 'V': case 'W': case 'X': case 'Y':
	case 'Z': case '_':
	case 'a': case 'b': case 'c': case 'd': case 'e':
	case 'f': case 'g': case 'h': case 'i': case 'j':
	case 'k': case 'l': case 'm': case 'n': case 'o':
	case 'p': case 'q': case 'r': case 's': case 't':
	case 'u': case 'v': case 'w': case 'x': case 'y':
	case 'z':



# 477 "cpp.c"


# 483 "cpp.c"





	if (flslvl) goto nomac;
	for (;;) {
		c= p[-1];                           if (! ((macbit+128)[c] & (1))) goto nomac;
		i= *p++; if (!  ((fastab+128)[i]&1)) goto endid;  if (! ((macbit+128)[i] & (2))) goto nomac; ;
		c= *p++; if (!  ((fastab+128)[c]&1)) goto endid;  if (! ((macbit+128)[c] & (4))) goto nomac; ;
		i= *p++; if (!  ((fastab+128)[i]&1)) goto endid;  if (! ((macbit+128)[i] & (8))) goto nomac; ;
		c= *p++; if (!  ((fastab+128)[c]&1)) goto endid;  if (! ((macbit+128)[c] & (16))) goto nomac; ;
		i= *p++; if (!  ((fastab+128)[i]&1)) goto endid;  if (! ((macbit+128)[i] & (32))) goto nomac; ;
		c= *p++; if (!  ((fastab+128)[c]&1)) goto endid;  if (! ((macbit+128)[c] & (64))) goto nomac; ;
		i= *p++; if (!  ((fastab+128)[i]&1)) goto endid;  if (! ((macbit+128)[i] & (128))) goto nomac; ;
		                                                ;
		while (  ((fastab+128)[*p++]&1));
		if ( ((--p)>=pend)) {refill(p); p=inp+1; continue;}
		goto lokid;
	endid:
		if ( ((--p)>=pend)) {refill(p); p=inp+1; continue;}
		;
	lokid:
		slookup(inp,p,0); if (newp) {p=newp; goto again;}
		else break;
	nomac:
		while (  ((fastab+128)[*p++]&1));
		if ( ((--p)>=pend)) {p=refill(p); goto nomac;}
		else break;
	} break;
	} 
	
	if ((ptrtab==(slotab+128))) return(p);
} 
}

char *
skipbl(p) register char *p; {
	do {outp=inp=p; p=cotoken(p);} while ((toktyp+128)[*inp]==1);
	return(p);
}

char *
unfill(p) register char *p; {



	register char *np,*op; register int d;
	if (mactop>=14	) {
		pperror("%s: too much pushback",macnam);
		p=inp=pend; dump();	
		while (mactop>inctop[ifno]) {p=refill(p); p=inp=pend; dump();}
	}
	if (fretop>0) np=bufstack[--fretop];
	else {
		np=savch; savch+=1024;
		if (savch>=sbf+60000		) {pperror("no space"); exit(exfail);}
		*savch++='\0';
	}
	instack[mactop]=np; op=pend-1024; if (op<p) op=p;
	for (;;) {while (*np++= *op++); if ( ((op)>=pend)) break;} 
	endbuf[mactop++]=np;	
	np=pbuf+1024; op=pend-1024; pend=np; if (op<p) op=p;
	while (outp<op) *--np= *--op; 
	if ( (pbeg>=(np))) pperror("token too long");
	d=np-outp; outp+=d; inp+=d; macdam+=d; return(p+d);
}

char *
doincl(p) register char *p; {
	int filok,inctype;
	register char *cp; char **dirp,*nfil; char filname[1024];

	p=skipbl(p); cp=filname;
	if (*inp++=='<') {
		inctype=1;
		++flslvl;	
		for (;;) {
			outp=inp=p; p=cotoken(p);
			if (*inp=='\n') {--p; *cp='\0'; break;}
			if (*inp=='>') {      *cp='\0'; break;}
# 566 "cpp.c"

			while (inp<p) *cp++= *inp++;
		}
		--flslvl;	
	} else if (inp[-1]=='"') {
		inctype=0;
# 574 "cpp.c"

		while (inp<p) *cp++= *inp++;

		if (*--cp=='"') *cp='\0';
	} else {pperror("bad include syntax",0); inctype=2;}
	
	++flslvl; do {outp=inp=p; p=cotoken(p);} while (*inp!='\n'); --flslvl;
	inp=p; dump(); if (inctype==2) return(p);
	
	if (ifno+1 >=10) {
		pperror("Unreasonable include nesting",0); return(p);
	}
	if((nfil=savch)>sbf+60000		-1024) {pperror("no space"); exit(exfail);}
	filok=0;
	for (dirp=dirs+inctype; *dirp; ++dirp) {
		if (
# 592 "cpp.c"

			filname[0]=='/' 

				|| **dirp=='\0') strcpy(nfil,filname);
		else {
			strcpy(nfil,*dirp);

			strcat(nfil,"/");

# 605 "cpp.c"

			strcat(nfil,filname);
		}
		if (0<(fins[ifno+1]=open(nfil,0))) {
			filok=1; fin=fins[++ifno]; break;
		}
	}
	if (filok==0) pperror("Can't find include file %s",filname);
	else {
		lineno[ifno]=1; fnames[ifno]=cp=nfil; while (*cp++); savch=cp;
		dirnams[ifno]=dirs[0]=trmdir(copy(nfil));
		sayline(1);
		
		while (! ((p)>=pend)) p=unfill(p);
		inctop[ifno]=mactop;
	}
	return(p);
}

equfrm(a,p1,p2) register char *a,*p1,*p2; {
	register char c; int flag;
	c= *p2; *p2='\0';
	flag=strcmp(a,p1); *p2=c; return(flag==0);
}

char *
dodef(p) char *p; {
	register char *pin,*psav,*cf;
	char **pf,**qf; int b,c,params; struct symtab *np;
	char *oldval,*oldsavch;
	char *formal[31	]; 
	char formtxt[1024]; 

	if (savch>sbf+60000		-1024) {pperror("too much defining"); return(p);}
	oldsavch=savch; 
	++flslvl; 
	p=skipbl(p); pin=inp;
	if ((toktyp+128)[*pin]!=2) {
		ppwarn("illegal macro name"); while (*inp!='\n') p=skipbl(p); return(p);
	}
	np=slookup(pin,p,1);
	if (oldval=np->value) savch=oldsavch;	
	b=1; cf=pin;
	while (cf<p) {
		c= *cf++;  ((macbit+128)[c] |= (b)); b=(b+b)&0xFF;
		if (cf!=p) ;
		else ;
	}
	params=0; outp=inp=p; p=cotoken(p); pin=inp;
	if (*pin=='(') {
		cf=formtxt; pf=formal;
		for (;;) {
			p=skipbl(p); pin=inp;
			if (*pin=='\n') {
				--lineno[ifno]; --p; pperror("%s: missing )",np->name); break;
			}
			if (*pin==')') break;
			if (*pin==',') continue;
			if ((toktyp+128)[*pin]!=2) {
				c= *p; *p='\0'; pperror("bad formal: %s",pin); *p=c;
			} else if (pf>= &formal[31	]) {
				c= *p; *p='\0'; pperror("too many formals: %s",pin); *p=c;
			} else {
				*pf++=cf; while (pin<p) *cf++= *pin++; *cf++='\0'; ++params;
			}
		}
		if (params==0) --params; 
	} else if (*pin=='\n') {--lineno[ifno]; --p;}
	


	oldsavch=psav=savch;
	for (;;) {
		outp=inp=p; p=cotoken(p); pin=inp;
		if (*pin=='\\' && pin[1]=='\n') {	(--(fout)->_cnt >= 0 ?	(int)(*(fout)->_ptr++ = (unsigned char)('\n')) :	(((fout)->_flag & 0200) && -(fout)->_cnt < (fout)->_bufsiz ?		((*(fout)->_ptr = (unsigned char)('\n')) != '\n' ?			(int)(*(fout)->_ptr++) :			_flsbuf(*(unsigned char *)(fout)->_ptr, fout)) :		_flsbuf((unsigned char)('\n'), fout))); continue;}	
		if (*pin=='\n') break;
		if (params) {
			if ((toktyp+128)[*pin]==2) {
				for (qf=pf; --qf>=formal; ) {
					if (equfrm(*qf,pin,p)) {
						*psav++=qf-formal+1; *psav++=0xFE	; pin=p; break;
					}
				}
			} else if (*pin=='"' || *pin=='\''
# 691 "cpp.c"

						) {
				char quoc= *pin;
				for (*psav++= *pin++; pin<p && *pin!=quoc; ) {
					while (pin<p && !  ((fastab+128)[*pin]&1)) *psav++= *pin++;
					cf=pin; while (cf<p &&   ((fastab+128)[*cf]&1)) ++cf;
					for (qf=pf; --qf>=formal; ) {
						if (equfrm(*qf,pin,cf)) {
							*psav++=qf-formal+1; *psav++=0xFE	; pin=cf; break;
						}
					}
					while (pin<cf) *psav++= *pin++;
				}
			}
		}
		while (pin<p) *psav++= *pin++;
	}
	*psav++=params; *psav++='\0';
	if ((cf=oldval)!=0) {
		--cf;	
		while (*--cf);	
		if (0!=strcmp(++cf,oldsavch)) {
			--lineno[ifno]; ppwarn("%s redefined",np->name); ++lineno[ifno];
			np->value=psav-1;
		} else psav=oldsavch; 
	} else np->value=psav-1;
	--flslvl; inp=pin; savch=psav; return(p);
}




char *
control(p) register char *p; {
	register struct symtab *np;
for (;;) {
	 ptrtab=fastab+128; p=cotoken(p); if (*inp=='\n') ++inp; dump();
	 ptrtab=slotab+128; p=skipbl(p);
	*--inp='#'; outp=inp; ++flslvl; np=slookup(inp,p,0); --flslvl;
	if (np==defloc) {
		if (flslvl==0) {p=dodef(p); continue;}
	} else if (np==incloc) {
		if (flslvl==0) {p=doincl(p); continue;}
	} else if (np==ifnloc) {
		++flslvl; p=skipbl(p); np=slookup(inp,p,0); --flslvl;
		if (flslvl==0 && np->value==0) ++trulvl;
		else ++flslvl;
	} else if (np==ifdloc) {
		++flslvl; p=skipbl(p); np=slookup(inp,p,0); --flslvl;
		if (flslvl==0 && np->value!=0) ++trulvl;
		else ++flslvl;
	} else if (np==eifloc) {
		if (flslvl) {if (--flslvl==0) sayline( 2);}
		else if (trulvl) --trulvl;
		else pperror("If-less endif",0);
	} else if (np==elsloc) {
		if (flslvl) {
			if (--flslvl!=0) ++flslvl;
			else {++trulvl; sayline( 2);}
		}
		else if (trulvl) {++flslvl; --trulvl;}
		else pperror("If-less else",0);
	} else if (np==udfloc) {
		if (flslvl==0) {
			++flslvl; p=skipbl(p); slookup(inp,p,0xFE	); --flslvl;
		}
	} else if (np==ifloc) {
# 761 "cpp.c"

		newp=p;
		if (flslvl==0 && yyparse()) ++trulvl; else ++flslvl;
		p=newp;

	} else if (np==lneloc) {
		if (flslvl==0 && pflag==0) {
			char *cp, *cp2, *savestring();
			outp=inp=p; *--outp='#'; while (*inp!='\n') p=cotoken(p);
			cp = outp + 1;
			while (	((_ctype_+1)[*cp]&010) && cp < inp)
				cp++;
			while (	((_ctype_+1)[*cp]&04) && cp < inp)
				cp++;
			while (*cp != '"' && cp < inp)
				cp++;
			if (cp < inp) {
				cp++;
				cp2 = cp;
				while (*cp2 != '"' && cp2 < inp)
					cp2++;
				fnames[ifno] = savestring(cp, cp2);
			}
			continue;
		}
	} else if (*++inp=='\n') outp=inp;	
	else {
		pperror("undefined control", 0);
	}
	
	++flslvl; while (*inp!='\n') {outp=inp=p; p=cotoken(p);} --flslvl;
}
}

char *
savestring(start, finish)
	register char *start, *finish;
{
	char *retbuf;
	register char *cp;

	retbuf = (char *) calloc(finish - start + 1, sizeof (char));
	cp = retbuf;
	while (start < finish)
		*cp++ = *start++;
	*cp = 0;
	return(retbuf);
}

struct symtab *
stsym(s) register char *s; {
	char buf[1024]; register char *p;

	
	
	p=buf; while (*p++= *s++);
	p=buf; while (  ((fastab+128)[*p++]&1)); 
	if (*--p=='=') {*p++=' '; while (*p++);}
	else {s=" 1"; while (*p++= *s++);}
	pend=p; *--p='\n';
	 ptrtab=slotab+128; dodef(buf); return(lastsym);
}

struct symtab *
ppsym(s) char *s; {
	register struct symtab *sp;
	cinit='#'; *savch++='#'; sp=stsym(s); --sp->name; cinit=0; return(sp);
}


pperror(s,x,y) char *s; {
	if (fnames[ifno][0]) fprintf((&_iob[2]),
# 835 "cpp.c"

			"%s: ",

				 fnames[ifno]);
	fprintf((&_iob[2]), "%d: ",lineno[ifno]);
	fprintf((&_iob[2]), s, x, y);
	fprintf((&_iob[2]),"\n");
	++exfail;
}

yyerror(s,a,b) char *s; {
	pperror(s,a,b);
}

ppwarn(s,x) char *s; {
	int fail = exfail;
	exfail = -1;
	pperror(s,x);
	exfail = fail;
}

struct symtab *
lookup(namep, enterf)
char *namep;
{
	register char *np, *snp;
	register int c, i; int around;
	register struct symtab *sp;

	
	np=namep; around=0; i=cinit;
	while (c= *np++) i += i+c; c=i;	
	c %= 2000		; if (c<0) c += 2000		;
	sp = &stab[c];
	while (snp=sp->name) {
		np = namep;
		while (*snp++ == *np) if (*np++ == '\0') {
				if (enterf==0xFE	) {sp->name[0]= 0xFE	; sp->value=0;}
				return(lastsym=sp);
			}
		if (--sp < &stab[0])
			if (around) {pperror("too many defines", 0); exit(exfail);}
			else {++around; sp = &stab[2000		-1];}
	}
	if (enterf==1) sp->name=namep;
	return(lastsym=sp);
}

struct symtab *
slookup(p1,p2,enterf) register char *p1,*p2; int enterf;{
	register char *p3; char c2,c3; struct symtab *np;
	         c2= *p2; *p2='\0';	
	if ((p2-p1)>8) p3=p1+8; else p3=p2;
			 c3= *p3; *p3='\0';	
	if (enterf==1) p1=copy(p1);
	np=lookup(p1,enterf); *p3=c3; *p2=c2;
	if (np->value!=0 && flslvl==0) newp=subst(p2,np);
	else newp=0;
	return(np);
}

char *
subst(p,sp) register char *p; struct symtab *sp; {
	static char match[]="%s: argument mismatch";
	register char *ca,*vp; int params;
	char *actual[31	]; 
	char actused[31	]; 
	char acttxt[1024];  
	int  nlines = 0;

	if (0==(vp=sp->value)) return(p);
	if ((p-macforw)<=macdam) {
		if (++maclvl>2000		 && !rflag) {
			pperror("%s: macro recursion",sp->name); return(p);
		}
	} else maclvl=0;	
	macforw=p; macdam=0;	
	macnam=sp->name;
	dump();
	if (sp==ulnloc) {
		vp=acttxt; *vp++='\0';
		sprintf(vp,"%d",lineno[ifno]); while (*vp++);
	} else if (sp==uflloc) {
		vp=acttxt; *vp++='\0';
		sprintf(vp,"\"%s\"",fnames[ifno]); while (*vp++);
	}
	if (0!=(params= *--vp&0xFF)) {
		register char **pa;
		ca=acttxt; pa=actual;
		if (params==0xFF) params=1;	
		 ptrtab=slotab+128; ++flslvl; 
		plvl= -1;
		do p=skipbl(p); while (*inp=='\n');	
		if (*inp=='(') {
			maclin=lineno[ifno]; macfil=fnames[ifno];
			for (plvl=1; plvl!=0; ) {
				*ca++='\0';
				for (;;) {
					outp=inp=p; p=cotoken(p);
					if (*inp=='(') ++plvl;
					if (*inp==')' && --plvl==0) {--params; break;}
					if (plvl==1 && *inp==',') {--params; break;}
					while (inp<p) *ca++= *inp++;
					if (ca> &acttxt[1024])
						pperror("%s: actuals too long",sp->name);
				}
				if (pa>= &actual[31	]) ppwarn(match,sp->name);
				else { actused[pa-actual]=0; *pa++=ca; }
			}
			nlines = lineno[ifno] - maclin;
			lineno[ifno] = maclin; 
		}
		if (params!=0) ppwarn(match,sp->name);
		while (--params>=0) *pa++=""+1;	
		--flslvl;  ptrtab=fastab+128;
	}
	for (;;) {
		while (! ((fastab+128)[*--vp]&32)) {
			if ( (pbeg>=(p))) {outp=inp=p; p=unfill(p);}
			*--p= *vp;
		}
		if (*vp==warnc) {
			ca=actual[*--vp-1];
			while (*--ca) {
				if ( (pbeg>=(p))) {outp=inp=p; p=unfill(p);}
				
				if (*ca == '\n' && actused[*vp-1])
					if (*(ca-1) == '\\') ca--;
					else *--p = ' ';
				else { *--p= *ca; if (*ca == '\n') nlines--; }
			}
			actused[*vp-1] = 1;
		} else {
			if (nlines > 0 )
				while (nlines-- > 0)
					*--p = '\n';
			break;
		}
	}
	outp=inp=p;
	return(p);
}




char *
trmdir(s) register char *s; {
	register char *p = s;
	while (*p++); --p; while (p>s && *--p!='/');

	if (p==s) *p++='.';

	*p='\0';
	return(s);
}

 char *
copy(s) register char *s; {
	register char *old;

	old = savch; while (*savch++ = *s++);
	return(old);
}

char *
strdex(s,c) char *s,c; {
	while (*s) if (*s++==c) return(--s);
	return(0);
}

yywrap(){ return(1); }

main(argc,argv)
	char *argv[];
{
	register int i,c;
	register char *p;
	char *tf,**cp2;

# 1017 "cpp.c"

	p="_$ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
		i=0;
		while (c= *p++) {
			(fastab+128)[c] |= 1|4|2; (toktyp+128)[c]=2;



# 1028 "cpp.c"

		}
	p="0123456789.";
		while (c= *p++) {(fastab+128)[c] |= 4|2; (toktyp+128)[c]=3;}
# 1034 "cpp.c"

	p="\n\"'/\\";

		while (c= *p++) (fastab+128)[c] |= 2;
# 1040 "cpp.c"

	p="\n\"'\\";

		while (c= *p++) (fastab+128)[c] |= 16;
	p="*\n"; while (c= *p++) (fastab+128)[c] |= 8;
	(fastab+128)[warnc] |= 32;
	(fastab+128)['\0'] |= 8|16|2|32;
	for (i=256	; --i>=0; ) slotab[i]=fastab[i]|2;
	p=" \t\013\f\r";	
		while (c= *p++) (toktyp+128)[c]=1;
# 1053 "cpp.c"



	fnames[ifno=0] = ""; dirnams[0]=dirs[0]=".";

# 1060 "cpp.c"

# 1063 "cpp.c"

# 1067 "cpp.c"

	for(i=1; i<argc; i++)
		{
		switch(argv[i][0])
			{
			case '-':
# 1075 "cpp.c"

			switch(argv[i][1]) {

				case 'M': mflag++;
				case 'P': pflag++;
				case 'E': continue;
				case 'R': ++rflag; continue;
				case 'C': passcom++; continue;
				case 'D':
					if (predef>prespc+20) {
						pperror("too many -D options, ignoring %s",argv[i]);
						continue;
					}
					
					if (*(argv[i]+2)) *predef++ = argv[i]+2;
					continue;
				case 'U':
					if (prund>punspc+20) {
						pperror("too many -U options, ignoring %s",argv[i]);
						continue;
					}
					*prund++ = argv[i]+2;
					continue;
				case 'I':
					if (nd>8) pperror("excessive -I file (%s) ignored",argv[i]);
					else dirs[nd++] = argv[i]+2;
					continue;
				case '\0': continue;
				default: 
					pperror("unknown flag %s", argv[i]);
					continue;
				}
			default:
				if (fin==0) {
					if (0>(fin=open(argv[i], 0))) {
						pperror("No source file %s",argv[i]); exit(8);
					}
					fnames[ifno]=copy(argv[i]);
					infile=copy(argv[i]);
					dirs[0]=dirnams[ifno]=trmdir(argv[i]);






				} else if (fout==(&_iob[1])) {
					if (0==(fout=freopen(argv[i], "w", (&_iob[1])))) {
						perror(argv[1]);
						pperror("Can't create %s", argv[i]); exit(8);
					}

				} else pperror("extraneous name %s", argv[i]);
			}
		}

	if (mflag) {
		if (infile==(char *)0) {
			fprintf((&_iob[2]),
				"no input file specified with -M flag\n");
			exit(8);
		}
		tf=(char *)rindex(infile, '.');
		if (tf==0) {
			fprintf((&_iob[2]), "missing component name on %s\n",
				infile);
			exit(8);
		}
		tf[1]='o';
		tf=(char *)rindex(infile, '/');
		if (tf!=(char *)0)
			infile = tf + 1;
		mout=fout;
		if (0==(fout=fopen("/dev/null", "w"))) {
			pperror("Can't open /dev/null");
			exit(8);
		}
	}
	fins[ifno]=fin;
	exfail = 0;
		

	dirs[nd++] = "/usr/include";

# 1161 "cpp.c"

# 1166 "cpp.c"

# 1169 "cpp.c"

	
	dirs[nd++] = 0;
	defloc=ppsym("define");
	udfloc=ppsym("undef");
	incloc=ppsym("include");
	elsloc=ppsym("else");
	eifloc=ppsym("endif");
	ifdloc=ppsym("ifdef");
	ifnloc=ppsym("ifndef");
	ifloc=ppsym("if");
	lneloc=ppsym("line");
	for (i=sizeof(macbit)/sizeof(macbit[0]); --i>=0; ) macbit[i]=0;

	ysysloc=stsym("unix");

# 1187 "cpp.c"

# 1190 "cpp.c"

# 1193 "cpp.c"

# 1196 "cpp.c"

# 1199 "cpp.c"

# 1202 "cpp.c"

# 1205 "cpp.c"

# 1208 "cpp.c"


	varloc=stsym("mc68000");

# 1214 "cpp.c"


	varloc=stsym("sun");

	ulnloc=stsym ("__LINE__");
	uflloc=stsym ("__FILE__");

	tf=fnames[ifno]; fnames[ifno]="command line"; lineno[ifno]=1;
	cp2=prespc;
	while (cp2<predef) stsym(*cp2++);
	cp2=punspc;
	while (cp2<prund) {
		if (p=strdex(*cp2, '=')) *p++='\0';
		lookup(*cp2++, 0xFE	);
	}
	fnames[ifno]=tf;
	pbeg=buffer+8; pbuf=pbeg+1024; pend=pbuf+1024;

	trulvl = 0; flslvl = 0;
	lineno[0] = 1; sayline(1);
	outp=inp=pend;
	control(pend);
	return (exfail);
}
