10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * Copyright (C) 2004 by Darren Reed. 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * See the IPFILTER.LICENCE file for details on licencing. 50Sstevel@tonic-gate * 6*2393Syz155240 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 70Sstevel@tonic-gate * Use is subject to license terms. 80Sstevel@tonic-gate */ 90Sstevel@tonic-gate 100Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 110Sstevel@tonic-gate 120Sstevel@tonic-gate #include <ctype.h> 130Sstevel@tonic-gate #include "ipf.h" 140Sstevel@tonic-gate #ifdef IPFILTER_SCAN 150Sstevel@tonic-gate # include "netinet/ip_scan.h" 160Sstevel@tonic-gate #endif 170Sstevel@tonic-gate #include <sys/ioctl.h> 180Sstevel@tonic-gate #include <syslog.h> 190Sstevel@tonic-gate #ifdef TEST_LEXER 200Sstevel@tonic-gate # define NO_YACC 210Sstevel@tonic-gate union { 220Sstevel@tonic-gate int num; 230Sstevel@tonic-gate char *str; 240Sstevel@tonic-gate struct in_addr ipa; 250Sstevel@tonic-gate i6addr_t ip6; 260Sstevel@tonic-gate } yylval; 270Sstevel@tonic-gate #endif 280Sstevel@tonic-gate #include "lexer.h" 290Sstevel@tonic-gate #include "y.tab.h" 300Sstevel@tonic-gate 310Sstevel@tonic-gate FILE *yyin; 320Sstevel@tonic-gate 33*2393Syz155240 #define ishex(c) (ISDIGIT(c) || ((c) >= 'a' && (c) <= 'f') || \ 340Sstevel@tonic-gate ((c) >= 'A' && (c) <= 'F')) 350Sstevel@tonic-gate #define TOOLONG -3 360Sstevel@tonic-gate 370Sstevel@tonic-gate extern int string_start; 380Sstevel@tonic-gate extern int string_end; 390Sstevel@tonic-gate extern char *string_val; 400Sstevel@tonic-gate extern int pos; 410Sstevel@tonic-gate extern int yydebug; 420Sstevel@tonic-gate 430Sstevel@tonic-gate char *yystr = NULL; 440Sstevel@tonic-gate int yytext[YYBUFSIZ+1]; 450Sstevel@tonic-gate int yylineNum = 1; 460Sstevel@tonic-gate int yypos = 0; 470Sstevel@tonic-gate int yylast = -1; 480Sstevel@tonic-gate int yyexpectaddr = 0; 490Sstevel@tonic-gate int yybreakondot = 0; 500Sstevel@tonic-gate int yyvarnext = 0; 510Sstevel@tonic-gate int yytokentype = 0; 520Sstevel@tonic-gate wordtab_t *yywordtab = NULL; 530Sstevel@tonic-gate int yysavedepth = 0; 540Sstevel@tonic-gate wordtab_t *yysavewords[30]; 550Sstevel@tonic-gate 560Sstevel@tonic-gate 570Sstevel@tonic-gate static wordtab_t *yyfindkey __P((char *)); 580Sstevel@tonic-gate static int yygetc __P((void)); 590Sstevel@tonic-gate static void yyunputc __P((int)); 600Sstevel@tonic-gate static int yyswallow __P((int)); 610Sstevel@tonic-gate static char *yytexttostr __P((int, int)); 620Sstevel@tonic-gate static void yystrtotext __P((char *)); 630Sstevel@tonic-gate 640Sstevel@tonic-gate static int yygetc() 650Sstevel@tonic-gate { 660Sstevel@tonic-gate int c; 670Sstevel@tonic-gate 680Sstevel@tonic-gate if (yypos < yylast) { 690Sstevel@tonic-gate c = yytext[yypos++]; 70*2393Syz155240 if (c == '\n') 71*2393Syz155240 yylineNum++; 720Sstevel@tonic-gate return c; 730Sstevel@tonic-gate } 740Sstevel@tonic-gate 750Sstevel@tonic-gate if (yypos == YYBUFSIZ) 760Sstevel@tonic-gate return TOOLONG; 770Sstevel@tonic-gate 780Sstevel@tonic-gate if (pos >= string_start && pos <= string_end) { 790Sstevel@tonic-gate c = string_val[pos - string_start]; 800Sstevel@tonic-gate yypos++; 810Sstevel@tonic-gate } else { 820Sstevel@tonic-gate c = fgetc(yyin); 830Sstevel@tonic-gate } 84*2393Syz155240 if (c == '\n') 85*2393Syz155240 yylineNum++; 860Sstevel@tonic-gate yytext[yypos++] = c; 870Sstevel@tonic-gate yylast = yypos; 880Sstevel@tonic-gate yytext[yypos] = '\0'; 890Sstevel@tonic-gate 900Sstevel@tonic-gate return c; 910Sstevel@tonic-gate } 920Sstevel@tonic-gate 930Sstevel@tonic-gate 940Sstevel@tonic-gate static void yyunputc(c) 950Sstevel@tonic-gate int c; 960Sstevel@tonic-gate { 97*2393Syz155240 if (c == '\n') 98*2393Syz155240 yylineNum--; 990Sstevel@tonic-gate yytext[--yypos] = c; 1000Sstevel@tonic-gate } 1010Sstevel@tonic-gate 1020Sstevel@tonic-gate 1030Sstevel@tonic-gate static int yyswallow(last) 1040Sstevel@tonic-gate int last; 1050Sstevel@tonic-gate { 1060Sstevel@tonic-gate int c; 1070Sstevel@tonic-gate 1080Sstevel@tonic-gate while (((c = yygetc()) > '\0') && (c != last)) 1090Sstevel@tonic-gate ; 1100Sstevel@tonic-gate 1110Sstevel@tonic-gate if (c != EOF) 1120Sstevel@tonic-gate yyunputc(c); 1130Sstevel@tonic-gate if (c == last) 1140Sstevel@tonic-gate return 0; 1150Sstevel@tonic-gate return -1; 1160Sstevel@tonic-gate } 1170Sstevel@tonic-gate 1180Sstevel@tonic-gate 1190Sstevel@tonic-gate static void yystrtotext(str) 1200Sstevel@tonic-gate char *str; 1210Sstevel@tonic-gate { 1220Sstevel@tonic-gate int len; 1230Sstevel@tonic-gate char *s; 1240Sstevel@tonic-gate 1250Sstevel@tonic-gate len = strlen(str); 1260Sstevel@tonic-gate if (len > YYBUFSIZ) 1270Sstevel@tonic-gate len = YYBUFSIZ; 1280Sstevel@tonic-gate 1290Sstevel@tonic-gate for (s = str; *s != '\0' && len > 0; s++, len--) 1300Sstevel@tonic-gate yytext[yylast++] = *s; 1310Sstevel@tonic-gate yytext[yylast] = '\0'; 1320Sstevel@tonic-gate } 1330Sstevel@tonic-gate 1340Sstevel@tonic-gate 1350Sstevel@tonic-gate static char *yytexttostr(offset, max) 1360Sstevel@tonic-gate int offset, max; 1370Sstevel@tonic-gate { 1380Sstevel@tonic-gate char *str; 1390Sstevel@tonic-gate int i; 1400Sstevel@tonic-gate 1410Sstevel@tonic-gate if ((yytext[offset] == '\'' || yytext[offset] == '"') && 1420Sstevel@tonic-gate (yytext[offset] == yytext[offset + max - 1])) { 1430Sstevel@tonic-gate offset++; 1440Sstevel@tonic-gate max--; 1450Sstevel@tonic-gate } 1460Sstevel@tonic-gate 1470Sstevel@tonic-gate if (max > yylast) 1480Sstevel@tonic-gate max = yylast; 1490Sstevel@tonic-gate str = malloc(max + 1); 1500Sstevel@tonic-gate if (str != NULL) { 1510Sstevel@tonic-gate for (i = offset; i < max; i++) 1520Sstevel@tonic-gate str[i - offset] = (char)(yytext[i] & 0xff); 1530Sstevel@tonic-gate str[i - offset] = '\0'; 1540Sstevel@tonic-gate } 1550Sstevel@tonic-gate return str; 1560Sstevel@tonic-gate } 1570Sstevel@tonic-gate 1580Sstevel@tonic-gate 1590Sstevel@tonic-gate int yylex() 1600Sstevel@tonic-gate { 1610Sstevel@tonic-gate int c, n, isbuilding, rval, lnext, nokey = 0; 1620Sstevel@tonic-gate char *name; 1630Sstevel@tonic-gate 1640Sstevel@tonic-gate isbuilding = 0; 1650Sstevel@tonic-gate lnext = 0; 1660Sstevel@tonic-gate rval = 0; 1670Sstevel@tonic-gate 1680Sstevel@tonic-gate if (yystr != NULL) { 1690Sstevel@tonic-gate free(yystr); 1700Sstevel@tonic-gate yystr = NULL; 1710Sstevel@tonic-gate } 1720Sstevel@tonic-gate 1730Sstevel@tonic-gate nextchar: 1740Sstevel@tonic-gate c = yygetc(); 1750Sstevel@tonic-gate 1760Sstevel@tonic-gate switch (c) 1770Sstevel@tonic-gate { 1780Sstevel@tonic-gate case '\n' : 1790Sstevel@tonic-gate case '\t' : 1800Sstevel@tonic-gate case '\r' : 1810Sstevel@tonic-gate case ' ' : 1820Sstevel@tonic-gate if (isbuilding == 1) { 1830Sstevel@tonic-gate yyunputc(c); 1840Sstevel@tonic-gate goto done; 1850Sstevel@tonic-gate } 1860Sstevel@tonic-gate if (yylast > yypos) { 1870Sstevel@tonic-gate bcopy(yytext + yypos, yytext, 1880Sstevel@tonic-gate sizeof(yytext[0]) * (yylast - yypos + 1)); 1890Sstevel@tonic-gate } 1900Sstevel@tonic-gate yylast -= yypos; 1910Sstevel@tonic-gate yypos = 0; 192*2393Syz155240 lnext = 0; 193*2393Syz155240 nokey = 0; 1940Sstevel@tonic-gate goto nextchar; 1950Sstevel@tonic-gate 1960Sstevel@tonic-gate case '\\' : 1970Sstevel@tonic-gate if (lnext == 0) { 1980Sstevel@tonic-gate lnext = 1; 1990Sstevel@tonic-gate if (yylast == yypos) { 2000Sstevel@tonic-gate yylast--; 2010Sstevel@tonic-gate yypos--; 2020Sstevel@tonic-gate } else 2030Sstevel@tonic-gate yypos--; 2040Sstevel@tonic-gate if (yypos == 0) 2050Sstevel@tonic-gate nokey = 1; 2060Sstevel@tonic-gate goto nextchar; 2070Sstevel@tonic-gate } 2080Sstevel@tonic-gate break; 2090Sstevel@tonic-gate } 2100Sstevel@tonic-gate 2110Sstevel@tonic-gate if (lnext == 1) { 2120Sstevel@tonic-gate lnext = 0; 213*2393Syz155240 if ((isbuilding == 0) && !ISALNUM(c)) { 214*2393Syz155240 return c; 215*2393Syz155240 } 2160Sstevel@tonic-gate goto nextchar; 2170Sstevel@tonic-gate } 2180Sstevel@tonic-gate 2190Sstevel@tonic-gate switch (c) 2200Sstevel@tonic-gate { 2210Sstevel@tonic-gate case '#' : 2220Sstevel@tonic-gate if (isbuilding == 1) { 2230Sstevel@tonic-gate yyunputc(c); 2240Sstevel@tonic-gate goto done; 2250Sstevel@tonic-gate } 2260Sstevel@tonic-gate yyswallow('\n'); 2270Sstevel@tonic-gate rval = YY_COMMENT; 2280Sstevel@tonic-gate goto done; 2290Sstevel@tonic-gate 2300Sstevel@tonic-gate case '$' : 2310Sstevel@tonic-gate if (isbuilding == 1) { 2320Sstevel@tonic-gate yyunputc(c); 2330Sstevel@tonic-gate goto done; 2340Sstevel@tonic-gate } 2350Sstevel@tonic-gate n = yygetc(); 2360Sstevel@tonic-gate if (n == '{') { 2370Sstevel@tonic-gate if (yyswallow('}') == -1) { 2380Sstevel@tonic-gate rval = -2; 2390Sstevel@tonic-gate goto done; 2400Sstevel@tonic-gate } 2410Sstevel@tonic-gate (void) yygetc(); 2420Sstevel@tonic-gate } else { 243*2393Syz155240 if (!ISALPHA(n)) { 2440Sstevel@tonic-gate yyunputc(n); 2450Sstevel@tonic-gate break; 2460Sstevel@tonic-gate } 2470Sstevel@tonic-gate do { 2480Sstevel@tonic-gate n = yygetc(); 249*2393Syz155240 } while (ISALPHA(n) || ISDIGIT(n) || n == '_'); 2500Sstevel@tonic-gate yyunputc(n); 2510Sstevel@tonic-gate } 2520Sstevel@tonic-gate 2530Sstevel@tonic-gate name = yytexttostr(1, yypos); /* skip $ */ 2540Sstevel@tonic-gate 2550Sstevel@tonic-gate if (name != NULL) { 2560Sstevel@tonic-gate string_val = get_variable(name, NULL, yylineNum); 2570Sstevel@tonic-gate free(name); 2580Sstevel@tonic-gate if (string_val != NULL) { 2590Sstevel@tonic-gate name = yytexttostr(yypos, yylast); 2600Sstevel@tonic-gate if (name != NULL) { 2610Sstevel@tonic-gate yypos = 0; 2620Sstevel@tonic-gate yylast = 0; 2630Sstevel@tonic-gate yystrtotext(string_val); 2640Sstevel@tonic-gate yystrtotext(name); 2650Sstevel@tonic-gate free(string_val); 2660Sstevel@tonic-gate free(name); 2670Sstevel@tonic-gate goto nextchar; 2680Sstevel@tonic-gate } 2690Sstevel@tonic-gate free(string_val); 2700Sstevel@tonic-gate } 2710Sstevel@tonic-gate } 2720Sstevel@tonic-gate break; 2730Sstevel@tonic-gate 2740Sstevel@tonic-gate case '\'': 2750Sstevel@tonic-gate case '"' : 2760Sstevel@tonic-gate if (isbuilding == 1) { 2770Sstevel@tonic-gate goto done; 2780Sstevel@tonic-gate } 2790Sstevel@tonic-gate do { 2800Sstevel@tonic-gate n = yygetc(); 2810Sstevel@tonic-gate if (n == EOF || n == TOOLONG) { 2820Sstevel@tonic-gate rval = -2; 2830Sstevel@tonic-gate goto done; 2840Sstevel@tonic-gate } 2850Sstevel@tonic-gate if (n == '\n') { 2860Sstevel@tonic-gate yyunputc(' '); 2870Sstevel@tonic-gate yypos++; 2880Sstevel@tonic-gate } 2890Sstevel@tonic-gate } while (n != c); 2900Sstevel@tonic-gate yyunputc(n); 2910Sstevel@tonic-gate break; 2920Sstevel@tonic-gate 2930Sstevel@tonic-gate case EOF : 2940Sstevel@tonic-gate yylineNum = 1; 2950Sstevel@tonic-gate yypos = 0; 2960Sstevel@tonic-gate yylast = -1; 2970Sstevel@tonic-gate yyexpectaddr = 0; 2980Sstevel@tonic-gate yybreakondot = 0; 2990Sstevel@tonic-gate yyvarnext = 0; 3000Sstevel@tonic-gate yytokentype = 0; 3010Sstevel@tonic-gate return 0; 3020Sstevel@tonic-gate } 3030Sstevel@tonic-gate 3040Sstevel@tonic-gate if (strchr("=,/;{}()@", c) != NULL) { 3050Sstevel@tonic-gate if (isbuilding == 1) { 3060Sstevel@tonic-gate yyunputc(c); 3070Sstevel@tonic-gate goto done; 3080Sstevel@tonic-gate } 3090Sstevel@tonic-gate rval = c; 3100Sstevel@tonic-gate goto done; 3110Sstevel@tonic-gate } else if (c == '.') { 3120Sstevel@tonic-gate if (isbuilding == 0) { 3130Sstevel@tonic-gate rval = c; 3140Sstevel@tonic-gate goto done; 3150Sstevel@tonic-gate } 3160Sstevel@tonic-gate if (yybreakondot != 0) { 3170Sstevel@tonic-gate yyunputc(c); 3180Sstevel@tonic-gate goto done; 3190Sstevel@tonic-gate } 3200Sstevel@tonic-gate } 3210Sstevel@tonic-gate 3220Sstevel@tonic-gate switch (c) 3230Sstevel@tonic-gate { 3240Sstevel@tonic-gate case '-' : 3250Sstevel@tonic-gate if (yyexpectaddr) 3260Sstevel@tonic-gate break; 3270Sstevel@tonic-gate if (isbuilding == 1) 3280Sstevel@tonic-gate break; 3290Sstevel@tonic-gate n = yygetc(); 3300Sstevel@tonic-gate if (n == '>') { 3310Sstevel@tonic-gate isbuilding = 1; 3320Sstevel@tonic-gate goto done; 3330Sstevel@tonic-gate } 3340Sstevel@tonic-gate yyunputc(n); 3350Sstevel@tonic-gate rval = '-'; 3360Sstevel@tonic-gate goto done; 3370Sstevel@tonic-gate 3380Sstevel@tonic-gate case '!' : 3390Sstevel@tonic-gate if (isbuilding == 1) { 3400Sstevel@tonic-gate yyunputc(c); 3410Sstevel@tonic-gate goto done; 3420Sstevel@tonic-gate } 3430Sstevel@tonic-gate n = yygetc(); 3440Sstevel@tonic-gate if (n == '=') { 3450Sstevel@tonic-gate rval = YY_CMP_NE; 3460Sstevel@tonic-gate goto done; 3470Sstevel@tonic-gate } 3480Sstevel@tonic-gate yyunputc(n); 3490Sstevel@tonic-gate rval = '!'; 3500Sstevel@tonic-gate goto done; 3510Sstevel@tonic-gate 3520Sstevel@tonic-gate case '<' : 3530Sstevel@tonic-gate if (yyexpectaddr) 3540Sstevel@tonic-gate break; 3550Sstevel@tonic-gate if (isbuilding == 1) { 3560Sstevel@tonic-gate yyunputc(c); 3570Sstevel@tonic-gate goto done; 3580Sstevel@tonic-gate } 3590Sstevel@tonic-gate n = yygetc(); 3600Sstevel@tonic-gate if (n == '=') { 3610Sstevel@tonic-gate rval = YY_CMP_LE; 3620Sstevel@tonic-gate goto done; 3630Sstevel@tonic-gate } 3640Sstevel@tonic-gate if (n == '>') { 3650Sstevel@tonic-gate rval = YY_RANGE_OUT; 3660Sstevel@tonic-gate goto done; 3670Sstevel@tonic-gate } 3680Sstevel@tonic-gate yyunputc(n); 3690Sstevel@tonic-gate rval = YY_CMP_LT; 3700Sstevel@tonic-gate goto done; 3710Sstevel@tonic-gate 3720Sstevel@tonic-gate case '>' : 3730Sstevel@tonic-gate if (yyexpectaddr) 3740Sstevel@tonic-gate break; 3750Sstevel@tonic-gate if (isbuilding == 1) { 3760Sstevel@tonic-gate yyunputc(c); 3770Sstevel@tonic-gate goto done; 3780Sstevel@tonic-gate } 3790Sstevel@tonic-gate n = yygetc(); 3800Sstevel@tonic-gate if (n == '=') { 3810Sstevel@tonic-gate rval = YY_CMP_GE; 3820Sstevel@tonic-gate goto done; 3830Sstevel@tonic-gate } 3840Sstevel@tonic-gate if (n == '<') { 3850Sstevel@tonic-gate rval = YY_RANGE_IN; 3860Sstevel@tonic-gate goto done; 3870Sstevel@tonic-gate } 3880Sstevel@tonic-gate yyunputc(n); 3890Sstevel@tonic-gate rval = YY_CMP_GT; 3900Sstevel@tonic-gate goto done; 3910Sstevel@tonic-gate } 3920Sstevel@tonic-gate 3930Sstevel@tonic-gate /* 3940Sstevel@tonic-gate * Now for the reason this is here...IPv6 address parsing. 3950Sstevel@tonic-gate * The longest string we can expect is of this form: 3960Sstevel@tonic-gate * 0000:0000:0000:0000:0000:0000:000.000.000.000 3970Sstevel@tonic-gate * not: 3980Sstevel@tonic-gate * 0000:0000:0000:0000:0000:0000:0000:0000 3990Sstevel@tonic-gate */ 4000Sstevel@tonic-gate #ifdef USE_INET6 4010Sstevel@tonic-gate if (yyexpectaddr == 1 && isbuilding == 0 && (ishex(c) || c == ':')) { 4020Sstevel@tonic-gate char ipv6buf[45 + 1], *s, oc; 4030Sstevel@tonic-gate int start; 4040Sstevel@tonic-gate 4050Sstevel@tonic-gate start = yypos; 4060Sstevel@tonic-gate s = ipv6buf; 4070Sstevel@tonic-gate oc = c; 4080Sstevel@tonic-gate 4090Sstevel@tonic-gate /* 4100Sstevel@tonic-gate * Perhaps we should implement stricter controls on what we 4110Sstevel@tonic-gate * swallow up here, but surely it would just be duplicating 4120Sstevel@tonic-gate * the code in inet_pton() anyway. 4130Sstevel@tonic-gate */ 4140Sstevel@tonic-gate do { 4150Sstevel@tonic-gate *s++ = c; 4160Sstevel@tonic-gate c = yygetc(); 4170Sstevel@tonic-gate } while ((ishex(c) || c == ':' || c == '.') && 4180Sstevel@tonic-gate (s - ipv6buf < 46)); 4190Sstevel@tonic-gate yyunputc(c); 4200Sstevel@tonic-gate *s = '\0'; 4210Sstevel@tonic-gate 4220Sstevel@tonic-gate if (inet_pton(AF_INET6, ipv6buf, &yylval.ip6) == 1) { 4230Sstevel@tonic-gate rval = YY_IPV6; 4240Sstevel@tonic-gate yyexpectaddr = 0; 4250Sstevel@tonic-gate goto done; 4260Sstevel@tonic-gate } 4270Sstevel@tonic-gate yypos = start; 4280Sstevel@tonic-gate c = oc; 4290Sstevel@tonic-gate } 4300Sstevel@tonic-gate #endif 4310Sstevel@tonic-gate 4320Sstevel@tonic-gate if (c == ':') { 4330Sstevel@tonic-gate if (isbuilding == 1) { 4340Sstevel@tonic-gate yyunputc(c); 4350Sstevel@tonic-gate goto done; 4360Sstevel@tonic-gate } 4370Sstevel@tonic-gate rval = ':'; 4380Sstevel@tonic-gate goto done; 4390Sstevel@tonic-gate } 4400Sstevel@tonic-gate 4410Sstevel@tonic-gate if (isbuilding == 0 && c == '0') { 4420Sstevel@tonic-gate n = yygetc(); 4430Sstevel@tonic-gate if (n == 'x') { 4440Sstevel@tonic-gate do { 4450Sstevel@tonic-gate n = yygetc(); 4460Sstevel@tonic-gate } while (ishex(n)); 4470Sstevel@tonic-gate yyunputc(n); 4480Sstevel@tonic-gate rval = YY_HEX; 4490Sstevel@tonic-gate goto done; 4500Sstevel@tonic-gate } 4510Sstevel@tonic-gate yyunputc(n); 4520Sstevel@tonic-gate } 4530Sstevel@tonic-gate 4540Sstevel@tonic-gate /* 4550Sstevel@tonic-gate * No negative numbers with leading - sign.. 4560Sstevel@tonic-gate */ 457*2393Syz155240 if (isbuilding == 0 && ISDIGIT(c)) { 4580Sstevel@tonic-gate do { 4590Sstevel@tonic-gate n = yygetc(); 460*2393Syz155240 } while (ISDIGIT(n)); 4610Sstevel@tonic-gate yyunputc(n); 4620Sstevel@tonic-gate rval = YY_NUMBER; 4630Sstevel@tonic-gate goto done; 4640Sstevel@tonic-gate } 4650Sstevel@tonic-gate 4660Sstevel@tonic-gate isbuilding = 1; 4670Sstevel@tonic-gate goto nextchar; 4680Sstevel@tonic-gate 4690Sstevel@tonic-gate done: 4700Sstevel@tonic-gate yystr = yytexttostr(0, yypos); 4710Sstevel@tonic-gate 4720Sstevel@tonic-gate if (isbuilding == 1) { 4730Sstevel@tonic-gate wordtab_t *w; 4740Sstevel@tonic-gate 4750Sstevel@tonic-gate w = NULL; 4760Sstevel@tonic-gate isbuilding = 0; 4770Sstevel@tonic-gate 4780Sstevel@tonic-gate if ((yyvarnext == 0) && (nokey == 0)) { 4790Sstevel@tonic-gate w = yyfindkey(yystr); 4800Sstevel@tonic-gate if (w == NULL && yywordtab != NULL) { 4810Sstevel@tonic-gate yyresetdict(); 4820Sstevel@tonic-gate w = yyfindkey(yystr); 4830Sstevel@tonic-gate } 4840Sstevel@tonic-gate } else 4850Sstevel@tonic-gate yyvarnext = 0; 4860Sstevel@tonic-gate if (w != NULL) 4870Sstevel@tonic-gate rval = w->w_value; 4880Sstevel@tonic-gate else 4890Sstevel@tonic-gate rval = YY_STR; 4900Sstevel@tonic-gate } 4910Sstevel@tonic-gate 4920Sstevel@tonic-gate if (rval == YY_STR && yysavedepth > 0) 4930Sstevel@tonic-gate yyresetdict(); 4940Sstevel@tonic-gate 4950Sstevel@tonic-gate yytokentype = rval; 4960Sstevel@tonic-gate 4970Sstevel@tonic-gate if (yydebug) 498*2393Syz155240 printf("lexed(%s) [%d,%d,%d] => %d\n", yystr, string_start, 499*2393Syz155240 string_end, pos, rval); 5000Sstevel@tonic-gate 5010Sstevel@tonic-gate switch (rval) 5020Sstevel@tonic-gate { 5030Sstevel@tonic-gate case YY_NUMBER : 504*2393Syz155240 sscanf(yystr, "%u", &yylval.num); 5050Sstevel@tonic-gate break; 5060Sstevel@tonic-gate 5070Sstevel@tonic-gate case YY_HEX : 5080Sstevel@tonic-gate sscanf(yystr, "0x%x", (u_int *)&yylval.num); 5090Sstevel@tonic-gate break; 5100Sstevel@tonic-gate 5110Sstevel@tonic-gate case YY_STR : 5120Sstevel@tonic-gate yylval.str = strdup(yystr); 5130Sstevel@tonic-gate break; 5140Sstevel@tonic-gate 5150Sstevel@tonic-gate default : 5160Sstevel@tonic-gate break; 5170Sstevel@tonic-gate } 5180Sstevel@tonic-gate 5190Sstevel@tonic-gate if (yylast > 0) { 5200Sstevel@tonic-gate bcopy(yytext + yypos, yytext, 5210Sstevel@tonic-gate sizeof(yytext[0]) * (yylast - yypos + 1)); 5220Sstevel@tonic-gate yylast -= yypos; 5230Sstevel@tonic-gate yypos = 0; 5240Sstevel@tonic-gate } 5250Sstevel@tonic-gate 5260Sstevel@tonic-gate return rval; 5270Sstevel@tonic-gate } 5280Sstevel@tonic-gate 5290Sstevel@tonic-gate 5300Sstevel@tonic-gate static wordtab_t *yyfindkey(key) 5310Sstevel@tonic-gate char *key; 5320Sstevel@tonic-gate { 5330Sstevel@tonic-gate wordtab_t *w; 5340Sstevel@tonic-gate 5350Sstevel@tonic-gate if (yywordtab == NULL) 5360Sstevel@tonic-gate return NULL; 5370Sstevel@tonic-gate 5380Sstevel@tonic-gate for (w = yywordtab; w->w_word != 0; w++) 5390Sstevel@tonic-gate if (strcasecmp(key, w->w_word) == 0) 5400Sstevel@tonic-gate return w; 5410Sstevel@tonic-gate return NULL; 5420Sstevel@tonic-gate } 5430Sstevel@tonic-gate 5440Sstevel@tonic-gate 5450Sstevel@tonic-gate char *yykeytostr(num) 5460Sstevel@tonic-gate int num; 5470Sstevel@tonic-gate { 5480Sstevel@tonic-gate wordtab_t *w; 5490Sstevel@tonic-gate 5500Sstevel@tonic-gate if (yywordtab == NULL) 5510Sstevel@tonic-gate return "<unknown>"; 5520Sstevel@tonic-gate 5530Sstevel@tonic-gate for (w = yywordtab; w->w_word; w++) 5540Sstevel@tonic-gate if (w->w_value == num) 5550Sstevel@tonic-gate return w->w_word; 5560Sstevel@tonic-gate return "<unknown>"; 5570Sstevel@tonic-gate } 5580Sstevel@tonic-gate 5590Sstevel@tonic-gate 5600Sstevel@tonic-gate wordtab_t *yysettab(words) 5610Sstevel@tonic-gate wordtab_t *words; 5620Sstevel@tonic-gate { 5630Sstevel@tonic-gate wordtab_t *save; 5640Sstevel@tonic-gate 5650Sstevel@tonic-gate save = yywordtab; 5660Sstevel@tonic-gate yywordtab = words; 5670Sstevel@tonic-gate return save; 5680Sstevel@tonic-gate } 5690Sstevel@tonic-gate 5700Sstevel@tonic-gate 5710Sstevel@tonic-gate void yyerror(msg) 5720Sstevel@tonic-gate char *msg; 5730Sstevel@tonic-gate { 5740Sstevel@tonic-gate char *txt, letter[2]; 5750Sstevel@tonic-gate int freetxt = 0; 5760Sstevel@tonic-gate 5770Sstevel@tonic-gate if (yytokentype < 256) { 5780Sstevel@tonic-gate letter[0] = yytokentype; 5790Sstevel@tonic-gate letter[1] = '\0'; 5800Sstevel@tonic-gate txt = letter; 5810Sstevel@tonic-gate } else if (yytokentype == YY_STR || yytokentype == YY_HEX || 5820Sstevel@tonic-gate yytokentype == YY_NUMBER) { 5830Sstevel@tonic-gate if (yystr == NULL) { 5840Sstevel@tonic-gate txt = yytexttostr(yypos, YYBUFSIZ); 5851448Sschuster if (txt == NULL) { 5861448Sschuster fprintf(stderr, "sorry, out of memory," 5871448Sschuster " bailing out\n"); 5881448Sschuster exit(1); 5891448Sschuster } 5900Sstevel@tonic-gate freetxt = 1; 5910Sstevel@tonic-gate } else 5920Sstevel@tonic-gate txt = yystr; 5930Sstevel@tonic-gate } else { 5940Sstevel@tonic-gate txt = yykeytostr(yytokentype); 5951448Sschuster if (txt == NULL) { 5961448Sschuster fprintf(stderr, "sorry, out of memory," 5971448Sschuster " bailing out\n"); 5981448Sschuster exit(1); 5991448Sschuster } 6000Sstevel@tonic-gate } 6010Sstevel@tonic-gate fprintf(stderr, "%s error at \"%s\", line %d\n", msg, txt, yylineNum); 6020Sstevel@tonic-gate if (freetxt == 1) 6030Sstevel@tonic-gate free(txt); 6040Sstevel@tonic-gate exit(1); 6050Sstevel@tonic-gate } 6060Sstevel@tonic-gate 6070Sstevel@tonic-gate 6080Sstevel@tonic-gate void yysetdict(newdict) 6090Sstevel@tonic-gate wordtab_t *newdict; 6100Sstevel@tonic-gate { 6110Sstevel@tonic-gate if (yysavedepth == sizeof(yysavewords)/sizeof(yysavewords[0])) { 6120Sstevel@tonic-gate fprintf(stderr, "%d: at maximum dictionary depth\n", 6130Sstevel@tonic-gate yylineNum); 6140Sstevel@tonic-gate return; 6150Sstevel@tonic-gate } 6160Sstevel@tonic-gate 6170Sstevel@tonic-gate yysavewords[yysavedepth++] = yysettab(newdict); 6180Sstevel@tonic-gate if (yydebug) 6190Sstevel@tonic-gate printf("yysavedepth++ => %d\n", yysavedepth); 6200Sstevel@tonic-gate } 6210Sstevel@tonic-gate 6220Sstevel@tonic-gate void yyresetdict() 6230Sstevel@tonic-gate { 6240Sstevel@tonic-gate if (yysavedepth > 0) { 6250Sstevel@tonic-gate yysettab(yysavewords[--yysavedepth]); 6260Sstevel@tonic-gate if (yydebug) 6270Sstevel@tonic-gate printf("yysavedepth-- => %d\n", yysavedepth); 6280Sstevel@tonic-gate } 6290Sstevel@tonic-gate } 6300Sstevel@tonic-gate 6310Sstevel@tonic-gate 6320Sstevel@tonic-gate 6330Sstevel@tonic-gate #ifdef TEST_LEXER 6340Sstevel@tonic-gate int main(argc, argv) 6350Sstevel@tonic-gate int argc; 6360Sstevel@tonic-gate char *argv[]; 6370Sstevel@tonic-gate { 6380Sstevel@tonic-gate int n; 6390Sstevel@tonic-gate 6400Sstevel@tonic-gate yyin = stdin; 6410Sstevel@tonic-gate 6420Sstevel@tonic-gate while ((n = yylex()) != 0) 6430Sstevel@tonic-gate printf("%d.n = %d [%s] %d %d\n", 6440Sstevel@tonic-gate yylineNum, n, yystr, yypos, yylast); 6450Sstevel@tonic-gate } 6460Sstevel@tonic-gate #endif 647