Quick Search:

Mode

Context

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

Other Diffs

Ignore

Blank Lines Whitespace: Expand:

Diff

1.182
 
1.183
 
MAIN:plunky:20121029171246
 
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 */
 413413                         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 
 424424 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         }
<>488 -        if (c != STRING && c != '<')
 489 -                goto bad;
490488 
 491489         if (c == '<') {
 492490                 fn = stringbuf;
<>493 -                while ((c = sloscan()) != '>' && c != '\n') {
 494 -                        if (c == '\n') /* XXX check - cannot reach */
  491+                while ((c = sloscan()) != '>') {
  492+                        if (c == 0)
  493+                                prem();
  494+                        if (c == '\n')
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;
<>506 -        } else {
  506+        } else if (c == STRING) {
507507                 usch *nm = stringbuf;
 508508 
<>509 -                yytext[strlen((char *)yytext)-1] = 0;
 510 -                fn = &yytext[1];
  509+                fn = yytext;
  510+                if (*fn++ == 'L')
  511+                        fn++;
  512+                fn[strlen((char *)fn) - 1] = 0;
511513                 /* first try to open file relative to previous file */
 512514                 /* but only if it is not an absolute path */
 513515                 if (*fn != '/') {
 514516                         savstr(ifiles->orgfn);
 515517                         if ((stringbuf =
 516518                             (usch *)strrchr((char *)nm, '/')) == NULL)
 517519                                 stringbuf = nm;
 518520                         else
 519521                                 stringbuf++;
 520522                 }
 521523                 safefn = stringbuf;
 522524                 savstr(fn); savch(0);
 523525                 c = yylex();
 524526                 if (c == 0)
 525527                         prem();
 526528                 if (c != '\n')
 527529                         goto bad;
 528530                 if (pushfile(nm, safefn, 0, NULL) == 0)
 529531                         goto okret;
 530532                 /* XXX may lose stringbuf space */
<>531 -        }
  533+        } else
  534+                goto bad;
<_532535 
 533536         if (fsrch(safefn, 0, incdir[0]))
 534537                 goto okret;
 535538 
 536539         error("cannot find '%s'", safefn);
 537540         /* error() do not return */
 538541 
 539542 bad:    error("bad #include");
 540543         /* error() do not return */
 541544 okret:
 542545         prtline();
 543546 }
 544547 
 545548 void
 546549 include_next(void)
 547550 {
 548551         struct symtab *nl;
 549552         usch *osp;
 550553         usch *fn;
 551554         int c;
 552555 
 553556         if (flslvl)
 554557                 return;
 555558         osp = stringbuf;
 556559         while ((c = sloscan()) == WSPACE)
 557560                 ;
 558561         if (c == IDENT) {
 559562                 /* sloscan() will not expand idents */
 560563                 if ((nl = lookup(yytext, FIND)) == NULL)
 561564                         goto bad;
 562565                 if (kfind(nl))
 563566                         unpstr(stringbuf);
 564567                 else
 565568                         unpstr(nl->namep);
 566569                 stringbuf = osp;
 567570                 c = yylex();
 568571         }
 569572         if (c != STRING && c != '<')
 570573                 goto bad;
 571574 
 572575         fn = stringbuf;
 573576         if (c == STRING) {
 574577                 savstr(&yytext[1]);
 575578                 stringbuf[-1] = 0;
 576579         } else { /* < > */
 577580                 while ((c = sloscan()) != '>') {
 578581                         if (c == '\n')
 579582                                 goto bad;
 580583                         savstr(yytext);
 581584                 }
 582585                 savch('\0');
 583586         }
 584587         while ((c = sloscan()) == WSPACE)
 585588                 ;
 586589         if (c != '\n')
 587590                 goto bad;
 588591 
 589592         if (fsrch(fn, ifiles->idx, ifiles->incs) == 0)
 590593                 error("cannot find '%s'", fn);
 591594         prtline();
 592595         return;
 593596 
 594597 bad:    error("bad #include_next");
 595598         /* error() do not return */
 596599 }
 597600 
 598601 static int
 599602 definp(void)
 600603 {
 601604         int c;
 602605 
 603606         do
 604607                 c = sloscan();
 605608         while (c == WSPACE);
 606609         return c;
 607610 }
 608611 
 609612 void
 610613 getcmnt(void)
 611614 {
 612615         int c;
 613616 
 614617         savstr(yytext);
 615618         savch(cinput()); /* Lost * */
 616619         for (;;) {
 617620                 c = cinput();
 618621                 if (c == '*') {
 619622                         c = cinput();
 620623                         if (c == '/') {
 621624                                 savstr((const usch *)"*/");
 622625                                 return;
 623626                         }
 624627                         cunput(c);
 625628                         c = '*';
 626629                 }
 627630                 savch(c);
 628631         }
 629632 }
 630633 
 631634 /*
 632635  * Compare two replacement lists, taking in account comments etc.
 633636  */
 634637 static int
 635638 cmprepl(const usch *o, const usch *n)
 636639 {
 637640         for (; *o; o--, n--) {
 638641                 /* comment skip */
 639642                 if (*o == '/' && o[-1] == '*') {
 640643                         while (*o != '*' || o[-1] != '/')
 641644                                 o--;
 642645                         o -= 2;
 643646                 }
 644647                 if (*n == '/' && n[-1] == '*') {
 645648                         while (*n != '*' || n[-1] != '/')
 646649                                 n--;
 647650                         n -= 2;
 648651                 }
 649652                 while (*o == ' ' || *o == '\t')
 650653                         o--;
 651654                 while (*n == ' ' || *n == '\t')
 652655                         n--;
 653656                 if (*o != *n)
 654657                         return 1;
 655658         }
 656659         return 0;
 657660 }
 658661 
 659662 static int
 660663 isell(void)
 661664 {
 662665         int ch;
 663666 
 664667         if ((ch = cinput()) != '.') {
 665668                 cunput(ch);
 666669                 return 0;
 667670         }
 668671         if ((ch = cinput()) != '.') {
 669672                 cunput(ch);
 670673                 cunput('.');
 671674                 return 0;
 672675         }
 673676         return 1;
 674677 }
 675678 
 676679 void
 677680 define(void)
 678681 {
 679682         struct symtab *np;
 680683         usch *args[MAXARGS+1], *ubuf, *sbeg;
 681684         int c, i, redef;
 682685         int mkstr = 0, narg = -1;
 683686         int ellips = 0;
 684687 #ifdef GCC_COMPAT
 685688         usch *gccvari = NULL;
 686689         int wascon;
 687690 #endif
 688691 
 689692         if (flslvl)
 690693                 return;
 691694         if (sloscan() != WSPACE || sloscan() != IDENT)
 692695                 goto bad;
 693696 
 694697         np = lookup(yytext, ENTER);
 695698         redef = np->value != NULL;
 696699 
 697700         defining = readmac = 1;
 698701         sbeg = stringbuf;
 699702         if ((c = sloscan()) == '(') {
 700703                 narg = 0;
 701704                 /* function-like macros, deal with identifiers */
 702705                 c = definp();
 703706                 for (;;) {
 704707                         if (c == ')')
 705708                                 break;
 706709                         if (c == '.' && isell()) {
 707710                                 ellips = 1;
 708711                                 if (definp() != ')')
 709712                                         goto bad;
 710713                                 break;
 711714                         }
 712715                         if (c == IDENT) {
 713716                                 /* make sure there is no arg of same name */
 714717                                 for (i = 0; i < narg; i++)
 715718                                         if (!strcmp((char *) args[i], (char *)yytext))
 716719                                                 error("Duplicate macro "
 717720                                                   "parameter \"%s\"", yytext);
 718721                                 if (narg == MAXARGS)
 719722                                         error("Too many macro args");
 720723                                 args[narg++] = xstrdup(yytext);
 721724                                 if ((c = definp()) == ',') {
 722725                                         if ((c = definp()) == ')')
 723726                                                 goto bad;
 724727                                         continue;
 725728                                 }
 726729 #ifdef GCC_COMPAT
 727730                                 if (c == '.' && isell()) {
 728731                                         if (definp() != ')')
 729732                                                 goto bad;
 730733                                         gccvari = args[--narg];
 731734                                         break;
 732735                                 }
 733736 #endif
 734737                                 if (c == ')')
 735738                                         break;
 736739                         }
 737740                         goto bad;
 738741                 }
 739742                 c = sloscan();
 740743         } else if (c == '\n') {
 741744                 /* #define foo */
 742745                 ;
 743746         } else if (c == 0) {
 744747                 prem();
 745748         } else if (c != WSPACE)
 746749                 goto bad;
 747750 
 748751         while (c == WSPACE)
 749752                 c = sloscan();
 750753 
 751754         /* replacement list cannot start with ## operator */
 752755         if (c == '#') {
 753756                 if ((c = sloscan()) == '#')
 754757                         goto bad;
 755758                 savch('\0');
 756759 #ifdef GCC_COMPAT
 757760                 wascon = 0;
 758761 #endif
 759762                 goto in2;
 760763         }
 761764 
 762765         /* parse replacement-list, substituting arguments */
 763766         savch('\0');
 764767         while (c != '\n') {
 765768 #ifdef GCC_COMPAT
 766769                 wascon = 0;
 767770 loop:
 768771 #endif
 769772                 switch (c) {
 770773                 case WSPACE:
 771774                         /* remove spaces if it surrounds a ## directive */
 772775                         ubuf = stringbuf;
 773776                         savstr(yytext);
 774777                         c = sloscan();
 775778                         if (c == '#') {
 776779                                 if ((c = sloscan()) != '#')
 777780                                         goto in2;
 778781                                 stringbuf = ubuf;
 779782                                 savch(CONC);
 780783                                 if ((c = sloscan()) == WSPACE)
 781784                                         c = sloscan();
 782785 #ifdef GCC_COMPAT
 783786                                 if (c == '\n')
 784787                                         break;
 785788                                 wascon = 1;
 786789                                 goto loop;
 787790 #endif
 788791                         }
 789792                         continue;
 790793 
 791794                 case '#':
 792795                         c = sloscan();
 793796                         if (c == '#') {
 794797                                 /* concat op */
 795798                                 savch(CONC);
 796799                                 if ((c = sloscan()) == WSPACE)
 797800                                         c = sloscan();
 798801 #ifdef GCC_COMPAT
 799802                                 if (c == '\n')
 800803                                         break;
 801804                                 wascon = 1;
 802805                                 goto loop;
 803806 #else
 804807                                 continue;
 805808 #endif
 806809                         }
 807810 in2:                    if (narg < 0) {
 808811                                 /* no meaning in object-type macro */
 809812                                 savch('#');
 810813                                 continue;
 811814                         }
 812815                         /* remove spaces between # and arg */
 813816                         savch(SNUFF);
 814817                         if (c == WSPACE)
 815818                                 c = sloscan(); /* whitespace, ignore */
 816819                         mkstr = 1;
 817820                         if (c == IDENT && strcmp((char *)yytext, "__VA_ARGS__") == 0)
 818821                                 continue;
 819822 
 820823                         /* FALLTHROUGH */
 821824                 case IDENT:
 822825                         if (strcmp((char *)yytext, "__VA_ARGS__") == 0) {
 823826                                 if (ellips == 0)
 824827                                         error("unwanted %s", yytext);
 825828 #ifdef GCC_COMPAT
 826829                                 savch(wascon ? GCCARG : VARG);
 827830 #else
 828831                                 savch(VARG);
 829832 #endif
 830833 
 831834                                 savch(WARN);
 832835                                 if (mkstr)
 833836                                         savch(SNUFF), mkstr = 0;
 834837                                 break;
 835838                         }
 836839                         if (narg < 0)
 837840                                 goto id; /* just add it if object */
 838841                         /* check if its an argument */
 839842                         for (i = 0; i < narg; i++)
 840843                                 if (strcmp((char *)yytext, (char *)args[i]) == 0)
 841844                                         break;
 842845                         if (i == narg) {
 843846 #ifdef GCC_COMPAT
 844847                                 if (gccvari &&
 845848                                     strcmp((char *)yytext, (char *)gccvari) == 0) {
 846849                                         savch(wascon ? GCCARG : VARG);
 847850                                         savch(WARN);
 848851                                         if (mkstr)
 849852                                                 savch(SNUFF), mkstr = 0;
 850853                                         break;
 851854                                 }
 852855 #endif
 853856                                 if (mkstr)
 854857                                         error("not argument");
 855858                                 goto id;
 856859                         }
 857860                         savch(i);
 858861                         savch(WARN);
 859862                         if (mkstr)
 860863                                 savch(SNUFF), mkstr = 0;
 861864                         break;
 862865 
 863866                 case CMNT: /* save comments */
 864867                         getcmnt();
 865868                         break;
 866869 
 867870                 case 0:
 868871                         prem();
 869872 
 870873                 default:
 871874 id:                     savstr(yytext);
 872875                         break;
 873876                 }
 874877                 c = sloscan();
 875878         }
 876879         defining = readmac = 0;
 877880         /* remove trailing whitespace */
 878881         while (stringbuf > sbeg) {
 879882                 if (stringbuf[-1] == ' ' || stringbuf[-1] == '\t')
 880883                         stringbuf--;
 881884                 /* replacement list cannot end with ## operator */
 882885                 else if (stringbuf[-1] == CONC)
 883886                         goto bad;
 884887                 else
 885888                         break;
 886889         }
 887890 #ifdef GCC_COMPAT
 888891         if (gccvari) {
 889892                 savch(narg);
 890893                 savch(VARG);
 891894         } else
 892895 #endif
 893896         if (ellips) {
 894897                 savch(narg);
 895898                 savch(VARG);
 896899         } else
 897900                 savch(narg < 0 ? OBJCT : narg);
 898901         if (redef && ifiles->idx != SYSINC) {
 899902                 if (cmprepl(np->value, stringbuf-1)) {
 900903                         sbeg = stringbuf;
 901904                         np->value = stringbuf-1;
 902905                         warning("%s redefined (previously defined at \"%s\" line %d)",
 903906                             np->namep, np->file, np->line);
 904907                 }
 905908                 stringbuf = sbeg/* forget this space */
 906909         } else
 907910                 np->value = stringbuf-1;
 908911 
 909912 #ifdef PCC_DEBUG
 910913         if (dflag) {
 911914                 const usch *w = np->value;
 912915 
 913916                 printf("!define: ");
 914917                 if (*w == OBJCT)
 915918                         printf("[object]");
 916919                 else if (*w == VARG)
 917920                         printf("[VARG%d]", *--w);
 918921                 while (*--w) {
 919922                         switch (*w) {
 920923                         case WARN: printf("<%d>", *--w); break;
 921924                         case CONC: printf("<##>"); break;
 922925                         case SNUFF: printf("<\">"); break;
 923926                         default: putchar(*w); break;
 924927                         }
 925928                 }
 926929                 putchar('\n');
 927930         }
 928931 #endif
 929932         for (i = 0; i < narg; i++)
 930933                 free(args[i]);
 931934         return;
 932935 
 933936 bad:    error("bad #define");
 934937 }
 935938 
 936939 void
 937940 warning(const char *fmt, ...)
 938941 {
 939942         va_list ap;
 940943         usch *sb;
 941944 
 942945         flbuf();
 943946         savch(0);
 944947 
 945948         sb = stringbuf;
 946949         if (ifiles != NULL)
 947950                 sheap("%s:%d: warning: ", ifiles->fname, ifiles->lineno);
 948951 
 949952         va_start(ap, fmt);
 950953         vsheap(fmt, ap);
 951954         va_end(ap);
 952955         savch('\n');
 953956         xwrite(2, sb, stringbuf - sb);
 954957         stringbuf = sb;
 955958 }
 956959 
 957960 void
 958961 error(const char *fmt, ...)
 959962 {
 960963         va_list ap;
 961964         usch *sb;
 962965 
 963966         flbuf();
 964967         savch(0);
 965968 
 966969         sb = stringbuf;
 967970         if (ifiles != NULL)
 968971                 sheap("%s:%d: error: ", ifiles->fname, ifiles->lineno);
 969972 
 970973         va_start(ap, fmt);
 971974         vsheap(fmt, ap);
 972975         va_end(ap);
 973976         savch('\n');
 974977         xwrite(2, sb, stringbuf - sb);
 975978         stringbuf = sb;
 976979 
 977980         exit(1);
 978981 }
 979982 
 980983 static void
 981984 sss(void)
 982985 {
 983986         savch(EBLOCK);
 984987         savch(cinput());
 985988         savch(cinput());
 986989 }
 987990 
 988991 static int
 989992 addmac(struct symtab *sp)
 990993 {
 991994         int c, i;
 992995 
 993996         /* Check if it exists; then save some space */
 994997         /* May be more difficult to debug cpp */
 995998         for (i = 1; i < norepptr; i++)
 996999                 if (norep[i] == sp)
 9971000                         return i;
 9981001         if (norepptr >= RECMAX)
 9991002                 error("too many macros");
 10001003         /* check norepptr */
 10011004         if ((norepptr & 255) == 0)
 10021005                 norepptr++;
 10031006         if (((norepptr >> 8) & 255) == 0)
 10041007                 norepptr += 256;
 10051008         c = norepptr;
 10061009         norep[norepptr++] = sp;
 10071010         return c;
 10081011 }
 10091012 
 10101013 static void
 10111014 doblk(void)
 10121015 {
 10131016         int c;
 10141017 
 10151018         do {
 10161019                 donex();
 10171020         } while ((c = sloscan()) == EBLOCK);
 10181021         if (c == IDENT)
 10191022                 return;
 10201023         error("EBLOCK sync error");
 10211024 }
 10221025 
 10231026 /* Block next nr in lex buffer to expand */
 10241027 int
 10251028 donex(void)
 10261029 {
 10271030         int n, i;
 10281031 
 10291032         if (bidx == RECMAX)
 10301033                 error("too deep macro recursion");
 10311034         n = cinput();
 10321035         n = MKB(n, cinput());
 10331036         for (i = 0; i < bidx; i++)
 10341037                 if (bptr[i] == n)
 10351038                         return n; /* already blocked */
 10361039         bptr[bidx++] = n;
 10371040         /* XXX - check for sp buffer overflow */
 10381041 #ifdef PCC_DEBUG
 10391042         if (dflag>1) {
 10401043                 printf("donex %d (%d) blocking:\n", bidx, n);
 10411044                 printf("donex %s(%d) blocking:", norep[n]->namep, n);
 10421045                 for (i = bidx-1; i >= 0; i--)
 10431046                         printf(" '%s'", norep[bptr[i]]->namep);
 10441047                 printf("\n");
 10451048         }
 10461049 #endif
 10471050         return n;
 10481051 }
 10491052 
 10501053 /*
 10511054  * store a character into the "define" buffer.
 10521055  */
 10531056 void
 10541057 savch(int c)
 10551058 {
 10561059         if (stringbuf >= &sbf[SBSIZE]) {
 10571060                 stringbuf = sbf; /* need space to write error message */
 10581061                 error("out of macro space!");
 10591062         }
 10601063         *stringbuf++ = (usch)c;
 10611064 }
 10621065 
 10631066 /*
 10641067  * convert _Pragma() to #pragma for output.
 10651068  * Syntax is already correct.
 10661069  */
 10671070 static void
 10681071 pragoper(void)
 10691072 {
 10701073         usch *s;
 10711074         int t;
 10721075 
 10731076         while ((t = sloscan()) != '(')
 10741077                 ;
 10751078 
 10761079         while ((t = sloscan()) == WSPACE)
 10771080                 ;
 10781081         if (t != STRING)
 10791082                 error("_Pragma() must have string argument");
 10801083         savstr((const usch *)"\n#pragma ");
 10811084         s = yytext;
 10821085         if (*s == 'L')
 10831086                 s++;
 10841087         for (; *s; s++) {
 10851088                 if (*s == EBLOCK) {
 10861089                         s+=2;
 10871090                         continue;
 10881091                 }
 10891092                 if (*s == '\"')
 10901093                         continue;
 10911094                 if (*s == '\\' && (s[1] == '\"' || s[1] == '\\'))
 10921095                         s++;
 10931096                 savch(*s);
 10941097         }
 10951098         sheap("\n# %d \"%s\"\n", ifiles->lineno, ifiles->fname);
 10961099         while ((t = sloscan()) == WSPACE)
 10971100                 ;
 10981101         if (t != ')')
 10991102                 error("_Pragma() syntax error");
 11001103 }
 11011104 
 11021105 /*
 11031106  * Return true if it is OK to expand this symbol.
 11041107  */
 11051108 static int
 11061109 okexp(struct symtab *sp)
 11071110 {
 11081111         int i;
 11091112 
 11101113         if (sp == NULL)
 11111114                 return 0;
 11121115         for (i = 0; i < bidx; i++)
 11131116                 if (norep[bptr[i]] == sp)
 11141117                         return 0;
 11151118         return 1;
 11161119 }
 11171120 
 11181121 /*
 11191122  * Insert block(s) before each expanded name.
 11201123  * Input is in lex buffer, output on lex buffer.
 11211124  */
 11221125 static void
 11231126 insblock(int bnr)
 11241127 {
 11251128         usch *bp = stringbuf;
 11261129         int c, i;
 11271130  
 11281131         IMP("IB");
 11291132         readmac++;
 11301133         while ((c = sloscan()) != WARN) {
 11311134                 if (c == EBLOCK) {
 11321135                         sss();
 11331136                         continue;
 11341137                 }
 11351138                 if (c == CMNT) {
 11361139                         getcmnt();
 11371140                         continue;
 11381141                 }
 11391142                 if (c == IDENT) {
 11401143                         savch(EBLOCK), savch(bnr & 255), savch(bnr >> 8);
 11411144                         for (i = 0; i < bidx; i++)
 11421145                                 savch(EBLOCK), savch(bptr[i] & 255),
 11431146                                     savch(bptr[i] >> 8);
 11441147                 }
 11451148                 savstr(yytext);
 11461149                 if (c == '\n')
 11471150                         (void)cinput();
 11481151         }
 11491152         savch(0);
 11501153         cunput(WARN);
 11511154         unpstr(bp);
 11521155         stringbuf = bp;
 11531156         readmac--;
 11541157         IMP("IBRET");
 11551158 }
 11561159 
 11571160 /* Delete next WARN on the input stream */
 11581161 static void
 11591162 delwarn(void)
 11601163 {
 11611164         usch *bp = stringbuf;
 11621165         int c;
 11631166  
 11641167         IMP("DELWARN");
 11651168         while ((c = sloscan()) != WARN) {
 11661169                 if (c == CMNT) {
 11671170                         getcmnt();
 11681171                 } else if (c == EBLOCK) {
 11691172                         sss();
 11701173                 } else if (c == '\n') {
 11711174                         putch(cinput());
 11721175                 } else
 11731176                         savstr(yytext);
 11741177         }
 11751178         if (stringbuf[-1] == '/')
 11761179                 savch(PHOLD); /* avoid creating comments */
 11771180         savch(0);
 11781181         unpstr(bp);
 11791182         stringbuf = bp;
 11801183         IMP("DELWRET");
 11811184 }
 11821185 
 11831186 /*
 11841187  * Handle defined macro keywords found on input stream.
 11851188  * When finished print out the full expanded line.
 11861189  * Everything on lex buffer except for the symtab.
 11871190  */
 11881191 int
 11891192 kfind(struct symtab *sp)
 11901193 {
 11911194         struct symtab *nl;
 11921195         const usch *argary[MAXARGS+1], *cbp;
 11931196         usch *sbp;
 11941197         int c, o, chkf;
 11951198 
 11961199         DPRINT(("%d:enter kfind(%s)\n",0,sp->namep));
 11971200         IMP("KFIND");
 11981201         if (*sp->value == OBJCT) {
 11991202                 if (sp == filloc) {
 12001203                         unpstr(sheap("\"%s\"", ifiles->fname));
 12011204                         return 1;
 12021205                 } else if (sp == linloc) {
 12031206                         unpstr(sheap("%d", ifiles->lineno));
 12041207                         return 1;
 12051208                 }
 12061209                 IMP("END1");
 12071210                 cunput(WARN);
 12081211                 for (cbp = sp->value-1; *cbp; cbp--)
 12091212                         cunput(*cbp);
 12101213                 insblock(addmac(sp));
 12111214                 IMP("ENDX");
 12121215                 exparg(1);
 12131216 
 12141217 upp:            sbp = stringbuf;
 12151218                 chkf = 1;
 12161219                 if (obufp != 0)
 12171220                         lastoch = outbuf[obufp-1];
 12181221                 if (iswsnl(lastoch))
 12191222                         chkf = 0;
 12201223                 if (Cflag)
 12211224                         readmac++;
 12221225                 while ((c = sloscan()) != WARN) {
 12231226                         switch (c) {
 12241227                         case CMNT:
 12251228                                 getcmnt();
 12261229                                 break;
 12271230 
 12281231                         case STRING:
 12291232                                 /* Remove embedded directives */
 12301233                                 for (cbp = yytext; *cbp; cbp++) {
 12311234                                         if (*cbp == EBLOCK)
 12321235                                                 cbp+=2;
 12331236                                         else if (*cbp != CONC)
 12341237                                                 savch(*cbp);
 12351238                                 }
 12361239                                 break;
 12371240 
 12381241                         case EBLOCK:
 12391242                                 doblk();
 12401243                                 /* FALLTHROUGH */
 12411244                         case IDENT:
 12421245                                 /*
 12431246                                  * Tricky: if this is the last identifier
 12441247                                  * in the expanded list, and it is defined
 12451248                                  * as a function-like macro, then push it
 12461249                                  * back on the input stream and let fastscan
 12471250                                  * handle it as a new macro.
 12481251                                  * BUT: if this macro is blocked then this
 12491252                                  * should not be done.
 12501253                                  */
 12511254                                 nl = lookup(yytext, FIND);
 12521255                                 o = okexp(nl);
 12531256                                 bidx = 0;
 12541257                                 /* Deal with pragmas here */
 12551258                                 if (nl == pragloc) {
 12561259                                         pragoper();
 12571260                                         break;
 12581261                                 }
 12591262                                 if (nl == NULL || !o || *nl->value == OBJCT) {
 12601263                                         /* Not fun-like macro */
 12611264                                         savstr(yytext);
 12621265                                         break;
 12631266                                 }
 12641267                                 c = cinput();
 12651268                                 if (c == WARN) {
 12661269                                         /* succeeded, push back */
 12671270                                         unpstr(yytext);
 12681271                                 } else {
 12691272                                         savstr(yytext);
 12701273                                 }
 12711274                                 cunput(c);
 12721275                                 break;
 12731276 
 12741277                         default:
 12751278                                 if (chkf && c < 127)
 12761279                                         putch(' ');
 12771280                                 savstr(yytext);
 12781281                                 break;
 12791282                         }
 12801283                         chkf = 0;
 12811284                 }
 12821285                 if (Cflag)
 12831286                         readmac--;
 12841287                 IMP("END2");
 12851288                 norepptr = 1;
 12861289                 savch(0);
 12871290                 stringbuf = sbp;
 12881291                 return 1;
 12891292         }
 12901293         /* Is a function-like macro */
 12911294 
 12921295         /* Search for '(' */
 12931296         sbp = stringbuf;
 12941297         while (iswsnl(c = cinput()))
 12951298                 savch(c);
 12961299         savch(0);
 12971300         stringbuf = sbp;
 12981301         if (c != '(') {
 12991302                 cunput(c);
 13001303                 unpstr(sbp);
 13011304                 return 0; /* Failed */
 13021305         }
 13031306 
 13041307         /* Found one, output \n to be in sync */
 13051308         for (; *sbp; sbp++) {
 13061309                 if (*sbp == '\n')
 13071310                         putch('\n'), ifiles->lineno++;
 13081311         }
 13091312 
 13101313         /* fetch arguments */
 13111314         if (Cflag)
 13121315                 readmac++;
 13131316         if (readargs(sp, argary))
 13141317                 error("readargs");
 13151318 
 13161319         c = addmac(sp);
 13171320         sbp = stringbuf;
 13181321         cunput(WARN);
 13191322 
 13201323         IMP("KEXP");
 13211324         subarg(sp, argary, 1);
 13221325         IMP("KNEX");
 13231326         insblock(c);
 13241327         IMP("KBLK");
 13251328 
 13261329         stringbuf = sbp;
 13271330 
 13281331         exparg(1);
 13291332         if (Cflag)
 13301333                 readmac--;
 13311334 
 13321335         IMP("END");
 13331336 
 13341337         goto upp;
 13351338 
 13361339 }
 13371340 
 13381341 /*
 13391342  * Replace and push-back on input stream the eventual replaced macro.
 13401343  * The check for whether it can expand or not should already have been done.
 13411344  * Blocks for this identifier will be added via insblock() after expansion.
 13421345  */
 13431346 int
 13441347 submac(struct symtab *sp, int lvl)
 13451348 {
 13461349         const usch *argary[MAXARGS+1];
 13471350         const usch *cp;
 13481351         usch *bp;
 13491352         int ch;
 13501353 
 13511354         DPRINT(("%d:submac1: trying '%s'\n", lvl, sp->namep));
 13521355         if (*sp->value == OBJCT) {
 13531356                 if (sp == filloc) {
 13541357                         unpstr(sheap("\"%s\"", ifiles->fname));
 13551358                         return 1;
 13561359                 } else if (sp == linloc) {
 13571360                         unpstr(sheap("%d", ifiles->lineno));
 13581361                         return 1;
 13591362                 }
 13601363 
 13611364                 DPRINT(("submac: exp object macro '%s'\n",sp->namep));
 13621365                 /* expand object-type macros */
 13631366                 ch = addmac(sp);
 13641367                 cunput(WARN);
 13651368 
 13661369                 for (cp = sp->value-1; *cp; cp--)
 13671370                         cunput(*cp);
 13681371                 insblock(ch);
 13691372                 delwarn();
 13701373                 return 1;
 13711374         }
 13721375 
 13731376         /*
 13741377          * Function-like macro; see if it is followed by a (
 13751378          * Be careful about the expand/noexpand balance.
 13761379          * Store read data on heap meanwhile.
 13771380          * For directive        #define foo() kaka
 13781381          * If input is          <NEX><NEX>foo<EXP>()<EXP> then
 13791382          * output should be     <NEX><NEX><EXP>kaka<EXP>.
 13801383          */
 13811384         bp = stringbuf;
 13821385         while (iswsnl(ch = cinput()))
 13831386                 savch(ch);
 13841387         savch(0);
 13851388         stringbuf = bp;
 13861389         if (ch != '(') {
 13871390                 cunput(ch);
 13881391                 unpstr(bp);
 13891392                 return 0; /* Failed */
 13901393         }
 13911394 
 13921395         /* no \n should be here */
 13931396 
 13941397         /*
 13951398          * A function-like macro has been found.  Read in the arguments,
 13961399          * expand them and push-back everything for another scan.
 13971400          */
 13981401         DPRINT(("%d:submac: continue macro '%s'\n", lvl, sp->namep));
 13991402         savch(0);
 14001403         if (readargs(sp, argary)) {
 14011404                 /* Bailed out in the middle of arg list */
 14021405                 unpstr(bp);
 14031406                 DDPRINT(("%d:noreadargs\n", lvl));
 14041407                 stringbuf = bp;
 14051408                 return 0;
 14061409         }
 14071410 
 14081411         /* when all args are read from input stream */
 14091412         ch = addmac(sp);
 14101413 
 14111414         DDPRINT(("%d:submac pre\n", lvl));
 14121415         cunput(WARN);
 14131416 
 14141417         subarg(sp, argary, lvl+1);
 14151418 
 14161419         DDPRINT(("%d:submac post\n", lvl));
 14171420         insblock(ch);
 14181421         delwarn();
 14191422 
 14201423         stringbuf = bp; /* Reset heap */
 14211424         DPRINT(("%d:Return submac\n", lvl));
 14221425         IMP("SM1");
 14231426         return 1;
 14241427 }
 14251428 
 14261429 static int
 14271430 isdir(void)
 14281431 {
 14291432         usch *bp = stringbuf;
 14301433         usch ch;
 14311434 
 14321435         while ((ch = cinput()) == ' ' || ch == '\t')
 14331436                 *stringbuf++ = ch;
 14341437         *stringbuf++ = ch;
 14351438         *stringbuf++ = 0;
 14361439         stringbuf = bp;
 14371440         if (ch == '#')
 14381441                 return 1;
 14391442         unpstr(bp);
 14401443         return 0;
 14411444 }
 14421445 
 14431446 /*
 14441447  * Deal with directives inside a macro.
 14451448  * Doing so is really ugly but gcc allows it, so...
 14461449  */
 14471450 static void
 14481451 chkdir(void)
 14491452 {
 14501453         usch ch;
 14511454 
 14521455         for (;;) {
 14531456                 if (isdir())
 14541457                         ppdir();
 14551458                 if (flslvl == 0)
 14561459                         return;
 14571460                 while ((ch = cinput()) != '\n')
 14581461                         ;
 14591462                 ifiles->lineno++;
 14601463                 putch('\n');
 14611464         }
 14621465 }
 14631466 
 14641467 /*
 14651468  * Read arguments and put in argument array.
 14661469  * If WARN is encountered return 1, otherwise 0.
 14671470  */
 14681471 int
 14691472 readargs(struct symtab *sp, const usch **args)
 14701473 {
 14711474         const usch *vp = sp->value;
 14721475         int c, i, plev, narg, ellips = 0;
 14731476         int warn;
 14741477 
 14751478         DPRINT(("readargs\n"));
 14761479 
 14771480         narg = *vp--;
 14781481         if (narg == VARG) {
 14791482                 narg = *vp--;
 14801483                 ellips = 1;
 14811484         }
 14821485 
 14831486         IMP("RDA1");
 14841487         /*
 14851488          * read arguments and store them on heap.
 14861489          */
 14871490         warn = 0;
 14881491         c = '(';
 14891492         for (i = 0; i < narg && c != ')'; i++) {
 14901493                 args[i] = stringbuf;
 14911494                 plev = 0;
 14921495                 while ((c = sloscan()) == WSPACE || c == '\n')
 14931496                         if (c == '\n') {
 14941497                                 ifiles->lineno++;
 14951498                                 putch(cinput());
 14961499                                 chkdir();
 14971500                         }
 14981501                 for (;;) {
 14991502                         while (c == EBLOCK) {
 15001503                                 sss();
 15011504                                 c = sloscan();
 15021505                         }
 15031506                         if (c == WARN) {
 15041507                                 warn++;
 15051508                                 goto oho;
 15061509                         }
 15071510                         if (plev == 0 && (c == ')' || c == ','))
 15081511                                 break;
 15091512                         if (c == '(')
 15101513                                 plev++;
 15111514                         if (c == ')')
 15121515                                 plev--;
 15131516                         savstr(yytext);
 15141517 oho:                    while ((c = sloscan()) == '\n') {
 15151518                                 ifiles->lineno++;
 15161519                                 putch(cinput());
 15171520                                 chkdir();
 15181521                                 savch(' ');
 15191522                         }
 15201523                         while (c == CMNT) {
 15211524                                 getcmnt();
 15221525                                 c = sloscan();
 15231526                         }
 15241527                         if (c == 0)
 15251528                                 error("eof in macro");
 15261529                 }
 15271530                 while (args[i] < stringbuf &&
 15281531                     iswsnl(stringbuf[-1]) && stringbuf[-3] != EBLOCK)
 15291532                         stringbuf--;
 15301533                 savch('\0');
 15311534 #ifdef PCC_DEBUG
 15321535                 if (dflag) {
 15331536                         printf("readargs: save arg %d '", i);
 15341537                         prline(args[i]);
 15351538                         printf("'\n");
 15361539                 }
 15371540 #endif
 15381541         }
 15391542 
 15401543         IMP("RDA2");
 15411544         /* Handle varargs readin */
 15421545         if (ellips)
 15431546                 args[i] = (const usch *)"";
 15441547         if (ellips && c != ')') {
 15451548                 args[i] = stringbuf;
 15461549                 plev = 0;
 15471550                 while ((c = sloscan()) == WSPACE || c == '\n')
 15481551                         if (c == '\n')
 15491552                                 cinput();
 15501553                 for (;;) {
 15511554                         if (plev == 0 && c == ')')
 15521555                                 break;
 15531556                         if (c == '(')
 15541557                                 plev++;
 15551558                         if (c == ')')
 15561559                                 plev--;
 15571560                         if (c == EBLOCK) {
 15581561                                 sss();
 15591562                         } else
 15601563                                 savstr(yytext);
 15611564                         while ((c = sloscan()) == '\n') {
 15621565                                 ifiles->lineno++;
 15631566                                 cinput();
 15641567                                 chkdir();
 15651568                                 savch(' ');
 15661569                         }
 15671570                 }
 15681571                 while (args[i] < stringbuf && iswsnl(stringbuf[-1]))
 15691572                         stringbuf--;
 15701573                 savch('\0');
 15711574                 
 15721575         }
 15731576         if (narg == 0 && ellips == 0)
 15741577                 while ((c = sloscan()) == WSPACE || c == '\n')
 15751578                         if (c == '\n')
 15761579                                 cinput();
 15771580 
 15781581         if (c != ')' || (i != narg && ellips == 0) || (i < narg && ellips == 1))
 15791582                 error("wrong arg count");
 15801583         while (warn)
 15811584                 cunput(WARN), warn--;
 15821585         return 0;
 15831586 }
 15841587 
 15851588 /*
 15861589  * expand a function-like macro.
 15871590  * vp points to end of replacement-list
 15881591  * reads function arguments from sloscan()
 15891592  * result is pushed-back for more scanning.
 15901593  */
 15911594 void
 15921595 subarg(struct symtab *nl, const usch **args, int lvl)
 15931596 {
 15941597         int narg, instr, snuff;
 15951598         const usch *sp, *bp, *ap, *vp;
 15961599 
 15971600         DPRINT(("%d:subarg '%s'\n", lvl, nl->namep));
 15981601         vp = nl->value;
 15991602         narg = *vp--;
 16001603         if (narg == VARG)
 16011604                 narg = *vp--;
 16021605 
 16031606         sp = vp;
 16041607         instr = snuff = 0;
 16051608 #ifdef PCC_DEBUG
 16061609         if (dflag>1) {
 16071610                 printf("%d:subarg ARGlist for %s: '", lvl, nl->namep);
 16081611                 prrep(vp);
 16091612                 printf("'\n");
 16101613         }
 16111614 #endif
 16121615 
 16131616         /*
 16141617          * push-back replacement-list onto lex buffer while replacing
 16151618          * arguments.  Arguments are macro-expanded if required.
 16161619          */
 16171620         while (*sp != 0) {
 16181621                 if (*sp == SNUFF)
 16191622                         cunput('\"'), snuff ^= 1;
 16201623                 else if (*sp == CONC)
 16211624                         ;
 16221625                 else if (*sp == WARN) {
 16231626 
 16241627                         if (sp[-1] == VARG) {
 16251628                                 bp = ap = args[narg];
 16261629                                 sp--;
 16271630 #ifdef GCC_COMPAT
 16281631                         } else if (sp[-1] == GCCARG) {
 16291632                                 ap = args[narg];
 16301633                                 if (ap[0] == 0)
 16311634                                         ap = (const usch *)"0";
 16321635                                 bp = ap;
 16331636                                 sp--;
 16341637 #endif
 16351638                         } else
 16361639                                 bp = ap = args[(int)*--sp];
 16371640 #ifdef PCC_DEBUG
 16381641                         if (dflag>1){
 16391642                                 printf("%d:subarg GOTwarn; arglist '", lvl);
 16401643                                 prline(bp);
 16411644                                 printf("'\n");
 16421645                         }
 16431646 #endif
 16441647                         if (sp[2] != CONC && !snuff && sp[-1] != CONC) {
 16451648                                 /*
 16461649                                  * Expand an argument; 6.10.3.1:
 16471650                                  * "A parameter in the replacement list,
 16481651                                  *  is replaced by the corresponding argument
 16491652                                  *  after all macros contained therein have
 16501653                                  *  been expanded.".
 16511654                                  */
 16521655                                 cunput(WARN);
 16531656                                 unpstr(bp);
 16541657                                 exparg(lvl+1);
 16551658                                 delwarn();
 16561659                         } else {
 16571660                         while (*bp)
 16581661                                 bp++;
 16591662                         while (bp > ap) {
 16601663                                 bp--;
 16611664                                 if (snuff && !instr && iswsnl(*bp)) {
 16621665                                         while (iswsnl(*bp))
 16631666                                                 bp--;
 16641667                                         cunput(' ');
 16651668                                 }
 16661669 
 16671670                                 cunput(*bp);
 16681671                                 if ((*bp == '\'' || *bp == '"')
 16691672                                      && bp[-1] != '\\' && snuff) {
 16701673                                         instr ^= 1;
 16711674                                         if (instr == 0 && *bp == '"')
 16721675                                                 cunput('\\');
 16731676                                 }
 16741677                                 if (instr && (*bp == '\\' || *bp == '"'))
 16751678                                         cunput('\\');
 16761679                         }
 16771680                         }
 16781681                 } else
 16791682                         cunput(*sp);
 16801683                 sp--;
 16811684         }
 16821685         DPRINT(("%d:Return subarg\n", lvl));
 16831686         IMP("SUBARG");
 16841687 }
 16851688 
 16861689 /*
 16871690  * Do a (correct) expansion of a WARN-terminated buffer of tokens.
 16881691  * Data is read from the lex buffer, result on lex buffer, WARN-terminated.
 16891692  * Expansion blocking is not altered here unless when tokens are
 16901693  * concatenated, in which case they are removed.
 16911694  */
 16921695 void
 16931696 exparg(int lvl)
 16941697 {
 16951698         struct symtab *nl;
 16961699         int c, i, gmult;
 16971700         usch *och;
 16981701         usch *osb = stringbuf;
 16991702         int anychange;
 17001703 
 17011704         DPRINT(("%d:exparg\n", lvl));
 17021705         IMP("EXPARG");
 17031706 
 17041707         readmac++;
 17051708 rescan:
 17061709         anychange = 0;
 17071710         while ((c = sloscan()) != WARN) {
 17081711                 DDPRINT(("%d:exparg swdata %d\n", lvl, c));
 17091712                 IMP("EA0");
 17101713                 bidx = 0;
 17111714                 switch (c) {
 17121715 
 17131716                 case EBLOCK:
 17141717                         doblk();
 17151718                         /* FALLTHROUGH */
 17161719                 case IDENT:
 17171720                         /*
 17181721                          * Handle argument concatenation here.
 17191722                          * In case of concatenation, add all blockings.
 17201723                          */
 17211724                         DDPRINT(("%d:exparg ident %d\n", lvl, c));
 17221725                         och = stringbuf;
 17231726                         gmult = 0;
 17241727 
 17251728 sav:                    savstr(yytext);
 17261729 
 17271730                         if ((c = cinput()) == EBLOCK) {
 17281731                                 /* yep, are concatenating; add blocks */
 17291732                                 gmult = 1;
 17301733                                 do {
 17311734                                         donex();
 17321735                                 } while ((c = sloscan()) == EBLOCK);
 17331736                                 goto sav;
 17341737                         }
 17351738                         cunput(c);
 17361739 
 17371740                         DPRINT(("%d:exparg: str '%s'\n", lvl, och));
 17381741                         IMP("EA1");
 17391742                         /* see if ident is expandable */
 17401743                         if ((nl = lookup(och, FIND)) && okexp(nl)) {
 17411744                                 /* Save blocks */
 17421745                                 int donothing;
 17431746                                 unsigned short *svidx =
 17441747                                     malloc(sizeof(int)*(bidx+1));
 17451748                                 int svbidx = bidx;
 17461749 
 17471750                                 for (i = 0; i < bidx; i++)
 17481751                                         svidx[i] = bptr[i];
 17491752                                 if (submac(nl, lvl+1)) {
 17501753                                         /* Could expand, result on lexbuffer */
 17511754                                         stringbuf = och; /* clear saved name */
 17521755                                         anychange = 1;
 17531756                                 }
 17541757                                 donothing = 0;
 17551758                                 c = cinput();
 17561759                                 if (c == 'L') {
 17571760                                         int c2 = cinput();
 17581761                                         if (c2 == '\"' || c2 == '\'')
 17591762                                                 donothing = 1;
 17601763                                         cunput(c2);
 17611764                                 }
 17621765                                 cunput(c);
 17631766 
 17641767                                 if (donothing == 0)
 17651768                                     if (((spechr[c] & C_ID) && c > 63) ||
 17661769                                     c == EBLOCK) {
 17671770                                         for (i = 0; i < svbidx; i++) {
 17681771                                                 cunput(svidx[i] >> 8);
 17691772                                                 cunput(svidx[i] & 255);
 17701773                                                 cunput(EBLOCK);
 17711774                                         }
 17721775                                 }
 17731776                                 free(svidx);
 17741777                         } else if (bidx) {
 17751778                                 /* must restore blocks */
 17761779                                 if (gmult) {
 17771780                                         unpstr(och);
 17781781                                         if (sloscan() != IDENT)
 17791782                                                 error("exparg sync error");
 17801783                                 }
 17811784                                 stringbuf = och;
 17821785                                 for (i = 0; i < bidx; i++)
 17831786                                         savch(EBLOCK), savch(bptr[i] & 255),
 17841787                                             savch(bptr[i] >> 8);
 17851788                                 savstr(yytext);
 17861789                         }
 17871790                         bidx = 0;
 17881791                         IMP("EA2");
 17891792                         break;
 17901793 
 17911794                 case CMNT:
 17921795                         getcmnt();
 17931796                         break;
 17941797 
 17951798                 case '\n':
 17961799                         cinput();
 17971800                         savch(' ');
 17981801                         break;
 17991802 
 18001803                 default:
 18011804                         savstr(yytext);
 18021805                         break;
 18031806                 }
 18041807         }
 18051808         *stringbuf = 0;
 18061809         cunput(WARN);
 18071810         unpstr(osb);
 18081811         DPRINT(("%d:exparg return: change %d\n", lvl, anychange));
 18091812         IMP("EXPRET");
 18101813         stringbuf = osb;
 18111814         if (anychange)
 18121815                 goto rescan;
 18131816         readmac--;
 18141817 }
 18151818 
 18161819 #ifdef PCC_DEBUG
 18171820 static void
 18181821 imp(const char *str)
 18191822 {
 18201823         printf("%s (%d) '", str, bidx);
 18211824         prline(ifiles->curptr);
 18221825         printf("'\n");
 18231826 }
 18241827 
 18251828 static void
 18261829 prrep(const usch *s)
 18271830 {
 18281831         while (*s) {
 18291832                 switch (*s) {
 18301833                 case WARN: printf("<ARG(%d)>", *--s); break;
 18311834                 case CONC: printf("<CONC>"); break;
 18321835                 case SNUFF: printf("<SNUFF>"); break;
 18331836                 case EBLOCK: printf("<E(%d)>",s[-1] + s[-2] * 256); s-=2; break;
 18341837                 default: printf("%c", *s); break;
 18351838                 }
 18361839                 s--;
 18371840         }
 18381841 }
 18391842 
 18401843 static void
 18411844 prline(const usch *s)
 18421845 {
 18431846         while (*s) {
 18441847                 switch (*s) {
 18451848                 case WARN: printf("<WARN>"); break;
 18461849                 case CONC: printf("<CONC>"); break;
 18471850                 case SNUFF: printf("<SNUFF>"); break;
 18481851                 case PHOLD: printf("<PHOLD>"); break;
 18491852                 case EBLOCK: printf("<E(%d)>",s[1] + s[2] * 256); s+=2; break;
 18501853                 case '\n': printf("<NL>"); break;
 18511854                 default: printf("%c", *s); break;
 18521855                 }
 18531856                 s++;
 18541857         }
 18551858 }
 18561859 #endif
 18571860 
 18581861 usch *
 18591862 savstr(const usch *str)
 18601863 {
 18611864         usch *rv = stringbuf;
 18621865 
 18631866         do {
 18641867                 if (stringbuf >= &sbf[SBSIZE])   {
 18651868                         stringbuf = sbf; /* need space to write error message */
 18661869                         error("out of macro space!");
 18671870                 }
 18681871         } while ((*stringbuf++ = *str++));
 18691872         stringbuf--;
 18701873         return rv;
 18711874 }
 18721875 
 18731876 void
 18741877 unpstr(const usch *c)
 18751878 {
 18761879         const usch *d = c;
 18771880 
 18781881 #if 0
 18791882         if (dflag>1) {
 18801883                 printf("Xunpstr: '");
 18811884                 prline(c);
 18821885                 printf("'\n");
 18831886         }
 18841887 #endif
 18851888         while (*d) {
 18861889                 if (*d == EBLOCK)
 18871890                         d += 2;
 18881891                 d++;
 18891892         }
 18901893         while (d > c) {
 18911894                 cunput(*--d);
 18921895         }
 18931896 }
 18941897 
 18951898 static void
 18961899 flbuf(void)
 18971900 {
 18981901         if (obufp == 0)
 18991902                 return;
 19001903         if (Mflag == 0)
 19011904                 xwrite(ofd, outbuf, obufp);
 19021905         lastoch = outbuf[obufp-1];
 19031906         obufp = 0;
 19041907 }
 19051908 
 19061909 void
 19071910 putch(int ch)
 19081911 {
 19091912         outbuf[obufp++] = (usch)ch;
 19101913         if (obufp == CPPBUF || (istty && ch == '\n'))
 19111914                 flbuf();
 19121915 }
 19131916 
 19141917 void
 19151918 putstr(const usch *s)
 19161919 {
 19171920         for (; *s; s++) {
 19181921                 if (*s == PHOLD)
 19191922                         continue;
 19201923                 outbuf[obufp++] = *s;
 19211924                 if (obufp == CPPBUF || (istty && *s == '\n'))
 19221925                         flbuf();
 19231926         }
 19241927 }
 19251928 
 19261929 /*
 19271930  * convert a number to an ascii string. Store it on the heap.
 19281931  */
 19291932 static void
 19301933 num2str(int num)
 19311934 {
 19321935         static usch buf[12];
 19331936         usch *b = buf;
 19341937         int m = 0;
 19351938         
 19361939         if (num < 0)
 19371940                 num = -num, m = 1;
 19381941         do {
 19391942                 *b++ = (usch)(num % 10 + '0');
 19401943                 num /= 10;
 19411944         } while (num);
 19421945         if (m)
 19431946                 *b++ = '-';
 19441947         while (b > buf)
 19451948                 savch(*--b);
 19461949 }
 19471950 
 19481951 /*
 19491952  * similar to sprintf, but only handles %c, %s and %d.
 19501953  * saves result on heap.
 19511954  */
 19521955 static void
 19531956 vsheap(const char *fmt, va_list ap)
 19541957 {
 19551958         for (; *fmt; fmt++) {
 19561959                 if (*fmt == '%') {
 19571960                         fmt++;
 19581961                         switch (*fmt) {
 19591962                         case 's':
 19601963                                 savstr(va_arg(ap, usch *));
 19611964                                 break;
 19621965                         case 'd':
 19631966                                 num2str(va_arg(ap, int));
 19641967                                 break;
 19651968                         case 'c':
 19661969                                 savch(va_arg(ap, int));
 19671970                                 break;
 19681971                         default:
 19691972                                 error("bad sheap");
 19701973                         }
 19711974                 } else
 19721975                         savch(*fmt);
 19731976         }
 19741977         *stringbuf = 0;
 19751978 }
 19761979 
 19771980 usch *
 19781981 sheap(const char *fmt, ...)
 19791982 {
 19801983         va_list ap;
 19811984         usch *op = stringbuf;
 19821985 
 19831986         va_start(ap, fmt);
 19841987         vsheap(fmt, ap);
 19851988         va_end(ap);
 19861989 
 19871990         return op;
 19881991 }
 19891992 
 19901993 static void
 19911994 usage(void)
 19921995 {
 19931996         error("Usage: cpp [-Cdt] [-Dvar=val] [-Uvar] [-Ipath] [-Spath]");
 19941997 }
 19951998 
 19961999 #ifdef notyet
 19972000 /*
 19982001  * Symbol table stuff.
 19992002  * The data structure used is a patricia tree implementation using only
 20002003  * bytes to store offsets. 
 20012004  * The information stored is (lower address to higher):
 20022005  *
 20032006  *      unsigned char bitno[2]; bit number in the string
 20042007  *      unsigned char left[3];  offset from base to left element
 20052008  *      unsigned char right[3]; offset from base to right element
 20062009  */
 20072010 #endif
 20082011 
 20092012 /*
 20102013  * This patricia implementation is more-or-less the same as
 20112014  * used in ccom for string matching.
 20122015  */
 20132016 struct tree {
 20142017         int bitno;
 20152018         struct tree *lr[2];
 20162019 };
 20172020 
 20182021 #define BITNO(x)                ((x) & ~(LEFT_IS_LEAF|RIGHT_IS_LEAF))
 20192022 #define LEFT_IS_LEAF            0x80000000
 20202023 #define RIGHT_IS_LEAF           0x40000000
 20212024 #define IS_LEFT_LEAF(x)         (((x) & LEFT_IS_LEAF) != 0)
 20222025 #define IS_RIGHT_LEAF(x)        (((x) & RIGHT_IS_LEAF) != 0)
 20232026 #define P_BIT(key, bit)         (key[bit >> 3] >> (bit & 7)) & 1
 20242027 #define CHECKBITS               8
 20252028 
 20262029 static struct tree *sympole;
 20272030 static int numsyms;
 20282031 
 20292032 /*
 20302033  * Allocate a symtab struct and store the string.
 20312034  */
 20322035 static struct symtab *
 20332036 getsymtab(const usch *str)
 20342037 {
 20352038         struct symtab *sp = malloc(sizeof(struct symtab));
 20362039 
 20372040         if (sp == NULL)
 20382041                 error("getsymtab: couldn't allocate symtab");
 20392042         sp->namep = savstr(str);
 20402043         savch('\0');
 20412044         sp->value = NULL;
 20422045         sp->file = ifiles ? ifiles->orgfn : (const usch *)"<initial>";
 20432046         sp->line = ifiles ? ifiles->lineno : 0;
 20442047         return sp;
 20452048 }
 20462049 
 20472050 /*
 20482051  * Do symbol lookup in a patricia tree.
 20492052  * Only do full string matching, no pointer optimisations.
 20502053  */
 20512054 struct symtab *
 20522055 lookup(const usch *key, int enterf)
 20532056 {
 20542057         struct symtab *sp;
 20552058         struct tree *w, *new, *last;
 20562059         int len, cix, bit, fbit, svbit, ix, bitno;
 20572060         const usch *k, *m;
 20582061 
 20592062         /* Count full string length */
 20602063         for (k = key, len = 0; *k; k++, len++)
 20612064                 ;
 20622065 
 20632066         switch (numsyms) {
 20642067         case 0: /* no symbols yet */
 20652068                 if (enterf != ENTER)
 20662069                         return NULL;
 20672070                 sympole = (struct tree *)getsymtab(key);
 20682071                 numsyms++;
 20692072                 return (struct symtab *)sympole;
 20702073 
 20712074         case 1:
 20722075                 w = sympole;
 20732076                 svbit = 0; /* XXX gcc */
 20742077                 break;
 20752078 
 20762079         default:
 20772080                 w = sympole;
 20782081                 bitno = len * CHECKBITS;
 20792082                 for (;;) {
 20802083                         bit = BITNO(w->bitno);
 20812084                         fbit = bit > bitno ? 0 : P_BIT(key, bit);
 20822085                         svbit = fbit ? IS_RIGHT_LEAF(w->bitno) :
 20832086                             IS_LEFT_LEAF(w->bitno);
 20842087                         w = w->lr[fbit];
 20852088                         if (svbit)
 20862089                                 break;
 20872090                 }
 20882091         }
 20892092 
 20902093         sp = (struct symtab *)w;
 20912094 
 20922095         m = sp->namep;
 20932096         k = key;
 20942097 
 20952098         /* Check for correct string and return */
 20962099         for (cix = 0; *m && *k && *m == *k; m++, k++, cix += CHECKBITS)
 20972100                 ;
 20982101         if (*m == 0 && *k == 0) {
 20992102                 if (enterf != ENTER && sp->value == NULL)
 21002103                         return NULL;
 21012104                 return sp;
 21022105         }
 21032106 
 21042107         if (enterf != ENTER)
 21052108                 return NULL; /* no string found and do not enter */
 21062109 
 21072110         ix = *m ^ *k;
 21082111         while ((ix & 1) == 0)
 21092112                 ix >>= 1, cix++;
 21102113 
 21112114         /* Create new node */
 21122115         if ((new = malloc(sizeof *new)) == NULL)
 21132116                 error("getree: couldn't allocate tree");
 21142117         bit = P_BIT(key, cix);
 21152118         new->bitno = cix | (bit ? RIGHT_IS_LEAF : LEFT_IS_LEAF);
 21162119         new->lr[bit] = (struct tree *)getsymtab(key);
 21172120 
 21182121         if (numsyms++ == 1) {
 21192122                 new->lr[!bit] = sympole;
 21202123                 new->bitno |= (bit ? LEFT_IS_LEAF : RIGHT_IS_LEAF);
 21212124                 sympole = new;
 21222125                 return (struct symtab *)new->lr[bit];
 21232126         }
 21242127 
 21252128         w = sympole;
 21262129         last = NULL;
 21272130         for (;;) {
 21282131                 fbit = w->bitno;
 21292132                 bitno = BITNO(w->bitno);
 21302133                 if (bitno == cix)
 21312134                         error("bitno == cix");
 21322135                 if (bitno > cix)
 21332136                         break;
 21342137                 svbit = P_BIT(key, bitno);
 21352138                 last = w;
 21362139                 w = w->lr[svbit];
 21372140                 if (fbit & (svbit ? RIGHT_IS_LEAF : LEFT_IS_LEAF))
 21382141                         break;
 21392142         }
 21402143 
 21412144         new->lr[!bit] = w;
 21422145         if (last == NULL) {
 21432146                 sympole = new;
 21442147         } else {
 21452148                 last->lr[svbit] = new;
 21462149                 last->bitno &= ~(svbit ? RIGHT_IS_LEAF : LEFT_IS_LEAF);
 21472150         }
 21482151         if (bitno < cix)
 21492152                 new->bitno |= (bit ? LEFT_IS_LEAF : RIGHT_IS_LEAF);
 21502153         return (struct symtab *)new->lr[bit];
 21512154 }
 21522155 
 21532156 static usch *
 21542157 xstrdup(const usch *str)
 21552158 {
 21562159         usch *rv;
 21572160 
 21582161         if ((rv = (usch *)strdup((const char *)str)) == NULL)
 21592162                 error("xstrdup: out of mem");
 21602163         return rv;
 21612164 }
 21622165 
 21632166 void
 21642167 xwrite(int fd, const void *buf, unsigned int len)
 21652168 {
 21662169         if (write(fd, buf, len) != (int)len) {
 21672170                 if (fd == 2)
 21682171                         exit(2);
 21692172                 error("write error");
 21702173         }
 21712174 }
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-22 01:40 +0100