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