Print this page
cpp: don't handroll an incorrect strtoul(3c), use the one in libc
The previous implementation would parse 0x7ff as 0x755 (etc).


  63  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  64  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  65  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  66  * POSSIBILITY OF SUCH DAMAGE.
  67  */
  68 
  69 #include "cpp.h"
  70 
  71 /*
  72  * XXX This block should be moved to cpp.h, it is douplicated in cpp.c
  73  */
  74 #define isid(a)  ((fastab+COFF)[(int)a]&IB)
  75 #define IB 1
  76 /*      #if '\377' < 0               it would be nice if this worked properly!!!!! */
  77 #if pdp11 | vax | '\377' < 0
  78 #define COFF 128
  79 #else
  80 #define COFF 0
  81 #endif
  82 
  83 static  int     tobinary(char *, int);
  84 
  85 int
  86 yylex(void)
  87 {
  88         static int ifdef=0;
  89         static char *op2[]={"||",  "&&" , ">>", "<<", ">=", "<=", "!=", "=="};
  90         static int  val2[]={OROR, ANDAND,  RS,   LS,   GE,   LE,   NE,   EQ};
  91         static char *opc="b\bt\tn\nf\fr\r\\\\";
  92         extern char fastab[];
  93         extern char *outp,*inp,*newp; extern int flslvl;
  94         register char savc, *s;
  95         int val;
  96         register char **p2;
  97         struct symtab *sp;
  98 
  99 for (;;) {
 100         newp=skipbl(newp);
 101         if (*inp=='\n') return(stop);   /* end of #if */
 102         savc= *newp; *newp='\0';
 103         for (p2=op2+8; --p2>=op2; )  /* check 2-char ops */


 118                 }
 119         } else  if (*inp=='\'') {/* character constant */
 120                 val=number;
 121                 if (inp[1]=='\\') {/* escaped */
 122                         char c; if (newp[-1]=='\'') newp[-1]='\0';
 123                         s=opc;
 124                         while (*s) if (*s++!=inp[2]) ++s; else {yylval= *s; goto ret;}
 125                         if (inp[2]<='9' && inp[2]>='0') yylval=c=tobinary(inp+2,8);
 126                         else yylval=inp[2];
 127                 } else yylval=inp[1];
 128         } else if (0==strcmp("\\\n",inp)) {*newp=savc; continue;}
 129         else {
 130                 *newp=savc; pperror("Illegal character %c in preprocessor if", *inp);
 131                 continue;
 132         }
 133 ret:
 134         *newp=savc; outp=inp=newp; return(val);
 135 }
 136 }
 137 
 138 static int
 139 tobinary(st, b)
 140         char    *st;
 141         int     b;
 142 {
 143         int n, c, t;
 144         char *s;
 145         n=0;
 146         s=st;
 147         while ((c = *s++) != '\0') {
 148         switch(c) {
 149                 case '0': case '1': case '2': case '3': case '4': 
 150                 case '5': case '6': case '7': case '8': case '9': 
 151                         t = c-'0'; break;
 152                 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': 
 153                         t = c-'a'; if (b>10) break;
 154                 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': 
 155                         t = c - 'A'; if (b>10) break;
 156                 default:
 157                         t = -1;
 158                         if ( c=='l' || c=='L') if (*s=='\0') break;
 159                         pperror("Illegal number %s", st);
 160         }
 161         if (t<0) break;
 162         n = n*b+t;
 163         }
 164 return(n);
 165 }


  63  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  64  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  65  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  66  * POSSIBILITY OF SUCH DAMAGE.
  67  */
  68 
  69 #include "cpp.h"
  70 
  71 /*
  72  * XXX This block should be moved to cpp.h, it is douplicated in cpp.c
  73  */
  74 #define isid(a)  ((fastab+COFF)[(int)a]&IB)
  75 #define IB 1
  76 /*      #if '\377' < 0               it would be nice if this worked properly!!!!! */
  77 #if pdp11 | vax | '\377' < 0
  78 #define COFF 128
  79 #else
  80 #define COFF 0
  81 #endif
  82 
  83 static  long    tobinary(char *, int);
  84 
  85 int
  86 yylex(void)
  87 {
  88         static int ifdef=0;
  89         static char *op2[]={"||",  "&&" , ">>", "<<", ">=", "<=", "!=", "=="};
  90         static int  val2[]={OROR, ANDAND,  RS,   LS,   GE,   LE,   NE,   EQ};
  91         static char *opc="b\bt\tn\nf\fr\r\\\\";
  92         extern char fastab[];
  93         extern char *outp,*inp,*newp; extern int flslvl;
  94         register char savc, *s;
  95         int val;
  96         register char **p2;
  97         struct symtab *sp;
  98 
  99 for (;;) {
 100         newp=skipbl(newp);
 101         if (*inp=='\n') return(stop);   /* end of #if */
 102         savc= *newp; *newp='\0';
 103         for (p2=op2+8; --p2>=op2; )  /* check 2-char ops */


 118                 }
 119         } else  if (*inp=='\'') {/* character constant */
 120                 val=number;
 121                 if (inp[1]=='\\') {/* escaped */
 122                         char c; if (newp[-1]=='\'') newp[-1]='\0';
 123                         s=opc;
 124                         while (*s) if (*s++!=inp[2]) ++s; else {yylval= *s; goto ret;}
 125                         if (inp[2]<='9' && inp[2]>='0') yylval=c=tobinary(inp+2,8);
 126                         else yylval=inp[2];
 127                 } else yylval=inp[1];
 128         } else if (0==strcmp("\\\n",inp)) {*newp=savc; continue;}
 129         else {
 130                 *newp=savc; pperror("Illegal character %c in preprocessor if", *inp);
 131                 continue;
 132         }
 133 ret:
 134         *newp=savc; outp=inp=newp; return(val);
 135 }
 136 }
 137 
 138 static long
 139 tobinary(char *st, int b)


 140 {
 141         char *tmp;
 142         int n;
 143         n = strtoul(st, &tmp, b);
 144         if (*tmp != '\0') {
 145                 if ((strcasecmp(tmp, "L") != 0) &&
 146                     (strcasecmp(tmp, "LL") != 0) &&
 147                     (strcasecmp(tmp, "UL") != 0) &&
 148                     (strcasecmp(tmp, "ULL") != 0))
 149                         pperror("illegal number: %s", st);








 150         }
 151         return(n);



 152 }