Quick Search:

Mode

Context

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

Other Diffs

Ignore

Blank Lines Whitespace: Expand:

Diff

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