Quick Search:

Mode

Context

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

Other Diffs

Ignore

Blank Lines Whitespace: Expand:

Diff

1.5
 
1.6
 
MAIN:ragge:20050511193259
 
token.c
_>11 /*      $Id$    */
 22 
 33 /*
 44  * Copyright (c) 2004 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  * 3. The name of the author may not be used to endorse or promote products
 1515  *    derived from this software without specific prior written permission
 1616  *
 1717  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 1818  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 1919  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 2020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 2121  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 2222  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 2323  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 2424  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 2525  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 2626  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 2727  */
 2828 
 2929 #include <stdlib.h>
 3030 #include <string.h>
 3131 #include <ctype.h>
 3232 #include <unistd.h>
 3333 #include <fcntl.h>
 3434 
 3535 #include "cpp.h"
 3636 
 3737 /* definition for include file info */
 3838 struct includ {
 3939         struct includ *next;
 4040         char *fname;
 4141         int lineno;
 4242 #ifdef NEWBUF
 4343         int infil;
 4444         usch *curptr;
<>45 -        int curlen;
  45+        usch *maxread;
4646         usch *ostr;
<>47 -        usch buffer[CPPBUF];
  47+        usch *buffer;
  48+        usch bbuf[NAMEMAX+CPPBUF+1];
4849 #else
 4950         FILE *ifil;
 5051 #endif
<>51 -} *ifiles, *freebufs;
  52+} *ifiles;
