Quick Search:

Mode

Context

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

Other Diffs

Ignore

Blank Lines Whitespace: Expand:

Diff

1.1
 
1.2
 
MAIN:ragge:20050319093405
 
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>
<> 32+#include <unistd.h>
  33+#include <fcntl.h>
3234 
 3335 #include "cpp.h"
 3436 
 3537 /* definition for include file info */
 3638 struct includ {
 3739         struct includ *next;
 3840         char *fname;
 3941         int lineno;
<> 42+#ifdef NEWBUF
  43+        int infil;
  44+        usch *inpbuf;
  45+        usch *curptr;
  46+        int curlen;
  47+#else
4048         FILE *ifil;
<> 49+#endif
4150 } *ifiles;
 4251 
 4352 usch *yyp, yystr[CPPBUF];
<> 53+#if 0
  54+usch inpbuf[NAMEMAX+CPPBUF], *curimp, *imptop;
  55+#endif
4456 
 4557 int yylex(void);
<>46 -int nyylex(void);
4758 int yywrap(void);
<>48 -#if 0
4959 
<>50 -"\n"                    { return nyylex(); }
 51 -.                       { return nyylex(); }
  60+#ifdef NEWBUF
  61+static int
  62+input(void)
  63+{
  64+        if (ifiles->curptr < ifiles->inpbuf+ifiles->curlen)
  65+                return *ifiles->curptr++;
  66+        if ((ifiles->curlen = read(ifiles->infil, ifiles->inpbuf, CPPBUF)) < 0)
  67+                error("read error on file %s", ifiles->fname);
  68+        ifiles->curptr = ifiles->inpbuf;
  69+        return input();
  70+}
5271 
<>53 -#endif
 54 -
  72+static void
  73+unput(int c)
  74+{
  75+        if (ifiles->curptr > ifiles->inpbuf)
  76+                *--ifiles->curptr = c;
  77+        else
  78+                error("out of pushback space");
  79+}
  80+#else
5581 #define input() fgetc(ifiles->ifil)
 5682 #define unput(c) ungetc(c, ifiles->ifil)
<> 83+#endif
5784 static int
 5885 slofgetc(void)
 5986 {
 6087         int c;
 6188 
 6289 again:  switch (c = input()) {
 6390         case '\\': /* continued lines */
 6491                 if ((c = input()) == '\n') {
 6592                         ifiles->lineno++;
 6693                         putc('\n', obuf);
 6794                         goto again;
 6895                 }
 6996                 cunput(c);
 7097                 return '\\';
 7198         case '?': /* trigraphs */
 7299                 if ((c = input()) != '?') {
 73100                         cunput(c);
 74101                         return '?';
 75102                 }
 76103                 switch (c = input()) {
 77104                 case '=': c = '#'; break;
 78105                 case '(': c = '['; break;
 79106                 case ')': c = ']'; break;
 80107                 case '<': c = '{'; break;
 81108                 case '>': c = '}'; break;
 82109                 case '/': c = '\\'; break;
 83110                 case '\'': c = '^'; break;
 84111                 case '!': c = '|'; break;
 85112                 case '-': c = '~'; break;
 86113                 default:
 87114                         cunput(c);
 88115                         cunput('?');
 89116                         return '?';
 90117                 }
 91118                 cunput(c);
 92119                 goto again;
 93120         default:
 94121                 return c;
 95122         }
 96123 }
 97124 
<>98 -int yylex() { return nyylex(); }
 99 -
100125 int
<>101 -nyylex()
  126+yylex()
