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