Quick Search:

Mode

Context

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

Other Diffs

Ignore

Blank Lines Whitespace: Expand:

Diff

1.181
 
1.182
 
MAIN:plunky:20121029170721
 
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;
 7979 usch outbuf[CPPBUF];
 8080 int obufp, istty;
 8181 int Cflag, Mflag, dMflag, Pflag, MPflag;
 8282 usch *Mfile, *MPfile, *Mxfile;
 8383 struct initar *initar;
 8484 int readmac, lastoch;
 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 
 395395         if ((c = yylex()) == '\n')
 396396                 goto okret;
 397397 
 398398         if (c != STRING)
 399399                 goto bad;
 400400 
 401401         p = yytext;
 402402         if (*p++ == 'L')
 403403                 p++;
 404404         c = strlen((char *)p);
 405405         p[c - 1] = '\0';
 406406         if (llen < c) {
 407407                 /* XXX may lose heap space */
 408408                 lbuf = stringbuf;
 409409                 stringbuf += c;
 410410                 llen = c;
 411411                 if (stringbuf >= &sbf[SBSIZE]) {
 412412                         stringbuf = sbf; /* need space to write error message */
<>413 -                        error("line filename exceeds buffer size");
  413+                        error("#line filename exceeds buffer size");
414414                 }
 415415         }
 416416         memcpy(lbuf, p, c);
 417417         ifiles->fname = lbuf;
 418418         if (yylex() != '\n')
 419419                 goto bad;
 420420 
 421421 okret:  prtline();
 422422         return;
 423423 
<>424 -bad:    error("bad line directive");
  424+bad:    error("bad #line");
