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