1 /*
   2  * C command
   3  * written by John F. Reiser
   4  * July/August 1978
   5  */
   6 /* Copyright (c) 2012 Joyent, Inc. All rights reserved. */
   7 /*
   8  * This implementation is based on the UNIX 32V release from 1978
   9  * with permission from Caldera Inc.
  10  *
  11  * Copyright (c) 2010 J. Schilling
  12  * All rights reserved.
  13  *
  14  * Redistribution and use in source and binary forms, with or without
  15  * modification, are permitted provided that the following conditions
  16  * are met:
  17  * 1. Redistributions of source code must retain the above copyright
  18  *    notice, this list of conditions and the following disclaimer.
  19  * 2. Redistributions in binary form must reproduce the above copyright
  20  *    notice, this list of conditions and the following disclaimer in the
  21  *    documentation and/or other materials provided with the distribution.
  22  * 3. Neither the name of the copyright holder nor the names of contributors
  23  *    may be used to endorse or promote products derived from this software
  24  *    without specific prior written permission.
  25  *
  26  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
  27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
  30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  36  * SUCH DAMAGE.
  37  */
  38 /*
  39  * Copyright(C) Caldera International Inc. 2001-2002. All rights reserved.
  40  *
  41  * Redistribution and use in source and binary forms, with or without
  42  * modification, are permitted provided that the following conditions are
  43  * met:
  44  * 1. Redistributions of source code and documentation must retain the above
  45  *    copyright notice, this list of conditions and the following
  46  *    disclaimer.
  47  *
  48  * 2. Redistributions in binary form must reproduce the above copyright
  49  *    notice, this list of conditions and the following disclaimer in the
  50  *    documentation and/or other materials provided with the distribution.
  51  *
  52  * 3. All advertising materials mentioning features or use of this software
  53  *    must display the following acknowledgement:  This product includes
  54  *    software developed or owned by Caldera International, Inc.
  55  *
  56  * 4. Neither the name of Caldera International, Inc. nor the names of other
  57  *    contributors may be used to endorse or promote products derived from
  58  *    this software without specific prior written permission.
  59  *
  60  * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
  61  * INTERNATIONAL, INC.  AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
  62  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  63  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  64  * DISCLAIMED. IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE FOR
  65  * ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  66  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  67  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  68  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  69  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  70  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  71  * POSSIBILITY OF SUCH DAMAGE.
  72  */
  73 #include <stdio.h>
  74 #include <unistd.h>
  75 #include <stdlib.h>
  76 #include <fcntl.h>
  77 #include <string.h>
  78 #include <stdarg.h>
  79 
  80 #include "cpp.h"
  81 
  82 #define SYMLEN  128
  83 static  int symlen = SYMLEN;
  84 
  85 
  86 #define SALT '#'
  87 #ifndef BUFSIZ
  88 #define BUFSIZ 512
  89 #endif
  90 
  91 static  char *pbeg;
  92 static  char *pbuf;
  93 static  char *pend;
  94 char *outp,*inp;
  95 char *newp;
  96 static  char cinit;
  97 
  98 /* some code depends on whether characters are sign or zero extended */
  99 /*      #if '\377' < 0               not used here, old cpp doesn't understand */
 100 #if pdp11 | vax | '\377' < 0
 101 #define COFF 128
 102 #else
 103 #define COFF 0
 104 #endif
 105 
 106 #define ALFSIZ 256      /* alphabet size */
 107 static  char macbit[ALFSIZ+11];
 108 static  char toktyp[ALFSIZ];
 109 #define BLANK 1         /* white space (" \t\v\f\r") */
 110 #define IDENT 2         /* valid char for identifier names */
 111 #define NUMBR 3         /* chars is of "0123456789." */
 112 
 113 /*
 114  * a superimposed code is used to reduce the number of calls to the
 115  * symbol table lookup routine.  (if the kth character of an identifier
 116  * is 'a' and there are no macro names whose kth character is 'a'
 117  * then the identifier cannot be a macro name, hence there is no need
 118  * to look in the symbol table.)  'scw1' enables the test based on
 119  * single characters and their position in the identifier.  'scw2'
 120  * enables the test based on adjacent pairs of characters and their
 121  * position in the identifier.  scw1 typically costs 1 indexed fetch,
 122  * an AND, and a jump per character of identifier, until the identifier
 123  * is known as a non-macro name or until the end of the identifier.
 124  * scw1 is inexpensive.  scw2 typically costs 4 indexed fetches,
 125  * an add, an AND, and a jump per character of identifier, but it is also
 126  * slightly more effective at reducing symbol table searches.
 127  * scw2 usually costs too much because the symbol table search is
 128  * usually short; but if symbol table search should become expensive,
 129  * the code is here.
 130  * using both scw1 and scw2 is of dubious value.
 131  */
 132 #define scw1 1
 133 #define scw2 0
 134 
 135 #if scw2
 136 char t21[ALFSIZ],t22[ALFSIZ],t23[ALFSIZ+SYMLEN];
 137 #endif
 138 
 139 #if scw1
 140 #define b0 1
 141 #define b1 2
 142 #define b2 4
 143 #define b3 8
 144 #define b4 16
 145 #define b5 32
 146 #define b6 64
 147 #define b7 128
 148 #endif
 149 
 150 #define IB 1
 151 #define SB 2
 152 #define NB 4
 153 #define CB 8
 154 #define QB 16
 155 #define WB 32
 156         char fastab[ALFSIZ];
 157 static  char slotab[ALFSIZ];
 158 static  char *ptrtab;
 159 
 160 /*
 161  * Cast the array index to int in order to avoid GCCs warnings:
 162  * warning: subscript has type `char'
 163  */
 164 #define isslo (ptrtab==(slotab+COFF))
 165 #define isid(a)  ((fastab+COFF)[(int)a]&IB)
 166 #define isspc(a) (ptrtab[(int)a]&SB)
 167 #define isnum(a) ((fastab+COFF)[(int)a]&NB)
 168 #define iscom(a) ((fastab+COFF)[(int)a]&CB)
 169 #define isquo(a) ((fastab+COFF)[(int)a]&QB)
 170 #define iswarn(a) ((fastab+COFF)[(int)a]&WB)
 171 
 172 #define eob(a) ((a)>=pend)
 173 #define bob(a) (pbeg>=(a))
 174 
 175 #define BUFFERSIZ       8192
 176 static  char buffer[SYMLEN+BUFFERSIZ+BUFFERSIZ+SYMLEN];
 177 
 178 /*
 179  * SBSIZE was 12000 in 1978, we need to have a way to
 180  * malloc more space.
 181  */
 182 #define SBSIZE 512000
 183 static  char    sbf[SBSIZE];
 184 static  char    *savch  = sbf;
 185 
 186 # define DROP 0xFE      /* special character not legal ASCII or EBCDIC */
 187 # define WARN DROP
 188 # define SAME 0
 189 # define MAXINC 16      /* max include nesting depth */
 190 # define MAXIDIRS 20    /* max # of -I directories */
 191 # define MAXFRE 14      /* max buffers of macro pushback */
 192 # define MAXFRM 31      /* max number of formals/actuals to a macro */
 193 
 194 static char warnc = (char)WARN;
 195 
 196 static  int mactop;
 197 static  int fretop;
 198 static  char *instack[MAXFRE];
 199 static  char *bufstack[MAXFRE];
 200 static  char *endbuf[MAXFRE];
 201 
 202 static  int plvl;       /* parenthesis level during scan for macro actuals */
 203 static  int maclin;     /* line number of macro call requiring actuals */
 204 static  char *macfil;   /* file name of macro call requiring actuals */
 205 static  char *macnam;   /* name of macro requiring actuals */
 206 static  int maclvl;     /* # calls since last decrease in nesting level */
 207 static  char *macforw;  /* ptr which must be exceeded to decrease nesting lvl */
 208 static  int macdam;     /* offset to macforw due to buffer shifting */
 209 
 210 static  int     inctop[MAXINC];
 211 static  char    *fnames[MAXINC];
 212 static  char    *dirnams[MAXINC];       /* actual directory of #include files */
 213 static  int     fins[MAXINC];
 214 static  int     lineno[MAXINC];
 215 
 216 /*
 217  * We need:
 218  *      "" include dir as dirs[0] +
 219  *      MAXIDIRS +
 220  *      system default include dir +
 221  *      a NULL pointer at the end
 222  */
 223 static  char    *dirs[MAXIDIRS+3];      /* -I and <> directories */
 224 static  int     fin     = STDIN_FILENO;
 225 static  FILE    *fout;                  /* Init in main(), Mac OS is nonPOSIX */
 226 static  int     nd      = 1;
 227 static  int     pflag;  /* don't put out lines "# 12 foo.c" */
 228 static  int     passcom;        /* don't delete comments */
 229 static  int rflag;      /* allow macro recursion */
 230 static  int     hflag;  /* Print included filenames */
 231 static  int     nopredef;       /* -undef all */
 232 static  int     ifno;
 233 # define NPREDEF 64
 234 static  char *prespc[NPREDEF];
 235 static  char **predef = prespc;
 236 static  char *punspc[NPREDEF];
 237 static  char **prund = punspc;
 238 static  int     exfail;
 239 static  struct symtab *lastsym;
 240 
 241 
 242 static  void            sayline(char *);
 243 static  void            dump(void);
 244 static  char            *refill(char *);
 245 static  char            *cotoken(char *);
 246 char            *skipbl(char *);
 247 static  char            *unfill(char *);
 248 static  char            *doincl(char *);
 249 static  int             equfrm(char *, char *, char *);
 250 static  char            *dodef(char *);
 251 static  char            *control(char *);
 252 static  struct symtab   *stsym(char *);
 253 static  struct symtab   *ppsym(char *);
 254 void            pperror(char *fmt, ...);
 255 void            yyerror(char *fmt, ...);
 256 static  void            ppwarn(char *fmt, ...);
 257 struct symtab   *lookup(char *, int);
 258 static  struct symtab   *slookup(char *, char *, int);
 259 static  char            *subst(char *, struct symtab *);
 260 static  char            *trmdir(char *);
 261 static  char            *copy(char *);
 262 static  char            *strdex(char *, int);
 263 int             yywrap(void);
 264 int             main(int argc, char **argav);
 265 
 266 
 267 #define symsiz 4000
 268 static  struct symtab stab[symsiz];
 269 
 270 static  struct symtab *defloc;
 271 static  struct symtab *udfloc;
 272 static  struct symtab *incloc;
 273 static  struct symtab *ifloc;
 274 static  struct symtab *elsloc;
 275 static  struct symtab *eifloc;
 276 static  struct symtab *elifloc;
 277 static  struct symtab *ifdloc;
 278 static  struct symtab *ifnloc;
 279 static  struct symtab *ysysloc;
 280 static  struct symtab *varloc;
 281 static  struct symtab *lneloc;
 282 static  struct symtab *ulnloc;
 283 static  struct symtab *uflloc;
 284 static  struct symtab *idtloc;
 285 static  struct symtab *pragmaloc;
 286 static  struct symtab *errorloc;
 287 static  int     trulvl;
 288         int     flslvl;
 289 static  int     elflvl;
 290 static  int     elslvl;
 291 
 292 /*
 293  * The sun cpp prints a classification token past the
 294  * "# linenumber filename" lines:
 295  */
 296 #define NOINCLUDE       ""      /* Not related to enter/leave incl. file */
 297 #define ENTERINCLUDE    "1"     /* We are just entering an include file  */
 298 #define LEAVEINCLUDE    "2"     /* We are just leaving an include file   */
 299 
 300 /* ARGSUSED */
 301 static void
 302 sayline(what)
 303         char    *what;
 304 {
 305         if (pflag==0)
 306             fprintf(fout,"# %d \"%s\" %s\n", lineno[ifno], fnames[ifno], what);
 307 }
 308 
 309 /*
 310  * data structure guide
 311  *
 312  * most of the scanning takes place in the buffer:
 313  *
 314  *  (low address)                                             (high address)
 315  *  pbeg                           pbuf                                 pend
 316  *  |      <-- BUFFERSIZ chars -->   |         <-- BUFFERSIZ chars -->     |
 317  *  _______________________________________________________________________
 318  * |_______________________________________________________________________|
 319  *          |               |               | 
 320  *          |<-- waiting -->|               |<-- waiting -->
 321  *          |    to be      |<-- current -->|    to be
 322  *          |    written    |    token      |    scanned
 323  *          |               |               |
 324  *          outp            inp             p
 325  *
 326  *  *outp   first char not yet written to output file
 327  *  *inp    first char of current token
 328  *  *p      first char not yet scanned
 329  *
 330  * macro expansion: write from *outp to *inp (chars waiting to be written),
 331  * ignore from *inp to *p (chars of the macro call), place generated
 332  * characters in front of *p (in reverse order), update pointers,
 333  * resume scanning.
 334  *
 335  * symbol table pointers point to just beyond the end of macro definitions;
 336  * the first preceding character is the number of formal parameters.
 337  * the appearance of a formal in the body of a definition is marked by
 338  * 2 chars: the char WARN, and a char containing the parameter number.
 339  * the first char of a definition is preceded by a zero character.
 340  *
 341  * when macro expansion attempts to back up over the beginning of the
 342  * buffer, some characters preceding *pend are saved in a side buffer,
 343  * the address of the side buffer is put on 'instack', and the rest
 344  * of the main buffer is moved to the right.  the end of the saved buffer
 345  * is kept in 'endbuf' since there may be nulls in the saved buffer.
 346  *
 347  * similar action is taken when an 'include' statement is processed,
 348  * except that the main buffer must be completely emptied.  the array
 349  * element 'inctop[ifno]' records the last side buffer saved when
 350  * file 'ifno' was included.  these buffers remain dormant while
 351  * the file is being read, and are reactivated at end-of-file.
 352  *
 353  * instack[0 : mactop] holds the addresses of all pending side buffers.
 354  * instack[inctop[ifno]+1 : mactop-1] holds the addresses of the side
 355  * buffers which are "live"; the side buffers instack[0 : inctop[ifno]]
 356  * are dormant, waiting for end-of-file on the current file.
 357  *
 358  * space for side buffers is obtained from 'savch' and is never returned.
 359  * bufstack[0:fretop-1] holds addresses of side buffers which
 360  * are available for use.
 361  */
 362 
 363 static void
 364 dump() {
 365         register char *p1;
 366         if ((p1=outp)==inp || flslvl!=0) return;
 367         fwrite(p1, inp - p1, 1, fout);
 368         outp=p1;
 369 }
 370 
 371 static char *
 372 refill(p) register char *p; {
 373 /*
 374  * dump buffer.  save chars from inp to p.  read into buffer at pbuf,
 375  * contiguous with p.  update pointers, return new p.
 376  */
 377         register char *np,*op; register int ninbuf;
 378         dump(); np=pbuf-(p-inp); op=inp;
 379         if (bob(np+1)) {pperror("token too long"); np=pbeg; p=inp+BUFFERSIZ;}
 380         macdam += np-inp; outp=inp=np;
 381         while (op<p) *np++= *op++;
 382         p=np;
 383         for (;;) {
 384                 if (mactop>inctop[ifno]) {
 385                         /* retrieve hunk of pushed-back macro text */
 386                         op=instack[--mactop]; np=pbuf;
 387                         do {
 388                                 while ((*np++= *op++) != '\0');
 389                         } while (op<endbuf[mactop]); pend=np-1;
 390                         /* make buffer space avail for 'include' processing */
 391                         if (fretop<MAXFRE) bufstack[fretop++]=instack[mactop];
 392                         return(p);
 393                 } else {/* get more text from file(s) */
 394                         maclvl=0;
 395                         if (0<(ninbuf=read(fin,pbuf,BUFFERSIZ))) {
 396                                 pend=pbuf+ninbuf; *pend='\0';
 397                                 return(p);
 398                         }
 399                         /* end of #include file */
 400                         if (ifno==0) {/* end of input */
 401                                 if (plvl!=0) {
 402                                         int n=plvl,tlin=lineno[ifno];
 403                                         char *tfil=fnames[ifno];
 404                                         lineno[ifno]=maclin;
 405                                         fnames[ifno]=macfil;
 406                                         pperror("%s: unterminated macro call",
 407                                             macnam);
 408                                         lineno[ifno]=tlin; fnames[ifno]=tfil;
 409                                         np=p;
 410                                         /*
 411                                          * shut off unterminated quoted string
 412                                          */
 413                                         *np++='\n';
 414                                         /* supply missing parens */
 415                                         while (--n>=0) *np++=')';
 416                                         pend=np; *np='\0';
 417                                         if (plvl<0) plvl=0;
 418                                         return(p);
 419                                 }
 420                                 inp=p; dump(); exit(exfail);
 421                         }
 422                         close(fin);
 423                         fin=fins[--ifno];
 424                         dirs[0]=dirnams[ifno];
 425                         sayline(LEAVEINCLUDE);
 426                 }
 427         }
 428 }
 429 
 430 #define BEG 0
 431 #define LF 1
 432 
 433 static char *
 434 cotoken(p) register char *p; {
 435         register int c,i; char quoc;
 436         static int state = BEG;
 437 
 438         if (state!=BEG) goto prevlf;
 439 for (;;) {
 440 again:
 441         while (!isspc(*p++));
 442         switch (*(inp=p-1)) {
 443         case 0: {
 444                 if (eob(--p)) {p=refill(p); goto again;}
 445                 else ++p; /* ignore null byte */
 446         } break;
 447         case '|': case '&': for (;;) {/* sloscan only */
 448                 if (*p++== *inp) break;
 449                 if (eob(--p)) p=refill(p);
 450                 else break;
 451         } break;
 452         case '=': case '!': for (;;) {/* sloscan only */
 453                 if (*p++=='=') break;
 454                 if (eob(--p)) p=refill(p);
 455                 else break;
 456         } break;
 457         case '<': case '>': for (;;) {/* sloscan only */
 458                 if (*p++=='=' || p[-2]==p[-1]) break;
 459                 if (eob(--p)) p=refill(p);
 460                 else break;
 461         } break;
 462         case '\\': for (;;) {
 463                 if (*p++=='\n') {++lineno[ifno]; break;}
 464                 if (eob(--p)) p=refill(p);
 465                 else {++p; break;}
 466         } break;
 467         case '/': for (;;) {
 468                 if (*p++=='*') {/* comment */
 469                         if (!passcom) {inp=p-2; dump(); ++flslvl;}
 470                         for (;;) {
 471                                 while (!iscom(*p++));
 472                                 if (p[-1]=='*') for (;;) {
 473                                         if (*p++=='/') goto endcom;
 474                                         if (eob(--p)) {
 475                                                 if (!passcom) {
 476                                                         inp=p;
 477                                                         p=refill(p);
 478                                                 } else if ((p-inp)>=BUFFERSIZ) {
 479                                                         /* split long comment */
 480                                                         inp=p;
 481                                                         /*
 482                                                          * last char written
 483                                                          * is '*'
 484                                                          */
 485                                                         p=refill(p);
 486                                                         /*
 487                                                          * terminate first part
 488                                                          */
 489                                                         putc('/',fout);
 490                                                         /*
 491                                                          * and fake start of 2nd
 492                                                          */
 493                                                         outp=inp=p-=3;
 494                                                         *p++='/';
 495                                                         *p++='*';
 496                                                         *p++='*';
 497                                                 } else {
 498                                                         p=refill(p);
 499                                                 }
 500                                         } else {
 501                                                 break;
 502                                         }
 503                                 } else if (p[-1]=='\n') {
 504                                         ++lineno[ifno];
 505                                         if (!passcom)
 506                                                 putc('\n',fout);
 507                                 } else if (eob(--p)) {
 508                                         if (!passcom) {
 509                                                 inp=p; p=refill(p);
 510                                         } else if ((p-inp)>=BUFFERSIZ) {
 511                                                 /* split long comment */
 512                                                 inp=p; p=refill(p);
 513                                                 putc('*',fout); putc('/',fout);
 514                                                 outp=inp=p-=2;
 515                                                 *p++='/';
 516                                                 *p++='*';
 517                                         } else {
 518                                                 p=refill(p);
 519                                         }
 520                                 } else {
 521                                         ++p; /* ignore null byte */
 522                                 }
 523                         }
 524                 endcom:
 525                         if (!passcom) {outp=inp=p; --flslvl; goto again;}
 526                         break;
 527                 }
 528                 if (eob(--p)) p=refill(p);
 529                 else break;
 530         } break;
 531         case '"': case '\'': {
 532                 quoc=p[-1];
 533                 for (;;) {
 534                         while (!isquo(*p++));
 535                         if (p[-1]==quoc)
 536                                 break;
 537                         if (p[-1]=='\n') {
 538                                 --p;
 539                                 break;
 540                         } /* bare \n terminates quotation */
 541                         if (p[-1]=='\\') {
 542                                 for (;;) {
 543                                         if (*p++=='\n') {
 544                                                 ++lineno[ifno];
 545                                                 break;
 546                                         } /* escaped \n ignored */
 547                                         if (eob(--p)) {
 548                                                 p=refill(p);
 549                                         } else {
 550                                                 ++p;
 551                                                 break;
 552                                         }
 553                                 }
 554                         } else if (eob(--p)) {
 555                                 p=refill(p);
 556                         } else {
 557                                 ++p;    /* it was a different quote character */
 558                         }
 559                 }
 560         } break;
 561         case '\n': {
 562                 ++lineno[ifno]; if (isslo) {state=LF; return(p);}
 563 prevlf:
 564                 state=BEG;
 565                 for (;;) {
 566                         if (*p++=='#') return(p);
 567                         if (eob(inp= --p)) p=refill(p);
 568                         else goto again;
 569                 }
 570         }
 571         /* NOTREACHED */
 572         case '0': case '1': case '2': case '3': case '4':
 573         case '5': case '6': case '7': case '8': case '9':
 574         for (;;) {
 575                 while (isnum(*p++));
 576                 if (eob(--p)) p=refill(p);
 577                 else break;
 578         } break;
 579         case 'A': case 'B': case 'C': case 'D': case 'E':
 580         case 'F': case 'G': case 'H': case 'I': case 'J':
 581         case 'K': case 'L': case 'M': case 'N': case 'O':
 582         case 'P': case 'Q': case 'R': case 'S': case 'T':
 583         case 'U': case 'V': case 'W': case 'X': case 'Y':
 584         case 'Z': case '_':
 585         case 'a': case 'b': case 'c': case 'd': case 'e':
 586         case 'f': case 'g': case 'h': case 'i': case 'j':
 587         case 'k': case 'l': case 'm': case 'n': case 'o':
 588         case 'p': case 'q': case 'r': case 's': case 't':
 589         case 'u': case 'v': case 'w': case 'x': case 'y':
 590         case 'z':
 591 #if scw1
 592 #define tmac1(c,bit) if (!xmac1(c,bit,&)) goto nomac
 593 #define xmac1(c,bit,op) ((macbit+COFF)[c] op (bit))
 594 #else
 595 #define tmac1(c,bit)
 596 #define xmac1(c,bit,op)
 597 #endif
 598 
 599 #if scw2
 600 #define tmac2(c0,c1,cpos) if (!xmac2(c0,c1,cpos,&)) goto nomac
 601 #define xmac2(c0,c1,cpos,op)\
 602         ((macbit+COFF)[(t21+COFF)[c0]+(t22+COFF)[c1]] op (t23+COFF+cpos)[c0])
 603 #else
 604 #define tmac2(c0,c1,cpos)
 605 #define xmac2(c0,c1,cpos,op)
 606 #endif
 607 
 608         if (flslvl) goto nomac;
 609         for (;;) {
 610                 c= p[-1];                          tmac1(c,b0);
 611                 i= *p++; if (!isid(i)) goto endid; tmac1(i,b1); tmac2(c,i,0);
 612                 c= *p++; if (!isid(c)) goto endid; tmac1(c,b2); tmac2(i,c,1);
 613                 i= *p++; if (!isid(i)) goto endid; tmac1(i,b3); tmac2(c,i,2);
 614                 c= *p++; if (!isid(c)) goto endid; tmac1(c,b4); tmac2(i,c,3);
 615                 i= *p++; if (!isid(i)) goto endid; tmac1(i,b5); tmac2(c,i,4);
 616                 c= *p++; if (!isid(c)) goto endid; tmac1(c,b6); tmac2(i,c,5);
 617                 i= *p++; if (!isid(i)) goto endid; tmac1(i,b7); tmac2(c,i,6);
 618                                                                 tmac2(i,0,7);
 619                 while (isid(*p++));
 620                 if (eob(--p)) {refill(p); p=inp+1; continue;}
 621                 goto lokid;
 622         endid:
 623                 if (eob(--p)) {refill(p); p=inp+1; continue;}
 624                 tmac2(p[-1],0,-1+(p-inp));
 625         lokid:
 626                 slookup(inp,p,0); if (newp) {p=newp; goto again;}
 627                 else break;
 628         nomac:
 629                 while (isid(*p++));
 630                 if (eob(--p)) {p=refill(p); goto nomac;}
 631                 else break;
 632         } break;
 633         } /* end of switch */
 634         
 635         if (isslo) return(p);
 636 } /* end of infinite loop */
 637 }
 638 
 639 char *
 640 skipbl(p) register char *p; {/* get next non-blank token */
 641         do {
 642                 outp=inp=p;
 643                 p=cotoken(p);
 644         } while ((toktyp+COFF)[(int)*inp]==BLANK);
 645         return(p);
 646 }
 647 
 648 static char *
 649 unfill(p) register char *p; {
 650 /*
 651  * take <= BUFFERSIZ chars from right end of buffer and put them on instack .
 652  * slide rest of buffer to the right, update pointers, return new p.
 653  */
 654         register char *np,*op; register int d;
 655         if (mactop>=MAXFRE) {
 656                 pperror("%s: too much pushback",macnam);
 657                 p=inp=pend; dump();     /* begin flushing pushback */
 658                 while (mactop>inctop[ifno]) {p=refill(p); p=inp=pend; dump();}
 659         }
 660         if (fretop>0)
 661                 np=bufstack[--fretop];
 662         else {
 663                 np=savch; savch+=BUFFERSIZ;
 664                 if (savch>=sbf+SBSIZE) {pperror("no space"); exit(exfail);}
 665                 *savch++='\0';
 666         }
 667         instack[mactop]=np; op=pend-BUFFERSIZ; if (op<p) op=p;
 668         for (;;) {
 669                 while ((*np++= *op++) != '\0');
 670                 if (eob(op))
 671                         break;
 672         } /* out with old */
 673         endbuf[mactop++]=np;    /* mark end of saved text */
 674         np=pbuf+BUFFERSIZ;
 675         op=pend-BUFFERSIZ;
 676         pend=np;
 677         if (op<p)
 678                 op=p;
 679         while (outp<op) *--np= *--op; /* slide over new */
 680         if (bob(np))
 681                 pperror("token too long");
 682         d=np-outp; outp+=d; inp+=d; macdam+=d;
 683         return(p+d);
 684 }
 685 
 686 static char *
 687 doincl(p) register char *p; {
 688         int filok,inctype;
 689         register char *cp; char **dirp,*nfil; char filname[BUFFERSIZ];
 690 
 691         filname[0] = '\0';      /* Make lint quiet */
 692         p=skipbl(p); cp=filname;
 693         if (*inp++=='<') {/* special <> syntax */
 694                 inctype=1;
 695                 for (;;) {
 696                         outp=inp=p; p=cotoken(p);
 697                         if (*inp=='\n') {--p; *cp='\0'; break;}
 698                         if (*inp=='>') {      *cp='\0'; break;}
 699 # ifdef gimpel
 700                         if (*inp=='.' && !intss()) *inp='#';
 701 # endif
 702                         while (inp<p) *cp++= *inp++;
 703                 }
 704         } else if (inp[-1]=='"') {/* regular "" syntax */
 705                 inctype=0;
 706 # ifdef gimpel
 707                 while (inp<p) {if (*inp=='.' && !intss()) *inp='#'; *cp++= *inp++;}
 708 # else
 709                 while (inp<p) *cp++= *inp++;
 710 # endif
 711                 if (*--cp=='"') *cp='\0';
 712         } else {pperror("bad include syntax",0); inctype=2;}
 713         /* flush current file to \n , then write \n */
 714         ++flslvl; do {outp=inp=p; p=cotoken(p);} while (*inp!='\n'); --flslvl;
 715         inp=p; dump(); if (inctype==2) return(p);
 716         /* look for included file */
 717         if (ifno+1 >=MAXINC) {
 718                 pperror("Unreasonable include nesting",0); return(p);
 719         }
 720         if ((nfil=savch)>sbf+SBSIZE-BUFFERSIZ) {
 721                 pperror("no space");
 722                 exit(exfail);
 723         }
 724         filok=0;
 725         for (dirp=dirs+inctype; *dirp; ++dirp) {
 726                 if (filname[0]=='/' || **dirp=='\0') {
 727                         strcpy(nfil,filname);
 728                 } else {
 729                         strcpy(nfil,*dirp);
 730 # if unix
 731                         strcat(nfil,"/");
 732 # endif
 733                         strcat(nfil,filname);
 734                 }
 735                 if (0<(fins[ifno+1]=open(nfil, O_RDONLY))) {
 736                         filok=1; fin=fins[++ifno]; break;
 737                 }
 738         }
 739         if (filok==0) {
 740                 pperror("Can't find include file %s",filname);
 741         } else {
 742                 lineno[ifno]=1; fnames[ifno]=cp=nfil; while (*cp++); savch=cp;
 743                 dirnams[ifno]=dirs[0]=trmdir(copy(nfil));
 744                 sayline(ENTERINCLUDE);
 745                 if (hflag)
 746                         fprintf(stderr, "%s\n", nfil);
 747                 /* save current contents of buffer */
 748                 while (!eob(p)) p=unfill(p);
 749                 inctop[ifno]=mactop;
 750         }
 751         return(p);
 752 }
 753 
 754 static int
 755 equfrm(a,p1,p2) register char *a,*p1,*p2; {
 756         register char c; int flag;
 757         c= *p2; *p2='\0';
 758         flag=strcmp(a,p1); *p2=c; return(flag==SAME);
 759 }
 760 
 761 static char *
 762 dodef(p) char *p; {/* process '#define' */
 763         register char *pin,*psav,*cf;
 764         char **pf,**qf; int b,c,params; struct symtab *np;
 765         char *oldval,*oldsavch;
 766         char *formal[MAXFRM]; /* formal[n] is name of nth formal */
 767         char formtxt[BUFFERSIZ]; /* space for formal names */
 768 
 769         formtxt[0] = '\0';      /* Make lint quiet */
 770 
 771         if (savch>sbf+SBSIZE-BUFFERSIZ) {
 772                 pperror("too much defining");
 773                 return(p);
 774         }
 775         oldsavch=savch; /* to reclaim space if redefinition */
 776         ++flslvl; /* prevent macro expansion during 'define' */
 777         p=skipbl(p); pin=inp;
 778         if ((toktyp+COFF)[(int)*pin]!=IDENT) {
 779                 ppwarn("illegal macro name");
 780                 while (*inp!='\n')
 781                         p=skipbl(p);
 782                 return(p);
 783         }
 784         np=slookup(pin,p,1);
 785         if (getenv("CPP_DEBUG_DEFINITIONS") != NULL)
 786                 fprintf(stderr, "*** defining %s at %s:%d\n",
 787                     np->name, fnames[ifno], lineno[ifno]);
 788         if ((oldval=np->value) != NULL)
 789                 savch=oldsavch; /* was previously defined */
 790         b=1; cf=pin;
 791         while (cf<p) {/* update macbit */
 792                 c= *cf++; xmac1(c,b,|=); b=(b+b)&0xFF;
 793                 if (cf!=p) {
 794                         xmac2(c,*cf,-1+(cf-pin),|=);
 795                 } else {
 796                         xmac2(c,0,-1+(cf-pin),|=);
 797                 }
 798         }
 799         params=0; outp=inp=p; p=cotoken(p); pin=inp;
 800         formal[0] = ""; /* Prepare for hack at next line... */
 801         pf = formal;    /* Make gcc/lint quiet, pf only used with params!=0 */
 802         if (*pin=='(') {/* with parameters; identify the formals */
 803                 cf=formtxt; pf=formal;
 804                 for (;;) {
 805                         p=skipbl(p); pin=inp;
 806                         if (*pin=='\n') {
 807                                 --lineno[ifno];
 808                                 --p;
 809                                 pperror("%s: missing )",np->name);
 810                                 break;
 811                         }
 812                         if (*pin==')') break;
 813                         if (*pin==',') continue;
 814                         if ((toktyp+COFF)[(int)*pin]!=IDENT) {
 815                                 c= *p;
 816                                 *p='\0';
 817                                 pperror("bad formal: %s",pin);
 818                                 *p=c;
 819                         } else if (pf>= &formal[MAXFRM]) {
 820                                 c= *p;
 821                                 *p='\0';
 822                                 pperror("too many formals: %s",pin);
 823                                 *p=c;
 824                         } else {
 825                                 *pf++=cf;
 826                                 while (pin<p)
 827                                         *cf++= *pin++;
 828                                 *cf++='\0';
 829                                 ++params;
 830                         }
 831                 }
 832                 if (params==0)
 833                         --params; /* #define foo() ... */
 834         } else if (*pin=='\n') {
 835                 --lineno[ifno];
 836                 --p;
 837         }
 838         /*
 839          * remember beginning of macro body, so that we can
 840          * warn if a redefinition is different from old value.
 841          */
 842         oldsavch=psav=savch;
 843         for (;;) {/* accumulate definition until linefeed */
 844                 outp=inp=p; p=cotoken(p); pin=inp;
 845                 if (*pin=='\\' && pin[1]=='\n')
 846                         continue;       /* ignore escaped lf */
 847                 if (*pin=='\n') break;
 848                 if (params) {
 849                         /* mark the appearance of formals in the definiton */
 850                         if ((toktyp+COFF)[(int)*pin]==IDENT) {
 851                                 for (qf=pf; --qf>=formal; ) {
 852                                         if (equfrm(*qf,pin,p)) {
 853                                                 *psav++=qf-formal+1;
 854                                                 *psav++=WARN;
 855                                                 pin=p;
 856                                                 break;
 857                                         }
 858                                 }
 859                         } else if (*pin=='"' || *pin=='\'') {
 860                                 /* inside quotation marks, too */
 861                                 char quoc= *pin;
 862                                 for (*psav++= *pin++; pin<p && *pin!=quoc; ) {
 863                                         while (pin<p && !isid(*pin))
 864                                                 *psav++= *pin++;
 865                                         cf=pin;
 866                                         while (cf<p && isid(*cf))
 867                                                 ++cf;
 868                                         for (qf=pf; --qf>=formal; ) {
 869                                                 if (equfrm(*qf,pin,cf)) {
 870                                                         *psav++=qf-formal+1;
 871                                                         *psav++=WARN;
 872                                                         pin=cf;
 873                                                         break;
 874                                                 }
 875                                         }
 876                                         while (pin<cf)
 877                                                 *psav++= *pin++;
 878                                 }
 879                         }
 880                 }
 881                 while (pin<p) *psav++= *pin++;
 882         }
 883         *psav++=params; *psav++='\0';
 884         if ((cf=oldval)!=NULL) {/* redefinition */
 885                 --cf;   /* skip no. of params, which may be zero */
 886                 while (*--cf);  /* go back to the beginning */
 887                 if (0!=strcmp(++cf,oldsavch)) {
 888                         /* redefinition different from old */
 889                         --lineno[ifno];
 890                         ppwarn("%s redefined",np->name);
 891                         ++lineno[ifno];
 892                         np->value=psav-1;
 893                 } else {
 894                         psav=oldsavch; /* identical redef.; reclaim space */
 895                 }
 896         } else {
 897                 np->value=psav-1;
 898         }
 899         --flslvl; inp=pin; savch=psav; return(p);
 900 }
 901 
 902 #define fasscan() ptrtab=fastab+COFF
 903 #define sloscan() ptrtab=slotab+COFF
 904 
 905 static char *
 906 control(p) register char *p; {/* find and handle preprocessor control lines */
 907         register struct symtab *np;
 908 for (;;) {
 909         fasscan(); p=cotoken(p); if (*inp=='\n') ++inp; dump();
 910         sloscan(); p=skipbl(p);
 911         *--inp=SALT; outp=inp; ++flslvl; np=slookup(inp,p,0); --flslvl;
 912         if (np==defloc) {/* define */
 913                 if (flslvl==0) {p=dodef(p); continue;}
 914         } else if (np==incloc) {/* include */
 915                 if (flslvl==0) {p=doincl(p); continue;}
 916         } else if (np==ifnloc) {/* ifndef */
 917                 ++flslvl; p=skipbl(p); np=slookup(inp,p,0); --flslvl;
 918                 if (flslvl==0 && np->value==0) ++trulvl;
 919                 else ++flslvl;
 920         } else if (np==ifdloc) {/* ifdef */
 921                 ++flslvl; p=skipbl(p); np=slookup(inp,p,0); --flslvl;
 922                 if (flslvl==0 && np->value!=0) ++trulvl;
 923                 else ++flslvl;
 924         } else if (np==eifloc) {/* endif */
 925                 if (flslvl) {if (--flslvl==0) sayline(NOINCLUDE);}
 926                 else if (trulvl) --trulvl;
 927                 else pperror("If-less endif",0);
 928 
 929                 if (flslvl == 0)
 930                         elflvl = 0;
 931                 elslvl = 0;
 932         } else if (np==elifloc) {/* elif */
 933                 if (flslvl == 0)
 934                         elflvl = trulvl;
 935                 if (flslvl) {
 936                         if (elflvl > trulvl) {
 937                                 ;
 938                         } else if (--flslvl != 0) {
 939                                 ++flslvl;
 940                         } else {
 941                                 newp = p;
 942                                 if (yyparse()) {
 943                                         ++trulvl;
 944                                         sayline(NOINCLUDE);
 945                                 } else {
 946                                         ++flslvl;
 947                                 }
 948                                 p = newp;
 949                         }
 950                 } else if (trulvl) {
 951                         ++flslvl;
 952                         --trulvl;
 953                 } else
 954                         pperror("If-less elif");
 955 
 956         } else if (np==elsloc) {/* else */
 957                 if (flslvl) {
 958                         if (elflvl > trulvl)
 959                                 ;
 960                         else if (--flslvl!=0) ++flslvl;
 961                         else {++trulvl; sayline(NOINCLUDE);}
 962                 }
 963                 else if (trulvl) {++flslvl; --trulvl;}
 964                 else pperror("If-less else",0);
 965 
 966                 if (elslvl==trulvl+flslvl) 
 967                         pperror("Too many #else's"); 
 968                 elslvl=trulvl+flslvl; 
 969 
 970         } else if (np==udfloc) {/* undefine */
 971                 if (flslvl==0) {
 972                         ++flslvl; p=skipbl(p); slookup(inp,p,DROP); --flslvl;
 973                 }
 974         } else if (np==ifloc) {/* if */
 975                 newp=p;
 976                 if (flslvl==0 && yyparse()) ++trulvl; else ++flslvl;
 977                 p=newp;
 978         } else if (np == idtloc) {              /* ident */
 979                 if (pflag == 0)
 980                         while (*inp != '\n')    /* pass text */
 981                                 p = cotoken(p);
 982         } else if (np == pragmaloc) {           /* pragma */
 983                 while (*inp != '\n')            /* pass text */
 984                         p = cotoken(p);
 985 #ifdef EXIT_ON_ERROR
 986         } else if (np == errorloc) {            /* error */
 987                 if (trulvl > 0) {
 988                         char ebuf[BUFFERSIZ];
 989 
 990                         p = ebuf;
 991                         while (*inp != '\n') {
 992                                 if (*inp == '\0')
 993                                         if (eob(--inp)) {
 994                                                 inp = refill(inp);
 995                                                 continue;
 996                                         }
 997                                 *p++ = *inp++;
 998                                 if (p >= &ebuf[BUFFERSIZ-1])
 999                                         break;
1000                         }
1001                         *p = '\0';
1002                         pperror(ebuf);
1003                         exit(exfail);
1004                 }
1005 #endif
1006         } else if (np==lneloc) {/* line */
1007                 if (flslvl==0 && pflag==0) {
1008                         outp=inp=p;
1009                         *--outp='#';
1010                         while (*inp!='\n')
1011                                 p=cotoken(p);
1012                         continue;
1013                 }
1014         } else if (*++inp=='\n') {
1015                 outp=inp;       /* allows blank line after # */
1016         } else {
1017                 pperror("undefined control",0);
1018         }
1019         /* flush to lf */
1020         ++flslvl;
1021         while (*inp!='\n') {
1022                 outp=inp=p;
1023                 p=cotoken(p);
1024         }
1025         --flslvl;
1026 }
1027 }
1028 
1029 static struct symtab *
1030 stsym(s) register char *s; {
1031         char buf[BUFFERSIZ]; register char *p;
1032 
1033         /* make definition look exactly like end of #define line */
1034         /* copy to avoid running off end of world when param list is at end */
1035         p=buf; while ((*p++= *s++) != '\0');
1036         p=buf; while (isid(*p++)); /* skip first identifier */
1037         if (*--p=='=') {*p++=' '; while (*p++);}
1038         else {s=" 1"; while ((*p++= *s++) != '\0');}
1039         pend=p; *--p='\n';
1040         sloscan(); dodef(buf); return(lastsym);
1041 }
1042 
1043 static struct symtab *
1044 ppsym(s) char *s; {/* kluge */
1045         register struct symtab *sp;
1046         cinit=SALT; *savch++=SALT; sp=stsym(s); --sp->name; cinit=0; return(sp);
1047 }
1048 
1049 void
1050 verror(char *fmt, va_list args)
1051 {
1052         if (fnames[ifno][0])
1053                 fprintf(stderr, "%s: ", fnames[ifno]);
1054         fprintf(stderr, "%d: ",lineno[ifno]);
1055 
1056         (void)vfprintf(stderr, fmt, args);
1057         fputc('\n', stderr);
1058 }
1059 
1060 /* VARARGS1 */
1061 void
1062 pperror(char *fmt, ...)
1063 {
1064         va_list args;
1065 
1066         va_start(args, fmt);
1067         verror(fmt, args);
1068         va_end(args);
1069 
1070         ++exfail;
1071 }
1072 
1073 /* VARARGS1 */
1074 void
1075 yyerror(char *fmt, ...)
1076 {
1077         va_list args;
1078 
1079         va_start(args, fmt);
1080         verror(fmt, args);
1081         va_end(args);
1082 }
1083 
1084 /* VARARGS1 */
1085 static void
1086 ppwarn(char *fmt, ...)
1087 {
1088         va_list args;
1089         int fail = exfail;
1090         exfail = -1;
1091 
1092         va_start(args, fmt);
1093         verror(fmt, args);
1094         va_end(args);
1095 
1096         exfail = fail;
1097 }
1098 
1099 struct symtab *
1100 lookup(namep, enterf)
1101 char *namep;
1102 int enterf;
1103 {
1104         register char *np, *snp;
1105         register int c, i; int around;
1106         register struct symtab *sp;
1107 
1108         /* namep had better not be too long (currently, <=symlen chars) */
1109         np=namep; around=0; i=cinit;
1110         while ((c = *np++) != '\0')
1111                 i += i+c;
1112         c=i;    /* c=i for register usage on pdp11 */
1113         c %= symsiz;
1114         if (c<0)
1115                 c += symsiz;
1116         sp = &stab[c];
1117         while ((snp=sp->name) != NULL) {
1118                 np = namep;
1119                 while (*snp++ == *np)
1120                         if (*np++ == '\0') {
1121                                 if (enterf==DROP) {
1122                                         sp->name[0]= DROP;
1123                                         sp->value=0;
1124                                 }
1125                                 return(lastsym=sp);
1126                         }
1127                 if (--sp < &stab[0]) {
1128                         if (around) {
1129                                 pperror("too many defines", 0);
1130                                 exit(exfail);
1131                         } else {
1132                                 ++around;
1133                                 sp = &stab[symsiz-1];
1134                         }
1135                 }
1136         }
1137         if (enterf>0)
1138                 sp->name=namep;
1139         return (lastsym=sp);
1140 }
1141 
1142 static struct symtab *
1143 slookup(p1,p2,enterf) register char *p1,*p2; int enterf;{
1144         register char *p3; char c2,c3; struct symtab *np;
1145         c2= *p2; *p2='\0';      /* mark end of token */
1146         if ((p2-p1)>symlen)
1147                 p3=p1+symlen;
1148         else
1149                 p3=p2;
1150         c3= *p3; *p3='\0';      /* truncate to symlen chars or less */
1151         if (enterf==1)
1152                 p1=copy(p1);
1153         np=lookup(p1,enterf); *p3=c3; *p2=c2;
1154         if (np->value!=0 && flslvl==0)
1155                 newp=subst(p2,np);
1156         else
1157                 newp=0;
1158         return(np);
1159 }
1160 
1161 /*
1162  * When a macro substitution must happen, arrange the input stack based on the
1163  * macro definition and any parameters such that the expanded macro is what is
1164  * next read by the preprocessor as if it were input
1165  */
1166 static char *
1167 subst(p,sp) register char *p; struct symtab *sp; {
1168         static char match[]="%s: argument mismatch";
1169         register char *ca,*vp; int params;
1170         char *actual[MAXFRM]; /* actual[n] is text of nth actual */
1171         char acttxt[BUFFERSIZ]; /* space for actuals */
1172         /* State while pasting, TRAIL is trailing space, INTRA is in the body */
1173         enum { TRAIL, INTRA } state = TRAIL;
1174         int pasted = 0;         /* # of character pasted */
1175 
1176         if (0==(vp=sp->value)) return(p);
1177         if ((p-macforw)<=macdam) {
1178                 if (++maclvl>symsiz && !rflag) {
1179                         pperror("%s: macro recursion",sp->name);
1180                         return(p);
1181                 }
1182         } else {
1183                 maclvl=0;       /* level decreased */
1184         }
1185         macforw=p; macdam=0;    /* new target for decrease in level */
1186         macnam=sp->name;
1187         /* flush all buffered output prior to the expansion */
1188         dump();
1189         if (sp==ulnloc) {
1190                 vp=acttxt; *vp++='\0';
1191                 sprintf(vp,"%d",lineno[ifno]); while (*vp++);
1192         } else if (sp==uflloc) {
1193                 vp=acttxt; *vp++='\0';
1194                 sprintf(vp,"\"%s\"",fnames[ifno]); while (*vp++);
1195         }
1196         if (0!=(params= *--vp&0xFF)) {/* definition calls for params */
1197                 register char **pa;
1198                 ca=acttxt; pa=actual;
1199                 if (params==0xFF)
1200                         params=1;       /* #define foo() ... */
1201                 sloscan();
1202                 ++flslvl; /* no expansion during search for actuals */
1203                 plvl= -1;
1204                 do p=skipbl(p); while (*inp=='\n');     /* skip \n too */
1205                 if (*inp=='(') {
1206                         maclin=lineno[ifno]; macfil=fnames[ifno];
1207                         for (plvl=1; plvl!=0; ) {
1208                                 *ca++='\0';
1209                                 for (;;) {
1210                                         outp=inp=p; p=cotoken(p);
1211                                         if (*inp=='(') ++plvl;
1212                                         if (*inp==')' && --plvl==0) {
1213                                                 --params;
1214                                                 break;
1215                                         }
1216                                         if (plvl==1 && *inp==',') {
1217                                                 --params;
1218                                                 break;
1219                                         }
1220                                         while (inp<p) {
1221                                                 /*
1222                                                  * Sun cpp compatibility.
1223                                                  * Needed for kernel assembler
1224                                                  * preprocessing.
1225                                                  * Replace newlines in actual
1226                                                  * macro parameters by spaces.
1227                                                  * Keep escaped newlines, they
1228                                                  * are assumed to be inside a
1229                                                  * string.
1230                                                  * 
1231                                                  * XXX: The above is actually
1232                                                  * false in a couple of ways.
1233                                                  *
1234                                                  * 1) Sun cpp turns newlines
1235                                                  * into spaces, but inserts an
1236                                                  * equal number of newlines
1237                                                  * prior to pasting the body.
1238                                                  *
1239                                                  * 2) Sun does _not_ preserved
1240                                                  * escaped newlines, the \ is
1241                                                  * removed, and the newline
1242                                                  * otherwise treated
1243                                                  * identically to in #1.
1244                                                  */
1245                                                 if (*inp == '\n' &&
1246                                                     inp[-1] != '\\')
1247                                                         *inp = ' ';
1248                                                 *ca++= *inp++;
1249                                         }
1250                                         if (ca> &acttxt[BUFFERSIZ])
1251                                                 pperror("%s: actuals too long",
1252                                                     sp->name);
1253                                 }
1254                                 if (pa>= &actual[MAXFRM])
1255                                         ppwarn(match,sp->name);
1256                                 else
1257                                         *pa++=ca;
1258                         }
1259                 }
1260                 if (params!=0)
1261                         ppwarn(match,sp->name);
1262                 while (--params>=0)
1263                         *pa++=""+1;     /* null string for missing actuals */
1264                 --flslvl; fasscan();
1265         }
1266 
1267         for (;;) {/* push definition onto front of input stack */
1268                 /*
1269                  * Loop until we hit the end of the macro, or a parameter
1270                  * placement.  Note that we expand the macro into the input
1271                  * backwards (so it replays forwards.)
1272                  */
1273                 while (!iswarn(*--vp)) {
1274                         if (bob(p)) {outp=inp=p; p=unfill(p);}
1275                                 
1276                         /* Unless we are mid-paste, swallow all spaces */
1277                         if (state == TRAIL) {
1278                                 while (isspace(*vp) && !iswarn(*vp))
1279                                         vp--;
1280                         } else {
1281                                 /*
1282                                  * If we're mid-paste, compress spaces to a
1283                                  * single space
1284                                  */
1285                                 while (isspace(*vp)) {
1286                                         if (!isspace(vp[1])) {
1287                                                 *vp = ' ';
1288                                                 break;
1289                                         } else {
1290                                                 vp--;
1291                                         }
1292                                 }
1293                         }
1294                         state = INTRA; /* Hit a non-space */
1295                         
1296                         if (iswarn(*vp))
1297                                 break;
1298                         *--p= *vp;
1299                         pasted++;
1300                 }
1301                 if (*vp==warnc) {/* insert actual param */
1302                         state = INTRA;
1303                         ca=actual[*--vp-1];
1304                         while (*--ca) {
1305                                 if (bob(p)) {outp=inp=p; p=unfill(p);}
1306                                 *--p= *ca;
1307                                 pasted++;
1308                         }
1309                 } else {
1310                         /*
1311                          * Trim leading spaces, but only those from our pasting
1312                          */
1313                         while (isspace(*p) && pasted > 0) {
1314                                 p++;
1315                                 pasted--;
1316                         }
1317                         break;
1318                 }
1319         }
1320         outp=inp=p;
1321         return(p);
1322 }
1323 
1324 static char *
1325 trmdir(s) register char *s; {
1326         register char *p = s;
1327         while (*p++); --p; while (p>s && *--p!='/');
1328 # if unix
1329         if (p==s) *p++='.';
1330 # endif
1331         *p='\0';
1332         return(s);
1333 }
1334 
1335 static char *
1336 copy(s) register char *s; {
1337         register char *old;
1338 
1339         old = savch; while ((*savch++ = *s++) != '\0');
1340         return(old);
1341 }
1342 
1343 static char *
1344 strdex(s,c) char *s,c; {
1345         while (*s) if (*s++==c) return(--s);
1346         return(0);
1347 }
1348 
1349 int
1350 yywrap() {
1351         return(1);
1352 }
1353 
1354 int
1355 main(argc,argv)
1356         char *argv[];
1357         int  argc;
1358 {
1359         register int i,c;
1360         register char *p;
1361         char *tf,**cp2;
1362         char *sysdir = NULL;
1363 
1364         fout = stdout;  /* Mac OS X is not POSIX compliant (stdout nonconst.) */
1365 
1366         p="_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
1367         i=0;
1368         while ((c= *p++) != '\0') {
1369                 (fastab+COFF)[c] |= IB|NB|SB;
1370                 (toktyp+COFF)[c]=IDENT;
1371 #if scw2
1372                 /*
1373                  * 53 == 63-10; digits rarely appear in identifiers,
1374                  * and can never be the first char of an identifier.
1375                  * 11 == 53*53/sizeof(macbit) .
1376                  */
1377                 ++i; (t21+COFF)[c]=(53*i)/11; (t22+COFF)[c]=i%11;
1378 #endif
1379         }
1380         p="0123456789.";
1381         while ((c = *p++) != '\0') {
1382                 (fastab+COFF)[c] |= NB|SB;
1383                 (toktyp+COFF)[c]=NUMBR;
1384         }
1385         p="\n\"'/\\";
1386         while ((c = *p++) != '\0')
1387                 (fastab+COFF)[c] |= SB;
1388         p="\n\"'\\";
1389         while ((c = *p++) != '\0')
1390                 (fastab+COFF)[c] |= QB;
1391         p="*\n";
1392         while ((c = *p++)!= '\0')
1393                 (fastab+COFF)[c] |= CB;
1394         (fastab+COFF)[(int)warnc] |= WB;
1395         (fastab+COFF)['\0'] |= CB|QB|SB|WB;
1396         for (i=ALFSIZ; --i>=0; )
1397                 slotab[i]=fastab[i]|SB;
1398         p=" \t\013\f\r";        /* note no \n; \v not legal for vertical tab? */
1399         while ((c = *p++) != '\0')
1400                 (toktyp+COFF)[c]=BLANK;
1401 #if scw2
1402         for ((t23+COFF)[i=ALFSIZ+7-COFF]=1; --i>=-COFF; )
1403                 if (((t23+COFF)[i]=(t23+COFF+1)[i]<<1)==0)
1404                         (t23+COFF)[i]=1;
1405 #endif
1406 
1407 # if unix
1408         fnames[ifno=0] = "";
1409         dirs[0]=dirnams[0]= ".";
1410 # endif
1411 # if ibm
1412         fnames[ifno=0] = "";
1413 # endif
1414 # if gimpel
1415         fnames[ifno=0] = (char *)inquire(stdin, _FILENAME);
1416         dirnams[0] = dirs[0] = trmdir(copy(fnames[0]));
1417 # endif
1418         for (i=1; i<argc; i++) {
1419                 switch(argv[i][0]) {
1420                 case '-':
1421                         switch(argv[i][1]) {
1422                         case 'P':
1423                                 pflag++;
1424                                 continue;
1425                         case 'E':
1426                                 continue;
1427                         case 'R':
1428                                 ++rflag;
1429                                 continue;
1430                         case 'C':
1431                                 passcom++;
1432                                 continue;
1433                         case 'D':
1434                                 if (predef>prespc+NPREDEF) {
1435                                         pperror("too many -D options, "
1436                                             "ignoring %s", argv[i]);
1437                                         continue;
1438                                 }
1439                                 /* ignore plain "-D" (no argument) */
1440                                 if (*(argv[i]+2))
1441                                         *predef++ = argv[i]+2;
1442                                 continue;
1443                         case 'U':
1444                                 if (prund>punspc+NPREDEF) {
1445                                         pperror("too many -U options, "
1446                                             "ignoring %s", argv[i]);
1447                                         continue;
1448                                 }
1449                                 *prund++ = argv[i]+2;
1450                                 continue;
1451                         case 'u':
1452                                 if (strcmp(argv[i], "-undef") == 0)
1453                                         nopredef = 1;
1454                                 else
1455                                         goto unknown;
1456                                 continue;
1457                         case 'I':
1458                                 if (nd>=MAXIDIRS)
1459                                         pperror("excessive -I file "
1460                                             "(%s) ignored", argv[i]);
1461                                 else
1462                                         dirs[nd++] = argv[i]+2;
1463                                 continue;
1464                         case 'T':
1465                                 symlen = 8;
1466                                 /* Compatibility with V7 */
1467                                 continue;
1468                         case 'H':
1469                                 /* Print included filenames */
1470                                 hflag++;
1471                                 continue;
1472                         case 'Y':
1473                                 /* Replace system include dir */
1474                                 sysdir = argv[i]+2;
1475                                 continue;
1476                         case '\0': continue;
1477                         default:
1478                         unknown:
1479                                 pperror("unknown flag %s", argv[i]);
1480                                 continue;
1481                         }
1482                 default:
1483                         if (fin == STDIN_FILENO) {
1484                                 if (0>(fin=open(argv[i], O_RDONLY))) {
1485                                         pperror("No source file %s",
1486                                             argv[i]);
1487                                         exit(8);
1488                                 }
1489                                 fnames[ifno]=copy(argv[i]);
1490                                 dirs[0]=dirnams[ifno]=trmdir(argv[i]);
1491 /* too dangerous to have file name in same syntactic position
1492    be input or output file depending on file redirections,
1493    so force output to stdout, willy-nilly
1494         [i don't see what the problem is.  jfr]
1495 */
1496                         } else if (fout==stdout) {
1497                                 static char _sobuff[BUFSIZ];
1498                                 if (NULL==(fout=fopen(argv[i], "w"))) {
1499                                         pperror("Can't create %s",
1500                                             argv[i]);
1501                                         exit(8);
1502                                 } else {
1503                                         fclose(stdout);
1504                                         setbuf(fout,_sobuff);
1505                                 }
1506                         } else {
1507                                 pperror("extraneous name %s", argv[i]);
1508                         }
1509                 }
1510         }
1511 
1512         fins[ifno]=fin;
1513         exfail = 0;
1514         /* after user -I files here are the standard include libraries */
1515         if (sysdir != NULL) {
1516                 dirs[nd++] = sysdir;
1517         } else {
1518 # if unix
1519         dirs[nd++] = "/usr/include";
1520 # endif
1521         /* dirs[nd++] = "/compool"; */
1522         }
1523         dirs[nd++] = 0;
1524         defloc=ppsym("define");
1525         udfloc=ppsym("undef");
1526         incloc=ppsym("include");
1527         elsloc=ppsym("else");
1528         eifloc=ppsym("endif");
1529         elifloc=ppsym("elif");
1530         ifdloc=ppsym("ifdef");
1531         ifnloc=ppsym("ifndef");
1532         ifloc=ppsym("if");
1533         lneloc=ppsym("line");
1534         idtloc=ppsym("ident");
1535         pragmaloc=ppsym("pragma");
1536         errorloc=ppsym("error");
1537         for (i=sizeof(macbit)/sizeof(macbit[0]); --i>=0; )
1538                 macbit[i]=0;
1539 
1540         if (! nopredef) {
1541         ysysloc=stsym("unix");
1542         ysysloc=stsym("sun");
1543 # if __sparc__
1544         varloc=stsym ("sparc");
1545 # endif
1546 # if __i386__
1547         varloc=stsym ("i386");
1548 # endif
1549         }
1550         ulnloc=stsym ("__LINE__");
1551         uflloc=stsym ("__FILE__");
1552         varloc=stsym ("__BUILTIN_VA_ARG_INCR");
1553 
1554         tf=fnames[ifno]; fnames[ifno]="command line"; lineno[ifno]=1;
1555         cp2=prespc;
1556         while (cp2<predef) stsym(*cp2++);
1557         cp2=punspc;
1558         while (cp2<prund) {
1559                 if ((p=strdex(*cp2, '=')) != NULL) *p++='\0';
1560                 if (strlen(*cp2) > symlen)
1561                         (*cp2)[symlen] = '\0';
1562                 lookup(*cp2++, DROP);
1563         }
1564         fnames[ifno]=tf;
1565         pbeg=buffer+symlen; pbuf=pbeg+BUFFERSIZ; pend=pbuf+BUFFERSIZ;
1566 
1567         trulvl = 0; flslvl = 0;
1568         lineno[0] = 1; sayline(NOINCLUDE);
1569         outp=inp=pend;
1570         control(pend);
1571         return (exfail);
1572 }