425425 }
 426426 
 427427 /*
 428428  * Search for and include next file.
 429429  * Return 1 on success.
 430430  */
 431431 static int
 432432 fsrch(const usch *fn, int idx, struct incs *w)
 433433 {
 434434         int i;
 435435 
 436436         for (i = idx; i < 2; i++) {
 437437                 if (i > idx)
 438438                         w = incdir[i];
 439439                 for (; w; w = w->next) {
 440440                         usch *nm = stringbuf;
 441441 
 442442                         savstr(w->dir); savch('/');
 443443                         savstr(fn); savch(0);
 444444                         if (pushfile(nm, fn, i, w->next) == 0)
 445445                                 return 1;
 446446                         stringbuf = nm;
 447447                 }
 448448         }
 449449         return 0;
 450450 }
 451451 
 452452 static void
 453453 prem(void)
 454454 {
 455455         error("premature EOF");
 456456 }
 457457 
 458458 /*
 459459  * Include a file. Include order:
 460460  * - For <...> files, first search -I directories, then system directories.
 461461  * - For "..." files, first search "current" dir, then as <...> files.
 462462  */
 463463 void
 464464 include(void)
 465465 {
 466466         struct symtab *nl;
 467467         usch *osp;
 468468         usch *fn, *safefn;
 469469         int c;
 470470 
 471471         if (flslvl)
 472472                 return;
 473473         osp = stringbuf;
 474474 
 475475         while ((c = sloscan()) == WSPACE)
 476476                 ;
 477477         if (c == IDENT) {
 478478                 /* sloscan() will not expand idents */
 479479                 if ((nl = lookup(yytext, FIND)) == NULL)
 480480                         goto bad;
 481481                 if (kfind(nl))
 482482                         unpstr(stringbuf);
 483483                 else
 484484                         unpstr(nl->namep);
 485485                 stringbuf = osp;
 486486                 c = yylex();
 487487         }
 488488         if (c != STRING && c != '<')
 489489                 goto bad;
 490490 
 491491         if (c == '<') {
 492492                 fn = stringbuf;
 493493                 while ((c = sloscan()) != '>' && c != '\n') {
 494494                         if (c == '\n') /* XXX check - cannot reach */
 495495                                 goto bad;
 496496                         savstr(yytext);
 497497                 }
 498498                 savch('\0');
 499499                 while ((c = sloscan()) == WSPACE)
 500500                         ;
 501501                 if (c == 0)
 502502                         prem();
 503503                 if (c != '\n')
 504504                         goto bad;
 505505                 safefn = fn;
 506506         } else {
 507507                 usch *nm = stringbuf;
 508508 
 509509                 yytext[strlen((char *)yytext)-1] = 0;
 510510                 fn = &yytext[1];
 511511                 /* first try to open file relative to previous file */
 512512                 /* but only if it is not an absolute path */
 513513                 if (*fn != '/') {
 514514                         savstr(ifiles->orgfn);
 515515                         if ((stringbuf =
 516516                             (usch *)strrchr((char *)nm, '/')) == NULL)
 517517                                 stringbuf = nm;
 518518                         else
 519519                                 stringbuf++;
 520520                 }
 521521                 safefn = stringbuf;
 522522                 savstr(fn); savch(0);
 523523                 c = yylex();
 524524                 if (c == 0)
 525525                         prem();
 526526                 if (c != '\n')
 527527                         goto bad;
 528528                 if (pushfile(nm, safefn, 0, NULL) == 0)
 529529                         goto okret;
 530530                 /* XXX may lose stringbuf space */
 531531         }
 532532 
 533533         if (fsrch(safefn, 0, incdir[0]))
 534534                 goto okret;
 535535 
 536536         error("cannot find '%s'", safefn);
 537537         /* error() do not return */
 538538 
<>539 -bad:    error("bad include");
  539+bad:    error("bad #include");
540540         /* error() do not return */
 541541 okret:
 542542         prtline();
 543543 }
 544544 
 545545 void
 546546 include_next(void)
 547547 {
 548548         struct symtab *nl;
 549549         usch *osp;
 550550         usch *fn;
 551551         int c;
 552552 
 553553         if (flslvl)
 554554                 return;
 555555         osp = stringbuf;
 556556         while ((c = sloscan()) == WSPACE)
 557557                 ;
 558558         if (c == IDENT) {
 559559                 /* sloscan() will not expand idents */
 560560                 if ((nl = lookup(yytext, FIND)) == NULL)
 561561                         goto bad;
 562562                 if (kfind(nl))
 563563                         unpstr(stringbuf);
 564564                 else
 565565                         unpstr(nl->namep);
 566566                 stringbuf = osp;
 567567                 c = yylex();
 568568         }
 569569         if (c != STRING && c != '<')
 570570                 goto bad;
 571571 
 572572         fn = stringbuf;
 573573         if (c == STRING) {
 574574                 savstr(&yytext[1]);
 575575                 stringbuf[-1] = 0;
 576576         } else { /* < > */
 577577                 while ((c = sloscan()) != '>') {
 578578                         if (c == '\n')
 579579                                 goto bad;
 580580                         savstr(yytext);
 581581                 }
 582582                 savch('\0');
 583583         }
 584584         while ((c = sloscan()) == WSPACE)
 585585                 ;
 586586         if (c != '\n')
 587587                 goto bad;
 588588 
 589589         if (fsrch(fn, ifiles->idx, ifiles->incs) == 0)
 590590                 error("cannot find '%s'", fn);
 591591         prtline();
 592592         return;
 593593 
<>594 -bad:    error("bad include");
  594+bad:    error("bad #include_next");
595595         /* error() do not return */
 596596 }
 597597 
 598598 static int
 599599 definp(void)
 600600 {
 601601         int c;
 602602 
 603603         do
 604604                 c = sloscan();
 605605         while (c == WSPACE);
 606606         return c;
 607607 }
 608608 
 609609 void
 610610 getcmnt(void)
 611611 {
 612612         int c;
 613613 
 614614         savstr(yytext);
 615615         savch(cinput()); /* Lost * */
 616616         for (;;) {
 617617                 c = cinput();
 618618                 if (c == '*') {
 619619                         c = cinput();
 620620                         if (c == '/') {
 621621                                 savstr((const usch *)"*/");
 622622                                 return;
 623623                         }
 624624                         cunput(c);
 625625                         c = '*';
 626626                 }
 627627                 savch(c);
 628628         }
 629629 }
 630630 
 631631 /*
 632632  * Compare two replacement lists, taking in account comments etc.
 633633  */
 634634 static int
 635635 cmprepl(const usch *o, const usch *n)
 636636 {
 637637         for (; *o; o--, n--) {
 638638                 /* comment skip */
 639639                 if (*o == '/' && o[-1] == '*') {
 640640                         while (*o != '*' || o[-1] != '/')
 641641                                 o--;
 642642                         o -= 2;
 643643                 }
 644644                 if (*n == '/' && n[-1] == '*') {
 645645                         while (*n != '*' || n[-1] != '/')
 646646                                 n--;
 647647                         n -= 2;
 648648                 }
 649649                 while (*o == ' ' || *o == '\t')
 650650                         o--;
 651651                 while (*n == ' ' || *n == '\t')
 652652                         n--;
 653653                 if (*o != *n)
 654654                         return 1;
 655655         }
 656656         return 0;
 657657 }
 658658 
 659659 static int
 660660 isell(void)
 661661 {
 662662         int ch;
 663663 
 664664         if ((ch = cinput()) != '.') {
 665665                 cunput(ch);
 666666                 return 0;
 667667         }
 668668         if ((ch = cinput()) != '.') {
 669669                 cunput(ch);
 670670                 cunput('.');
 671671                 return 0;
 672672         }
 673673         return 1;
 674674 }
 675675 
 676676 void
 677677 define(void)
 678678 {
 679679         struct symtab *np;
 680680         usch *args[MAXARGS+1], *ubuf, *sbeg;
 681681         int c, i, redef;
 682682         int mkstr = 0, narg = -1;
 683683         int ellips = 0;
 684684 #ifdef GCC_COMPAT
 685685         usch *gccvari = NULL;
 686686         int wascon;
 687687 #endif
 688688 
 689689         if (flslvl)
 690690                 return;
 691691         if (sloscan() != WSPACE || sloscan() != IDENT)
 692692                 goto bad;
 693693 
 694694         np = lookup(yytext, ENTER);
 695695         redef = np->value != NULL;
 696696 
 697697         defining = readmac = 1;
 698698         sbeg = stringbuf;
 699699         if ((c = sloscan()) == '(') {
 700700                 narg = 0;
 701701                 /* function-like macros, deal with identifiers */
 702702                 c = definp();
 703703                 for (;;) {
 704704                         if (c == ')')
 705705                                 break;
 706706                         if (c == '.' && isell()) {
 707707                                 ellips = 1;
 708708                                 if (definp() != ')')
 709709                                         goto bad;
 710710                                 break;
 711711                         }
 712712                         if (c == IDENT) {
 713713                                 /* make sure there is no arg of same name */
 714714                                 for (i = 0; i < narg; i++)
 715715                                         if (!strcmp((char *) args[i], (char *)yytext))
 716716                                                 error("Duplicate macro "
 717717                                                   "parameter \"%s\"", yytext);
 718718                                 if (narg == MAXARGS)
 719719                                         error("Too many macro args");
 720720                                 args[narg++] = xstrdup(yytext);
 721721                                 if ((c = definp()) == ',') {
 722722                                         if ((c = definp()) == ')')
 723723                                                 goto bad;
 724724                                         continue;
 725725                                 }
 726726 #ifdef GCC_COMPAT
 727727                                 if (c == '.' && isell()) {
 728728                                         if (definp() != ')')
 729729                                                 goto bad;
 730730                                         gccvari = args[--narg];
 731731                                         break;
 732732                                 }
 733733 #endif
 734734                                 if (c == ')')
 735735                                         break;
 736736                         }
 737737                         goto bad;
 738738                 }
 739739                 c = sloscan();
 740740         } else if (c == '\n') {
 741741                 /* #define foo */
 742742                 ;
 743743         } else if (c == 0) {
 744744                 prem();
 745745         } else if (c != WSPACE)
 746746                 goto bad;
 747747 
 748748         while (c == WSPACE)
 749749                 c = sloscan();
 750750 
 751751         /* replacement list cannot start with ## operator */
 752752         if (c == '#') {
 753753                 if ((c = sloscan()) == '#')
 754754                         goto bad;
 755755                 savch('\0');
 756756 #ifdef GCC_COMPAT
 757757                 wascon = 0;
 758758 #endif
 759759                 goto in2;
 760760         }
 761761 
 762762         /* parse replacement-list, substituting arguments */
 763763         savch('\0');
 764764         while (c != '\n') {
 765765 #ifdef GCC_COMPAT
 766766                 wascon = 0;
 767767 loop:
 768768 #endif
 769769                 switch (c) {
 770770                 case WSPACE:
 771771                         /* remove spaces if it surrounds a ## directive */
 772772                         ubuf = stringbuf;
 773773                         savstr(yytext);
 774774                         c = sloscan();
 775775                         if (c == '#') {
 776776                                 if ((c = sloscan()) != '#')
 777777                                         goto in2;
 778778                                 stringbuf = ubuf;
 779779                                 savch(CONC);
 780780                                 if ((c = sloscan()) == WSPACE)
 781781                                         c = sloscan();
 782782 #ifdef GCC_COMPAT
 783783                                 if (c == '\n')
 784784                                         break;
 785785                                 wascon = 1;
 786786                                 goto loop;
 787787 #endif
 788788                         }
 789789                         continue;
 790790 
 791791                 case '#':
 792792                         c = sloscan();
 793793                         if (c == '#') {
 794794                                 /* concat op */
 795795                                 savch(CONC);
 796796                                 if ((c = sloscan()) == WSPACE)
 797797                                         c = sloscan();
 798798 #ifdef GCC_COMPAT
 799799                                 if (c == '\n')
 800800                                         break;
 801801                                 wascon = 1;
 802802                                 goto loop;
 803803 #else
 804804                                 continue;
 805805 #endif
 806806                         }
 807807 in2:                    if (narg < 0) {
 808808                                 /* no meaning in object-type macro */
 809809                                 savch('#');
 810810                                 continue;
 811811                         }
 812812                         /* remove spaces between # and arg */
 813813                         savch(SNUFF);
 814814                         if (c == WSPACE)
 815815                                 c = sloscan(); /* whitespace, ignore */
 816816                         mkstr = 1;
 817817                         if (c == IDENT && strcmp((char *)yytext, "__VA_ARGS__") == 0)
 818818                                 continue;
 819819 
 820820                         /* FALLTHROUGH */
 821821                 case IDENT:
 822822                         if (strcmp((char *)yytext, "__VA_ARGS__") == 0) {
 823823                                 if (ellips == 0)
 824824                                         error("unwanted %s", yytext);
 825825 #ifdef GCC_COMPAT
 826826                                 savch(wascon ? GCCARG : VARG);
 827827 #else
 828828                                 savch(VARG);
 829829 #endif
 830830 
 831831                                 savch(WARN);
 832832                                 if (mkstr)
 833833                                         savch(SNUFF), mkstr = 0;
 834834                                 break;
 835835                         }
 836836                         if (narg < 0)
 837837                                 goto id; /* just add it if object */
 838838                         /* check if its an argument */
 839839                         for (i = 0; i < narg; i++)
 840840                                 if (strcmp((char *)yytext, (char *)args[i]) == 0)
 841841                                         break;
 842842                         if (i == narg) {
 843843 #ifdef GCC_COMPAT
 844844                                 if (gccvari &&
 845845                                     strcmp((char *)yytext, (char *)gccvari) == 0) {
 846846                                         savch(wascon ? GCCARG : VARG);
 847847                                         savch(WARN);
 848848                                         if (mkstr)
 849849                                                 savch(SNUFF), mkstr = 0;
 850850                                         break;
 851851                                 }
 852852 #endif
 853853                                 if (mkstr)
 854854                                         error("not argument");
 855855                                 goto id;
 856856                         }
 857857                         savch(i);
 858858                         savch(WARN);
 859859                         if (mkstr)
 860860                                 savch(SNUFF), mkstr = 0;
 861861                         break;
 862862 
 863863                 case CMNT: /* save comments */
 864864                         getcmnt();
 865865                         break;
 866866 
 867867                 case 0:
 868868                         prem();
 869869 
 870870                 default:
 871871 id:                     savstr(yytext);
 872872                         break;
 873873                 }
 874874                 c = sloscan();
 875875         }
 876876         defining = readmac = 0;
 877877         /* remove trailing whitespace */
 878878         while (stringbuf > sbeg) {
 879879                 if (stringbuf[-1] == ' ' || stringbuf[-1] == '\t')
 880880                         stringbuf--;
 881881                 /* replacement list cannot end with ## operator */
 882882                 else if (stringbuf[-1] == CONC)
 883883                         goto bad;
 884884                 else
 885885                         break;
 886886         }
 887887 #ifdef GCC_COMPAT
 888888         if (gccvari) {
 889889                 savch(narg);
 890890                 savch(VARG);
 891891         } else
 892892 #endif
 893893         if (ellips) {
 894894                 savch(narg);
 895895                 savch(VARG);
 896896         } else
 897897                 savch(narg < 0 ? OBJCT : narg);
 898898         if (redef && ifiles->idx != SYSINC) {
 899899                 if (cmprepl(np->value, stringbuf-1)) {
 900900                         sbeg = stringbuf;
 901901                         np->value = stringbuf-1;
 902902                         warning("%s redefined (previously defined at \"%s\" line %d)",
 903903                             np->namep, np->file, np->line);
 904904                 }
 905905                 stringbuf = sbeg/* forget this space */
 906906         } else
 907907                 np->value = stringbuf-1;
 908908 
 909909 #ifdef PCC_DEBUG
 910910         if (dflag) {
 911911                 const usch *w = np->value;
 912912 
 913913                 printf("!define: ");
 914914                 if (*w == OBJCT)
 915915                         printf("[object]");
 916916                 else if (*w == VARG)
 917917                         printf("[VARG%d]", *--w);
 918918                 while (*--w) {
 919919                         switch (*w) {
 920920                         case WARN: printf("<%d>", *--w); break;
 921921                         case CONC: printf("<##>"); break;
 922922                         case SNUFF: printf("<\">"); break;
 923923                         default: putchar(*w); break;
 924924                         }
 925925                 }
 926926                 putchar('\n');
 927927         }
 928928 #endif
 929929         for (i = 0; i < narg; i++)
 930930                 free(args[i]);
 931931         return;
 932932 
<>933 -bad:    error("bad define");
  933+bad:    error("bad #define");
<_934934 }
 935935 
 936936 void
 937937 warning(const char *fmt, ...)
 938938 {
 939939         va_list ap;
 940940         usch *sb;
 941941 
 942942         flbuf();
 943943         savch(0);
 944944 
 945945         sb = stringbuf;
 946946         if (ifiles != NULL)
 947947                 sheap("%s:%d: warning: ", ifiles->fname, ifiles->lineno);
 948948 
 949949         va_start(ap, fmt);
 950950         vsheap(fmt, ap);
 951951         va_end(ap);
 952952         savch('\n');
 953953         xwrite(2, sb, stringbuf - sb);
 954954         stringbuf = sb;
 955955 }
 956956 
 957957 void
 958958 error(const char *fmt, ...)
 959959 {
 960960         va_list ap;
 961961         usch *sb;
 962962 
 963963         flbuf();
 964964         savch(0);
 965965 
 966966         sb = stringbuf;
 967967         if (ifiles != NULL)
 968968                 sheap("%s:%d: error: ", ifiles->fname, ifiles->lineno);
 969969 
 970970         va_start(ap, fmt);
 971971         vsheap(fmt, ap);
 972972         va_end(ap);
 973973         savch('\n');
 974974         xwrite(2, sb, stringbuf - sb);
 975975         stringbuf = sb;
 976976 
 977977         exit(1);
 978978 }
 979979 
 980980 static void
 981981 sss(void)
 982982 {
 983983         savch(EBLOCK);
 984984         savch(cinput());
 985985         savch(cinput());
 986986 }
 987987 
 988988 static int
 989989 addmac(struct symtab *sp)
 990990 {
 991991         int c, i;
 992992 
 993993         /* Check if it exists; then save some space */
 994994         /* May be more difficult to debug cpp */
 995995         for (i = 1; i < norepptr; i++)
 996996                 if (norep[i] == sp)
 997997                         return i;
 998998         if (norepptr >= RECMAX)
 999999                 error("too many macros");
 10001000         /* check norepptr */
 10011001         if ((norepptr & 255) == 0)
 10021002                 norepptr++;
 10031003         if (((norepptr >> 8) & 255) == 0)
 10041004                 norepptr += 256;
 10051005         c = norepptr;
 10061006         norep[norepptr++] = sp;
 10071007         return c;
 10081008 }
 10091009 
 10101010 static void
 10111011 doblk(void)
 10121012 {
 10131013         int c;
 10141014 
 10151015         do {
 10161016                 donex();
 10171017         } while ((c = sloscan()) == EBLOCK);
 10181018         if (c == IDENT)
 10191019                 return;
 10201020         error("EBLOCK sync error");
 10211021 }
 10221022 
 10231023 /* Block next nr in lex buffer to expand */
 10241024 int
 10251025 donex(void)
 10261026 {
 10271027         int n, i;
 10281028 
 10291029         if (bidx == RECMAX)
 10301030                 error("too deep macro recursion");
 10311031         n = cinput();
 10321032         n = MKB(n, cinput());
 10331033         for (i = 0; i < bidx; i++)
 10341034                 if (bptr[i] == n)
 10351035                         return n; /* already blocked */
 10361036         bptr[bidx++] = n;
 10371037         /* XXX - check for sp buffer overflow */
 10381038 #ifdef PCC_DEBUG
 10391039         if (dflag>1) {
 10401040                 printf("donex %d (%d) blocking:\n", bidx, n);
 10411041                 printf("donex %s(%d) blocking:", norep[n]->namep, n);
 10421042                 for (i = bidx-1; i >= 0; i--)
 10431043                         printf(" '%s'", norep[bptr[i]]->namep);
 10441044                 printf("\n");
 10451045         }
 10461046 #endif
 10471047         return n;
 10481048 }
 10491049 
 10501050 /*
 10511051  * store a character into the "define" buffer.
 10521052  */
 10531053 void
 10541054 savch(int c)
 10551055 {
 10561056         if (stringbuf >= &sbf[SBSIZE]) {
 10571057                 stringbuf = sbf; /* need space to write error message */
 10581058                 error("out of macro space!");
 10591059         }
 10601060         *stringbuf++ = (usch)c;
 10611061 }
 10621062 
 10631063 /*
 10641064  * convert _Pragma() to #pragma for output.
 10651065  * Syntax is already correct.
 10661066  */
 10671067 static void
 10681068 pragoper(void)
 10691069 {
 10701070         usch *s;
 10711071         int t;
 10721072 
 10731073         while ((t = sloscan()) != '(')
 10741074                 ;
 10751075 
 10761076         while ((t = sloscan()) == WSPACE)
 10771077                 ;
 10781078         if (t != STRING)
 10791079                 error("_Pragma() must have string argument");
 10801080         savstr((const usch *)"\n#pragma ");
 10811081         s = yytext;
 10821082         if (*s == 'L')
 10831083                 s++;
 10841084         for (; *s; s++) {
 10851085                 if (*s == EBLOCK) {
 10861086                         s+=2;
 10871087                         continue;
 10881088                 }
 10891089                 if (*s == '\"')
 10901090                         continue;
 10911091                 if (*s == '\\' && (s[1] == '\"' || s[1] == '\\'))
 10921092                         s++;
 10931093                 savch(*s);
 10941094         }
 10951095         sheap("\n# %d \"%s\"\n", ifiles->lineno, ifiles->fname);
 10961096         while ((t = sloscan()) == WSPACE)
 10971097                 ;
 10981098         if (t != ')')
 10991099                 error("_Pragma() syntax error");
 11001100 }
 11011101 
 11021102 /*
 11031103  * Return true if it is OK to expand this symbol.
 11041104  */
 11051105 static int
 11061106 okexp(struct symtab *sp)
 11071107 {
 11081108         int i;
 11091109 
 11101110         if (sp == NULL)
 11111111                 return 0;
 11121112         for (i = 0; i < bidx; i++)
 11131113                 if (norep[bptr[i]] == sp)
 11141114                         return 0;
 11151115         return 1;
 11161116 }
 11171117 
 11181118 /*
 11191119  * Insert block(s) before each expanded name.
 11201120  * Input is in lex buffer, output on lex buffer.
 11211121  */
 11221122 static void
 11231123 insblock(int bnr)
 11241124 {
 11251125         usch *bp = stringbuf;
 11261126         int c, i;
 11271127  
 11281128         IMP("IB");
 11291129         readmac++;
 11301130         while ((c = sloscan()) != WARN) {
 11311131                 if (c == EBLOCK) {
 11321132                         sss();
 11331133                         continue;
 11341134                 }
 11351135                 if (c == CMNT) {
 11361136                         getcmnt();
 11371137                         continue;
 11381138                 }
 11391139                 if (c == IDENT) {
 11401140                         savch(EBLOCK), savch(bnr & 255), savch(bnr >> 8);
 11411141                         for (i = 0; i < bidx; i++)
 11421142                                 savch(EBLOCK), savch(bptr[i] & 255),
 11431143                                     savch(bptr[i] >> 8);
 11441144                 }
 11451145                 savstr(yytext);
 11461146                 if (c == '\n')
 11471147                         (void)cinput();
 11481148         }
 11491149         savch(0);
 11501150         cunput(WARN);
 11511151         unpstr(bp);
 11521152         stringbuf = bp;
 11531153         readmac--;
 11541154         IMP("IBRET");
 11551155 }
 11561156 
 11571157 /* Delete next WARN on the input stream */
 11581158 static void
 11591159 delwarn(void)
 11601160 {
 11611161         usch *bp = stringbuf;
 11621162         int c;
 11631163  
 11641164         IMP("DELWARN");
 11651165         while ((c = sloscan()) != WARN) {
 11661166                 if (c == CMNT) {
 11671167                         getcmnt();
 11681168                 } else if (c == EBLOCK) {
 11691169                         sss();
 11701170                 } else if (c == '\n') {
 11711171                         putch(cinput());
 11721172                 } else
 11731173                         savstr(yytext);
 11741174         }
 11751175         if (stringbuf[-1] == '/')
 11761176                 savch(PHOLD); /* avoid creating comments */
 11771177         savch(0);
 11781178         unpstr(bp);
 11791179         stringbuf = bp;
 11801180         IMP("DELWRET");
 11811181 }
 11821182 
 11831183 /*
 11841184  * Handle defined macro keywords found on input stream.
 11851185  * When finished print out the full expanded line.
 11861186  * Everything on lex buffer except for the symtab.
 11871187  */
 11881188 int
 11891189 kfind(struct symtab *sp)
 11901190 {
 11911191         struct symtab *nl;
 11921192         const usch *argary[MAXARGS+1], *cbp;
 11931193         usch *sbp;
 11941194         int c, o, chkf;
 11951195 
 11961196         DPRINT(("%d:enter kfind(%s)\n",0,sp->namep));
 11971197         IMP("KFIND");
 11981198         if (*sp->value == OBJCT) {
 11991199                 if (sp == filloc) {
 12001200                         unpstr(sheap("\"%s\"", ifiles->fname));
 12011201                         return 1;
 12021202                 } else if (sp == linloc) {
 12031203                         unpstr(sheap("%d", ifiles->lineno));
 12041204                         return 1;
 12051205                 }
 12061206                 IMP("END1");
 12071207                 cunput(WARN);
 12081208                 for (cbp = sp->value-1; *cbp; cbp--)
 12091209                         cunput(*cbp);
 12101210                 insblock(addmac(sp));
 12111211                 IMP("ENDX");
 12121212                 exparg(1);
 12131213 
 12141214 upp:            sbp = stringbuf;
 12151215                 chkf = 1;
 12161216                 if (obufp != 0)
 12171217                         lastoch = outbuf[obufp-1];
 12181218                 if (iswsnl(lastoch))
 12191219                         chkf = 0;
 12201220                 if (Cflag)
 12211221                         readmac++;
 12221222                 while ((c = sloscan()) != WARN) {
 12231223                         switch (c) {
 12241224                         case CMNT:
 12251225                                 getcmnt();
 12261226                                 break;
 12271227 
 12281228                         case STRING:
 12291229                                 /* Remove embedded directives */
 12301230                                 for (cbp = yytext; *cbp; cbp++) {
 12311231                                         if (*cbp == EBLOCK)
 12321232                                                 cbp+=2;
 12331233                                         else if (*cbp != CONC)
 12341234                                                 savch(*cbp);
 12351235                                 }
 12361236                                 break;
 12371237 
 12381238                         case EBLOCK:
 12391239                                 doblk();
 12401240                                 /* FALLTHROUGH */
 12411241                         case IDENT:
 12421242                                 /*
 12431243                                  * Tricky: if this is the last identifier
 12441244                                  * in the expanded list, and it is defined
 12451245                                  * as a function-like macro, then push it
 12461246                                  * back on the input stream and let fastscan
 12471247                                  * handle it as a new macro.
 12481248                                  * BUT: if this macro is blocked then this
 12491249                                  * should not be done.
 12501250                                  */
 12511251                                 nl = lookup(yytext, FIND);
 12521252                                 o = okexp(nl);
 12531253                                 bidx = 0;
 12541254                                 /* Deal with pragmas here */
 12551255                                 if (nl == pragloc) {
 12561256                                         pragoper();
 12571257                                         break;
 12581258                                 }
 12591259                                 if (nl == NULL || !o || *nl->value == OBJCT) {
 12601260                                         /* Not fun-like macro */
 12611261                                         savstr(yytext);
 12621262                                         break;
 12631263                                 }
 12641264                                 c = cinput();
 12651265                                 if (c == WARN) {
 12661266                                         /* succeeded, push back */
 12671267                                         unpstr(yytext);
 12681268                                 } else {
 12691269                                         savstr(yytext);
 12701270                                 }
 12711271                                 cunput(c);
 12721272                                 break;
 12731273 
 12741274                         default:
 12751275                                 if (chkf && c < 127)
 12761276                                         putch(' ');
 12771277                                 savstr(yytext);
 12781278                                 break;
 12791279                         }
 12801280                         chkf = 0;
 12811281                 }
 12821282                 if (Cflag)
 12831283                         readmac--;
 12841284                 IMP("END2");
 12851285                 norepptr = 1;
 12861286                 savch(0);
 12871287                 stringbuf = sbp;
 12881288                 return 1;
 12891289         }
 12901290         /* Is a function-like macro */
 12911291 
 12921292         /* Search for '(' */
 12931293         sbp = stringbuf;
 12941294         while (iswsnl(c = cinput()))
 12951295                 savch(c);
 12961296         savch(0);
 12971297         stringbuf = sbp;
 12981298         if (c != '(') {
 12991299                 cunput(c);
 13001300                 unpstr(sbp);
 13011301                 return 0; /* Failed */
 13021302         }
 13031303 
 13041304         /* Found one, output \n to be in sync */
 13051305         for (; *sbp; sbp++) {
 13061306                 if (*sbp == '\n')
 13071307                         putch('\n'), ifiles->lineno++;
 13081308         }
 13091309 
 13101310         /* fetch arguments */
 13111311         if (Cflag)
 13121312                 readmac++;
 13131313         if (readargs(sp, argary))
 13141314                 error("readargs");
 13151315 
 13161316         c = addmac(sp);
 13171317         sbp = stringbuf;
 13181318         cunput(WARN);
 13191319 
 13201320         IMP("KEXP");
 13211321         subarg(sp, argary, 1);
 13221322         IMP("KNEX");
 13231323         insblock(c);
 13241324         IMP("KBLK");
 13251325 
 13261326         stringbuf = sbp;
 13271327 
 13281328         exparg(1);
 13291329         if (Cflag)
 13301330                 readmac--;
 13311331 
 13321332         IMP("END");
 13331333 
 13341334         goto upp;
 13351335 
 13361336 }
 13371337 
 13381338 /*
 13391339  * Replace and push-back on input stream the eventual replaced macro.
 13401340  * The check for whether it can expand or not should already have been done.
 13411341  * Blocks for this identifier will be added via insblock() after expansion.
 13421342  */
 13431343 int
 13441344 submac(struct symtab *sp, int lvl)
 13451345 {
 13461346         const usch *argary[MAXARGS+1];
 13471347         const usch *cp;
 13481348         usch *bp;
 13491349         int ch;
 13501350 
 13511351         DPRINT(("%d:submac1: trying '%s'\n", lvl, sp->namep));
 13521352         if (*sp->value == OBJCT) {
 13531353                 if (sp == filloc) {
 13541354                         unpstr(sheap("\"%s\"", ifiles->fname));
 13551355                         return 1;
 13561356                 } else if (sp == linloc) {
 13571357                         unpstr(sheap("%d", ifiles->lineno));
 13581358                         return 1;
 13591359                 }
 13601360 
 13611361                 DPRINT(("submac: exp object macro '%s'\n",sp->namep));
 13621362                 /* expand object-type macros */
 13631363                 ch = addmac(sp);
 13641364                 cunput(WARN);
 13651365 
 13661366                 for (cp = sp->value-1; *cp; cp--)
 13671367                         cunput(*cp);
 13681368                 insblock(ch);
 13691369                 delwarn();
 13701370                 return 1;
 13711371         }
 13721372 
 13731373         /*
 13741374          * Function-like macro; see if it is followed by a (
 13751375          * Be careful about the expand/noexpand balance.
 13761376          * Store read data on heap meanwhile.
 13771377          * For directive        #define foo() kaka
 13781378          * If input is          <NEX><NEX>foo<EXP>()<EXP> then
 13791379          * output should be     <NEX><NEX><EXP>kaka<EXP>.
 13801380          */
 13811381         bp = stringbuf;
 13821382         while (iswsnl(ch = cinput()))
 13831383                 savch(ch);
 13841384         savch(0);
 13851385         stringbuf = bp;
 13861386         if (ch != '(') {
 13871387                 cunput(ch);
 13881388                 unpstr(bp);
 13891389                 return 0; /* Failed */
 13901390         }
 13911391 
 13921392         /* no \n should be here */
 13931393 
 13941394         /*
 13951395          * A function-like macro has been found.  Read in the arguments,
 13961396          * expand them and push-back everything for another scan.
 13971397          */
 13981398         DPRINT(("%d:submac: continue macro '%s'\n", lvl, sp->namep));
 13991399         savch(0);
 14001400         if (readargs(sp, argary)) {
 14011401                 /* Bailed out in the middle of arg list */
 14021402                 unpstr(bp);
 14031403                 DDPRINT(("%d:noreadargs\n", lvl));
 14041404                 stringbuf = bp;
 14051405                 return 0;
 14061406         }
 14071407 
 14081408         /* when all args are read from input stream */
 14091409         ch = addmac(sp);
 14101410 
 14111411         DDPRINT(("%d:submac pre\n", lvl));
 14121412         cunput(WARN);
 14131413 
 14141414         subarg(sp, argary, lvl+1);
 14151415 
 14161416         DDPRINT(("%d:submac post\n", lvl));
 14171417         insblock(ch);
 14181418         delwarn();
 14191419 
 14201420         stringbuf = bp; /* Reset heap */
 14211421         DPRINT(("%d:Return submac\n", lvl));
 14221422         IMP("SM1");
 14231423         return 1;
 14241424 }
 14251425 
 14261426 static int
 14271427 isdir(void)
 14281428 {
 14291429         usch *bp = stringbuf;
 14301430         usch ch;
 14311431 
 14321432         while ((ch = cinput()) == ' ' || ch == '\t')
 14331433                 *stringbuf++ = ch;
 14341434         *stringbuf++ = ch;
 14351435         *stringbuf++ = 0;
 14361436         stringbuf = bp;
 14371437         if (ch == '#')
 14381438                 return 1;
 14391439         unpstr(bp);
 14401440         return 0;
 14411441 }
 14421442 
 14431443 /*
 14441444  * Deal with directives inside a macro.
 14451445  * Doing so is really ugly but gcc allows it, so...
 14461446  */
 14471447 static void
 14481448 chkdir(void)
 14491449 {
 14501450         usch ch;
 14511451 
 14521452         for (;;) {
 14531453                 if (isdir())
 14541454                         ppdir();
 14551455                 if (flslvl == 0)
 14561456                         return;
 14571457                 while ((ch = cinput()) != '\n')
 14581458                         ;
 14591459                 ifiles->lineno++;
 14601460                 putch('\n');
 14611461         }
 14621462 }
 14631463 
 14641464 /*
 14651465  * Read arguments and put in argument array.
 14661466  * If WARN is encountered return 1, otherwise 0.
 14671467  */
 14681468 int
 14691469 readargs(struct symtab *sp, const usch **args)
 14701470 {
 14711471         const usch *vp = sp->value;
 14721472         int c, i, plev, narg, ellips = 0;
 14731473         int warn;
 14741474 
 14751475         DPRINT(("readargs\n"));
 14761476 
 14771477         narg = *vp--;
 14781478         if (narg == VARG) {
 14791479                 narg = *vp--;
 14801480                 ellips = 1;
 14811481         }
 14821482 
 14831483         IMP("RDA1");
 14841484         /*
 14851485          * read arguments and store them on heap.
 14861486          */
 14871487         warn = 0;
 14881488         c = '(';
 14891489         for (i = 0; i < narg && c != ')'; i++) {
 14901490                 args[i] = stringbuf;
 14911491                 plev = 0;
 14921492                 while ((c = sloscan()) == WSPACE || c == '\n')
 14931493                         if (c == '\n') {
 14941494                                 ifiles->lineno++;
 14951495                                 putch(cinput());
 14961496                                 chkdir();
 14971497                         }
 14981498                 for (;;) {
 14991499                         while (c == EBLOCK) {
 15001500                                 sss();
 15011501                                 c = sloscan();
 15021502                         }
 15031503                         if (c == WARN) {
 15041504                                 warn++;
 15051505                                 goto oho;
 15061506                         }
 15071507                         if (plev == 0 && (c == ')' || c == ','))
 15081508                                 break;
 15091509                         if (c == '(')
 15101510                                 plev++;
 15111511                         if (c == ')')
 15121512                                 plev--;
 15131513                         savstr(yytext);
 15141514 oho:                    while ((c = sloscan()) == '\n') {
 15151515                                 ifiles->lineno++;
 15161516                                 putch(cinput());
 15171517                                 chkdir();
 15181518                                 savch(' ');
 15191519                         }
 15201520                         while (c == CMNT) {
 15211521                                 getcmnt();
 15221522                                 c = sloscan();
 15231523                         }
 15241524                         if (c == 0)
 15251525                                 error("eof in macro");
 15261526                 }
 15271527                 while (args[i] < stringbuf &&
 15281528                     iswsnl(stringbuf[-1]) && stringbuf[-3] != EBLOCK)
 15291529                         stringbuf--;
 15301530                 savch('\0');
 15311531 #ifdef PCC_DEBUG
 15321532                 if (dflag) {
 15331533                         printf("readargs: save arg %d '", i);
 15341534                         prline(args[i]);
 15351535                         printf("'\n");
 15361536                 }
 15371537 #endif
 15381538         }
 15391539 
 15401540         IMP("RDA2");
 15411541         /* Handle varargs readin */
 15421542         if (ellips)
 15431543                 args[i] = (const usch *)"";
 15441544         if (ellips && c != ')') {
 15451545                 args[i] = stringbuf;
 15461546                 plev = 0;
 15471547                 while ((c = sloscan()) == WSPACE || c == '\n')
 15481548                         if (c == '\n')
 15491549                                 cinput();
 15501550                 for (;;) {
 15511551                         if (plev == 0 && c == ')')
 15521552                                 break;
 15531553                         if (c == '(')
 15541554                                 plev++;
 15551555                         if (c == ')')
 15561556                                 plev--;
 15571557                         if (c == EBLOCK) {
 15581558                                 sss();
 15591559                         } else
 15601560                                 savstr(yytext);
 15611561                         while ((c = sloscan()) == '\n') {
 15621562                                 ifiles->lineno++;
 15631563                                 cinput();
 15641564                                 chkdir();
 15651565                                 savch(' ');
 15661566                         }
 15671567                 }
 15681568                 while (args[i] < stringbuf && iswsnl(stringbuf[-1]))
 15691569                         stringbuf--;
 15701570                 savch('\0');
 15711571                 
 15721572         }
 15731573         if (narg == 0 && ellips == 0)
 15741574                 while ((c = sloscan()) == WSPACE || c == '\n')
 15751575                         if (c == '\n')
 15761576                                 cinput();
 15771577 
 15781578         if (c != ')' || (i != narg && ellips == 0) || (i < narg && ellips == 1))
 15791579                 error("wrong arg count");
 15801580         while (warn)
 15811581                 cunput(WARN), warn--;
 15821582         return 0;
 15831583 }
 15841584 
 15851585 /*
 15861586  * expand a function-like macro.
 15871587  * vp points to end of replacement-list
 15881588  * reads function arguments from sloscan()
 15891589  * result is pushed-back for more scanning.
 15901590  */
 15911591 void
 15921592 subarg(struct symtab *nl, const usch **args, int lvl)
 15931593 {
 15941594         int narg, instr, snuff;
 15951595         const usch *sp, *bp, *ap, *vp;
 15961596 
 15971597         DPRINT(("%d:subarg '%s'\n", lvl, nl->namep));
 15981598         vp = nl->value;
 15991599         narg = *vp--;
 16001600         if (narg == VARG)
 16011601                 narg = *vp--;
 16021602 
 16031603         sp = vp;
 16041604         instr = snuff = 0;
 16051605 #ifdef PCC_DEBUG
 16061606         if (dflag>1) {
 16071607                 printf("%d:subarg ARGlist for %s: '", lvl, nl->namep);
 16081608                 prrep(vp);
 16091609                 printf("'\n");
 16101610         }
 16111611 #endif
 16121612 
 16131613         /*
 16141614          * push-back replacement-list onto lex buffer while replacing
 16151615          * arguments.  Arguments are macro-expanded if required.
 16161616          */
 16171617         while (*sp != 0) {
 16181618                 if (*sp == SNUFF)
 16191619                         cunput('\"'), snuff ^= 1;
 16201620                 else if (*sp == CONC)
 16211621                         ;
 16221622                 else if (*sp == WARN) {
 16231623 
 16241624                         if (sp[-1] == VARG) {
 16251625                                 bp = ap = args[narg];
 16261626                                 sp--;
 16271627 #ifdef GCC_COMPAT
 16281628                         } else if (sp[-1] == GCCARG) {
 16291629                                 ap = args[narg];
 16301630                                 if (ap[0] == 0)
 16311631                                         ap = (const usch *)"0";
 16321632                                 bp = ap;
 16331633                                 sp--;
 16341634 #endif
 16351635                         } else
 16361636                                 bp = ap = args[(int)*--sp];
 16371637 #ifdef PCC_DEBUG
 16381638                         if (dflag>1){
 16391639                                 printf("%d:subarg GOTwarn; arglist '", lvl);
 16401640                                 prline(bp);
 16411641                                 printf("'\n");
 16421642                         }
 16431643 #endif
 16441644                         if (sp[2] != CONC && !snuff && sp[-1] != CONC) {
 16451645                                 /*
 16461646                                  * Expand an argument; 6.10.3.1:
 16471647                                  * "A parameter in the replacement list,
 16481648                                  *  is replaced by the corresponding argument
 16491649                                  *  after all macros contained therein have
 16501650                                  *  been expanded.".
 16511651                                  */
 16521652                                 cunput(WARN);
 16531653                                 unpstr(bp);
 16541654                                 exparg(lvl+1);
 16551655                                 delwarn();
 16561656                         } else {
 16571657                         while (*bp)
 16581658                                 bp++;
 16591659                         while (bp > ap) {
 16601660                                 bp--;
 16611661                                 if (snuff && !instr && iswsnl(*bp)) {
 16621662                                         while (iswsnl(*bp))
 16631663                                                 bp--;
 16641664                                         cunput(' ');
 16651665                                 }
 16661666 
 16671667                                 cunput(*bp);
 16681668                                 if ((*bp == '\'' || *bp == '"')
 16691669                                      && bp[-1] != '\\' && snuff) {
 16701670                                         instr ^= 1;
 16711671                                         if (instr == 0 && *bp == '"')
 16721672                                                 cunput('\\');
 16731673                                 }
 16741674                                 if (instr && (*bp == '\\' || *bp == '"'))
 16751675                                         cunput('\\');
 16761676                         }
 16771677                         }
 16781678                 } else
 16791679                         cunput(*sp);
 16801680                 sp--;
 16811681         }
 16821682         DPRINT(("%d:Return subarg\n", lvl));
 16831683         IMP("SUBARG");
 16841684 }
 16851685 
 16861686 /*
 16871687  * Do a (correct) expansion of a WARN-terminated buffer of tokens.
 16881688  * Data is read from the lex buffer, result on lex buffer, WARN-terminated.
 16891689  * Expansion blocking is not altered here unless when tokens are
 16901690  * concatenated, in which case they are removed.
 16911691  */
 16921692 void
 16931693 exparg(int lvl)
 16941694 {
 16951695         struct symtab *nl;
 16961696         int c, i, gmult;
 16971697         usch *och;
 16981698         usch *osb = stringbuf;
 16991699         int anychange;
 17001700 
 17011701         DPRINT(("%d:exparg\n", lvl));
 17021702         IMP("EXPARG");
 17031703 
 17041704         readmac++;
 17051705 rescan:
 17061706         anychange = 0;
 17071707         while ((c = sloscan()) != WARN) {
 17081708                 DDPRINT(("%d:exparg swdata %d\n", lvl, c));
 17091709                 IMP("EA0");
 17101710                 bidx = 0;
 17111711                 switch (c) {
 17121712 
 17131713                 case EBLOCK:
 17141714                         doblk();
 17151715                         /* FALLTHROUGH */
 17161716                 case IDENT:
 17171717                         /*
 17181718                          * Handle argument concatenation here.
 17191719                          * In case of concatenation, add all blockings.
 17201720                          */
 17211721                         DDPRINT(("%d:exparg ident %d\n", lvl, c));
 17221722                         och = stringbuf;
 17231723                         gmult = 0;
 17241724 
 17251725 sav:                    savstr(yytext);
 17261726 
 17271727                         if ((c = cinput()) == EBLOCK) {
 17281728                                 /* yep, are concatenating; add blocks */
 17291729                                 gmult = 1;
 17301730                                 do {
 17311731                                         donex();
 17321732                                 } while ((c = sloscan()) == EBLOCK);
 17331733                                 goto sav;
 17341734                         }
 17351735                         cunput(c);
 17361736 
 17371737                         DPRINT(("%d:exparg: str '%s'\n", lvl, och));
 17381738                         IMP("EA1");
 17391739                         /* see if ident is expandable */
 17401740                         if ((nl = lookup(och, FIND)) && okexp(nl)) {
 17411741                                 /* Save blocks */
 17421742                                 int donothing;
 17431743                                 unsigned short *svidx =
 17441744                                     malloc(sizeof(int)*(bidx+1));
 17451745                                 int svbidx = bidx;
 17461746 
 17471747                                 for (i = 0; i < bidx; i++)
 17481748                                         svidx[i] = bptr[i];
 17491749                                 if (submac(nl, lvl+1)) {
 17501750                                         /* Could expand, result on lexbuffer */
 17511751                                         stringbuf = och; /* clear saved name */
 17521752                                         anychange = 1;
 17531753                                 }
 17541754                                 donothing = 0;
 17551755                                 c = cinput();
 17561756                                 if (c == 'L') {
 17571757                                         int c2 = cinput();
 17581758                                         if (c2 == '\"' || c2 == '\'')
 17591759                                                 donothing = 1;
 17601760                                         cunput(c2);
 17611761                                 }
 17621762                                 cunput(c);
 17631763 
 17641764                                 if (donothing == 0)
 17651765                                     if (((spechr[c] & C_ID) && c > 63) ||
 17661766                                     c == EBLOCK) {
 17671767                                         for (i = 0; i < svbidx; i++) {
 17681768                                                 cunput(svidx[i] >> 8);
 17691769                                                 cunput(svidx[i] & 255);
 17701770                                                 cunput(EBLOCK);
 17711771                                         }
 17721772                                 }
 17731773                                 free(svidx);
 17741774                         } else if (bidx) {
 17751775                                 /* must restore blocks */
 17761776                                 if (gmult) {
 17771777                                         unpstr(och);
 17781778                                         if (sloscan() != IDENT)
 17791779                                                 error("exparg sync error");
 17801780                                 }
 17811781                                 stringbuf = och;
 17821782                                 for (i = 0; i < bidx; i++)
 17831783                                         savch(EBLOCK), savch(bptr[i] & 255),
 17841784                                             savch(bptr[i] >> 8);
 17851785                                 savstr(yytext);
 17861786                         }
 17871787                         bidx = 0;
 17881788                         IMP("EA2");
 17891789                         break;
 17901790 
 17911791                 case CMNT:
 17921792                         getcmnt();
 17931793                         break;
 17941794 
 17951795                 case '\n':
 17961796                         cinput();
 17971797                         savch(' ');
 17981798                         break;
 17991799 
 18001800                 default:
 18011801                         savstr(yytext);
 18021802                         break;
 18031803                 }
 18041804         }
 18051805         *stringbuf = 0;
 18061806         cunput(WARN);
 18071807         unpstr(osb);
 18081808         DPRINT(("%d:exparg return: change %d\n", lvl, anychange));
 18091809         IMP("EXPRET");
 18101810         stringbuf = osb;
 18111811         if (anychange)
 18121812                 goto rescan;
 18131813         readmac--;
 18141814 }
 18151815 
 18161816 #ifdef PCC_DEBUG
 18171817 static void
 18181818 imp(const char *str)
 18191819 {
 18201820         printf("%s (%d) '", str, bidx);
 18211821         prline(ifiles->curptr);
 18221822         printf("'\n");
 18231823 }
 18241824 
 18251825 static void
 18261826 prrep(const usch *s)
 18271827 {
 18281828         while (*s) {
 18291829                 switch (*s) {
 18301830                 case WARN: printf("<ARG(%d)>", *--s); break;
 18311831                 case CONC: printf("<CONC>"); break;
 18321832                 case SNUFF: printf("<SNUFF>"); break;
 18331833                 case EBLOCK: printf("<E(%d)>",s[-1] + s[-2] * 256); s-=2; break;
 18341834                 default: printf("%c", *s); break;
 18351835                 }
 18361836                 s--;
 18371837         }
 18381838 }
 18391839 
 18401840 static void
 18411841 prline(const usch *s)
 18421842 {
 18431843         while (*s) {
 18441844                 switch (*s) {
 18451845                 case WARN: printf("<WARN>"); break;
 18461846                 case CONC: printf("<CONC>"); break;
 18471847                 case SNUFF: printf("<SNUFF>"); break;
 18481848                 case PHOLD: printf("<PHOLD>"); break;
 18491849                 case EBLOCK: printf("<E(%d)>",s[1] + s[2] * 256); s+=2; break;
 18501850                 case '\n': printf("<NL>"); break;
 18511851                 default: printf("%c", *s); break;
 18521852                 }
 18531853                 s++;
 18541854         }
 18551855 }
 18561856 #endif
 18571857 
 18581858 usch *
 18591859 savstr(const usch *str)
 18601860 {
 18611861         usch *rv = stringbuf;
 18621862 
 18631863         do {
 18641864                 if (stringbuf >= &sbf[SBSIZE])   {
 18651865                         stringbuf = sbf; /* need space to write error message */
 18661866                         error("out of macro space!");
 18671867                 }
 18681868         } while ((*stringbuf++ = *str++));
 18691869         stringbuf--;
 18701870         return rv;
 18711871 }
 18721872 
 18731873 void
 18741874 unpstr(const usch *c)
 18751875 {
 18761876         const usch *d = c;
 18771877 
 18781878 #if 0
 18791879         if (dflag>1) {
 18801880                 printf("Xunpstr: '");
 18811881                 prline(c);
 18821882                 printf("'\n");
 18831883         }
 18841884 #endif
 18851885         while (*d) {
 18861886                 if (*d == EBLOCK)
 18871887                         d += 2;
 18881888                 d++;
 18891889         }
 18901890         while (d > c) {
 18911891                 cunput(*--d);
 18921892         }
 18931893 }
 18941894 
 18951895 static void
 18961896 flbuf(void)
 18971897 {
 18981898         if (obufp == 0)
 18991899                 return;
 19001900         if (Mflag == 0)
 19011901                 xwrite(ofd, outbuf, obufp);
 19021902         lastoch = outbuf[obufp-1];
 19031903         obufp = 0;
 19041904 }
 19051905 
 19061906 void
 19071907 putch(int ch)
 19081908 {
 19091909         outbuf[obufp++] = (usch)ch;
 19101910         if (obufp == CPPBUF || (istty && ch == '\n'))
 19111911                 flbuf();
 19121912 }
 19131913 
 19141914 void
 19151915 putstr(const usch *s)
 19161916 {
 19171917         for (; *s; s++) {
 19181918                 if (*s == PHOLD)
 19191919                         continue;
 19201920                 outbuf[obufp++] = *s;
 19211921                 if (obufp == CPPBUF || (istty && *s == '\n'))
 19221922                         flbuf();
 19231923         }
 19241924 }
 19251925 
 19261926 /*
 19271927  * convert a number to an ascii string. Store it on the heap.
 19281928  */
 19291929 static void
 19301930 num2str(int num)
 19311931 {
 19321932         static usch buf[12];
 19331933         usch *b = buf;
 19341934         int m = 0;
 19351935         
 19361936         if (num < 0)
 19371937                 num = -num, m = 1;
 19381938         do {
 19391939                 *b++ = (usch)(num % 10 + '0');
 19401940                 num /= 10;
 19411941         } while (num);
 19421942         if (m)
 19431943                 *b++ = '-';
 19441944         while (b > buf)
 19451945                 savch(*--b);
 19461946 }
 19471947 
 19481948 /*
 19491949  * similar to sprintf, but only handles %c, %s and %d.
 19501950  * saves result on heap.
 19511951  */
 19521952 static void
 19531953 vsheap(const char *fmt, va_list ap)
 19541954 {
 19551955         for (; *fmt; fmt++) {
 19561956                 if (*fmt == '%') {
 19571957                         fmt++;
 19581958                         switch (*fmt) {
 19591959                         case 's':
 19601960                                 savstr(va_arg(ap, usch *));
 19611961                                 break;
 19621962                         case 'd':
 19631963                                 num2str(va_arg(ap, int));
 19641964                                 break;
 19651965                         case 'c':
 19661966                                 savch(va_arg(ap, int));
 19671967                                 break;
 19681968                         default:
 19691969                                 error("bad sheap");
 19701970                         }
 19711971                 } else
 19721972                         savch(*fmt);
 19731973         }
 19741974         *stringbuf = 0;
 19751975 }
 19761976 
 19771977 usch *
 19781978 sheap(const char *fmt, ...)
 19791979 {
 19801980         va_list ap;
 19811981         usch *op = stringbuf;
 19821982 
 19831983         va_start(ap, fmt);
 19841984         vsheap(fmt, ap);
 19851985         va_end(ap);
 19861986 
 19871987         return op;
 19881988 }
 19891989 
 19901990 static void
 19911991 usage(void)
 19921992 {
 19931993         error("Usage: cpp [-Cdt] [-Dvar=val] [-Uvar] [-Ipath] [-Spath]");
 19941994 }
 19951995 
 19961996 #ifdef notyet
 19971997 /*
 19981998  * Symbol table stuff.
 19991999  * The data structure used is a patricia tree implementation using only
 20002000  * bytes to store offsets. 
 20012001  * The information stored is (lower address to higher):
 20022002  *
 20032003  *      unsigned char bitno[2]; bit number in the string
 20042004  *      unsigned char left[3];  offset from base to left element
 20052005  *      unsigned char right[3]; offset from base to right element
 20062006  */
 20072007 #endif
 20082008 
 20092009 /*
 20102010  * This patricia implementation is more-or-less the same as
 20112011  * used in ccom for string matching.
 20122012  */
 20132013 struct tree {
 20142014         int bitno;
 20152015         struct tree *lr[2];
 20162016 };
 20172017 
 20182018 #define BITNO(x)                ((x) & ~(LEFT_IS_LEAF|RIGHT_IS_LEAF))
 20192019 #define LEFT_IS_LEAF            0x80000000
 20202020 #define RIGHT_IS_LEAF           0x40000000
 20212021 #define IS_LEFT_LEAF(x)         (((x) & LEFT_IS_LEAF) != 0)
 20222022 #define IS_RIGHT_LEAF(x)        (((x) & RIGHT_IS_LEAF) != 0)
 20232023 #define P_BIT(key, bit)         (key[bit >> 3] >> (bit & 7)) & 1
 20242024 #define CHECKBITS               8
 20252025 
 20262026 static struct tree *sympole;
 20272027 static int numsyms;
 20282028 
 20292029 /*
 20302030  * Allocate a symtab struct and store the string.
 20312031  */
 20322032 static struct symtab *
 20332033 getsymtab(const usch *str)
 20342034 {
 20352035         struct symtab *sp = malloc(sizeof(struct symtab));
 20362036 
 20372037         if (sp == NULL)
 20382038                 error("getsymtab: couldn't allocate symtab");
 20392039         sp->namep = savstr(str);
 20402040         savch('\0');
 20412041         sp->value = NULL;
 20422042         sp->file = ifiles ? ifiles->orgfn : (const usch *)"<initial>";
 20432043         sp->line = ifiles ? ifiles->lineno : 0;
 20442044         return sp;
 20452045 }
 20462046 
 20472047 /*
 20482048  * Do symbol lookup in a patricia tree.
 20492049  * Only do full string matching, no pointer optimisations.
 20502050  */
 20512051 struct symtab *
 20522052 lookup(const usch *key, int enterf)
 20532053 {
 20542054         struct symtab *sp;
 20552055         struct tree *w, *new, *last;
 20562056         int len, cix, bit, fbit, svbit, ix, bitno;
 20572057         const usch *k, *m;
 20582058 
 20592059         /* Count full string length */
 20602060         for (k = key, len = 0; *k; k++, len++)
 20612061                 ;
 20622062 
 20632063         switch (numsyms) {
 20642064         case 0: /* no symbols yet */
 20652065                 if (enterf != ENTER)
 20662066                         return NULL;
 20672067                 sympole = (struct tree *)getsymtab(key);
 20682068                 numsyms++;
 20692069                 return (struct symtab *)sympole;
 20702070 
 20712071         case 1:
 20722072                 w = sympole;
 20732073                 svbit = 0; /* XXX gcc */
 20742074                 break;
 20752075 
 20762076         default:
 20772077                 w = sympole;
 20782078                 bitno = len * CHECKBITS;
 20792079                 for (;;) {
 20802080                         bit = BITNO(w->bitno);
 20812081                         fbit = bit > bitno ? 0 : P_BIT(key, bit);
 20822082                         svbit = fbit ? IS_RIGHT_LEAF(w->bitno) :
 20832083                             IS_LEFT_LEAF(w->bitno);
 20842084                         w = w->lr[fbit];
 20852085                         if (svbit)
 20862086                                 break;
 20872087                 }
 20882088         }
 20892089 
 20902090         sp = (struct symtab *)w;
 20912091 
 20922092         m = sp->namep;
 20932093         k = key;
 20942094 
 20952095         /* Check for correct string and return */
 20962096         for (cix = 0; *m && *k && *m == *k; m++, k++, cix += CHECKBITS)
 20972097                 ;
 20982098         if (*m == 0 && *k == 0) {
 20992099                 if (enterf != ENTER && sp->value == NULL)
 21002100                         return NULL;
 21012101                 return sp;
 21022102         }
 21032103 
 21042104         if (enterf != ENTER)
 21052105                 return NULL; /* no string found and do not enter */
 21062106 
 21072107         ix = *m ^ *k;
 21082108         while ((ix & 1) == 0)
 21092109                 ix >>= 1, cix++;
 21102110 
 21112111         /* Create new node */
 21122112         if ((new = malloc(sizeof *new)) == NULL)
 21132113                 error("getree: couldn't allocate tree");
 21142114         bit = P_BIT(key, cix);
 21152115         new->bitno = cix | (bit ? RIGHT_IS_LEAF : LEFT_IS_LEAF);
 21162116         new->lr[bit] = (struct tree *)getsymtab(key);
 21172117 
 21182118         if (numsyms++ == 1) {
 21192119                 new->lr[!bit] = sympole;
 21202120                 new->bitno |= (bit ? LEFT_IS_LEAF : RIGHT_IS_LEAF);
 21212121                 sympole = new;
 21222122                 return (struct symtab *)new->lr[bit];
 21232123         }
 21242124 
 21252125         w = sympole;
 21262126         last = NULL;
 21272127         for (;;) {
 21282128                 fbit = w->bitno;
 21292129                 bitno = BITNO(w->bitno);
 21302130                 if (bitno == cix)
 21312131                         error("bitno == cix");
 21322132                 if (bitno > cix)
 21332133                         break;
 21342134                 svbit = P_BIT(key, bitno);
 21352135                 last = w;
 21362136                 w = w->lr[svbit];
 21372137                 if (fbit & (svbit ? RIGHT_IS_LEAF : LEFT_IS_LEAF))
 21382138                         break;
 21392139         }
 21402140 
 21412141         new->lr[!bit] = w;
 21422142         if (last == NULL) {
 21432143                 sympole = new;
 21442144         } else {
 21452145                 last->lr[svbit] = new;
 21462146                 last->bitno &= ~(svbit ? RIGHT_IS_LEAF : LEFT_IS_LEAF);
 21472147         }
 21482148         if (bitno < cix)
 21492149                 new->bitno |= (bit ? LEFT_IS_LEAF : RIGHT_IS_LEAF);
 21502150         return (struct symtab *)new->lr[bit];
 21512151 }
 21522152 
 21532153 static usch *
 21542154 xstrdup(const usch *str)
 21552155 {
 21562156         usch *rv;
 21572157 
 21582158         if ((rv = (usch *)strdup((const char *)str)) == NULL)
 21592159                 error("xstrdup: out of mem");
 21602160         return rv;
 21612161 }
 21622162 
 21632163 void
 21642164 xwrite(int fd, const void *buf, unsigned int len)
 21652165 {
 21662166         if (write(fd, buf, len) != (int)len) {
 21672167                 if (fd == 2)
 21682168                         exit(2);
 21692169                 error("write error");
 21702170         }
 21712171 }
FishEye: Open Source License registered to PCC.
Your maintenance has expired. You can renew your license at http://www.atlassian.com/fisheye/renew
Atlassian FishEye, CVS analysis. (Version:1.6.3 Build:build-336 2008-11-04) - Administration - Page generated 2014-09-23 22:26 +0200