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