102127 {
 103128         static int wasnl = 1;
 104129         int c, oc, rval;
 105130 
 106131         yyp = yystr;
 107132         c = input();
 108133         if (c != ' ' && c != '\t' && c != '#')
 109134                 wasnl = 0;
 110135 #define ONEMORE()       { *yyp++ = c; c = slofgetc(); }
 111136 again:  switch (c) {
 112137         case -1:
<>113 -                rval = yywrap() ? 0 : nyylex();
  138+                rval = yywrap() ? 0 : yylex();
114139                 break;
 115140 
 116141         case '\'': /* charcon */
 117142         case '"': /* string */
 118143 chstr:          oc = c;
 119144                 do {
 120145                         *yyp++ = c;
 121146                         if (c == '\\')
 122147                                 *yyp++ = slofgetc();
 123148                 } while ((c = slofgetc()) != EOF && c != oc);
 124149                 *yyp++ = c; *yyp = 0;
 125150                 rval = oc == '"' ? STRING : CHARCON;
 126151                 break;
 127152 
 128153         case '0': case '1': case '2': case '3': case '4':
 129154         case '5': case '6': case '7': case '8': case '9':
 130155                 *yyp++ = c;
 131156                 c = slofgetc();
 132157                 if (yyp[-1] == '0' && (c == 'x' || c == 'X')) {
 133158                         do {
 134159                                 ONEMORE();
 135160                         } while (isxdigit(c));
 136161                 } else {
 137162                         while (isdigit(c))
 138163                                 ONEMORE();
 139164                 }
 140165                 if (c != '.' && c != 'e' && c != 'E') {
 141166                         /* not floating point number */
 142167                         while (c == 'l' || c == 'L' || c == 'u' || c == 'U') {
 143168                                 ONEMORE();
 144169                         }
 145170                         cunput(c);
 146171                         *yyp = 0;
 147172                         rval = NUMBER;
 148173                         break;
 149174                 }
 150175                 /* it's a floating point number here */
 151176                 if (c == '.') { /* decimal point */
 152177 F:                      do { /* may be followed by digits */
 153178                                 ONEMORE();
 154179                         } while (isdigit(c));
 155180                         if (c == 'e' || c == 'E') {
 156181 E:                              ONEMORE();
 157182                                 if (c == '-' || c == '+') {
 158183                                         ONEMORE();
 159184                                 }
 160185                                 while (isdigit(c))
 161186                                         ONEMORE();
 162187                         }
 163188                         if (c == 'f' || c == 'F' || c == 'l' || c == 'L')
 164189                                 ONEMORE();
 165190                         cunput(c);
 166191                         *yyp = 0;
 167192                         rval = FPOINT;
 168193                         break;
 169194                 } else
 170195                         goto E;
 171196 
 172197         case '.':
 173198                 ONEMORE();
 174199                 if (isdigit(c))
 175200                         goto F;
 176201                 if (c == '.') {
 177202                         ONEMORE();
 178203                         if (c == '.') {
 179204                                 *yyp++ = '.'; *yyp = 0;
 180205                                 rval = ELLIPS;
 181206                                 break;
 182207                         }
 183208                         cunput(c);
 184209                         cunput('.');
 185210                         *--yyp = 0;
 186211                         rval = '.';
 187212                         break;
 188213                 }
 189214                 cunput(c);
 190215                 *yyp = 0;
 191216                 rval = '.';
 192217                 break;
 193218 
 194219         case '\\':
 195220                 c = input();
 196221                 if (c == '\n') {
 197222                         ifiles->lineno++;
 198223                         putc('\n', obuf);
 199224                         c = input();
 200225                         goto again;
 201226                 } else {
 202227                         cunput(c);
 203228                         *yyp++ = '\\'; *yyp = 0;
 204229                         rval = '\\';
 205230                 }
 206231                 break;
 207232                 
 208233         case '\n':
 209234                 wasnl = 1;
 210235                 ifiles->lineno++;
 211236                 *yyp++ = c; *yyp = 0;
 212237                 rval = NL;
 213238                 break;
 214239 
 215240         case '#':
 216241                 if (wasnl) {
 217242                         wasnl = 0;
 218243                         rval = CONTROL;
 219244                         break;
 220245                 }
 221246                 *yyp++ = c;
 222247                 c = input();
 223248                 if (c == '#') {
 224249                         *yyp++ = c;
 225250                         *yyp = 0;
 226251                         rval = CONCAT;
 227252                 } else {
 228253                         unput(c);
 229254                         *yyp = 0;
 230255                         rval = MKSTR;
 231256                 }
 232257                 break;
 233258 
 234259         case ' ':
 235260         case '\t': /* whitespace */
 236261                 do {
 237262                         *yyp++ = c;
 238263                         c = input();
 239264                 } while (c == ' ' || c == '\t');
 240265                 if (wasnl && c == '#') {
 241266                         wasnl = 0;
 242267                         rval = CONTROL;
 243268                 } else {
 244269                         unput(c);
 245270                         *yyp = 0;
 246271                         rval = WSPACE;
 247272                 }
 248273                 break;
 249274 
 250275         case '/':
 251276                 if ((c = slofgetc()) == '/') {
 252277                         if (Cflag)
 253278                                 fprintf(obuf, "//");
 254279                         while ((c = slofgetc()) && c != '\n')
 255280                                 if (Cflag)
 256281                                         putc(c, obuf);
 257282                         *yyp++ = c; *yyp = 0;
 258283                         rval = c;
 259284                 } else if (c == '*') {
 260285                         if (Cflag)
 261286                                 fprintf(obuf, "/*");
 262287                         oc = 0;
 263288                         do {
 264289                                 while ((c = slofgetc()) && c != '*') {
 265290                                         if (c == '\n') {
 266291                                                 putc(c, obuf);
 267292                                                 ifiles->lineno++;
 268293                                         } else if (Cflag)
 269294                                                 putc(c, obuf);
 270295                                 }
 271296                                 if (Cflag)
 272297                                         putc(c, obuf);
 273298                                 if ((c = slofgetc()) == '/')
 274299                                         break;
 275300                                 unput(c);
 276301                         } while (c);
 277302                         if (Cflag)
 278303                                 putc(c, obuf);
 279304                         if (tflag) {
<>280 -                                rval = nyylex();
  305+                                rval = yylex();
281306                         } else {
 282307                                 *yyp++ = ' '; *yyp = 0;
 283308                                 rval = WSPACE;
 284309                         }
 285310                 } else {
 286311                         unput(c);
 287312                         *yyp++ = '/'; *yyp = 0;
 288313                         rval = '/';
 289314                 }
 290315                 break;
 291316 
 292317         case 'L': /* may be STRING, CHARCON or identifier */
 293318                 *yyp++ = c;
 294319                 if ((c = slofgetc()) == '"' || c == '\'')
 295320                         goto chstr;
 296321 gotid:          while (isalnum(c) || c == '_') {
 297322                         *yyp++ = c;
 298323                         c = slofgetc();
 299324                 }
 300325                 *yyp = 0;
 301326                 unput(c);
 302327                 rval = IDENT;
 303328                 break;
 304329 
 305330         default:
 306331                 if (isalpha(c) || c == '_')
 307332                         goto gotid;
 308333                 yystr[0] = c; yystr[1] = 0;
 309334                 rval = c;
 310335                 break;
 311336         }
 312337         return rval;
 313338 }
 314339 
 315340 /*
 316341  * A new file included.
 317342  * If ifiles == NULL, this is the first file and already opened (stdin).
 318343  * Return 0 on success, -1 on failure to open file.
 319344  */
 320345 int
 321346 pushfile(char *file)
 322347 {
 323348         struct includ *ic;
 324349 
 325350         ic = malloc(sizeof(struct includ));
 326351         ic->fname = strdup(file);
 327352         ic->lineno = 1;
 328353         if (ifiles != NULL) {
<> 354+#ifdef NEWBUF
  355+                if ((ic->infil = open(file, O_RDONLY)) < 0)
  356+                        return -1;
  357+#else
329358                 if ((ic->ifil = fopen(file, "r")) == NULL)
 330359                         return -1;
<> 360+#endif
331361         } else
<> 362+#ifdef NEWBUF
  363+                ic->infil = 0;
  364+#else
332365                 ic->ifil = stdin;
<> 366+#endif
  367+#ifdef NEWBUF
  368+        ic->curptr = ic->inpbuf = malloc(CPPBUF);
  369+#endif
333370         ic->next = ifiles;
 334371         ifiles = ic;
 335372 
 336373         return 0;
 337374 }
 338375 
 339376 /*
 340377  * End of included file (or everything).
 341378  */
 342379 void
 343380 popfile()
 344381 {
 345382         struct includ *ic;
 346383 
 347384         ic = ifiles;
 348385         ifiles = ifiles->next;
<> 386+#ifdef NEWBUF
  387+        close(ic->infil);
  388+#else
349389         fclose(ic->ifil);
<> 390+#endif
<_350391         free(ic->fname);
 351392         free(ic);
 352393         prtline();
 353394 }
 354395 
 355396 /*
 356397  * Print current position to output file.
 357398  */
 358399 void
 359400 prtline()
 360401 {
 361402         fprintf(obuf, "# %d \"%s\"\n", ifiles->lineno, ifiles->fname);
 362403 }
 363404 
 364405 void
 365406 cunput(int c)
 366407 {
 367408 extern int dflag;
 368409 if (dflag)printf(": '%c'(%d)", c, c);
 369410         unput(c);
 370411 }
 371412 
 372413 int
 373414 yywrap()
 374415 {
 375416         if (ifiles->next == 0)
 376417                 return 1;
 377418         popfile();
 378419         return 0;
 379420 }
 380421 
 381422 void
 382423 setline(int line)
 383424 {
 384425         if (ifiles)
 385426                 ifiles->lineno = line-1;
 386427 }
 387428 
 388429 void
 389430 setfile(char *name)
 390431 {
 391432         if (ifiles)
 392433                 free(ifiles->fname), ifiles->fname = strdup(name);
 393434 }
 394435 
 395436 int
 396437 curline()
 397438 {
 398439         return ifiles ? ifiles->lineno : 0;
 399440 }
 400441 
 401442 char *
 402443 curfile()
 403444 {
 404445         return ifiles ? ifiles->fname : "";
 405446 }
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-29 16:04 +0200