Quick Search:

Mode

Context

Displaying 3 lines of context. None | Less | More | Full

Other Diffs

Ignore

Blank Lines Whitespace: Expand:

Diff

1.9
 
1.10
 
MAIN:ragge:20040828131747
 
cpp.c
_>7878 
 7979 #include "cpp.h"
 8080 
<>81 -typedef unsigned char usch;
 82 -
8381 #define MAXARG  250     /* # of args to a macro, limited by char value */
 8482 #define SBSIZE  2000
 8583 #define SYMSIZ  200
     
 !
9694 #ifdef CPP_DEBUG
 9795 int dflag;      /* debug printouts */
 9896 #endif
<>99 -char *sysinc = "/usr/include"; /* default header files */
10097 FILE *obuf;
 10198 static int exfail;
<>102 -static struct symtab {
 103 -        usch *namep;
 104 -        char *value;
 105 -} symtab[SYMSIZ];
  99+struct symtab symtab[SYMSIZ];
106100 
 107101 /* avoid recursion */
 108102 struct recur {
 109103         struct recur *next;
 110104         struct symtab *sp;
 111105 };
 112106 
<> 107+/* include dirs */
  108+struct incs {
  109+        struct incs *next;
  110+        char *dir;
  111+} *incdir[2];
  112+#define INCINC 0
  113+#define SYSINC 1
  114+
113115 static struct symtab *filloc;
 114116 static struct symtab *linloc;
 115117 static struct symtab *datloc;
 116118 static struct symtab *timloc;
<>117 -static int      trulvl;
 118 -static int      flslvl;
 119 -static usch *stringbuf = sbf;
  119+int     trulvl;
  120+int     flslvl;
  121+int     elflvl;
  122+int     elslvl;
  123+usch *stringbuf = sbf;
120124 
 121125 /*
 122126  * Macro replacement list syntax:
     
 !
143147 #define FORGET  3
 144148 
 145149 static void expdef(usch *proto, struct recur *, int gotwarn);
<>146 -static int subst(char *, struct symtab *, struct recur *);
147150 static void savch(int c);
 148151 static void insym(struct symtab **sp, char *namep);
<>149 -static struct symtab *lookup(char *namep, int enterf);
150152 static void control(void);
 151153 static usch *savstr(usch *str);
 152154 static void define(void);
 153155 static void expmac(struct recur *);
 154156 static int canexpand(struct recur *, struct symtab *np);
<>155 -static void unpstr(usch *);
156157 static void include(void);
 157158 static void line(void);
 158159 
 159160 int
 160161 main(int argc, char **argv)
 161162 {
<> 163+        struct incs *w, *w2;
162164         struct symtab *nl, *thisnl;
 163165         register int c, gotspc, ch;
 164166         usch *osp;
     
 !
170172                         while (*osp && *osp != '=')
 171173                                 osp++;
 172174                         if (*osp == '=') {
<>173 -                                *osp = 0;
  175+                                *osp++ = 0;
  176+                                while (*osp)
  177+                                        osp++;
  178+                                *osp = OBJCT;
174179                         } else {
<>175 -                                static char c[3] = { 0, '1', 0 };
 176 -                                osp = c;
  180+                                static char c[3] = { 0, '1', OBJCT };
  181+                                osp = &c[2];
177182                         }
<>178 -                        osp++;
 179 -                        while (*osp)
 180 -                                osp++;
 181 -                        *osp = OBJCT;
182183                         nl = lookup(optarg, ENTER);
 183184                         if (nl->value)
 184185                                 error("%s redefined", optarg);
 185186                         nl->value = osp;
 186187                         break;
 187188 
 188189                 case 'S':
<>189 -                        sysinc = optarg;
  190+                case 'I':
  191+                        w = calloc(sizeof(struct incs), 1);
  192+                        w->dir = optarg;
  193+                        w2 = incdir[ch == 'I' ? INCINC : SYSINC];
  194+                        if (w2 != NULL) {
  195+                                while (w2->next)
  196+                                        w2 = w2->next;
  197+                                w2->next = w;
  198+                        } else
  199+                                incdir[ch == 'I' ? INCINC : SYSINC] = w;
190200                         break;
 191201 
 192202                 case 'U':
     
 !
217227                         exit(8);
 218228                 }
 219229         }
<>220 -        pushfile(argc ? argv[0] : "<stdin>");
  230+        if (pushfile(argc ? argv[0] : "<stdin>"))
  231+                error("cannot open %s", argv[0]);
221232 
 222233         if (argc == 2) {
 223234                 if ((obuf = fopen(argv[1], "w")) == 0) {
     
 !
241252                         control();
 242253                         break;
 243254 
<>244 -                case BEGCOM:
 245 -                case ENDCOM:
 246 -                case LINECOM:
 247 -                        error("special: %d\n", c);
 248 -
249255                 case IDENT:
 250256                         if (flslvl)
 251257                                 break;
     
 !
324330                 return;
 325331         } else if (CHECK(else)) {
 326332                 if (flslvl) {
<>327 -                        if (--flslvl!=0) {
  333+                        if (elflvl > trulvl)
  334+                                ;
  335+                        else if (--flslvl!=0) {
328336                                 flslvl++;
 329337                         } else {
 330338                                 trulvl++;
     
 !
335343                         trulvl--;
 336344                 } else
 337345                         error("If-less else");
<> 346+                if (elslvl==trulvl+flslvl) error("Too many else");
  347+                elslvl=trulvl+flslvl;
338348         } else if (CHECK(endif)) {
 339349                 if (flslvl) {
 340350                         flslvl--;
     
 !
344354                         trulvl--;
 345355                 else
 346356                         error("If-less endif");
<> 357+                if (flslvl == 0)
  358+                        elflvl = 0;
  359+                elslvl = 0;
347360         } else if (CHECK(error)) {
 348361                 usch *ch = stringbuf;
 349362                 while (yylex() != NL)
     
 !
352365                 error("error: %s", ch);
 353366 #define GETID() if (yylex() != WSPACE || yylex() != IDENT) goto cfail
 354367         } else if (CHECK(define)) {
<> 368+                if (flslvl)
  369+                        goto exit;
355370                 GETID();
 356371                 define();
 357372         } else if (CHECK(ifdef)) {
     
 !
371386                 if (flslvl == 0 && (np = lookup(yytext, FIND)))
 372387                         np->value = 0;
 373388         } else if (CHECK(line)) {
<> 389+                if (flslvl)
  390+                        goto exit;
374391                 line();
<> 392+        } else if (CHECK(if)) {
  393+                if (flslvl==0 && yyparse())
  394+                        ++trulvl;
  395+                else
  396+                        ++flslvl;
  397+        } else if (CHECK(elif)) {
  398+                if (flslvl == 0)
  399+                        elflvl = trulvl;
  400+                if (flslvl) {
  401+                        if (elflvl > trulvl)
  402+                                ;
  403+                        else if (--flslvl!=0)
  404+                                ++flslvl;
  405+                        else {
  406+                                if (yyparse()) {
  407+                                        ++trulvl;
  408+                                        prtline();
  409+                                } else
  410+                                        ++flslvl;
  411+                        }
  412+                } else if (trulvl) {
  413+                        ++flslvl;
  414+                        --trulvl;
  415+                } else
  416+                        error("If-less elif");
375417         } else
 376418                 error("undefined control '%s'", yytext);
 377419 
     
 !
383425 exit:
 384426         while (yylex() != NL)
 385427                 ;
<> 428+        putc('\n', obuf);
386429 #undef CHECK
 387430 }
 388431 
     
 !
423466 bad:    error("bad line directive");
 424467 }
 425468 
<> 469+/*
  470+ * Include a file. Include order:
  471+ * - if name inside <>, only search system includes.
  472+ * - if name inside "", first search current dir, then -I dirs,
  473+ *   then system includes.
  474+ */
