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