Quick Search:

Mode

Context

Displaying 3 lines of context. None | Less | More | Full

Other Diffs

Ignore

Blank Lines Whitespace: Expand:

Diff

1.35
 
1.36
 
MAIN:ragge:20060928111007
 
cpp.c
_>6666  * from V7 cpp, and at last ansi/c99 support.
 6767  */
 6868 
<> 69+/*
  70+ * Arbetsgång:
  71+ *      1) Skriv om så att det finns en input() som förprocessar streamen.
  72+ *      2) Skriv om så att makros går framifrån, inte bakifrån.
  73+ *      3) Skriv om så att man använder lex, inte manuellt.
  74+ */
  75+
6976 #include "../../config.h"
 7077 
 7178 #include <sys/wait.h>
     
 !
7885 #include <string.h>
 7986 #include <time.h>
 8087 #include <unistd.h>
<>81 -#include <ctype.h>
8288 
 8389 #ifdef HAVE_ALLOCA_H
 8490 #include <alloca.h>
 8591 #endif
 8692 
 8793 #include "cpp.h"
<> 94+#include "y.tab.h"
8895 
 8996 #define MAXARG  250     /* # of args to a macro, limited by char value */
<>90 -#define SBSIZE  400000
 91 -#define SYMSIZ  10000
  97+#define SBSIZE  200000
  98+#define SYMSIZ  6000
9299 
 93100 static usch     sbf[SBSIZE];
 94101 /* C command */
 95102 
<>96 -#ifdef ragge
 97 -
 98 -#define CHAR_BITS       8 /* XXX - should be checked in autoconf */
 99 -#define CHAR_MAX        (1 << CHAR_BITS)
 100 -
 101 -#define TOKTYP  3       /* mask bits for token type */
 102 -#define S_      1       /* string or char constant start */
 103 -
 104 -/*
 105 - * Index into this array to find out whether a char has a special
 106 - * meaning.  This is ASCII only, for EBCDIC write a new table.
 107 - */
 108 -usch utype[CHAR_MAX] = {
 109 -        0,      0,      0,      0,      0,      0,      0,      0, /* 0  - 7  */
 110 -        0,      0,      0,      0,      0,      0,      0,      0, /* 8  - 15 */
 111 -        0,      0,      0,      0,      0,      0,      0,      0, /* 16 - 23 */
 112 -        0,      0,      0,      0,      0,      0,      0,      0, /* 24 - 31 */
 113 -        0,      S_,     0,      0,      0,      0,      0,      S_,/* 32 - 39 */
 114 -        0,      0,      0,      0,      0,      0,      0,      0, /* 40 - 47 */
 115 -        0,      0,      0,      0,      0,      0,      0,      0, /* 48 - 55 */
 116 -        0,      0,      0,      0,      0,      0,      0,      0, /* 56 - 63 */
 117 -        0,      0,      0,      0,      0,      0,      0,      0, /* 64 - 71 */
 118 -        0,      0,      0,      0,      0,      0,      0,      0, /* 72 - 79 */
 119 -        0,      0,      0,      0,      0,      0,      0,      0, /* 80 - 87 */
 120 -        0,      0,      0,      0,      0,      0,      0,      0, /* 88 - 95 */
 121 -        0,      0,      0,      0,      0,      0,      0,      0, /* 96 - 103*/
 122 -        0,      0,      0,      0,      0,      0,      0,      0, /* 104- 111*/
 123 -        0,      0,      0,      0,      0,      0,      0,      0, /* 112- 119*/
 124 -        0,      0,      0,      0,      0,      0,      0,      0, /* 120- 127*/
 125 -};
 126 -#endif
 127 -
128103 int tflag;      /* traditional cpp syntax */
 129104 #ifdef CPP_DEBUG
 130105 int dflag;      /* debug printouts */
<> 106+#define DPRINT(x) if (dflag) printf x
  107+#define DDPRINT(x) if (dflag > 1) printf x
  108+#else
  109+#define DPRINT(x)
  110+#define DDPRINT(x)
131111 #endif
<> 112+
132113 FILE *obuf;
 133114 static int exfail;
 134115 struct symtab symtab[SYMSIZ];
     
 !
183164 #define ENTER   1
 184165 
 185166 static void expdef(usch *proto, struct recur *, int gotwarn);
<>186 -#ifdef ragge
 187 -static void control(struct includ *);
 188 -#else
 189 -static void control(void);
 190 -#endif
 191 -static void define(void);
  167+void define(void);
192168 static void expmac(struct recur *);
 193169 static int canexpand(struct recur *, struct symtab *np);
<>194 -static void include(void);
 195 -static void line(void);
  170+void include(void);
  171+void line(void);
196172 
 197173 int
 198174 main(int argc, char **argv)
     
 !
275251                  * Manually move in the predefined macros.
 276252                  */
 277253                 nl = lookup("__TIME__", ENTER);
<>278 -#ifdef ragge
 279 -                nl->value = stringbuf;
 280 -                savch(OBJCT);
 281 -                savch('"'); savstr(&n[11]); n[19] = 0; savch('"'); savch(0);
 282 -#else
283254                 savch(0); savch('"');  n[19] = 0; savstr(&n[11]); savch('"');
 284255                 savch(OBJCT);
 285256                 nl->value = stringbuf-1;
<>286 -#endif
287257 
 288258                 nl = lookup("__DATE__", ENTER);
<>289 -#ifdef ragge
 290 -                nl->value = stringbuf;
 291 -                savch(OBJCT); savch('"'); savstr(&n[20]);
 292 -                savstr(&n[4]); n[24] = n[11] = 0; savch('"'); savch(0);
 293 -#else
294259                 savch(0); savch('"'); n[24] = n[11] = 0; savstr(&n[4]);
 295260                 savstr(&n[20]); savch('"'); savch(OBJCT);
 296261                 nl->value = stringbuf-1;
<>297 -#endif
298262 
 299263                 nl = lookup("__STDC__", ENTER);
<>300 -#ifdef ragge
 301 -                nl->value = stringbuf;
 302 -                savch(OBJCT); savch('1'); savch(0);
 303 -#else
304264                 savch(0); savch('1'); savch(OBJCT);
 305265                 nl->value = stringbuf-1;
<>306 -#endif
307266         }
 308267 
 309268         if (argc == 2) {
     
 !
321280         return exfail;
 322281 }
 323282 
