Quick Search:

Mode

Context

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

Other Diffs

Ignore

Blank Lines Whitespace: Expand:

Diff

1.104
 
1.105
 
MAIN:plunky:20121129095214
 
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                                         warning("unterminated string literal");
 364364                                         goto xloop;
 365365                                 }
 366366                                 if (ch == -1)
 367367                                         return;
 368368                                 PUTCH(ch);
 369369                         }
 370370                         PUTCH(ch);
 371371                         break;
 372372 
 373373                 case '.'/* for pp-number */
 374374                         PUTCH(ch);
 375375                         ch = inch();
 376376                         if (ch < '0' || ch > '9')
 377377                                 goto xloop;
 378378 
 379379                         /* FALLTHROUGH */
 380380                 case '0': case '1': case '2': case '3': case '4':
 381381                 case '5': case '6': case '7': case '8': case '9':
 382382                         do {
 383383 nxp:                            PUTCH(ch);
 384384                                 ch = inch();
 385385                                 if (ch == -1)
 386386                                         return;
 387387                                 if (spechr[ch] & C_EP) {
 388388                                         PUTCH(ch);
 389389                                         ch = inch();
 390390                                         if (ch == '-' || ch == '+')
 391391                                                 goto nxp;
 392392                                         if (ch == -1)
 393393                                                 return;
 394394                                 }
 395395                         } while ((spechr[ch] & C_ID) || (ch == '.'));
 396396                         goto xloop;
 397397 
 398398                 case '\'': /* character constant */
 399399 con:                    PUTCH(ch);
 400400                         if (tflag)
 401401                                 break; /* character constants ignored */
 402402                         while ((ch = inch()) != '\'') {
 403403                                 if (ch == '\\') {
 404404                                         PUTCH('\\');
 405405                                         ch = inch();
 406406                                 }
 407407                                 if (ch == '\n') {
 408408                                         warning("unterminated character constant");
 409409                                         goto xloop;
 410410                                 }
 411411                                 if (ch == -1)
 412412                                         return;
 413413                                 PUTCH(ch);
 414414                         }
 415415                         PUTCH(ch);
 416416                         break;
 417417 
 418418                 case 'L':
 419419                         ch = inch();
 420420                         if (ch == '\"') {
 421421                                 PUTCH('L');
 422422                                 goto str;
 423423                         }
 424424                         if (ch == '\'') {
 425425                                 PUTCH('L');
 426426                                 goto con;
 427427                         }
 428428                         unch(ch);
 429429                         ch = 'L';
 430430 
 431431                         /* FALLTHROUGH */
 432432                 default:
 433433 #ifdef PCC_DEBUG
 434434                         if ((spechr[ch] & C_ID) == 0)
 435435                                 error("fastscan");
 436436 #endif
 437437                         i = 0;
 438438                         do {
 439439                                 yytext[i++] = (usch)ch;
 440440                                 ch = inch();
 441441                         } while (ch != -1 && (spechr[ch] & C_ID));
 442442 
 443443                         if (flslvl)
 444444                                 goto xloop;
 445445 
 446446                         yytext[i] = 0;
 447447                         unch(ch);
 448448 
 449449                         cp = stringbuf;
 450450                         if ((nl = lookup(yytext, FIND)) && kfind(nl)) {
 451451                                 putstr(stringbuf);
 452452                         } else
 453453                                 putstr(yytext);
 454454                         stringbuf = cp;
 455455 
 456456                         break;
 457457                 }
 458458         }
 459459 }
 460460 
 461461 int
 462462 sloscan(void)
 463463 {
 464464         int ch;
 465465         int yyp;
 466466 
 467467 zagain:
 468468         yyp = 0;
 469469         ch = inch();
 470470         yytext[yyp++] = (usch)ch;
 471471         switch (ch) {
 472472         case -1:
 473473                 return 0;
 474474         case '\n':
 475475                 /* sloscan() never passes \n, that's up to fastscan() */
 476476                 unch(ch);
 477477                 yytext[yyp] = 0;
 478478                 return ch;
 479479 
 480480         case '\r': /* Ignore CR's */
 481481                 yyp = 0;
 482482                 break;
 483483 
 484484         case '0': case '1': case '2': case '3': case '4': case '5':
 485485         case '6': case '7': case '8': case '9':
 486486                 /* readin a "pp-number" */
 487487 ppnum:          for (;;) {
 488488                         ch = inch();
 489489                         if (ch == -1)
 490490                                 break;
 491491                         if (spechr[ch] & C_EP) {
 492492                                 yytext[yyp++] = (usch)ch;
 493493                                 ch = inch();
 494494                                 if (ch == '-' || ch == '+') {
 495495                                         yytext[yyp++] = (usch)ch;
 496496                                 } else
 497497                                         unch(ch);
 498498                                 continue;
 499499                         }
 500500                         if ((spechr[ch] & C_ID) || ch == '.') {
 501501                                 yytext[yyp++] = (usch)ch;
 502502                                 continue;
 503503                         }
 504504                         break;
 505505                 }
 506506                 unch(ch);
 507507                 yytext[yyp] = 0;
 508508 
 509509                 return NUMBER;
 510510 
 511511         case '\'':
 512512 chlit:          
 513513                 for (;;) {
 514514                         if ((ch = inch()) == '\\') {
 515515                                 yytext[yyp++] = (usch)ch;
 516516                                 yytext[yyp++] = (usch)inch();
 517517                                 continue;
 518518                         } else if (ch == -1 || ch == '\n') {
 519519                                 /* not a constant */
 520520                                 while (yyp > 1)
 521521                                         unch(yytext[--yyp]);
 522522                                 ch = '\'';
 523523                                 goto any;
 524524                         } else
 525525                                 yytext[yyp++] = (usch)ch;
 526526                         if (ch == '\'')
 527527                                 break;
 528528                 }
 529529                 yytext[yyp] = 0;
 530530 
 531531                 return NUMBER;
 532532 
 533533         case ' ':
 534534         case '\t':
 535535                 while ((ch = inch()) == ' ' || ch == '\t')
 536536                         yytext[yyp++] = (usch)ch;
 537537                 unch(ch);
 538538                 yytext[yyp] = 0;
 539539                 return WSPACE;
 540540 
 541541         case '/':
 542542                 if ((ch = inch()) == '/') {
 543543                         do {
 544544                                 yytext[yyp++] = (usch)ch;
 545545                                 ch = inch();
 546546                         } while (ch != -1 && ch != '\n');
 547547                         yytext[yyp] = 0;
 548548                         unch(ch);
 549549                         goto zagain;
 550550                 } else if (ch == '*') {
 551551                         int c, wrn;
 552552                         extern int readmac;
 553553 
 554554                         if (Cflag && !flslvl && readmac) {
 555555                                 unch(ch);
 556556                                 yytext[yyp] = 0;
 557557                                 return CMNT;
 558558                         }
 559559 
 560560                         wrn = 0;
 561561                 more:   while ((c = inch()) != '*') {
 562562                                 if (c == -1)
 563563                                         return 0;       
 564564                                 if (c == '\n')
 565565                                         putch(c), ifiles->lineno++;
 566566                                 else if (c == EBLOCK) {
 567567                                         (void)inch();
 568568                                         (void)inch();
 569569                                 } else if (c == WARN)
 570570                                         wrn = 1;
 571571                         }
 572572                         if ((c = inch()) == -1)
 573573                                 return 0;
 574574                         if (c != '/') {
 575575                                 unch(c);
 576576                                 goto more;
 577577                         }
 578578                         if (!tflag && !Cflag && !flslvl)
 579579                                 unch(' ');
 580580                         if (wrn)
 581581                                 unch(WARN);
 582582                         goto zagain;
 583583                 }
 584584                 unch(ch);
 585585                 ch = '/';
 586586                 goto any;
 587587 
 588588         case '.':
 589589                 if ((ch = inch()) == -1)
 590590                         return 0;
 591591                 if ((spechr[ch] & C_DIGIT)) {
 592592                         yytext[yyp++] = (usch)ch;
 593593                         goto ppnum;
 594594                 } else {
 595595                         unch(ch);
 596596                         ch = '.';
 597597                 }
 598598                 goto any;
 599599 
 600600         case '\"':
 601601                 if (tflag && defining)
 602602                         goto any;
 603603         strng:
 604604                 for (;;) {
 605605                         if ((ch = inch()) == '\\') {
 606606                                 yytext[yyp++] = (usch)ch;
 607607                                 yytext[yyp++] = (usch)inch();
 608608                                 continue;
 609609                         } else if (ch == -1) {
 610610                                 break;
 611611                         } else
 612612                                 yytext[yyp++] = (usch)ch;
 613613                         if (ch == '\"')
 614614                                 break;
 615615                 }
 616616                 yytext[yyp] = 0;
 617617                 return STRING;
 618618 
 619619         case 'L':
 620620                 if ((ch = inch()) == '\"' && !tflag) {
 621621                         yytext[yyp++] = (usch)ch;
 622622                         goto strng;
 623623                 } else if (ch == '\'' && !tflag) {
 624624                         yytext[yyp++] = (usch)ch;
 625625                         goto chlit;
 626626                 }
 627627                 unch(ch);
 628628                 /* FALLTHROUGH */
 629629 
 630630         /* Yetch, all identifiers */
 631631         case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
 632632         case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
 633633         case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
 634634         case 's': case 't': case 'u': case 'v': case 'w': case 'x':
 635635         case 'y': case 'z':
 636636         case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
 637637         case 'G': case 'H': case 'I': case 'J': case 'K':
 638638         case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
 639639         case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
 640640         case 'Y': case 'Z':
 641641         case '_': /* {L}({L}|{D})* */
 642642 
 643643                 /* Special hacks */
 644644                 for (;;) { /* get chars */
 645645                         if ((ch = inch()) == -1)
 646646                                 break;
 647647                         if ((spechr[ch] & C_ID)) {
 648648                                 yytext[yyp++] = (usch)ch;
 649649                         } else {
 650650                                 unch(ch);
 651651                                 break;
 652652                         }
 653653                 }
 654654                 yytext[yyp] = 0; /* need already string */
 655655                 /* end special hacks */
 656656 
 657657                 return IDENT;
 658658         default:
 659659         any:
 660660                 yytext[yyp] = 0;
 661661                 return yytext[0];
 662662 
 663663         } /* endcase */
 664664         goto zagain;
 665665 }
 666666 
 667667 int
 668668 yylex(void)
 669669 {
 670670         static int ifdef, noex;
 671671         struct symtab *nl;
 672672         int ch, c2;
 673673 
 674674         while ((ch = sloscan()) == WSPACE)
 675675                 ;
 676676         if (ch < 128 && (spechr[ch] & C_2))
 677677                 c2 = inch();
 678678         else
 679679                 c2 = 0;
 680680 
 681681         switch (ch) {
 682682         case '=':
 683683                 if (c2 == '=') return EQ;
 684684                 break;
 685685         case '!':
 686686                 if (c2 == '=') return NE;
 687687                 break;
 688688         case '|':
 689689                 if (c2 == '|') return OROR;
 690690                 break;
 691691         case '&':
 692692                 if (c2 == '&') return ANDAND;
 693693                 break;
 694694         case '<':
 695695                 if (c2 == '<') return LS;
 696696                 if (c2 == '=') return LE;
 697697                 break;
 698698         case '>':
 699699                 if (c2 == '>') return RS;
 700700                 if (c2 == '=') return GE;
 701701                 break;
 702702         case '+':
 703703         case '-':
 704704                 if (ch == c2)
 705705                         error("invalid preprocessor operator %c%c", ch, c2);
 706706                 break;
 707707 
 708708         case '/':
 709709                 if (Cflag == 0 || c2 != '*')
 710710                         break;
 711711                 /* Found comment that need to be skipped */
 712712                 for (;;) {
 713713                         ch = inch();
 714714                 c1:     if (ch != '*')
 715715                                 continue;
 716716                         if ((ch = inch()) == '/')
 717717                                 break;
 718718                         goto c1;
 719719                 }
 720720                 return yylex();
 721721 
 722722         case NUMBER:
 723723                 if (yytext[0] == '\'') {
 724724                         yylval.node.op = NUMBER;
 725725                         yylval.node.nd_val = charcon(yytext);
 726726                 } else
 727727                         cvtdig(yytext[0] != '0' ? 10 :
 728728                             yytext[1] == 'x' || yytext[1] == 'X' ? 16 : 8);
 729729                 return NUMBER;
 730730 
 731731         case IDENT:
 732732                 if (strcmp((char *)yytext, "defined") == 0) {
 733733                         ifdef = 1;
 734734                         return DEFINED;
 735735                 }
 736736                 nl = lookup(yytext, FIND);
 737737                 if (ifdef) {
 738738                         yylval.node.nd_val = nl != NULL;
 739739                         ifdef = 0;
 740740                 } else if (nl && noex == 0) {
 741741                         usch *och = stringbuf;
 742742                         int i;
 743743 
 744744                         i = kfind(nl);
 745745                         unch(WARN);
 746746                         if (i)
 747747                                 unpstr(stringbuf);
 748748                         else
 749749                                 unpstr(nl->namep);
 750750                         stringbuf = och;
 751751                         noex = 1;
 752752                         return yylex();
 753753                 } else {
 754754                         yylval.node.nd_val = 0;
 755755                 }
 756756                 yylval.node.op = NUMBER;
 757757                 return NUMBER;
 758758         case WARN:
 759759                 noex = 0;
 760760                 /* FALLTHROUGH */
 761761         case PHOLD:
 762762                 return yylex();
 763763         default:
 764764                 return ch;
 765765         }
 766766         unch(c2);
 767767         return ch;
 768768 }
 769769 
 770770 /*
 771771  * Let the command-line args be faked defines at beginning of file.
 772772  */
 773773 static void
 774774 prinit(struct initar *it, struct includ *ic)
 775775 {
 776776         const char *pre, *post;
 777777         char *a;
 778778 
 779779         if (it->next)
 780780                 prinit(it->next, ic);
 781781         pre = post = NULL; /* XXX gcc */
 782782         switch (it->type) {
 783783         case 'D':
 784784                 pre = "#define ";
 785785                 if ((a = strchr(it->str, '=')) != NULL) {
 786786                         *a = ' ';
 787787                         post = "\n";
 788788                 } else
 789789                         post = " 1\n";
 790790                 break;
 791791         case 'U':
 792792                 pre = "#undef ";
 793793                 post = "\n";
 794794                 break;
 795795         case 'i':
 796796                 pre = "#include \"";
 797797                 post = "\"\n";
 798798                 break;
 799799         default:
 800800                 error("prinit");
 801801         }
 802802         strlcat((char *)ic->buffer, pre, CPPBUF+1);
 803803         strlcat((char *)ic->buffer, it->str, CPPBUF+1);
 804804         if (strlcat((char *)ic->buffer, post, CPPBUF+1) >= CPPBUF+1)
 805805                 error("line exceeds buffer size");
 806806 
 807807         ic->lineno--;
 808808         while (*ic->maxread)
 809809                 ic->maxread++;
 810810 }
 811811 
 812812 /*
 813813  * A new file included.
 814814  * If ifiles == NULL, this is the first file and already opened (stdin).
 815815  * Return 0 on success, -1 if file to be included is not found.
 816816  */
 817817 int
 818818 pushfile(const usch *file, const usch *fn, int idx, void *incs)
 819819 {
 820820         extern struct initar *initar;
 821821         struct includ ibuf;
 822822         struct includ *ic;
 823823         int otrulvl;
 824824 
 825825         ic = &ibuf;
 826826         ic->next = ifiles;
 827827 
 828828         if (file != NULL) {
 829829                 if ((ic->infil = open((const char *)file, O_RDONLY)) < 0)
 830830                         return -1;
 831831                 ic->orgfn = ic->fname = file;
 832832                 if (++inclevel > MAX_INCLEVEL)
 833833                         error("limit for nested includes exceeded");
 834834         } else {
 835835                 ic->infil = 0;
 836836                 ic->orgfn = ic->fname = (const usch *)"<stdin>";
 837837         }
 838838 #ifndef BUF_STACK
 839839         ic->bbuf = malloc(BBUFSZ);
 840840 #endif
 841841         ic->buffer = ic->bbuf+NAMEMAX;
 842842         ic->curptr = ic->buffer;
 843843         ifiles = ic;
 844844         ic->lineno = 1;
 845845         ic->escln = 0;
 846846         ic->maxread = ic->curptr;
 847847         ic->idx = idx;
 848848         ic->incs = incs;
 849849         ic->fn = fn;
 850850         prtline();
 851851         if (initar) {
 852852                 int oin = ic->infil;
 853853                 ic->infil = -1;
 854854                 *ic->maxread = 0;
 855855                 prinit(initar, ic);
 856856                 initar = NULL;
 857857                 if (dMflag)
 858858                         xwrite(ofd, ic->buffer, strlen((char *)ic->buffer));
 859859                 fastscan();
 860860                 prtline();
 861861                 ic->infil = oin;
 862862         }
 863863 
 864864         otrulvl = trulvl;
 865865 
 866866         fastscan();
 867867 
 868868         if (otrulvl != trulvl || flslvl)
 869869                 error("unterminated conditional");
 870870 
 871871 #ifndef BUF_STACK
 872872         free(ic->bbuf);
 873873 #endif
 874874         ifiles = ic->next;
 875875         close(ic->infil);
 876876         inclevel--;
 877877         return 0;
 878878 }
 879879 
 880880 /*
 881881  * Print current position to output file.
 882882  */
 883883 void
 884884 prtline(void)
 885885 {
 886886         usch *sb = stringbuf;
 887887 
 888888         if (Mflag) {
 889889                 if (dMflag)
 890890                         return; /* no output */
 891891                 if (ifiles->lineno == 1) {
 892892                         sheap("%s: %s\n", Mfile, ifiles->fname);
 893893                         if (MPflag &&
 894894                             strcmp((const char *)ifiles->fname, (char *)MPfile))
 895895                                 sheap("%s:\n", ifiles->fname);
 896896                         xwrite(ofd, sb, stringbuf - sb);
 897897                 }
 898898         } else if (!Pflag) {
 899899                 sheap("\n# %d \"%s\"", ifiles->lineno, ifiles->fname);
 900900                 if (ifiles->idx == SYSINC)
 901901                         sheap(" 3");
 902902                 sheap("\n");
 903903                 putstr(sb);
 904904         }
 905905         stringbuf = sb;
 906906 }
 907907 
 908908 void
 909909 cunput(int c)
 910910 {
 911911 #ifdef PCC_DEBUG
 912912 //      if (dflag)printf(": '%c'(%d)\n", c > 31 ? c : ' ', c);
 913913 #endif
 914914         unch(c);
 915915 }
 916916 
 917917 static int
 918918 dig2num(int c)
 919919 {
 920920         if (c >= 'a')
 921921                 c = c - 'a' + 10;
 922922         else if (c >= 'A')
 923923                 c = c - 'A' + 10;
 924924         else
 925925                 c = c - '0';
 926926         return c;
 927927 }
 928928 
 929929 /*
 930930  * Convert string numbers to unsigned long long and check overflow.
 931931  */
 932932 static void
 933933 cvtdig(int rad)
 934934 {
 935935         unsigned long long rv = 0;
 936936         unsigned long long rv2 = 0;
 937937         usch *y = yytext;
 938938         int c;
 939939 
 940940         c = *y++;
 941941         if (rad == 16)
 942942                 y++;
 943943         while ((spechr[c] & C_HEX)) {
 944944                 rv = rv * rad + dig2num(c);
 945945                 /* check overflow */
 946946                 if (rv / rad < rv2)
 947947                         error("constant \"%s\" is out of range", yytext);
 948948                 rv2 = rv;
 949949                 c = *y++;
 950950         }
 951951         y--;
 952952         while (*y == 'l' || *y == 'L')
 953953                 y++;
 954954         yylval.node.op = *y == 'u' || *y == 'U' ? UNUMBER : NUMBER;
 955955         yylval.node.nd_uval = rv;
 956956         if ((rad == 8 || rad == 16) && yylval.node.nd_val < 0)
 957957                 yylval.node.op = UNUMBER;
 958958         if (yylval.node.op == NUMBER && yylval.node.nd_val < 0)
 959959                 /* too large for signed, see 6.4.4.1 */
 960960                 error("constant \"%s\" is out of range", yytext);
 961961 }
 962962 
 963963 static int
 964964 charcon(usch *p)
 965965 {
 966966         int val, c;
 967967 
 968968         p++; /* skip first ' */
 969969         val = 0;
 970970         if (*p++ == '\\') {
 971971                 switch (*p++) {
 972972                 case 'a': val = '\a'; break;
 973973                 case 'b': val = '\b'; break;
 974974                 case 'f': val = '\f'; break;
 975975                 case 'n': val = '\n'; break;
 976976                 case 'r': val = '\r'; break;
 977977                 case 't': val = '\t'; break;
 978978                 case 'v': val = '\v'; break;
 979979                 case '\"': val = '\"'; break;
 980980                 case '\'': val = '\''; break;
 981981                 case '\\': val = '\\'; break;
 982982                 case 'x':
 983983                         while ((spechr[c = *p] & C_HEX)) {
 984984                                 val = val * 16 + dig2num(c);
 985985                                 p++;
 986986                         }
 987987                         break;
 988988                 case '0': case '1': case '2': case '3': case '4':
 989989                 case '5': case '6': case '7':
 990990                         p--;
 991991                         while ((spechr[c = *p] & C_DIGIT)) {
 992992                                 val = val * 8 + (c - '0');
 993993                                 p++;
 994994                         }
 995995                         break;
 996996                 default: val = p[-1];
 997997                 }
 998998 
 999999         } else
 10001000                 val = p[-1];
 10011001         return val;
 10021002 }
 10031003 
 10041004 static void
 10051005 chknl(int ignore)
 10061006 {
 10071007         int t;
 10081008 
 10091009         while ((t = sloscan()) == WSPACE)
 10101010                 ;
 10111011         if (t != '\n') {
 10121012                 if (t) {
 10131013                         if (ignore) {
 10141014                                 warning("newline expected, got \"%s\"", yytext);
 10151015                                 /* ignore rest of line */
 10161016                                 while ((t = sloscan()) && t != '\n')
 10171017                                         ;
 10181018                         }
 10191019                         else
 10201020                                 error("newline expected, got \"%s\"", yytext);
 10211021                 } else {
 10221022                         if (ignore)
 10231023                                 warning("no newline at end of file");
 10241024                         else
 10251025                                 error("no newline at end of file");
 10261026                 }
 10271027         }
 10281028 }
 10291029 
 10301030 static void
 10311031 elsestmt(void)
 10321032 {
 10331033         if (flslvl) {
 10341034                 if (elflvl > trulvl)
 10351035                         ;
 10361036                 else if (--flslvl!=0)
 10371037                         flslvl++;
 10381038                 else
 10391039                         trulvl++;
 10401040         } else if (trulvl) {
 10411041                 flslvl++;
 10421042                 trulvl--;
 10431043         } else
 10441044                 error("#else in non-conditional section");
 10451045         if (elslvl==trulvl+flslvl)
 10461046                 error("too many #else");
 10471047         elslvl=trulvl+flslvl;
 10481048         chknl(1);
 10491049 }
 10501050 
 10511051 static void
 10521052 skpln(void)
 10531053 {
 10541054         int ch;
 10551055 
 10561056         /* just ignore the rest of the line */
 10571057         while ((ch = inch()) != -1) {
 10581058                 if (ch == '\n') {
 10591059                         unch('\n');
 10601060                         break;
 10611061                 }
 10621062         }
 10631063 }
 10641064 
 10651065 static void
 10661066 ifdefstmt(void)         
 10671067 {
 10681068         int t;
 10691069 
 10701070         if (flslvl) {
 10711071                 flslvl++;
 10721072                 skpln();
 10731073                 return;
 10741074         }
 10751075         do
 10761076                 t = sloscan();
 10771077         while (t == WSPACE);
 10781078         if (t != IDENT)
 10791079                 error("bad #ifdef");
 10801080         if (lookup(yytext, FIND) == NULL)
 10811081                 flslvl++;
 10821082         else
 10831083                 trulvl++;
 10841084         chknl(0);
 10851085 }
 10861086 
 10871087 static void
 10881088 ifndefstmt(void)         
 10891089 {
 10901090         int t;
 10911091 
 10921092         if (flslvl) {
 10931093                 flslvl++;
 10941094                 skpln();
 10951095                 return;
 10961096         }
 10971097         do
 10981098                 t = sloscan();
 10991099         while (t == WSPACE);
 11001100         if (t != IDENT)
 11011101                 error("bad #ifndef");
 11021102         if (lookup(yytext, FIND) != NULL)
 11031103                 flslvl++;
 11041104         else
 11051105                 trulvl++;
 11061106         chknl(0);
 11071107 }
 11081108 
 11091109 static void
 11101110 endifstmt(void)         
 11111111 {
 11121112         if (flslvl)
 11131113                 flslvl--;
 11141114         else if (trulvl)
 11151115                 trulvl--;
 11161116         else
 11171117                 error("#endif in non-conditional section");
 11181118         if (flslvl == 0)
 11191119                 elflvl = 0;
 11201120         elslvl = 0;
 11211121         chknl(1);
 11221122 }
 11231123 
 11241124 static void
 11251125 ifstmt(void)
 11261126 {
 11271127         if (flslvl || yyparse() == 0)
 11281128                 flslvl++;
 11291129         else
 11301130                 trulvl++;
 11311131 }
 11321132 
 11331133 static void
 11341134 elifstmt(void)
 11351135 {
 11361136         if (flslvl == 0)
 11371137                 elflvl = trulvl;
 11381138         if (flslvl) {
 11391139                 if (elflvl > trulvl)
 11401140                         ;
 11411141                 else if (--flslvl!=0)
 11421142                         flslvl++;
 11431143                 else if (yyparse())
 11441144                         trulvl++;
 11451145                 else
 11461146                         flslvl++;
 11471147         } else if (trulvl) {
 11481148                 flslvl++;
 11491149                 trulvl--;
 11501150         } else
 11511151                 error("#elif in non-conditional section");
 11521152 }
 11531153 
 11541154 /* save line into stringbuf */
 11551155 static usch *
 11561156 savln(void)
 11571157 {
 11581158         int c;
 11591159         usch *cp = stringbuf;
 11601160 
 11611161         while ((c = inch()) != -1) {
 11621162                 if (c == '\n') {
 11631163                         unch(c);
 11641164                         break;
 11651165                 }
 11661166                 savch(c);
 11671167         }
 11681168         savch(0);
 11691169 
 11701170         return cp;
 11711171 }
 11721172 
 11731173 static void
 11741174 cpperror(void)
 11751175 {
 11761176         usch *cp;
 11771177         int c;
 11781178 
 11791179         if (flslvl)
 11801180                 return;
 11811181         c = sloscan();
 11821182         if (c != WSPACE && c != '\n')
 11831183                 error("bad #error");
 11841184         cp = savln();
 11851185         error("#error %s", cp);
 11861186 }
 11871187 
 11881188 static void
 11891189 cppwarning(void)
 11901190 {
 11911191         usch *cp;
 11921192         int c;
 11931193 
 11941194         if (flslvl)
 11951195                 return;
 11961196         c = sloscan();
 11971197         if (c != WSPACE && c != '\n')
 11981198                 error("bad #warning");
 11991199         cp = savln();
 12001200         warning("#warning %s", cp);
 12011201         stringbuf = cp;
 12021202 }
 12031203 
 12041204 static void
 12051205 undefstmt(void)
 12061206 {
 12071207         struct symtab *np;
 12081208 
 12091209         if (flslvl)
 12101210                 return;
 12111211         if (sloscan() != WSPACE || sloscan() != IDENT)
 12121212                 error("bad #undef");
 12131213         if ((np = lookup(yytext, FIND)) != NULL)
 12141214                 np->value = 0;
 12151215         chknl(0);
 12161216 }
 12171217 
 12181218 static void
 12191219 pragmastmt(void)
 12201220 {
 12211221         usch *sb;
 12221222 
 12231223         if (flslvl)
 12241224                 return;
 12251225         if (sloscan() != WSPACE)
 12261226                 error("bad #pragma");
 12271227         sb = stringbuf;
<>1228 -        savstr((const usch *)"#pragma ");
  1228+        savstr((const usch *)"\n#pragma ");
12291229         savln();
 12301230         putstr(sb);
<> 1231+        prtline();
<_12311232         stringbuf = sb;
 12321233 }
 12331234 
 12341235 int
 12351236 cinput(void)
 12361237 {
 12371238         return inch();
 12381239 }
 12391240 
 12401241 static struct {
 12411242         const char *name;
 12421243         void (*fun)(void);
 12431244 } ppd[] = {
 12441245         { "ifndef", ifndefstmt },
 12451246         { "ifdef", ifdefstmt },
 12461247         { "if", ifstmt },
 12471248         { "include", include },
 12481249         { "else", elsestmt },
 12491250         { "endif", endifstmt },
 12501251         { "error", cpperror },
 12511252         { "warning", cppwarning },
 12521253         { "define", define },
 12531254         { "undef", undefstmt },
 12541255         { "line", line },
 12551256         { "pragma", pragmastmt },
 12561257         { "elif", elifstmt },
 12571258 #ifdef GCC_COMPAT
 12581259         { "include_next", include_next },
 12591260 #endif
 12601261 };
 12611262 #define NPPD    (int)(sizeof(ppd) / sizeof(ppd[0]))
 12621263 
 12631264 /*
 12641265  * Handle a preprocessor directive.
 12651266  */
 12661267 void
 12671268 ppdir(void)
 12681269 {
 12691270         char bp[20];
 12701271         int ch, i;
 12711272 
 12721273 redo:   while ((ch = inch()) == ' ' || ch == '\t')
 12731274                 ;
 12741275         if (ch == '/') {
 12751276                 if ((ch = inch()) == '/') {
 12761277                         skpln();
 12771278                         return;
 12781279                 }
 12791280                 if (ch == '*') {
 12801281                         while ((ch = inch()) != -1) {
 12811282                                 if (ch == '*') {
 12821283                                         if ((ch = inch()) == '/')
 12831284                                                 goto redo;
 12841285                                         unch(ch);
 12851286                                 } else if (ch == '\n') {
 12861287                                         putch('\n');
 12871288                                         ifiles->lineno++;
 12881289                                 }
 12891290                         }
 12901291                 }
 12911292         }
 12921293         if (ch == '\n') { /* empty directive */
 12931294                 unch(ch);
 12941295                 return;
 12951296         }
 12961297         if (ch < 'a' || ch > 'z')
 12971298                 goto out; /* something else, ignore */
 12981299         i = 0;
 12991300         do {
 13001301                 bp[i++] = (usch)ch;
 13011302                 if (i == sizeof(bp)-1)
 13021303                         goto out; /* too long */
 13031304                 ch = inch();
 13041305         } while ((ch >= 'a' && ch <= 'z') || (ch == '_'));
 13051306         unch(ch);
 13061307         bp[i++] = 0;
 13071308 
 13081309         /* got keyword */
 13091310         for (i = 0; i < NPPD; i++) {
 13101311                 if (bp[0] == ppd[i].name[0] && strcmp(bp, ppd[i].name) == 0) {
 13111312                         (*ppd[i].fun)();
 13121313                         return;
 13131314                 }
 13141315         }
 13151316 
 13161317 out:    error("invalid preprocessor directive");
 13171318 }
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-07-10 11:38 +0200