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