17978Srrh #ifndef lint 2*25241Smckusick static char sccsid[] = "@(#)cpp.c 1.12 10/21/85"; 37978Srrh #endif lint 47982Srrh 57978Srrh #ifdef FLEXNAMES 67978Srrh #define NCPS 128 77978Srrh #else 87978Srrh #define NCPS 8 97978Srrh #endif 107978Srrh 117978Srrh # include "stdio.h" 127982Srrh # include "ctype.h" 137978Srrh /* C command 147978Srrh /* written by John F. Reiser 157978Srrh /* July/August 1978 167978Srrh */ 177978Srrh 187978Srrh #define STATIC 197978Srrh 20*25241Smckusick #define FIRSTOPEN -2 217978Srrh #define READ 0 227978Srrh #define WRITE 1 237978Srrh #define SALT '#' 2419915Smckusick #if !defined BUFSIZ || BUFSIZ < 8192 2519915Smckusick #undef BUFSIZ 2619915Smckusick #define BUFSIZ 8192 277978Srrh #endif 287978Srrh 297978Srrh char *pbeg,*pbuf,*pend; 307978Srrh char *outp,*inp; 317978Srrh char *newp; 327978Srrh char cinit; 337978Srrh 347978Srrh /* some code depends on whether characters are sign or zero extended */ 357978Srrh /* #if '\377' < 0 not used here, old cpp doesn't understand */ 3613556Ssam #if pdp11 | vax | mc68000 377978Srrh #define COFF 128 387978Srrh #else 397978Srrh #define COFF 0 407978Srrh #endif 417978Srrh 427978Srrh # if gcos 437978Srrh #define ALFSIZ 512 /* alphabet size */ 447978Srrh # else 457978Srrh #define ALFSIZ 256 /* alphabet size */ 467978Srrh # endif 477978Srrh char macbit[ALFSIZ+11]; 487978Srrh char toktyp[ALFSIZ]; 497978Srrh #define BLANK 1 507978Srrh #define IDENT 2 517978Srrh #define NUMBR 3 527978Srrh 537978Srrh /* a superimposed code is used to reduce the number of calls to the 547978Srrh /* symbol table lookup routine. (if the kth character of an identifier 557978Srrh /* is 'a' and there are no macro names whose kth character is 'a' 567978Srrh /* then the identifier cannot be a macro name, hence there is no need 577978Srrh /* to look in the symbol table.) 'scw1' enables the test based on 587978Srrh /* single characters and their position in the identifier. 'scw2' 597978Srrh /* enables the test based on adjacent pairs of characters and their 607978Srrh /* position in the identifier. scw1 typically costs 1 indexed fetch, 617978Srrh /* an AND, and a jump per character of identifier, until the identifier 627978Srrh /* is known as a non-macro name or until the end of the identifier. 637978Srrh /* scw1 is inexpensive. scw2 typically costs 4 indexed fetches, 647978Srrh /* an add, an AND, and a jump per character of identifier, but it is also 657978Srrh /* slightly more effective at reducing symbol table searches. 667978Srrh /* scw2 usually costs too much because the symbol table search is 677978Srrh /* usually short; but if symbol table search should become expensive, 687978Srrh /* the code is here. 697978Srrh /* using both scw1 and scw2 is of dubious value. 707978Srrh */ 717978Srrh #define scw1 1 727978Srrh #define scw2 0 737978Srrh 747978Srrh #if scw2 757978Srrh char t21[ALFSIZ],t22[ALFSIZ],t23[ALFSIZ+NCPS]; 767978Srrh #endif 777978Srrh 787978Srrh #if scw1 797978Srrh #define b0 1 807978Srrh #define b1 2 817978Srrh #define b2 4 827978Srrh #define b3 8 837978Srrh #define b4 16 847978Srrh #define b5 32 857978Srrh #define b6 64 867978Srrh #define b7 128 877978Srrh #endif 887978Srrh 897978Srrh #define IB 1 907978Srrh #define SB 2 917978Srrh #define NB 4 927978Srrh #define CB 8 937978Srrh #define QB 16 947978Srrh #define WB 32 957978Srrh char fastab[ALFSIZ]; 967978Srrh char slotab[ALFSIZ]; 977978Srrh char *ptrtab; 987978Srrh #define isslo (ptrtab==(slotab+COFF)) 997978Srrh #define isid(a) ((fastab+COFF)[a]&IB) 1007978Srrh #define isspc(a) (ptrtab[a]&SB) 1017978Srrh #define isnum(a) ((fastab+COFF)[a]&NB) 1027978Srrh #define iscom(a) ((fastab+COFF)[a]&CB) 1037978Srrh #define isquo(a) ((fastab+COFF)[a]&QB) 1047978Srrh #define iswarn(a) ((fastab+COFF)[a]&WB) 1057978Srrh 1067978Srrh #define eob(a) ((a)>=pend) 1077978Srrh #define bob(a) (pbeg>=(a)) 1087978Srrh 10913556Ssam # define cputc(a,b) if(!flslvl) putc(a,b) 11013556Ssam 1117978Srrh char buffer[NCPS+BUFSIZ+BUFSIZ+NCPS]; 1127978Srrh 11324202Smckusick char *lastcopy; 1147978Srrh 11524202Smckusick char *malloc(), *realloc(); 11624202Smckusick 1177978Srrh # define DROP 0xFE /* special character not legal ASCII or EBCDIC */ 1187978Srrh # define WARN DROP 1197978Srrh # define SAME 0 1207978Srrh # define MAXINC 10 1217978Srrh # define MAXFRE 14 /* max buffers of macro pushback */ 1227978Srrh # define MAXFRM 31 /* max number of formals/actuals to a macro */ 1237978Srrh 1247978Srrh static char warnc = WARN; 1257978Srrh 1267978Srrh int mactop,fretop; 1277978Srrh char *instack[MAXFRE],*bufstack[MAXFRE],*endbuf[MAXFRE]; 1287978Srrh 1297978Srrh int plvl; /* parenthesis level during scan for macro actuals */ 1307978Srrh int maclin; /* line number of macro call requiring actuals */ 1317978Srrh char *macfil; /* file name of macro call requiring actuals */ 1327978Srrh char *macnam; /* name of macro requiring actuals */ 1337978Srrh int maclvl; /* # calls since last decrease in nesting level */ 1347978Srrh char *macforw; /* pointer which must be exceeded to decrease nesting level */ 1357978Srrh int macdam; /* offset to macforw due to buffer shifting */ 1367978Srrh 1377978Srrh #if tgp 1387978Srrh int tgpscan; /* flag for dump(); */ 1397978Srrh #endif 1407978Srrh 1417978Srrh STATIC int inctop[MAXINC]; 1427978Srrh STATIC char *fnames[MAXINC]; 1437978Srrh STATIC char *dirnams[MAXINC]; /* actual directory of #include files */ 1447978Srrh STATIC int fins[MAXINC]; 1457978Srrh STATIC int lineno[MAXINC]; 1467978Srrh 1477978Srrh STATIC char *dirs[10]; /* -I and <> directories */ 1487978Srrh char *strdex(), *copy(), *subst(), *trmdir(); 1497978Srrh struct symtab *stsym(); 150*25241Smckusick STATIC int fin = FIRSTOPEN; 1517978Srrh STATIC FILE *fout = stdout; 1527978Srrh STATIC int nd = 1; 1537978Srrh STATIC int pflag; /* don't put out lines "# 12 foo.c" */ 15413556Ssam int passcom; /* don't delete comments */ 1557978Srrh STATIC int rflag; /* allow macro recursion */ 15616235Smckusick STATIC int mflag; /* generate makefile dependencies */ 15716235Smckusick STATIC char *infile; /* name of .o file to build dependencies from */ 15816235Smckusick STATIC FILE *mout; /* file to place dependencies on */ 15916235Smckusick #define START 1 16016235Smckusick #define CONT 2 16116235Smckusick #define BACK 3 1627978Srrh STATIC int ifno; 1637978Srrh # define NPREDEF 20 1647978Srrh STATIC char *prespc[NPREDEF]; 1657978Srrh STATIC char **predef = prespc; 1667978Srrh STATIC char *punspc[NPREDEF]; 1677978Srrh STATIC char **prund = punspc; 1687978Srrh STATIC int exfail; 1697978Srrh struct symtab { 1707978Srrh char *name; 1717978Srrh char *value; 1727978Srrh } *lastsym, *lookup(), *slookup(); 1737978Srrh 1747978Srrh # if gcos 1757978Srrh #include <setjmp.h> 1767978Srrh static jmp_buf env; 1777978Srrh # define main mainpp 1787978Srrh # undef exit 1797978Srrh # define exit(S) longjmp(env, 1) 1807978Srrh # define open(S,D) fileno(fopen(S, "r")) 1817978Srrh # define close(F) fclose(_f[F]) 1827978Srrh extern FILE *_f[]; 1837978Srrh # define symsiz 500 1847978Srrh # else 18513556Ssam # define symsiz 2000 /* std = 500, wnj aug 1979 */ 1867978Srrh # endif 1877978Srrh STATIC struct symtab stab[symsiz]; 1887978Srrh 1897978Srrh STATIC struct symtab *defloc; 1907978Srrh STATIC struct symtab *udfloc; 1917978Srrh STATIC struct symtab *incloc; 1927978Srrh STATIC struct symtab *ifloc; 1937978Srrh STATIC struct symtab *elsloc; 1947978Srrh STATIC struct symtab *eifloc; 1957978Srrh STATIC struct symtab *ifdloc; 1967978Srrh STATIC struct symtab *ifnloc; 1977978Srrh STATIC struct symtab *ysysloc; 1987978Srrh STATIC struct symtab *varloc; 1997978Srrh STATIC struct symtab *lneloc; 2007978Srrh STATIC struct symtab *ulnloc; 2017978Srrh STATIC struct symtab *uflloc; 20224202Smckusick STATIC struct symtab *identloc; /* Sys 5r3 compatibility */ 2037978Srrh STATIC int trulvl; 2047978Srrh STATIC int flslvl; 2057978Srrh 20616235Smckusick sayline(where) 20716235Smckusick int where; 20816235Smckusick { 20916235Smckusick if (mflag && where==START) fprintf(mout, "%s: %s\n", infile, fnames[ifno]); 2107978Srrh if (pflag==0) fprintf(fout,"# %d \"%s\"\n", lineno[ifno], fnames[ifno]); 2117978Srrh } 2127978Srrh 2137978Srrh /* data structure guide 2147978Srrh /* 2157978Srrh /* most of the scanning takes place in the buffer: 2167978Srrh /* 2177978Srrh /* (low address) (high address) 2187978Srrh /* pbeg pbuf pend 2197978Srrh /* | <-- BUFSIZ chars --> | <-- BUFSIZ chars --> | 2207978Srrh /* _______________________________________________________________________ 2217978Srrh /* |_______________________________________________________________________| 2227978Srrh /* | | | 2237978Srrh /* |<-- waiting -->| |<-- waiting --> 2247978Srrh /* | to be |<-- current -->| to be 2257978Srrh /* | written | token | scanned 2267978Srrh /* | | | 2277978Srrh /* outp inp p 2287978Srrh /* 2297978Srrh /* *outp first char not yet written to output file 2307978Srrh /* *inp first char of current token 2317978Srrh /* *p first char not yet scanned 2327978Srrh /* 2337978Srrh /* macro expansion: write from *outp to *inp (chars waiting to be written), 2347978Srrh /* ignore from *inp to *p (chars of the macro call), place generated 2357978Srrh /* characters in front of *p (in reverse order), update pointers, 2367978Srrh /* resume scanning. 2377978Srrh /* 2387978Srrh /* symbol table pointers point to just beyond the end of macro definitions; 2397978Srrh /* the first preceding character is the number of formal parameters. 2407978Srrh /* the appearance of a formal in the body of a definition is marked by 2417978Srrh /* 2 chars: the char WARN, and a char containing the parameter number. 2427978Srrh /* the first char of a definition is preceded by a zero character. 2437978Srrh /* 2447978Srrh /* when macro expansion attempts to back up over the beginning of the 2457978Srrh /* buffer, some characters preceding *pend are saved in a side buffer, 2467978Srrh /* the address of the side buffer is put on 'instack', and the rest 2477978Srrh /* of the main buffer is moved to the right. the end of the saved buffer 2487978Srrh /* is kept in 'endbuf' since there may be nulls in the saved buffer. 2497978Srrh /* 2507978Srrh /* similar action is taken when an 'include' statement is processed, 2517978Srrh /* except that the main buffer must be completely emptied. the array 2527978Srrh /* element 'inctop[ifno]' records the last side buffer saved when 2537978Srrh /* file 'ifno' was included. these buffers remain dormant while 2547978Srrh /* the file is being read, and are reactivated at end-of-file. 2557978Srrh /* 2567978Srrh /* instack[0 : mactop] holds the addresses of all pending side buffers. 2577978Srrh /* instack[inctop[ifno]+1 : mactop-1] holds the addresses of the side 2587978Srrh /* buffers which are "live"; the side buffers instack[0 : inctop[ifno]] 2597978Srrh /* are dormant, waiting for end-of-file on the current file. 2607978Srrh /* 26124202Smckusick /* space for side buffers is obtained from 'malloc' and is never returned. 2627978Srrh /* bufstack[0:fretop-1] holds addresses of side buffers which 2637978Srrh /* are available for use. 2647978Srrh */ 2657978Srrh 2667978Srrh dump() { 2677978Srrh /* write part of buffer which lies between outp and inp . 2687978Srrh /* this should be a direct call to 'write', but the system slows to a crawl 2697978Srrh /* if it has to do an unaligned copy. thus we buffer. this silly loop 2707978Srrh /* is 15% of the total time, thus even the 'putc' macro is too slow. 2717978Srrh */ 2727978Srrh register char *p1,*p2; register FILE *f; 2737978Srrh if ((p1=outp)==inp || flslvl!=0) return; 2747978Srrh #if tgp 2757978Srrh #define MAXOUT 80 2767978Srrh if (!tgpscan) {/* scan again to insure <= MAXOUT chars between linefeeds */ 2777978Srrh register char c,*pblank; char savc,stopc,brk; 2787978Srrh tgpscan=1; brk=stopc=pblank=0; p2=inp; savc= *p2; *p2='\0'; 2797978Srrh while (c= *p1++) { 2807978Srrh if (c=='\\') c= *p1++; 2817978Srrh if (stopc==c) stopc=0; 2827978Srrh else if (c=='"' || c=='\'') stopc=c; 2837978Srrh if (p1-outp>MAXOUT && pblank!=0) { 2847978Srrh *pblank++='\n'; inp=pblank; dump(); brk=1; pblank=0; 2857978Srrh } 2867978Srrh if (c==' ' && stopc==0) pblank=p1-1; 2877978Srrh } 28816235Smckusick if (brk) sayline(CONT); 2897978Srrh *p2=savc; inp=p2; p1=outp; tgpscan=0; 2907978Srrh } 2917978Srrh #endif 2927978Srrh f=fout; 2937978Srrh # if gcos 2947978Srrh /* filter out "$ program c" card if first line of input */ 2957978Srrh /* gmatch is a simple pattern matcher in the GCOS Standard Library */ 2967978Srrh { static int gmfirst = 0; 2977978Srrh if (!gmfirst) { 2987978Srrh ++gmfirst; 2997978Srrh if (gmatch(p1, "^$*program[ \t]*c*")) 3007978Srrh p1 = strdex(p1, '\n'); 3017978Srrh } 3027978Srrh } 3037978Srrh # endif 3047978Srrh while (p1<inp) putc(*p1++,f); 3057978Srrh outp=p1; 3067978Srrh } 3077978Srrh 3087978Srrh char * 3097978Srrh refill(p) register char *p; { 3107978Srrh /* dump buffer. save chars from inp to p. read into buffer at pbuf, 3117978Srrh /* contiguous with p. update pointers, return new p. 3127978Srrh */ 3137978Srrh register char *np,*op; register int ninbuf; 3147978Srrh dump(); np=pbuf-(p-inp); op=inp; 3157978Srrh if (bob(np+1)) {pperror("token too long"); np=pbeg; p=inp+BUFSIZ;} 3167978Srrh macdam += np-inp; outp=inp=np; 3177978Srrh while (op<p) *np++= *op++; 3187978Srrh p=np; 3197978Srrh for (;;) { 3207978Srrh if (mactop>inctop[ifno]) {/* retrieve hunk of pushed-back macro text */ 3217978Srrh op=instack[--mactop]; np=pbuf; 3227978Srrh do {while (*np++= *op++);} while (op<endbuf[mactop]); pend=np-1; 3237978Srrh /* make buffer space avail for 'include' processing */ 3247978Srrh if (fretop<MAXFRE) bufstack[fretop++]=instack[mactop]; 3257978Srrh return(p); 3267978Srrh } else {/* get more text from file(s) */ 3277978Srrh maclvl=0; 3287978Srrh if (0<(ninbuf=read(fin,pbuf,BUFSIZ))) { 3297978Srrh pend=pbuf+ninbuf; *pend='\0'; 3307978Srrh return(p); 3317978Srrh } 3327978Srrh /* end of #include file */ 3337978Srrh if (ifno==0) {/* end of input */ 3347978Srrh if (plvl!=0) { 3357978Srrh int n=plvl,tlin=lineno[ifno]; char *tfil=fnames[ifno]; 3367978Srrh lineno[ifno]=maclin; fnames[ifno]=macfil; 3377978Srrh pperror("%s: unterminated macro call",macnam); 3387978Srrh lineno[ifno]=tlin; fnames[ifno]=tfil; 3397978Srrh np=p; *np++='\n'; /* shut off unterminated quoted string */ 3407978Srrh while (--n>=0) *np++=')'; /* supply missing parens */ 3417978Srrh pend=np; *np='\0'; if (plvl<0) plvl=0; 3427978Srrh return(p); 3437978Srrh } 34412979Ssam if (trulvl || flslvl) 34512979Ssam pperror("missing endif"); 3467978Srrh inp=p; dump(); exit(exfail); 3477978Srrh } 34816235Smckusick close(fin); fin=fins[--ifno]; dirs[0]=dirnams[ifno]; sayline(BACK); 3497978Srrh } 3507978Srrh } 3517978Srrh } 3527978Srrh 3537978Srrh #define BEG 0 3547978Srrh #define LF 1 3557978Srrh 3567978Srrh char * 3577978Srrh cotoken(p) register char *p; { 3587978Srrh register int c,i; char quoc; 3597978Srrh static int state = BEG; 3607978Srrh 3617978Srrh if (state!=BEG) goto prevlf; 3627978Srrh for (;;) { 3637978Srrh again: 3647978Srrh while (!isspc(*p++)); 3657978Srrh switch (*(inp=p-1)) { 3667978Srrh case 0: { 3677978Srrh if (eob(--p)) {p=refill(p); goto again;} 3687978Srrh else ++p; /* ignore null byte */ 3697978Srrh } break; 3707978Srrh case '|': case '&': for (;;) {/* sloscan only */ 3717978Srrh if (*p++== *inp) break; 3727978Srrh if (eob(--p)) p=refill(p); 3737978Srrh else break; 3747978Srrh } break; 3757978Srrh case '=': case '!': for (;;) {/* sloscan only */ 3767978Srrh if (*p++=='=') break; 3777978Srrh if (eob(--p)) p=refill(p); 3787978Srrh else break; 3797978Srrh } break; 3807978Srrh case '<': case '>': for (;;) {/* sloscan only */ 3817978Srrh if (*p++=='=' || p[-2]==p[-1]) break; 3827978Srrh if (eob(--p)) p=refill(p); 3837978Srrh else break; 3847978Srrh } break; 3857978Srrh case '\\': for (;;) { 3867978Srrh if (*p++=='\n') {++lineno[ifno]; break;} 3877978Srrh if (eob(--p)) p=refill(p); 3887978Srrh else {++p; break;} 3897978Srrh } break; 3907978Srrh case '/': for (;;) { 3917978Srrh if (*p++=='*') {/* comment */ 3927978Srrh if (!passcom) {inp=p-2; dump(); ++flslvl;} 3937978Srrh for (;;) { 3947978Srrh while (!iscom(*p++)); 3957978Srrh if (p[-1]=='*') for (;;) { 3967978Srrh if (*p++=='/') goto endcom; 3977978Srrh if (eob(--p)) { 3987978Srrh if (!passcom) {inp=p; p=refill(p);} 3997978Srrh else if ((p-inp)>=BUFSIZ) {/* split long comment */ 4007978Srrh inp=p; p=refill(p); /* last char written is '*' */ 40113556Ssam cputc('/',fout); /* terminate first part */ 4027978Srrh /* and fake start of 2nd */ 4037978Srrh outp=inp=p-=3; *p++='/'; *p++='*'; *p++='*'; 4047978Srrh } else p=refill(p); 4057978Srrh } else break; 4067978Srrh } else if (p[-1]=='\n') { 4077978Srrh ++lineno[ifno]; if (!passcom) putc('\n',fout); 4087978Srrh } else if (eob(--p)) { 4097978Srrh if (!passcom) {inp=p; p=refill(p);} 4107978Srrh else if ((p-inp)>=BUFSIZ) {/* split long comment */ 4117978Srrh inp=p; p=refill(p); 41213556Ssam cputc('*',fout); cputc('/',fout); 4137978Srrh outp=inp=p-=2; *p++='/'; *p++='*'; 4147978Srrh } else p=refill(p); 4157978Srrh } else ++p; /* ignore null byte */ 4167978Srrh } 4177978Srrh endcom: 4187978Srrh if (!passcom) {outp=inp=p; --flslvl; goto again;} 4197978Srrh break; 4207978Srrh } 4217978Srrh if (eob(--p)) p=refill(p); 4227978Srrh else break; 4237978Srrh } break; 4247978Srrh # if gcos 4257978Srrh case '`': 4267978Srrh # endif 4277978Srrh case '"': case '\'': { 4287978Srrh quoc=p[-1]; 4297978Srrh for (;;) { 4307978Srrh while (!isquo(*p++)); 4317978Srrh if (p[-1]==quoc) break; 4327978Srrh if (p[-1]=='\n') {--p; break;} /* bare \n terminates quotation */ 4337978Srrh if (p[-1]=='\\') for (;;) { 4347978Srrh if (*p++=='\n') {++lineno[ifno]; break;} /* escaped \n ignored */ 4357978Srrh if (eob(--p)) p=refill(p); 4367978Srrh else {++p; break;} 4377978Srrh } else if (eob(--p)) p=refill(p); 4387978Srrh else ++p; /* it was a different quote character */ 4397978Srrh } 4407978Srrh } break; 4417978Srrh case '\n': { 4427978Srrh ++lineno[ifno]; if (isslo) {state=LF; return(p);} 4437978Srrh prevlf: 4447978Srrh state=BEG; 4457978Srrh for (;;) { 4467978Srrh if (*p++=='#') return(p); 4477978Srrh if (eob(inp= --p)) p=refill(p); 4487978Srrh else goto again; 4497978Srrh } 4507978Srrh } break; 4517978Srrh case '0': case '1': case '2': case '3': case '4': 4527978Srrh case '5': case '6': case '7': case '8': case '9': 4537978Srrh for (;;) { 4547978Srrh while (isnum(*p++)); 4557978Srrh if (eob(--p)) p=refill(p); 4567978Srrh else break; 4577978Srrh } break; 4587978Srrh case 'A': case 'B': case 'C': case 'D': case 'E': 4597978Srrh case 'F': case 'G': case 'H': case 'I': case 'J': 4607978Srrh case 'K': case 'L': case 'M': case 'N': case 'O': 4617978Srrh case 'P': case 'Q': case 'R': case 'S': case 'T': 4627978Srrh case 'U': case 'V': case 'W': case 'X': case 'Y': 4637978Srrh case 'Z': case '_': 4647978Srrh case 'a': case 'b': case 'c': case 'd': case 'e': 4657978Srrh case 'f': case 'g': case 'h': case 'i': case 'j': 4667978Srrh case 'k': case 'l': case 'm': case 'n': case 'o': 4677978Srrh case 'p': case 'q': case 'r': case 's': case 't': 4687978Srrh case 'u': case 'v': case 'w': case 'x': case 'y': 4697978Srrh case 'z': 4707978Srrh #if scw1 4717978Srrh #define tmac1(c,bit) if (!xmac1(c,bit,&)) goto nomac 4727978Srrh #define xmac1(c,bit,op) ((macbit+COFF)[c] op (bit)) 4737978Srrh #else 4747978Srrh #define tmac1(c,bit) 4757978Srrh #define xmac1(c,bit,op) 4767978Srrh #endif 4777978Srrh 4787978Srrh #if scw2 4797978Srrh #define tmac2(c0,c1,cpos) if (!xmac2(c0,c1,cpos,&)) goto nomac 4807978Srrh #define xmac2(c0,c1,cpos,op)\ 4817978Srrh ((macbit+COFF)[(t21+COFF)[c0]+(t22+COFF)[c1]] op (t23+COFF+cpos)[c0]) 4827978Srrh #else 4837978Srrh #define tmac2(c0,c1,cpos) 4847978Srrh #define xmac2(c0,c1,cpos,op) 4857978Srrh #endif 4867978Srrh 4877978Srrh if (flslvl) goto nomac; 4887978Srrh for (;;) { 4897978Srrh c= p[-1]; tmac1(c,b0); 4907978Srrh i= *p++; if (!isid(i)) goto endid; tmac1(i,b1); tmac2(c,i,0); 4917978Srrh c= *p++; if (!isid(c)) goto endid; tmac1(c,b2); tmac2(i,c,1); 4927978Srrh i= *p++; if (!isid(i)) goto endid; tmac1(i,b3); tmac2(c,i,2); 4937978Srrh c= *p++; if (!isid(c)) goto endid; tmac1(c,b4); tmac2(i,c,3); 4947978Srrh i= *p++; if (!isid(i)) goto endid; tmac1(i,b5); tmac2(c,i,4); 4957978Srrh c= *p++; if (!isid(c)) goto endid; tmac1(c,b6); tmac2(i,c,5); 4967978Srrh i= *p++; if (!isid(i)) goto endid; tmac1(i,b7); tmac2(c,i,6); 4977978Srrh tmac2(i,0,7); 4987978Srrh while (isid(*p++)); 4997978Srrh if (eob(--p)) {refill(p); p=inp+1; continue;} 5007978Srrh goto lokid; 5017978Srrh endid: 5027978Srrh if (eob(--p)) {refill(p); p=inp+1; continue;} 5037978Srrh tmac2(p[-1],0,-1+(p-inp)); 5047978Srrh lokid: 5057978Srrh slookup(inp,p,0); if (newp) {p=newp; goto again;} 5067978Srrh else break; 5077978Srrh nomac: 5087978Srrh while (isid(*p++)); 5097978Srrh if (eob(--p)) {p=refill(p); goto nomac;} 5107978Srrh else break; 5117978Srrh } break; 5127978Srrh } /* end of switch */ 5137978Srrh 5147978Srrh if (isslo) return(p); 5157978Srrh } /* end of infinite loop */ 5167978Srrh } 5177978Srrh 5187978Srrh char * 5197978Srrh skipbl(p) register char *p; {/* get next non-blank token */ 5207978Srrh do {outp=inp=p; p=cotoken(p);} while ((toktyp+COFF)[*inp]==BLANK); 5217978Srrh return(p); 5227978Srrh } 5237978Srrh 5247978Srrh char * 5257978Srrh unfill(p) register char *p; { 5267978Srrh /* take <= BUFSIZ chars from right end of buffer and put them on instack . 5277978Srrh /* slide rest of buffer to the right, update pointers, return new p. 5287978Srrh */ 5297978Srrh register char *np,*op; register int d; 5307978Srrh if (mactop>=MAXFRE) { 5317978Srrh pperror("%s: too much pushback",macnam); 5327978Srrh p=inp=pend; dump(); /* begin flushing pushback */ 5337978Srrh while (mactop>inctop[ifno]) {p=refill(p); p=inp=pend; dump();} 5347978Srrh } 5357978Srrh if (fretop>0) np=bufstack[--fretop]; 5367978Srrh else { 53724202Smckusick np=malloc(BUFSIZ+1); 53824202Smckusick if (np==NULL) {pperror("no space"); exit(exfail);} 53924202Smckusick np[BUFSIZ]='\0'; 5407978Srrh } 5417978Srrh instack[mactop]=np; op=pend-BUFSIZ; if (op<p) op=p; 5427978Srrh for (;;) {while (*np++= *op++); if (eob(op)) break;} /* out with old */ 5437978Srrh endbuf[mactop++]=np; /* mark end of saved text */ 5447978Srrh np=pbuf+BUFSIZ; op=pend-BUFSIZ; pend=np; if (op<p) op=p; 5457978Srrh while (outp<op) *--np= *--op; /* slide over new */ 5467978Srrh if (bob(np)) pperror("token too long"); 5477978Srrh d=np-outp; outp+=d; inp+=d; macdam+=d; return(p+d); 5487978Srrh } 5497978Srrh 5507978Srrh char * 5517978Srrh doincl(p) register char *p; { 5527978Srrh int filok,inctype; 5537978Srrh register char *cp; char **dirp,*nfil; char filname[BUFSIZ]; 5547978Srrh 5557978Srrh p=skipbl(p); cp=filname; 5567978Srrh if (*inp++=='<') {/* special <> syntax */ 5577978Srrh inctype=1; 5587978Srrh ++flslvl; /* prevent macro expansion */ 5597978Srrh for (;;) { 5607978Srrh outp=inp=p; p=cotoken(p); 5617978Srrh if (*inp=='\n') {--p; *cp='\0'; break;} 5627978Srrh if (*inp=='>') { *cp='\0'; break;} 5637978Srrh # ifdef gimpel 5647978Srrh if (*inp=='.' && !intss()) *inp='#'; 5657978Srrh # endif 5667978Srrh while (inp<p) *cp++= *inp++; 5677978Srrh } 5687978Srrh --flslvl; /* reenable macro expansion */ 5697978Srrh } else if (inp[-1]=='"') {/* regular "" syntax */ 5707978Srrh inctype=0; 5717978Srrh # ifdef gimpel 5727978Srrh while (inp<p) {if (*inp=='.' && !intss()) *inp='#'; *cp++= *inp++;} 5737978Srrh # else 5747978Srrh while (inp<p) *cp++= *inp++; 5757978Srrh # endif 5767978Srrh if (*--cp=='"') *cp='\0'; 5777978Srrh } else {pperror("bad include syntax",0); inctype=2;} 5787978Srrh /* flush current file to \n , then write \n */ 5797978Srrh ++flslvl; do {outp=inp=p; p=cotoken(p);} while (*inp!='\n'); --flslvl; 5807978Srrh inp=p; dump(); if (inctype==2) return(p); 5817978Srrh /* look for included file */ 5827978Srrh if (ifno+1 >=MAXINC) { 5837978Srrh pperror("Unreasonable include nesting",0); return(p); 5847978Srrh } 58524202Smckusick if((nfil=malloc(BUFSIZ))==NULL) {pperror("no space"); exit(exfail);} 5867978Srrh filok=0; 5877978Srrh for (dirp=dirs+inctype; *dirp; ++dirp) { 5887978Srrh if ( 5897978Srrh # if gcos 5907978Srrh strdex(filname, '/') 5917978Srrh # else 5927978Srrh filname[0]=='/' 5937978Srrh # endif 5947978Srrh || **dirp=='\0') strcpy(nfil,filname); 5957978Srrh else { 5967978Srrh strcpy(nfil,*dirp); 5977978Srrh # if unix || gcos 5987978Srrh strcat(nfil,"/"); 5997978Srrh # endif 6007978Srrh #ifdef ibm 6017978Srrh #ifndef gimpel 6027978Srrh strcat(nfil,"."); 6037978Srrh #endif 6047978Srrh #endif 6057978Srrh strcat(nfil,filname); 6067978Srrh } 6077978Srrh if (0<(fins[ifno+1]=open(nfil,READ))) { 6087978Srrh filok=1; fin=fins[++ifno]; break; 6097978Srrh } 6107978Srrh } 61124202Smckusick if(filok==0){pperror("Can't find include file %s",filname);free(nfil);} 6127978Srrh else { 61324202Smckusick nfil=realloc(nfil,strlen(nfil)+1); 61424202Smckusick lineno[ifno]=1; fnames[ifno]=nfil; 6157978Srrh dirnams[ifno]=dirs[0]=trmdir(copy(nfil)); 61616235Smckusick sayline(START); 6177978Srrh /* save current contents of buffer */ 6187978Srrh while (!eob(p)) p=unfill(p); 6197978Srrh inctop[ifno]=mactop; 6207978Srrh } 6217978Srrh return(p); 6227978Srrh } 6237978Srrh 6247978Srrh equfrm(a,p1,p2) register char *a,*p1,*p2; { 6257978Srrh register char c; int flag; 6267978Srrh c= *p2; *p2='\0'; 6277978Srrh flag=strcmp(a,p1); *p2=c; return(flag==SAME); 6287978Srrh } 6297978Srrh 6307978Srrh char * 6317978Srrh dodef(p) char *p; {/* process '#define' */ 6327978Srrh register char *pin,*psav,*cf; 6337978Srrh char **pf,**qf; int b,c,params; struct symtab *np; 63424202Smckusick char *oldval; 63524202Smckusick char *space, *newspace; 6367978Srrh char *formal[MAXFRM]; /* formal[n] is name of nth formal */ 6377978Srrh char formtxt[BUFSIZ]; /* space for formal names */ 6387978Srrh 6397978Srrh ++flslvl; /* prevent macro expansion during 'define' */ 6407978Srrh p=skipbl(p); pin=inp; 6417978Srrh if ((toktyp+COFF)[*pin]!=IDENT) { 6427978Srrh ppwarn("illegal macro name"); while (*inp!='\n') p=skipbl(p); return(p); 6437978Srrh } 6447978Srrh np=slookup(pin,p,1); 64524202Smckusick if (oldval=np->value) free(lastcopy); /* was previously defined */ 6467978Srrh b=1; cf=pin; 6477978Srrh while (cf<p) {/* update macbit */ 6487978Srrh c= *cf++; xmac1(c,b,|=); b=(b+b)&0xFF; 6497978Srrh if (cf!=p) xmac2(c,*cf,-1+(cf-pin),|=); 6507978Srrh else xmac2(c,0,-1+(cf-pin),|=); 6517978Srrh } 6527978Srrh params=0; outp=inp=p; p=cotoken(p); pin=inp; 6537978Srrh if (*pin=='(') {/* with parameters; identify the formals */ 6547978Srrh cf=formtxt; pf=formal; 6557978Srrh for (;;) { 6567978Srrh p=skipbl(p); pin=inp; 6577978Srrh if (*pin=='\n') { 6587978Srrh --lineno[ifno]; --p; pperror("%s: missing )",np->name); break; 6597978Srrh } 6607978Srrh if (*pin==')') break; 6617978Srrh if (*pin==',') continue; 6627978Srrh if ((toktyp+COFF)[*pin]!=IDENT) { 6637978Srrh c= *p; *p='\0'; pperror("bad formal: %s",pin); *p=c; 6647978Srrh } else if (pf>= &formal[MAXFRM]) { 6657978Srrh c= *p; *p='\0'; pperror("too many formals: %s",pin); *p=c; 6667978Srrh } else { 6677978Srrh *pf++=cf; while (pin<p) *cf++= *pin++; *cf++='\0'; ++params; 6687978Srrh } 6697978Srrh } 6707978Srrh if (params==0) --params; /* #define foo() ... */ 6717978Srrh } else if (*pin=='\n') {--lineno[ifno]; --p;} 6727978Srrh /* remember beginning of macro body, so that we can 6737978Srrh /* warn if a redefinition is different from old value. 6747978Srrh */ 67524202Smckusick space=psav=malloc(BUFSIZ); 67624202Smckusick if (space==NULL) {pperror("too much defining"); return(p);} 67724202Smckusick *psav++ = '\0'; 6787978Srrh for (;;) {/* accumulate definition until linefeed */ 6797978Srrh outp=inp=p; p=cotoken(p); pin=inp; 6807978Srrh if (*pin=='\\' && pin[1]=='\n') {putc('\n',fout); continue;} /* ignore escaped lf */ 6817978Srrh if (*pin=='\n') break; 6827978Srrh if (params) {/* mark the appearance of formals in the definiton */ 6837978Srrh if ((toktyp+COFF)[*pin]==IDENT) { 6847978Srrh for (qf=pf; --qf>=formal; ) { 6857978Srrh if (equfrm(*qf,pin,p)) { 6867978Srrh *psav++=qf-formal+1; *psav++=WARN; pin=p; break; 6877978Srrh } 6887978Srrh } 6897978Srrh } else if (*pin=='"' || *pin=='\'' 6907978Srrh # if gcos 6917978Srrh || *pin=='`' 6927978Srrh # endif 6937978Srrh ) {/* inside quotation marks, too */ 6947978Srrh char quoc= *pin; 6957978Srrh for (*psav++= *pin++; pin<p && *pin!=quoc; ) { 6967978Srrh while (pin<p && !isid(*pin)) *psav++= *pin++; 6977978Srrh cf=pin; while (cf<p && isid(*cf)) ++cf; 6987978Srrh for (qf=pf; --qf>=formal; ) { 6997978Srrh if (equfrm(*qf,pin,cf)) { 7007978Srrh *psav++=qf-formal+1; *psav++=WARN; pin=cf; break; 7017978Srrh } 7027978Srrh } 7037978Srrh while (pin<cf) *psav++= *pin++; 7047978Srrh } 7057978Srrh } 7067978Srrh } 7077978Srrh while (pin<p) *psav++= *pin++; 7087978Srrh } 7097978Srrh *psav++=params; *psav++='\0'; 7107978Srrh if ((cf=oldval)!=NULL) {/* redefinition */ 7117978Srrh --cf; /* skip no. of params, which may be zero */ 7127978Srrh while (*--cf); /* go back to the beginning */ 71324202Smckusick if (0!=strcmp(++cf,space+1)) {/* redefinition different from old */ 7147978Srrh --lineno[ifno]; ppwarn("%s redefined",np->name); ++lineno[ifno]; 7157978Srrh np->value=psav-1; 71624202Smckusick } else free(space); /* identical redef.; reclaim space */ 7177978Srrh } else np->value=psav-1; 71824202Smckusick --flslvl; inp=pin; 71924202Smckusick if (np->value == psav-1) { 72024202Smckusick newspace = realloc(space, psav-space); 72124202Smckusick if (newspace==NULL) {pperror("no space"); exit(exfail);} 72224202Smckusick /* 72324202Smckusick * Adjust pointer in case this moved. 72424202Smckusick */ 72524202Smckusick np->value += newspace-space; 72624202Smckusick } 72724202Smckusick return(p); 7287978Srrh } 7297978Srrh 7307978Srrh #define fasscan() ptrtab=fastab+COFF 7317978Srrh #define sloscan() ptrtab=slotab+COFF 7327978Srrh 7337978Srrh char * 7347978Srrh control(p) register char *p; {/* find and handle preprocessor control lines */ 7357978Srrh register struct symtab *np; 7367978Srrh for (;;) { 7377978Srrh fasscan(); p=cotoken(p); if (*inp=='\n') ++inp; dump(); 7387978Srrh sloscan(); p=skipbl(p); 7397978Srrh *--inp=SALT; outp=inp; ++flslvl; np=slookup(inp,p,0); --flslvl; 7407978Srrh if (np==defloc) {/* define */ 7417978Srrh if (flslvl==0) {p=dodef(p); continue;} 7427978Srrh } else if (np==incloc) {/* include */ 7437978Srrh if (flslvl==0) {p=doincl(p); continue;} 7447978Srrh } else if (np==ifnloc) {/* ifndef */ 7457978Srrh ++flslvl; p=skipbl(p); np=slookup(inp,p,0); --flslvl; 7467978Srrh if (flslvl==0 && np->value==0) ++trulvl; 7477978Srrh else ++flslvl; 7487978Srrh } else if (np==ifdloc) {/* ifdef */ 7497978Srrh ++flslvl; p=skipbl(p); np=slookup(inp,p,0); --flslvl; 7507978Srrh if (flslvl==0 && np->value!=0) ++trulvl; 7517978Srrh else ++flslvl; 7527978Srrh } else if (np==eifloc) {/* endif */ 75316235Smckusick if (flslvl) {if (--flslvl==0) sayline(CONT);} 7547978Srrh else if (trulvl) --trulvl; 7557978Srrh else pperror("If-less endif",0); 7567978Srrh } else if (np==elsloc) {/* else */ 7577978Srrh if (flslvl) { 7587978Srrh if (--flslvl!=0) ++flslvl; 75916235Smckusick else {++trulvl; sayline(CONT);} 7607978Srrh } 7617978Srrh else if (trulvl) {++flslvl; --trulvl;} 7627978Srrh else pperror("If-less else",0); 7637978Srrh } else if (np==udfloc) {/* undefine */ 7647978Srrh if (flslvl==0) { 7657978Srrh ++flslvl; p=skipbl(p); slookup(inp,p,DROP); --flslvl; 7667978Srrh } 7677978Srrh } else if (np==ifloc) {/* if */ 7687978Srrh #if tgp 7697978Srrh pperror(" IF not implemented, true assumed", 0); 7707978Srrh if (flslvl==0) ++trulvl; else ++flslvl; 7717978Srrh #else 7727978Srrh newp=p; 7737978Srrh if (flslvl==0 && yyparse()) ++trulvl; else ++flslvl; 7747978Srrh p=newp; 7757978Srrh #endif 7767978Srrh } else if (np==lneloc) {/* line */ 7777978Srrh if (flslvl==0 && pflag==0) { 7787982Srrh char *cp, *cp2, *savestring(); 7797978Srrh outp=inp=p; *--outp='#'; while (*inp!='\n') p=cotoken(p); 7807982Srrh cp = outp + 1; 7817982Srrh while (isspace(*cp) && cp < inp) 7827982Srrh cp++; 7837982Srrh while (isdigit(*cp) && cp < inp) 7847982Srrh cp++; 7857982Srrh while (*cp != '"' && cp < inp) 7867982Srrh cp++; 7877982Srrh if (cp < inp) { 7887982Srrh cp++; 7897982Srrh cp2 = cp; 7907982Srrh while (*cp2 != '"' && cp2 < inp) 7917982Srrh cp2++; 7927982Srrh fnames[ifno] = savestring(cp, cp2); 7937982Srrh } 7947978Srrh continue; 7957978Srrh } 79624202Smckusick } else if (np==identloc) {/* ident (for Sys 5r3 compat) */ 79724202Smckusick while(*inp!='\n') p=cotoken(p); 7987978Srrh } else if (*++inp=='\n') outp=inp; /* allows blank line after # */ 7997978Srrh else pperror("undefined control",0); 8007978Srrh /* flush to lf */ 8017978Srrh ++flslvl; while (*inp!='\n') {outp=inp=p; p=cotoken(p);} --flslvl; 8027978Srrh } 8037978Srrh } 8047978Srrh 8057982Srrh char * 8067982Srrh savestring(start, finish) 8077982Srrh register char *start, *finish; 8087982Srrh { 8097982Srrh char *retbuf; 8107982Srrh register char *cp; 8117982Srrh 8127982Srrh retbuf = (char *) calloc(finish - start + 1, sizeof (char)); 8137982Srrh cp = retbuf; 8147982Srrh while (start < finish) 8157982Srrh *cp++ = *start++; 8167982Srrh *cp = 0; 8177982Srrh return(retbuf); 8187982Srrh } 8197982Srrh 8207978Srrh struct symtab * 8217978Srrh stsym(s) register char *s; { 8227978Srrh char buf[BUFSIZ]; register char *p; 8237978Srrh 8247978Srrh /* make definition look exactly like end of #define line */ 8257978Srrh /* copy to avoid running off end of world when param list is at end */ 8267978Srrh p=buf; while (*p++= *s++); 8277978Srrh p=buf; while (isid(*p++)); /* skip first identifier */ 8287978Srrh if (*--p=='=') {*p++=' '; while (*p++);} 8297978Srrh else {s=" 1"; while (*p++= *s++);} 8307978Srrh pend=p; *--p='\n'; 8317978Srrh sloscan(); dodef(buf); return(lastsym); 8327978Srrh } 8337978Srrh 8347978Srrh struct symtab * 8357978Srrh ppsym(s) char *s; {/* kluge */ 8367978Srrh register struct symtab *sp; 83724202Smckusick register char *name; 83824202Smckusick 83924202Smckusick cinit=SALT; sp=stsym(s); name = malloc(strlen(sp->name)+1+1); 84024202Smckusick name[0] = '#'; strcpy(name+1, sp->name); sp->name = name; 84124202Smckusick cinit=0; return(sp); 8427978Srrh } 8437978Srrh 8447978Srrh /* VARARGS1 */ 8457978Srrh pperror(s,x,y) char *s; { 8467978Srrh if (fnames[ifno][0]) fprintf(stderr, 8477978Srrh # if gcos 8487978Srrh "*%c* \"%s\", line ", exfail >= 0 ? 'F' : 'W', 8497978Srrh # else 8507978Srrh "%s: ", 8517978Srrh # endif 8527978Srrh fnames[ifno]); 8537978Srrh fprintf(stderr, "%d: ",lineno[ifno]); 8547978Srrh fprintf(stderr, s, x, y); 8557978Srrh fprintf(stderr,"\n"); 8567978Srrh ++exfail; 8577978Srrh } 8587978Srrh 8597978Srrh yyerror(s,a,b) char *s; { 8607978Srrh pperror(s,a,b); 8617978Srrh } 8627978Srrh 8637978Srrh ppwarn(s,x) char *s; { 8647978Srrh int fail = exfail; 8657978Srrh exfail = -1; 8667978Srrh pperror(s,x); 8677978Srrh exfail = fail; 8687978Srrh } 8697978Srrh 8707978Srrh struct symtab * 8717978Srrh lookup(namep, enterf) 8727978Srrh char *namep; 8737978Srrh { 8747978Srrh register char *np, *snp; 8757978Srrh register int c, i; int around; 8767978Srrh register struct symtab *sp; 8777978Srrh 8787978Srrh /* namep had better not be too long (currently, <=NCPS chars) */ 8797978Srrh np=namep; around=0; i=cinit; 8807978Srrh while (c= *np++) i += i+c; c=i; /* c=i for register usage on pdp11 */ 8817978Srrh c %= symsiz; if (c<0) c += symsiz; 8827978Srrh sp = &stab[c]; 8837978Srrh while (snp=sp->name) { 8847978Srrh np = namep; 8857978Srrh while (*snp++ == *np) if (*np++ == '\0') { 8867978Srrh if (enterf==DROP) {sp->name[0]= DROP; sp->value=0;} 8877978Srrh return(lastsym=sp); 8887978Srrh } 8897978Srrh if (--sp < &stab[0]) 8907978Srrh if (around) {pperror("too many defines", 0); exit(exfail);} 8917978Srrh else {++around; sp = &stab[symsiz-1];} 8927978Srrh } 8937978Srrh if (enterf==1) sp->name=namep; 8947978Srrh return(lastsym=sp); 8957978Srrh } 8967978Srrh 8977978Srrh struct symtab * 8987978Srrh slookup(p1,p2,enterf) register char *p1,*p2; int enterf;{ 8997978Srrh register char *p3; char c2,c3; struct symtab *np; 9007978Srrh c2= *p2; *p2='\0'; /* mark end of token */ 9017978Srrh if ((p2-p1)>NCPS) p3=p1+NCPS; else p3=p2; 9027978Srrh c3= *p3; *p3='\0'; /* truncate to NCPS chars or less */ 9037978Srrh if (enterf==1) p1=copy(p1); 9047978Srrh np=lookup(p1,enterf); *p3=c3; *p2=c2; 9057978Srrh if (np->value!=0 && flslvl==0) newp=subst(p2,np); 9067978Srrh else newp=0; 9077978Srrh return(np); 9087978Srrh } 9097978Srrh 9107978Srrh char * 9117978Srrh subst(p,sp) register char *p; struct symtab *sp; { 9127978Srrh static char match[]="%s: argument mismatch"; 9137978Srrh register char *ca,*vp; int params; 91413556Ssam char *actual[MAXFRM]; /* actual[n] is text of nth actual */ 91513556Ssam char actused[MAXFRM]; /* for newline processing in actuals */ 91613556Ssam char acttxt[BUFSIZ]; /* space for actuals */ 91713556Ssam int nlines = 0; 9187978Srrh 9197978Srrh if (0==(vp=sp->value)) return(p); 9207978Srrh if ((p-macforw)<=macdam) { 9217978Srrh if (++maclvl>symsiz && !rflag) { 9227978Srrh pperror("%s: macro recursion",sp->name); return(p); 9237978Srrh } 9247978Srrh } else maclvl=0; /* level decreased */ 9257978Srrh macforw=p; macdam=0; /* new target for decrease in level */ 9267978Srrh macnam=sp->name; 9277978Srrh dump(); 9287978Srrh if (sp==ulnloc) { 9297978Srrh vp=acttxt; *vp++='\0'; 9307978Srrh sprintf(vp,"%d",lineno[ifno]); while (*vp++); 9317978Srrh } else if (sp==uflloc) { 9327978Srrh vp=acttxt; *vp++='\0'; 9337978Srrh sprintf(vp,"\"%s\"",fnames[ifno]); while (*vp++); 9347978Srrh } 9357978Srrh if (0!=(params= *--vp&0xFF)) {/* definition calls for params */ 9367978Srrh register char **pa; 9377978Srrh ca=acttxt; pa=actual; 9387978Srrh if (params==0xFF) params=1; /* #define foo() ... */ 9397978Srrh sloscan(); ++flslvl; /* no expansion during search for actuals */ 9407978Srrh plvl= -1; 9417978Srrh do p=skipbl(p); while (*inp=='\n'); /* skip \n too */ 9427978Srrh if (*inp=='(') { 9437978Srrh maclin=lineno[ifno]; macfil=fnames[ifno]; 9447978Srrh for (plvl=1; plvl!=0; ) { 9457978Srrh *ca++='\0'; 9467978Srrh for (;;) { 9477978Srrh outp=inp=p; p=cotoken(p); 9487978Srrh if (*inp=='(') ++plvl; 9497978Srrh if (*inp==')' && --plvl==0) {--params; break;} 9507978Srrh if (plvl==1 && *inp==',') {--params; break;} 9517978Srrh while (inp<p) *ca++= *inp++; 9527978Srrh if (ca> &acttxt[BUFSIZ]) 9537978Srrh pperror("%s: actuals too long",sp->name); 9547978Srrh } 9557978Srrh if (pa>= &actual[MAXFRM]) ppwarn(match,sp->name); 95613556Ssam else { actused[pa-actual]=0; *pa++=ca; } 9577978Srrh } 95813556Ssam nlines = lineno[ifno] - maclin; 95913556Ssam lineno[ifno] = maclin; /* don't count newlines here */ 9607978Srrh } 9617978Srrh if (params!=0) ppwarn(match,sp->name); 9627978Srrh while (--params>=0) *pa++=""+1; /* null string for missing actuals */ 9637978Srrh --flslvl; fasscan(); 9647978Srrh } 9657978Srrh for (;;) {/* push definition onto front of input stack */ 9667978Srrh while (!iswarn(*--vp)) { 9677978Srrh if (bob(p)) {outp=inp=p; p=unfill(p);} 9687978Srrh *--p= *vp; 9697978Srrh } 9707978Srrh if (*vp==warnc) {/* insert actual param */ 9717978Srrh ca=actual[*--vp-1]; 9727978Srrh while (*--ca) { 9737978Srrh if (bob(p)) {outp=inp=p; p=unfill(p);} 97413556Ssam /* Actuals with newlines confuse line numbering */ 97513556Ssam if (*ca == '\n' && actused[*vp-1]) 97613556Ssam if (*(ca-1) == '\\') ca--; 97713556Ssam else *--p = ' '; 97813556Ssam else { *--p= *ca; if (*ca == '\n') nlines--; } 9797978Srrh } 98013556Ssam actused[*vp-1] = 1; 98113556Ssam } else { 98213556Ssam if (nlines > 0 ) 98313556Ssam while (nlines-- > 0) 98413556Ssam *--p = '\n'; 98513556Ssam break; 98613556Ssam } 9877978Srrh } 9887978Srrh outp=inp=p; 9897978Srrh return(p); 9907978Srrh } 9917978Srrh 9927978Srrh 9937978Srrh 9947978Srrh 9957978Srrh char * 9967978Srrh trmdir(s) register char *s; { 9977978Srrh register char *p = s; 9987978Srrh while (*p++); --p; while (p>s && *--p!='/'); 9997978Srrh # if unix 10007978Srrh if (p==s) *p++='.'; 10017978Srrh # endif 10027978Srrh *p='\0'; 10037978Srrh return(s); 10047978Srrh } 10057978Srrh 10067978Srrh STATIC char * 10077978Srrh copy(s) register char *s; { 10087978Srrh register char *old; 10097978Srrh 101024202Smckusick old = malloc(strlen(s)+1); 101124202Smckusick if (old==NULL) {pperror("no space"); exit(exfail);} 101224202Smckusick strcpy(old, s); 101324202Smckusick return(lastcopy=old); 10147978Srrh } 10157978Srrh 10167978Srrh char * 10177978Srrh strdex(s,c) char *s,c; { 10187978Srrh while (*s) if (*s++==c) return(--s); 10197978Srrh return(0); 10207978Srrh } 10217978Srrh 10227978Srrh yywrap(){ return(1); } 10237978Srrh 10247978Srrh main(argc,argv) 10257978Srrh char *argv[]; 10267978Srrh { 10277978Srrh register int i,c; 10287978Srrh register char *p; 10297978Srrh char *tf,**cp2; 10307978Srrh 10317978Srrh # if gcos 10327978Srrh if (setjmp(env)) return (exfail); 10337978Srrh # endif 10347978Srrh p="_$ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; 10357978Srrh i=0; 10367978Srrh while (c= *p++) { 10377978Srrh (fastab+COFF)[c] |= IB|NB|SB; (toktyp+COFF)[c]=IDENT; 10387978Srrh #if scw2 10397978Srrh /* 53 == 63-10; digits rarely appear in identifiers, 10407978Srrh /* and can never be the first char of an identifier. 10417978Srrh /* 11 == 53*53/sizeof(macbit) . 10427978Srrh */ 10437978Srrh ++i; (t21+COFF)[c]=(53*i)/11; (t22+COFF)[c]=i%11; 10447978Srrh #endif 10457978Srrh } 10467978Srrh p="0123456789."; 10477978Srrh while (c= *p++) {(fastab+COFF)[c] |= NB|SB; (toktyp+COFF)[c]=NUMBR;} 10487978Srrh # if gcos 10497978Srrh p="\n\"'`/\\"; 10507978Srrh # else 10517978Srrh p="\n\"'/\\"; 10527978Srrh # endif 10537978Srrh while (c= *p++) (fastab+COFF)[c] |= SB; 10547978Srrh # if gcos 10557978Srrh p="\n\"'`\\"; 10567978Srrh # else 10577978Srrh p="\n\"'\\"; 10587978Srrh # endif 10597978Srrh while (c= *p++) (fastab+COFF)[c] |= QB; 10607978Srrh p="*\n"; while (c= *p++) (fastab+COFF)[c] |= CB; 10617978Srrh (fastab+COFF)[warnc] |= WB; 10627978Srrh (fastab+COFF)['\0'] |= CB|QB|SB|WB; 10637978Srrh for (i=ALFSIZ; --i>=0; ) slotab[i]=fastab[i]|SB; 10647978Srrh p=" \t\013\f\r"; /* note no \n; \v not legal for vertical tab? */ 10657978Srrh while (c= *p++) (toktyp+COFF)[c]=BLANK; 10667978Srrh #if scw2 10677978Srrh for ((t23+COFF)[i=ALFSIZ+7-COFF]=1; --i>=-COFF; ) 10687978Srrh if (((t23+COFF)[i]=(t23+COFF+1)[i]<<1)==0) (t23+COFF)[i]=1; 10697978Srrh #endif 10707978Srrh 10717978Srrh # if unix 10727978Srrh fnames[ifno=0] = ""; dirnams[0]=dirs[0]="."; 10737978Srrh # endif 10747978Srrh # if ibm 10757978Srrh fnames[ifno=0] = ""; 10767978Srrh # endif 10777978Srrh # if gcos 10787978Srrh if (inquire(stdin, _TTY)) freopen("*src", "rt", stdin); 10797978Srrh # endif 10807978Srrh # if gimpel || gcos 10817978Srrh fnames[ifno=0] = (char *)inquire(stdin, _FILENAME); 10827978Srrh dirnams[0] = dirs[0] = trmdir(copy(fnames[0])); 10837978Srrh # endif 10847978Srrh for(i=1; i<argc; i++) 10857978Srrh { 10867978Srrh switch(argv[i][0]) 10877978Srrh { 10887978Srrh case '-': 10897978Srrh # if gcos 10907978Srrh switch(toupper(argv[i][1])) { /* case-independent on GCOS */ 10917978Srrh # else 10927978Srrh switch(argv[i][1]) { 10937978Srrh # endif 109416235Smckusick case 'M': mflag++; 10957978Srrh case 'P': pflag++; 10967978Srrh case 'E': continue; 10977978Srrh case 'R': ++rflag; continue; 10987978Srrh case 'C': passcom++; continue; 10997978Srrh case 'D': 11007978Srrh if (predef>prespc+NPREDEF) { 11017978Srrh pperror("too many -D options, ignoring %s",argv[i]); 11027978Srrh continue; 11037978Srrh } 11047978Srrh /* ignore plain "-D" (no argument) */ 11057978Srrh if (*(argv[i]+2)) *predef++ = argv[i]+2; 11067978Srrh continue; 11077978Srrh case 'U': 11087978Srrh if (prund>punspc+NPREDEF) { 11097978Srrh pperror("too many -U options, ignoring %s",argv[i]); 11107978Srrh continue; 11117978Srrh } 11127978Srrh *prund++ = argv[i]+2; 11137978Srrh continue; 11147978Srrh case 'I': 11157978Srrh if (nd>8) pperror("excessive -I file (%s) ignored",argv[i]); 11167978Srrh else dirs[nd++] = argv[i]+2; 11177978Srrh continue; 11187978Srrh case '\0': continue; 11197978Srrh default: 11207978Srrh pperror("unknown flag %s", argv[i]); 11217978Srrh continue; 11227978Srrh } 11237978Srrh default: 1124*25241Smckusick if (fin==FIRSTOPEN) { 11257978Srrh if (0>(fin=open(argv[i], READ))) { 11267978Srrh pperror("No source file %s",argv[i]); exit(8); 11277978Srrh } 11287978Srrh fnames[ifno]=copy(argv[i]); 112916235Smckusick infile=copy(argv[i]); 11307982Srrh dirs[0]=dirnams[ifno]=trmdir(argv[i]); 11317978Srrh # ifndef gcos 11327978Srrh /* too dangerous to have file name in same syntactic position 11337978Srrh be input or output file depending on file redirections, 11347978Srrh so force output to stdout, willy-nilly 11357978Srrh [i don't see what the problem is. jfr] 11367978Srrh */ 11377978Srrh } else if (fout==stdout) { 11387978Srrh if (NULL==(fout=fopen(argv[i], "w"))) { 11397978Srrh pperror("Can't create %s", argv[i]); exit(8); 114016495Sralph } else fclose(stdout); 11417978Srrh # endif 11427978Srrh } else pperror("extraneous name %s", argv[i]); 11437978Srrh } 11447978Srrh } 11457978Srrh 114616235Smckusick if (mflag) { 114716235Smckusick if (infile==(char *)0) { 114816235Smckusick fprintf(stderr, 114916235Smckusick "no input file specified with -M flag\n"); 115016235Smckusick exit(8); 115116235Smckusick } 115216235Smckusick tf=(char *)rindex(infile, '.'); 115316235Smckusick if (tf==0) { 115416235Smckusick fprintf(stderr, "missing component name on %s\n", 115516235Smckusick infile); 115616235Smckusick exit(8); 115716235Smckusick } 115816235Smckusick tf[1]='o'; 115916235Smckusick tf=(char *)rindex(infile, '/'); 116016235Smckusick if (tf!=(char *)0) 116116235Smckusick infile = tf + 1; 116216235Smckusick mout=fout; 116316235Smckusick if (NULL==(fout=fopen("/dev/null", "w"))) { 116416235Smckusick pperror("Can't open /dev/null"); 116516235Smckusick exit(8); 116616235Smckusick } 116716235Smckusick } 11687978Srrh fins[ifno]=fin; 11697978Srrh exfail = 0; 11707978Srrh /* after user -I files here are the standard include libraries */ 11717978Srrh # if unix 11727978Srrh dirs[nd++] = "/usr/include"; 11737978Srrh # endif 11747978Srrh # if gcos 11757978Srrh dirs[nd++] = "cc/include"; 11767978Srrh # endif 11777978Srrh # if ibm 11787978Srrh # ifndef gimpel 11797978Srrh dirs[nd++] = "BTL$CLIB"; 11807978Srrh # endif 11817978Srrh # endif 11827978Srrh # ifdef gimpel 11837978Srrh dirs[nd++] = intss() ? "SYS3.C." : "" ; 11847978Srrh # endif 11857978Srrh /* dirs[nd++] = "/compool"; */ 11867978Srrh dirs[nd++] = 0; 11877978Srrh defloc=ppsym("define"); 11887978Srrh udfloc=ppsym("undef"); 11897978Srrh incloc=ppsym("include"); 11907978Srrh elsloc=ppsym("else"); 11917978Srrh eifloc=ppsym("endif"); 11927978Srrh ifdloc=ppsym("ifdef"); 11937978Srrh ifnloc=ppsym("ifndef"); 11947978Srrh ifloc=ppsym("if"); 11957978Srrh lneloc=ppsym("line"); 119624202Smckusick identloc=ppsym("ident"); /* Sys 5r3 compatibility */ 11977978Srrh for (i=sizeof(macbit)/sizeof(macbit[0]); --i>=0; ) macbit[i]=0; 11987978Srrh # if unix 11997978Srrh ysysloc=stsym("unix"); 12007978Srrh # endif 12017978Srrh # if gcos 12027978Srrh ysysloc=stsym ("gcos"); 12037978Srrh # endif 12047978Srrh # if ibm 12057978Srrh ysysloc=stsym ("ibm"); 12067978Srrh # endif 12077978Srrh # if pdp11 12087978Srrh varloc=stsym("pdp11"); 12097978Srrh # endif 12107978Srrh # if vax 12117978Srrh varloc=stsym("vax"); 12127978Srrh # endif 12137978Srrh # if interdata 12147978Srrh varloc=stsym ("interdata"); 12157978Srrh # endif 12167978Srrh # if tss 12177978Srrh varloc=stsym ("tss"); 12187978Srrh # endif 12197978Srrh # if os 12207978Srrh varloc=stsym ("os"); 12217978Srrh # endif 12227978Srrh # if mert 12237978Srrh varloc=stsym ("mert"); 12247978Srrh # endif 122510083Ssam # if mc68000 122610083Ssam varloc=stsym("mc68000"); 122710083Ssam # endif 122810083Ssam # if sun 122910083Ssam varloc=stsym("sun"); 123010083Ssam # endif 12317978Srrh ulnloc=stsym ("__LINE__"); 12327978Srrh uflloc=stsym ("__FILE__"); 12337978Srrh 12347978Srrh tf=fnames[ifno]; fnames[ifno]="command line"; lineno[ifno]=1; 12357978Srrh cp2=prespc; 12367978Srrh while (cp2<predef) stsym(*cp2++); 12377978Srrh cp2=punspc; 12387978Srrh while (cp2<prund) { 12397978Srrh if (p=strdex(*cp2, '=')) *p++='\0'; 12407978Srrh lookup(*cp2++, DROP); 12417978Srrh } 12427978Srrh fnames[ifno]=tf; 12437978Srrh pbeg=buffer+NCPS; pbuf=pbeg+BUFSIZ; pend=pbuf+BUFSIZ; 12447978Srrh 12457978Srrh trulvl = 0; flslvl = 0; 124616235Smckusick lineno[0] = 1; sayline(START); 12477978Srrh outp=inp=pend; 12487978Srrh control(pend); 12497978Srrh return (exfail); 12507978Srrh } 1251