Quick Search:

Mode

Context

Displaying the whole file. None | Less | More | Full

Other Diffs

Ignore

Blank Lines Whitespace: Expand:

Diff

1.185
 
1.186
 
MAIN:plunky:20121108110838
 
cpp.c
_>11 /*      $Id$    */
 22 
 33 /*
 44  * Copyright (c) 2004,2010 Anders Magnusson (ragge@ludd.luth.se).
 55  * All rights reserved.
 66  *
 77  * Redistribution and use in source and binary forms, with or without
 88  * modification, are permitted provided that the following conditions
 99  * are met:
 1010  * 1. Redistributions of source code must retain the above copyright
 1111  *    notice, this list of conditions and the following disclaimer.
 1212  * 2. Redistributions in binary form must reproduce the above copyright
 1313  *    notice, this list of conditions and the following disclaimer in the
 1414  *    documentation and/or other materials provided with the distribution.
 1515  *
 1616  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 1717  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 1818  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 1919  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 2020  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 2121  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 2222  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 2323  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 2424  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 2525  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 2626  */
 2727 
 2828 /*
 2929  * The C preprocessor.
 3030  * This code originates from the V6 preprocessor with some additions
 3131  * from V7 cpp, and at last ansi/c99 support.
 3232  */
 3333 
 3434 #include "config.h"
 3535 
 3636 #include <sys/stat.h>
 3737 
 3838 #include <fcntl.h>
 3939 #ifdef HAVE_UNISTD_H
 4040 #include <unistd.h>
 4141 #endif
 4242 #include <stdio.h>
 4343 #include <stdarg.h>
 4444 #include <stdlib.h>
 4545 #include <string.h>
 4646 #include <time.h>
 4747 
 4848 #include "compat.h"
 4949 #include "cpp.h"
 5050 #include "cpy.h"
 5151 
 5252 #ifndef S_ISDIR
 5353 #define S_ISDIR(m)      (((m) & S_IFMT) == S_IFDIR)
 5454 #endif
 5555 
 5656 #define SBSIZE  1000000
 5757 
 5858 static const char versstr[] = "PCC preprocessor version " VERSSTR "\n";
 5959 
 6060 static usch     sbf[SBSIZE];
 6161 /* C command */
 6262 
 6363 int tflag;      /* traditional cpp syntax */
 6464 #ifdef PCC_DEBUG
 6565 int dflag;      /* debug printouts */
 6666 static void imp(const char *);
 6767 static void prline(const usch *s);
 6868 static void prrep(const usch *s);
 6969 #define DPRINT(x) if (dflag) printf x
 7070 #define DDPRINT(x) if (dflag > 1) printf x
 7171 #define IMP(x) if (dflag > 1) imp(x)
 7272 #else
 7373 #define DPRINT(x)
 7474 #define DDPRINT(x)
 7575 #define IMP(x)
 7676 #endif
 7777 
 7878 int ofd;
<>79 -usch outbuf[CPPBUF];
  79+usch outbuf[CPPBUF], lastoch;
8080 int obufp, istty;
 8181 int Cflag, Mflag, dMflag, Pflag, MPflag;
 8282 usch *Mfile, *MPfile, *Mxfile;
 8383 struct initar *initar;
<>84 -int readmac, lastoch;
  84+int readmac;
