Quick Search:

Mode

Context

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

Other Diffs

Ignore

Blank Lines Whitespace: Expand:

Diff

1.102
 
1.103
 
MAIN:plunky:20121107095702
 
token.c
_>11 /*      $Id$    */
 22 
 33 /*
 44  * Copyright (c) 2004,2009 Anders Magnusson. All rights reserved.
 55  *
 66  * Redistribution and use in source and binary forms, with or without
 77  * modification, are permitted provided that the following conditions
 88  * are met:
 99  * 1. Redistributions of source code must retain the above copyright
 1010  *    notice, this list of conditions and the following disclaimer.
 1111  * 2. Redistributions in binary form must reproduce the above copyright
 1212  *    notice, this list of conditions and the following disclaimer in the
 1313  *    documentation and/or other materials provided with the distribution.
 1414  *
 1515  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 1616  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 1717  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 1818  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 1919  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 2020  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 2121  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 2222  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 2323  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 2424  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 2525  */
 2626 
 2727 /*
 2828  * Tokenizer for the C preprocessor.
 2929  * There are three main routines:
 3030  *      - fastscan() loops over the input stream searching for magic
 3131  *              characters that may require actions.
 3232  *      - sloscan() tokenize the input stream and returns tokens.
 3333  *              It may recurse into itself during expansion.
 3434  *      - yylex() returns something from the input stream that
 3535  *              is suitable for yacc.
 3636  *
 3737  *      Other functions of common use:
 3838  *      - inpch() returns a raw character from the current input stream.
 3939  *      - inch() is like inpch but \\n and trigraphs are expanded.
 4040  *      - unch() pushes back a character to the input stream.
 4141  */
 4242 
 4343 #include "config.h"
 4444 
 4545 #include <stdlib.h>
 4646 #include <string.h>
 4747 #ifdef HAVE_UNISTD_H
 4848 #include <unistd.h>
 4949 #endif
 5050 #include <fcntl.h>
 5151 
 5252 #include "compat.h"
 5353 #include "cpp.h"
 5454 #include "cpy.h"
 5555 
 5656 static void cvtdig(int rad);
 5757 static int charcon(usch *);
 5858 static void elsestmt(void);
 5959 static void ifdefstmt(void);
 6060 static void ifndefstmt(void);
 6161 static void endifstmt(void);
 6262 static void ifstmt(void);
 6363 static void cpperror(void);
 6464 static void cppwarning(void);
 6565 static void undefstmt(void);
 6666 static void pragmastmt(void);
 6767 static void elifstmt(void);
 6868 
 6969 #define PUTCH(ch) if (!flslvl) putch(ch)
 7070 /* protection against recursion in #include */
 7171 #define MAX_INCLEVEL    100
 7272 static int inclevel;
 7373 
 7474 usch yytext[CPPBUF];
 7575 
 7676 struct includ *ifiles;
 7777 
 7878 /* some common special combos for init */
 7979 #define C_NL    (C_SPEC|C_WSNL)
 8080 #define C_DX    (C_SPEC|C_ID|C_DIGIT|C_HEX)
 8181 #define C_I     (C_SPEC|C_ID|C_ID0)
 8282 #define C_IP    (C_SPEC|C_ID|C_ID0|C_EP)
 8383 #define C_IX    (C_SPEC|C_ID|C_ID0|C_HEX)
 8484 #define C_IXE   (C_SPEC|C_ID|C_ID0|C_HEX|C_EP)
 8585 
 8686 usch spechr[256] = {
 8787         0,      0,      0,      0,      C_SPECC_SPEC0,      0,
 8888         0,      C_WSNLC_NL,   0,      0,      C_WSNL0,      0,
 8989         0,      0,      0,      0,      0,      0,      0,      0,
 9090         0,      0,      0,      0,      0,      0,      0,      0,
 9191 
 9292         C_WSNLC_2,    C_SPEC0,      0,      0,      C_2,    C_SPEC,
 9393         0,      0,      0,      C_2,    0,      C_2,    0,      C_SPEC|C_2,
 9494         C_DX,   C_DX,   C_DX,   C_DX,   C_DX,   C_DX,   C_DX,   C_DX,
 9595         C_DX,   C_DX,   0,      0,      C_2,    C_2,    C_2,    0,
 9696 
 9797         0,      C_IX,   C_IX,   C_IX,   C_IX,   C_IXE,  C_IX,   C_I,
 9898         C_I,    C_I,    C_I,    C_I,    C_I,    C_I,    C_I,    C_I,
 9999         C_IP,   C_I,    C_I,    C_I,    C_I,    C_I,    C_I,    C_I,
 100100         C_I,    C_I,    C_I,    0,      0,      0,      0,      C_I,
 101101 
 102102         0,      C_IX,   C_IX,   C_IX,   C_IX,   C_IXE,  C_IX,   C_I,
 103103         C_I,    C_I,    C_I,    C_I,    C_I,    C_I,    C_I,    C_I,
 104104         C_IP,   C_I,    C_I,    C_I,    C_I,    C_I,    C_I,    C_I,
 105105         C_I,    C_I,    C_I,    0,      C_2,    0,      0,      0,
 106106 };
 107107 
 108108 /*
 109109  * fill up the input buffer
 110110  */
 111111 static int
 112112 inpbuf(void)
 113113 {
 114114         int len;
 115115 
 116116         if (ifiles->infil == -1)
 117117                 return 0;
 118118         len = read(ifiles->infil, ifiles->buffer, CPPBUF);
 119119         if (len == -1)
 120120                 error("read error on file %s", ifiles->orgfn);
 121121         if (len > 0) {
 122122                 ifiles->buffer[len] = 0;
 123123                 ifiles->curptr = ifiles->buffer;
 124124                 ifiles->maxread = ifiles->buffer + len;
 125125         }
 126126         return len;
 127127 }
 128128 
 129129 /*
 130130  * return a raw character from the input stream
 131131  */
 132132 static inline int
 133133 inpch(void)
 134134 {
 135135 
 136136         do {
 137137                 if (ifiles->curptr < ifiles->maxread)
 138138                         return *ifiles->curptr++;
 139139         } while (inpbuf() > 0);
 140140 
 141141         return -1;
 142142 }
 143143 
 144144 /*
 145145  * push a character back to the input stream
 146146  */
 147147 static void
 148148 unch(int c)
 149149 {
 150150         if (c == -1)
 151151                 return;
 152152                 
 153153         ifiles->curptr--;
 154154         if (ifiles->curptr < ifiles->bbuf)
 155155                 error("pushback buffer full");
 156156         *ifiles->curptr = (usch)c;
 157157 }
 158158 
 159159 /*
 160160  * Check for (and convert) trigraphs.
 161161  */
 162162 static int
 163163 chktg(void)
 164164 {
 165165         int ch;
 166166 
 167167         if ((ch = inpch()) != '?') {
 168168                 unch(ch);
 169169                 return 0;
 170170         }
 171171 
 172172         switch (ch = inpch()) {
 173173         case '='return '#';
 174174         case '('return '[';
 175175         case ')'return ']';
 176176         case '<'return '{';
 177177         case '>'return '}';
 178178         case '/'return '\\';
 179179         case '\'': return '^';
 180180         case '!'return '|';
 181181         case '-'return '~';
 182182         }
 183183 
 184184         unch(ch);
 185185         unch('?');
 186186         return 0;
 187187 }
 188188 
 189189 /*
 190190  * check for (and eat) end-of-line
 191191  */
 192192 static int
 193193 chkeol(void)
 194194 {
 195195         int ch;
 196196 
 197197         ch = inpch();
 198198         if (ch == '\r') {
 199199                 ch = inpch();
 200200                 if (ch == '\n')
 201201                         return '\n';
 202202 
 203203                 unch(ch);
 204204                 unch('\r');
 205205                 return 0;
 206206         }
 207207         if (ch == '\n')
 208208                 return '\n';
 209209 
 210210         unch(ch);
 211211         return 0;
 212212 }
 213213 
 214214 /*
 215215  * return next char, after converting trigraphs and
 216216  * skipping escaped line endings
 217217  */
 218218 static inline int
 219219 inch(void)
 220220 {
 221221         int ch;
 222222 
 223223         for (;;) {
 224224                 ch = inpch();
 225225                 if (ch == '?' && (ch = chktg()) == 0)
 226226                         return '?';
 227227                 if (ch != '\\' || chkeol() == 0)
 228228                         return ch;
 229229                 ifiles->escln++;
 230230         }
 231231 }
 232232 
 233233 static void
 234234 eatcmnt(void)
 235235 {
 236236         int ch;
 237237 
 238238         if (Cflag) { PUTCH('/'); PUTCH('*'); }
 239239         for (;;) {
 240240                 ch = inch();
 241241                 if (ch == '\n') {
 242242                         ifiles->lineno++;
 243243                         putch('\n');
 244244                         continue;
 245245                 }
 246246                 if (ch == -1)
 247247                         break;
 248248                 if (ch == '*') {
 249249                         ch = inch();
 250250                         if (ch == '/') {
 251251                                 if (Cflag) {
 252252                                         PUTCH('*');
 253253                                         PUTCH('/');
 254254                                 } else
 255255                                         PUTCH(' ');
 256256                                 break;
 257257                         }
 258258                         unch(ch);
 259259                         ch = '*';
 260260                 }
 261261                 if (Cflag) PUTCH(ch);
 262262         }
 263263 }
 264264 
 265265 /*
 266266  * Scan quickly the input file searching for:
 267267  *      - '#' directives
 268268  *      - keywords (if not flslvl)
 269269  *      - comments
 270270  *
 271271  *      Handle strings, numbers and trigraphs with care.
 272272  *      Only data from pp files are scanned here, never any rescans.
 273273  *      TODO: Only print out strings before calling other functions.
 274274  */
 275275 static void
 276276 fastscan(void)
 277277 {
 278278         struct symtab *nl;
 279279         int ch, i;
 280280         usch *cp;
 281281 
 282282         goto run;
 283283         for (;;) {
 284284                 ch = inch();
 285285 xloop:          if (ch == -1)
 286286                         return;
 287287 #ifdef PCC_DEBUG
 288288                 if (dflag>1)
 289289                         printf("fastscan ch %d (%c)\n", ch, ch > 31 ? ch : '@');
 290290 #endif
 291291                 if ((spechr[ch] & C_SPEC) == 0) {
 292292                         PUTCH(ch);
 293293                         continue;
 294294                 }
 295295                 switch (ch) {
 296296                 case EBLOCK:
 297297                 case WARN:
 298298                 case CONC:
 299299                         error("bad char passed");
 300300                         break;
 301301 
 302302                 case '/': /* Comments */
 303303                         if ((ch = inch()) == '/') {
 304304 cppcmt:                         if (Cflag) { PUTCH(ch); } else { PUTCH(' '); }
 305305                                 do {
 306306                                         if (Cflag) PUTCH(ch);
 307307                                         ch = inch();
 308308                                 } while (ch != -1 && ch != '\n');
 309309                                 goto xloop;
 310310                         } else if (ch == '*') {
 311311                                 eatcmnt();
 312312                         } else {
 313313                                 PUTCH('/');
 314314                                 goto xloop;
 315315                         }
 316316                         break;
 317317 
 318318                 case '\n': /* newlines, for pp directives */
 319319                         i = ifiles->escln + 1;
 320320                         ifiles->lineno += i;
 321321                         ifiles->escln = 0;
 322322                         while (i-- > 0)
 323323                                 putch('\n');
 324324 run:                    for(;;) {
 325325                                 ch = inch();
 326326                                 if (ch == '/') {
 327327                                         ch = inch();
 328328                                         if (ch == '/')
 329329                                                 goto cppcmt;
 330330                                         if (ch == '*') {
 331331                                                 eatcmnt();
 332332                                                 continue;
 333333                                         }
 334334                                         unch(ch);
 335335                                         ch = '/';
 336336                                 }
 337337                                 if (ch != ' ' && ch != '\t')
 338338                                         break;
 339339                                 PUTCH(ch);
 340340                         }
 341341                         if (ch == '#') {
 342342                                 ppdir();
 343343                                 continue;
 344344                         } else if (ch == '%') {
 345345                                 ch = inch();
 346346                                 if (ch == ':') {
 347347                                         ppdir();
 348348                                         continue;
 349349                                 }
 350350                                 unch(ch);
 351351                                 ch = '%';
 352352                         }
 353353                         goto xloop;
 354354 
 355355                 case '\"': /* strings */
 356356 str:                    PUTCH(ch);
 357357                         while ((ch = inch()) != '\"') {
 358358                                 if (ch == '\\') {
 359359                                         PUTCH('\\');
 360360                                         ch = inch();
 361361                                 }
 362362                                 if (ch == '\n')
 363363                                         goto xloop;
 364364                                 if (ch == -1)
 365365                                         return;
 366366                                 PUTCH(ch);
 367367                         }
 368368                         PUTCH(ch);
 369369                         break;
 370370 
 371371                 case '.'/* for pp-number */
 372372                         PUTCH(ch);
 373373                         ch = inch();
 374374                         if (ch < '0' || ch > '9')
 375375                                 goto xloop;
 376376 
 377377                         /* FALLTHROUGH */
 378378                 case '0': case '1': case '2': case '3': case '4':
 379379                 case '5': case '6': case '7': case '8': case '9':
 380380                         do {
 381381 nxp:                            PUTCH(ch);
 382382                                 ch = inch();
 383383                                 if (ch == -1)
 384384                                         return;
 385385                                 if (spechr[ch] & C_EP) {
 386386                                         PUTCH(ch);
 387387                                         ch = inch();
 388388                                         if (ch == '-' || ch == '+')
 389389                                                 goto nxp;
 390390                                         if (ch == -1)
 391391                                                 return;
 392392                                 }
 393393                         } while ((spechr[ch] & C_ID) || (ch == '.'));
 394394                         goto xloop;
 395395 
 396396                 case '\'': /* character literal */
 397397 con:                    PUTCH(ch);
 398398                         if (tflag)
 399399                                 break; /* character constants ignored */
 400400                         while ((ch = inch()) != '\'') {
 401401                                 if (ch == '\\') {
 402402                                         PUTCH('\\');
 403403                                         ch = inch();
 404404                                 }
 405405                                 if (ch == '\n')
 406406                                         goto xloop;
 407407                                 if (ch == -1)
 408408                                         return;
 409409                                 PUTCH(ch);
 410410                         }
 411411                         PUTCH(ch);
 412412                         break;
 413413 
 414414                 case 'L':
 415415                         ch = inch();
 416416                         if (ch == '\"') {
 417417                                 PUTCH('L');
 418418                                 goto str;
 419419                         }
 420420                         if (ch == '\'') {
 421421                                 PUTCH('L');
 422422                                 goto con;
 423423                         }
 424424                         unch(ch);
 425425                         ch = 'L';
 426426 
 427427                         /* FALLTHROUGH */
 428428                 default:
 429429 #ifdef PCC_DEBUG
 430430                         if ((spechr[ch] & C_ID) == 0)
 431431                                 error("fastscan");
 432432 #endif
 433433                         i = 0;
 434434                         do {
 435435                                 yytext[i++] = (usch)ch;
 436436                                 ch = inch();
 437437                         } while (ch != -1 && (spechr[ch] & C_ID));
 438438 
 439439                         if (flslvl)
 440440                                 goto xloop;
 441441 
 442442                         yytext[i] = 0;
 443443                         unch(ch);
 444444 
 445445                         cp = stringbuf;
 446446                         if ((nl = lookup(yytext, FIND)) && kfind(nl)) {
 447447                                 putstr(stringbuf);
 448448                         } else
 449449                                 putstr(yytext);
 450450                         stringbuf = cp;
 451451 
 452452                         break;
 453453                 }
 454454         }
 455455 }
 456456 
 457457 int
 458458 sloscan(void)
 459459 {
 460460         int ch;
 461461         int yyp;
 462462 
 463463 zagain:
 464464         yyp = 0;
 465465         ch = inch();
 466466         yytext[yyp++] = (usch)ch;
 467467         switch (ch) {
 468468         case -1:
 469469                 return 0;
 470470         case '\n':
 471471                 /* sloscan() never passes \n, that's up to fastscan() */
 472472                 unch(ch);
 473473                 yytext[yyp] = 0;
 474474                 return ch;
 475475 
 476476         case '\r': /* Ignore CR's */
 477477                 yyp = 0;
 478478                 break;
 479479 
 480480         case '0': case '1': case '2': case '3': case '4': case '5':
 481481         case '6': case '7': case '8': case '9':
 482482                 /* readin a "pp-number" */
 483483 ppnum:          for (;;) {
 484484                         ch = inch();
 485485                         if (ch == -1)
 486486                                 break;
 487487                         if (spechr[ch] & C_EP) {
 488488                                 yytext[yyp++] = (usch)ch;
 489489                                 ch = inch();
 490490                                 if (ch == '-' || ch == '+') {
 491491                                         yytext[yyp++] = (usch)ch;
 492492                                 } else
 493493                                         unch(ch);
 494494                                 continue;
 495495                         }
 496496                         if ((spechr[ch] & C_ID) || ch == '.') {
 497497                                 yytext[yyp++] = (usch)ch;
 498498                                 continue;
 499499                         }
 500500                         break;
 501501                 }
 502502                 unch(ch);
 503503                 yytext[yyp] = 0;
 504504 
 505505                 return NUMBER;
 506506 
 507507         case '\'':
 508508 chlit:          
 509509                 for (;;) {
 510510                         if ((ch = inch()) == '\\') {
 511511                                 yytext[yyp++] = (usch)ch;
 512512                                 yytext[yyp++] = (usch)inch();
 513513                                 continue;
 514514                         } else if (ch == -1 || ch == '\n') {
 515515                                 /* not a constant */
 516516                                 while (yyp > 1)
 517517                                         unch(yytext[--yyp]);
 518518                                 ch = '\'';
 519519                                 goto any;
 520520                         } else
 521521                                 yytext[yyp++] = (usch)ch;
 522522                         if (ch == '\'')
 523523                                 break;
 524524                 }
 525525                 yytext[yyp] = 0;
 526526 
 527527                 return NUMBER;
 528528 
 529529         case ' ':
 530530         case '\t':
 531531                 while ((ch = inch()) == ' ' || ch == '\t')
 532532                         yytext[yyp++] = (usch)ch;
 533533                 unch(ch);
 534534                 yytext[yyp] = 0;
 535535                 return WSPACE;
 536536 
 537537         case '/':
 538538                 if ((ch = inch()) == '/') {
 539539                         do {
 540540                                 yytext[yyp++] = (usch)ch;
 541541                                 ch = inch();
 542542                         } while (ch != -1 && ch != '\n');
 543543                         yytext[yyp] = 0;
 544544                         unch(ch);
 545545                         goto zagain;
 546546                 } else if (ch == '*') {
 547547                         int c, wrn;
 548548                         extern int readmac;
 549549 
 550550                         if (Cflag && !flslvl && readmac) {
 551551                                 unch(ch);
 552552                                 yytext[yyp] = 0;
 553553                                 return CMNT;
 554554                         }
 555555 
 556556                         wrn = 0;
 557557                 more:   while ((c = inch()) != '*') {
 558558                                 if (c == -1)
 559559                                         return 0;       
 560560                                 if (c == '\n')
 561561                                         putch(c), ifiles->lineno++;
 562562                                 else if (c == EBLOCK) {
 563563                                         (void)inch();
 564564                                         (void)inch();
 565565                                 } else if (c == WARN)
 566566                                         wrn = 1;
 567567                         }
 568568                         if ((c = inch()) == -1)
 569569                                 return 0;
 570570                         if (c != '/') {
 571571                                 unch(c);
 572572                                 goto more;
 573573                         }
 574574                         if (!tflag && !Cflag && !flslvl)
 575575                                 unch(' ');
 576576                         if (wrn)
 577577                                 unch(WARN);
 578578                         goto zagain;
 579579                 }
 580580                 unch(ch);
 581581                 ch = '/';
 582582                 goto any;
 583583 
 584584         case '.':
 585585                 if ((ch = inch()) == -1)
 586586                         return 0;
 587587                 if ((spechr[ch] & C_DIGIT)) {
 588588                         yytext[yyp++] = (usch)ch;
 589589                         goto ppnum;
 590590                 } else {
 591591                         unch(ch);
 592592                         ch = '.';
 593593                 }
 594594                 goto any;
 595595 
 596596         case '\"':
 597597                 if (tflag && defining)
 598598                         goto any;
 599599         strng:
 600600                 for (;;) {
 601601                         if ((ch = inch()) == '\\') {
 602602                                 yytext[yyp++] = (usch)ch;
 603603                                 yytext[yyp++] = (usch)inch();
 604604                                 continue;
 605605                         } else if (ch == -1) {
 606606                                 break;
 607607                         } else
 608608                                 yytext[yyp++] = (usch)ch;
 609609                         if (ch == '\"')
 610610                                 break;
 611611                 }
 612612                 yytext[yyp] = 0;
 613613                 return STRING;
 614614 
 615615         case 'L':
 616616                 if ((ch = inch()) == '\"' && !tflag) {
 617617                         yytext[yyp++] = (usch)ch;
 618618                         goto strng;
 619619                 } else if (ch == '\'' && !tflag) {
 620620                         yytext[yyp++] = (usch)ch;
 621621                         goto chlit;
 622622                 }
 623623                 unch(ch);
 624624                 /* FALLTHROUGH */
 625625 
 626626         /* Yetch, all identifiers */
 627627         case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
 628628         case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
 629629         case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
 630630         case 's': case 't': case 'u': case 'v': case 'w': case 'x':
 631631         case 'y': case 'z':
 632632         case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
 633633         case 'G': case 'H': case 'I': case 'J': case 'K':
 634634         case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
 635635         case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
 636636         case 'Y': case 'Z':
 637637         case '_': /* {L}({L}|{D})* */
 638638 
 639639                 /* Special hacks */
 640640                 for (;;) { /* get chars */
 641641                         if ((ch = inch()) == -1)
 642642                                 break;
 643643                         if ((spechr[ch] & C_ID)) {
 644644                                 yytext[yyp++] = (usch)ch;
 645645                         } else {
 646646                                 unch(ch);
 647647                                 break;
 648648                         }
 649649                 }
 650650                 yytext[yyp] = 0; /* need already string */
 651651                 /* end special hacks */
 652652 
 653653                 return IDENT;
 654654         default:
 655655         any:
 656656                 yytext[yyp] = 0;
 657657                 return yytext[0];
 658658 
 659659         } /* endcase */
 660660         goto zagain;
 661661 }
 662662 
 663663 int
 664664 yylex(void)
 665665 {
 666666         static int ifdef, noex;
 667667         struct symtab *nl;
 668668         int ch, c2;
 669669 
 670670         while ((ch = sloscan()) == WSPACE)
 671671                 ;
 672672         if (ch < 128 && (spechr[ch] & C_2))
 673673                 c2 = inch();
 674674         else
 675675                 c2 = 0;
 676676 
 677677         switch (ch) {
 678678         case '=':
 679679                 if (c2 == '=') return EQ;
 680680                 break;
 681681         case '!':
 682682                 if (c2 == '=') return NE;
 683683                 break;
 684684         case '|':
 685685                 if (c2 == '|') return OROR;
 686686                 break;
 687687         case '&':
 688688                 if (c2 == '&') return ANDAND;
 689689                 break;
 690690         case '<':
 691691                 if (c2 == '<') return LS;
 692692                 if (c2 == '=') return LE;
 693693                 break;
 694694         case '>':
 695695                 if (c2 == '>') return RS;
 696696                 if (c2 == '=') return GE;
 697697                 break;
 698698         case '+':
 699699         case '-':
 700700                 if (ch == c2)
 701701                         error("invalid preprocessor operator %c%c", ch, c2);
 702702                 break;
 703703 
 704704         case '/':
 705705                 if (Cflag == 0 || c2 != '*')
 706706                         break;
 707707                 /* Found comment that need to be skipped */
 708708                 for (;;) {
 709709                         ch = inch();
 710710                 c1:     if (ch != '*')
 711711                                 continue;
 712712                         if ((ch = inch()) == '/')
 713713                                 break;
 714714                         goto c1;
 715715                 }
 716716                 return yylex();
 717717 
 718718         case NUMBER:
 719719                 if (yytext[0] == '\'') {
 720720                         yylval.node.op = NUMBER;
 721721                         yylval.node.nd_val = charcon(yytext);
 722722                 } else
 723723                         cvtdig(yytext[0] != '0' ? 10 :
 724724                             yytext[1] == 'x' || yytext[1] == 'X' ? 16 : 8);
 725725                 return NUMBER;
 726726 
 727727         case IDENT:
 728728                 if (strcmp((char *)yytext, "defined") == 0) {
 729729                         ifdef = 1;
 730730                         return DEFINED;
 731731                 }
 732732                 nl = lookup(yytext, FIND);
 733733                 if (ifdef) {
 734734                         yylval.node.nd_val = nl != NULL;
 735735                         ifdef = 0;
 736736                 } else if (nl && noex == 0) {
 737737                         usch *och = stringbuf;
 738738                         int i;
 739739 
 740740                         i = kfind(nl);
 741741                         unch(WARN);
 742742                         if (i)
 743743                                 unpstr(stringbuf);
 744744                         else
 745745                                 unpstr(nl->namep);
 746746                         stringbuf = och;
 747747                         noex = 1;
 748748                         return yylex();
 749749                 } else {
 750750                         yylval.node.nd_val = 0;
 751751                 }
 752752                 yylval.node.op = NUMBER;
 753753                 return NUMBER;
 754754         case WARN:
 755755                 noex = 0;
 756756                 /* FALLTHROUGH */
 757757         case PHOLD:
 758758                 return yylex();
 759759         default:
 760760                 return ch;
 761761         }
 762762         unch(c2);
 763763         return ch;
 764764 }
 765765 
 766766 /*
 767767  * Let the command-line args be faked defines at beginning of file.
 768768  */
 769769 static void
 770770 prinit(struct initar *it, struct includ *ic)
 771771 {
 772772         const char *pre, *post;
 773773         char *a;
 774774 
 775775         if (it->next)
 776776                 prinit(it->next, ic);
 777777         pre = post = NULL; /* XXX gcc */
 778778         switch (it->type) {
 779779         case 'D':
 780780                 pre = "#define ";
 781781                 if ((a = strchr(it->str, '=')) != NULL) {
 782782                         *a = ' ';
 783783                         post = "\n";
 784784                 } else
 785785                         post = " 1\n";
 786786                 break;
 787787         case 'U':
 788788                 pre = "#undef ";
 789789                 post = "\n";
 790790                 break;
 791791         case 'i':
 792792                 pre = "#include \"";
 793793                 post = "\"\n";
 794794                 break;
 795795         default:
 796796                 error("prinit");
 797797         }
 798798         strlcat((char *)ic->buffer, pre, CPPBUF+1);
 799799         strlcat((char *)ic->buffer, it->str, CPPBUF+1);
 800800         if (strlcat((char *)ic->buffer, post, CPPBUF+1) >= CPPBUF+1)
 801801                 error("line exceeds buffer size");
 802802 
 803803         ic->lineno--;
 804804         while (*ic->maxread)
 805805                 ic->maxread++;
 806806 }
 807807 
 808808 /*
 809809  * A new file included.
 810810  * If ifiles == NULL, this is the first file and already opened (stdin).
 811811  * Return 0 on success, -1 if file to be included is not found.
 812812  */
 813813 int
 814814 pushfile(const usch *file, const usch *fn, int idx, void *incs)
 815815 {
 816816         extern struct initar *initar;
 817817         struct includ ibuf;
 818818         struct includ *ic;
 819819         int otrulvl;
 820820 
 821821         ic = &ibuf;
 822822         ic->next = ifiles;
 823823 
 824824         if (file != NULL) {
 825825                 if ((ic->infil = open((const char *)file, O_RDONLY)) < 0)
 826826                         return -1;
 827827                 ic->orgfn = ic->fname = file;
 828828                 if (++inclevel > MAX_INCLEVEL)
 829829                         error("limit for nested includes exceeded");
 830830         } else {
 831831                 ic->infil = 0;
 832832                 ic->orgfn = ic->fname = (const usch *)"<stdin>";
 833833         }
 834834 #ifndef BUF_STACK
 835835         ic->bbuf = malloc(BBUFSZ);
 836836 #endif
 837837         ic->buffer = ic->bbuf+NAMEMAX;
 838838         ic->curptr = ic->buffer;
 839839         ifiles = ic;
 840840         ic->lineno = 1;
 841841         ic->escln = 0;
 842842         ic->maxread = ic->curptr;
 843843         ic->idx = idx;
 844844         ic->incs = incs;
 845845         ic->fn = fn;
 846846         prtline();
 847847         if (initar) {
 848848                 int oin = ic->infil;
 849849                 ic->infil = -1;
 850850                 *ic->maxread = 0;
 851851                 prinit(initar, ic);
 852852                 initar = NULL;
 853853                 if (dMflag)
 854854                         xwrite(ofd, ic->buffer, strlen((char *)ic->buffer));
 855855                 fastscan();
 856856                 prtline();
 857857                 ic->infil = oin;
 858858         }
 859859 
 860860         otrulvl = trulvl;
 861861 
 862862         fastscan();
 863863 
 864864         if (otrulvl != trulvl || flslvl)
 865865                 error("unterminated conditional");
 866866 
 867867 #ifndef BUF_STACK
 868868         free(ic->bbuf);
 869869 #endif
 870870         ifiles = ic->next;
 871871         close(ic->infil);
 872872         inclevel--;
 873873         return 0;
 874874 }
 875875 
 876876 /*
 877877  * Print current position to output file.
 878878  */
 879879 void
 880880 prtline(void)
 881881 {
 882882         usch *sb = stringbuf;
 883883 
 884884         if (Mflag) {
 885885                 if (dMflag)
 886886                         return; /* no output */
 887887                 if (ifiles->lineno == 1) {
 888888                         sheap("%s: %s\n", Mfile, ifiles->fname);
 889889                         if (MPflag &&
 890890                             strcmp((const char *)ifiles->fname, (char *)MPfile))
 891891                                 sheap("%s:\n", ifiles->fname);
 892892                         xwrite(ofd, sb, stringbuf - sb);
 893893                 }
 894894         } else if (!Pflag) {
 895895                 sheap("\n# %d \"%s\"", ifiles->lineno, ifiles->fname);
 896896                 if (ifiles->idx == SYSINC)
 897897                         sheap(" 3");
 898898                 sheap("\n");
 899899                 putstr(sb);
 900900         }
 901901         stringbuf = sb;
 902902 }
 903903 
 904904 void
 905905 cunput(int c)
 906906 {
 907907 #ifdef PCC_DEBUG
 908908 //      if (dflag)printf(": '%c'(%d)\n", c > 31 ? c : ' ', c);
 909909 #endif
 910910         unch(c);
 911911 }
 912912 
 913913 static int
 914914 dig2num(int c)
 915915 {
 916916         if (c >= 'a')
 917917                 c = c - 'a' + 10;
 918918         else if (c >= 'A')
 919919                 c = c - 'A' + 10;
 920920         else
 921921                 c = c - '0';
 922922         return c;
 923923 }
 924924 
 925925 /*
 926926  * Convert string numbers to unsigned long long and check overflow.
 927927  */
 928928 static void
 929929 cvtdig(int rad)
 930930 {
 931931         unsigned long long rv = 0;
 932932         unsigned long long rv2 = 0;
 933933         usch *y = yytext;
 934934         int c;
 935935 
 936936         c = *y++;
 937937         if (rad == 16)
 938938                 y++;
 939939         while ((spechr[c] & C_HEX)) {
 940940                 rv = rv * rad + dig2num(c);
 941941                 /* check overflow */
 942942                 if (rv / rad < rv2)
 943943                         error("constant \"%s\" is out of range", yytext);
 944944                 rv2 = rv;
 945945                 c = *y++;
 946946         }
 947947         y--;
 948948         while (*y == 'l' || *y == 'L')
 949949                 y++;
 950950         yylval.node.op = *y == 'u' || *y == 'U' ? UNUMBER : NUMBER;
 951951         yylval.node.nd_uval = rv;
 952952         if ((rad == 8 || rad == 16) && yylval.node.nd_val < 0)
 953953                 yylval.node.op = UNUMBER;
 954954         if (yylval.node.op == NUMBER && yylval.node.nd_val < 0)
 955955                 /* too large for signed, see 6.4.4.1 */
 956956                 error("constant \"%s\" is out of range", yytext);
 957957 }
 958958 
 959959 static int
 960960 charcon(usch *p)
 961961 {
 962962         int val, c;
 963963 
 964964         p++; /* skip first ' */
 965965         val = 0;
 966966         if (*p++ == '\\') {
 967967                 switch (*p++) {
 968968                 case 'a': val = '\a'; break;
 969969                 case 'b': val = '\b'; break;
 970970                 case 'f': val = '\f'; break;
 971971                 case 'n': val = '\n'; break;
 972972                 case 'r': val = '\r'; break;
 973973                 case 't': val = '\t'; break;
 974974                 case 'v': val = '\v'; break;
 975975                 case '\"': val = '\"'; break;
 976976                 case '\'': val = '\''; break;
 977977                 case '\\': val = '\\'; break;
 978978                 case 'x':
 979979                         while ((spechr[c = *p] & C_HEX)) {
 980980                                 val = val * 16 + dig2num(c);
 981981                                 p++;
 982982                         }
 983983                         break;
 984984                 case '0': case '1': case '2': case '3': case '4':
 985985                 case '5': case '6': case '7':
 986986                         p--;
 987987                         while ((spechr[c = *p] & C_DIGIT)) {
 988988                                 val = val * 8 + (c - '0');
 989989                                 p++;
 990990                         }
 991991                         break;
 992992                 default: val = p[-1];
 993993                 }
 994994 
 995995         } else
 996996                 val = p[-1];
 997997         return val;
 998998 }
 999999 
 10001000 static void
 10011001 chknl(int ignore)
 10021002 {
 10031003         int t;
 10041004 
 10051005         while ((t = sloscan()) == WSPACE)
 10061006                 ;
 10071007         if (t != '\n') {
 10081008                 if (t) {
 10091009                         if (ignore) {
 10101010                                 warning("newline expected, got \"%s\"", yytext);
 10111011                                 /* ignore rest of line */
 10121012                                 while ((t = sloscan()) && t != '\n')
 10131013                                         ;
 10141014                         }
 10151015                         else
 10161016                                 error("newline expected, got \"%s\"", yytext);
 10171017                 } else {
 10181018                         if (ignore)
 10191019                                 warning("no newline at end of file");
 10201020                         else
 10211021                                 error("no newline at end of file");
 10221022                 }
 10231023         }
 10241024 }
 10251025 
 10261026 static void
 10271027 elsestmt(void)
 10281028 {
 10291029         if (flslvl) {
 10301030                 if (elflvl > trulvl)
 10311031                         ;
 10321032                 else if (--flslvl!=0)
 10331033                         flslvl++;
 10341034                 else
 10351035                         trulvl++;
 10361036         } else if (trulvl) {
 10371037                 flslvl++;
 10381038                 trulvl--;
 10391039         } else
 10401040                 error("#else in non-conditional section");
 10411041         if (elslvl==trulvl+flslvl)
 10421042                 error("too many #else");
 10431043         elslvl=trulvl+flslvl;
 10441044         chknl(1);
 10451045 }
 10461046 
 10471047 static void
 10481048 skpln(void)
 10491049 {
 10501050         int ch;
 10511051 
 10521052         /* just ignore the rest of the line */
 10531053         while ((ch = inch()) != -1) {
 10541054                 if (ch == '\n') {
 10551055                         unch('\n');
 10561056                         break;
 10571057                 }
 10581058         }
 10591059 }
 10601060 
 10611061 static void
 10621062 ifdefstmt(void)         
 10631063 {
 10641064         int t;
 10651065 
 10661066         if (flslvl) {
 10671067                 flslvl++;
 10681068                 skpln();
 10691069                 return;
 10701070         }
 10711071         do
 10721072                 t = sloscan();
 10731073         while (t == WSPACE);
 10741074         if (t != IDENT)
 10751075                 error("bad #ifdef");
 10761076         if (lookup(yytext, FIND) == NULL)
 10771077                 flslvl++;
 10781078         else
 10791079                 trulvl++;
 10801080         chknl(0);
 10811081 }
 10821082 
 10831083 static void
 10841084 ifndefstmt(void)         
 10851085 {
 10861086         int t;
 10871087 
 10881088         if (flslvl) {
 10891089                 flslvl++;
 10901090                 skpln();
 10911091                 return;
 10921092         }
 10931093         do
 10941094                 t = sloscan();
 10951095         while (t == WSPACE);
 10961096         if (t != IDENT)
 10971097                 error("bad #ifndef");
 10981098         if (lookup(yytext, FIND) != NULL)
 10991099                 flslvl++;
 11001100         else
 11011101                 trulvl++;
 11021102         chknl(0);
 11031103 }
 11041104 
 11051105 static void
 11061106 endifstmt(void)         
 11071107 {
 11081108         if (flslvl)
 11091109                 flslvl--;
 11101110         else if (trulvl)
 11111111                 trulvl--;
 11121112         else
 11131113                 error("#endif in non-conditional section");
 11141114         if (flslvl == 0)
 11151115                 elflvl = 0;
 11161116         elslvl = 0;
 11171117         chknl(1);
 11181118 }
 11191119 
 11201120 static void
 11211121 ifstmt(void)
 11221122 {
 11231123         if (flslvl || yyparse() == 0)
 11241124                 flslvl++;
 11251125         else
 11261126                 trulvl++;
 11271127 }
 11281128 
 11291129 static void
 11301130 elifstmt(void)
 11311131 {
 11321132         if (flslvl == 0)
 11331133                 elflvl = trulvl;
 11341134         if (flslvl) {
 11351135                 if (elflvl > trulvl)
 11361136                         ;
 11371137                 else if (--flslvl!=0)
 11381138                         flslvl++;
 11391139                 else if (yyparse())
 11401140                         trulvl++;
 11411141                 else
 11421142                         flslvl++;
<>1143 -                }
<_11441143         } else if (trulvl) {
 11451144                 flslvl++;
 11461145                 trulvl--;
 11471146         } else
 11481147                 error("#elif in non-conditional section");
 11491148 }
 11501149 
 11511150 /* save line into stringbuf */
 11521151 static usch *
 11531152 savln(void)
 11541153 {
 11551154         int c;
 11561155         usch *cp = stringbuf;
 11571156 
 11581157         while ((c = inch()) != -1) {
 11591158                 if (c == '\n') {
 11601159                         unch(c);
 11611160                         break;
 11621161                 }
 11631162                 savch(c);
 11641163         }
 11651164         savch(0);
 11661165 
 11671166         return cp;
 11681167 }
 11691168 
 11701169 static void
 11711170 cpperror(void)
 11721171 {
 11731172         usch *cp;
 11741173         int c;
 11751174 
 11761175         if (flslvl)
 11771176                 return;
 11781177         c = sloscan();
 11791178         if (c != WSPACE && c != '\n')
 11801179                 error("bad #error");
 11811180         cp = savln();
 11821181         error("#error %s", cp);
 11831182 }
 11841183 
 11851184 static void
 11861185 cppwarning(void)
 11871186 {
 11881187         usch *cp;
 11891188         int c;
 11901189 
 11911190         if (flslvl)
 11921191                 return;
 11931192         c = sloscan();
 11941193         if (c != WSPACE && c != '\n')
 11951194                 error("bad #warning");
 11961195         cp = savln();
 11971196         warning("#warning %s", cp);
 11981197         stringbuf = cp;
 11991198 }
 12001199 
 12011200 static void
 12021201 undefstmt(void)
 12031202 {
 12041203         struct symtab *np;
 12051204 
 12061205         if (flslvl)
 12071206                 return;
 12081207         if (sloscan() != WSPACE || sloscan() != IDENT)
 12091208                 error("bad #undef");
 12101209         if ((np = lookup(yytext, FIND)) != NULL)
 12111210                 np->value = 0;
 12121211         chknl(0);
 12131212 }
 12141213 
 12151214 static void
 12161215 pragmastmt(void)
 12171216 {
 12181217         usch *sb;
 12191218 
 12201219         if (flslvl)
 12211220                 return;
 12221221         if (sloscan() != WSPACE)
 12231222                 error("bad #pragma");
 12241223         sb = stringbuf;
 12251224         savstr((const usch *)"#pragma ");
 12261225         savln();
 12271226         putstr(sb);
 12281227         stringbuf = sb;
 12291228 }
 12301229 
 12311230 int
 12321231 cinput(void)
 12331232 {
 12341233         return inch();
 12351234 }
 12361235 
 12371236 static struct {
 12381237         const char *name;
 12391238         void (*fun)(void);
 12401239 } ppd[] = {
 12411240         { "ifndef", ifndefstmt },
 12421241         { "ifdef", ifdefstmt },
 12431242         { "if", ifstmt },
 12441243         { "include", include },
 12451244         { "else", elsestmt },
 12461245         { "endif", endifstmt },
 12471246         { "error", cpperror },
 12481247         { "warning", cppwarning },
 12491248         { "define", define },
 12501249         { "undef", undefstmt },
 12511250         { "line", line },
 12521251         { "pragma", pragmastmt },
 12531252         { "elif", elifstmt },
 12541253 #ifdef GCC_COMPAT
 12551254         { "include_next", include_next },
 12561255 #endif
 12571256 };
 12581257 #define NPPD    (int)(sizeof(ppd) / sizeof(ppd[0]))
 12591258 
 12601259 /*
 12611260  * Handle a preprocessor directive.
 12621261  */
 12631262 void
 12641263 ppdir(void)
 12651264 {
 12661265         char bp[20];
 12671266         int ch, i;
 12681267 
 12691268 redo:   while ((ch = inch()) == ' ' || ch == '\t')
 12701269                 ;
 12711270         if (ch == '/') {
 12721271                 if ((ch = inch()) == '/') {
 12731272                         skpln();
 12741273                         return;
 12751274                 }
 12761275                 if (ch == '*') {
 12771276                         while ((ch = inch()) != -1) {
 12781277                                 if (ch == '*') {
 12791278                                         if ((ch = inch()) == '/')
 12801279                                                 goto redo;
 12811280                                         unch(ch);
 12821281                                 } else if (ch == '\n') {
 12831282                                         putch('\n');
 12841283                                         ifiles->lineno++;
 12851284                                 }
 12861285                         }
 12871286                 }
 12881287         }
 12891288         if (ch == '\n') { /* empty directive */
 12901289                 unch(ch);
 12911290                 return;
 12921291         }
 12931292         if (ch < 'a' || ch > 'z')
 12941293                 goto out; /* something else, ignore */
 12951294         i = 0;
 12961295         do {
 12971296                 bp[i++] = (usch)ch;
 12981297                 if (i == sizeof(bp)-1)
 12991298                         goto out; /* too long */
 13001299                 ch = inch();
 13011300         } while ((ch >= 'a' && ch <= 'z') || (ch == '_'));
 13021301         unch(ch);
 13031302         bp[i++] = 0;
 13041303 
 13051304         /* got keyword */
 13061305         for (i = 0; i < NPPD; i++) {
 13071306                 if (bp[0] == ppd[i].name[0] && strcmp(bp, ppd[i].name) == 0) {
 13081307                         (*ppd[i].fun)();
 13091308                         return;
 13101309                 }
 13111310         }
 13121311 
 13131312 out:    error("invalid preprocessor directive");
 13141313 }
FishEye: Open Source License registered to PCC.
Your maintenance has expired. You can renew your license at http://www.atlassian.com/fisheye/renew
Atlassian FishEye, CVS analysis. (Version:1.6.3 Build:build-336 2008-11-04) - Administration - Page generated 2014-09-21 10:10 +0200