1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * Copyright (C) 2004 by Darren Reed. 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * See the IPFILTER.LICENCE file for details on licencing. 5*0Sstevel@tonic-gate * 6*0Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 7*0Sstevel@tonic-gate * Use is subject to license terms. 8*0Sstevel@tonic-gate */ 9*0Sstevel@tonic-gate 10*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 11*0Sstevel@tonic-gate 12*0Sstevel@tonic-gate #include <ctype.h> 13*0Sstevel@tonic-gate #include "ipf.h" 14*0Sstevel@tonic-gate #ifdef IPFILTER_SCAN 15*0Sstevel@tonic-gate # include "netinet/ip_scan.h" 16*0Sstevel@tonic-gate #endif 17*0Sstevel@tonic-gate #include <sys/ioctl.h> 18*0Sstevel@tonic-gate #include <syslog.h> 19*0Sstevel@tonic-gate #ifdef TEST_LEXER 20*0Sstevel@tonic-gate # define NO_YACC 21*0Sstevel@tonic-gate union { 22*0Sstevel@tonic-gate int num; 23*0Sstevel@tonic-gate char *str; 24*0Sstevel@tonic-gate struct in_addr ipa; 25*0Sstevel@tonic-gate i6addr_t ip6; 26*0Sstevel@tonic-gate } yylval; 27*0Sstevel@tonic-gate #endif 28*0Sstevel@tonic-gate #include "lexer.h" 29*0Sstevel@tonic-gate #include "y.tab.h" 30*0Sstevel@tonic-gate 31*0Sstevel@tonic-gate FILE *yyin; 32*0Sstevel@tonic-gate 33*0Sstevel@tonic-gate #define ishex(c) (isdigit(c) || ((c) >= 'a' && (c) <= 'f') || \ 34*0Sstevel@tonic-gate ((c) >= 'A' && (c) <= 'F')) 35*0Sstevel@tonic-gate #define TOOLONG -3 36*0Sstevel@tonic-gate 37*0Sstevel@tonic-gate extern int string_start; 38*0Sstevel@tonic-gate extern int string_end; 39*0Sstevel@tonic-gate extern char *string_val; 40*0Sstevel@tonic-gate extern int pos; 41*0Sstevel@tonic-gate extern int yydebug; 42*0Sstevel@tonic-gate 43*0Sstevel@tonic-gate char *yystr = NULL; 44*0Sstevel@tonic-gate int yytext[YYBUFSIZ+1]; 45*0Sstevel@tonic-gate int yylineNum = 1; 46*0Sstevel@tonic-gate int yypos = 0; 47*0Sstevel@tonic-gate int yylast = -1; 48*0Sstevel@tonic-gate int yyexpectaddr = 0; 49*0Sstevel@tonic-gate int yybreakondot = 0; 50*0Sstevel@tonic-gate int yyvarnext = 0; 51*0Sstevel@tonic-gate int yytokentype = 0; 52*0Sstevel@tonic-gate wordtab_t *yywordtab = NULL; 53*0Sstevel@tonic-gate int yysavedepth = 0; 54*0Sstevel@tonic-gate wordtab_t *yysavewords[30]; 55*0Sstevel@tonic-gate 56*0Sstevel@tonic-gate 57*0Sstevel@tonic-gate static wordtab_t *yyfindkey __P((char *)); 58*0Sstevel@tonic-gate static int yygetc __P((void)); 59*0Sstevel@tonic-gate static void yyunputc __P((int)); 60*0Sstevel@tonic-gate static int yyswallow __P((int)); 61*0Sstevel@tonic-gate static char *yytexttostr __P((int, int)); 62*0Sstevel@tonic-gate static void yystrtotext __P((char *)); 63*0Sstevel@tonic-gate 64*0Sstevel@tonic-gate 65*0Sstevel@tonic-gate static int yygetc() 66*0Sstevel@tonic-gate { 67*0Sstevel@tonic-gate int c; 68*0Sstevel@tonic-gate 69*0Sstevel@tonic-gate if (yypos < yylast) { 70*0Sstevel@tonic-gate c = yytext[yypos++]; 71*0Sstevel@tonic-gate return c; 72*0Sstevel@tonic-gate } 73*0Sstevel@tonic-gate 74*0Sstevel@tonic-gate if (yypos == YYBUFSIZ) 75*0Sstevel@tonic-gate return TOOLONG; 76*0Sstevel@tonic-gate 77*0Sstevel@tonic-gate if (pos >= string_start && pos <= string_end) { 78*0Sstevel@tonic-gate c = string_val[pos - string_start]; 79*0Sstevel@tonic-gate yypos++; 80*0Sstevel@tonic-gate } else { 81*0Sstevel@tonic-gate c = fgetc(yyin); 82*0Sstevel@tonic-gate if (c == '\n') 83*0Sstevel@tonic-gate yylineNum++; 84*0Sstevel@tonic-gate } 85*0Sstevel@tonic-gate yytext[yypos++] = c; 86*0Sstevel@tonic-gate yylast = yypos; 87*0Sstevel@tonic-gate yytext[yypos] = '\0'; 88*0Sstevel@tonic-gate 89*0Sstevel@tonic-gate return c; 90*0Sstevel@tonic-gate } 91*0Sstevel@tonic-gate 92*0Sstevel@tonic-gate 93*0Sstevel@tonic-gate static void yyunputc(c) 94*0Sstevel@tonic-gate int c; 95*0Sstevel@tonic-gate { 96*0Sstevel@tonic-gate yytext[--yypos] = c; 97*0Sstevel@tonic-gate } 98*0Sstevel@tonic-gate 99*0Sstevel@tonic-gate 100*0Sstevel@tonic-gate static int yyswallow(last) 101*0Sstevel@tonic-gate int last; 102*0Sstevel@tonic-gate { 103*0Sstevel@tonic-gate int c; 104*0Sstevel@tonic-gate 105*0Sstevel@tonic-gate while (((c = yygetc()) > '\0') && (c != last)) 106*0Sstevel@tonic-gate ; 107*0Sstevel@tonic-gate 108*0Sstevel@tonic-gate if (c != EOF) 109*0Sstevel@tonic-gate yyunputc(c); 110*0Sstevel@tonic-gate if (c == last) 111*0Sstevel@tonic-gate return 0; 112*0Sstevel@tonic-gate return -1; 113*0Sstevel@tonic-gate } 114*0Sstevel@tonic-gate 115*0Sstevel@tonic-gate 116*0Sstevel@tonic-gate static void yystrtotext(str) 117*0Sstevel@tonic-gate char *str; 118*0Sstevel@tonic-gate { 119*0Sstevel@tonic-gate int len; 120*0Sstevel@tonic-gate char *s; 121*0Sstevel@tonic-gate 122*0Sstevel@tonic-gate len = strlen(str); 123*0Sstevel@tonic-gate if (len > YYBUFSIZ) 124*0Sstevel@tonic-gate len = YYBUFSIZ; 125*0Sstevel@tonic-gate 126*0Sstevel@tonic-gate for (s = str; *s != '\0' && len > 0; s++, len--) 127*0Sstevel@tonic-gate yytext[yylast++] = *s; 128*0Sstevel@tonic-gate yytext[yylast] = '\0'; 129*0Sstevel@tonic-gate } 130*0Sstevel@tonic-gate 131*0Sstevel@tonic-gate 132*0Sstevel@tonic-gate static char *yytexttostr(offset, max) 133*0Sstevel@tonic-gate int offset, max; 134*0Sstevel@tonic-gate { 135*0Sstevel@tonic-gate char *str; 136*0Sstevel@tonic-gate int i; 137*0Sstevel@tonic-gate 138*0Sstevel@tonic-gate if ((yytext[offset] == '\'' || yytext[offset] == '"') && 139*0Sstevel@tonic-gate (yytext[offset] == yytext[offset + max - 1])) { 140*0Sstevel@tonic-gate offset++; 141*0Sstevel@tonic-gate max--; 142*0Sstevel@tonic-gate } 143*0Sstevel@tonic-gate 144*0Sstevel@tonic-gate if (max > yylast) 145*0Sstevel@tonic-gate max = yylast; 146*0Sstevel@tonic-gate str = malloc(max + 1); 147*0Sstevel@tonic-gate if (str != NULL) { 148*0Sstevel@tonic-gate for (i = offset; i < max; i++) 149*0Sstevel@tonic-gate str[i - offset] = (char)(yytext[i] & 0xff); 150*0Sstevel@tonic-gate str[i - offset] = '\0'; 151*0Sstevel@tonic-gate } 152*0Sstevel@tonic-gate return str; 153*0Sstevel@tonic-gate } 154*0Sstevel@tonic-gate 155*0Sstevel@tonic-gate 156*0Sstevel@tonic-gate int yylex() 157*0Sstevel@tonic-gate { 158*0Sstevel@tonic-gate int c, n, isbuilding, rval, lnext, nokey = 0; 159*0Sstevel@tonic-gate char *name; 160*0Sstevel@tonic-gate 161*0Sstevel@tonic-gate isbuilding = 0; 162*0Sstevel@tonic-gate lnext = 0; 163*0Sstevel@tonic-gate rval = 0; 164*0Sstevel@tonic-gate 165*0Sstevel@tonic-gate if (yystr != NULL) { 166*0Sstevel@tonic-gate free(yystr); 167*0Sstevel@tonic-gate yystr = NULL; 168*0Sstevel@tonic-gate } 169*0Sstevel@tonic-gate 170*0Sstevel@tonic-gate nextchar: 171*0Sstevel@tonic-gate c = yygetc(); 172*0Sstevel@tonic-gate 173*0Sstevel@tonic-gate switch (c) 174*0Sstevel@tonic-gate { 175*0Sstevel@tonic-gate case '\n' : 176*0Sstevel@tonic-gate case '\t' : 177*0Sstevel@tonic-gate case '\r' : 178*0Sstevel@tonic-gate case ' ' : 179*0Sstevel@tonic-gate if (isbuilding == 1) { 180*0Sstevel@tonic-gate yyunputc(c); 181*0Sstevel@tonic-gate goto done; 182*0Sstevel@tonic-gate } 183*0Sstevel@tonic-gate if (yylast > yypos) { 184*0Sstevel@tonic-gate bcopy(yytext + yypos, yytext, 185*0Sstevel@tonic-gate sizeof(yytext[0]) * (yylast - yypos + 1)); 186*0Sstevel@tonic-gate } 187*0Sstevel@tonic-gate yylast -= yypos; 188*0Sstevel@tonic-gate yypos = 0; 189*0Sstevel@tonic-gate goto nextchar; 190*0Sstevel@tonic-gate 191*0Sstevel@tonic-gate case '\\' : 192*0Sstevel@tonic-gate if (lnext == 0) { 193*0Sstevel@tonic-gate lnext = 1; 194*0Sstevel@tonic-gate if (yylast == yypos) { 195*0Sstevel@tonic-gate yylast--; 196*0Sstevel@tonic-gate yypos--; 197*0Sstevel@tonic-gate } else 198*0Sstevel@tonic-gate yypos--; 199*0Sstevel@tonic-gate if (yypos == 0) 200*0Sstevel@tonic-gate nokey = 1; 201*0Sstevel@tonic-gate goto nextchar; 202*0Sstevel@tonic-gate } 203*0Sstevel@tonic-gate break; 204*0Sstevel@tonic-gate } 205*0Sstevel@tonic-gate 206*0Sstevel@tonic-gate if (lnext == 1) { 207*0Sstevel@tonic-gate lnext = 0; 208*0Sstevel@tonic-gate goto nextchar; 209*0Sstevel@tonic-gate } 210*0Sstevel@tonic-gate 211*0Sstevel@tonic-gate switch (c) 212*0Sstevel@tonic-gate { 213*0Sstevel@tonic-gate case '#' : 214*0Sstevel@tonic-gate if (isbuilding == 1) { 215*0Sstevel@tonic-gate yyunputc(c); 216*0Sstevel@tonic-gate goto done; 217*0Sstevel@tonic-gate } 218*0Sstevel@tonic-gate yyswallow('\n'); 219*0Sstevel@tonic-gate rval = YY_COMMENT; 220*0Sstevel@tonic-gate goto done; 221*0Sstevel@tonic-gate 222*0Sstevel@tonic-gate case '$' : 223*0Sstevel@tonic-gate if (isbuilding == 1) { 224*0Sstevel@tonic-gate yyunputc(c); 225*0Sstevel@tonic-gate goto done; 226*0Sstevel@tonic-gate } 227*0Sstevel@tonic-gate n = yygetc(); 228*0Sstevel@tonic-gate if (n == '{') { 229*0Sstevel@tonic-gate if (yyswallow('}') == -1) { 230*0Sstevel@tonic-gate rval = -2; 231*0Sstevel@tonic-gate goto done; 232*0Sstevel@tonic-gate } 233*0Sstevel@tonic-gate (void) yygetc(); 234*0Sstevel@tonic-gate } else { 235*0Sstevel@tonic-gate if (!isalpha(n)) { 236*0Sstevel@tonic-gate yyunputc(n); 237*0Sstevel@tonic-gate break; 238*0Sstevel@tonic-gate } 239*0Sstevel@tonic-gate do { 240*0Sstevel@tonic-gate n = yygetc(); 241*0Sstevel@tonic-gate } while (isalpha(n) || isdigit(n) || n == '_'); 242*0Sstevel@tonic-gate yyunputc(n); 243*0Sstevel@tonic-gate } 244*0Sstevel@tonic-gate 245*0Sstevel@tonic-gate name = yytexttostr(1, yypos); /* skip $ */ 246*0Sstevel@tonic-gate 247*0Sstevel@tonic-gate if (name != NULL) { 248*0Sstevel@tonic-gate string_val = get_variable(name, NULL, yylineNum); 249*0Sstevel@tonic-gate free(name); 250*0Sstevel@tonic-gate if (string_val != NULL) { 251*0Sstevel@tonic-gate name = yytexttostr(yypos, yylast); 252*0Sstevel@tonic-gate if (name != NULL) { 253*0Sstevel@tonic-gate yypos = 0; 254*0Sstevel@tonic-gate yylast = 0; 255*0Sstevel@tonic-gate yystrtotext(string_val); 256*0Sstevel@tonic-gate yystrtotext(name); 257*0Sstevel@tonic-gate free(string_val); 258*0Sstevel@tonic-gate free(name); 259*0Sstevel@tonic-gate goto nextchar; 260*0Sstevel@tonic-gate } 261*0Sstevel@tonic-gate free(string_val); 262*0Sstevel@tonic-gate } 263*0Sstevel@tonic-gate } 264*0Sstevel@tonic-gate break; 265*0Sstevel@tonic-gate 266*0Sstevel@tonic-gate case '\'': 267*0Sstevel@tonic-gate case '"' : 268*0Sstevel@tonic-gate if (isbuilding == 1) { 269*0Sstevel@tonic-gate goto done; 270*0Sstevel@tonic-gate } 271*0Sstevel@tonic-gate do { 272*0Sstevel@tonic-gate n = yygetc(); 273*0Sstevel@tonic-gate if (n == EOF || n == TOOLONG) { 274*0Sstevel@tonic-gate rval = -2; 275*0Sstevel@tonic-gate goto done; 276*0Sstevel@tonic-gate } 277*0Sstevel@tonic-gate if (n == '\n') { 278*0Sstevel@tonic-gate yyunputc(' '); 279*0Sstevel@tonic-gate yypos++; 280*0Sstevel@tonic-gate } 281*0Sstevel@tonic-gate } while (n != c); 282*0Sstevel@tonic-gate yyunputc(n); 283*0Sstevel@tonic-gate break; 284*0Sstevel@tonic-gate 285*0Sstevel@tonic-gate case EOF : 286*0Sstevel@tonic-gate yylineNum = 1; 287*0Sstevel@tonic-gate yypos = 0; 288*0Sstevel@tonic-gate yylast = -1; 289*0Sstevel@tonic-gate yyexpectaddr = 0; 290*0Sstevel@tonic-gate yybreakondot = 0; 291*0Sstevel@tonic-gate yyvarnext = 0; 292*0Sstevel@tonic-gate yytokentype = 0; 293*0Sstevel@tonic-gate yysavedepth = 0; 294*0Sstevel@tonic-gate return 0; 295*0Sstevel@tonic-gate } 296*0Sstevel@tonic-gate 297*0Sstevel@tonic-gate if (strchr("=,/;{}()@", c) != NULL) { 298*0Sstevel@tonic-gate if (isbuilding == 1) { 299*0Sstevel@tonic-gate yyunputc(c); 300*0Sstevel@tonic-gate goto done; 301*0Sstevel@tonic-gate } 302*0Sstevel@tonic-gate rval = c; 303*0Sstevel@tonic-gate goto done; 304*0Sstevel@tonic-gate } else if (c == '.') { 305*0Sstevel@tonic-gate if (isbuilding == 0) { 306*0Sstevel@tonic-gate rval = c; 307*0Sstevel@tonic-gate goto done; 308*0Sstevel@tonic-gate } 309*0Sstevel@tonic-gate if (yybreakondot != 0) { 310*0Sstevel@tonic-gate yyunputc(c); 311*0Sstevel@tonic-gate goto done; 312*0Sstevel@tonic-gate } 313*0Sstevel@tonic-gate } 314*0Sstevel@tonic-gate 315*0Sstevel@tonic-gate switch (c) 316*0Sstevel@tonic-gate { 317*0Sstevel@tonic-gate case '-' : 318*0Sstevel@tonic-gate if (yyexpectaddr) 319*0Sstevel@tonic-gate break; 320*0Sstevel@tonic-gate if (isbuilding == 1) 321*0Sstevel@tonic-gate break; 322*0Sstevel@tonic-gate n = yygetc(); 323*0Sstevel@tonic-gate if (n == '>') { 324*0Sstevel@tonic-gate isbuilding = 1; 325*0Sstevel@tonic-gate goto done; 326*0Sstevel@tonic-gate } 327*0Sstevel@tonic-gate yyunputc(n); 328*0Sstevel@tonic-gate rval = '-'; 329*0Sstevel@tonic-gate goto done; 330*0Sstevel@tonic-gate 331*0Sstevel@tonic-gate case '!' : 332*0Sstevel@tonic-gate if (isbuilding == 1) { 333*0Sstevel@tonic-gate yyunputc(c); 334*0Sstevel@tonic-gate goto done; 335*0Sstevel@tonic-gate } 336*0Sstevel@tonic-gate n = yygetc(); 337*0Sstevel@tonic-gate if (n == '=') { 338*0Sstevel@tonic-gate rval = YY_CMP_NE; 339*0Sstevel@tonic-gate goto done; 340*0Sstevel@tonic-gate } 341*0Sstevel@tonic-gate yyunputc(n); 342*0Sstevel@tonic-gate rval = '!'; 343*0Sstevel@tonic-gate goto done; 344*0Sstevel@tonic-gate 345*0Sstevel@tonic-gate case '<' : 346*0Sstevel@tonic-gate if (yyexpectaddr) 347*0Sstevel@tonic-gate break; 348*0Sstevel@tonic-gate if (isbuilding == 1) { 349*0Sstevel@tonic-gate yyunputc(c); 350*0Sstevel@tonic-gate goto done; 351*0Sstevel@tonic-gate } 352*0Sstevel@tonic-gate n = yygetc(); 353*0Sstevel@tonic-gate if (n == '=') { 354*0Sstevel@tonic-gate rval = YY_CMP_LE; 355*0Sstevel@tonic-gate goto done; 356*0Sstevel@tonic-gate } 357*0Sstevel@tonic-gate if (n == '>') { 358*0Sstevel@tonic-gate rval = YY_RANGE_OUT; 359*0Sstevel@tonic-gate goto done; 360*0Sstevel@tonic-gate } 361*0Sstevel@tonic-gate yyunputc(n); 362*0Sstevel@tonic-gate rval = YY_CMP_LT; 363*0Sstevel@tonic-gate goto done; 364*0Sstevel@tonic-gate 365*0Sstevel@tonic-gate case '>' : 366*0Sstevel@tonic-gate if (yyexpectaddr) 367*0Sstevel@tonic-gate break; 368*0Sstevel@tonic-gate if (isbuilding == 1) { 369*0Sstevel@tonic-gate yyunputc(c); 370*0Sstevel@tonic-gate goto done; 371*0Sstevel@tonic-gate } 372*0Sstevel@tonic-gate n = yygetc(); 373*0Sstevel@tonic-gate if (n == '=') { 374*0Sstevel@tonic-gate rval = YY_CMP_GE; 375*0Sstevel@tonic-gate goto done; 376*0Sstevel@tonic-gate } 377*0Sstevel@tonic-gate if (n == '<') { 378*0Sstevel@tonic-gate rval = YY_RANGE_IN; 379*0Sstevel@tonic-gate goto done; 380*0Sstevel@tonic-gate } 381*0Sstevel@tonic-gate yyunputc(n); 382*0Sstevel@tonic-gate rval = YY_CMP_GT; 383*0Sstevel@tonic-gate goto done; 384*0Sstevel@tonic-gate } 385*0Sstevel@tonic-gate 386*0Sstevel@tonic-gate /* 387*0Sstevel@tonic-gate * Now for the reason this is here...IPv6 address parsing. 388*0Sstevel@tonic-gate * The longest string we can expect is of this form: 389*0Sstevel@tonic-gate * 0000:0000:0000:0000:0000:0000:000.000.000.000 390*0Sstevel@tonic-gate * not: 391*0Sstevel@tonic-gate * 0000:0000:0000:0000:0000:0000:0000:0000 392*0Sstevel@tonic-gate */ 393*0Sstevel@tonic-gate #ifdef USE_INET6 394*0Sstevel@tonic-gate if (yyexpectaddr == 1 && isbuilding == 0 && (ishex(c) || c == ':')) { 395*0Sstevel@tonic-gate char ipv6buf[45 + 1], *s, oc; 396*0Sstevel@tonic-gate int start; 397*0Sstevel@tonic-gate 398*0Sstevel@tonic-gate start = yypos; 399*0Sstevel@tonic-gate s = ipv6buf; 400*0Sstevel@tonic-gate oc = c; 401*0Sstevel@tonic-gate 402*0Sstevel@tonic-gate /* 403*0Sstevel@tonic-gate * Perhaps we should implement stricter controls on what we 404*0Sstevel@tonic-gate * swallow up here, but surely it would just be duplicating 405*0Sstevel@tonic-gate * the code in inet_pton() anyway. 406*0Sstevel@tonic-gate */ 407*0Sstevel@tonic-gate do { 408*0Sstevel@tonic-gate *s++ = c; 409*0Sstevel@tonic-gate c = yygetc(); 410*0Sstevel@tonic-gate } while ((ishex(c) || c == ':' || c == '.') && 411*0Sstevel@tonic-gate (s - ipv6buf < 46)); 412*0Sstevel@tonic-gate yyunputc(c); 413*0Sstevel@tonic-gate *s = '\0'; 414*0Sstevel@tonic-gate 415*0Sstevel@tonic-gate if (inet_pton(AF_INET6, ipv6buf, &yylval.ip6) == 1) { 416*0Sstevel@tonic-gate rval = YY_IPV6; 417*0Sstevel@tonic-gate yyexpectaddr = 0; 418*0Sstevel@tonic-gate goto done; 419*0Sstevel@tonic-gate } 420*0Sstevel@tonic-gate yypos = start; 421*0Sstevel@tonic-gate c = oc; 422*0Sstevel@tonic-gate } 423*0Sstevel@tonic-gate #endif 424*0Sstevel@tonic-gate 425*0Sstevel@tonic-gate if (c == ':') { 426*0Sstevel@tonic-gate if (isbuilding == 1) { 427*0Sstevel@tonic-gate yyunputc(c); 428*0Sstevel@tonic-gate goto done; 429*0Sstevel@tonic-gate } 430*0Sstevel@tonic-gate rval = ':'; 431*0Sstevel@tonic-gate goto done; 432*0Sstevel@tonic-gate } 433*0Sstevel@tonic-gate 434*0Sstevel@tonic-gate if (isbuilding == 0 && c == '0') { 435*0Sstevel@tonic-gate n = yygetc(); 436*0Sstevel@tonic-gate if (n == 'x') { 437*0Sstevel@tonic-gate do { 438*0Sstevel@tonic-gate n = yygetc(); 439*0Sstevel@tonic-gate } while (ishex(n)); 440*0Sstevel@tonic-gate yyunputc(n); 441*0Sstevel@tonic-gate rval = YY_HEX; 442*0Sstevel@tonic-gate goto done; 443*0Sstevel@tonic-gate } 444*0Sstevel@tonic-gate yyunputc(n); 445*0Sstevel@tonic-gate } 446*0Sstevel@tonic-gate 447*0Sstevel@tonic-gate /* 448*0Sstevel@tonic-gate * No negative numbers with leading - sign.. 449*0Sstevel@tonic-gate */ 450*0Sstevel@tonic-gate if (isbuilding == 0 && isdigit(c)) { 451*0Sstevel@tonic-gate do { 452*0Sstevel@tonic-gate n = yygetc(); 453*0Sstevel@tonic-gate } while (isdigit(n)); 454*0Sstevel@tonic-gate yyunputc(n); 455*0Sstevel@tonic-gate rval = YY_NUMBER; 456*0Sstevel@tonic-gate goto done; 457*0Sstevel@tonic-gate } 458*0Sstevel@tonic-gate 459*0Sstevel@tonic-gate isbuilding = 1; 460*0Sstevel@tonic-gate goto nextchar; 461*0Sstevel@tonic-gate 462*0Sstevel@tonic-gate done: 463*0Sstevel@tonic-gate yystr = yytexttostr(0, yypos); 464*0Sstevel@tonic-gate 465*0Sstevel@tonic-gate if (isbuilding == 1) { 466*0Sstevel@tonic-gate wordtab_t *w; 467*0Sstevel@tonic-gate 468*0Sstevel@tonic-gate w = NULL; 469*0Sstevel@tonic-gate isbuilding = 0; 470*0Sstevel@tonic-gate 471*0Sstevel@tonic-gate if ((yyvarnext == 0) && (nokey == 0)) { 472*0Sstevel@tonic-gate w = yyfindkey(yystr); 473*0Sstevel@tonic-gate if (w == NULL && yywordtab != NULL) { 474*0Sstevel@tonic-gate yyresetdict(); 475*0Sstevel@tonic-gate w = yyfindkey(yystr); 476*0Sstevel@tonic-gate } 477*0Sstevel@tonic-gate } else 478*0Sstevel@tonic-gate yyvarnext = 0; 479*0Sstevel@tonic-gate if (w != NULL) 480*0Sstevel@tonic-gate rval = w->w_value; 481*0Sstevel@tonic-gate else 482*0Sstevel@tonic-gate rval = YY_STR; 483*0Sstevel@tonic-gate } 484*0Sstevel@tonic-gate 485*0Sstevel@tonic-gate if (rval == YY_STR && yysavedepth > 0) 486*0Sstevel@tonic-gate yyresetdict(); 487*0Sstevel@tonic-gate 488*0Sstevel@tonic-gate yytokentype = rval; 489*0Sstevel@tonic-gate 490*0Sstevel@tonic-gate if (yydebug) 491*0Sstevel@tonic-gate printf("lexed(%s) => %d\n", yystr, rval); 492*0Sstevel@tonic-gate 493*0Sstevel@tonic-gate switch (rval) 494*0Sstevel@tonic-gate { 495*0Sstevel@tonic-gate case YY_NUMBER : 496*0Sstevel@tonic-gate yylval.num = atoi(yystr); 497*0Sstevel@tonic-gate break; 498*0Sstevel@tonic-gate 499*0Sstevel@tonic-gate case YY_HEX : 500*0Sstevel@tonic-gate sscanf(yystr, "0x%x", (u_int *)&yylval.num); 501*0Sstevel@tonic-gate break; 502*0Sstevel@tonic-gate 503*0Sstevel@tonic-gate case YY_STR : 504*0Sstevel@tonic-gate yylval.str = strdup(yystr); 505*0Sstevel@tonic-gate break; 506*0Sstevel@tonic-gate 507*0Sstevel@tonic-gate default : 508*0Sstevel@tonic-gate break; 509*0Sstevel@tonic-gate } 510*0Sstevel@tonic-gate 511*0Sstevel@tonic-gate if (yylast > 0) { 512*0Sstevel@tonic-gate bcopy(yytext + yypos, yytext, 513*0Sstevel@tonic-gate sizeof(yytext[0]) * (yylast - yypos + 1)); 514*0Sstevel@tonic-gate yylast -= yypos; 515*0Sstevel@tonic-gate yypos = 0; 516*0Sstevel@tonic-gate } 517*0Sstevel@tonic-gate 518*0Sstevel@tonic-gate return rval; 519*0Sstevel@tonic-gate } 520*0Sstevel@tonic-gate 521*0Sstevel@tonic-gate 522*0Sstevel@tonic-gate static wordtab_t *yyfindkey(key) 523*0Sstevel@tonic-gate char *key; 524*0Sstevel@tonic-gate { 525*0Sstevel@tonic-gate wordtab_t *w; 526*0Sstevel@tonic-gate 527*0Sstevel@tonic-gate if (yywordtab == NULL) 528*0Sstevel@tonic-gate return NULL; 529*0Sstevel@tonic-gate 530*0Sstevel@tonic-gate for (w = yywordtab; w->w_word != 0; w++) 531*0Sstevel@tonic-gate if (strcasecmp(key, w->w_word) == 0) 532*0Sstevel@tonic-gate return w; 533*0Sstevel@tonic-gate return NULL; 534*0Sstevel@tonic-gate } 535*0Sstevel@tonic-gate 536*0Sstevel@tonic-gate 537*0Sstevel@tonic-gate char *yykeytostr(num) 538*0Sstevel@tonic-gate int num; 539*0Sstevel@tonic-gate { 540*0Sstevel@tonic-gate wordtab_t *w; 541*0Sstevel@tonic-gate 542*0Sstevel@tonic-gate if (yywordtab == NULL) 543*0Sstevel@tonic-gate return "<unknown>"; 544*0Sstevel@tonic-gate 545*0Sstevel@tonic-gate for (w = yywordtab; w->w_word; w++) 546*0Sstevel@tonic-gate if (w->w_value == num) 547*0Sstevel@tonic-gate return w->w_word; 548*0Sstevel@tonic-gate return "<unknown>"; 549*0Sstevel@tonic-gate } 550*0Sstevel@tonic-gate 551*0Sstevel@tonic-gate 552*0Sstevel@tonic-gate wordtab_t *yysettab(words) 553*0Sstevel@tonic-gate wordtab_t *words; 554*0Sstevel@tonic-gate { 555*0Sstevel@tonic-gate wordtab_t *save; 556*0Sstevel@tonic-gate 557*0Sstevel@tonic-gate save = yywordtab; 558*0Sstevel@tonic-gate yywordtab = words; 559*0Sstevel@tonic-gate return save; 560*0Sstevel@tonic-gate } 561*0Sstevel@tonic-gate 562*0Sstevel@tonic-gate 563*0Sstevel@tonic-gate void yyerror(msg) 564*0Sstevel@tonic-gate char *msg; 565*0Sstevel@tonic-gate { 566*0Sstevel@tonic-gate char *txt, letter[2]; 567*0Sstevel@tonic-gate int freetxt = 0; 568*0Sstevel@tonic-gate 569*0Sstevel@tonic-gate if (yytokentype < 256) { 570*0Sstevel@tonic-gate letter[0] = yytokentype; 571*0Sstevel@tonic-gate letter[1] = '\0'; 572*0Sstevel@tonic-gate txt = letter; 573*0Sstevel@tonic-gate } else if (yytokentype == YY_STR || yytokentype == YY_HEX || 574*0Sstevel@tonic-gate yytokentype == YY_NUMBER) { 575*0Sstevel@tonic-gate if (yystr == NULL) { 576*0Sstevel@tonic-gate txt = yytexttostr(yypos, YYBUFSIZ); 577*0Sstevel@tonic-gate freetxt = 1; 578*0Sstevel@tonic-gate } else 579*0Sstevel@tonic-gate txt = yystr; 580*0Sstevel@tonic-gate } else { 581*0Sstevel@tonic-gate txt = yykeytostr(yytokentype); 582*0Sstevel@tonic-gate } 583*0Sstevel@tonic-gate fprintf(stderr, "%s error at \"%s\", line %d\n", msg, txt, yylineNum); 584*0Sstevel@tonic-gate if (freetxt == 1) 585*0Sstevel@tonic-gate free(txt); 586*0Sstevel@tonic-gate exit(1); 587*0Sstevel@tonic-gate } 588*0Sstevel@tonic-gate 589*0Sstevel@tonic-gate 590*0Sstevel@tonic-gate void yysetdict(newdict) 591*0Sstevel@tonic-gate wordtab_t *newdict; 592*0Sstevel@tonic-gate { 593*0Sstevel@tonic-gate if (yysavedepth == sizeof(yysavewords)/sizeof(yysavewords[0])) { 594*0Sstevel@tonic-gate fprintf(stderr, "%d: at maximum dictionary depth\n", 595*0Sstevel@tonic-gate yylineNum); 596*0Sstevel@tonic-gate return; 597*0Sstevel@tonic-gate } 598*0Sstevel@tonic-gate 599*0Sstevel@tonic-gate yysavewords[yysavedepth++] = yysettab(newdict); 600*0Sstevel@tonic-gate if (yydebug) 601*0Sstevel@tonic-gate printf("yysavedepth++ => %d\n", yysavedepth); 602*0Sstevel@tonic-gate } 603*0Sstevel@tonic-gate 604*0Sstevel@tonic-gate void yyresetdict() 605*0Sstevel@tonic-gate { 606*0Sstevel@tonic-gate if (yysavedepth > 0) { 607*0Sstevel@tonic-gate yysettab(yysavewords[--yysavedepth]); 608*0Sstevel@tonic-gate if (yydebug) 609*0Sstevel@tonic-gate printf("yysavedepth-- => %d\n", yysavedepth); 610*0Sstevel@tonic-gate } 611*0Sstevel@tonic-gate } 612*0Sstevel@tonic-gate 613*0Sstevel@tonic-gate 614*0Sstevel@tonic-gate 615*0Sstevel@tonic-gate #ifdef TEST_LEXER 616*0Sstevel@tonic-gate int main(argc, argv) 617*0Sstevel@tonic-gate int argc; 618*0Sstevel@tonic-gate char *argv[]; 619*0Sstevel@tonic-gate { 620*0Sstevel@tonic-gate int n; 621*0Sstevel@tonic-gate 622*0Sstevel@tonic-gate yyin = stdin; 623*0Sstevel@tonic-gate 624*0Sstevel@tonic-gate while ((n = yylex()) != 0) 625*0Sstevel@tonic-gate printf("%d.n = %d [%s] %d %d\n", 626*0Sstevel@tonic-gate yylineNum, n, yystr, yypos, yylast); 627*0Sstevel@tonic-gate } 628*0Sstevel@tonic-gate #endif 629