xref: /csrg-svn/old/cpp/cpp.c (revision 7978)
1*7978Srrh #ifndef lint
2*7978Srrh static char sccsid[] = "@(#)cpp.c 1.1 08/30/82";
3*7978Srrh #endif lint
4*7978Srrh #ifdef FLEXNAMES
5*7978Srrh #define	NCPS	128
6*7978Srrh #else
7*7978Srrh #define	NCPS	8
8*7978Srrh #endif
9*7978Srrh 
10*7978Srrh # include "stdio.h"
11*7978Srrh /* C command
12*7978Srrh /* written by John F. Reiser
13*7978Srrh /* July/August 1978
14*7978Srrh */
15*7978Srrh 
16*7978Srrh #define STATIC
17*7978Srrh 
18*7978Srrh #define STDIN 0
19*7978Srrh #define STDOUT 1
20*7978Srrh #define STDERR 2
21*7978Srrh #define READ 0
22*7978Srrh #define WRITE 1
23*7978Srrh #define SALT '#'
24*7978Srrh #ifndef BUFSIZ
25*7978Srrh #define BUFSIZ 512
26*7978Srrh #endif
27*7978Srrh 
28*7978Srrh char *pbeg,*pbuf,*pend;
29*7978Srrh char *outp,*inp;
30*7978Srrh char *newp;
31*7978Srrh char cinit;
32*7978Srrh 
33*7978Srrh /* some code depends on whether characters are sign or zero extended */
34*7978Srrh /*	#if '\377' < 0		not used here, old cpp doesn't understand */
35*7978Srrh #if pdp11 | vax
36*7978Srrh #define COFF 128
37*7978Srrh #else
38*7978Srrh #define COFF 0
39*7978Srrh #endif
40*7978Srrh 
41*7978Srrh # if gcos
42*7978Srrh #define ALFSIZ 512	/* alphabet size */
43*7978Srrh # else
44*7978Srrh #define ALFSIZ 256	/* alphabet size */
45*7978Srrh # endif
46*7978Srrh char macbit[ALFSIZ+11];
47*7978Srrh char toktyp[ALFSIZ];
48*7978Srrh #define BLANK 1
49*7978Srrh #define IDENT 2
50*7978Srrh #define NUMBR 3
51*7978Srrh 
52*7978Srrh /* a superimposed code is used to reduce the number of calls to the
53*7978Srrh /* symbol table lookup routine.  (if the kth character of an identifier
54*7978Srrh /* is 'a' and there are no macro names whose kth character is 'a'
55*7978Srrh /* then the identifier cannot be a macro name, hence there is no need
56*7978Srrh /* to look in the symbol table.)  'scw1' enables the test based on
57*7978Srrh /* single characters and their position in the identifier.  'scw2'
58*7978Srrh /* enables the test based on adjacent pairs of characters and their
59*7978Srrh /* position in the identifier.  scw1 typically costs 1 indexed fetch,
60*7978Srrh /* an AND, and a jump per character of identifier, until the identifier
61*7978Srrh /* is known as a non-macro name or until the end of the identifier.
62*7978Srrh /* scw1 is inexpensive.  scw2 typically costs 4 indexed fetches,
63*7978Srrh /* an add, an AND, and a jump per character of identifier, but it is also
64*7978Srrh /* slightly more effective at reducing symbol table searches.
65*7978Srrh /* scw2 usually costs too much because the symbol table search is
66*7978Srrh /* usually short; but if symbol table search should become expensive,
67*7978Srrh /* the code is here.
68*7978Srrh /* using both scw1 and scw2 is of dubious value.
69*7978Srrh */
70*7978Srrh #define scw1 1
71*7978Srrh #define scw2 0
72*7978Srrh 
73*7978Srrh #if scw2
74*7978Srrh char t21[ALFSIZ],t22[ALFSIZ],t23[ALFSIZ+NCPS];
75*7978Srrh #endif
76*7978Srrh 
77*7978Srrh #if scw1
78*7978Srrh #define b0 1
79*7978Srrh #define b1 2
80*7978Srrh #define b2 4
81*7978Srrh #define b3 8
82*7978Srrh #define b4 16
83*7978Srrh #define b5 32
84*7978Srrh #define b6 64
85*7978Srrh #define b7 128
86*7978Srrh #endif
87*7978Srrh 
88*7978Srrh #define IB 1
89*7978Srrh #define SB 2
90*7978Srrh #define NB 4
91*7978Srrh #define CB 8
92*7978Srrh #define QB 16
93*7978Srrh #define WB 32
94*7978Srrh char fastab[ALFSIZ];
95*7978Srrh char slotab[ALFSIZ];
96*7978Srrh char *ptrtab;
97*7978Srrh #define isslo (ptrtab==(slotab+COFF))
98*7978Srrh #define isid(a)  ((fastab+COFF)[a]&IB)
99*7978Srrh #define isspc(a) (ptrtab[a]&SB)
100*7978Srrh #define isnum(a) ((fastab+COFF)[a]&NB)
101*7978Srrh #define iscom(a) ((fastab+COFF)[a]&CB)
102*7978Srrh #define isquo(a) ((fastab+COFF)[a]&QB)
103*7978Srrh #define iswarn(a) ((fastab+COFF)[a]&WB)
104*7978Srrh 
105*7978Srrh #define eob(a) ((a)>=pend)
106*7978Srrh #define bob(a) (pbeg>=(a))
107*7978Srrh 
108*7978Srrh char buffer[NCPS+BUFSIZ+BUFSIZ+NCPS];
109*7978Srrh 
110*7978Srrh # define SBSIZE 48000		/* std = 12000, wnj aug 1979 */
111*7978Srrh char	sbf[SBSIZE];
112*7978Srrh char	*savch	= sbf;
113*7978Srrh 
114*7978Srrh # define DROP 0xFE	/* special character not legal ASCII or EBCDIC */
115*7978Srrh # define WARN DROP
116*7978Srrh # define SAME 0
117*7978Srrh # define MAXINC 10
118*7978Srrh # define MAXFRE 14	/* max buffers of macro pushback */
119*7978Srrh # define MAXFRM 31	/* max number of formals/actuals to a macro */
120*7978Srrh 
121*7978Srrh static char warnc = WARN;
122*7978Srrh 
123*7978Srrh int mactop,fretop;
124*7978Srrh char *instack[MAXFRE],*bufstack[MAXFRE],*endbuf[MAXFRE];
125*7978Srrh 
126*7978Srrh int plvl;	/* parenthesis level during scan for macro actuals */
127*7978Srrh int maclin;	/* line number of macro call requiring actuals */
128*7978Srrh char *macfil;	/* file name of macro call requiring actuals */
129*7978Srrh char *macnam;	/* name of macro requiring actuals */
130*7978Srrh int maclvl;	/* # calls since last decrease in nesting level */
131*7978Srrh char *macforw;	/* pointer which must be exceeded to decrease nesting level */
132*7978Srrh int macdam;	/* offset to macforw due to buffer shifting */
133*7978Srrh 
134*7978Srrh #if tgp
135*7978Srrh int tgpscan;	/* flag for dump(); */
136*7978Srrh #endif
137*7978Srrh 
138*7978Srrh STATIC	int	inctop[MAXINC];
139*7978Srrh STATIC	char	*fnames[MAXINC];
140*7978Srrh STATIC	char	*dirnams[MAXINC];	/* actual directory of #include files */
141*7978Srrh STATIC	int	fins[MAXINC];
142*7978Srrh STATIC	int	lineno[MAXINC];
143*7978Srrh 
144*7978Srrh STATIC	char	*dirs[10];	/* -I and <> directories */
145*7978Srrh char *strdex(), *copy(), *subst(), *trmdir();
146*7978Srrh struct symtab *stsym();
147*7978Srrh STATIC	int	fin	= STDIN;
148*7978Srrh STATIC	FILE	*fout	= stdout;
149*7978Srrh STATIC	int	nd	= 1;
150*7978Srrh STATIC	int	pflag;	/* don't put out lines "# 12 foo.c" */
151*7978Srrh STATIC	int	passcom;	/* don't delete comments */
152*7978Srrh STATIC	int rflag;	/* allow macro recursion */
153*7978Srrh STATIC	int	ifno;
154*7978Srrh # define NPREDEF 20
155*7978Srrh STATIC	char *prespc[NPREDEF];
156*7978Srrh STATIC	char **predef = prespc;
157*7978Srrh STATIC	char *punspc[NPREDEF];
158*7978Srrh STATIC	char **prund = punspc;
159*7978Srrh STATIC	int	exfail;
160*7978Srrh struct symtab {
161*7978Srrh 	char	*name;
162*7978Srrh 	char	*value;
163*7978Srrh } *lastsym, *lookup(), *slookup();
164*7978Srrh 
165*7978Srrh # if gcos
166*7978Srrh #include <setjmp.h>
167*7978Srrh static jmp_buf env;
168*7978Srrh # define main	mainpp
169*7978Srrh # undef exit
170*7978Srrh # define exit(S)	longjmp(env, 1)
171*7978Srrh # define open(S,D)	fileno(fopen(S, "r"))
172*7978Srrh # define close(F)	fclose(_f[F])
173*7978Srrh extern FILE *_f[];
174*7978Srrh # define symsiz 500
175*7978Srrh # else
176*7978Srrh # define symsiz 1000		/* std = 500, wnj aug 1979 */
177*7978Srrh # endif
178*7978Srrh STATIC	struct symtab stab[symsiz];
179*7978Srrh 
180*7978Srrh STATIC	struct symtab *defloc;
181*7978Srrh STATIC	struct symtab *udfloc;
182*7978Srrh STATIC	struct symtab *incloc;
183*7978Srrh STATIC	struct symtab *ifloc;
184*7978Srrh STATIC	struct symtab *elsloc;
185*7978Srrh STATIC	struct symtab *eifloc;
186*7978Srrh STATIC	struct symtab *ifdloc;
187*7978Srrh STATIC	struct symtab *ifnloc;
188*7978Srrh STATIC	struct symtab *ysysloc;
189*7978Srrh STATIC	struct symtab *varloc;
190*7978Srrh STATIC	struct symtab *lneloc;
191*7978Srrh STATIC	struct symtab *ulnloc;
192*7978Srrh STATIC	struct symtab *uflloc;
193*7978Srrh STATIC	int	trulvl;
194*7978Srrh STATIC	int	flslvl;
195*7978Srrh 
196*7978Srrh sayline() {
197*7978Srrh 	if (pflag==0) fprintf(fout,"# %d \"%s\"\n", lineno[ifno], fnames[ifno]);
198*7978Srrh }
199*7978Srrh 
200*7978Srrh /* data structure guide
201*7978Srrh /*
202*7978Srrh /* most of the scanning takes place in the buffer:
203*7978Srrh /*
204*7978Srrh /*  (low address)                                             (high address)
205*7978Srrh /*  pbeg                           pbuf                                 pend
206*7978Srrh /*  |      <-- BUFSIZ chars -->      |         <-- BUFSIZ chars -->        |
207*7978Srrh /*  _______________________________________________________________________
208*7978Srrh /* |_______________________________________________________________________|
209*7978Srrh /*          |               |               |
210*7978Srrh /*          |<-- waiting -->|               |<-- waiting -->
211*7978Srrh /*          |    to be      |<-- current -->|    to be
212*7978Srrh /*          |    written    |    token      |    scanned
213*7978Srrh /*          |               |               |
214*7978Srrh /*          outp            inp             p
215*7978Srrh /*
216*7978Srrh /*  *outp   first char not yet written to output file
217*7978Srrh /*  *inp    first char of current token
218*7978Srrh /*  *p      first char not yet scanned
219*7978Srrh /*
220*7978Srrh /* macro expansion: write from *outp to *inp (chars waiting to be written),
221*7978Srrh /* ignore from *inp to *p (chars of the macro call), place generated
222*7978Srrh /* characters in front of *p (in reverse order), update pointers,
223*7978Srrh /* resume scanning.
224*7978Srrh /*
225*7978Srrh /* symbol table pointers point to just beyond the end of macro definitions;
226*7978Srrh /* the first preceding character is the number of formal parameters.
227*7978Srrh /* the appearance of a formal in the body of a definition is marked by
228*7978Srrh /* 2 chars: the char WARN, and a char containing the parameter number.
229*7978Srrh /* the first char of a definition is preceded by a zero character.
230*7978Srrh /*
231*7978Srrh /* when macro expansion attempts to back up over the beginning of the
232*7978Srrh /* buffer, some characters preceding *pend are saved in a side buffer,
233*7978Srrh /* the address of the side buffer is put on 'instack', and the rest
234*7978Srrh /* of the main buffer is moved to the right.  the end of the saved buffer
235*7978Srrh /* is kept in 'endbuf' since there may be nulls in the saved buffer.
236*7978Srrh /*
237*7978Srrh /* similar action is taken when an 'include' statement is processed,
238*7978Srrh /* except that the main buffer must be completely emptied.  the array
239*7978Srrh /* element 'inctop[ifno]' records the last side buffer saved when
240*7978Srrh /* file 'ifno' was included.  these buffers remain dormant while
241*7978Srrh /* the file is being read, and are reactivated at end-of-file.
242*7978Srrh /*
243*7978Srrh /* instack[0 : mactop] holds the addresses of all pending side buffers.
244*7978Srrh /* instack[inctop[ifno]+1 : mactop-1] holds the addresses of the side
245*7978Srrh /* buffers which are "live"; the side buffers instack[0 : inctop[ifno]]
246*7978Srrh /* are dormant, waiting for end-of-file on the current file.
247*7978Srrh /*
248*7978Srrh /* space for side buffers is obtained from 'savch' and is never returned.
249*7978Srrh /* bufstack[0:fretop-1] holds addresses of side buffers which
250*7978Srrh /* are available for use.
251*7978Srrh */
252*7978Srrh 
253*7978Srrh dump() {
254*7978Srrh /* write part of buffer which lies between  outp  and  inp .
255*7978Srrh /* this should be a direct call to 'write', but the system slows to a crawl
256*7978Srrh /* if it has to do an unaligned copy.  thus we buffer.  this silly loop
257*7978Srrh /* is 15% of the total time, thus even the 'putc' macro is too slow.
258*7978Srrh */
259*7978Srrh 	register char *p1,*p2; register FILE *f;
260*7978Srrh 	if ((p1=outp)==inp || flslvl!=0) return;
261*7978Srrh #if tgp
262*7978Srrh #define MAXOUT 80
263*7978Srrh 	if (!tgpscan) {/* scan again to insure <= MAXOUT chars between linefeeds */
264*7978Srrh 		register char c,*pblank; char savc,stopc,brk;
265*7978Srrh 		tgpscan=1; brk=stopc=pblank=0; p2=inp; savc= *p2; *p2='\0';
266*7978Srrh 		while (c= *p1++) {
267*7978Srrh 			if (c=='\\') c= *p1++;
268*7978Srrh 			if (stopc==c) stopc=0;
269*7978Srrh 			else if (c=='"' || c=='\'') stopc=c;
270*7978Srrh 			if (p1-outp>MAXOUT && pblank!=0) {
271*7978Srrh 				*pblank++='\n'; inp=pblank; dump(); brk=1; pblank=0;
272*7978Srrh 			}
273*7978Srrh 			if (c==' ' && stopc==0) pblank=p1-1;
274*7978Srrh 		}
275*7978Srrh 		if (brk) sayline();
276*7978Srrh 		*p2=savc; inp=p2; p1=outp; tgpscan=0;
277*7978Srrh 	}
278*7978Srrh #endif
279*7978Srrh 	f=fout;
280*7978Srrh # if gcos
281*7978Srrh /* filter out "$ program c" card if first line of input */
282*7978Srrh /* gmatch is a simple pattern matcher in the GCOS Standard Library */
283*7978Srrh {	static int gmfirst = 0;
284*7978Srrh 	if (!gmfirst) {
285*7978Srrh 		++gmfirst;
286*7978Srrh 		if (gmatch(p1, "^$*program[ \t]*c*"))
287*7978Srrh 			p1 = strdex(p1, '\n');
288*7978Srrh 	}
289*7978Srrh }
290*7978Srrh # endif
291*7978Srrh 	while (p1<inp) putc(*p1++,f);
292*7978Srrh 	outp=p1;
293*7978Srrh }
294*7978Srrh 
295*7978Srrh char *
296*7978Srrh refill(p) register char *p; {
297*7978Srrh /* dump buffer.  save chars from inp to p.  read into buffer at pbuf,
298*7978Srrh /* contiguous with p.  update pointers, return new p.
299*7978Srrh */
300*7978Srrh 	register char *np,*op; register int ninbuf;
301*7978Srrh 	dump(); np=pbuf-(p-inp); op=inp;
302*7978Srrh 	if (bob(np+1)) {pperror("token too long"); np=pbeg; p=inp+BUFSIZ;}
303*7978Srrh 	macdam += np-inp; outp=inp=np;
304*7978Srrh 	while (op<p) *np++= *op++;
305*7978Srrh 	p=np;
306*7978Srrh 	for (;;) {
307*7978Srrh 		if (mactop>inctop[ifno]) {/* retrieve hunk of pushed-back macro text */
308*7978Srrh 			op=instack[--mactop]; np=pbuf;
309*7978Srrh 			do {while (*np++= *op++);} while (op<endbuf[mactop]); pend=np-1;
310*7978Srrh 			/* make buffer space avail for 'include' processing */
311*7978Srrh 			if (fretop<MAXFRE) bufstack[fretop++]=instack[mactop];
312*7978Srrh 			return(p);
313*7978Srrh 		} else {/* get more text from file(s) */
314*7978Srrh 			maclvl=0;
315*7978Srrh 			if (0<(ninbuf=read(fin,pbuf,BUFSIZ))) {
316*7978Srrh 				pend=pbuf+ninbuf; *pend='\0';
317*7978Srrh 				return(p);
318*7978Srrh 			}
319*7978Srrh 			/* end of #include file */
320*7978Srrh 			if (ifno==0) {/* end of input */
321*7978Srrh 				if (plvl!=0) {
322*7978Srrh 					int n=plvl,tlin=lineno[ifno]; char *tfil=fnames[ifno];
323*7978Srrh 					lineno[ifno]=maclin; fnames[ifno]=macfil;
324*7978Srrh 					pperror("%s: unterminated macro call",macnam);
325*7978Srrh 					lineno[ifno]=tlin; fnames[ifno]=tfil;
326*7978Srrh 					np=p; *np++='\n';	/* shut off unterminated quoted string */
327*7978Srrh 					while (--n>=0) *np++=')';	/* supply missing parens */
328*7978Srrh 					pend=np; *np='\0'; if (plvl<0) plvl=0;
329*7978Srrh 					return(p);
330*7978Srrh 				}
331*7978Srrh 				inp=p; dump(); exit(exfail);
332*7978Srrh 			}
333*7978Srrh 			close(fin); fin=fins[--ifno]; dirs[0]=dirnams[ifno]; sayline();
334*7978Srrh 		}
335*7978Srrh 	}
336*7978Srrh }
337*7978Srrh 
338*7978Srrh #define BEG 0
339*7978Srrh #define LF 1
340*7978Srrh 
341*7978Srrh char *
342*7978Srrh cotoken(p) register char *p; {
343*7978Srrh 	register int c,i; char quoc;
344*7978Srrh 	static int state = BEG;
345*7978Srrh 
346*7978Srrh 	if (state!=BEG) goto prevlf;
347*7978Srrh for (;;) {
348*7978Srrh again:
349*7978Srrh 	while (!isspc(*p++));
350*7978Srrh 	switch (*(inp=p-1)) {
351*7978Srrh 	case 0: {
352*7978Srrh 		if (eob(--p)) {p=refill(p); goto again;}
353*7978Srrh 		else ++p; /* ignore null byte */
354*7978Srrh 	} break;
355*7978Srrh 	case '|': case '&': for (;;) {/* sloscan only */
356*7978Srrh 		if (*p++== *inp) break;
357*7978Srrh 		if (eob(--p)) p=refill(p);
358*7978Srrh 		else break;
359*7978Srrh 	} break;
360*7978Srrh 	case '=': case '!': for (;;) {/* sloscan only */
361*7978Srrh 		if (*p++=='=') break;
362*7978Srrh 		if (eob(--p)) p=refill(p);
363*7978Srrh 		else break;
364*7978Srrh 	} break;
365*7978Srrh 	case '<': case '>': for (;;) {/* sloscan only */
366*7978Srrh 		if (*p++=='=' || p[-2]==p[-1]) break;
367*7978Srrh 		if (eob(--p)) p=refill(p);
368*7978Srrh 		else break;
369*7978Srrh 	} break;
370*7978Srrh 	case '\\': for (;;) {
371*7978Srrh 		if (*p++=='\n') {++lineno[ifno]; break;}
372*7978Srrh 		if (eob(--p)) p=refill(p);
373*7978Srrh 		else {++p; break;}
374*7978Srrh 	} break;
375*7978Srrh 	case '/': for (;;) {
376*7978Srrh 		if (*p++=='*') {/* comment */
377*7978Srrh 			if (!passcom) {inp=p-2; dump(); ++flslvl;}
378*7978Srrh 			for (;;) {
379*7978Srrh 				while (!iscom(*p++));
380*7978Srrh 				if (p[-1]=='*') for (;;) {
381*7978Srrh 					if (*p++=='/') goto endcom;
382*7978Srrh 					if (eob(--p)) {
383*7978Srrh 						if (!passcom) {inp=p; p=refill(p);}
384*7978Srrh 						else if ((p-inp)>=BUFSIZ) {/* split long comment */
385*7978Srrh 							inp=p; p=refill(p);	/* last char written is '*' */
386*7978Srrh 							putc('/',fout);	/* terminate first part */
387*7978Srrh 							/* and fake start of 2nd */
388*7978Srrh 							outp=inp=p-=3; *p++='/'; *p++='*'; *p++='*';
389*7978Srrh 						} else p=refill(p);
390*7978Srrh 					} else break;
391*7978Srrh 				} else if (p[-1]=='\n') {
392*7978Srrh 					++lineno[ifno]; if (!passcom) putc('\n',fout);
393*7978Srrh 				} else if (eob(--p)) {
394*7978Srrh 					if (!passcom) {inp=p; p=refill(p);}
395*7978Srrh 					else if ((p-inp)>=BUFSIZ) {/* split long comment */
396*7978Srrh 						inp=p; p=refill(p);
397*7978Srrh 						putc('*',fout); putc('/',fout);
398*7978Srrh 						outp=inp=p-=2; *p++='/'; *p++='*';
399*7978Srrh 					} else p=refill(p);
400*7978Srrh 				} else ++p; /* ignore null byte */
401*7978Srrh 			}
402*7978Srrh 		endcom:
403*7978Srrh 			if (!passcom) {outp=inp=p; --flslvl; goto again;}
404*7978Srrh 			break;
405*7978Srrh 		}
406*7978Srrh 		if (eob(--p)) p=refill(p);
407*7978Srrh 		else break;
408*7978Srrh 	} break;
409*7978Srrh # if gcos
410*7978Srrh 	case '`':
411*7978Srrh # endif
412*7978Srrh 	case '"': case '\'': {
413*7978Srrh 		quoc=p[-1];
414*7978Srrh 		for (;;) {
415*7978Srrh 			while (!isquo(*p++));
416*7978Srrh 			if (p[-1]==quoc) break;
417*7978Srrh 			if (p[-1]=='\n') {--p; break;} /* bare \n terminates quotation */
418*7978Srrh 			if (p[-1]=='\\') for (;;) {
419*7978Srrh 				if (*p++=='\n') {++lineno[ifno]; break;} /* escaped \n ignored */
420*7978Srrh 				if (eob(--p)) p=refill(p);
421*7978Srrh 				else {++p; break;}
422*7978Srrh 			} else if (eob(--p)) p=refill(p);
423*7978Srrh 			else ++p;	/* it was a different quote character */
424*7978Srrh 		}
425*7978Srrh 	} break;
426*7978Srrh 	case '\n': {
427*7978Srrh 		++lineno[ifno]; if (isslo) {state=LF; return(p);}
428*7978Srrh prevlf:
429*7978Srrh 		state=BEG;
430*7978Srrh 		for (;;) {
431*7978Srrh 			if (*p++=='#') return(p);
432*7978Srrh 			if (eob(inp= --p)) p=refill(p);
433*7978Srrh 			else goto again;
434*7978Srrh 		}
435*7978Srrh 	} break;
436*7978Srrh 	case '0': case '1': case '2': case '3': case '4':
437*7978Srrh 	case '5': case '6': case '7': case '8': case '9':
438*7978Srrh 	for (;;) {
439*7978Srrh 		while (isnum(*p++));
440*7978Srrh 		if (eob(--p)) p=refill(p);
441*7978Srrh 		else break;
442*7978Srrh 	} break;
443*7978Srrh 	case 'A': case 'B': case 'C': case 'D': case 'E':
444*7978Srrh 	case 'F': case 'G': case 'H': case 'I': case 'J':
445*7978Srrh 	case 'K': case 'L': case 'M': case 'N': case 'O':
446*7978Srrh 	case 'P': case 'Q': case 'R': case 'S': case 'T':
447*7978Srrh 	case 'U': case 'V': case 'W': case 'X': case 'Y':
448*7978Srrh 	case 'Z': case '_':
449*7978Srrh 	case 'a': case 'b': case 'c': case 'd': case 'e':
450*7978Srrh 	case 'f': case 'g': case 'h': case 'i': case 'j':
451*7978Srrh 	case 'k': case 'l': case 'm': case 'n': case 'o':
452*7978Srrh 	case 'p': case 'q': case 'r': case 's': case 't':
453*7978Srrh 	case 'u': case 'v': case 'w': case 'x': case 'y':
454*7978Srrh 	case 'z':
455*7978Srrh #if scw1
456*7978Srrh #define tmac1(c,bit) if (!xmac1(c,bit,&)) goto nomac
457*7978Srrh #define xmac1(c,bit,op) ((macbit+COFF)[c] op (bit))
458*7978Srrh #else
459*7978Srrh #define tmac1(c,bit)
460*7978Srrh #define xmac1(c,bit,op)
461*7978Srrh #endif
462*7978Srrh 
463*7978Srrh #if scw2
464*7978Srrh #define tmac2(c0,c1,cpos) if (!xmac2(c0,c1,cpos,&)) goto nomac
465*7978Srrh #define xmac2(c0,c1,cpos,op)\
466*7978Srrh 	((macbit+COFF)[(t21+COFF)[c0]+(t22+COFF)[c1]] op (t23+COFF+cpos)[c0])
467*7978Srrh #else
468*7978Srrh #define tmac2(c0,c1,cpos)
469*7978Srrh #define xmac2(c0,c1,cpos,op)
470*7978Srrh #endif
471*7978Srrh 
472*7978Srrh 	if (flslvl) goto nomac;
473*7978Srrh 	for (;;) {
474*7978Srrh 		c= p[-1];                          tmac1(c,b0);
475*7978Srrh 		i= *p++; if (!isid(i)) goto endid; tmac1(i,b1); tmac2(c,i,0);
476*7978Srrh 		c= *p++; if (!isid(c)) goto endid; tmac1(c,b2); tmac2(i,c,1);
477*7978Srrh 		i= *p++; if (!isid(i)) goto endid; tmac1(i,b3); tmac2(c,i,2);
478*7978Srrh 		c= *p++; if (!isid(c)) goto endid; tmac1(c,b4); tmac2(i,c,3);
479*7978Srrh 		i= *p++; if (!isid(i)) goto endid; tmac1(i,b5); tmac2(c,i,4);
480*7978Srrh 		c= *p++; if (!isid(c)) goto endid; tmac1(c,b6); tmac2(i,c,5);
481*7978Srrh 		i= *p++; if (!isid(i)) goto endid; tmac1(i,b7); tmac2(c,i,6);
482*7978Srrh 		                                                tmac2(i,0,7);
483*7978Srrh 		while (isid(*p++));
484*7978Srrh 		if (eob(--p)) {refill(p); p=inp+1; continue;}
485*7978Srrh 		goto lokid;
486*7978Srrh 	endid:
487*7978Srrh 		if (eob(--p)) {refill(p); p=inp+1; continue;}
488*7978Srrh 		tmac2(p[-1],0,-1+(p-inp));
489*7978Srrh 	lokid:
490*7978Srrh 		slookup(inp,p,0); if (newp) {p=newp; goto again;}
491*7978Srrh 		else break;
492*7978Srrh 	nomac:
493*7978Srrh 		while (isid(*p++));
494*7978Srrh 		if (eob(--p)) {p=refill(p); goto nomac;}
495*7978Srrh 		else break;
496*7978Srrh 	} break;
497*7978Srrh 	} /* end of switch */
498*7978Srrh 
499*7978Srrh 	if (isslo) return(p);
500*7978Srrh } /* end of infinite loop */
501*7978Srrh }
502*7978Srrh 
503*7978Srrh char *
504*7978Srrh skipbl(p) register char *p; {/* get next non-blank token */
505*7978Srrh 	do {outp=inp=p; p=cotoken(p);} while ((toktyp+COFF)[*inp]==BLANK);
506*7978Srrh 	return(p);
507*7978Srrh }
508*7978Srrh 
509*7978Srrh char *
510*7978Srrh unfill(p) register char *p; {
511*7978Srrh /* take <= BUFSIZ chars from right end of buffer and put them on instack .
512*7978Srrh /* slide rest of buffer to the right, update pointers, return new p.
513*7978Srrh */
514*7978Srrh 	register char *np,*op; register int d;
515*7978Srrh 	if (mactop>=MAXFRE) {
516*7978Srrh 		pperror("%s: too much pushback",macnam);
517*7978Srrh 		p=inp=pend; dump();	/* begin flushing pushback */
518*7978Srrh 		while (mactop>inctop[ifno]) {p=refill(p); p=inp=pend; dump();}
519*7978Srrh 	}
520*7978Srrh 	if (fretop>0) np=bufstack[--fretop];
521*7978Srrh 	else {
522*7978Srrh 		np=savch; savch+=BUFSIZ;
523*7978Srrh 		if (savch>=sbf+SBSIZE) {pperror("no space"); exit(exfail);}
524*7978Srrh 		*savch++='\0';
525*7978Srrh 	}
526*7978Srrh 	instack[mactop]=np; op=pend-BUFSIZ; if (op<p) op=p;
527*7978Srrh 	for (;;) {while (*np++= *op++); if (eob(op)) break;} /* out with old */
528*7978Srrh 	endbuf[mactop++]=np;	/* mark end of saved text */
529*7978Srrh 	np=pbuf+BUFSIZ; op=pend-BUFSIZ; pend=np; if (op<p) op=p;
530*7978Srrh 	while (outp<op) *--np= *--op; /* slide over new */
531*7978Srrh 	if (bob(np)) pperror("token too long");
532*7978Srrh 	d=np-outp; outp+=d; inp+=d; macdam+=d; return(p+d);
533*7978Srrh }
534*7978Srrh 
535*7978Srrh char *
536*7978Srrh doincl(p) register char *p; {
537*7978Srrh 	int filok,inctype;
538*7978Srrh 	register char *cp; char **dirp,*nfil; char filname[BUFSIZ];
539*7978Srrh 
540*7978Srrh 	p=skipbl(p); cp=filname;
541*7978Srrh 	if (*inp++=='<') {/* special <> syntax */
542*7978Srrh 		inctype=1;
543*7978Srrh 		++flslvl;	/* prevent macro expansion */
544*7978Srrh 		for (;;) {
545*7978Srrh 			outp=inp=p; p=cotoken(p);
546*7978Srrh 			if (*inp=='\n') {--p; *cp='\0'; break;}
547*7978Srrh 			if (*inp=='>') {      *cp='\0'; break;}
548*7978Srrh # ifdef gimpel
549*7978Srrh 			if (*inp=='.' && !intss()) *inp='#';
550*7978Srrh # endif
551*7978Srrh 			while (inp<p) *cp++= *inp++;
552*7978Srrh 		}
553*7978Srrh 		--flslvl;	/* reenable macro expansion */
554*7978Srrh 	} else if (inp[-1]=='"') {/* regular "" syntax */
555*7978Srrh 		inctype=0;
556*7978Srrh # ifdef gimpel
557*7978Srrh 		while (inp<p) {if (*inp=='.' && !intss()) *inp='#'; *cp++= *inp++;}
558*7978Srrh # else
559*7978Srrh 		while (inp<p) *cp++= *inp++;
560*7978Srrh # endif
561*7978Srrh 		if (*--cp=='"') *cp='\0';
562*7978Srrh 	} else {pperror("bad include syntax",0); inctype=2;}
563*7978Srrh 	/* flush current file to \n , then write \n */
564*7978Srrh 	++flslvl; do {outp=inp=p; p=cotoken(p);} while (*inp!='\n'); --flslvl;
565*7978Srrh 	inp=p; dump(); if (inctype==2) return(p);
566*7978Srrh 	/* look for included file */
567*7978Srrh 	if (ifno+1 >=MAXINC) {
568*7978Srrh 		pperror("Unreasonable include nesting",0); return(p);
569*7978Srrh 	}
570*7978Srrh 	if((nfil=savch)>sbf+SBSIZE-BUFSIZ) {pperror("no space"); exit(exfail);}
571*7978Srrh 	filok=0;
572*7978Srrh 	for (dirp=dirs+inctype; *dirp; ++dirp) {
573*7978Srrh 		if (
574*7978Srrh # if gcos
575*7978Srrh 			strdex(filname, '/')
576*7978Srrh # else
577*7978Srrh 			filname[0]=='/'
578*7978Srrh # endif
579*7978Srrh 				|| **dirp=='\0') strcpy(nfil,filname);
580*7978Srrh 		else {
581*7978Srrh 			strcpy(nfil,*dirp);
582*7978Srrh # if unix || gcos
583*7978Srrh 			strcat(nfil,"/");
584*7978Srrh # endif
585*7978Srrh #ifdef ibm
586*7978Srrh #ifndef gimpel
587*7978Srrh 			strcat(nfil,".");
588*7978Srrh #endif
589*7978Srrh #endif
590*7978Srrh 			strcat(nfil,filname);
591*7978Srrh 		}
592*7978Srrh 		if (0<(fins[ifno+1]=open(nfil,READ))) {
593*7978Srrh 			filok=1; fin=fins[++ifno]; break;
594*7978Srrh 		}
595*7978Srrh 	}
596*7978Srrh 	if (filok==0) pperror("Can't find include file %s",filname);
597*7978Srrh 	else {
598*7978Srrh 		lineno[ifno]=1; fnames[ifno]=cp=nfil; while (*cp++); savch=cp;
599*7978Srrh 		dirnams[ifno]=dirs[0]=trmdir(copy(nfil));
600*7978Srrh 		sayline();
601*7978Srrh 		/* save current contents of buffer */
602*7978Srrh 		while (!eob(p)) p=unfill(p);
603*7978Srrh 		inctop[ifno]=mactop;
604*7978Srrh 	}
605*7978Srrh 	return(p);
606*7978Srrh }
607*7978Srrh 
608*7978Srrh equfrm(a,p1,p2) register char *a,*p1,*p2; {
609*7978Srrh 	register char c; int flag;
610*7978Srrh 	c= *p2; *p2='\0';
611*7978Srrh 	flag=strcmp(a,p1); *p2=c; return(flag==SAME);
612*7978Srrh }
613*7978Srrh 
614*7978Srrh char *
615*7978Srrh dodef(p) char *p; {/* process '#define' */
616*7978Srrh 	register char *pin,*psav,*cf;
617*7978Srrh 	char **pf,**qf; int b,c,params; struct symtab *np;
618*7978Srrh 	char *oldval,*oldsavch;
619*7978Srrh 	char *formal[MAXFRM]; /* formal[n] is name of nth formal */
620*7978Srrh 	char formtxt[BUFSIZ]; /* space for formal names */
621*7978Srrh 
622*7978Srrh 	if (savch>sbf+SBSIZE-BUFSIZ) {pperror("too much defining"); return(p);}
623*7978Srrh 	oldsavch=savch; /* to reclaim space if redefinition */
624*7978Srrh 	++flslvl; /* prevent macro expansion during 'define' */
625*7978Srrh 	p=skipbl(p); pin=inp;
626*7978Srrh 	if ((toktyp+COFF)[*pin]!=IDENT) {
627*7978Srrh 		ppwarn("illegal macro name"); while (*inp!='\n') p=skipbl(p); return(p);
628*7978Srrh 	}
629*7978Srrh 	np=slookup(pin,p,1);
630*7978Srrh 	if (oldval=np->value) savch=oldsavch;	/* was previously defined */
631*7978Srrh 	b=1; cf=pin;
632*7978Srrh 	while (cf<p) {/* update macbit */
633*7978Srrh 		c= *cf++; xmac1(c,b,|=); b=(b+b)&0xFF;
634*7978Srrh 		if (cf!=p) xmac2(c,*cf,-1+(cf-pin),|=);
635*7978Srrh 		else xmac2(c,0,-1+(cf-pin),|=);
636*7978Srrh 	}
637*7978Srrh 	params=0; outp=inp=p; p=cotoken(p); pin=inp;
638*7978Srrh 	if (*pin=='(') {/* with parameters; identify the formals */
639*7978Srrh 		cf=formtxt; pf=formal;
640*7978Srrh 		for (;;) {
641*7978Srrh 			p=skipbl(p); pin=inp;
642*7978Srrh 			if (*pin=='\n') {
643*7978Srrh 				--lineno[ifno]; --p; pperror("%s: missing )",np->name); break;
644*7978Srrh 			}
645*7978Srrh 			if (*pin==')') break;
646*7978Srrh 			if (*pin==',') continue;
647*7978Srrh 			if ((toktyp+COFF)[*pin]!=IDENT) {
648*7978Srrh 				c= *p; *p='\0'; pperror("bad formal: %s",pin); *p=c;
649*7978Srrh 			} else if (pf>= &formal[MAXFRM]) {
650*7978Srrh 				c= *p; *p='\0'; pperror("too many formals: %s",pin); *p=c;
651*7978Srrh 			} else {
652*7978Srrh 				*pf++=cf; while (pin<p) *cf++= *pin++; *cf++='\0'; ++params;
653*7978Srrh 			}
654*7978Srrh 		}
655*7978Srrh 		if (params==0) --params; /* #define foo() ... */
656*7978Srrh 	} else if (*pin=='\n') {--lineno[ifno]; --p;}
657*7978Srrh 	/* remember beginning of macro body, so that we can
658*7978Srrh 	/* warn if a redefinition is different from old value.
659*7978Srrh 	*/
660*7978Srrh 	oldsavch=psav=savch;
661*7978Srrh 	for (;;) {/* accumulate definition until linefeed */
662*7978Srrh 		outp=inp=p; p=cotoken(p); pin=inp;
663*7978Srrh 		if (*pin=='\\' && pin[1]=='\n') {putc('\n',fout); continue;}	/* ignore escaped lf */
664*7978Srrh 		if (*pin=='\n') break;
665*7978Srrh 		if (params) {/* mark the appearance of formals in the definiton */
666*7978Srrh 			if ((toktyp+COFF)[*pin]==IDENT) {
667*7978Srrh 				for (qf=pf; --qf>=formal; ) {
668*7978Srrh 					if (equfrm(*qf,pin,p)) {
669*7978Srrh 						*psav++=qf-formal+1; *psav++=WARN; pin=p; break;
670*7978Srrh 					}
671*7978Srrh 				}
672*7978Srrh 			} else if (*pin=='"' || *pin=='\''
673*7978Srrh # if gcos
674*7978Srrh 					|| *pin=='`'
675*7978Srrh # endif
676*7978Srrh 						) {/* inside quotation marks, too */
677*7978Srrh 				char quoc= *pin;
678*7978Srrh 				for (*psav++= *pin++; pin<p && *pin!=quoc; ) {
679*7978Srrh 					while (pin<p && !isid(*pin)) *psav++= *pin++;
680*7978Srrh 					cf=pin; while (cf<p && isid(*cf)) ++cf;
681*7978Srrh 					for (qf=pf; --qf>=formal; ) {
682*7978Srrh 						if (equfrm(*qf,pin,cf)) {
683*7978Srrh 							*psav++=qf-formal+1; *psav++=WARN; pin=cf; break;
684*7978Srrh 						}
685*7978Srrh 					}
686*7978Srrh 					while (pin<cf) *psav++= *pin++;
687*7978Srrh 				}
688*7978Srrh 			}
689*7978Srrh 		}
690*7978Srrh 		while (pin<p) *psav++= *pin++;
691*7978Srrh 	}
692*7978Srrh 	*psav++=params; *psav++='\0';
693*7978Srrh 	if ((cf=oldval)!=NULL) {/* redefinition */
694*7978Srrh 		--cf;	/* skip no. of params, which may be zero */
695*7978Srrh 		while (*--cf);	/* go back to the beginning */
696*7978Srrh 		if (0!=strcmp(++cf,oldsavch)) {/* redefinition different from old */
697*7978Srrh 			--lineno[ifno]; ppwarn("%s redefined",np->name); ++lineno[ifno];
698*7978Srrh 			np->value=psav-1;
699*7978Srrh 		} else psav=oldsavch; /* identical redef.; reclaim space */
700*7978Srrh 	} else np->value=psav-1;
701*7978Srrh 	--flslvl; inp=pin; savch=psav; return(p);
702*7978Srrh }
703*7978Srrh 
704*7978Srrh #define fasscan() ptrtab=fastab+COFF
705*7978Srrh #define sloscan() ptrtab=slotab+COFF
706*7978Srrh 
707*7978Srrh char *
708*7978Srrh control(p) register char *p; {/* find and handle preprocessor control lines */
709*7978Srrh 	register struct symtab *np;
710*7978Srrh for (;;) {
711*7978Srrh 	fasscan(); p=cotoken(p); if (*inp=='\n') ++inp; dump();
712*7978Srrh 	sloscan(); p=skipbl(p);
713*7978Srrh 	*--inp=SALT; outp=inp; ++flslvl; np=slookup(inp,p,0); --flslvl;
714*7978Srrh 	if (np==defloc) {/* define */
715*7978Srrh 		if (flslvl==0) {p=dodef(p); continue;}
716*7978Srrh 	} else if (np==incloc) {/* include */
717*7978Srrh 		if (flslvl==0) {p=doincl(p); continue;}
718*7978Srrh 	} else if (np==ifnloc) {/* ifndef */
719*7978Srrh 		++flslvl; p=skipbl(p); np=slookup(inp,p,0); --flslvl;
720*7978Srrh 		if (flslvl==0 && np->value==0) ++trulvl;
721*7978Srrh 		else ++flslvl;
722*7978Srrh 	} else if (np==ifdloc) {/* ifdef */
723*7978Srrh 		++flslvl; p=skipbl(p); np=slookup(inp,p,0); --flslvl;
724*7978Srrh 		if (flslvl==0 && np->value!=0) ++trulvl;
725*7978Srrh 		else ++flslvl;
726*7978Srrh 	} else if (np==eifloc) {/* endif */
727*7978Srrh 		if (flslvl) {if (--flslvl==0) sayline();}
728*7978Srrh 		else if (trulvl) --trulvl;
729*7978Srrh 		else pperror("If-less endif",0);
730*7978Srrh 	} else if (np==elsloc) {/* else */
731*7978Srrh 		if (flslvl) {
732*7978Srrh 			if (--flslvl!=0) ++flslvl;
733*7978Srrh 			else {++trulvl; sayline();}
734*7978Srrh 		}
735*7978Srrh 		else if (trulvl) {++flslvl; --trulvl;}
736*7978Srrh 		else pperror("If-less else",0);
737*7978Srrh 	} else if (np==udfloc) {/* undefine */
738*7978Srrh 		if (flslvl==0) {
739*7978Srrh 			++flslvl; p=skipbl(p); slookup(inp,p,DROP); --flslvl;
740*7978Srrh 		}
741*7978Srrh 	} else if (np==ifloc) {/* if */
742*7978Srrh #if tgp
743*7978Srrh 		pperror(" IF not implemented, true assumed", 0);
744*7978Srrh 		if (flslvl==0) ++trulvl; else ++flslvl;
745*7978Srrh #else
746*7978Srrh 		newp=p;
747*7978Srrh 		if (flslvl==0 && yyparse()) ++trulvl; else ++flslvl;
748*7978Srrh 		p=newp;
749*7978Srrh #endif
750*7978Srrh 	} else if (np==lneloc) {/* line */
751*7978Srrh 		if (flslvl==0 && pflag==0) {
752*7978Srrh 			outp=inp=p; *--outp='#'; while (*inp!='\n') p=cotoken(p);
753*7978Srrh 			continue;
754*7978Srrh 		}
755*7978Srrh 	} else if (*++inp=='\n') outp=inp;	/* allows blank line after # */
756*7978Srrh 	else pperror("undefined control",0);
757*7978Srrh 	/* flush to lf */
758*7978Srrh 	++flslvl; while (*inp!='\n') {outp=inp=p; p=cotoken(p);} --flslvl;
759*7978Srrh }
760*7978Srrh }
761*7978Srrh 
762*7978Srrh struct symtab *
763*7978Srrh stsym(s) register char *s; {
764*7978Srrh 	char buf[BUFSIZ]; register char *p;
765*7978Srrh 
766*7978Srrh 	/* make definition look exactly like end of #define line */
767*7978Srrh 	/* copy to avoid running off end of world when param list is at end */
768*7978Srrh 	p=buf; while (*p++= *s++);
769*7978Srrh 	p=buf; while (isid(*p++)); /* skip first identifier */
770*7978Srrh 	if (*--p=='=') {*p++=' '; while (*p++);}
771*7978Srrh 	else {s=" 1"; while (*p++= *s++);}
772*7978Srrh 	pend=p; *--p='\n';
773*7978Srrh 	sloscan(); dodef(buf); return(lastsym);
774*7978Srrh }
775*7978Srrh 
776*7978Srrh struct symtab *
777*7978Srrh ppsym(s) char *s; {/* kluge */
778*7978Srrh 	register struct symtab *sp;
779*7978Srrh 	cinit=SALT; *savch++=SALT; sp=stsym(s); --sp->name; cinit=0; return(sp);
780*7978Srrh }
781*7978Srrh 
782*7978Srrh /* VARARGS1 */
783*7978Srrh pperror(s,x,y) char *s; {
784*7978Srrh 	if (fnames[ifno][0]) fprintf(stderr,
785*7978Srrh # if gcos
786*7978Srrh 			"*%c*   \"%s\", line ", exfail >= 0 ? 'F' : 'W',
787*7978Srrh # else
788*7978Srrh 			"%s: ",
789*7978Srrh # endif
790*7978Srrh 				 fnames[ifno]);
791*7978Srrh 	fprintf(stderr, "%d: ",lineno[ifno]);
792*7978Srrh 	fprintf(stderr, s, x, y);
793*7978Srrh 	fprintf(stderr,"\n");
794*7978Srrh 	++exfail;
795*7978Srrh }
796*7978Srrh 
797*7978Srrh yyerror(s,a,b) char *s; {
798*7978Srrh 	pperror(s,a,b);
799*7978Srrh }
800*7978Srrh 
801*7978Srrh ppwarn(s,x) char *s; {
802*7978Srrh 	int fail = exfail;
803*7978Srrh 	exfail = -1;
804*7978Srrh 	pperror(s,x);
805*7978Srrh 	exfail = fail;
806*7978Srrh }
807*7978Srrh 
808*7978Srrh struct symtab *
809*7978Srrh lookup(namep, enterf)
810*7978Srrh char *namep;
811*7978Srrh {
812*7978Srrh 	register char *np, *snp;
813*7978Srrh 	register int c, i; int around;
814*7978Srrh 	register struct symtab *sp;
815*7978Srrh 
816*7978Srrh 	/* namep had better not be too long (currently, <=NCPS chars) */
817*7978Srrh 	np=namep; around=0; i=cinit;
818*7978Srrh 	while (c= *np++) i += i+c; c=i;	/* c=i for register usage on pdp11 */
819*7978Srrh 	c %= symsiz; if (c<0) c += symsiz;
820*7978Srrh 	sp = &stab[c];
821*7978Srrh 	while (snp=sp->name) {
822*7978Srrh 		np = namep;
823*7978Srrh 		while (*snp++ == *np) if (*np++ == '\0') {
824*7978Srrh 				if (enterf==DROP) {sp->name[0]= DROP; sp->value=0;}
825*7978Srrh 				return(lastsym=sp);
826*7978Srrh 			}
827*7978Srrh 		if (--sp < &stab[0])
828*7978Srrh 			if (around) {pperror("too many defines", 0); exit(exfail);}
829*7978Srrh 			else {++around; sp = &stab[symsiz-1];}
830*7978Srrh 	}
831*7978Srrh 	if (enterf==1) sp->name=namep;
832*7978Srrh 	return(lastsym=sp);
833*7978Srrh }
834*7978Srrh 
835*7978Srrh struct symtab *
836*7978Srrh slookup(p1,p2,enterf) register char *p1,*p2; int enterf;{
837*7978Srrh 	register char *p3; char c2,c3; struct symtab *np;
838*7978Srrh 	         c2= *p2; *p2='\0';	/* mark end of token */
839*7978Srrh 	if ((p2-p1)>NCPS) p3=p1+NCPS; else p3=p2;
840*7978Srrh 			 c3= *p3; *p3='\0';	/* truncate to NCPS chars or less */
841*7978Srrh 	if (enterf==1) p1=copy(p1);
842*7978Srrh 	np=lookup(p1,enterf); *p3=c3; *p2=c2;
843*7978Srrh 	if (np->value!=0 && flslvl==0) newp=subst(p2,np);
844*7978Srrh 	else newp=0;
845*7978Srrh 	return(np);
846*7978Srrh }
847*7978Srrh 
848*7978Srrh char *
849*7978Srrh subst(p,sp) register char *p; struct symtab *sp; {
850*7978Srrh 	static char match[]="%s: argument mismatch";
851*7978Srrh 	register char *ca,*vp; int params;
852*7978Srrh 	char *actual[MAXFRM]; /* actual[n] is text of nth actual */
853*7978Srrh 	char acttxt[BUFSIZ]; /* space for actuals */
854*7978Srrh 
855*7978Srrh 	if (0==(vp=sp->value)) return(p);
856*7978Srrh 	if ((p-macforw)<=macdam) {
857*7978Srrh 		if (++maclvl>symsiz && !rflag) {
858*7978Srrh 			pperror("%s: macro recursion",sp->name); return(p);
859*7978Srrh 		}
860*7978Srrh 	} else maclvl=0;	/* level decreased */
861*7978Srrh 	macforw=p; macdam=0;	/* new target for decrease in level */
862*7978Srrh 	macnam=sp->name;
863*7978Srrh 	dump();
864*7978Srrh 	if (sp==ulnloc) {
865*7978Srrh 		vp=acttxt; *vp++='\0';
866*7978Srrh 		sprintf(vp,"%d",lineno[ifno]); while (*vp++);
867*7978Srrh 	} else if (sp==uflloc) {
868*7978Srrh 		vp=acttxt; *vp++='\0';
869*7978Srrh 		sprintf(vp,"\"%s\"",fnames[ifno]); while (*vp++);
870*7978Srrh 	}
871*7978Srrh 	if (0!=(params= *--vp&0xFF)) {/* definition calls for params */
872*7978Srrh 		register char **pa;
873*7978Srrh 		ca=acttxt; pa=actual;
874*7978Srrh 		if (params==0xFF) params=1;	/* #define foo() ... */
875*7978Srrh 		sloscan(); ++flslvl; /* no expansion during search for actuals */
876*7978Srrh 		plvl= -1;
877*7978Srrh 		do p=skipbl(p); while (*inp=='\n');	/* skip \n too */
878*7978Srrh 		if (*inp=='(') {
879*7978Srrh 			maclin=lineno[ifno]; macfil=fnames[ifno];
880*7978Srrh 			for (plvl=1; plvl!=0; ) {
881*7978Srrh 				*ca++='\0';
882*7978Srrh 				for (;;) {
883*7978Srrh 					outp=inp=p; p=cotoken(p);
884*7978Srrh 					if (*inp=='(') ++plvl;
885*7978Srrh 					if (*inp==')' && --plvl==0) {--params; break;}
886*7978Srrh 					if (plvl==1 && *inp==',') {--params; break;}
887*7978Srrh 					while (inp<p) *ca++= *inp++;
888*7978Srrh 					if (ca> &acttxt[BUFSIZ])
889*7978Srrh 						pperror("%s: actuals too long",sp->name);
890*7978Srrh 				}
891*7978Srrh 				if (pa>= &actual[MAXFRM]) ppwarn(match,sp->name);
892*7978Srrh 				else *pa++=ca;
893*7978Srrh 			}
894*7978Srrh 		}
895*7978Srrh 		if (params!=0) ppwarn(match,sp->name);
896*7978Srrh 		while (--params>=0) *pa++=""+1;	/* null string for missing actuals */
897*7978Srrh 		--flslvl; fasscan();
898*7978Srrh 	}
899*7978Srrh 	for (;;) {/* push definition onto front of input stack */
900*7978Srrh 		while (!iswarn(*--vp)) {
901*7978Srrh 			if (bob(p)) {outp=inp=p; p=unfill(p);}
902*7978Srrh 			*--p= *vp;
903*7978Srrh 		}
904*7978Srrh 		if (*vp==warnc) {/* insert actual param */
905*7978Srrh 			ca=actual[*--vp-1];
906*7978Srrh 			while (*--ca) {
907*7978Srrh 				if (bob(p)) {outp=inp=p; p=unfill(p);}
908*7978Srrh 				*--p= *ca;
909*7978Srrh 			}
910*7978Srrh 		} else break;
911*7978Srrh 	}
912*7978Srrh 	outp=inp=p;
913*7978Srrh 	return(p);
914*7978Srrh }
915*7978Srrh 
916*7978Srrh 
917*7978Srrh 
918*7978Srrh 
919*7978Srrh char *
920*7978Srrh trmdir(s) register char *s; {
921*7978Srrh 	register char *p = s;
922*7978Srrh 	while (*p++); --p; while (p>s && *--p!='/');
923*7978Srrh # if unix
924*7978Srrh 	if (p==s) *p++='.';
925*7978Srrh # endif
926*7978Srrh 	*p='\0';
927*7978Srrh 	return(s);
928*7978Srrh }
929*7978Srrh 
930*7978Srrh STATIC char *
931*7978Srrh copy(s) register char *s; {
932*7978Srrh 	register char *old;
933*7978Srrh 
934*7978Srrh 	old = savch; while (*savch++ = *s++);
935*7978Srrh 	return(old);
936*7978Srrh }
937*7978Srrh 
938*7978Srrh char *
939*7978Srrh strdex(s,c) char *s,c; {
940*7978Srrh 	while (*s) if (*s++==c) return(--s);
941*7978Srrh 	return(0);
942*7978Srrh }
943*7978Srrh 
944*7978Srrh yywrap(){ return(1); }
945*7978Srrh 
946*7978Srrh main(argc,argv)
947*7978Srrh 	char *argv[];
948*7978Srrh {
949*7978Srrh 	register int i,c;
950*7978Srrh 	register char *p;
951*7978Srrh 	char *tf,**cp2;
952*7978Srrh 
953*7978Srrh # if gcos
954*7978Srrh 	if (setjmp(env)) return (exfail);
955*7978Srrh # endif
956*7978Srrh 	p="_$ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
957*7978Srrh 		i=0;
958*7978Srrh 		while (c= *p++) {
959*7978Srrh 			(fastab+COFF)[c] |= IB|NB|SB; (toktyp+COFF)[c]=IDENT;
960*7978Srrh #if scw2
961*7978Srrh 			/* 53 == 63-10; digits rarely appear in identifiers,
962*7978Srrh 			/* and can never be the first char of an identifier.
963*7978Srrh 			/* 11 == 53*53/sizeof(macbit) .
964*7978Srrh 			*/
965*7978Srrh 			++i; (t21+COFF)[c]=(53*i)/11; (t22+COFF)[c]=i%11;
966*7978Srrh #endif
967*7978Srrh 		}
968*7978Srrh 	p="0123456789.";
969*7978Srrh 		while (c= *p++) {(fastab+COFF)[c] |= NB|SB; (toktyp+COFF)[c]=NUMBR;}
970*7978Srrh # if gcos
971*7978Srrh 	p="\n\"'`/\\";
972*7978Srrh # else
973*7978Srrh 	p="\n\"'/\\";
974*7978Srrh # endif
975*7978Srrh 		while (c= *p++) (fastab+COFF)[c] |= SB;
976*7978Srrh # if gcos
977*7978Srrh 	p="\n\"'`\\";
978*7978Srrh # else
979*7978Srrh 	p="\n\"'\\";
980*7978Srrh # endif
981*7978Srrh 		while (c= *p++) (fastab+COFF)[c] |= QB;
982*7978Srrh 	p="*\n"; while (c= *p++) (fastab+COFF)[c] |= CB;
983*7978Srrh 	(fastab+COFF)[warnc] |= WB;
984*7978Srrh 	(fastab+COFF)['\0'] |= CB|QB|SB|WB;
985*7978Srrh 	for (i=ALFSIZ; --i>=0; ) slotab[i]=fastab[i]|SB;
986*7978Srrh 	p=" \t\013\f\r";	/* note no \n;	\v not legal for vertical tab? */
987*7978Srrh 		while (c= *p++) (toktyp+COFF)[c]=BLANK;
988*7978Srrh #if scw2
989*7978Srrh 	for ((t23+COFF)[i=ALFSIZ+7-COFF]=1; --i>=-COFF; )
990*7978Srrh 		if (((t23+COFF)[i]=(t23+COFF+1)[i]<<1)==0) (t23+COFF)[i]=1;
991*7978Srrh #endif
992*7978Srrh 
993*7978Srrh # if unix
994*7978Srrh 	fnames[ifno=0] = ""; dirnams[0]=dirs[0]=".";
995*7978Srrh # endif
996*7978Srrh # if ibm
997*7978Srrh 	fnames[ifno=0] = "";
998*7978Srrh # endif
999*7978Srrh # if gcos
1000*7978Srrh 	if (inquire(stdin, _TTY)) freopen("*src", "rt", stdin);
1001*7978Srrh # endif
1002*7978Srrh # if gimpel || gcos
1003*7978Srrh 	fnames[ifno=0] = (char *)inquire(stdin, _FILENAME);
1004*7978Srrh 	dirnams[0] = dirs[0] = trmdir(copy(fnames[0]));
1005*7978Srrh # endif
1006*7978Srrh 	for(i=1; i<argc; i++)
1007*7978Srrh 		{
1008*7978Srrh 		switch(argv[i][0])
1009*7978Srrh 			{
1010*7978Srrh 			case '-':
1011*7978Srrh # if gcos
1012*7978Srrh 			switch(toupper(argv[i][1])) { /* case-independent on GCOS */
1013*7978Srrh # else
1014*7978Srrh 			switch(argv[i][1]) {
1015*7978Srrh # endif
1016*7978Srrh 				case 'P': pflag++;
1017*7978Srrh 				case 'E': continue;
1018*7978Srrh 				case 'R': ++rflag; continue;
1019*7978Srrh 				case 'C': passcom++; continue;
1020*7978Srrh 				case 'D':
1021*7978Srrh 					if (predef>prespc+NPREDEF) {
1022*7978Srrh 						pperror("too many -D options, ignoring %s",argv[i]);
1023*7978Srrh 						continue;
1024*7978Srrh 					}
1025*7978Srrh 					/* ignore plain "-D" (no argument) */
1026*7978Srrh 					if (*(argv[i]+2)) *predef++ = argv[i]+2;
1027*7978Srrh 					continue;
1028*7978Srrh 				case 'U':
1029*7978Srrh 					if (prund>punspc+NPREDEF) {
1030*7978Srrh 						pperror("too many -U options, ignoring %s",argv[i]);
1031*7978Srrh 						continue;
1032*7978Srrh 					}
1033*7978Srrh 					*prund++ = argv[i]+2;
1034*7978Srrh 					continue;
1035*7978Srrh 				case 'I':
1036*7978Srrh 					if (nd>8) pperror("excessive -I file (%s) ignored",argv[i]);
1037*7978Srrh 					else dirs[nd++] = argv[i]+2;
1038*7978Srrh 					continue;
1039*7978Srrh 				case '\0': continue;
1040*7978Srrh 				default:
1041*7978Srrh 					pperror("unknown flag %s", argv[i]);
1042*7978Srrh 					continue;
1043*7978Srrh 				}
1044*7978Srrh 			default:
1045*7978Srrh 				if (fin==STDIN) {
1046*7978Srrh 					if (0>(fin=open(argv[i], READ))) {
1047*7978Srrh 						pperror("No source file %s",argv[i]); exit(8);
1048*7978Srrh 					}
1049*7978Srrh 					fnames[ifno]=copy(argv[i]);
1050*7978Srrh 					dirs[0]=dirnams[ifno]=trmdir(copy(argv[i]));
1051*7978Srrh # ifndef gcos
1052*7978Srrh /* too dangerous to have file name in same syntactic position
1053*7978Srrh    be input or output file depending on file redirections,
1054*7978Srrh    so force output to stdout, willy-nilly
1055*7978Srrh 	[i don't see what the problem is.  jfr]
1056*7978Srrh */
1057*7978Srrh 				} else if (fout==stdout) {
1058*7978Srrh 					extern char _sobuf[BUFSIZ];
1059*7978Srrh 					if (NULL==(fout=fopen(argv[i], "w"))) {
1060*7978Srrh 						pperror("Can't create %s", argv[i]); exit(8);
1061*7978Srrh 					} else {fclose(stdout); setbuf(fout,_sobuf);}
1062*7978Srrh # endif
1063*7978Srrh 				} else pperror("extraneous name %s", argv[i]);
1064*7978Srrh 			}
1065*7978Srrh 		}
1066*7978Srrh 
1067*7978Srrh 	fins[ifno]=fin;
1068*7978Srrh 	exfail = 0;
1069*7978Srrh 		/* after user -I files here are the standard include libraries */
1070*7978Srrh # if unix
1071*7978Srrh 	dirs[nd++] = "/usr/include";
1072*7978Srrh # endif
1073*7978Srrh # if gcos
1074*7978Srrh 	dirs[nd++] = "cc/include";
1075*7978Srrh # endif
1076*7978Srrh # if ibm
1077*7978Srrh # ifndef gimpel
1078*7978Srrh 	dirs[nd++] = "BTL$CLIB";
1079*7978Srrh # endif
1080*7978Srrh # endif
1081*7978Srrh # ifdef gimpel
1082*7978Srrh 	dirs[nd++] = intss() ?  "SYS3.C." : "" ;
1083*7978Srrh # endif
1084*7978Srrh 	/* dirs[nd++] = "/compool"; */
1085*7978Srrh 	dirs[nd++] = 0;
1086*7978Srrh 	defloc=ppsym("define");
1087*7978Srrh 	udfloc=ppsym("undef");
1088*7978Srrh 	incloc=ppsym("include");
1089*7978Srrh 	elsloc=ppsym("else");
1090*7978Srrh 	eifloc=ppsym("endif");
1091*7978Srrh 	ifdloc=ppsym("ifdef");
1092*7978Srrh 	ifnloc=ppsym("ifndef");
1093*7978Srrh 	ifloc=ppsym("if");
1094*7978Srrh 	lneloc=ppsym("line");
1095*7978Srrh 	for (i=sizeof(macbit)/sizeof(macbit[0]); --i>=0; ) macbit[i]=0;
1096*7978Srrh # if unix
1097*7978Srrh 	ysysloc=stsym("unix");
1098*7978Srrh # endif
1099*7978Srrh # if gcos
1100*7978Srrh 	ysysloc=stsym ("gcos");
1101*7978Srrh # endif
1102*7978Srrh # if ibm
1103*7978Srrh 	ysysloc=stsym ("ibm");
1104*7978Srrh # endif
1105*7978Srrh # if pdp11
1106*7978Srrh 	varloc=stsym("pdp11");
1107*7978Srrh # endif
1108*7978Srrh # if vax
1109*7978Srrh 	varloc=stsym("vax");
1110*7978Srrh # endif
1111*7978Srrh # if interdata
1112*7978Srrh 	varloc=stsym ("interdata");
1113*7978Srrh # endif
1114*7978Srrh # if tss
1115*7978Srrh 	varloc=stsym ("tss");
1116*7978Srrh # endif
1117*7978Srrh # if os
1118*7978Srrh 	varloc=stsym ("os");
1119*7978Srrh # endif
1120*7978Srrh # if mert
1121*7978Srrh 	varloc=stsym ("mert");
1122*7978Srrh # endif
1123*7978Srrh 	ulnloc=stsym ("__LINE__");
1124*7978Srrh 	uflloc=stsym ("__FILE__");
1125*7978Srrh 
1126*7978Srrh 	tf=fnames[ifno]; fnames[ifno]="command line"; lineno[ifno]=1;
1127*7978Srrh 	cp2=prespc;
1128*7978Srrh 	while (cp2<predef) stsym(*cp2++);
1129*7978Srrh 	cp2=punspc;
1130*7978Srrh 	while (cp2<prund) {
1131*7978Srrh 		if (p=strdex(*cp2, '=')) *p++='\0';
1132*7978Srrh 		lookup(*cp2++, DROP);
1133*7978Srrh 	}
1134*7978Srrh 	fnames[ifno]=tf;
1135*7978Srrh 	pbeg=buffer+NCPS; pbuf=pbeg+BUFSIZ; pend=pbuf+BUFSIZ;
1136*7978Srrh 
1137*7978Srrh 	trulvl = 0; flslvl = 0;
1138*7978Srrh 	lineno[0] = 1; sayline();
1139*7978Srrh 	outp=inp=pend;
1140*7978Srrh 	control(pend);
1141*7978Srrh 	return (exfail);
1142*7978Srrh }
1143