Quick Search:

Mode

Context

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

Other Diffs

Ignore

Blank Lines Whitespace: Expand:

Diff

1.94
 
1.95
 
MAIN:plunky:20121029175551
 
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 #include <ctype.h>
 4848 #ifdef HAVE_UNISTD_H
 4949 #include <unistd.h>
 5050 #endif
 5151 #include <fcntl.h>
 5252 
 5353 #include "compat.h"
 5454 #include "cpp.h"
 5555 #include "cpy.h"
 5656 
 5757 static void cvtdig(int rad);
 5858 static int charcon(usch *);
 5959 static void elsestmt(void);
 6060 static void ifdefstmt(void);
 6161 static void ifndefstmt(void);
 6262 static void endifstmt(void);
 6363 static void ifstmt(void);
 6464 static void cpperror(void);
 6565 static void pragmastmt(void);
 6666 static void undefstmt(void);
 6767 static void cppwarning(void);
 6868 static void elifstmt(void);
 6969 static int chktg(void);
 7070 static int inpch(void);
 7171 static int inch(void);
 7272 
 7373 #define PUTCH(ch) if (!flslvl) putch(ch)
 7474 /* protection against recursion in #include */
 7575 #define MAX_INCLEVEL    100
 7676 static int inclevel;
 7777 
 7878 /* get next character unaltered */
 7979 #define NXTCH() (ifiles->curptr < ifiles->maxread ? *ifiles->curptr++ : inpch())
 8080 
 8181 usch yytext[CPPBUF];
 8282 
 8383 struct includ *ifiles;
 8484 
 8585 char spechr[256] = {
 8686         0,      0,      0,      0,      C_SPECC_SPEC0,      0,
 8787         0,      C_WSNLC_SPEC|C_WSNL,  0,
 8888         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_I,    C_I,    C_I,    C_I,    C_I,    C_I,    C_I,    C_I,
 9595         C_I,    C_I,    0,      0,      C_2,    C_2,    C_2,    C_SPEC,
 9696 
 9797         0,      C_I,    C_I,    C_I,    C_I,    C_I|C_EP, C_I,  C_I,
 9898         C_I,    C_I,    C_I,    C_I,    C_I,    C_I,    C_I,    C_I,
 9999         C_I|C_EP, C_I,  C_I,    C_I,    C_I,    C_I,    C_I,    C_I,
 100100         C_I,    C_I,    C_I,    0,      C_SPEC0,      0,      C_I,
 101101 
 102102         0,      C_I,    C_I,    C_I,    C_I,    C_I|C_EP, C_I,  C_I,
 103103         C_I,    C_I,    C_I,    C_I,    C_I,    C_I,    C_I,    C_I,
 104104         C_I|C_EP, 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 static void
 110110 unch(int c)
 111111 {
 112112         if (c == -1)
 113113                 return;
 114114                 
 115115         --ifiles->curptr;
 116116         if (ifiles->curptr < ifiles->bbuf)
 117117                 error("pushback buffer full");
 118118         *ifiles->curptr = (usch)c;
 119119 }
 120120 
 121121 static void
 122122 eatcmnt(void)
 123123 {
 124124         int ch;
 125125 
 126126         if (Cflag) { PUTCH('/'); PUTCH('*'); }
 127127         for (;;) {
 128128                 ch = inch();
 129129                 if (ch == '\n') {
 130130                         ifiles->lineno++;
 131131                         if (!Cflag) PUTCH('\n');
 132132                 }
 133133                 if (ch == -1)
 134134                         break;
 135135                 if (ch == '*') {
 136136                         ch = inch();
 137137                         if (ch == '/') {
 138138                                 if (Cflag) {
 139139                                         PUTCH('*');
 140140                                         PUTCH('/');
 141141                                 } else
 142142                                         PUTCH(' ');
 143143                                 break;
 144144                         }
 145145                         unch(ch);
 146146                         ch = '*';
 147147                 }
 148148                 if (Cflag) PUTCH(ch);
 149149         }
 150150 }
 151151 
 152152 /*
 153153  * Scan quickly the input file searching for:
 154154  *      - '#' directives
 155155  *      - keywords (if not flslvl)
 156156  *      - comments
 157157  *
 158158  *      Handle strings, numbers and trigraphs with care.
 159159  *      Only data from pp files are scanned here, never any rescans.
 160160  *      TODO: Only print out strings before calling other functions.
 161161  */
 162162 static void
 163163 fastscan(void)
 164164 {
 165165         struct symtab *nl;
 166166         int ch, i = 0;
 167167         int nnl = 0;
 168168         usch *cp;
 169169 
 170170         goto run;
 171171         for (;;) {
 172172                 ch = NXTCH();
 173173 xloop:          if (ch == -1)
 174174                         return;
 175175 #ifdef PCC_DEBUG
 176176                 if (dflag>1)
 177177                         printf("fastscan ch %d (%c)\n", ch, ch > 31 ? ch : '@');
 178178 #endif
 179179                 if ((spechr[ch] & C_SPEC) == 0) {
 180180                         PUTCH(ch);
 181181                         continue;
 182182                 }
 183183                 switch (ch) {
 184184                 case EBLOCK:
 185185                 case WARN:
 186186                 case CONC:
 187187                         error("bad char passed");
 188188                         break;
 189189 
 190190                 case '/': /* Comments */
 191191                         if ((ch = inch()) == '/') {
 192192 cppcmt:                         if (Cflag) { PUTCH(ch); } else { PUTCH(' '); }
 193193                                 do {
 194194                                         if (Cflag) PUTCH(ch);
 195195                                         ch = inch();
 196196                                 } while (ch != -1 && ch != '\n');
 197197                                 goto xloop;
 198198                         } else if (ch == '*') {
 199199                                 eatcmnt();
 200200                                 continue;
 201201                         } else {
 202202                                 PUTCH('/');
 203203                                 goto xloop;
 204204                         }
 205205                         break;
 206206 
 207207                 case '?'/* trigraphs */
 208208                         if ((ch = chktg()))
 209209                                 goto xloop;
 210210                         PUTCH('?');
 211211                         break;
 212212 
 213213                 case '\\':
 214214                         if ((ch = NXTCH()) == '\n') {
 215215                                 ifiles->lineno++;
 216216                                 continue;
 217217                         }
 218218                         PUTCH('\\');
 219219                         goto xloop;
 220220 
 221221                 case '\n': /* newlines, for pp directives */
 222222                         while (nnl > 0) { PUTCH('\n'); nnl--; ifiles->lineno++; }
 223223 run2:                   ifiles->lineno++;
 224224                         do {
 225225                                 PUTCH(ch);
 226226 run:                            ch = NXTCH();
 227227                                 if (ch == '/') {
 228228                                         ch = NXTCH();
 229229                                         if (ch == '/')
 230230                                                 goto cppcmt;
 231231                                         if (ch == '*') {
 232232                                                 eatcmnt();
 233233                                                 goto run;
 234234                                         }
 235235                                         unch(ch);
 236236                                         ch = '/';
 237237                                 }
 238238                         } while (ch == ' ' || ch == '\t');
 239239                         if (ch == '\\') {
 240240                                 ch = NXTCH();
 241241                                 if (ch == '\n')
 242242                                         goto run2;
 243243                                 unch(ch);
 244244                                 ch = '\\';
 245245                         }
 246246                         if (ch == '#') {
 247247                                 ppdir();
 248248                                 continue;
 249249                         } else if (ch == '%') {
 250250                                 ch = NXTCH();
 251251                                 if (ch == ':') {
 252252                                         ppdir();
 253253                                         continue;
 254254                                 }
 255255                                 unch(ch);
 256256                                 ch = '%';
 257257                         } else if (ch == '?') {
 258258                                 if ((ch = chktg()) == '#') {
 259259                                         ppdir();
 260260                                         continue;
 261261                                 } else if (ch == 0)
 262262                                         ch = '?';
 263263                         }
 264264                         goto xloop;
 265265 
 266266                 case '\"': /* strings */
 267267 str:                    PUTCH(ch);
 268268                         while ((ch = NXTCH()) != '\"') {
 269269                                 if (ch == '\n')
 270270                                         goto xloop;
 271271                                 if (ch == '\\') {
 272272                                         if ((ch = NXTCH()) != '\n') {
 273273                                                 PUTCH('\\');
 274274                                                 PUTCH(ch);
 275275                                         } else
 276276                                                 nnl++;
 277277                                         continue;
 278278                                 }
 279279                                 if (ch == -1)
 280280                                         return;
 281281                                 PUTCH(ch);
 282282                         }
 283283                         PUTCH(ch);
 284284                         break;
 285285 
 286286                 case '.'/* for pp-number */
 287287                         PUTCH(ch);
 288288                         ch = NXTCH();
 289289                         if (ch < '0' || ch > '9')
 290290                                 goto xloop;
 291291                         /* FALLTHROUGH */
 292292                 case '0': case '1': case '2': case '3': case '4':
 293293                 case '5': case '6': case '7': case '8': case '9':
 294294                         do {
 295295                                 PUTCH(ch);
 296296 nxt:                            ch = NXTCH();
 297297                                 if (ch == '\\') {
 298298                                         ch = NXTCH();
 299299                                         if (ch == '\n') {
 300300                                                 nnl++;
 301301                                                 goto nxt;
 302302                                         }
 303303                                         unch(ch);
 304304                                         ch = '\\';
 305305                                 }
 306306                                 if (ch == -1)
 307307                                         return;
 308308                                 if (spechr[ch] & C_EP) {
 309309                                         PUTCH(ch);
 310310                                         ch = NXTCH();
 311311                                         if (ch == '-' || ch == '+')
 312312                                                 continue;
 313313                                         if (ch == -1)
 314314                                                 return;
 315315                                 }
 316316                         } while ((spechr[ch] & C_ID) || (ch == '.'));
 317317                         goto xloop;
 318318 
 319319                 case '\'': /* character literal */
 320320 con:                    PUTCH(ch);
 321321                         if (tflag)
 322322                                 continue; /* character constants ignored */
 323323                         while ((ch = NXTCH()) != '\'') {
 324324                                 if (ch == '\n')
 325325                                         goto xloop;
 326326                                 if (ch == '\\') {
 327327                                         if ((ch = NXTCH()) != '\n') {
 328328                                                 PUTCH('\\');
 329329                                                 PUTCH(ch);
 330330                                         } else
 331331                                                 nnl++;
 332332                                         continue;
 333333                                 }
 334334                                 if (ch == -1)
 335335                                         return;
 336336                                 PUTCH(ch);
 337337                         }
 338338                         PUTCH(ch);
 339339                         break;
 340340 
 341341                 case 'L':
 342342                         ch = NXTCH();
 343343                         if (ch == '\"') {
 344344                                 PUTCH('L');
 345345                                 goto str;
 346346                         }
 347347                         if (ch == '\'') {
 348348                                 PUTCH('L');
 349349                                 goto con;
 350350                         }
 351351                         unch(ch);
 352352                         ch = 'L';
 353353                         /* FALLTHROUGH */
 354354                 default:
 355355                         if ((spechr[ch] & C_ID) == 0)
 356356                                 error("fastscan");
 357357                         if (flslvl) {
<>358 -                                while (ch != -1 && spechr[ch] & C_ID)
  358+                                while (ch != -1 && (spechr[ch] & C_ID))
<_359359                                         ch = NXTCH();
 360360                                 goto xloop;
 361361                         }
 362362                         i = 0;
 363363                         do {
 364364                                 yytext[i++] = (usch)ch;
 365365                                 ch = NXTCH();
 366366                                 if (ch == '\\') {
 367367                                         ch = NXTCH();
 368368                                         if (ch != '\n') {
 369369                                                 unch(ch);
 370370                                                 ch = '\\';
 371371                                         } else {
 372372                                                 putch('\n');
 373373                                                 ifiles->lineno++;
 374374                                                 ch = NXTCH();
 375375                                         }
 376376                                 }
 377377                                 if (ch == -1)
 378378                                         return;
 379379                         } while (spechr[ch] & C_ID);
 380380 
 381381                         yytext[i] = 0;
 382382                         unch(ch);
 383383 
 384384                         cp = stringbuf;
 385385                         if ((nl = lookup(yytext, FIND)) && kfind(nl)) {
 386386                                 putstr(stringbuf);
 387387                         } else
 388388                                 putstr(yytext);
 389389                         stringbuf = cp;
 390390 
 391391                         break;
 392392                 }
 393393         }
 394394 }
 395395 
 396396 int
 397397 sloscan(void)
 398398 {
 399399         int ch;
 400400         int yyp;
 401401 
 402402 zagain:
 403403         yyp = 0;
 404404         ch = inch();
 405405         yytext[yyp++] = (usch)ch;
 406406         switch (ch) {
 407407         case -1:
 408408                 return 0;
 409409         case '\n':
 410410                 /* sloscan() never passes \n, that's up to fastscan() */
 411411                 unch(ch);
 412412                 yytext[yyp] = 0;
 413413                 return ch;
 414414 
 415415         case '\r': /* Ignore CR's */
 416416                 yyp = 0;
 417417                 break;
 418418 
 419419         case '0': case '1': case '2': case '3': case '4': case '5':
 420420         case '6': case '7': case '8': case '9':
 421421                 /* readin a "pp-number" */
 422422 ppnum:          for (;;) {
 423423                         ch = inch();
 424424                         if (ch == -1)
 425425                                 break;
 426426                         if (spechr[ch] & C_EP) {
 427427                                 yytext[yyp++] = (usch)ch;
 428428                                 ch = inch();
 429429                                 if (ch == '-' || ch == '+') {
 430430                                         yytext[yyp++] = (usch)ch;
 431431                                 } else
 432432                                         unch(ch);
 433433                                 continue;
 434434                         }
 435435                         if ((spechr[ch] & C_ID) || ch == '.') {
 436436                                 yytext[yyp++] = (usch)ch;
 437437                                 continue;
 438438                         }
 439439                         break;
 440440                 }
 441441                 unch(ch);
 442442                 yytext[yyp] = 0;
 443443 
 444444                 return NUMBER;
 445445 
 446446         case '\'':
 447447 chlit:          
 448448                 for (;;) {
 449449                         if ((ch = inch()) == '\\') {
 450450                                 yytext[yyp++] = (usch)ch;
 451451                                 yytext[yyp++] = (usch)inch();
 452452                                 continue;
 453453                         } else if (ch == -1 || ch == '\n') {
 454454                                 /* not a constant */
 455455                                 while (yyp > 1)
 456456                                         unch(yytext[--yyp]);
 457457                                 ch = '\'';
 458458                                 goto any;
 459459                         } else
 460460                                 yytext[yyp++] = (usch)ch;
 461461                         if (ch == '\'')
 462462                                 break;
 463463                 }
 464464                 yytext[yyp] = 0;
 465465 
 466466                 return NUMBER;
 467467 
 468468         case ' ':
 469469         case '\t':
 470470                 while ((ch = inch()) == ' ' || ch == '\t')
 471471                         yytext[yyp++] = (usch)ch;
 472472                 unch(ch);
 473473                 yytext[yyp] = 0;
 474474                 return WSPACE;
 475475 
 476476         case '/':
 477477                 if ((ch = inch()) == '/') {
 478478                         do {
 479479                                 yytext[yyp++] = (usch)ch;
 480480                                 ch = inch();
 481481                         } while (ch != -1 && ch != '\n');
 482482                         yytext[yyp] = 0;
 483483                         unch(ch);
 484484                         goto zagain;
 485485                 } else if (ch == '*') {
 486486                         int c, wrn;
 487487                         extern int readmac;
 488488 
 489489                         if (Cflag && !flslvl && readmac) {
 490490                                 unch(ch);
 491491                                 yytext[yyp] = 0;
 492492                                 return CMNT;
 493493                         }
 494494 
 495495                         wrn = 0;
 496496                 more:   while ((c = inch()) != '*') {
 497497                                 if (c == -1)
 498498                                         return 0;       
 499499                                 if (c == '\n')
 500500                                         putch(c), ifiles->lineno++;
 501501                                 else if (c == EBLOCK) {
 502502                                         (void)inch();
 503503                                         (void)inch();
 504504                                 } else if (c == 1) /* WARN */
 505505                                         wrn = 1;
 506506                         }
 507507                         if ((c = inch()) == -1)
 508508                                 return 0;
 509509                         if (c != '/') {
 510510                                 unch(c);
 511511                                 goto more;
 512512                         }
 513513                         if (!tflag && !Cflag && !flslvl)
 514514                                 unch(' ');
 515515                         if (wrn)
 516516                                 unch(1);
 517517                         goto zagain;
 518518                 }
 519519                 unch(ch);
 520520                 ch = '/';
 521521                 goto any;
 522522 
 523523         case '.':
 524524                 if ((ch = inch()) == -1)
 525525                         return 0;
 526526                 if (isdigit(ch)) {
 527527                         yytext[yyp++] = (usch)ch;
 528528                         goto ppnum;
 529529                 } else {
 530530                         unch(ch);
 531531                         ch = '.';
 532532                 }
 533533                 goto any;
 534534 
 535535         case '\"':
 536536                 if (tflag && defining)
 537537                         goto any;
 538538         strng:
 539539                 for (;;) {
 540540                         if ((ch = inch()) == '\\') {
 541541                                 yytext[yyp++] = (usch)ch;
 542542                                 yytext[yyp++] = (usch)inch();
 543543                                 continue;
 544544                         } else if (ch == -1) {
 545545                                 break;
 546546                         } else
 547547                                 yytext[yyp++] = (usch)ch;
 548548                         if (ch == '\"')
 549549                                 break;
 550550                 }
 551551                 yytext[yyp] = 0;
 552552                 return STRING;
 553553 
 554554         case 'L':
 555555                 if ((ch = inch()) == '\"' && !tflag) {
 556556                         yytext[yyp++] = (usch)ch;
 557557                         goto strng;
 558558                 } else if (ch == '\'' && !tflag) {
 559559                         yytext[yyp++] = (usch)ch;
 560560                         goto chlit;
 561561                 }
 562562                 unch(ch);
 563563                 /* FALLTHROUGH */
 564564 
 565565         /* Yetch, all identifiers */
 566566         case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
 567567         case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
 568568         case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
 569569         case 's': case 't': case 'u': case 'v': case 'w': case 'x':
 570570         case 'y': case 'z':
 571571         case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
 572572         case 'G': case 'H': case 'I': case 'J': case 'K':
 573573         case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
 574574         case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
 575575         case 'Y': case 'Z':
 576576         case '_': /* {L}({L}|{D})* */
 577577 
 578578                 /* Special hacks */
 579579                 for (;;) { /* get chars */
 580580                         if ((ch = inch()) == -1)
 581581                                 break;
 582582                         if (isalpha(ch) || isdigit(ch) || ch == '_') {
 583583                                 yytext[yyp++] = (usch)ch;
 584584                         } else {
 585585                                 unch(ch);
 586586                                 break;
 587587                         }
 588588                 }
 589589                 yytext[yyp] = 0; /* need already string */
 590590                 /* end special hacks */
 591591 
 592592                 return IDENT;
 593593         default:
 594594         any:
 595595                 yytext[yyp] = 0;
 596596                 return yytext[0];
 597597 
 598598         } /* endcase */
 599599         goto zagain;
 600600 }
 601601 
 602602 int
 603603 yylex(void)
 604604 {
 605605         static int ifdef, noex;
 606606         struct symtab *nl;
 607607         int ch, c2;
 608608 
 609609         while ((ch = sloscan()) == WSPACE)
 610610                 ;
 611611         if (ch < 128 && spechr[ch] & C_2)
 612612                 c2 = inpch();
 613613         else
 614614                 c2 = 0;
 615615 
 616616 #define C2(a,b,c) case a: if (c2 == b) return c; break
 617617         switch (ch) {
 618618         C2('=', '=', EQ);
 619619         C2('!', '=', NE);
 620620         C2('|', '|', OROR);
 621621         C2('&', '&', ANDAND);
 622622         case '<':
 623623                 if (c2 == '<') return LS;
 624624                 if (c2 == '=') return LE;
 625625                 break;
 626626         case '>':
 627627                 if (c2 == '>') return RS;
 628628                 if (c2 == '=') return GE;
 629629                 break;
 630630         case '+':
 631631         case '-':
 632632                 if (ch == c2)
 633633                         error("invalid preprocessor operator %c%c", ch, c2);
 634634                 break;
 635635 
 636636         case '/':
 637637                 if (Cflag == 0 || c2 != '*')
 638638                         break;
 639639                 /* Found comment that need to be skipped */
 640640                 for (;;) {
 641641                         ch = inpch();
 642642                 c1:     if (ch != '*')
 643643                                 continue;
 644644                         if ((ch = inpch()) == '/')
 645645                                 break;
 646646                         goto c1;
 647647                 }
 648648                 return yylex();
 649649 
 650650         case NUMBER:
 651651                 if (yytext[0] == '\'') {
 652652                         yylval.node.op = NUMBER;
 653653                         yylval.node.nd_val = charcon(yytext);
 654654                 } else
 655655                         cvtdig(yytext[0] != '0' ? 10 :
 656656                             yytext[1] == 'x' || yytext[1] == 'X' ? 16 : 8);
 657657                 return NUMBER;
 658658 
 659659         case IDENT:
 660660                 if (strcmp((char *)yytext, "defined") == 0) {
 661661                         ifdef = 1;
 662662                         return DEFINED;
 663663                 }
 664664                 nl = lookup(yytext, FIND);
 665665                 if (ifdef) {
 666666                         yylval.node.nd_val = nl != NULL;
 667667                         ifdef = 0;
 668668                 } else if (nl && noex == 0) {
 669669                         usch *och = stringbuf;
 670670                         int i;
 671671 
 672672                         i = kfind(nl);
 673673                         unch(WARN);
 674674                         if (i)
 675675                                 unpstr(stringbuf);
 676676                         else
 677677                                 unpstr(nl->namep);
 678678                         stringbuf = och;
 679679                         noex = 1;
 680680                         return yylex();
 681681                 } else {
 682682                         yylval.node.nd_val = 0;
 683683                 }
 684684                 yylval.node.op = NUMBER;
 685685                 return NUMBER;
 686686         case WARN:
 687687                 noex = 0;
 688688                 /* FALLTHROUGH */
 689689         case PHOLD:
 690690                 return yylex();
 691691         default:
 692692                 return ch;
 693693         }
 694694         unch(c2);
 695695         return ch;
 696696 }
 697697 
 698698 static int
 699699 inpch(void)
 700700 {
 701701         int len;
 702702 
 703703         if (ifiles->curptr < ifiles->maxread)
 704704                 return *ifiles->curptr++;
 705705 
 706706         if (ifiles->infil == -1)
 707707                 return -1;
 708708         if ((len = read(ifiles->infil, ifiles->buffer, CPPBUF)) < 0)
 709709                 error("read error on file %s", ifiles->orgfn);
 710710         if (len == 0)
 711711                 return -1;
 712712         ifiles->buffer[len] = 0;
 713713         ifiles->curptr = ifiles->buffer;
 714714         ifiles->maxread = ifiles->buffer + len;
 715715         return inpch();
 716716 }
 717717 
 718718 static int
 719719 inch(void)
 720720 {
 721721         int c;
 722722 
 723723 again:  switch (c = inpch()) {
 724724         case '\\': /* continued lines */
 725725 msdos:          if ((c = inpch()) == '\n') {
 726726                         ifiles->lineno++;
 727727                         putch('\n');
 728728                         goto again;
 729729                 } else if (c == '\r')
 730730                         goto msdos;
 731731                 unch(c);
 732732                 return '\\';
 733733         case '?': /* trigraphs */
 734734                 if ((c = chktg())) {
 735735                         unch(c);
 736736                         goto again;
 737737                 }
 738738                 return '?';
 739739         default:
 740740                 return c;
 741741         }
 742742 }
 743743 
 744744 /*
 745745  * Let the command-line args be faked defines at beginning of file.
 746746  */
 747747 static void
 748748 prinit(struct initar *it, struct includ *ic)
 749749 {
 750750         const char *pre, *post;
 751751         char *a;
 752752 
 753753         if (it->next)
 754754                 prinit(it->next, ic);
 755755         pre = post = NULL; /* XXX gcc */
 756756         switch (it->type) {
 757757         case 'D':
 758758                 pre = "#define ";
 759759                 if ((a = strchr(it->str, '=')) != NULL) {
 760760                         *a = ' ';
 761761                         post = "\n";
 762762                 } else
 763763                         post = " 1\n";
 764764                 break;
 765765         case 'U':
 766766                 pre = "#undef ";
 767767                 post = "\n";
 768768                 break;
 769769         case 'i':
 770770                 pre = "#include \"";
 771771                 post = "\"\n";
 772772                 break;
 773773         default:
 774774                 error("prinit");
 775775         }
 776776         strlcat((char *)ic->buffer, pre, CPPBUF+1);
 777777         strlcat((char *)ic->buffer, it->str, CPPBUF+1);
 778778         if (strlcat((char *)ic->buffer, post, CPPBUF+1) >= CPPBUF+1)
 779779                 error("line exceeds buffer size");
 780780 
 781781         ic->lineno--;
 782782         while (*ic->maxread)
 783783                 ic->maxread++;
 784784 }
 785785 
 786786 /*
 787787  * A new file included.
 788788  * If ifiles == NULL, this is the first file and already opened (stdin).
 789789  * Return 0 on success, -1 if file to be included is not found.
 790790  */
 791791 int
 792792 pushfile(const usch *file, const usch *fn, int idx, void *incs)
 793793 {
 794794         extern struct initar *initar;
 795795         struct includ ibuf;
 796796         struct includ *ic;
 797797         int otrulvl;
 798798 
 799799         ic = &ibuf;
 800800         ic->next = ifiles;
 801801 
 802802         if (file != NULL) {
 803803                 if ((ic->infil = open((const char *)file, O_RDONLY)) < 0)
 804804                         return -1;
 805805                 ic->orgfn = ic->fname = file;
 806806                 if (++inclevel > MAX_INCLEVEL)
 807807                         error("Limit for nested includes exceeded");
 808808         } else {
 809809                 ic->infil = 0;
 810810                 ic->orgfn = ic->fname = (const usch *)"<stdin>";
 811811         }
 812812 #ifndef BUF_STACK
 813813         ic->bbuf = malloc(BBUFSZ);
 814814 #endif
 815815         ic->buffer = ic->bbuf+NAMEMAX;
 816816         ic->curptr = ic->buffer;
 817817         ifiles = ic;
 818818         ic->lineno = 1;
 819819         ic->maxread = ic->curptr;
 820820         ic->idx = idx;
 821821         ic->incs = incs;
 822822         ic->fn = fn;
 823823         prtline();
 824824         if (initar) {
 825825                 int oin = ic->infil;
 826826                 ic->infil = -1;
 827827                 *ic->maxread = 0;
 828828                 prinit(initar, ic);
 829829                 initar = NULL;
 830830                 if (dMflag)
 831831                         xwrite(ofd, ic->buffer, strlen((char *)ic->buffer));
 832832                 fastscan();
 833833                 prtline();
 834834                 ic->infil = oin;
 835835         }
 836836 
 837837         otrulvl = trulvl;
 838838 
 839839         fastscan();
 840840 
 841841         if (otrulvl != trulvl || flslvl)
 842842                 error("unterminated conditional");
 843843 
 844844 #ifndef BUF_STACK
 845845         free(ic->bbuf);
 846846 #endif
 847847         ifiles = ic->next;
 848848         close(ic->infil);
 849849         inclevel--;
 850850         return 0;
 851851 }
 852852 
 853853 /*
 854854  * Print current position to output file.
 855855  */
 856856 void
 857857 prtline(void)
 858858 {
 859859         usch *sb = stringbuf;
 860860 
 861861         if (Mflag) {
 862862                 if (dMflag)
 863863                         return; /* no output */
 864864                 if (ifiles->lineno == 1) {
 865865                         sheap("%s: %s\n", Mfile, ifiles->fname);
 866866                         if (MPflag &&
 867867                             strcmp((const char *)ifiles->fname, (char *)MPfile))
 868868                                 sheap("%s:\n", ifiles->fname);
 869869                         xwrite(ofd, sb, stringbuf - sb);
 870870                 }
 871871         } else if (!Pflag) {
 872872                 sheap("\n# %d \"%s\"", ifiles->lineno, ifiles->fname);
 873873                 if (ifiles->idx == SYSINC)
 874874                         sheap(" 3");
 875875                 sheap("\n");
 876876                 putstr(sb);
 877877         }
 878878         stringbuf = sb;
 879879 }
 880880 
 881881 void
 882882 cunput(int c)
 883883 {
 884884 #ifdef PCC_DEBUG
 885885 //      if (dflag)printf(": '%c'(%d)\n", c > 31 ? c : ' ', c);
 886886 #endif
 887887         unch(c);
 888888 }
 889889 
 890890 static int
 891891 dig2num(int c)
 892892 {
 893893         if (c >= 'a')
 894894                 c = c - 'a' + 10;
 895895         else if (c >= 'A')
 896896                 c = c - 'A' + 10;
 897897         else
 898898                 c = c - '0';
 899899         return c;
 900900 }
 901901 
 902902 /*
 903903  * Convert string numbers to unsigned long long and check overflow.
 904904  */
 905905 static void
 906906 cvtdig(int rad)
 907907 {
 908908         unsigned long long rv = 0;
 909909         unsigned long long rv2 = 0;
 910910         usch *y = yytext;
 911911         int c;
 912912 
 913913         c = *y++;
 914914         if (rad == 16)
 915915                 y++;
 916916         while (isxdigit(c)) {
 917917                 rv = rv * rad + dig2num(c);
 918918                 /* check overflow */
 919919                 if (rv / rad < rv2)
 920920                         error("Constant \"%s\" is out of range", yytext);
 921921                 rv2 = rv;
 922922                 c = *y++;
 923923         }
 924924         y--;
 925925         while (*y == 'l' || *y == 'L')
 926926                 y++;
 927927         yylval.node.op = *y == 'u' || *y == 'U' ? UNUMBER : NUMBER;
 928928         yylval.node.nd_uval = rv;
 929929         if ((rad == 8 || rad == 16) && yylval.node.nd_val < 0)
 930930                 yylval.node.op = UNUMBER;
 931931         if (yylval.node.op == NUMBER && yylval.node.nd_val < 0)
 932932                 /* too large for signed, see 6.4.4.1 */
 933933                 error("Constant \"%s\" is out of range", yytext);
 934934 }
 935935 
 936936 static int
 937937 charcon(usch *p)
 938938 {
 939939         int val, c;
 940940 
 941941         p++; /* skip first ' */
 942942         val = 0;
 943943         if (*p++ == '\\') {
 944944                 switch (*p++) {
 945945                 case 'a': val = '\a'; break;
 946946                 case 'b': val = '\b'; break;
 947947                 case 'f': val = '\f'; break;
 948948                 case 'n': val = '\n'; break;
 949949                 case 'r': val = '\r'; break;
 950950                 case 't': val = '\t'; break;
 951951                 case 'v': val = '\v'; break;
 952952                 case '\"': val = '\"'; break;
 953953                 case '\'': val = '\''; break;
 954954                 case '\\': val = '\\'; break;
 955955                 case 'x':
 956956                         while (isxdigit(c = *p)) {
 957957                                 val = val * 16 + dig2num(c);
 958958                                 p++;
 959959                         }
 960960                         break;
 961961                 case '0': case '1': case '2': case '3': case '4':
 962962                 case '5': case '6': case '7':
 963963                         p--;
 964964                         while (isdigit(c = *p)) {
 965965                                 val = val * 8 + (c - '0');
 966966                                 p++;
 967967                         }
 968968                         break;
 969969                 default: val = p[-1];
 970970                 }
 971971 
 972972         } else
 973973                 val = p[-1];
 974974         return val;
 975975 }
 976976 
 977977 static void
 978978 chknl(int ignore)
 979979 {
 980980         int t;
 981981 
 982982         while ((t = sloscan()) == WSPACE)
 983983                 ;
 984984         if (t != '\n') {
 985985                 if (t) {
 986986                         if (ignore) {
 987987                                 warning("newline expected, got \"%s\"", yytext);
 988988                                 /* ignore rest of line */
 989989                                 while ((t = sloscan()) && t != '\n')
 990990                                         ;
 991991                         }
 992992                         else
 993993                                 error("newline expected, got \"%s\"", yytext);
 994994                 } else {
 995995                         if (ignore)
 996996                                 warning("no newline at end of file");
 997997                         else
 998998                                 error("no newline at end of file");
 999999                 }
 10001000         }
 10011001 }
 10021002 
 10031003 static void
 10041004 elsestmt(void)
 10051005 {
 10061006         if (flslvl) {
 10071007                 if (elflvl > trulvl)
 10081008                         ;
 10091009                 else if (--flslvl!=0) {
 10101010                         flslvl++;
 10111011                 } else {
 10121012                         trulvl++;
 10131013                         prtline();
 10141014                 }
 10151015         } else if (trulvl) {
 10161016                 flslvl++;
 10171017                 trulvl--;
 10181018         } else
 10191019                 error("If-less else");
 10201020         if (elslvl==trulvl+flslvl)
 10211021                 error("Too many else");
 10221022         elslvl=trulvl+flslvl;
 10231023         chknl(1);
 10241024 }
 10251025 
 10261026 static void
 10271027 skpln(void)
 10281028 {
 10291029         int ch;
 10301030 
 10311031         /* just ignore the rest of the line */
 10321032         while ((ch = inch()) != -1) {
 10331033                 if (ch == '\n') {
 10341034                         unch('\n');
 10351035                         break;
 10361036                 }
 10371037         }
 10381038         flslvl++;
 10391039 }
 10401040 
 10411041 static void
 10421042 ifdefstmt(void)         
 10431043 {
 10441044         int t;
 10451045 
 10461046         if (flslvl) {
 10471047                 skpln();
 10481048                 return;
 10491049         }
 10501050         do
 10511051                 t = sloscan();
 10521052         while (t == WSPACE);
 10531053         if (t != IDENT)
 10541054                 error("bad #ifdef");
 10551055         if (lookup(yytext, FIND) == NULL) {
 10561056                 putch('\n');
 10571057                 flslvl++;
 10581058         } else
 10591059                 trulvl++;
 10601060         chknl(0);
 10611061 }
 10621062 
 10631063 static void
 10641064 ifndefstmt(void)         
 10651065 {
 10661066         int t;
 10671067 
 10681068         if (flslvl) {
 10691069                 skpln();
 10701070                 return;
 10711071         }
 10721072         do
 10731073                 t = sloscan();
 10741074         while (t == WSPACE);
 10751075         if (t != IDENT)
 10761076                 error("bad #ifndef");
 10771077         if (lookup(yytext, FIND) != NULL) {
 10781078                 putch('\n');
 10791079                 flslvl++;
 10801080         } else
 10811081                 trulvl++;
 10821082         chknl(0);
 10831083 }
 10841084 
 10851085 static void
 10861086 endifstmt(void)         
 10871087 {
 10881088         if (flslvl) {
 10891089                 flslvl--;
 10901090                 if (flslvl == 0) {
 10911091                         putch('\n');
 10921092                         prtline();
 10931093                 }
 10941094         } else if (trulvl)
 10951095                 trulvl--;
 10961096         else
 10971097                 error("If-less endif");
 10981098         if (flslvl == 0)
 10991099                 elflvl = 0;
 11001100         elslvl = 0;
 11011101         chknl(1);
 11021102 }
 11031103 
 11041104 static void
 11051105 ifstmt(void)
 11061106 {
 11071107         if (flslvl == 0) {
 11081108                 if (yyparse() == 0) {
 11091109                         putch('\n');
 11101110                         ++flslvl;
 11111111                 } else
 11121112                         ++trulvl;
 11131113         } else
 11141114                 ++flslvl;
 11151115 }
 11161116 
 11171117 static void
 11181118 elifstmt(void)
 11191119 {
 11201120         if (flslvl == 0)
 11211121                 elflvl = trulvl;
 11221122         if (flslvl) {
 11231123                 if (elflvl > trulvl)
 11241124                         ;
 11251125                 else if (--flslvl!=0)
 11261126                         ++flslvl;
 11271127                 else {
 11281128                         if (yyparse()) {
 11291129                                 ++trulvl;
 11301130                                 prtline();
 11311131                         } else {
 11321132                                 putch('\n');
 11331133                                 ++flslvl;
 11341134                         }
 11351135                 }
 11361136         } else if (trulvl) {
 11371137                 ++flslvl;
 11381138                 --trulvl;
 11391139         } else
 11401140                 error("If-less elif");
 11411141 }
 11421142 
 11431143 /* save line into stringbuf */
 11441144 static usch *
 11451145 savln(void)
 11461146 {
 11471147         int c;
 11481148         usch *cp = stringbuf;
 11491149 
 11501150         while ((c = inch()) != -1) {
 11511151                 if (c == '\n') {
 11521152                         unch(c);
 11531153                         break;
 11541154                 }
 11551155                 savch(c);
 11561156         }
 11571157         savch(0);
 11581158 
 11591159         return cp;
 11601160 }
 11611161 
 11621162 static void
 11631163 cpperror(void)
 11641164 {
 11651165         usch *cp;
 11661166         int c;
 11671167 
 11681168         if (flslvl)
 11691169                 return;
 11701170         c = sloscan();
 11711171         if (c != WSPACE && c != '\n')
 11721172                 error("bad #error");
 11731173         cp = savln();
 11741174         error("#error %s", cp);
 11751175 }
 11761176 
 11771177 static void
 11781178 cppwarning(void)
 11791179 {
 11801180         usch *cp;
 11811181         int c;
 11821182 
 11831183         if (flslvl)
 11841184                 return;
 11851185         c = sloscan();
 11861186         if (c != WSPACE && c != '\n')
 11871187                 error("bad #warning");
 11881188         cp = savln();
 11891189         warning("#warning %s", cp);
 11901190         stringbuf = cp;
 11911191 }
 11921192 
 11931193 static void
 11941194 undefstmt(void)
 11951195 {
 11961196         struct symtab *np;
 11971197 
 11981198         if (flslvl)
 11991199                 return;
 12001200         if (sloscan() != WSPACE || sloscan() != IDENT)
 12011201                 error("bad #undef");
 12021202         if ((np = lookup(yytext, FIND)) != NULL)
 12031203                 np->value = 0;
 12041204         chknl(0);
 12051205 }
 12061206 
 12071207 static void
 12081208 pragmastmt(void)
 12091209 {
 12101210         usch *sb;
 12111211 
 12121212         if (flslvl)
 12131213                 return;
 12141214         if (sloscan() != WSPACE)
 12151215                 error("bad #pragma");
 12161216         sb = stringbuf;
 12171217         savstr((const usch *)"#pragma ");
 12181218         savln();
 12191219         putstr(sb);
 12201220         stringbuf = sb;
 12211221 }
 12221222 
 12231223 int
 12241224 cinput(void)
 12251225 {
 12261226         return inch();
 12271227 }
 12281228 
 12291229 /*
 12301230  * Check for (and convert) trigraphs.
 12311231  */
 12321232 int
 12331233 chktg(void)
 12341234 {
 12351235         int c;
 12361236 
 12371237         if ((c = inpch()) != '?') {
 12381238                 unch(c);
 12391239                 return 0;
 12401240         }
 12411241         switch (c = inpch()) {
 12421242         case '=': c = '#'; break;
 12431243         case '(': c = '['; break;
 12441244         case ')': c = ']'; break;
 12451245         case '<': c = '{'; break;
 12461246         case '>': c = '}'; break;
 12471247         case '/': c = '\\'; break;
 12481248         case '\'': c = '^'; break;
 12491249         case '!': c = '|'; break;
 12501250         case '-': c = '~'; break;
 12511251         default:
 12521252                 unch(c);
 12531253                 unch('?');
 12541254                 c = 0;
 12551255         }
 12561256         return c;
 12571257 }
 12581258 
 12591259 static struct {
 12601260         const char *name;
 12611261         void (*fun)(void);
 12621262 } ppd[] = {
 12631263         { "ifndef", ifndefstmt },
 12641264         { "ifdef", ifdefstmt },
 12651265         { "if", ifstmt },
 12661266         { "include", include },
 12671267         { "else", elsestmt },
 12681268         { "endif", endifstmt },
 12691269         { "error", cpperror },
 12701270         { "warning", cppwarning },
 12711271         { "define", define },
 12721272         { "undef", undefstmt },
 12731273         { "line", line },
 12741274         { "pragma", pragmastmt },
 12751275         { "elif", elifstmt },
 12761276 #ifdef GCC_COMPAT
 12771277         { "include_next", include_next },
 12781278 #endif
 12791279 };
 12801280 
 12811281 /*
 12821282  * Handle a preprocessor directive.
 12831283  */
 12841284 void
 12851285 ppdir(void)
 12861286 {
 12871287         char bp[20];
 12881288         int ch, i;
 12891289 
 12901290         while ((ch = inch()) == ' ' || ch == '\t')
 12911291                 ;
 12921292         if (ch == '\n') { /* empty directive */
 12931293                 unch(ch);
 12941294                 return;
 12951295         }
 12961296         if (ch < 'a' || ch > 'z')
 12971297                 goto out; /* something else, ignore */
 12981298         i = 0;
 12991299         do {
 13001300                 bp[i++] = (usch)ch;
 13011301                 if (i == sizeof(bp)-1)
 13021302                         goto out; /* too long */
 13031303                 ch = inch();
 13041304         } while ((ch >= 'a' && ch <= 'z') || (ch == '_'));
 13051305         unch(ch);
 13061306         bp[i++] = 0;
 13071307 
 13081308         /* got keyword */
 13091309 #define SZ (int)(sizeof(ppd)/sizeof(ppd[0]))
 13101310         for (i = 0; i < SZ; i++)
 13111311                 if (bp[0] == ppd[i].name[0] && strcmp(bp, ppd[i].name) == 0)
 13121312                         break;
 13131313         if (i == SZ)
 13141314                 goto out;
 13151315 
 13161316         /* Found matching keyword */
 13171317         (*ppd[i].fun)();
 13181318         return;
 13191319 
 13201320 out:    while ((ch = inch()) != '\n' && ch != -1)
 13211321                 ;
 13221322         unch('\n');
 13231323 }
FishEye: Open Source License registered to PCC.
Your maintenance has expired. You can renew your license at http://www.atlassian.com/fisheye/renew
Atlassian FishEye, CVS analysis. (Version:1.6.3 Build:build-336 2008-11-04) - Administration - Page generated 2014-08-31 06:17 +0200