<_8585 int defining;
 8686 
 8787 /* include dirs */
 8888 struct incs {
 8989         struct incs *next;
 9090         usch *dir;
 9191         dev_t dev;
 9292         ino_t ino;
 9393 } *incdir[2];
 9494 
 9595 static struct symtab *filloc;
 9696 static struct symtab *linloc;
 9797 static struct symtab *pragloc;
 9898 int     trulvl;
 9999 int     flslvl;
 100100 int     elflvl;
 101101 int     elslvl;
 102102 usch *stringbuf = sbf;
 103103 
 104104 /*
 105105  * Macro replacement list syntax:
 106106  * - For object-type macros, replacement strings are stored as-is.
 107107  * - For function-type macros, macro args are substituted for the
 108108  *   character WARN followed by the argument number.
 109109  * - The value element points to the end of the string, to simplify
 110110  *   pushback onto the input queue.
 111111  *
 112112  * The first character (from the end) in the replacement list is
 113113  * the number of arguments:
 114114  *   VARG  - ends with ellipsis, next char is argcount without ellips.
 115115  *   OBJCT - object-type macro
 116116  *   0     - empty parenthesis, foo()
 117117  *   1->   - number of args.
 118118  *
 119119  * WARN is used:
 120120  *      - in stored replacement lists to tell that an argument comes
 121121  *      - When expanding replacement lists to tell that the list ended.
 122122  *
 123123  * To ensure that an already expanded identifier won't get expanded
 124124  * again a EBLOCK char + its number is stored directly before any
 125125  * expanded identifier.
 126126  */
 127127 
 128128 /* args for lookup() */
 129129 #define FIND    0
 130130 #define ENTER   1
 131131 
 132132 /*
 133133  * No-replacement array.  If a macro is found and exists in this array
 134134  * then no replacement shall occur.  This is a stack.
 135135  */
 136136 struct symtab *norep[RECMAX];   /* Symbol table index table */
 137137 int norepptr = 1;                       /* Top of index table */
 138138 unsigned short bptr[RECMAX];    /* currently active noexpand macro stack */
 139139 int bidx;                       /* Top of bptr stack */
 140140 
 141141 static int readargs(struct symtab *sp, const usch **args);
 142142 static void exparg(int);
 143143 static void subarg(struct symtab *sp, const usch **args, int);
 144144 static void flbuf(void);
 145145 static void usage(void);
 146146 static usch *xstrdup(const usch *str);
 147147 static void addidir(char *idir, struct incs **ww);
 148148 static void vsheap(const char *, va_list);
 149149 
 150150 int
 151151 main(int argc, char **argv)
 152152 {
 153153         struct initar *it;
 154154         struct symtab *nl;
 155155         register int ch;
 156156         const usch *fn1, *fn2;
 157157 
 158158 #ifdef TIMING
 159159         struct timeval t1, t2;
 160160 
 161161         (void)gettimeofday(&t1, NULL);
 162162 #endif
 163163 
 164164         while ((ch = getopt(argc, argv, "CD:d:I:i:MPS:tU:Vvx:")) != -1) {
 165165                 switch (ch) {
 166166                 case 'C': /* Do not discard comments */
 167167                         Cflag++;
 168168                         break;
 169169 
 170170                 case 'D': /* define something */
 171171                 case 'i': /* include */
 172172                 case 'U': /* undef */
 173173                         /* XXX should not need malloc() here */
 174174                         if ((it = malloc(sizeof(struct initar))) == NULL)
 175175                                 error("couldn't apply -%c %s", ch, optarg);
 176176                         it->type = ch;
 177177                         it->str = optarg;
 178178                         it->next = initar;
 179179                         initar = it;
 180180                         break;
 181181 
 182182                 case 'd':
 183183                         while (*optarg) {
 184184                                 switch(*optarg) {
 185185                                 case 'M': /* display macro definitions */
 186186                                         dMflag = 1;
 187187                                         Mflag = 1;
 188188                                         break;
 189189 
 190190                                 default: /* ignore others */
 191191                                         break;
 192192                                 }
 193193                                 optarg++;
 194194                         }
 195195                         break;
 196196 
 197197                 case 'I':
 198198                 case 'S':
 199199                         addidir(optarg, &incdir[ch == 'I' ? INCINC : SYSINC]);
 200200                         break;
 201201 
 202202                 case 'M': /* Generate dependencies for make */
 203203                         Mflag++;
 204204                         break;
 205205 
 206206                 case 'P': /* Inhibit generation of line numbers */
 207207                         Pflag++;
 208208                         break;
 209209 
 210210                 case 't':
 211211                         tflag = 1;
 212212                         break;
 213213 
 214214 #ifdef PCC_DEBUG
 215215                 case 'V':
 216216                         dflag++;
 217217                         break;
 218218 #endif
 219219                 case 'v':
 220220                         xwrite(2, versstr, sizeof(versstr) - 1);
 221221                         break;
 222222 
 223223                 case 'x':
 224224                         if (strcmp(optarg, "MP") == 0) {
 225225                                 MPflag++;
 226226                         } else if (strncmp(optarg, "MT,", 3) == 0 ||
 227227                             strncmp(optarg, "MQ,", 3) == 0) {
 228228                                 usch *cp, *fn;
 229229                                 fn = stringbuf;
 230230                                 for (cp = (usch *)&optarg[3]; *cp; cp++) {
 231231                                         if (*cp == '$' && optarg[1] == 'Q')
 232232                                                 savch('$');
 233233                                         savch(*cp);
 234234                                 }
 235235                                 savstr((const usch *)"");
 236236                                 if (Mxfile) { savch(' '); savstr(Mxfile); }
 237237                                 savch(0);
 238238                                 Mxfile = fn;
 239239                         } else
 240240                                 usage();
 241241                         break;
 242242 
 243243                 case '?':
 244244                 default:
 245245                         usage();
 246246                 }
 247247         }
 248248 
 249249         argc -= optind;
 250250         argv += optind;
 251251 
 252252         filloc = lookup((const usch *)"__FILE__", ENTER);
 253253         linloc = lookup((const usch *)"__LINE__", ENTER);
 254254         filloc->value = linloc->value = stringbuf;
 255255         savch(OBJCT);
 256256 
 257257         /* create a complete macro for pragma */
 258258         pragloc = lookup((const usch *)"_Pragma", ENTER);
 259259         savch(0);
 260260         savstr((const usch *)"_Pragma(");
 261261         savch(0);
 262262         savch(WARN);
 263263         savch(')');
 264264         pragloc->value = stringbuf;
 265265         savch(1);
 266266 
 267267         if (tflag == 0) {
 268268                 time_t t = time(NULL);
 269269                 usch *n = (usch *)ctime(&t);
 270270 
 271271                 /*
 272272                  * Manually move in the predefined macros.
 273273                  */
 274274                 nl = lookup((const usch *)"__TIME__", ENTER);
 275275                 savch(0); savch('"');  n[19] = 0; savstr(&n[11]); savch('"');
 276276                 savch(OBJCT);
 277277                 nl->value = stringbuf-1;
 278278 
 279279                 nl = lookup((const usch *)"__DATE__", ENTER);
 280280                 savch(0); savch('"'); n[24] = n[11] = 0; savstr(&n[4]);
 281281                 savstr(&n[20]); savch('"'); savch(OBJCT);
 282282                 nl->value = stringbuf-1;
 283283 
 284284                 nl = lookup((const usch *)"__STDC__", ENTER);
 285285                 savch(0); savch('1'); savch(OBJCT);
 286286                 nl->value = stringbuf-1;
 287287 
 288288                 nl = lookup((const usch *)"__STDC_VERSION__", ENTER);
 289289                 savch(0); savstr((const usch *)"199901L"); savch(OBJCT);
 290290                 nl->value = stringbuf-1;
 291291         }
 292292 
 293293         if (Mflag && !dMflag) {
 294294                 usch *c;
 295295 
 296296                 if (argc < 1)
 297297                         error("-M and no infile");
 298298                 if ((c = (usch *)strrchr(argv[0], '/')) == NULL)
 299299                         c = (usch *)argv[0];
 300300                 else
 301301                         c++;
 302302                 Mfile = stringbuf;
 303303                 savstr(c); savch(0);
 304304                 if (MPflag) {
 305305                         MPfile = stringbuf;
 306306                         savstr(c); savch(0);
 307307                 }
 308308                 if (Mxfile)
 309309                         Mfile = Mxfile;
 310310                 if ((c = (usch *)strrchr((char *)Mfile, '.')) == NULL)
 311311                         error("-M and no extension: ");
 312312                 c[1] = 'o';
 313313                 c[2] = 0;
 314314         }
 315315 
 316316         if (argc == 2) {
 317317                 if ((ofd = open(argv[1], O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)
 318318                         error("Can't creat %s", argv[1]);
 319319         } else
 320320                 ofd = 1; /* stdout */
 321321         istty = isatty(ofd);
 322322 
 323323         if (argc && strcmp(argv[0], "-")) {
 324324                 fn1 = fn2 = (usch *)argv[0];
 325325         } else {
 326326                 fn1 = NULL;
 327327                 fn2 = (const usch *)"";
 328328         }
 329329         if (pushfile(fn1, fn2, 0, NULL))
 330330                 error("cannot open %s", argv[0]);
 331331 
 332332         flbuf();
 333333         close(ofd);
 334334 #ifdef TIMING
 335335         (void)gettimeofday(&t2, NULL);
 336336         t2.tv_sec -= t1.tv_sec;
 337337         t2.tv_usec -= t1.tv_usec;
 338338         if (t2.tv_usec < 0) {
 339339                 t2.tv_usec += 1000000;
 340340                 t2.tv_sec -= 1;
 341341         }
 342342         fprintf(stderr, "cpp total time: %ld s %ld us\n",
 343343              (long)t2.tv_sec, (long)t2.tv_usec);
 344344 #endif
 345345         return 0;
 346346 }
 347347 
 348348 static void
 349349 addidir(char *idir, struct incs **ww)
 350350 {
 351351         struct incs *w;
 352352         struct stat st;
 353353 
 354354         if (stat(idir, &st) == -1 || !S_ISDIR(st.st_mode))
 355355                 return; /* ignore */
 356356         if (*ww != NULL) {
 357357                 for (w = *ww; w->next; w = w->next) {
 358358 #ifdef os_win32
 359359                         if (strcmp(w->dir, idir) == 0)
 360360                                 return;
 361361 #else
 362362                         if (w->dev == st.st_dev && w->ino == st.st_ino)
 363363                                 return;
 364364 #endif
 365365                 }
 366366 #ifdef os_win32
 367367                 if (strcmp(w->dir, idir) == 0)
 368368                         return;
 369369 #else
 370370                 if (w->dev == st.st_dev && w->ino == st.st_ino)
 371371                         return;
 372372 #endif
 373373                 ww = &w->next;
 374374         }
 375375         if ((w = calloc(sizeof(struct incs), 1)) == NULL)
 376376                 error("couldn't add path %s", idir);
 377377         w->dir = (usch *)idir;
 378378         w->dev = st.st_dev;
 379379         w->ino = st.st_ino;
 380380         *ww = w;
 381381 }
 382382 
 383383 void
 384384 line(void)
 385385 {
 386386         static usch *lbuf;
 387387         static int llen;
 388388         usch *p;
 389389         int c;
 390390 
 391391         if ((c = yylex()) != NUMBER)
 392392                 goto bad;
 393393         ifiles->lineno = (int)(yylval.node.nd_val - 1);
 394394         ifiles->escln = 0;
 395395 
 396396         if ((c = yylex()) == '\n')
 397397                 goto okret;
 398398 
 399399         if (c != STRING)
 400400                 goto bad;
 401401 
 402402         p = yytext;
 403403         if (*p++ == 'L')
 404404                 p++;
 405405         c = strlen((char *)p);
 406406         p[c - 1] = '\0';
 407407         if (llen < c) {
 408408                 /* XXX may lose heap space */
 409409                 lbuf = stringbuf;
 410410                 stringbuf += c;
 411411                 llen = c;
 412412                 if (stringbuf >= &sbf[SBSIZE]) {
 413413                         stringbuf = sbf; /* need space to write error message */
 414414                         error("#line filename exceeds buffer size");
 415415                 }
 416416         }
 417417         memcpy(lbuf, p, c);
 418418         ifiles->fname = lbuf;
 419419         if (yylex() != '\n')
 420420                 goto bad;
 421421 
 422422 okret:  prtline();
 423423         return;
 424424 
 425425 bad:    error("bad #line");
 426426 }
 427427 
 428428 /*
 429429  * Search for and include next file.
 430430  * Return 1 on success.
 431431  */
 432432 static int
 433433 fsrch(const usch *fn, int idx, struct incs *w)
 434434 {
 435435         int i;
 436436 
 437437         for (i = idx; i < 2; i++) {
 438438                 if (i > idx)
 439439                         w = incdir[i];
 440440                 for (; w; w = w->next) {
 441441                         usch *nm = stringbuf;
 442442 
 443443                         savstr(w->dir); savch('/');
 444444                         savstr(fn); savch(0);
 445445                         if (pushfile(nm, fn, i, w->next) == 0)
 446446                                 return 1;
 447447                         stringbuf = nm;
 448448                 }
 449449         }
 450450         return 0;
 451451 }
 452452 
 453453 static void
 454454 prem(void)
 455455 {
 456456         error("premature EOF");
 457457 }
 458458 
 459459 /*
 460460  * Include a file. Include order:
 461461  * - For <...> files, first search -I directories, then system directories.
 462462  * - For "..." files, first search "current" dir, then as <...> files.
 463463  */
 464464 void
 465465 include(void)
 466466 {
 467467         struct symtab *nl;
 468468         usch *osp;
 469469         usch *fn, *safefn;
 470470         int c;
 471471 
 472472         if (flslvl)
 473473                 return;
 474474         osp = stringbuf;
 475475 
 476476         while ((c = sloscan()) == WSPACE)
 477477                 ;
 478478         if (c == IDENT) {
 479479                 /* sloscan() will not expand idents */
 480480                 if ((nl = lookup(yytext, FIND)) == NULL)
 481481                         goto bad;
 482482                 if (kfind(nl))
 483483                         unpstr(stringbuf);
 484484                 else
 485485                         unpstr(nl->namep);
 486486                 stringbuf = osp;
 487487                 c = yylex();
 488488         }
 489489 
 490490         if (c == '<') {
 491491                 fn = stringbuf;
 492492                 while ((c = sloscan()) != '>') {
 493493                         if (c == 0)
 494494                                 prem();
 495495                         if (c == '\n')
 496496                                 goto bad;
 497497                         savstr(yytext);
 498498                 }
 499499                 savch('\0');
 500500                 while ((c = sloscan()) == WSPACE)
 501501                         ;
 502502                 if (c == 0)
 503503                         prem();
 504504                 if (c != '\n')
 505505                         goto bad;
 506506                 safefn = fn;
 507507         } else if (c == STRING) {
 508508                 usch *nm = stringbuf;
 509509 
 510510                 fn = yytext;
 511511                 if (*fn++ == 'L')
 512512                         fn++;
 513513                 fn[strlen((char *)fn) - 1] = 0;
 514514                 /* first try to open file relative to previous file */
 515515                 /* but only if it is not an absolute path */
 516516                 if (*fn != '/') {
 517517                         savstr(ifiles->orgfn);
 518518                         if ((stringbuf =
 519519                             (usch *)strrchr((char *)nm, '/')) == NULL)
 520520                                 stringbuf = nm;
 521521                         else
 522522                                 stringbuf++;
 523523                 }
 524524                 safefn = stringbuf;
 525525                 savstr(fn); savch(0);
 526526                 c = yylex();
 527527                 if (c == 0)
 528528                         prem();
 529529                 if (c != '\n')
 530530                         goto bad;
 531531                 if (pushfile(nm, safefn, 0, NULL) == 0)
 532532                         goto okret;
 533533                 /* XXX may lose stringbuf space */
 534534         } else
 535535                 goto bad;
 536536 
 537537         if (fsrch(safefn, 0, incdir[0]))
 538538                 goto okret;
 539539 
 540540         error("cannot find '%s'", safefn);
 541541         /* error() do not return */
 542542 
 543543 bad:    error("bad #include");
 544544         /* error() do not return */
 545545 okret:
 546546         prtline();
 547547 }
 548548 
 549549 void
 550550 include_next(void)
 551551 {
 552552         struct symtab *nl;
 553553         usch *osp;
 554554         usch *fn;
 555555         int c;
 556556 
 557557         if (flslvl)
 558558                 return;
 559559         osp = stringbuf;
 560560         while ((c = sloscan()) == WSPACE)
 561561                 ;
 562562         if (c == IDENT) {
 563563                 /* sloscan() will not expand idents */
 564564                 if ((nl = lookup(yytext, FIND)) == NULL)
 565565                         goto bad;
 566566                 if (kfind(nl))
 567567                         unpstr(stringbuf);
 568568                 else
 569569                         unpstr(nl->namep);
 570570                 stringbuf = osp;
 571571                 c = yylex();
 572572         }
 573573         if (c != STRING && c != '<')
 574574                 goto bad;
 575575 
 576576         fn = stringbuf;
 577577         if (c == STRING) {
 578578                 savstr(&yytext[1]);
 579579                 stringbuf[-1] = 0;
 580580         } else { /* < > */
 581581                 while ((c = sloscan()) != '>') {
 582582                         if (c == '\n')
 583583                                 goto bad;
 584584                         savstr(yytext);
 585585                 }
 586586                 savch('\0');
 587587         }
 588588         while ((c = sloscan()) == WSPACE)
 589589                 ;
 590590         if (c != '\n')
 591591                 goto bad;
 592592 
 593593         if (fsrch(fn, ifiles->idx, ifiles->incs) == 0)
 594594                 error("cannot find '%s'", fn);
 595595         prtline();
 596596         return;
 597597 
 598598 bad:    error("bad #include_next");
 599599         /* error() do not return */
 600600 }
 601601 
 602602 static int
 603603 definp(void)
 604604 {
 605605         int c;
 606606 
 607607         do
 608608                 c = sloscan();
 609609         while (c == WSPACE);
 610610         return c;
 611611 }
 612612 
 613613 void
 614614 getcmnt(void)
 615615 {
 616616         int c;
 617617 
 618618         savstr(yytext);
 619619         savch(cinput()); /* Lost * */
 620620         for (;;) {
 621621                 c = cinput();
 622622                 if (c == '*') {
 623623                         c = cinput();
 624624                         if (c == '/') {
 625625                                 savstr((const usch *)"*/");
 626626                                 return;
 627627                         }
 628628                         cunput(c);
 629629                         c = '*';
 630630                 }
 631631                 savch(c);
 632632         }
 633633 }
 634634 
 635635 /*
 636636  * Compare two replacement lists, taking in account comments etc.
 637637  */
 638638 static int
 639639 cmprepl(const usch *o, const usch *n)
 640640 {
 641641         for (; *o; o--, n--) {
 642642                 /* comment skip */
 643643                 if (*o == '/' && o[-1] == '*') {
 644644                         while (*o != '*' || o[-1] != '/')
 645645                                 o--;
 646646                         o -= 2;
 647647                 }
 648648                 if (*n == '/' && n[-1] == '*') {
 649649                         while (*n != '*' || n[-1] != '/')
 650650                                 n--;
 651651                         n -= 2;
 652652                 }
 653653                 while (*o == ' ' || *o == '\t')
 654654                         o--;
 655655                 while (*n == ' ' || *n == '\t')
 656656                         n--;
 657657                 if (*o != *n)
 658658                         return 1;
 659659         }
 660660         return 0;
 661661 }
 662662 
 663663 static int
 664664 isell(void)
 665665 {
 666666         int ch;
 667667 
 668668         if ((ch = cinput()) != '.') {
 669669                 cunput(ch);
 670670                 return 0;
 671671         }
 672672         if ((ch = cinput()) != '.') {
 673673                 cunput(ch);
 674674                 cunput('.');
 675675                 return 0;
 676676         }
 677677         return 1;
 678678 }
 679679 
 680680 void
 681681 define(void)
 682682 {
 683683         struct symtab *np;
 684684         usch *args[MAXARGS+1], *ubuf, *sbeg;
 685685         int c, i, redef;
 686686         int mkstr = 0, narg = -1;
 687687         int ellips = 0;
 688688 #ifdef GCC_COMPAT
 689689         usch *gccvari = NULL;
 690690         int wascon;
 691691 #endif
 692692 
 693693         if (flslvl)
 694694                 return;
 695695         if (sloscan() != WSPACE || sloscan() != IDENT)
 696696                 goto bad;
 697697 
 698698         np = lookup(yytext, ENTER);
 699699         redef = np->value != NULL;
 700700 
 701701         defining = readmac = 1;
 702702         sbeg = stringbuf;
 703703         if ((c = sloscan()) == '(') {
 704704                 narg = 0;
 705705                 /* function-like macros, deal with identifiers */
 706706                 c = definp();
 707707                 for (;;) {
 708708                         if (c == ')')
 709709                                 break;
 710710                         if (c == '.' && isell()) {
 711711                                 ellips = 1;
 712712                                 if (definp() != ')')
 713713                                         goto bad;
 714714                                 break;
 715715                         }
 716716                         if (c == IDENT) {
 717717                                 /* make sure there is no arg of same name */
 718718                                 for (i = 0; i < narg; i++)
 719719                                         if (!strcmp((char *) args[i], (char *)yytext))
 720720                                                 error("Duplicate macro "
 721721                                                   "parameter \"%s\"", yytext);
 722722                                 if (narg == MAXARGS)
 723723                                         error("Too many macro args");
 724724                                 args[narg++] = xstrdup(yytext);
 725725                                 if ((c = definp()) == ',') {
 726726                                         if ((c = definp()) == ')')
 727727                                                 goto bad;
 728728                                         continue;
 729729                                 }
 730730 #ifdef GCC_COMPAT
 731731                                 if (c == '.' && isell()) {
 732732                                         if (definp() != ')')
 733733                                                 goto bad;
 734734                                         gccvari = args[--narg];
 735735                                         break;
 736736                                 }
 737737 #endif
 738738                                 if (c == ')')
 739739                                         break;
 740740                         }
 741741                         goto bad;
 742742                 }
 743743                 c = sloscan();
 744744         } else if (c == '\n') {
 745745                 /* #define foo */
 746746                 ;
 747747         } else if (c == 0) {
 748748                 prem();
 749749         } else if (c != WSPACE)
 750750                 goto bad;
 751751 
 752752         while (c == WSPACE)
 753753                 c = sloscan();
 754754 
 755755         /* replacement list cannot start with ## operator */
 756756         if (c == '#') {
 757757                 if ((c = sloscan()) == '#')
 758758                         goto bad;
 759759                 savch('\0');
 760760 #ifdef GCC_COMPAT
 761761                 wascon = 0;
 762762 #endif
 763763                 goto in2;
 764764         }
 765765 
 766766         /* parse replacement-list, substituting arguments */
 767767         savch('\0');
 768768         while (c != '\n') {
 769769 #ifdef GCC_COMPAT
 770770                 wascon = 0;
 771771 loop:
 772772 #endif
 773773                 switch (c) {
 774774                 case WSPACE:
 775775                         /* remove spaces if it surrounds a ## directive */
 776776                         ubuf = stringbuf;
 777777                         savstr(yytext);
 778778                         c = sloscan();
 779779                         if (c == '#') {
 780780                                 if ((c = sloscan()) != '#')
 781781                                         goto in2;
 782782                                 stringbuf = ubuf;
 783783                                 savch(CONC);
 784784                                 if ((c = sloscan()) == WSPACE)
 785785                                         c = sloscan();
 786786 #ifdef GCC_COMPAT
 787787                                 if (c == '\n')
 788788                                         break;
 789789                                 wascon = 1;
 790790                                 goto loop;
 791791 #endif
 792792                         }
 793793                         continue;
 794794 
 795795                 case '#':
 796796                         c = sloscan();
 797797                         if (c == '#') {
 798798                                 /* concat op */
 799799                                 savch(CONC);
 800800                                 if ((c = sloscan()) == WSPACE)
 801801                                         c = sloscan();
 802802 #ifdef GCC_COMPAT
 803803                                 if (c == '\n')
 804804                                         break;
 805805                                 wascon = 1;
 806806                                 goto loop;
 807807 #else
 808808                                 continue;
 809809 #endif
 810810                         }
 811811 in2:                    if (narg < 0) {
 812812                                 /* no meaning in object-type macro */
 813813                                 savch('#');
 814814                                 continue;
 815815                         }
 816816                         /* remove spaces between # and arg */
 817817                         savch(SNUFF);
 818818                         if (c == WSPACE)
 819819                                 c = sloscan(); /* whitespace, ignore */
 820820                         mkstr = 1;
 821821                         if (c == IDENT && strcmp((char *)yytext, "__VA_ARGS__") == 0)
 822822                                 continue;
 823823 
 824824                         /* FALLTHROUGH */
 825825                 case IDENT:
 826826                         if (strcmp((char *)yytext, "__VA_ARGS__") == 0) {
 827827                                 if (ellips == 0)
 828828                                         error("unwanted %s", yytext);
 829829 #ifdef GCC_COMPAT
 830830                                 savch(wascon ? GCCARG : VARG);
 831831 #else
 832832                                 savch(VARG);
 833833 #endif
 834834 
 835835                                 savch(WARN);
 836836                                 if (mkstr)
 837837                                         savch(SNUFF), mkstr = 0;
 838838                                 break;
 839839                         }
 840840                         if (narg < 0)
 841841                                 goto id; /* just add it if object */
 842842                         /* check if its an argument */
 843843                         for (i = 0; i < narg; i++)
 844844                                 if (strcmp((char *)yytext, (char *)args[i]) == 0)
 845845                                         break;
 846846                         if (i == narg) {
 847847 #ifdef GCC_COMPAT
 848848                                 if (gccvari &&
 849849                                     strcmp((char *)yytext, (char *)gccvari) == 0) {
 850850                                         savch(wascon ? GCCARG : VARG);
 851851                                         savch(WARN);
 852852                                         if (mkstr)
 853853                                                 savch(SNUFF), mkstr = 0;
 854854                                         break;
 855855                                 }
 856856 #endif
 857857                                 if (mkstr)
 858858                                         error("not argument");
 859859                                 goto id;
 860860                         }
 861861                         savch(i);
 862862                         savch(WARN);
 863863                         if (mkstr)
 864864                                 savch(SNUFF), mkstr = 0;
 865865                         break;
 866866 
 867867                 case CMNT: /* save comments */
 868868                         getcmnt();
 869869                         break;
 870870 
 871871                 case 0:
 872872                         prem();
 873873 
 874874                 default:
 875875 id:                     savstr(yytext);
 876876                         break;
 877877                 }
 878878                 c = sloscan();
 879879         }
 880880         defining = readmac = 0;
 881881         /* remove trailing whitespace */
 882882         while (stringbuf > sbeg) {
 883883                 if (stringbuf[-1] == ' ' || stringbuf[-1] == '\t')
 884884                         stringbuf--;
 885885                 /* replacement list cannot end with ## operator */
 886886                 else if (stringbuf[-1] == CONC)
 887887                         goto bad;
 888888                 else
 889889                         break;
 890890         }
 891891 #ifdef GCC_COMPAT
 892892         if (gccvari) {
 893893                 savch(narg);
 894894                 savch(VARG);
 895895         } else
 896896 #endif
 897897         if (ellips) {
 898898                 savch(narg);
 899899                 savch(VARG);
 900900         } else
 901901                 savch(narg < 0 ? OBJCT : narg);
 902902         if (redef && ifiles->idx != SYSINC) {
 903903                 if (cmprepl(np->value, stringbuf-1)) {
 904904                         sbeg = stringbuf;
 905905                         np->value = stringbuf-1;
 906906                         warning("%s redefined (previously defined at \"%s\" line %d)",
 907907                             np->namep, np->file, np->line);
 908908                 }
 909909                 stringbuf = sbeg/* forget this space */
 910910         } else
 911911                 np->value = stringbuf-1;
 912912 
 913913 #ifdef PCC_DEBUG
 914914         if (dflag) {
 915915                 const usch *w = np->value;
 916916 
 917917                 printf("!define: ");
 918918                 if (*w == OBJCT)
 919919                         printf("[object]");
 920920                 else if (*w == VARG)
 921921                         printf("[VARG%d]", *--w);
 922922                 while (*--w) {
 923923                         switch (*w) {
 924924                         case WARN: printf("<%d>", *--w); break;
 925925                         case CONC: printf("<##>"); break;
 926926                         case SNUFF: printf("<\">"); break;
 927927                         default: putchar(*w); break;
 928928                         }
 929929                 }
 930930                 putchar('\n');
 931931         }
 932932 #endif
 933933         for (i = 0; i < narg; i++)
 934934                 free(args[i]);
 935935         return;
 936936 
 937937 bad:    error("bad #define");
 938938 }
 939939 
 940940 void
 941941 warning(const char *fmt, ...)
 942942 {
 943943         va_list ap;
 944944         usch *sb;
 945945 
 946946         flbuf();
 947947         savch(0);
 948948 
 949949         sb = stringbuf;
 950950         if (ifiles != NULL)
 951951                 sheap("%s:%d: warning: ", ifiles->fname, ifiles->lineno);
 952952 
 953953         va_start(ap, fmt);
 954954         vsheap(fmt, ap);
 955955         va_end(ap);
 956956         savch('\n');
 957957         xwrite(2, sb, stringbuf - sb);
 958958         stringbuf = sb;
 959959 }
 960960 
 961961 void
 962962 error(const char *fmt, ...)
 963963 {
 964964         va_list ap;
 965965         usch *sb;
 966966 
 967967         flbuf();
 968968         savch(0);
 969969 
 970970         sb = stringbuf;
 971971         if (ifiles != NULL)
 972972                 sheap("%s:%d: error: ", ifiles->fname, ifiles->lineno);
 973973 
 974974         va_start(ap, fmt);
 975975         vsheap(fmt, ap);
 976976         va_end(ap);
 977977         savch('\n');
 978978         xwrite(2, sb, stringbuf - sb);
 979979         stringbuf = sb;
 980980 
 981981         exit(1);
 982982 }
 983983 
 984984 static void
 985985 sss(void)
 986986 {
 987987         savch(EBLOCK);
 988988         savch(cinput());
 989989         savch(cinput());
 990990 }
 991991 
 992992 static int
 993993 addmac(struct symtab *sp)
 994994 {
 995995         int c, i;
 996996 
 997997         /* Check if it exists; then save some space */
 998998         /* May be more difficult to debug cpp */
 999999         for (i = 1; i < norepptr; i++)
 10001000                 if (norep[i] == sp)
 10011001                         return i;
 10021002         if (norepptr >= RECMAX)
 10031003                 error("too many macros");
 10041004         /* check norepptr */
 10051005         if ((norepptr & 255) == 0)
 10061006                 norepptr++;
 10071007         if (((norepptr >> 8) & 255) == 0)
 10081008                 norepptr += 256;
 10091009         c = norepptr;
 10101010         norep[norepptr++] = sp;
 10111011         return c;
 10121012 }
 10131013 
 10141014 static void
 10151015 doblk(void)
 10161016 {
 10171017         int c;
 10181018 
 10191019         do {
 10201020                 donex();
 10211021         } while ((c = sloscan()) == EBLOCK);
 10221022         if (c == IDENT)
 10231023                 return;
 10241024         error("EBLOCK sync error");
 10251025 }
 10261026 
 10271027 /* Block next nr in lex buffer to expand */
 10281028 int
 10291029 donex(void)
 10301030 {
 10311031         int n, i;
 10321032 
 10331033         if (bidx == RECMAX)
 10341034                 error("too deep macro recursion");
 10351035         n = cinput();
 10361036         n = MKB(n, cinput());
 10371037         for (i = 0; i < bidx; i++)
 10381038                 if (bptr[i] == n)
 10391039                         return n; /* already blocked */
 10401040         bptr[bidx++] = n;
 10411041         /* XXX - check for sp buffer overflow */
 10421042 #ifdef PCC_DEBUG
 10431043         if (dflag>1) {
 10441044                 printf("donex %d (%d) blocking:\n", bidx, n);
 10451045                 printf("donex %s(%d) blocking:", norep[n]->namep, n);
 10461046                 for (i = bidx-1; i >= 0; i--)
 10471047                         printf(" '%s'", norep[bptr[i]]->namep);
 10481048                 printf("\n");
 10491049         }
 10501050 #endif
 10511051         return n;
 10521052 }
 10531053 
 10541054 /*
 10551055  * store a character into the "define" buffer.
 10561056  */
 10571057 void
 10581058 savch(int c)
 10591059 {
 10601060         if (stringbuf >= &sbf[SBSIZE]) {
 10611061                 stringbuf = sbf; /* need space to write error message */
 10621062                 error("out of macro space!");
 10631063         }
 10641064         *stringbuf++ = (usch)c;
 10651065 }
 10661066 
 10671067 /*
 10681068  * convert _Pragma() to #pragma for output.
 10691069  * Syntax is already correct.
 10701070  */
 10711071 static void
 10721072 pragoper(void)
 10731073 {
 10741074         usch *s;
 10751075         int t;
 10761076 
 10771077         while ((t = sloscan()) != '(')
 10781078                 ;
 10791079 
 10801080         while ((t = sloscan()) == WSPACE)
 10811081                 ;
 10821082         if (t != STRING)
 10831083                 error("_Pragma() must have string argument");
 10841084         savstr((const usch *)"\n#pragma ");
 10851085         s = yytext;
 10861086         if (*s == 'L')
 10871087                 s++;
 10881088         for (; *s; s++) {
 10891089                 if (*s == EBLOCK) {
 10901090                         s+=2;
 10911091                         continue;
 10921092                 }
 10931093                 if (*s == '\"')
 10941094                         continue;
 10951095                 if (*s == '\\' && (s[1] == '\"' || s[1] == '\\'))
 10961096                         s++;
 10971097                 savch(*s);
 10981098         }
 10991099         sheap("\n# %d \"%s\"\n", ifiles->lineno, ifiles->fname);
 11001100         while ((t = sloscan()) == WSPACE)
 11011101                 ;
 11021102         if (t != ')')
 11031103                 error("_Pragma() syntax error");
 11041104 }
 11051105 
 11061106 /*
 11071107  * Return true if it is OK to expand this symbol.
 11081108  */
 11091109 static int
 11101110 okexp(struct symtab *sp)
 11111111 {
 11121112         int i;
 11131113 
 11141114         if (sp == NULL)
 11151115                 return 0;
 11161116         for (i = 0; i < bidx; i++)
 11171117                 if (norep[bptr[i]] == sp)
 11181118                         return 0;
 11191119         return 1;
 11201120 }
 11211121 
 11221122 /*
 11231123  * Insert block(s) before each expanded name.
 11241124  * Input is in lex buffer, output on lex buffer.
 11251125  */
 11261126 static void
 11271127 insblock(int bnr)
 11281128 {
 11291129         usch *bp = stringbuf;
 11301130         int c, i;
 11311131  
 11321132         IMP("IB");
 11331133         readmac++;
 11341134         while ((c = sloscan()) != WARN) {
 11351135                 if (c == EBLOCK) {
 11361136                         sss();
 11371137                         continue;
 11381138                 }
 11391139                 if (c == CMNT) {
 11401140                         getcmnt();
 11411141                         continue;
 11421142                 }
 11431143                 if (c == IDENT) {
 11441144                         savch(EBLOCK), savch(bnr & 255), savch(bnr >> 8);
 11451145                         for (i = 0; i < bidx; i++)
 11461146                                 savch(EBLOCK), savch(bptr[i] & 255),
 11471147                                     savch(bptr[i] >> 8);
 11481148                 }
 11491149                 savstr(yytext);
 11501150                 if (c == '\n')
 11511151                         (void)cinput();
 11521152         }
 11531153         savch(0);
 11541154         cunput(WARN);
 11551155         unpstr(bp);
 11561156         stringbuf = bp;
 11571157         readmac--;
 11581158         IMP("IBRET");
 11591159 }
 11601160 
 11611161 /* Delete next WARN on the input stream */
 11621162 static void
 11631163 delwarn(void)
 11641164 {
 11651165         usch *bp = stringbuf;
 11661166         int c;
 11671167  
 11681168         IMP("DELWARN");
 11691169         while ((c = sloscan()) != WARN) {
 11701170                 if (c == CMNT) {
 11711171                         getcmnt();
 11721172                 } else if (c == EBLOCK) {
 11731173                         sss();
 11741174                 } else if (c == '\n') {
 11751175                         putch(cinput());
 11761176                 } else
 11771177                         savstr(yytext);
 11781178         }
 11791179         if (stringbuf[-1] == '/')
 11801180                 savch(PHOLD); /* avoid creating comments */
 11811181         savch(0);
 11821182         unpstr(bp);
 11831183         stringbuf = bp;
 11841184         IMP("DELWRET");
 11851185 }
 11861186 
 11871187 /*
 11881188  * Handle defined macro keywords found on input stream.
 11891189  * When finished print out the full expanded line.
 11901190  * Everything on lex buffer except for the symtab.
 11911191  */
 11921192 int
 11931193 kfind(struct symtab *sp)
 11941194 {
 11951195         struct symtab *nl;
 11961196         const usch *argary[MAXARGS+1], *cbp;
 11971197         usch *sbp;
 11981198         int c, o, chkf;
 11991199 
 12001200         DPRINT(("%d:enter kfind(%s)\n",0,sp->namep));
 12011201         IMP("KFIND");
 12021202         if (*sp->value == OBJCT) {
 12031203                 if (sp == filloc) {
 12041204                         unpstr(sheap("\"%s\"", ifiles->fname));
 12051205                         return 1;
 12061206                 } else if (sp == linloc) {
 12071207                         unpstr(sheap("%d", ifiles->lineno));
 12081208                         return 1;
 12091209                 }
 12101210                 IMP("END1");
 12111211                 cunput(WARN);
 12121212                 for (cbp = sp->value-1; *cbp; cbp--)
 12131213                         cunput(*cbp);
 12141214                 insblock(addmac(sp));
 12151215                 IMP("ENDX");
 12161216                 exparg(1);
 12171217 
 12181218 upp:            sbp = stringbuf;
 12191219                 chkf = 1;
 12201220                 if (obufp != 0)
 12211221                         lastoch = outbuf[obufp-1];
 12221222                 if (iswsnl(lastoch))
 12231223                         chkf = 0;
 12241224                 if (Cflag)
 12251225                         readmac++;
 12261226                 while ((c = sloscan()) != WARN) {
 12271227                         switch (c) {
 12281228                         case CMNT:
 12291229                                 getcmnt();
 12301230                                 break;
 12311231 
 12321232                         case STRING:
 12331233                                 /* Remove embedded directives */
 12341234                                 for (cbp = yytext; *cbp; cbp++) {
 12351235                                         if (*cbp == EBLOCK)
 12361236                                                 cbp+=2;
 12371237                                         else if (*cbp != CONC)
 12381238                                                 savch(*cbp);
 12391239                                 }
 12401240                                 break;
 12411241 
 12421242                         case EBLOCK:
 12431243                                 doblk();
 12441244                                 /* FALLTHROUGH */
 12451245                         case IDENT:
 12461246                                 /*
 12471247                                  * Tricky: if this is the last identifier
 12481248                                  * in the expanded list, and it is defined
 12491249                                  * as a function-like macro, then push it
 12501250                                  * back on the input stream and let fastscan
 12511251                                  * handle it as a new macro.
 12521252                                  * BUT: if this macro is blocked then this
 12531253                                  * should not be done.
 12541254                                  */
 12551255                                 nl = lookup(yytext, FIND);
 12561256                                 o = okexp(nl);
 12571257                                 bidx = 0;
 12581258                                 /* Deal with pragmas here */
 12591259                                 if (nl == pragloc) {
 12601260                                         pragoper();
 12611261                                         break;
 12621262                                 }
 12631263                                 if (nl == NULL || !o || *nl->value == OBJCT) {
 12641264                                         /* Not fun-like macro */
 12651265                                         savstr(yytext);
 12661266                                         break;
 12671267                                 }
 12681268                                 c = cinput();
 12691269                                 if (c == WARN) {
 12701270                                         /* succeeded, push back */
 12711271                                         unpstr(yytext);
 12721272                                 } else {
 12731273                                         savstr(yytext);
 12741274                                 }
 12751275                                 cunput(c);
 12761276                                 break;
 12771277 
 12781278                         default:
 12791279                                 if (chkf && c < 127)
 12801280                                         putch(' ');
 12811281                                 savstr(yytext);
 12821282                                 break;
 12831283                         }
 12841284                         chkf = 0;
 12851285                 }
 12861286                 if (Cflag)
 12871287                         readmac--;
 12881288                 IMP("END2");
 12891289                 norepptr = 1;
 12901290                 savch(0);
 12911291                 stringbuf = sbp;
 12921292                 return 1;
 12931293         }
 12941294         /* Is a function-like macro */
 12951295 
 12961296         /* Search for '(' */
 12971297         sbp = stringbuf;
 12981298         while (iswsnl(c = cinput()))
 12991299                 savch(c);
 13001300         savch(0);
 13011301         stringbuf = sbp;
 13021302         if (c != '(') {
 13031303                 cunput(c);
 13041304                 unpstr(sbp);
 13051305                 return 0; /* Failed */
 13061306         }
 13071307 
 13081308         /* Found one, output \n to be in sync */
 13091309         for (; *sbp; sbp++) {
 13101310                 if (*sbp == '\n')
 13111311                         putch('\n'), ifiles->lineno++;
 13121312         }
 13131313 
 13141314         /* fetch arguments */
 13151315         if (Cflag)
 13161316                 readmac++;
 13171317         if (readargs(sp, argary))
 13181318                 error("readargs");
 13191319 
 13201320         c = addmac(sp);
 13211321         sbp = stringbuf;
 13221322         cunput(WARN);
 13231323 
 13241324         IMP("KEXP");
 13251325         subarg(sp, argary, 1);
 13261326         IMP("KNEX");
 13271327         insblock(c);
 13281328         IMP("KBLK");
 13291329 
 13301330         stringbuf = sbp;
 13311331 
 13321332         exparg(1);
 13331333         if (Cflag)
 13341334                 readmac--;
 13351335 
 13361336         IMP("END");
 13371337 
 13381338         goto upp;
 13391339 
 13401340 }
 13411341 
 13421342 /*
 13431343  * Replace and push-back on input stream the eventual replaced macro.
 13441344  * The check for whether it can expand or not should already have been done.
 13451345  * Blocks for this identifier will be added via insblock() after expansion.
 13461346  */
 13471347 int
 13481348 submac(struct symtab *sp, int lvl)
 13491349 {
 13501350         const usch *argary[MAXARGS+1];
 13511351         const usch *cp;
 13521352         usch *bp;
 13531353         int ch;
 13541354 
 13551355         DPRINT(("%d:submac1: trying '%s'\n", lvl, sp->namep));
 13561356         if (*sp->value == OBJCT) {
 13571357                 if (sp == filloc) {
 13581358                         unpstr(sheap("\"%s\"", ifiles->fname));
 13591359                         return 1;
 13601360                 } else if (sp == linloc) {
 13611361                         unpstr(sheap("%d", ifiles->lineno));
 13621362                         return 1;
 13631363                 }
 13641364 
 13651365                 DPRINT(("submac: exp object macro '%s'\n",sp->namep));
 13661366                 /* expand object-type macros */
 13671367                 ch = addmac(sp);
 13681368                 cunput(WARN);
 13691369 
 13701370                 for (cp = sp->value-1; *cp; cp--)
 13711371                         cunput(*cp);
 13721372                 insblock(ch);
 13731373                 delwarn();
 13741374                 return 1;
 13751375         }
 13761376 
 13771377         /*
 13781378          * Function-like macro; see if it is followed by a (
 13791379          * Be careful about the expand/noexpand balance.
 13801380          * Store read data on heap meanwhile.
 13811381          * For directive        #define foo() kaka
 13821382          * If input is          <NEX><NEX>foo<EXP>()<EXP> then
 13831383          * output should be     <NEX><NEX><EXP>kaka<EXP>.
 13841384          */
 13851385         bp = stringbuf;
 13861386         while (iswsnl(ch = cinput()))
 13871387                 savch(ch);
 13881388         savch(0);
 13891389         stringbuf = bp;
 13901390         if (ch != '(') {
 13911391                 cunput(ch);
 13921392                 unpstr(bp);
 13931393                 return 0; /* Failed */
 13941394         }
 13951395 
 13961396         /* no \n should be here */
 13971397 
 13981398         /*
 13991399          * A function-like macro has been found.  Read in the arguments,
 14001400          * expand them and push-back everything for another scan.
 14011401          */
 14021402         DPRINT(("%d:submac: continue macro '%s'\n", lvl, sp->namep));
 14031403         savch(0);
 14041404         if (readargs(sp, argary)) {
 14051405                 /* Bailed out in the middle of arg list */
 14061406                 unpstr(bp);
 14071407                 DDPRINT(("%d:noreadargs\n", lvl));
 14081408                 stringbuf = bp;
 14091409                 return 0;
 14101410         }
 14111411 
 14121412         /* when all args are read from input stream */
 14131413         ch = addmac(sp);
 14141414 
 14151415         DDPRINT(("%d:submac pre\n", lvl));
 14161416         cunput(WARN);
 14171417 
 14181418         subarg(sp, argary, lvl+1);
 14191419 
 14201420         DDPRINT(("%d:submac post\n", lvl));
 14211421         insblock(ch);
 14221422         delwarn();
 14231423 
 14241424         stringbuf = bp; /* Reset heap */
 14251425         DPRINT(("%d:Return submac\n", lvl));
 14261426         IMP("SM1");
 14271427         return 1;
 14281428 }
 14291429 
 14301430 static int
 14311431 isdir(void)
 14321432 {
 14331433         usch *bp = stringbuf;
 14341434         usch ch;
 14351435 
 14361436         while ((ch = cinput()) == ' ' || ch == '\t')
 14371437                 *stringbuf++ = ch;
 14381438         *stringbuf++ = ch;
 14391439         *stringbuf++ = 0;
 14401440         stringbuf = bp;
 14411441         if (ch == '#')
 14421442                 return 1;
 14431443         unpstr(bp);
 14441444         return 0;
 14451445 }
 14461446 
 14471447 /*
 14481448  * Deal with directives inside a macro.
 14491449  * Doing so is really ugly but gcc allows it, so...
 14501450  */
 14511451 static void
 14521452 chkdir(void)
 14531453 {
 14541454         usch ch;
 14551455 
 14561456         for (;;) {
 14571457                 if (isdir())
 14581458                         ppdir();
 14591459                 if (flslvl == 0)
 14601460                         return;
 14611461                 while ((ch = cinput()) != '\n')
 14621462                         ;
 14631463                 ifiles->lineno++;
 14641464                 putch('\n');
 14651465         }
 14661466 }
 14671467 
 14681468 /*
 14691469  * Read arguments and put in argument array.
 14701470  * If WARN is encountered return 1, otherwise 0.
 14711471  */
 14721472 int
 14731473 readargs(struct symtab *sp, const usch **args)
 14741474 {
 14751475         const usch *vp = sp->value;
 14761476         int c, i, plev, narg, ellips = 0;
 14771477         int warn;
 14781478 
 14791479         DPRINT(("readargs\n"));
 14801480 
 14811481         narg = *vp--;
 14821482         if (narg == VARG) {
 14831483                 narg = *vp--;
 14841484                 ellips = 1;
 14851485         }
 14861486 
 14871487         IMP("RDA1");
 14881488         /*
 14891489          * read arguments and store them on heap.
 14901490          */
 14911491         warn = 0;
 14921492         c = '(';
 14931493         for (i = 0; i < narg && c != ')'; i++) {
 14941494                 args[i] = stringbuf;
 14951495                 plev = 0;
 14961496                 while ((c = sloscan()) == WSPACE || c == '\n')
 14971497                         if (c == '\n') {
 14981498                                 ifiles->lineno++;
 14991499                                 putch(cinput());
 15001500                                 chkdir();
 15011501                         }
 15021502                 for (;;) {
 15031503                         while (c == EBLOCK) {
 15041504                                 sss();
 15051505                                 c = sloscan();
 15061506                         }
 15071507                         if (c == WARN) {
 15081508                                 warn++;
 15091509                                 goto oho;
 15101510                         }
 15111511                         if (plev == 0 && (c == ')' || c == ','))
 15121512                                 break;
 15131513                         if (c == '(')
 15141514                                 plev++;
 15151515                         if (c == ')')
 15161516                                 plev--;
 15171517                         savstr(yytext);
 15181518 oho:                    while ((c = sloscan()) == '\n') {
 15191519                                 ifiles->lineno++;
 15201520                                 putch(cinput());
 15211521                                 chkdir();
 15221522                                 savch(' ');
 15231523                         }
 15241524                         while (c == CMNT) {
 15251525                                 getcmnt();
 15261526                                 c = sloscan();
 15271527                         }
 15281528                         if (c == 0)
 15291529                                 error("eof in macro");
 15301530                 }
 15311531                 while (args[i] < stringbuf &&
 15321532                     iswsnl(stringbuf[-1]) && stringbuf[-3] != EBLOCK)
 15331533                         stringbuf--;
 15341534                 savch('\0');
 15351535 #ifdef PCC_DEBUG
 15361536                 if (dflag) {
 15371537                         printf("readargs: save arg %d '", i);
 15381538                         prline(args[i]);
 15391539                         printf("'\n");
 15401540                 }
 15411541 #endif
 15421542         }
 15431543 
 15441544         IMP("RDA2");
 15451545         /* Handle varargs readin */
 15461546         if (ellips)
 15471547                 args[i] = (const usch *)"";
 15481548         if (ellips && c != ')') {
 15491549                 args[i] = stringbuf;
 15501550                 plev = 0;
 15511551                 while ((c = sloscan()) == WSPACE || c == '\n')
 15521552                         if (c == '\n')
 15531553                                 cinput();
 15541554                 for (;;) {
 15551555                         if (plev == 0 && c == ')')
 15561556                                 break;
 15571557                         if (c == '(')
 15581558                                 plev++;
 15591559                         if (c == ')')
 15601560                                 plev--;
 15611561                         if (c == EBLOCK) {
 15621562                                 sss();
 15631563                         } else
 15641564                                 savstr(yytext);
 15651565                         while ((c = sloscan()) == '\n') {
 15661566                                 ifiles->lineno++;
 15671567                                 cinput();
 15681568                                 chkdir();
 15691569                                 savch(' ');
 15701570                         }
 15711571                 }
 15721572                 while (args[i] < stringbuf && iswsnl(stringbuf[-1]))
 15731573                         stringbuf--;
 15741574                 savch('\0');
 15751575                 
 15761576         }
 15771577         if (narg == 0 && ellips == 0)
 15781578                 while ((c = sloscan()) == WSPACE || c == '\n')
 15791579                         if (c == '\n')
 15801580                                 cinput();
 15811581 
 15821582         if (c != ')' || (i != narg && ellips == 0) || (i < narg && ellips == 1))
 15831583                 error("wrong arg count");
 15841584         while (warn)
 15851585                 cunput(WARN), warn--;
 15861586         return 0;
 15871587 }
 15881588 
 15891589 /*
 15901590  * expand a function-like macro.
 15911591  * vp points to end of replacement-list
 15921592  * reads function arguments from sloscan()
 15931593  * result is pushed-back for more scanning.
 15941594  */
 15951595 void
 15961596 subarg(struct symtab *nl, const usch **args, int lvl)
 15971597 {
 15981598         int narg, instr, snuff;
 15991599         const usch *sp, *bp, *ap, *vp;
 16001600 
 16011601         DPRINT(("%d:subarg '%s'\n", lvl, nl->namep));
 16021602         vp = nl->value;
 16031603         narg = *vp--;
 16041604         if (narg == VARG)
 16051605                 narg = *vp--;
 16061606 
 16071607         sp = vp;
 16081608         instr = snuff = 0;
 16091609 #ifdef PCC_DEBUG
 16101610         if (dflag>1) {
 16111611                 printf("%d:subarg ARGlist for %s: '", lvl, nl->namep);
 16121612                 prrep(vp);
 16131613                 printf("'\n");
 16141614         }
 16151615 #endif
 16161616 
 16171617         /*
 16181618          * push-back replacement-list onto lex buffer while replacing
 16191619          * arguments.  Arguments are macro-expanded if required.
 16201620          */
 16211621         while (*sp != 0) {
 16221622                 if (*sp == SNUFF)
 16231623                         cunput('\"'), snuff ^= 1;
 16241624                 else if (*sp == CONC)
 16251625                         ;
 16261626                 else if (*sp == WARN) {
 16271627 
 16281628                         if (sp[-1] == VARG) {
 16291629                                 bp = ap = args[narg];
 16301630                                 sp--;
 16311631 #ifdef GCC_COMPAT
 16321632                         } else if (sp[-1] == GCCARG) {
 16331633                                 ap = args[narg];
 16341634                                 if (ap[0] == 0)
 16351635                                         ap = (const usch *)"0";
 16361636                                 bp = ap;
 16371637                                 sp--;
 16381638 #endif
 16391639                         } else
 16401640                                 bp = ap = args[(int)*--sp];
 16411641 #ifdef PCC_DEBUG
 16421642                         if (dflag>1){
 16431643                                 printf("%d:subarg GOTwarn; arglist '", lvl);
 16441644                                 prline(bp);
 16451645                                 printf("'\n");
 16461646                         }
 16471647 #endif
 16481648                         if (sp[2] != CONC && !snuff && sp[-1] != CONC) {
 16491649                                 /*
 16501650                                  * Expand an argument; 6.10.3.1:
 16511651                                  * "A parameter in the replacement list,
 16521652                                  *  is replaced by the corresponding argument
 16531653                                  *  after all macros contained therein have
 16541654                                  *  been expanded.".
 16551655                                  */
 16561656                                 cunput(WARN);
 16571657                                 unpstr(bp);
 16581658                                 exparg(lvl+1);
 16591659                                 delwarn();
 16601660                         } else {
 16611661                         while (*bp)
 16621662                                 bp++;
 16631663                         while (bp > ap) {
 16641664                                 bp--;
 16651665                                 if (snuff && !instr && iswsnl(*bp)) {
 16661666                                         while (iswsnl(*bp))
 16671667                                                 bp--;
 16681668                                         cunput(' ');
 16691669                                 }
 16701670 
 16711671                                 cunput(*bp);
 16721672                                 if ((*bp == '\'' || *bp == '"')
 16731673                                      && bp[-1] != '\\' && snuff) {
 16741674                                         instr ^= 1;
 16751675                                         if (instr == 0 && *bp == '"')
 16761676                                                 cunput('\\');
 16771677                                 }
 16781678                                 if (instr && (*bp == '\\' || *bp == '"'))
 16791679                                         cunput('\\');
 16801680                         }
 16811681                         }
 16821682                 } else
 16831683                         cunput(*sp);
 16841684                 sp--;
 16851685         }
 16861686         DPRINT(("%d:Return subarg\n", lvl));
 16871687         IMP("SUBARG");
 16881688 }
 16891689 
 16901690 /*
 16911691  * Do a (correct) expansion of a WARN-terminated buffer of tokens.
 16921692  * Data is read from the lex buffer, result on lex buffer, WARN-terminated.
 16931693  * Expansion blocking is not altered here unless when tokens are
 16941694  * concatenated, in which case they are removed.
 16951695  */
 16961696 void
 16971697 exparg(int lvl)
 16981698 {
 16991699         struct symtab *nl;
 17001700         int c, i, gmult;
 17011701         usch *och;
 17021702         usch *osb = stringbuf;
 17031703         int anychange;
 17041704 
 17051705         DPRINT(("%d:exparg\n", lvl));
 17061706         IMP("EXPARG");
 17071707 
 17081708         readmac++;
 17091709 rescan:
 17101710         anychange = 0;
 17111711         while ((c = sloscan()) != WARN) {
 17121712                 DDPRINT(("%d:exparg swdata %d\n", lvl, c));
 17131713                 IMP("EA0");
 17141714                 bidx = 0;
 17151715                 switch (c) {
 17161716 
 17171717                 case EBLOCK:
 17181718                         doblk();
 17191719                         /* FALLTHROUGH */
 17201720                 case IDENT:
 17211721                         /*
 17221722                          * Handle argument concatenation here.
 17231723                          * In case of concatenation, add all blockings.
 17241724                          */
 17251725                         DDPRINT(("%d:exparg ident %d\n", lvl, c));
 17261726                         och = stringbuf;
 17271727                         gmult = 0;
 17281728 
 17291729 sav:                    savstr(yytext);
 17301730 
 17311731                         if ((c = cinput()) == EBLOCK) {
 17321732                                 /* yep, are concatenating; add blocks */
 17331733                                 gmult = 1;
 17341734                                 do {
 17351735                                         donex();
 17361736                                 } while ((c = sloscan()) == EBLOCK);
 17371737                                 goto sav;
 17381738                         }
 17391739                         cunput(c);
 17401740 
 17411741                         DPRINT(("%d:exparg: str '%s'\n", lvl, och));
 17421742                         IMP("EA1");
 17431743                         /* see if ident is expandable */
 17441744                         if ((nl = lookup(och, FIND)) && okexp(nl)) {
 17451745                                 /* Save blocks */
 17461746                                 int donothing;
 17471747                                 unsigned short *svidx =
 17481748                                     malloc(sizeof(int)*(bidx+1));
 17491749                                 int svbidx = bidx;
 17501750 
 17511751                                 for (i = 0; i < bidx; i++)
 17521752                                         svidx[i] = bptr[i];
 17531753                                 if (submac(nl, lvl+1)) {
 17541754                                         /* Could expand, result on lexbuffer */
 17551755                                         stringbuf = och; /* clear saved name */
 17561756                                         anychange = 1;
 17571757                                 }
 17581758                                 donothing = 0;
 17591759                                 c = cinput();
 17601760                                 if (c == 'L') {
 17611761                                         int c2 = cinput();
 17621762                                         if (c2 == '\"' || c2 == '\'')
 17631763                                                 donothing = 1;
 17641764                                         cunput(c2);
 17651765                                 }
 17661766                                 cunput(c);
 17671767 
 17681768                                 if (donothing == 0)
 17691769                                     if ((spechr[c] & C_ID0) || c == EBLOCK) {
 17701770                                         for (i = 0; i < svbidx; i++) {
 17711771                                                 cunput(svidx[i] >> 8);
 17721772                                                 cunput(svidx[i] & 255);
 17731773                                                 cunput(EBLOCK);
 17741774                                         }
 17751775                                 }
 17761776                                 free(svidx);
 17771777                         } else if (bidx) {
 17781778                                 /* must restore blocks */
 17791779                                 if (gmult) {
 17801780                                         unpstr(och);
 17811781                                         if (sloscan() != IDENT)
 17821782                                                 error("exparg sync error");
 17831783                                 }
 17841784                                 stringbuf = och;
 17851785                                 for (i = 0; i < bidx; i++)
 17861786                                         savch(EBLOCK), savch(bptr[i] & 255),
 17871787                                             savch(bptr[i] >> 8);
 17881788                                 savstr(yytext);
 17891789                         }
 17901790                         bidx = 0;
 17911791                         IMP("EA2");
 17921792                         break;
 17931793 
 17941794                 case CMNT:
 17951795                         getcmnt();
 17961796                         break;
 17971797 
 17981798                 case '\n':
 17991799                         cinput();
 18001800                         savch(' ');
 18011801                         break;
 18021802 
 18031803                 default:
 18041804                         savstr(yytext);
 18051805                         break;
 18061806                 }
 18071807         }
 18081808         *stringbuf = 0;
 18091809         cunput(WARN);
 18101810         unpstr(osb);
 18111811         DPRINT(("%d:exparg return: change %d\n", lvl, anychange));
 18121812         IMP("EXPRET");
 18131813         stringbuf = osb;
 18141814         if (anychange)
 18151815                 goto rescan;
 18161816         readmac--;
 18171817 }
 18181818 
 18191819 #ifdef PCC_DEBUG
 18201820 static void
 18211821 imp(const char *str)
 18221822 {
 18231823         printf("%s (%d) '", str, bidx);
 18241824         prline(ifiles->curptr);
 18251825         printf("'\n");
 18261826 }
 18271827 
 18281828 static void
 18291829 prrep(const usch *s)
 18301830 {
 18311831         while (*s) {
 18321832                 switch (*s) {
 18331833                 case WARN: printf("<ARG(%d)>", *--s); break;
 18341834                 case CONC: printf("<CONC>"); break;
 18351835                 case SNUFF: printf("<SNUFF>"); break;
 18361836                 case EBLOCK: printf("<E(%d)>",s[-1] + s[-2] * 256); s-=2; break;
 18371837                 default: printf("%c", *s); break;
 18381838                 }
 18391839                 s--;
 18401840         }
 18411841 }
 18421842 
 18431843 static void
 18441844 prline(const usch *s)
 18451845 {
 18461846         while (*s) {
 18471847                 switch (*s) {
 18481848                 case WARN: printf("<WARN>"); break;
 18491849                 case CONC: printf("<CONC>"); break;
 18501850                 case SNUFF: printf("<SNUFF>"); break;
 18511851                 case PHOLD: printf("<PHOLD>"); break;
 18521852                 case EBLOCK: printf("<E(%d)>",s[1] + s[2] * 256); s+=2; break;
 18531853                 case '\n': printf("<NL>"); break;
 18541854                 default: printf("%c", *s); break;
 18551855                 }
 18561856                 s++;
 18571857         }
 18581858 }
 18591859 #endif
 18601860 
 18611861 usch *
 18621862 savstr(const usch *str)
 18631863 {
 18641864         usch *rv = stringbuf;
 18651865 
 18661866         do {
 18671867                 if (stringbuf >= &sbf[SBSIZE])   {
 18681868                         stringbuf = sbf; /* need space to write error message */
 18691869                         error("out of macro space!");
 18701870                 }
 18711871         } while ((*stringbuf++ = *str++));
 18721872         stringbuf--;
 18731873         return rv;
 18741874 }
 18751875 
 18761876 void
 18771877 unpstr(const usch *c)
 18781878 {
 18791879         const usch *d = c;
 18801880 
 18811881 #if 0
 18821882         if (dflag>1) {
 18831883                 printf("Xunpstr: '");
 18841884                 prline(c);
 18851885                 printf("'\n");
 18861886         }
 18871887 #endif
 18881888         while (*d) {
 18891889                 if (*d == EBLOCK)
 18901890                         d += 2;
 18911891                 d++;
 18921892         }
 18931893         while (d > c) {
 18941894                 cunput(*--d);
 18951895         }
 18961896 }
 18971897 
 18981898 static void
 18991899 flbuf(void)
 19001900 {
 19011901         if (obufp == 0)
 19021902                 return;
 19031903         if (Mflag == 0)
 19041904                 xwrite(ofd, outbuf, obufp);
 19051905         lastoch = outbuf[obufp-1];
 19061906         obufp = 0;
 19071907 }
 19081908 
 19091909 void
 19101910 putch(int ch)
 19111911 {
 19121912         outbuf[obufp++] = (usch)ch;
 19131913         if (obufp == CPPBUF || (istty && ch == '\n'))
 19141914                 flbuf();
 19151915 }
 19161916 
 19171917 void
 19181918 putstr(const usch *s)
 19191919 {
 19201920         for (; *s; s++) {
 19211921                 if (*s == PHOLD)
 19221922                         continue;
 19231923                 outbuf[obufp++] = *s;
 19241924                 if (obufp == CPPBUF || (istty && *s == '\n'))
 19251925                         flbuf();
 19261926         }
 19271927 }
 19281928 
 19291929 /*
 19301930  * convert a number to an ascii string. Store it on the heap.
 19311931  */
 19321932 static void
 19331933 num2str(int num)
 19341934 {
 19351935         static usch buf[12];
 19361936         usch *b = buf;
 19371937         int m = 0;
 19381938         
 19391939         if (num < 0)
 19401940                 num = -num, m = 1;
 19411941         do {
 19421942                 *b++ = (usch)(num % 10 + '0');
 19431943                 num /= 10;
 19441944         } while (num);
 19451945         if (m)
 19461946                 *b++ = '-';
 19471947         while (b > buf)
 19481948                 savch(*--b);
 19491949 }
 19501950 
 19511951 /*
 19521952  * similar to sprintf, but only handles %c, %s and %d.
 19531953  * saves result on heap.
 19541954  */
 19551955 static void
 19561956 vsheap(const char *fmt, va_list ap)
 19571957 {
 19581958         for (; *fmt; fmt++) {
 19591959                 if (*fmt == '%') {
 19601960                         fmt++;
 19611961                         switch (*fmt) {
 19621962                         case 's':
 19631963                                 savstr(va_arg(ap, usch *));
 19641964                                 break;
 19651965                         case 'd':
 19661966                                 num2str(va_arg(ap, int));
 19671967                                 break;
 19681968                         case 'c':
 19691969                                 savch(va_arg(ap, int));
 19701970                                 break;
 19711971                         default:
 19721972                                 error("bad sheap");
 19731973                         }
 19741974                 } else
 19751975                         savch(*fmt);
 19761976         }
 19771977         *stringbuf = 0;
 19781978 }
 19791979 
 19801980 usch *
 19811981 sheap(const char *fmt, ...)
 19821982 {
 19831983         va_list ap;
 19841984         usch *op = stringbuf;
 19851985 
 19861986         va_start(ap, fmt);
 19871987         vsheap(fmt, ap);
 19881988         va_end(ap);
 19891989 
 19901990         return op;
 19911991 }
 19921992 
 19931993 static void
 19941994 usage(void)
 19951995 {
 19961996         error("Usage: cpp [-Cdt] [-Dvar=val] [-Uvar] [-Ipath] [-Spath]");
 19971997 }
 19981998 
 19991999 #ifdef notyet
 20002000 /*
 20012001  * Symbol table stuff.
 20022002  * The data structure used is a patricia tree implementation using only
 20032003  * bytes to store offsets. 
 20042004  * The information stored is (lower address to higher):
 20052005  *
 20062006  *      unsigned char bitno[2]; bit number in the string
 20072007  *      unsigned char left[3];  offset from base to left element
 20082008  *      unsigned char right[3]; offset from base to right element
 20092009  */
 20102010 #endif
 20112011 
 20122012 /*
 20132013  * This patricia implementation is more-or-less the same as
 20142014  * used in ccom for string matching.
 20152015  */
 20162016 struct tree {
 20172017         int bitno;
 20182018         struct tree *lr[2];
 20192019 };
 20202020 
 20212021 #define BITNO(x)                ((x) & ~(LEFT_IS_LEAF|RIGHT_IS_LEAF))
 20222022 #define LEFT_IS_LEAF            0x80000000
 20232023 #define RIGHT_IS_LEAF           0x40000000
 20242024 #define IS_LEFT_LEAF(x)         (((x) & LEFT_IS_LEAF) != 0)
 20252025 #define IS_RIGHT_LEAF(x)        (((x) & RIGHT_IS_LEAF) != 0)
 20262026 #define P_BIT(key, bit)         (key[bit >> 3] >> (bit & 7)) & 1
 20272027 #define CHECKBITS               8
 20282028 
 20292029 static struct tree *sympole;
 20302030 static int numsyms;
 20312031 
 20322032 /*
 20332033  * Allocate a symtab struct and store the string.
 20342034  */
 20352035 static struct symtab *
 20362036 getsymtab(const usch *str)
 20372037 {
 20382038         struct symtab *sp = malloc(sizeof(struct symtab));
 20392039 
 20402040         if (sp == NULL)
 20412041                 error("getsymtab: couldn't allocate symtab");
 20422042         sp->namep = savstr(str);
 20432043         savch('\0');
 20442044         sp->value = NULL;
 20452045         sp->file = ifiles ? ifiles->orgfn : (const usch *)"<initial>";
 20462046         sp->line = ifiles ? ifiles->lineno : 0;
 20472047         return sp;
 20482048 }
 20492049 
 20502050 /*
 20512051  * Do symbol lookup in a patricia tree.
 20522052  * Only do full string matching, no pointer optimisations.
 20532053  */
 20542054 struct symtab *
 20552055 lookup(const usch *key, int enterf)
 20562056 {
 20572057         struct symtab *sp;
 20582058         struct tree *w, *new, *last;
 20592059         int len, cix, bit, fbit, svbit, ix, bitno;
 20602060         const usch *k, *m;
 20612061 
 20622062         /* Count full string length */
 20632063         for (k = key, len = 0; *k; k++, len++)
 20642064                 ;
 20652065 
 20662066         switch (numsyms) {
 20672067         case 0: /* no symbols yet */
 20682068                 if (enterf != ENTER)
 20692069                         return NULL;
 20702070                 sympole = (struct tree *)getsymtab(key);
 20712071                 numsyms++;
 20722072                 return (struct symtab *)sympole;
 20732073 
 20742074         case 1:
 20752075                 w = sympole;
 20762076                 svbit = 0; /* XXX gcc */
 20772077                 break;
 20782078 
 20792079         default:
 20802080                 w = sympole;
 20812081                 bitno = len * CHECKBITS;
 20822082                 for (;;) {
 20832083                         bit = BITNO(w->bitno);
 20842084                         fbit = bit > bitno ? 0 : P_BIT(key, bit);
 20852085                         svbit = fbit ? IS_RIGHT_LEAF(w->bitno) :
 20862086                             IS_LEFT_LEAF(w->bitno);
 20872087                         w = w->lr[fbit];
 20882088                         if (svbit)
 20892089                                 break;
 20902090                 }
 20912091         }
 20922092 
 20932093         sp = (struct symtab *)w;
 20942094 
 20952095         m = sp->namep;
 20962096         k = key;
 20972097 
 20982098         /* Check for correct string and return */
 20992099         for (cix = 0; *m && *k && *m == *k; m++, k++, cix += CHECKBITS)
 21002100                 ;
 21012101         if (*m == 0 && *k == 0) {
 21022102                 if (enterf != ENTER && sp->value == NULL)
 21032103                         return NULL;
 21042104                 return sp;
 21052105         }
 21062106 
 21072107         if (enterf != ENTER)
 21082108                 return NULL; /* no string found and do not enter */
 21092109 
 21102110         ix = *m ^ *k;
 21112111         while ((ix & 1) == 0)
 21122112                 ix >>= 1, cix++;
 21132113 
 21142114         /* Create new node */
 21152115         if ((new = malloc(sizeof *new)) == NULL)
 21162116                 error("getree: couldn't allocate tree");
 21172117         bit = P_BIT(key, cix);
 21182118         new->bitno = cix | (bit ? RIGHT_IS_LEAF : LEFT_IS_LEAF);
 21192119         new->lr[bit] = (struct tree *)getsymtab(key);
 21202120 
 21212121         if (numsyms++ == 1) {
 21222122                 new->lr[!bit] = sympole;
 21232123                 new->bitno |= (bit ? LEFT_IS_LEAF : RIGHT_IS_LEAF);
 21242124                 sympole = new;
 21252125                 return (struct symtab *)new->lr[bit];
 21262126         }
 21272127 
 21282128         w = sympole;
 21292129         last = NULL;
 21302130         for (;;) {
 21312131                 fbit = w->bitno;
 21322132                 bitno = BITNO(w->bitno);
 21332133                 if (bitno == cix)
 21342134                         error("bitno == cix");
 21352135                 if (bitno > cix)
 21362136                         break;
 21372137                 svbit = P_BIT(key, bitno);
 21382138                 last = w;
 21392139                 w = w->lr[svbit];
 21402140                 if (fbit & (svbit ? RIGHT_IS_LEAF : LEFT_IS_LEAF))
 21412141                         break;
 21422142         }
 21432143 
 21442144         new->lr[!bit] = w;
 21452145         if (last == NULL) {
 21462146                 sympole = new;
 21472147         } else {
 21482148                 last->lr[svbit] = new;
 21492149                 last->bitno &= ~(svbit ? RIGHT_IS_LEAF : LEFT_IS_LEAF);
 21502150         }
 21512151         if (bitno < cix)
 21522152                 new->bitno |= (bit ? LEFT_IS_LEAF : RIGHT_IS_LEAF);
 21532153         return (struct symtab *)new->lr[bit];
 21542154 }
 21552155 
 21562156 static usch *
 21572157 xstrdup(const usch *str)
 21582158 {
 21592159         usch *rv;
 21602160 
 21612161         if ((rv = (usch *)strdup((const char *)str)) == NULL)
 21622162                 error("xstrdup: out of mem");
 21632163         return rv;
 21642164 }
 21652165 
 21662166 void
 21672167 xwrite(int fd, const void *buf, unsigned int len)
 21682168 {
 21692169         if (write(fd, buf, len) != (int)len) {
 21702170                 if (fd == 2)
 21712171                         exit(2);
 21722172                 error("write error");
 21732173         }
 21742174 }
FishEye: Open Source License registered to PCC.
Atlassian FishEye, CVS analysis. (Version:1.6.3 Build:build-336 2008-11-04) - Administration - Page generated 2014-12-21 03:39 +0100