426475 void
 427476 include()
 428477 {
<> 478+        struct incs *w;
429479         struct symtab *nl;
<>430 -        usch *osp, *instr;
 431 -        int c;
  480+        usch *osp;
  481+        char *fn;
  482+        int i, c, it;
432483 
<> 484+        osp = stringbuf;
433485         if (yylex() != WSPACE)
 434486                 goto bad;
<>435 -        if ((c = yylex()) != STRING && c != '<' && c != IDENT)
  487+again:  if ((c = yylex()) != STRING && c != '<' && c != IDENT)
436488                 goto bad;
 437489 
 438490         if (c == IDENT) {
<>439 -                instr = osp = stringbuf;
440491                 if ((nl = lookup(yytext, FIND)) == NULL)
 441492                         goto bad;
 442493                 if (subst(yytext, nl, NULL) == 0)
 443494                         goto bad;
 444495                 savch('\0');
<> 496+                unpstr(osp);
  497+                goto again;
  498+        } else if (c == '<') {
  499+                fn = stringbuf;
  500+                while ((c = yylex()) != '>' && c != NL) {
  501+                        if (c == NL)
  502+                                goto bad;
  503+                        savstr(yytext);
  504+                }
  505+                savch('\0');
  506+                it = SYSINC;
445507         } else {
 446508                 yytext[strlen(yytext)-1] = 0;
<>447 -                instr = &yytext[1];
  509+                fn = &yytext[1];
  510+                it = INCINC;
448511         }
<>449 -        pushfile(instr);
 450 -        if (c == IDENT)
 451 -                stringbuf = osp;
  512+
  513+        /* create search path and try to open file */
  514+        for (i = it; i < 2; i++) {
  515+                for (w = incdir[i]; w; w = w->next) {
  516+                        usch *nm = stringbuf;
  517+
  518+                        savstr(w->dir); savch('/');
  519+                        savstr(fn); savch(0);
  520+                        if (pushfile(nm) == 0)
  521+                                goto ret;
  522+                        stringbuf = nm;
  523+                }
  524+        }
  525+        error("cannot find '%s'", fn);
  526+        stringbuf = osp;
452527         return;
 453528 
 454529 bad:    error("bad include");
<> 530+ret:    prtline();
  531+        stringbuf = osp;
<_455532 }
 456533 
 457534 void
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-31 20:10 +0100