5253 
 5354 usch *yyp, yystr[CPPBUF];
 5455 
 5556 int yylex(void);
 5657 int yywrap(void);
 5758 
 5859 #ifdef NEWBUF
 5960 static struct includ *
 6061 getbuf(usch *file)
 6162 {
 6263         struct includ *ic;
<> 64+        usch *ostr = stringbuf;
6365 
<>64 -        if (freebufs) {
 65 -                /* Have buffer for use already */
 66 -                ic = freebufs;
 67 -                freebufs = freebufs->next;
 68 -                ic->ostr = NULL;
 69 -        } else {
 70 -                stringbuf = (usch *)ROUND((int)stringbuf);
 71 -                ic = (struct includ *)stringbuf;
 72 -                stringbuf += sizeof(struct includ);
 73 -                ic->ostr = stringbuf;
 74 -        }
 75 -        if (file) {
 76 -                ic->fname = savstr(file); /* XXX - will loose space */
 77 -                savch('\0');
 78 -        }
 79 -        if (ic->ostr)
 80 -                ic->ostr = stringbuf;
  66+//printf("getbuf1: stringbuf %p\n", stringbuf);
  67+        stringbuf = (usch *)ROUND((int)stringbuf);
  68+//printf("getbuf2: stringbuf %p\n", stringbuf);
  69+        ic = (struct includ *)stringbuf;
  70+        stringbuf += sizeof(struct includ);
  71+        ic->ostr = ostr;
  72+
  73+//printf("getbuf3: stringbuf %p\n", stringbuf);
8174         return ic;
 8275 }
 8376 
 8477 static void
 8578 putbuf(struct includ *ic)
 8679 {
<>87 -        if (ic->ostr == stringbuf) {
 88 -                /* no new macros, free this buffer */
 89 -                stringbuf = (usch *)ic;
 90 -        } else {
 91 -                ic->next = freebufs;
 92 -                freebufs = ic;
 93 -        }
  80+//printf("putbuf: stringbuf %p\n", stringbuf);
  81+if (stringbuf < (usch *)&ic[1])
  82+;//     printf("ERROR!!!\n");
  83+else
  84+        stringbuf = ic->ostr;
  85+//printf("putbuf2: stringbuf %p\n", stringbuf);
9486 }
 9587 
 9688 static int
 9789 input(void)
 9890 {
 9991         struct includ *ic;
<> 92+        int len;
10093 
<>101 -        if (ifiles->curptr < ifiles->buffer+ifiles->curlen)
  94+        if (ifiles->curptr < ifiles->maxread) {
  95+//printf("c %d\n", *ifiles->curptr);
10296                 return *ifiles->curptr++;
<> 97+}
10398         if (ifiles->infil < 0) {
 10499                 ic = ifiles;
 105100                 ifiles = ifiles->next;
 106101                 putbuf(ic);
 107102                 return input();
 108103         }
<>109 -        if ((ifiles->curlen = read(ifiles->infil, ifiles->buffer, CPPBUF)) < 0)
  104+        if ((len = read(ifiles->infil, ifiles->buffer, CPPBUF)) < 0)
110105                 error("read error on file %s", ifiles->fname);
<>111 -        if (ifiles->curlen == 0)
  106+        if (len == 0)
112107                 return -1;
 113108         ifiles->curptr = ifiles->buffer;
<> 109+        ifiles->maxread = ifiles->buffer + len;
114110         return input();
 115111 }
 116112 
 117113 static void
 118114 unput(int c)
 119115 {
 120116         struct includ *ic;
 121117 
<>122 -        if (ifiles->curptr > ifiles->buffer) {
  118+if (c == 0) {
  119+printf("no;;\n");
  120+}
  121+
  122+        if (ifiles->curptr > ifiles->bbuf) {
123123                 *--ifiles->curptr = c;
 124124         } else {
 125125                 ic = getbuf(NULL);
 126126                 ic->fname = ifiles->fname;
 127127                 ic->lineno = ifiles->lineno;
 128128                 ic->infil = -1;
<>129 -                ic->curlen = CPPBUF;
 130 -                ic->curptr = ic->buffer+CPPBUF;
  129+                ic->curptr = &ic->bbuf[NAMEMAX+CPPBUF+1];
  130+                ic->maxread = ic->curptr;
131131                 ic->next = ifiles;
 132132                 ifiles = ic;
 133133                 *--ifiles->curptr = c;
 134134         }
<> 135+//printf("unput %d\n", c);
135136 }
 136137 #else
 137138 #define input() fgetc(ifiles->ifil)
 138139 #define unput(c) ungetc(c, ifiles->ifil)
 139140 #endif
 140141 static int
 141142 slofgetc(void)
 142143 {
 143144         int c;
 144145 
 145146 again:  switch (c = input()) {
 146147         case '\\': /* continued lines */
 147148                 if ((c = input()) == '\n') {
 148149                         ifiles->lineno++;
 149150                         putc('\n', obuf);
 150151                         goto again;
 151152                 }
 152153                 cunput(c);
 153154                 return '\\';
 154155         case '?': /* trigraphs */
 155156                 if ((c = input()) != '?') {
 156157                         cunput(c);
 157158                         return '?';
 158159                 }
 159160                 switch (c = input()) {
 160161                 case '=': c = '#'; break;
 161162                 case '(': c = '['; break;
 162163                 case ')': c = ']'; break;
 163164                 case '<': c = '{'; break;
 164165                 case '>': c = '}'; break;
 165166                 case '/': c = '\\'; break;
 166167                 case '\'': c = '^'; break;
 167168                 case '!': c = '|'; break;
 168169                 case '-': c = '~'; break;
 169170                 default:
 170171                         cunput(c);
 171172                         cunput('?');
 172173                         return '?';
 173174                 }
 174175                 cunput(c);
 175176                 goto again;
 176177         default:
 177178                 return c;
 178179         }
 179180 }
 180181 
 181182 int
 182183 yylex()
 183184 {
 184185         static int wasnl = 1;
 185186         int c, oc, rval;
 186187 
 187188         yyp = yystr;
 188189         c = input();
 189190         if (c != ' ' && c != '\t' && c != '#')
 190191                 wasnl = 0;
 191192 #define ONEMORE()       { *yyp++ = c; c = slofgetc(); }
 192193 again:  switch (c) {
 193194         case -1:
<> 195+#ifdef NEWBUF
  196+                rval = 0;
  197+#else
194198                 rval = yywrap() ? 0 : yylex();
<> 199+#endif
195200                 break;
 196201 
 197202         case '\'': /* charcon */
 198203         case '"': /* string */
 199204 chstr:          oc = c;
 200205                 do {
 201206                         *yyp++ = c;
 202207                         if (c == '\\')
 203208                                 *yyp++ = slofgetc();
 204209                 } while ((c = slofgetc()) != EOF && c != oc);
 205210                 *yyp++ = c; *yyp = 0;
 206211                 rval = oc == '"' ? STRING : CHARCON;
 207212                 break;
 208213 
 209214         case '0': case '1': case '2': case '3': case '4':
 210215         case '5': case '6': case '7': case '8': case '9':
 211216                 *yyp++ = c;
 212217                 c = slofgetc();
 213218                 if (yyp[-1] == '0' && (c == 'x' || c == 'X')) {
 214219                         do {
 215220                                 ONEMORE();
 216221                         } while (isxdigit(c));
 217222                 } else {
 218223                         while (isdigit(c))
 219224                                 ONEMORE();
 220225                 }
 221226                 if (c != '.' && c != 'e' && c != 'E') {
 222227                         /* not floating point number */
 223228                         while (c == 'l' || c == 'L' || c == 'u' || c == 'U') {
 224229                                 ONEMORE();
 225230                         }
 226231                         cunput(c);
 227232                         *yyp = 0;
 228233                         rval = NUMBER;
 229234                         break;
 230235                 }
 231236                 /* it's a floating point number here */
 232237                 if (c == '.') { /* decimal point */
 233238 F:                      do { /* may be followed by digits */
 234239                                 ONEMORE();
 235240                         } while (isdigit(c));
 236241                         if (c == 'e' || c == 'E') {
 237242 E:                              ONEMORE();
 238243                                 if (c == '-' || c == '+') {
 239244                                         ONEMORE();
 240245                                 }
 241246                                 while (isdigit(c))
 242247                                         ONEMORE();
 243248                         }
 244249                         if (c == 'f' || c == 'F' || c == 'l' || c == 'L')
 245250                                 ONEMORE();
 246251                         cunput(c);
 247252                         *yyp = 0;
 248253                         rval = FPOINT;
 249254                         break;
 250255                 } else
 251256                         goto E;
 252257 
 253258         case '.':
 254259                 ONEMORE();
 255260                 if (isdigit(c))
 256261                         goto F;
 257262                 if (c == '.') {
 258263                         ONEMORE();
 259264                         if (c == '.') {
 260265                                 *yyp++ = '.'; *yyp = 0;
 261266                                 rval = ELLIPS;
 262267                                 break;
 263268                         }
 264269                         cunput(c);
 265270                         cunput('.');
 266271                         *--yyp = 0;
 267272                         rval = '.';
 268273                         break;
 269274                 }
 270275                 cunput(c);
 271276                 *yyp = 0;
 272277                 rval = '.';
 273278                 break;
 274279 
 275280         case '\\':
 276281                 c = input();
 277282                 if (c == '\n') {
 278283                         ifiles->lineno++;
 279284                         putc('\n', obuf);
 280285                         c = input();
 281286                         goto again;
 282287                 } else {
 283288                         cunput(c);
 284289                         *yyp++ = '\\'; *yyp = 0;
 285290                         rval = '\\';
 286291                 }
 287292                 break;
 288293                 
 289294         case '\n':
 290295                 wasnl = 1;
 291296                 ifiles->lineno++;
 292297                 *yyp++ = c; *yyp = 0;
 293298                 rval = NL;
 294299                 break;
 295300 
 296301         case '#':
 297302                 if (wasnl) {
 298303                         wasnl = 0;
 299304                         rval = CONTROL;
 300305                         break;
 301306                 }
 302307                 *yyp++ = c;
 303308                 c = input();
 304309                 if (c == '#') {
 305310                         *yyp++ = c;
 306311                         *yyp = 0;
 307312                         rval = CONCAT;
 308313                 } else {
 309314                         unput(c);
 310315                         *yyp = 0;
 311316                         rval = MKSTR;
 312317                 }
 313318                 break;
 314319 
 315320         case ' ':
 316321         case '\t': /* whitespace */
 317322                 do {
 318323                         *yyp++ = c;
 319324                         c = input();
 320325                 } while (c == ' ' || c == '\t');
 321326                 if (wasnl && c == '#') {
 322327                         wasnl = 0;
 323328                         rval = CONTROL;
 324329                 } else {
 325330                         unput(c);
 326331                         *yyp = 0;
 327332                         rval = WSPACE;
 328333                 }
 329334                 break;
 330335 
 331336         case '/':
 332337                 if ((c = slofgetc()) == '/') {
 333338                         if (Cflag)
 334339                                 fprintf(obuf, "//");
 335340                         while ((c = slofgetc()) && c != '\n')
 336341                                 if (Cflag)
 337342                                         putc(c, obuf);
 338343                         goto again;
 339344                 } else if (c == '*') {
 340345                         if (Cflag)
 341346                                 fprintf(obuf, "/*");
 342347                         oc = 0;
 343348                         do {
 344349                                 while ((c = slofgetc()) && c != '*') {
 345350                                         if (c == '\n') {
 346351                                                 putc(c, obuf);
 347352                                                 ifiles->lineno++;
 348353                                         } else if (Cflag)
 349354                                                 putc(c, obuf);
 350355                                 }
 351356                                 if (Cflag)
 352357                                         putc(c, obuf);
 353358                                 if ((c = slofgetc()) == '/')
 354359                                         break;
 355360                                 unput(c);
 356361                         } while (c);
 357362                         if (Cflag)
 358363                                 putc(c, obuf);
 359364                         if (tflag) {
 360365                                 rval = yylex();
 361366                         } else {
 362367                                 *yyp++ = ' '; *yyp = 0;
 363368                                 rval = WSPACE;
 364369                         }
 365370                 } else {
 366371                         unput(c);
 367372                         *yyp++ = '/'; *yyp = 0;
 368373                         rval = '/';
 369374                 }
 370375                 break;
 371376 
 372377         case 'L': /* may be STRING, CHARCON or identifier */
 373378                 *yyp++ = c;
 374379                 if ((c = slofgetc()) == '"' || c == '\'')
 375380                         goto chstr;
 376381 gotid:          while (isalnum(c) || c == '_') {
 377382                         *yyp++ = c;
 378383                         c = slofgetc();
 379384                 }
 380385                 *yyp = 0;
 381386                 unput(c);
 382387                 rval = IDENT;
 383388                 break;
 384389 
 385390         default:
 386391                 if (isalpha(c) || c == '_')
 387392                         goto gotid;
 388393                 yystr[0] = c; yystr[1] = 0;
 389394                 rval = c;
 390395                 break;
 391396         }
 392397         return rval;
 393398 }
 394399 
<>395 -#ifdef NEW_READFILE
  400+#ifdef NEWBUF
396401 /*
<>397 - * A new file included.  Read buffers are allocated on the stack and
 398 - * all subroutines are called from hereThis function will be called
 399 - * recursive when multiple files are included.
  402+ * A new file included.
  403+ * If ifiles == NULL, this is the first file and already opened (stdin).
  404+ * Return 0 on success, -1 on failure to open file.
400405  */
 401406 int
 402407 pushfile(char *file)
 403408 {
<>404 -        struct includ incl, *ic = &incl;
  409+        struct includ ibuf;
  410+        struct includ *old;
  411+        struct includ *ic;
405412 
<>406 -        ic->lineno = 1;
 407 -        if (ifiles != NULL) { /* not if first file */
  413+        ic = &ibuf;
  414+        old = ifiles;
  415+
  416+        if (file != NULL) {
408417                 if ((ic->infil = open(file, O_RDONLY)) < 0)
 409418                         return -1;
<>410 -        } else
 411 -                ic->infil = 0; /* STDIN_FILENO */
 412 -
 413 -        ic->fname = savstr(file); /* XXX - will loose space */
 414 -        savch('\0');
  419+                ic->fname = file;
  420+        } else {
  421+                ic->infil = 0;
  422+                ic->fname = "<stdin>";
  423+        }
  424+        ic->buffer = ic->bbuf+NAMEMAX;
415425         ic->curptr = ic->buffer;
<>416 -        ic->next = ifiles;
417426         ifiles = ic;
<> 427+        ic->lineno = 0;
  428+        ic->maxread = ic->curptr;
  429+        unput('\n');
418430 
<>419 -        while ((c = qscan()) != 0) {
 420 -                switch (c) {
 421 -                case CONTROL:
 422 -                        control();
 423 -                        break;
  431+        mainscan();
424432 
<>425 -                
  433+        if (trulvl || flslvl)
  434+                error("unterminated conditional");
426435 
<>427 -
 428 -
  436+        ifiles = old;
  437+        close(ic->infil);
  438+        return 0;
429439 }
 430440 #else
<> 441+
431442 /*
 432443  * A new file included.
 433444  * If ifiles == NULL, this is the first file and already opened (stdin).
 434445  * Return 0 on success, -1 on failure to open file.
 435446  */
 436447 int
 437448 pushfile(char *file)
 438449 {
 439450         struct includ *ic;
 440451 
<>441 -#ifdef NEWBUF
 442 -        ic = getbuf(file);
 443 -#else
444452         ic = malloc(sizeof(struct includ));
 445453         ic->fname = strdup(file);
<>446 -#endif
447454         if (ifiles != NULL) {
<>448 -#ifdef NEWBUF
 449 -                if ((ic->infil = open(file, O_RDONLY)) < 0)
 450 -                        return -1;
 451 -#else
452455                 if ((ic->ifil = fopen(file, "r")) == NULL)
 453456                         return -1;
<>454 -#endif
455457         } else
<>456 -#ifdef NEWBUF
 457 -                ic->infil = 0;
 458 -#else
459458                 ic->ifil = stdin;
<>460 -#endif
 461 -#ifdef NEWBUF
 462 -        ic->curptr = ic->buffer;
 463 -#endif
464459         ic->next = ifiles;
 465460         ifiles = ic;
 466461         ic->lineno = 0;
 467462         unput('\n');
 468463 
 469464         return 0;
 470465 }
<>471 -#endif
472466 
 473467 /*
 474468  * End of included file (or everything).
 475469  */
 476470 void
 477471 popfile()
 478472 {
 479473         struct includ *ic;
 480474 
 481475         ic = ifiles;
 482476         ifiles = ifiles->next;
<>483 -#ifdef NEWBUF
 484 -        close(ic->infil);
 485 -        putbuf(ic);
 486 -#else
487477         fclose(ic->ifil);
 488478         free(ic->fname);
 489479         free(ic);
<>490 -#endif
491480         prtline();
 492481 }
<> 482+#endif
493483 
 494484 /*
 495485  * Print current position to output file.
 496486  */
 497487 void
 498488 prtline()
 499489 {
 500490         fprintf(obuf, "# %d \"%s\"\n", ifiles->lineno, ifiles->fname);
 501491 }
 502492 
 503493 void
 504494 cunput(int c)
 505495 {
 506496 extern int dflag;
 507497 if (dflag)printf(": '%c'(%d)", c, c);
 508498         unput(c);
 509499 }
 510500 
<> 501+#ifndef NEWBUF
511502 int
 512503 yywrap()
 513504 {
 514505         if (ifiles->next == 0)
 515506                 return 1;
 516507         popfile();
 517508         return 0;
 518509 }
<> 510+#endif
519511 
 520512 void
 521513 setline(int line)
 522514 {
 523515         if (ifiles)
 524516                 ifiles->lineno = line-1;
 525517 }
 526518 
 527519 void
 528520 setfile(char *name)
 529521 {
 530522         if (ifiles)
<>531 -                free(ifiles->fname), ifiles->fname = strdup(name);
  523+                ifiles->fname = strdup(name);
<_532524 }
 533525 
 534526 int
 535527 curline()
 536528 {
 537529         return ifiles ? ifiles->lineno : 0;
 538530 }
 539531 
 540532 char *
 541533 curfile()
 542534 {
 543535         return ifiles ? ifiles->fname : "";
 544536 }
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-23 07:31 +0200