Quick Search:

Mode

Context

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

Other Diffs

Ignore

Blank Lines Whitespace: Expand:

Diff

1.8
 
1.9
 
MAIN:ragge:20050515210634
 
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         int infil;
 4343         usch *curptr;
 4444         usch *maxread;
 4545         usch *ostr;
 4646         usch *buffer;
 4747         usch bbuf[NAMEMAX+CPPBUF+1];
 4848 } *ifiles;
 4949 
 5050 usch *yyp, *yystr, yybuf[CPPBUF];
 5151 
 5252 int yylex(void);
 5353 int yywrap(void);
 5454 
 5555 static struct includ *
 5656 getbuf(usch *file)
 5757 {
 5858         struct includ *ic;
<> 59+
  60+        ic = calloc(sizeof(struct includ), 1);
  61+#if 0
5962         usch *ostr = stringbuf;
 6063 
 6164 //printf("getbuf1: stringbuf %p\n", stringbuf);
 6265         stringbuf = (usch *)ROUND((int)stringbuf);
 6366 //printf("getbuf2: stringbuf %p\n", stringbuf);
 6467         ic = (struct includ *)stringbuf;
 6568         stringbuf += sizeof(struct includ);
 6669         ic->ostr = ostr;
 6770 
 6871 //printf("getbuf3: stringbuf %p\n", stringbuf);
<> 72+#endif
6973         return ic;
 7074 }
 7175 
 7276 static void
 7377 putbuf(struct includ *ic)
 7478 {
<> 79+        free(ic);
  80+#if 0
7581 //printf("putbuf: stringbuf %p\n", stringbuf);
 7682 if (stringbuf < (usch *)&ic[1])
 7783 ;//     printf("ERROR!!!\n");
 7884 else
 7985         stringbuf = ic->ostr;
 8086 //printf("putbuf2: stringbuf %p\n", stringbuf);
<> 87+#endif
8188 }
 8289 
 8390 static int
 8491 input(void)
 8592 {
 8693         struct includ *ic;
 8794         int len;
 8895 
 8996         if (ifiles->curptr < ifiles->maxread) {
<>90 -//printf("c %d\n", *ifiles->curptr);
  97+if (*ifiles->curptr == 0)
  98+printf("c %d\n", *ifiles->curptr);
9199                 return *ifiles->curptr++;
 92100 }
 93101         if (ifiles->infil < 0) {
 94102                 ic = ifiles;
 95103                 ifiles = ifiles->next;
 96104                 putbuf(ic);
 97105                 return input();
 98106         }
 99107         if ((len = read(ifiles->infil, ifiles->buffer, CPPBUF)) < 0)
 100108                 error("read error on file %s", ifiles->fname);
 101109         if (len == 0)
 102110                 return -1;
 103111         ifiles->curptr = ifiles->buffer;
 104112         ifiles->maxread = ifiles->buffer + len;
 105113         return input();
 106114 }
 107115 
 108116 static void
 109117 unput(int c)
 110118 {
 111119         struct includ *ic;
 112120 
 113121 if (c == 0) {
 114122 printf("no;;\n");
 115123 }
 116124 
 117125         if (ifiles->curptr > ifiles->bbuf) {
 118126                 *--ifiles->curptr = c;
 119127         } else {
 120128                 ic = getbuf(NULL);
 121129                 ic->fname = ifiles->fname;
 122130                 ic->lineno = ifiles->lineno;
 123131                 ic->infil = -1;
 124132                 ic->curptr = &ic->bbuf[NAMEMAX+CPPBUF+1];
 125133                 ic->maxread = ic->curptr;
 126134                 ic->next = ifiles;
 127135                 ifiles = ic;
 128136                 *--ifiles->curptr = c;
 129137         }
 130138 //printf("unput %d\n", c);
 131139 }
 132140 
 133141 #define UNPUT(c) *--ifiles->curptr = c
 134142 #define ADJUST(x) { \
 135143         if (x > (ifiles->maxread-ifiles->curptr)) { \
 136144                 memcpy(
 137145 
 138146 static int
 139147 slofgetc(void)
 140148 {
 141149         int c;
 142150 
 143151 again:  switch (c = input()) {
 144152         case '\\': /* continued lines */
 145153                 if ((c = input()) == '\n') {
 146154                         ifiles->lineno++;
 147155                         putc('\n', obuf);
 148156                         goto again;
 149157                 }
 150158                 cunput(c);
 151159                 return '\\';
 152160         case '?': /* trigraphs */
 153161                 if ((c = input()) != '?') {
 154162                         cunput(c);
 155163                         return '?';
 156164                 }
 157165                 switch (c = input()) {
 158166                 case '=': c = '#'; break;
 159167                 case '(': c = '['; break;
 160168                 case ')': c = ']'; break;
 161169                 case '<': c = '{'; break;
 162170                 case '>': c = '}'; break;
 163171                 case '/': c = '\\'; break;
 164172                 case '\'': c = '^'; break;
 165173                 case '!': c = '|'; break;
 166174                 case '-': c = '~'; break;
 167175                 default:
 168176                         cunput(c);
 169177                         cunput('?');
 170178                         return '?';
 171179                 }
 172180                 cunput(c);
 173181                 goto again;
 174182         default:
 175183                 return c;
 176184         }
 177185 }
 178186 
 179187 int
 180188 yylex()
 181189 {
 182190         static int wasnl = 1;
 183191         int c, oc, rval;
 184192 
 185193 fast:   yystr = yybuf;
 186194         yyp = yystr;
 187195         c = input();
 188196         if (c != ' ' && c != '\t' && c != '#')
 189197                 wasnl = 0;
 190198 #define ONEMORE()       { *yyp++ = c; c = slofgetc(); }
 191199 again:  switch (c) {
 192200         case -1:
 193201                 rval = 0;
 194202                 break;
 195203 
 196204         case '\'': /* charcon */
 197205         case '"': /* string */
 198206 chstr:          oc = c;
<>199 -                if (slow == 0) {
 200 -                        do {
 201 -                                putch(c);
 202 -                                if (c == '\\')
 203 -                                        putch(slofgetc());
 204 -                        } while ((c = slofgetc()) != EOF && c != oc);
 205 -                        if (c == oc)
 206 -                                putch(c);
 207 -                        goto fast;
 208 -                } else {
 209 -                        do {
 210 -                                *yyp++ = c;
 211 -                                if (c == '\\')
 212 -                                        *yyp++ = slofgetc();
 213 -                        } while ((c = slofgetc()) != EOF && c != oc);
 214 -                        *yyp++ = c; *yyp = 0;
 215 -                }
  207+                do {
  208+                        *yyp++ = c;
  209+                        if (c == '\\')
  210+                                *yyp++ = slofgetc();
  211+                } while ((c = slofgetc()) != EOF && c != oc);
  212+                *yyp++ = c; *yyp = 0;
216213                 rval = oc == '"' ? STRING : CHARCON;
 217214                 break;
 218215 
 219216         case '0': case '1': case '2': case '3': case '4':
 220217         case '5': case '6': case '7': case '8': case '9':
 221218                 *yyp++ = c;
 222219                 c = slofgetc();
 223220                 if (yyp[-1] == '0' && (c == 'x' || c == 'X')) {
 224221                         do {
 225222                                 ONEMORE();
 226223                         } while (isxdigit(c));
 227224                 } else {
 228225                         while (isdigit(c))
 229226                                 ONEMORE();
 230227                 }
 231228                 if (c != '.' && c != 'e' && c != 'E') {
 232229                         /* not floating point number */
 233230                         while (c == 'l' || c == 'L' || c == 'u' || c == 'U') {
 234231                                 ONEMORE();
 235232                         }
 236233                         cunput(c);
 237234                         *yyp = 0;
 238235                         rval = NUMBER;
 239236                         break;
 240237                 }
 241238                 /* it's a floating point number here */
 242239                 if (c == '.') { /* decimal point */
 243240 F:                      do { /* may be followed by digits */
 244241                                 ONEMORE();
 245242                         } while (isdigit(c));
 246243                         if (c == 'e' || c == 'E') {
 247244 E:                              ONEMORE();
 248245                                 if (c == '-' || c == '+') {
 249246                                         ONEMORE();
 250247                                 }
 251248                                 while (isdigit(c))
 252249                                         ONEMORE();
 253250                         }
 254251                         if (c == 'f' || c == 'F' || c == 'l' || c == 'L')
 255252                                 ONEMORE();
 256253                         cunput(c);
 257254                         *yyp = 0;
 258255                         rval = FPOINT;
 259256                         break;
 260257                 } else
 261258                         goto E;
 262259 
 263260         case '.':
 264261                 ONEMORE();
 265262                 if (isdigit(c))
 266263                         goto F;
<>267 -                if (!slow) {
 268 -                        UNPUT(c);
 269 -                        putch('.');
 270 -                        goto fast;
 271 -                }
272264                 if (c == '.') {
 273265                         ONEMORE();
 274266                         if (c == '.') {
 275267                                 *yyp++ = '.'; *yyp = 0;
 276268                                 rval = ELLIPS;
 277269                                 break;
 278270                         }
 279271                         cunput(c);
 280272                         cunput('.');
 281273                         *--yyp = 0;
 282274                         rval = '.';
 283275                         break;
 284276                 }
 285277                 cunput(c);
 286278                 *yyp = 0;
 287279                 rval = '.';
 288280                 break;
 289281 
 290282         case '\\':
 291283                 c = input();
 292284                 if (c == '\n') {
 293285                         ifiles->lineno++;
 294286                         putch('\n');
 295287                         goto fast;
 296288                 }
<>297 -                if (!slow) {
 298 -                        putch('\\');
 299 -                        goto again;
 300 -                }
301289                 UNPUT(c);
 302290                 *yyp++ = '\\'; *yyp = 0;
 303291                 rval = '\\';
 304292                 break;
 305293                 
 306294         case '\n':
 307295                 wasnl = 1;
 308296                 ifiles->lineno++;
<> 297+                *yyp++ = '\n'; *yyp = 0;
309298                 rval = NL;
<>310 -                if (slow)
 311 -                        break;
 312 -                if (flslvl == 0) {
 313 -                        if (curline() == 1)
 314 -                                prtline();
 315 -                        else
 316 -                                putch('\n');
 317 -                }
 318 -                goto fast;
  299+                break;
319300 
 320301         case '#':
 321302                 if (wasnl) {
 322303                         wasnl = 0;
 323304                         rval = CONTROL;
 324305                         break;
 325306                 }
<>326 -                if (!slow) {
 327 -                        putch('#');
 328 -                        goto fast;
 329 -                }
330307                 *yyp++ = c;
 331308                 c = input();
 332309                 if (c == '#') {
 333310                         *yyp++ = c;
 334311                         *yyp = 0;
 335312                         rval = CONCAT;
 336313                 } else {
 337314                         unput(c);
 338315                         *yyp = 0;
 339316                         rval = MKSTR;
 340317                 }
 341318                 break;
 342319 
 343320         case ' ':
 344321         case '\t': /* whitespace */
 345322                 do {
 346323                         *yyp++ = c;
 347324                         c = input();
 348325                 } while (c == ' ' || c == '\t');
 349326                 if (wasnl && c == '#') {
 350327                         wasnl = 0;
 351328                         rval = CONTROL;
 352329                 } else {
 353330                         unput(c);
 354331                         *yyp = 0;
 355332                         rval = WSPACE;
 356333                 }
 357334                 break;
 358335 
 359336         case '/':
 360337                 if ((c = slofgetc()) == '/') {
 361338                         if (Cflag)
 362339                                 fprintf(obuf, "//");
 363340                         while ((c = slofgetc()) && c != '\n')
 364341                                 if (Cflag)
 365342                                         putc(c, obuf);
 366343                         goto again;
 367344                 } else if (c == '*') {
 368345                         if (Cflag)
 369346                                 fprintf(obuf, "/*");
 370347                         oc = 0;
 371348                         do {
 372349                                 while ((c = slofgetc()) && c != '*') {
 373350                                         if (c == '\n') {
 374351                                                 putc(c, obuf);
 375352                                                 ifiles->lineno++;
 376353                                         } else if (Cflag)
 377354                                                 putc(c, obuf);
 378355                                 }
 379356                                 if (Cflag)
 380357                                         putc(c, obuf);
 381358                                 if ((c = slofgetc()) == '/')
 382359                                         break;
 383360                                 unput(c);
 384361                         } while (c);
 385362                         if (Cflag)
 386363                                 putc(c, obuf);
 387364                         if (tflag) {
 388365                                 rval = yylex();
 389366                         } else {
 390367                                 *yyp++ = ' '; *yyp = 0;
 391368                                 rval = WSPACE;
 392369                         }
 393370                 } else {
 394371                         unput(c);
 395372                         *yyp++ = '/'; *yyp = 0;
 396373                         rval = '/';
 397374                 }
 398375                 break;
 399376 
 400377         case 'L': /* may be STRING, CHARCON or identifier */
 401378                 *yyp++ = c;
 402379                 if ((c = slofgetc()) == '"' || c == '\'')
 403380                         goto chstr;
 404381 gotid:          while (isalnum(c) || c == '_') {
 405382                         *yyp++ = c;
 406383                         c = slofgetc();
 407384                 }
 408385                 *yyp = 0;
 409386                 unput(c);
 410387                 rval = IDENT;
 411388                 break;
 412389 
 413390         default:
 414391                 if (isalpha(c) || c == '_')
 415392                         goto gotid;
<>416 -                if (!slow && c > 5) {
 417 -                        putch(c);
 418 -                        goto fast;
 419 -                }
420393                 yystr[0] = c; yystr[1] = 0;
 421394                 rval = c;
 422395                 break;
 423396         }
 424397         return rval;
 425398 }
 426399 
 427400 /*
 428401  * A new file included.
 429402  * If ifiles == NULL, this is the first file and already opened (stdin).
 430403  * Return 0 on success, -1 on failure to open file.
 431404  */
 432405 int
 433406 pushfile(char *file)
 434407 {
 435408         struct includ ibuf;
 436409         struct includ *old;
 437410         struct includ *ic;
 438411         int otrulvl = trulvl, oflslvl = flslvl;
 439412 
 440413         ic = &ibuf;
<> 414+        memset(ic, 0, sizeof(struct includ));
441415         old = ifiles;
<> 416+//printf("ifiles %p ic %p\n", ifiles, ic);
442417 
<>443 -        slow = 0;
444418         if (file != NULL) {
 445419                 if ((ic->infil = open(file, O_RDONLY)) < 0)
 446420                         return -1;
 447421                 ic->fname = file;
 448422         } else {
 449423                 ic->infil = 0;
 450424                 ic->fname = "<stdin>";
 451425         }
 452426         ic->buffer = ic->bbuf+NAMEMAX;
 453427         ic->curptr = ic->buffer;
 454428         ifiles = ic;
 455429         ic->lineno = 0;
 456430         ic->maxread = ic->curptr;
 457431         unput('\n');
 458432 
 459433         mainscan();
 460434 
 461435         if (trulvl != otrulvl || flslvl != oflslvl)
 462436                 error("unterminated conditional");
 463437 
 464438         ifiles = old;
<> 439+//printf("ifiles2 %p\n", ifiles);
<_465440         close(ic->infil);
 466441         return 0;
 467442 }
 468443 
 469444 /*
 470445  * Print current position to output file.
 471446  */
 472447 void
 473448 prtline()
 474449 {
 475450         fprintf(obuf, "# %d \"%s\"\n", ifiles->lineno, ifiles->fname);
 476451 }
 477452 
 478453 void
 479454 cunput(int c)
 480455 {
 481456 extern int dflag;
 482457 if (dflag)printf(": '%c'(%d)", c, c);
 483458         unput(c);
 484459 }
 485460 
 486461 void
 487462 setline(int line)
 488463 {
 489464         if (ifiles)
 490465                 ifiles->lineno = line-1;
 491466 }
 492467 
 493468 void
 494469 setfile(char *name)
 495470 {
 496471         if (ifiles)
 497472                 ifiles->fname = strdup(name);
 498473 }
 499474 
 500475 int
 501476 curline()
 502477 {
 503478         return ifiles ? ifiles->lineno : 0;
 504479 }
 505480 
 506481 char *
 507482 curfile()
 508483 {
 509484         return ifiles ? ifiles->fname : "";
 510485 }
FishEye: Open Source License registered to PCC.
Your maintenance has expired. You can renew your license at http://www.atlassian.com/fisheye/renew
Atlassian FishEye, CVS analysis. (Version:1.6.3 Build:build-336 2008-11-04) - Administration - Page generated 2014-09-03 06:51 +0200