<>324 -#ifdef ragge
 325 -/*
 326 - * Scan the input file for macros to replace and preprocessor directives.
 327 - */
328283 void
<>329 -scanover(struct includ *ic)
  284+gotident(struct symtab *nl)
330285 {
<>331 -        struct symtab *nl;
 332 -        usch *sp, *st;
 333 -        int c, oc, gnl;
 334 -        int wasnl = 1;
  286+        struct symtab *thisnl;
  287+        usch *osp, *ss2;
  288+        int c;
335289 
<>336 -        for (;;) {
 337 -                if ((c = inch(ic)) < 0)
 338 -                        return;
 339 -                if (c != ' ' && c != '\n' && c != '#')
 340 -                        wasnl = 0; /* cannot be control */
  290+        thisnl = NULL;
  291+        slow = 1;
  292+        osp = stringbuf;
  293+        goto found;
  294+
  295+        while ((c = yylex()) != 0) {
341296                 switch (c) {
<>342 -                case '\'': /* charcon */
 343 -                case '"': /* string */
 344 -                        gnl = 0;
 345 -                        oc = c;
 346 -                        do {
 347 -                                if (c == GOTNL)
 348 -                                        gnl++;
 349 -                                else
 350 -                                        outch(c);
 351 -                                if (c == '\\') /* avoid escaped chars */
 352 -                                        outch(inch(ic));
 353 -                        } while ((c = inch(ic)) != -1 && c != oc);
 354 -                        outch(c);
 355 -                        while (gnl--)
 356 -                                putc('\n', obuf);
 357 -                        break;
  297+                case IDENT:
  298+                        if (flslvl)
  299+                                break;
  300+                        osp = stringbuf;
358301 
<>359 -                case '0': case '1': case '2': case '3': case '4':
 360 -                case '5': case '6': case '7': case '8': case '9':
 361 -#define ONEMORE(c, ic)  (outch(c), inch(ic))
 362 -                        oc = c;
 363 -                        outch(c);
 364 -                        c = inch(ic);
 365 -                        if (oc == '0' && (c == 'x' || c == 'X')) {
 366 -                                do {
 367 -                                        c = ONEMORE(c, ic);
 368 -                                } while (isxdigit(c));
  302+                        DPRINT(("IDENT0: %s\n", yytext));
  303+                        nl = lookup(yytext, FIND);
  304+                        if (nl == 0 || thisnl == 0)
  305+                                goto found;
  306+                        if (thisnl == nl) {
  307+                                nl = 0;
  308+                                goto found;
  309+                        }
  310+                        ss2 = stringbuf;
  311+                        if ((c = yylex()) == WSPACE) {
  312+                                savstr(yytext);
  313+                                c = yylex();
  314+                        }
  315+                        if (c != EXPAND) {
  316+                                unpstr(yytext);
  317+                                if (ss2 != stringbuf)
  318+                                        unpstr(ss2);
  319+                                unpstr(nl->namep);
  320+                                (void)yylex(); /* get yytext correct */
  321+                                nl = 0; /* ignore */
369322                         } else {
<>370 -                                while (isdigit(c)) {
 371 -                                        c = ONEMORE(c, ic);
  323+                                thisnl = NULL;
  324+                                if (nl->value[0] == OBJCT) {
  325+                                        unpstr(nl->namep);
  326+                                        (void)yylex(); /* get yytext correct */
  327+                                        nl = 0;
372328                                 }
 373329                         }
<>374 -                        if (c != '.' && c != 'e' && c != 'E') {
 375 -                                /* not floating point number */
 376 -                                while (c == 'l' || c == 'L' ||
 377 -                                    c == 'u' || c == 'U') {
 378 -                                        c = ONEMORE(c, ic);
 379 -                                }
 380 -                                unch(ic, c);
 381 -                                break; /* done here */
 382 -                        }
 383 -                        /* it's a floating point number here */
 384 -                        if (c != '.')
 385 -                                goto E;
  330+                        stringbuf = ss2;
386331 
<>387 -                        /* decimal point */
 388 -F:                      do { /* may be followed by digits */
 389 -                                c = ONEMORE(c, ic);
 390 -                        } while (isdigit(c));
 391 -                        if (c == 'e' || c == 'E') {
 392 -E:                              c = ONEMORE(c, ic);
 393 -                                if (c == '-' || c == '+')
 394 -                                        c = ONEMORE(c, ic);
 395 -                                while (isdigit(c))
 396 -                                        c = ONEMORE(c, ic);
  332+found:                  if (nl == 0 || subst(nl, NULL) == 0) {
  333+                                fputs(yytext, obuf);
  334+                        } else if (osp != stringbuf) {
  335+                                DPRINT(("IDENT1: unput osp %p stringbuf %p\n",
  336+                                    osp, stringbuf));
  337+                                ss2 = stringbuf;
  338+                                cunput(EXPAND);
  339+                                while (ss2 > osp)
  340+                                        cunput(*--ss2);
  341+                                thisnl = nl;
397342                         }
<>398 -                        if (c == 'f' || c == 'F'||c == 'l' || c == 'L')
 399 -                                c = ONEMORE(c, ic);
 400 -                        unch(ic, c);
  343+                        stringbuf = osp; /* clean up heap */
401344                         break;
 402345 
<>403 -                case '.':
 404 -                        c = ONEMORE(c, ic);
 405 -                        if (isdigit(c))
 406 -                                goto F;
 407 -                        unch(ic, c);
  346+                case EXPAND:
  347+                        DPRINT(("EXPAND!\n"));
  348+                        thisnl = NULL;
408349                         break;
 409350 
<>410 -                case GOTNL:
 411 -                        putch('\n');
  351+                case CHARCON:
  352+                case STRING:
  353+                case '\n':
  354+                case NUMBER:
  355+                case FPOINT:
  356+                case WSPACE:
  357+                        if (flslvl == 0)
  358+                                fputs(yytext, obuf);
412359                         break;
 413360 
<>414 -                case ' ':
 415 -                case '\t': /* whitespace */
 416 -                        while (c == ' ' || c == '\t')
 417 -                                c = ONEMORE(c, ic);
 418 -                        /* FALLTHROUGH */
 419 -                case '#':
 420 -                        if (wasnl && c == '#')
 421 -                                control(ic);
 422 -                        else
 423 -                                unch(ic, c);
 424 -                        break;
 425 -
 426 -                case '/':
 427 -                        if ((c = inch(ic)) == '/') {
 428 -                                if (Cflag)
 429 -                                        fprintf(obuf, "//");
 430 -                                while ((c = inch(ic)) >= 0 && c != '\n')
 431 -                                        if (Cflag)
 432 -                                                putc(c, obuf);
 433 -                                unch(ic, c);
 434 -                        } else if (c == '*') {
 435 -                                if (Cflag)
 436 -                                        fprintf(obuf, "/*");
 437 -                                oc = 0;
 438 -                                do {
 439 -                                        while ((c = inch(ic)) >= 0&& c != '*') {
 440 -                                                if (c == '\n') {
 441 -                                                        putc(c, obuf);
 442 -                                                        ic->lineno++;
 443 -                                                } else if (Cflag)
 444 -                                                        putc(c, obuf);
 445 -                                        }
 446 -                                        if (Cflag)
 447 -                                                putc(c, obuf);
 448 -                                        if ((c = inch(ic)) == '/')
 449 -                                                break;
 450 -                                        unch(ic, c);
 451 -                                } while (c >= 0);
 452 -                                if (Cflag)
 453 -                                        putc(c, obuf);
 454 -                                if (!tflag)
 455 -                                        putc(' ', obuf);
 456 -                        } else {
 457 -                                unch(ic, c);
 458 -                                putc('/', obuf);
 459 -                        }
 460 -                        break;
 461 -
 462 -                case 'L': /* may be STRING, CHARCON or identifier */
 463 -                        if ((c = inch(ic)) == '"' || c == '\'') {
 464 -                                /* just print out L and continue */
 465 -                                unch(ic, c);
 466 -                                putc('L', obuf);
 467 -                                break;
 468 -                        } else {
 469 -                                unch(ic, c);
 470 -                                c = 'L';
 471 -                        }
 472 -                        /* FALLTHROUGH */
473361                 default:
<>474 -                        if (!isalpha(c) && c != '_') {
 475 -                                putc(c, obuf);
 476 -                                break;
  362+                        if (flslvl == 0) {
  363+                                if (c < 256)
  364+                                        putch(c);
  365+                                else
  366+                                        error("bad dir2 %d", c);
477367                         }
<>478 -                        /*
 479 -                         * got an identifier, store it on the heap and
 480 -                         * try to find a definition.
 481 -                         */
 482 -                        sp = stringbuf;
 483 -                        while (isalpha(c) || isdigit(c) || c == '_') {
 484 -                                *stringbuf++ = c;
 485 -                                c = inch(ic);
 486 -                        }
 487 -                        unch(ic, c);
 488 -                        *stringbuf = 0;
 489 -                        if ((nl = lookup(sp, FIND)) != NULL &&
 490 -                            (st = subst(nl, NULL)) != NULL) {
 491 -                                fprintf(obuf, "%s", st);
 492 -                        } else
 493 -                                fprintf(obuf, "%s", sp);
 494 -                        stringbuf = sp;
  368+                        break;
495369                 }
<> 370+                if (thisnl == NULL) {
  371+                        slow = 0;
  372+                        return;
  373+                }
496374         }
 497375 }
<>498 -#endif
499376 
<>500 -#ifndef ragge
501377 void
 502378 mainscan()
 503379 {
 504380         struct symtab *nl, *thisnl;
 505381         usch *osp, *ss2;
<>506 -        int c, gotspc;
  382+        int c;
507383 
 508384         thisnl = NULL;
 509385         while ((c = yylex()) != 0) {
 510386                 switch (c) {
<>511 -                case CONTROL:
 512 -                        control();
 513 -                        break;
 514 -
515387                 case IDENT:
 516388                         if (flslvl)
 517389                                 break;
 518390                         osp = stringbuf;
<>519 -if(dflag)printf("IDENT0: %s\n", yystr);
 520 -                        nl = lookup(yystr, FIND);
 521 -if(dflag)printf("IDENT: %s\n", yystr);
  391+if(dflag)printf("IDENT0: %s\n", yytext);
  392+                        nl = lookup(yytext, FIND);
  393+if(dflag)printf("IDENT: %s\n", yytext);
  394+                        slow = 1;
522395                         if (nl == 0 || thisnl == 0)
 523396                                 goto found;
 524397                         if (thisnl == nl) {
 525398                                 nl = 0;
 526399                                 goto found;
 527400                         }
<>528 -                        gotspc = 0;
 529 -                        if ((c = yylex()) == WSPACE)
 530 -                                gotspc = 1, c = yylex();
  401+                        ss2 = stringbuf;
  402+                        if ((c = yylex()) == WSPACE) {
  403+                                savstr(yytext);
  404+                                c = yylex();
  405+                        }
531406                         if (c != EXPAND) {
<>532 -                                unpstr(yystr);
 533 -                                if (gotspc)
 534 -                                        cunput(' ');
  407+                                unpstr(yytext);
  408+                                if (ss2 != stringbuf)
  409+                                        unpstr(ss2);
535410                                 unpstr(nl->namep);
<>536 -                                (void)yylex(); /* get yystr correct */
  411+                                (void)yylex(); /* get yytext correct */
537412                                 nl = 0; /* ignore */
 538413                         } else {
 539414                                 thisnl = NULL;
 540415                                 if (nl->value[0] == OBJCT) {
 541416                                         unpstr(nl->namep);
<>542 -                                        (void)yylex(); /* get yystr correct */
  417+                                        (void)yylex(); /* get yytext correct */
543418                                         nl = 0;
 544419                                 }
 545420                         }
<> 421+                        stringbuf = ss2;
546422 
<>547 -#ifdef ragge
548423 found:                  if (nl == 0 || subst(nl, NULL) == 0) {
<>549 -#else
 550 -found:                  if (nl == 0 || subst(yystr, nl, NULL) == 0) {
 551 -#endif
 552 -                                fputs(yystr, obuf);
  424+                                fputs(yytext, obuf);
553425                         } else if (osp != stringbuf) {
 554426 if(dflag)printf("IDENT1: unput osp %p stringbuf %p\n", osp, stringbuf);
 555427                                 ss2 = stringbuf;
     
 !
559431                                 thisnl = nl;
 560432                         }
 561433                         stringbuf = osp; /* clean up heap */
<> 434+                        slow = 0;
562435                         break;
 563436 
 564437                 case EXPAND:
 565438 if(dflag)printf("EXPAND!\n");
 566439                         thisnl = NULL;
 567440                         break;
 568441 
<>569 -                case NL:
 570 -                        if (flslvl == 0) {
 571 -                                if (curline() == 1)
 572 -                                        prtline();
 573 -                                else
 574 -                                        putch('\n');
 575 -                        }
 576 -                        break;
 577 -
578442                 case CHARCON:
 579443                 case STRING:
<> 444+                case '\n':
  445+                        error("bad dir %d", c);
  446+                        break;
  447+
580448                 case NUMBER:
 581449                 case FPOINT:
 582450                 case WSPACE:
<>583 -                case ELLIPS:
 584 -                case CONCAT:
 585 -                case MKSTR:
586451                         if (flslvl == 0)
<>587 -                                fputs(yystr, obuf);
  452+                                fputs(yytext, obuf);
588453                         break;
 589454 
 590455                 default:
     
 !
598463                 }
 599464         }
 600465 }
<>601 -#endif
602466 
<>603 -#if 0
 604 -struct noexp {
 605 -        struct noexp *prev;
 606 -        struct symtab *nl;
 607 -};
 608 -
 609 -/*
 610 - * Create a replace list from nl, avoiding to expand bd.
 611 - */
 612 -static usch *
 613 -replfun(struct symtab *nl, struct noexp *bd, inp...)
 614 -{
 615 -        usch **args;
 616 -
 617 -}
 618 -
 619 -/*
 620 - * Replace the object-type macro nl with its value.
 621 - */
 622 -static usch *
 623 -replobj(struct symtab *nl, struct noexp *bd, inp...)
 624 -{
 625 -        usch *base = stringbuf;
 626 -
 627 -        while ((t = intok(nl->value)) != 0) {
 628 -                if (t != IDENT) {
 629 -                        wstr(
 630 -}
 631 -
 632 -/*
 633 - * Macro-expand the occurrence of string str with its relacement-list.
 634 - * Return the expanded and replaced string.
 635 - */
 636 -static usch *
 637 -replace(struct symtab *nl, struct noexp *bd, inp...)
 638 -{
 639 -        struct noexp n;
 640 -
 641 -        n.prev = bd;
 642 -        n.nl = nl;
 643 -
 644 -        /* is this macro something we want? */
 645 -        if (nl->value[0] == OBJCT)
 646 -                return replobj(nl, &n, inp);
 647 -        /* search for first ( */
 648 -        while ((c = tokget(inp)) == WSPACE || c == NL)
 649 -                if (c == NL) /* happens only when read from file */
 650 -                        putc('\n', obuf);
 651 -        if (c != '(')
 652 -                return tokput(c), NULL; /* not correct, ignore */
 653 -        return replfun(nl, &n, inp);
 654 -}
 655 -#endif
 656 -
 657 -/*
 658 - * do something when a '#' is found.
 659 - */
660467 void
<>661 -#ifdef ragge
 662 -control(struct includ *ic)
 663 -#else
 664 -control()
 665 -#endif
 666 -{
 667 -        struct symtab *np;
 668 -        int t;
 669 -
 670 -#define CHECK(x) (yystr[0] == #x[0]) && strcmp(yystr, #x) == 0
 671 -
 672 -        if ((t = yylex()) == WSPACE)
 673 -                t = yylex();
 674 -        if (t == NL) {
 675 -                /* Just ignore */
 676 -                putc('\n', obuf);
 677 -                return;
 678 -        }
 679 -        if (t != IDENT)
 680 -                return error("bad control '%s'", yystr);
 681 -
 682 -        if (CHECK(include)) {
 683 -                if (flslvl)
 684 -                        goto exit;
 685 -                include();
 686 -                return;
 687 -        } else if (CHECK(else)) {
 688 -                if (flslvl) {
 689 -                        if (elflvl > trulvl)
 690 -                                ;
 691 -                        else if (--flslvl!=0) {
 692 -                                flslvl++;
 693 -                        } else {
 694 -                                trulvl++;
 695 -                                prtline();
 696 -                        }
 697 -                } else if (trulvl) {
 698 -                        flslvl++;
 699 -                        trulvl--;
 700 -                } else
 701 -                        error("If-less else");
 702 -                if (elslvl==trulvl+flslvl) error("Too many else");
 703 -                elslvl=trulvl+flslvl;
 704 -        } else if (CHECK(endif)) {
 705 -                if (flslvl) {
 706 -                        flslvl--;
 707 -                        if (flslvl == 0)
 708 -                                prtline();
 709 -                } else if (trulvl)
 710 -                        trulvl--;
 711 -                else
 712 -                        error("If-less endif");
 713 -                if (flslvl == 0)
 714 -                        elflvl = 0;
 715 -                elslvl = 0;
 716 -        } else if (CHECK(error)) {
 717 -                usch *ch = stringbuf;
 718 -                if (flslvl)
 719 -                        goto exit;
 720 -                while (yylex() != NL)
 721 -                        savstr(yystr);
 722 -                savch('\n');
 723 -                error("error: %s", ch);
 724 -#define GETID() if (yylex() != WSPACE || yylex() != IDENT) goto cfail
 725 -        } else if (CHECK(define)) {
 726 -                if (flslvl)
 727 -                        goto exit;
 728 -                GETID();
 729 -                define();
 730 -        } else if (CHECK(ifdef)) {
 731 -                GETID();
 732 -                if (flslvl == 0 && lookup(yystr, FIND) != 0)
 733 -                        trulvl++;
 734 -                else
 735 -                        flslvl++;
 736 -        } else if (CHECK(ifndef)) {
 737 -                GETID();
 738 -                if (flslvl == 0 && lookup(yystr, FIND) == 0)
 739 -                        trulvl++;
 740 -                else
 741 -                        flslvl++;
 742 -        } else if (CHECK(undef)) {
 743 -                GETID();
 744 -                if (flslvl == 0 && (np = lookup(yystr, FIND)))
 745 -                        np->value = 0;
 746 -        } else if (CHECK(line)) {
 747 -                if (flslvl)
 748 -                        goto exit;
 749 -                line();
 750 -        } else if (CHECK(if)) {
 751 -                if (flslvl==0 && yyparse())
 752 -                        ++trulvl;
 753 -                else
 754 -                        ++flslvl;
 755 -        } else if (CHECK(pragma)) {
 756 -                goto exit;
 757 -        } else if (CHECK(elif)) {
 758 -                if (flslvl == 0)
 759 -                        elflvl = trulvl;
 760 -                if (flslvl) {
 761 -                        if (elflvl > trulvl)
 762 -                                ;
 763 -                        else if (--flslvl!=0)
 764 -                                ++flslvl;
 765 -                        else {
 766 -                                if (yyparse()) {
 767 -                                        ++trulvl;
 768 -                                        prtline();
 769 -                                } else
 770 -                                        ++flslvl;
 771 -                        }
 772 -                } else if (trulvl) {
 773 -                        ++flslvl;
 774 -                        --trulvl;
 775 -                } else
 776 -                        error("If-less elif");
 777 -        } else
 778 -#if 0
 779 -                error("undefined control '%s'", yystr);
 780 -#else
 781 -                goto exit;
 782 -#endif
 783 -
 784 -        return;
 785 -
 786 -cfail:
 787 -        error("control line syntax error");
 788 -
 789 -exit:
 790 -        while (yylex() != NL)
 791 -                ;
 792 -        putc('\n', obuf);
 793 -#undef CHECK
 794 -}
 795 -
 796 -void
797468 line()
 798469 {
 799470         struct symtab *nl;
     
 !
804475         if ((c = yylex()) == IDENT) {
 805476                 /* Do macro preprocessing first */
 806477                 usch *osp = stringbuf;
<>807 -                if ((nl = lookup(yystr, FIND)) == NULL)
  478+                if ((nl = lookup(yytext, FIND)) == NULL)
808479                         goto bad;
<>809 -#ifdef ragge
810480                 if (subst(nl, NULL) == 0)
<>811 -#else
 812 -                if (subst(yystr, nl, NULL) == 0)
 813 -#endif
814481                         goto bad;
 815482                 while (stringbuf > osp)
 816483                         cunput(*--stringbuf);
     
 !
819486 
 820487         if (c != NUMBER)
 821488                 goto bad;
<>822 -        setline(atoi(yystr));
  489+        setline(atoi(yytext));
823490 
<>824 -        if ((c = yylex()) != NL && c != WSPACE)
  491+        if ((c = yylex()) != '\n' && c != WSPACE)
825492                 goto bad;
<>826 -        if (c == NL)
  493+        if (c == '\n')
827494                 return setline(curline()+1);
 828495         if (yylex() != STRING)
 829496                 goto bad;
<>830 -        yystr[strlen(yystr)-1] = 0;
 831 -        setfile(&yystr[1]);
  497+        yytext[strlen(yytext)-1] = 0;
  498+        setfile(&yytext[1]);
832499         return;
 833500 
 834501 bad:    error("bad line directive");
     
 !
855522                 goto bad;
 856523 
 857524         if (c == IDENT) {
<>858 -                if ((nl = lookup(yystr, FIND)) == NULL)
  525+                if ((nl = lookup(yytext, FIND)) == NULL)
859526                         goto bad;
<>860 -#ifdef ragge
861527                 if (subst(nl, NULL) == 0)
<>862 -#else
 863 -                if (subst(yystr, nl, NULL) == 0)
 864 -#endif
865528                         goto bad;
 866529                 savch('\0');
 867530                 unpstr(osp);
 868531                 goto again;
 869532         } else if (c == '<') {
 870533                 fn = stringbuf;
<>871 -                while ((c = yylex()) != '>' && c != NL) {
 872 -                        if (c == NL)
  534+                while ((c = yylex()) != '>' && c != '\n') {
  535+                        if (c == '\n')
873536                                 goto bad;
<>874 -                        savstr(yystr);
  537+                        savstr(yytext);
875538                 }
 876539                 savch('\0');
 877540                 it = SYSINC;
 878541         } else {
 879542                 usch *nm = stringbuf;
 880543 
<>881 -                yystr[strlen(yystr)-1] = 0;
 882 -                fn = &yystr[1];
  544+                yytext[strlen(yytext)-1] = 0;
  545+                fn = &yytext[1];
883546                 /* first try to open file relative to previous file */
 884547                 savstr(curfile());
 885548                 if ((stringbuf = strrchr(nm, '/')) == NULL)
     
 !
919582         struct symtab *np;
 920583         usch *args[MAXARG], *ubuf, *sbeg;
 921584         int c, i, redef;
<>922 -#ifdef ragge
 923 -        int mkstr = 0, narg = OBJCT;
 924 -#else
925585         int mkstr = 0, narg = -1;
<>926 -#endif
927586 
<>928 -        np = lookup(yystr, ENTER);
  587+        slow = 1;
  588+        if (yylex() != WSPACE || yylex() != IDENT)
  589+                error("bad define1");
  590+
  591+        np = lookup(yytext, ENTER);
929592         redef = np->value != NULL;
 930593 
 931594         sbeg = stringbuf;
 932595         if ((c = yylex()) == '(') {
 933596                 narg = 0;
 934597                 /* function-like macros, deal with identifiers */
 935598                 while ((c = yylex()) != ')') {
<>936 -                        while (c == WSPACE)
 937 -                                c = yylex();
 938 -                        if (c == ',') c = yylex();
 939 -                        while (c == WSPACE)
 940 -                                c = yylex();
  599+                        while (c == WSPACE) c = yylex();
  600+                        if (c == ',') c = yylex(); /* XXX may fail */
  601+                        while (c == WSPACE) c = yylex();
941602                         if (c == ')')
 942603                                 break;
 943604                         if (c != IDENT)
<>944 -                                error("define error, c %d", c);
 945 -                        args[narg] = alloca(strlen(yystr)+1);
 946 -                        strcpy(args[narg], yystr);
  605+                                error("define error");
  606+                        args[narg] = alloca(strlen(yytext)+1);
  607+                        strcpy(args[narg], yytext);
947608                         narg++;
 948609                 }
<>949 -        } else if (c == NL) {
  610+                c = yylex();
  611+        } else if (c == '\n') {
950612                 /* #define foo */
<>951 -                cunput('\n');
  613+                ;
952614         } else if (c != WSPACE)
 953615                 error("bad define");
 954616 
<>955 -        c = yylex();
956617         while (c == WSPACE)
 957618                 c = yylex();
 958619 
 959620         /* parse replacement-list, substituting arguments */
<>960 -#ifdef ragge
 961 -        savch(narg);
 962 -#else
963621         savch('\0');
<>964 -#endif
 965 -        while (c != NL) {
  622+        while (c != '\n') {
966623                 switch (c) {
 967624                 case WSPACE:
 968625                         /* remove spaces if it surrounds a ## directive */
 969626                         ubuf = stringbuf;
<>970 -                        savstr(yystr);
  627+                        savstr(yytext);
971628                         c = yylex();
 972629                         if (c == CONCAT) {
 973630                                 stringbuf = ubuf;
     
 !
998655 
 999656                         /* FALLTHROUGH */
 1000657                 case IDENT:
<>1001 -#ifdef ragge
 1002 -                        if (narg == OBJCT)
 1003 -#else
1004658                         if (narg < 0)
<>1005 -#endif
1006659                                 goto id; /* just add it if object */
 1007660                         /* check if its an argument */
 1008661                         for (i = 0; i < narg; i++)
<>1009 -                                if (strcmp(yystr, args[i]) == 0)
  662+                                if (strcmp(yytext, args[i]) == 0)
1010663                                         break;
 1011664                         if (i == narg) {
 1012665                                 if (mkstr)
 1013666                                         error("not argument");
 1014667                                 goto id;
 1015668                         }
<>1016 -#ifdef ragge
 1017 -                        savch(WARN);
1018669                         savch(i);
<>1019 -#else
 1020 -                        savch(i);
1021670                         savch(WARN);
<>1022 -#endif
1023671                         if (mkstr)
 1024672                                 savch(SNUFF), mkstr = 0;
 1025673                         break;
 1026674 
 1027675                 default:
<>1028 -id:                     savstr(yystr);
  676+id:                     savstr(yytext);
1029677                         break;
 1030678                 }
 1031679                 c = yylex();
     
 !
1037685                 else
 1038686                         break;
 1039687         }
<>1040 -#ifdef ragge
 1041 -        savch(0); /* end of macro */
 1042 -#else
1043688         savch(narg < 0 ? OBJCT : narg);
<>1044 -#endif
1045689         if (redef) {
 1046690                 usch *o = np->value, *n = stringbuf-1;
 1047691 
     
 !
1052696                         error("%s redefined", np->namep);
 1053697                 stringbuf = sbeg/* forget this space */
 1054698         } else
<>1055 -#ifdef ragge
 1056 -                np->value = sbeg;
 1057 -#else
1058699                 np->value = stringbuf-1;
<>1059 -#endif
 1060 -        putc('\n', obuf);
1061700 
 1062701 #ifdef CPP_DEBUG
 1063702         if (dflag) {
     
 !
1068707                         printf("[object]");
 1069708                 else
 1070709                         printf("[%d]", *w);
<>1071 -#ifdef ragge
 1072 -                while (*++w) {
 1073 -                        switch (*w) {
 1074 -                        case WARN: printf("<%d>", *++w); break;
 1075 -#else
1076710                 while (*--w) {
 1077711                         switch (*w) {
 1078712                         case WARN: printf("<%d>", *--w); break;
<>1079 -#endif
1080713                         case CONC: printf("<##>"); break;
 1081714                         case SNUFF: printf("<\">"); break;
 1082715                         default: putchar(*w); break;
     
 !
1085718                 putchar('\n');
 1086719         }
 1087720 #endif
<> 721+        slow = 0;
1088722 }
 1089723 
 1090724 void
     
 !
1151785         return(sp->namep ? sp : 0);
 1152786 }
 1153787 
<>1154 -#if 0
1155788 /*
<>1156 - * Scan over a string, copying its contents to the heap until
 1157 - * an identifier or an argument is reached. When reached,
 1158 - * return a pointer to the object.
 1159 - */
 1160 -static usch *
 1161 -tokenize(usch *s)
 1162 -{
 1163 -        usch *op = stringbuf;
 1164 -        int os;
 1165 -
 1166 -        for (; *s; s++) {
 1167 -                switch (utype[*s] & TOKTYP) {
 1168 -                case S_: /* char or string constant to skip over */
 1169 -                        os = *s;
 1170 -                        do {
 1171 -                                savch(*s);
 1172 -                                if (*s == 0)
 1173 -                                        return op;
 1174 -                                if (*s == '\\') {
 1175 -                                        s++;
 1176 -                                        savch(*s);
 1177 -                                        if (*s == 0)
 1178 -                                                return op;
 1179 -                                }
 1180 -                                s++;
 1181 -                        } while (*s && *s != os);
 1182 -                        savch(*s);
 1183 -                        if (*s == 0)
 1184 -                                return op;
 1185 -                        break;
 1186 -
 1187 -                case B_: /* Beginning of an identifier */
 1188 -                        
 1189 -                        
 1190 -
 1191 -                default:
 1192 -                        savch(*s);
 1193 -                        break;
 1194 -                }
 1195 -        }
 1196 -        savch(*s);
 1197 -        return op;
 1198 -}
 1199 -#endif
 1200 -
 1201 -#ifdef ragge
 1202 -/*
 1203 - * Replace the symbol sp with its replacement list, macroexpand the list
 1204 - * and return the fully replaced list.
 1205 - * If substitution fails return NULL.
 1206 - */
 1207 -usch *
 1208 -subst(struct symtab *sp, struct recur *rp)
 1209 -#else
 1210 -/*
1211789  * substitute namep for sp->value.
 1212790  */
 1213791 int
<>1214 -subst(np, sp, rp)
 1215 -char *np;
  792+subst(sp, rp)
1216793 struct symtab *sp;
 1217794 struct recur *rp;
<>1218 -#endif
1219795 {
 1220796         struct recur rp2;
 1221797         register usch *vp, *cp;
<>1222 -#ifdef ragge
 1223 -        usch *basesb = stringbuf;
 1224 -#endif
1225798         int c, rv = 0;
 1226799 
 1227800 if (dflag)printf("subst: %s\n", sp->namep);
     
 !
1232805                 savch('"');
 1233806                 savstr(curfile());
 1234807                 savch('"');
<>1235 -#ifdef ragge
 1236 -                savch('\0');
 1237 -                return basesb;
 1238 -#else
1239808                 return 1;
<>1240 -#endif
1241809         } else if (sp == linloc) {
 1242810                 char buf[12];
 1243811                 sprintf(buf, "%d", curline());
 1244812                 savstr(buf);
<>1245 -#ifdef ragge
 1246 -                return basesb;
 1247 -#else
1248813                 return 1;
<>1249 -#endif
1250814         }
 1251815         vp = sp->value;
 1252816 
 1253817         rp2.next = rp;
 1254818         rp2.sp = sp;
 1255819 
<>1256 -#ifdef ragge
 1257 -        if (*vp++ != OBJCT) {
 1258 -#else
1259820         if (*vp-- != OBJCT) {
<>1260 -#endif
1261821                 int gotwarn = 0;
 1262822 
 1263823                 /* should we be here at all? */
 1264824                 /* check if identifier is followed by parentheses */
 1265825                 rv = 1;
 1266826                 do {
<>1267 -                        if ((c = yylex()) == NL)
 1268 -                                putc('\n', obuf);
  827+                        c = yylex();
1269828                         if (c == WARN) {
 1270829                                 gotwarn++;
 1271830                                 if (rp == NULL)
 1272831                                         goto noid;
 1273832                         }
<>1274 -                } while (c == WSPACE || c == NL || c == WARN);
  833+                } while (c == WSPACE || c == '\n' || c == WARN);
1275834 
<>1276 -                cp = yystr;
  835+                cp = yytext;
1277836                 while (*cp)
 1278837                         cp++;
<>1279 -                while (cp > (usch *)yystr)
  838+                while (cp > (usch *)yytext)
1280839                         cunput(*--cp);
<>1281 -if (dflag)printf("c %d\n", c);
  840+                DPRINT(("c %d\n", c));
1282841                 if (c == '(' ) {
 1283842                         expdef(vp, &rp2, gotwarn);
<>1284 -#ifdef ragge
 1285 -                        return basesb;
 1286 -#else
 1287 -                        return 1;
 1288 -#endif
  843+                        return rv;
1289844                 } else {
 1290845                         /* restore identifier */
 1291846 noid:                   while (gotwarn--)
     
 !
1301856                         return 0;
 1302857                 }
 1303858         } else {
<>1304 -#if 0
 1305 -                while ((*vp
 1306 -                        tokenize(
 1307 -                expmac(&rp2);
 1308 -#else
1309859                 cunput(WARN);
 1310860                 cp = vp;
 1311861                 while (*cp) {
     
 !
1314864                         cp--;
 1315865                 }
 1316866                 expmac(&rp2);
<>1317 -#endif
1318867         }
<>1319 -#ifdef ragge
 1320 -        return basesb;
 1321 -#else
1322868         return 1;
<>1323 -#endif
1324869 }
 1325870 
 1326871 /*
     
 !
1332877 expmac(struct recur *rp)
 1333878 {
 1334879         struct symtab *nl;
<>1335 -        int c, noexp = 0, gotspc;
 1336 -        usch *och;
  880+        int c, noexp = 0;
  881+        usch *och, *stksv;
  882+        extern int yyleng;
1337883 
 1338884 #ifdef CPP_DEBUG
 1339885         if (dflag) {
     
 !
1353899                 case IDENT:
 1354900                         /* workaround if an arg will be concatenated */
 1355901                         och = stringbuf;
<>1356 -                        savstr(yystr);
  902+                        savstr(yytext);
1357903                         savch('\0');
<>1358 -if (dflag > 1)printf("id: str %s\n", och);
  904+                        DDPRINT(("id: str %s\n", och));
1359905                         if ((c = yylex()) == EXPAND) {
<>1360 -if (dflag > 1)printf("funnet expand\n");
  906+                                DDPRINT(("funnet expand\n"));
1361907                                 if ((c = yylex()) == NOEXP) {
<>1362 -if (dflag > 1)printf("funnet noexp\n");
  908+                                        DDPRINT(("funnet noexp\n"));
1363909                                         if ((c = yylex()) == IDENT) {
 1364910 yid:
<>1365 -if (dflag > 1)printf("funnet ident %s%s\n", och, yystr);
  911+                                                DDPRINT(("funnet ident %s%s\n",
  912+                                                    och, yytext));
1366913                                                 stringbuf--;
<>1367 -                                                savstr(yystr);
  914+                                                savstr(yytext);
1368915                                                 savch('\0');
 1369916                                                 cunput(NOEXP);
 1370917                                                 unpstr(och);
 1371918                                                 noexp--;
 1372919                                                 stringbuf = och;
 1373920                                                 continue;
 1374921                                         } else {
<>1375 -if (dflag > 1)printf("ofunnet ident\n");
 1376 -                                                unpstr(yystr);
  922+                                                DDPRINT(("ofunnet ident\n"));
  923+                                                unpstr(yytext);
1377924                                                 unpstr(och);
 1378925                                                 stringbuf = och;
 1379926                                                 continue;
 1380927                                         }
 1381928                                 } else {
 1382929                                         if (c == IDENT)
 1383930                                                 goto yid;
<>1384 -if (dflag > 1)printf("ofunnet inoexp\n");
 1385 -                                        unpstr(yystr);
  931+                                        DDPRINT(("ofunnet inoexp\n"));
  932+                                        unpstr(yytext);
1386933                                         cunput(EXPAND);
 1387934                                         unpstr(och);
 1388935                                         yylex();
 1389936                                 }
 1390937                         } else {
<>1391 -if (dflag > 1)printf("ofunnet expand got (%d)\n", c);
  938+                                DDPRINT(("ofunnet expand got (%d)\n", c));
1392939                                 if (c == NOEXP) {
 1393940                                         if ((c = yylex()) == IDENT) {
 1394941                                                 noexp++;
 1395942                                                 goto yid;
 1396943                                         }
<>1397 -                                        unpstr(yystr);
  944+                                        unpstr(yytext);
1398945                                         cunput(NOEXP);
 1399946                                 } else
<>1400 -                                        unpstr(yystr);
 1401 -if (dflag > 1)printf("ofunnet expand yys (%d)\n", *yystr);
  947+                                        unpstr(yytext);
  948+                                DDPRINT(("ofunnet expand yys (%d)\n", *yytext));
1402949                                 unpstr(och);
 1403950                                 yylex();
<>1404 -if (dflag > 1)printf("ofunnet expand: yystr %s\n", yystr);
  951+                                DDPRINT(("ofunnet expand: yytext %s\n",
  952+                                    yytext));
1405953                         }
 1406954                         stringbuf = och;
 1407955 
<>1408 -                        if ((nl = lookup(yystr, FIND)) == NULL)
  956+                        if ((nl = lookup(yytext, FIND)) == NULL)
1409957                                 goto def;
 1410958 
 1411959                         if (canexpand(rp, nl) == 0)
 1412960                                 goto def;
<> 961+                        /*
  962+                         * If noexp == 0 then expansion of any macro is
  963+                         * allowed.  If noexp == 1 then expansion of a
  964+                         * fun-like macro is allowed iff there is an
  965+                         * EXPAND between the identifier and the '('.
  966+                         */
1413967                         if (noexp == 0) {
<>1414 -#ifdef ragge
 1415 -                                if (subst(nl, rp) == NULL)
  968+                                if ((c = subst(nl, rp)) == 0)
1416969                                         goto def;
<>1417 -#else
 1418 -                                if ((c = subst(nl->namep, nl, rp)) == 0)
 1419 -                                        goto def;
 1420 -#endif
1421970                                 break;
 1422971                         }
 1423972                         if (noexp != 1)
 1424973                                 error("bad noexp %d", noexp);
<>1425 -                        gotspc = 0;
 1426 -                        if ((c = yylex()) == WSPACE)
 1427 -                                gotspc = 1, c = yylex();
 1428 -                        if (c == EXPAND) {
  974+                        stksv = NULL;
  975+                        if ((c = yylex()) == WSPACE) {
  976+                                stksv = alloca(yyleng+1);
  977+                                strcpy(stksv, yytext);
  978+                                c = yylex();
  979+                        }
  980+                        /* only valid for expansion if fun macro */
  981+                        if (c == EXPAND && *nl->value != OBJCT) {
1429982                                 noexp--;
<>1430 -#ifdef ragge
1431983                                 if (subst(nl, rp))
 1432984                                         break;
<>1433 -#else
 1434 -                                if (subst(nl->namep, nl, rp))
 1435 -                                        break;
 1436 -#endif
1437985                                 savstr(nl->namep);
<>1438 -                                if (gotspc)
 1439 -                                        savch(' ');
  986+                                if (stksv)
  987+                                        savstr(stksv);
1440988                         } else {
<>1441 -                                unpstr(yystr);
 1442 -                                if (gotspc)
 1443 -                                        cunput(' ');
  989+                                unpstr(yytext);
  990+                                if (stksv)
  991+                                        unpstr(stksv);
1444992                                 savstr(nl->namep);
 1445993                         }
 1446994                         break;
 1447995 
 1448996                 case STRING:
 1449997                         /* remove EXPAND/NOEXP from strings */
<>1450 -                        if (yystr[1] == NOEXP) {
  998+                        if (yytext[1] == NOEXP) {
1451999                                 savch('"');
<>1452 -                                och = &yystr[2];
  1000+                                och = &yytext[2];
14531001                                 while (*och != EXPAND)
 14541002                                         savch(*och++);
 14551003                                 savch('"');
     
 !
14581006                         /* FALLTHROUGH */
 14591007 
 14601008 def:            default:
<>1461 -                        savstr(yystr);
  1009+                        savstr(yytext);
14621010                         break;
 14631011                 }
 14641012         }
<>1465 -if (dflag)printf("return from expmac\n");
  1013+        DPRINT(("return from expmac\n"));
14661014 }
 14671015 
 14681016 /*
     
 !
14791027         usch **args, *sptr, *ap, *bp, *sp;
 14801028         int narg, c, i, plev, snuff, instr;
 14811029 
<>1482 -if (dflag)printf("expdef %s rp %s\n", vp, (rp ? (char *)rp->sp->namep : ""));
  1030+        DPRINT(("expdef %s rp %s\n", vp, (rp ? (char *)rp->sp->namep : "")));
14831031         if ((c = yylex()) != '(')
 14841032                 error("got %c, expected )", c);
 14851033         narg = vp[1];
     
 !
15031051                                 plev++;
 15041052                         if (c == ')')
 15051053                                 plev--;
<>1506 -                        savstr(yystr);
 1507 -                        while ((c = yylex()) == NL) {
 1508 -                                putch('\n');
 1509 -                                savch(' ');
 1510 -                        }
  1054+                        savstr(yytext);
  1055+                        while ((c = yylex()) == '\n')
  1056+                                c = yylex(), savch('\n');
15111057                 }
 15121058                 while (args[i] < stringbuf &&
 15131059                     (stringbuf[-1] == ' ' || stringbuf[-1] == '\t'))
     
 !
15221068         while (gotwarn--)
 15231069                 cunput(WARN);
 15241070 
<>1525 -#ifdef CPP_DEBUG
 1526 -        if (dflag) {
 1527 -
 1528 -        }
 1529 -#endif
15301071         sp = vp;
 15311072         instr = snuff = 0;
 15321073 
     
 !
15491090                                         bp++;
 15501091                                 while (bp > ap)
 15511092                                         cunput(*--bp);
<>1552 -if (dflag) printf("expand arg %d string %s\n", *sp, ap);
  1093+                                DPRINT(("expand arg %d string %s\n", *sp, ap));
15531094                                 bp = ap = stringbuf;
 15541095                                 savch(NOEXP);
 15551096                                 expmac(NULL);
     
 !
15601101                                 bp++;
 15611102                         while (bp > ap) {
 15621103                                 bp--;
<>1563 -//printf("*bp %d\n", *bp);
15641104                                 if (snuff && !instr &&
 15651105                                     (*bp == ' ' || *bp == '\t' || *bp == '\n')){
 15661106                                         while (*bp == ' ' || *bp == '\t' ||
 15671107                                             *bp == '\n') {
<>1568 -                                                if (*bp == '\n')
 1569 -                                                        putc('\n', obuf);
15701108                                                 bp--;
 15711109                                         }
 15721110                                         cunput(' ');
     
 !
16321170 {
 16331171         putc(ch, obuf);
 16341172 }
<_ 1173+
  1174+void
  1175+putstr(usch *s)
  1176+{
  1177+        while (*s) {
  1178+                putc(*s, obuf);
  1179+                s++;
  1180+        }
  1181+}
  1182+
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-08-31 02:14 +0200