17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * Copyright (C) 2004 by Darren Reed. 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * See the IPFILTER.LICENCE file for details on licencing. 57c478bd9Sstevel@tonic-gate * 6*ab25eeb5Syz155240 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 77c478bd9Sstevel@tonic-gate * Use is subject to license terms. 87c478bd9Sstevel@tonic-gate */ 97c478bd9Sstevel@tonic-gate 107c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 117c478bd9Sstevel@tonic-gate 127c478bd9Sstevel@tonic-gate #include <ctype.h> 137c478bd9Sstevel@tonic-gate #include "ipf.h" 147c478bd9Sstevel@tonic-gate #ifdef IPFILTER_SCAN 157c478bd9Sstevel@tonic-gate # include "netinet/ip_scan.h" 167c478bd9Sstevel@tonic-gate #endif 177c478bd9Sstevel@tonic-gate #include <sys/ioctl.h> 187c478bd9Sstevel@tonic-gate #include <syslog.h> 197c478bd9Sstevel@tonic-gate #ifdef TEST_LEXER 207c478bd9Sstevel@tonic-gate # define NO_YACC 217c478bd9Sstevel@tonic-gate union { 227c478bd9Sstevel@tonic-gate int num; 237c478bd9Sstevel@tonic-gate char *str; 247c478bd9Sstevel@tonic-gate struct in_addr ipa; 257c478bd9Sstevel@tonic-gate i6addr_t ip6; 267c478bd9Sstevel@tonic-gate } yylval; 277c478bd9Sstevel@tonic-gate #endif 287c478bd9Sstevel@tonic-gate #include "lexer.h" 297c478bd9Sstevel@tonic-gate #include "y.tab.h" 307c478bd9Sstevel@tonic-gate 317c478bd9Sstevel@tonic-gate FILE *yyin; 327c478bd9Sstevel@tonic-gate 33*ab25eeb5Syz155240 #define ishex(c) (ISDIGIT(c) || ((c) >= 'a' && (c) <= 'f') || \ 347c478bd9Sstevel@tonic-gate ((c) >= 'A' && (c) <= 'F')) 357c478bd9Sstevel@tonic-gate #define TOOLONG -3 367c478bd9Sstevel@tonic-gate 377c478bd9Sstevel@tonic-gate extern int string_start; 387c478bd9Sstevel@tonic-gate extern int string_end; 397c478bd9Sstevel@tonic-gate extern char *string_val; 407c478bd9Sstevel@tonic-gate extern int pos; 417c478bd9Sstevel@tonic-gate extern int yydebug; 427c478bd9Sstevel@tonic-gate 437c478bd9Sstevel@tonic-gate char *yystr = NULL; 447c478bd9Sstevel@tonic-gate int yytext[YYBUFSIZ+1]; 457c478bd9Sstevel@tonic-gate int yylineNum = 1; 467c478bd9Sstevel@tonic-gate int yypos = 0; 477c478bd9Sstevel@tonic-gate int yylast = -1; 487c478bd9Sstevel@tonic-gate int yyexpectaddr = 0; 497c478bd9Sstevel@tonic-gate int yybreakondot = 0; 507c478bd9Sstevel@tonic-gate int yyvarnext = 0; 517c478bd9Sstevel@tonic-gate int yytokentype = 0; 527c478bd9Sstevel@tonic-gate wordtab_t *yywordtab = NULL; 537c478bd9Sstevel@tonic-gate int yysavedepth = 0; 547c478bd9Sstevel@tonic-gate wordtab_t *yysavewords[30]; 557c478bd9Sstevel@tonic-gate 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate static wordtab_t *yyfindkey __P((char *)); 587c478bd9Sstevel@tonic-gate static int yygetc __P((void)); 597c478bd9Sstevel@tonic-gate static void yyunputc __P((int)); 607c478bd9Sstevel@tonic-gate static int yyswallow __P((int)); 617c478bd9Sstevel@tonic-gate static char *yytexttostr __P((int, int)); 627c478bd9Sstevel@tonic-gate static void yystrtotext __P((char *)); 637c478bd9Sstevel@tonic-gate 647c478bd9Sstevel@tonic-gate static int yygetc() 657c478bd9Sstevel@tonic-gate { 667c478bd9Sstevel@tonic-gate int c; 677c478bd9Sstevel@tonic-gate 687c478bd9Sstevel@tonic-gate if (yypos < yylast) { 697c478bd9Sstevel@tonic-gate c = yytext[yypos++]; 70*ab25eeb5Syz155240 if (c == '\n') 71*ab25eeb5Syz155240 yylineNum++; 727c478bd9Sstevel@tonic-gate return c; 737c478bd9Sstevel@tonic-gate } 747c478bd9Sstevel@tonic-gate 757c478bd9Sstevel@tonic-gate if (yypos == YYBUFSIZ) 767c478bd9Sstevel@tonic-gate return TOOLONG; 777c478bd9Sstevel@tonic-gate 787c478bd9Sstevel@tonic-gate if (pos >= string_start && pos <= string_end) { 797c478bd9Sstevel@tonic-gate c = string_val[pos - string_start]; 807c478bd9Sstevel@tonic-gate yypos++; 817c478bd9Sstevel@tonic-gate } else { 827c478bd9Sstevel@tonic-gate c = fgetc(yyin); 83*ab25eeb5Syz155240 } 847c478bd9Sstevel@tonic-gate if (c == '\n') 857c478bd9Sstevel@tonic-gate yylineNum++; 867c478bd9Sstevel@tonic-gate yytext[yypos++] = c; 877c478bd9Sstevel@tonic-gate yylast = yypos; 887c478bd9Sstevel@tonic-gate yytext[yypos] = '\0'; 897c478bd9Sstevel@tonic-gate 907c478bd9Sstevel@tonic-gate return c; 917c478bd9Sstevel@tonic-gate } 927c478bd9Sstevel@tonic-gate 937c478bd9Sstevel@tonic-gate 947c478bd9Sstevel@tonic-gate static void yyunputc(c) 957c478bd9Sstevel@tonic-gate int c; 967c478bd9Sstevel@tonic-gate { 97*ab25eeb5Syz155240 if (c == '\n') 98*ab25eeb5Syz155240 yylineNum--; 997c478bd9Sstevel@tonic-gate yytext[--yypos] = c; 1007c478bd9Sstevel@tonic-gate } 1017c478bd9Sstevel@tonic-gate 1027c478bd9Sstevel@tonic-gate 1037c478bd9Sstevel@tonic-gate static int yyswallow(last) 1047c478bd9Sstevel@tonic-gate int last; 1057c478bd9Sstevel@tonic-gate { 1067c478bd9Sstevel@tonic-gate int c; 1077c478bd9Sstevel@tonic-gate 1087c478bd9Sstevel@tonic-gate while (((c = yygetc()) > '\0') && (c != last)) 1097c478bd9Sstevel@tonic-gate ; 1107c478bd9Sstevel@tonic-gate 1117c478bd9Sstevel@tonic-gate if (c != EOF) 1127c478bd9Sstevel@tonic-gate yyunputc(c); 1137c478bd9Sstevel@tonic-gate if (c == last) 1147c478bd9Sstevel@tonic-gate return 0; 1157c478bd9Sstevel@tonic-gate return -1; 1167c478bd9Sstevel@tonic-gate } 1177c478bd9Sstevel@tonic-gate 1187c478bd9Sstevel@tonic-gate 1197c478bd9Sstevel@tonic-gate static void yystrtotext(str) 1207c478bd9Sstevel@tonic-gate char *str; 1217c478bd9Sstevel@tonic-gate { 1227c478bd9Sstevel@tonic-gate int len; 1237c478bd9Sstevel@tonic-gate char *s; 1247c478bd9Sstevel@tonic-gate 1257c478bd9Sstevel@tonic-gate len = strlen(str); 1267c478bd9Sstevel@tonic-gate if (len > YYBUFSIZ) 1277c478bd9Sstevel@tonic-gate len = YYBUFSIZ; 1287c478bd9Sstevel@tonic-gate 1297c478bd9Sstevel@tonic-gate for (s = str; *s != '\0' && len > 0; s++, len--) 1307c478bd9Sstevel@tonic-gate yytext[yylast++] = *s; 1317c478bd9Sstevel@tonic-gate yytext[yylast] = '\0'; 1327c478bd9Sstevel@tonic-gate } 1337c478bd9Sstevel@tonic-gate 1347c478bd9Sstevel@tonic-gate 1357c478bd9Sstevel@tonic-gate static char *yytexttostr(offset, max) 1367c478bd9Sstevel@tonic-gate int offset, max; 1377c478bd9Sstevel@tonic-gate { 1387c478bd9Sstevel@tonic-gate char *str; 1397c478bd9Sstevel@tonic-gate int i; 1407c478bd9Sstevel@tonic-gate 1417c478bd9Sstevel@tonic-gate if ((yytext[offset] == '\'' || yytext[offset] == '"') && 1427c478bd9Sstevel@tonic-gate (yytext[offset] == yytext[offset + max - 1])) { 1437c478bd9Sstevel@tonic-gate offset++; 1447c478bd9Sstevel@tonic-gate max--; 1457c478bd9Sstevel@tonic-gate } 1467c478bd9Sstevel@tonic-gate 1477c478bd9Sstevel@tonic-gate if (max > yylast) 1487c478bd9Sstevel@tonic-gate max = yylast; 1497c478bd9Sstevel@tonic-gate str = malloc(max + 1); 1507c478bd9Sstevel@tonic-gate if (str != NULL) { 1517c478bd9Sstevel@tonic-gate for (i = offset; i < max; i++) 1527c478bd9Sstevel@tonic-gate str[i - offset] = (char)(yytext[i] & 0xff); 1537c478bd9Sstevel@tonic-gate str[i - offset] = '\0'; 1547c478bd9Sstevel@tonic-gate } 1557c478bd9Sstevel@tonic-gate return str; 1567c478bd9Sstevel@tonic-gate } 1577c478bd9Sstevel@tonic-gate 1587c478bd9Sstevel@tonic-gate 1597c478bd9Sstevel@tonic-gate int yylex() 1607c478bd9Sstevel@tonic-gate { 1617c478bd9Sstevel@tonic-gate int c, n, isbuilding, rval, lnext, nokey = 0; 1627c478bd9Sstevel@tonic-gate char *name; 1637c478bd9Sstevel@tonic-gate 1647c478bd9Sstevel@tonic-gate isbuilding = 0; 1657c478bd9Sstevel@tonic-gate lnext = 0; 1667c478bd9Sstevel@tonic-gate rval = 0; 1677c478bd9Sstevel@tonic-gate 1687c478bd9Sstevel@tonic-gate if (yystr != NULL) { 1697c478bd9Sstevel@tonic-gate free(yystr); 1707c478bd9Sstevel@tonic-gate yystr = NULL; 1717c478bd9Sstevel@tonic-gate } 1727c478bd9Sstevel@tonic-gate 1737c478bd9Sstevel@tonic-gate nextchar: 1747c478bd9Sstevel@tonic-gate c = yygetc(); 1757c478bd9Sstevel@tonic-gate 1767c478bd9Sstevel@tonic-gate switch (c) 1777c478bd9Sstevel@tonic-gate { 1787c478bd9Sstevel@tonic-gate case '\n' : 1797c478bd9Sstevel@tonic-gate case '\t' : 1807c478bd9Sstevel@tonic-gate case '\r' : 1817c478bd9Sstevel@tonic-gate case ' ' : 1827c478bd9Sstevel@tonic-gate if (isbuilding == 1) { 1837c478bd9Sstevel@tonic-gate yyunputc(c); 1847c478bd9Sstevel@tonic-gate goto done; 1857c478bd9Sstevel@tonic-gate } 1867c478bd9Sstevel@tonic-gate if (yylast > yypos) { 1877c478bd9Sstevel@tonic-gate bcopy(yytext + yypos, yytext, 1887c478bd9Sstevel@tonic-gate sizeof(yytext[0]) * (yylast - yypos + 1)); 1897c478bd9Sstevel@tonic-gate } 1907c478bd9Sstevel@tonic-gate yylast -= yypos; 1917c478bd9Sstevel@tonic-gate yypos = 0; 192*ab25eeb5Syz155240 lnext = 0; 193*ab25eeb5Syz155240 nokey = 0; 1947c478bd9Sstevel@tonic-gate goto nextchar; 1957c478bd9Sstevel@tonic-gate 1967c478bd9Sstevel@tonic-gate case '\\' : 1977c478bd9Sstevel@tonic-gate if (lnext == 0) { 1987c478bd9Sstevel@tonic-gate lnext = 1; 1997c478bd9Sstevel@tonic-gate if (yylast == yypos) { 2007c478bd9Sstevel@tonic-gate yylast--; 2017c478bd9Sstevel@tonic-gate yypos--; 2027c478bd9Sstevel@tonic-gate } else 2037c478bd9Sstevel@tonic-gate yypos--; 2047c478bd9Sstevel@tonic-gate if (yypos == 0) 2057c478bd9Sstevel@tonic-gate nokey = 1; 2067c478bd9Sstevel@tonic-gate goto nextchar; 2077c478bd9Sstevel@tonic-gate } 2087c478bd9Sstevel@tonic-gate break; 2097c478bd9Sstevel@tonic-gate } 2107c478bd9Sstevel@tonic-gate 2117c478bd9Sstevel@tonic-gate if (lnext == 1) { 2127c478bd9Sstevel@tonic-gate lnext = 0; 213*ab25eeb5Syz155240 if ((isbuilding == 0) && !ISALNUM(c)) { 214*ab25eeb5Syz155240 return c; 215*ab25eeb5Syz155240 } 2167c478bd9Sstevel@tonic-gate goto nextchar; 2177c478bd9Sstevel@tonic-gate } 2187c478bd9Sstevel@tonic-gate 2197c478bd9Sstevel@tonic-gate switch (c) 2207c478bd9Sstevel@tonic-gate { 2217c478bd9Sstevel@tonic-gate case '#' : 2227c478bd9Sstevel@tonic-gate if (isbuilding == 1) { 2237c478bd9Sstevel@tonic-gate yyunputc(c); 2247c478bd9Sstevel@tonic-gate goto done; 2257c478bd9Sstevel@tonic-gate } 2267c478bd9Sstevel@tonic-gate yyswallow('\n'); 2277c478bd9Sstevel@tonic-gate rval = YY_COMMENT; 2287c478bd9Sstevel@tonic-gate goto done; 2297c478bd9Sstevel@tonic-gate 2307c478bd9Sstevel@tonic-gate case '$' : 2317c478bd9Sstevel@tonic-gate if (isbuilding == 1) { 2327c478bd9Sstevel@tonic-gate yyunputc(c); 2337c478bd9Sstevel@tonic-gate goto done; 2347c478bd9Sstevel@tonic-gate } 2357c478bd9Sstevel@tonic-gate n = yygetc(); 2367c478bd9Sstevel@tonic-gate if (n == '{') { 2377c478bd9Sstevel@tonic-gate if (yyswallow('}') == -1) { 2387c478bd9Sstevel@tonic-gate rval = -2; 2397c478bd9Sstevel@tonic-gate goto done; 2407c478bd9Sstevel@tonic-gate } 2417c478bd9Sstevel@tonic-gate (void) yygetc(); 2427c478bd9Sstevel@tonic-gate } else { 243*ab25eeb5Syz155240 if (!ISALPHA(n)) { 2447c478bd9Sstevel@tonic-gate yyunputc(n); 2457c478bd9Sstevel@tonic-gate break; 2467c478bd9Sstevel@tonic-gate } 2477c478bd9Sstevel@tonic-gate do { 2487c478bd9Sstevel@tonic-gate n = yygetc(); 249*ab25eeb5Syz155240 } while (ISALPHA(n) || ISDIGIT(n) || n == '_'); 2507c478bd9Sstevel@tonic-gate yyunputc(n); 2517c478bd9Sstevel@tonic-gate } 2527c478bd9Sstevel@tonic-gate 2537c478bd9Sstevel@tonic-gate name = yytexttostr(1, yypos); /* skip $ */ 2547c478bd9Sstevel@tonic-gate 2557c478bd9Sstevel@tonic-gate if (name != NULL) { 2567c478bd9Sstevel@tonic-gate string_val = get_variable(name, NULL, yylineNum); 2577c478bd9Sstevel@tonic-gate free(name); 2587c478bd9Sstevel@tonic-gate if (string_val != NULL) { 2597c478bd9Sstevel@tonic-gate name = yytexttostr(yypos, yylast); 2607c478bd9Sstevel@tonic-gate if (name != NULL) { 2617c478bd9Sstevel@tonic-gate yypos = 0; 2627c478bd9Sstevel@tonic-gate yylast = 0; 2637c478bd9Sstevel@tonic-gate yystrtotext(string_val); 2647c478bd9Sstevel@tonic-gate yystrtotext(name); 2657c478bd9Sstevel@tonic-gate free(string_val); 2667c478bd9Sstevel@tonic-gate free(name); 2677c478bd9Sstevel@tonic-gate goto nextchar; 2687c478bd9Sstevel@tonic-gate } 2697c478bd9Sstevel@tonic-gate free(string_val); 2707c478bd9Sstevel@tonic-gate } 2717c478bd9Sstevel@tonic-gate } 2727c478bd9Sstevel@tonic-gate break; 2737c478bd9Sstevel@tonic-gate 2747c478bd9Sstevel@tonic-gate case '\'': 2757c478bd9Sstevel@tonic-gate case '"' : 2767c478bd9Sstevel@tonic-gate if (isbuilding == 1) { 2777c478bd9Sstevel@tonic-gate goto done; 2787c478bd9Sstevel@tonic-gate } 2797c478bd9Sstevel@tonic-gate do { 2807c478bd9Sstevel@tonic-gate n = yygetc(); 2817c478bd9Sstevel@tonic-gate if (n == EOF || n == TOOLONG) { 2827c478bd9Sstevel@tonic-gate rval = -2; 2837c478bd9Sstevel@tonic-gate goto done; 2847c478bd9Sstevel@tonic-gate } 2857c478bd9Sstevel@tonic-gate if (n == '\n') { 2867c478bd9Sstevel@tonic-gate yyunputc(' '); 2877c478bd9Sstevel@tonic-gate yypos++; 2887c478bd9Sstevel@tonic-gate } 2897c478bd9Sstevel@tonic-gate } while (n != c); 2907c478bd9Sstevel@tonic-gate yyunputc(n); 2917c478bd9Sstevel@tonic-gate break; 2927c478bd9Sstevel@tonic-gate 2937c478bd9Sstevel@tonic-gate case EOF : 2947c478bd9Sstevel@tonic-gate yylineNum = 1; 2957c478bd9Sstevel@tonic-gate yypos = 0; 2967c478bd9Sstevel@tonic-gate yylast = -1; 2977c478bd9Sstevel@tonic-gate yyexpectaddr = 0; 2987c478bd9Sstevel@tonic-gate yybreakondot = 0; 2997c478bd9Sstevel@tonic-gate yyvarnext = 0; 3007c478bd9Sstevel@tonic-gate yytokentype = 0; 3017c478bd9Sstevel@tonic-gate return 0; 3027c478bd9Sstevel@tonic-gate } 3037c478bd9Sstevel@tonic-gate 3047c478bd9Sstevel@tonic-gate if (strchr("=,/;{}()@", c) != NULL) { 3057c478bd9Sstevel@tonic-gate if (isbuilding == 1) { 3067c478bd9Sstevel@tonic-gate yyunputc(c); 3077c478bd9Sstevel@tonic-gate goto done; 3087c478bd9Sstevel@tonic-gate } 3097c478bd9Sstevel@tonic-gate rval = c; 3107c478bd9Sstevel@tonic-gate goto done; 3117c478bd9Sstevel@tonic-gate } else if (c == '.') { 3127c478bd9Sstevel@tonic-gate if (isbuilding == 0) { 3137c478bd9Sstevel@tonic-gate rval = c; 3147c478bd9Sstevel@tonic-gate goto done; 3157c478bd9Sstevel@tonic-gate } 3167c478bd9Sstevel@tonic-gate if (yybreakondot != 0) { 3177c478bd9Sstevel@tonic-gate yyunputc(c); 3187c478bd9Sstevel@tonic-gate goto done; 3197c478bd9Sstevel@tonic-gate } 3207c478bd9Sstevel@tonic-gate } 3217c478bd9Sstevel@tonic-gate 3227c478bd9Sstevel@tonic-gate switch (c) 3237c478bd9Sstevel@tonic-gate { 3247c478bd9Sstevel@tonic-gate case '-' : 3257c478bd9Sstevel@tonic-gate if (yyexpectaddr) 3267c478bd9Sstevel@tonic-gate break; 3277c478bd9Sstevel@tonic-gate if (isbuilding == 1) 3287c478bd9Sstevel@tonic-gate break; 3297c478bd9Sstevel@tonic-gate n = yygetc(); 3307c478bd9Sstevel@tonic-gate if (n == '>') { 3317c478bd9Sstevel@tonic-gate isbuilding = 1; 3327c478bd9Sstevel@tonic-gate goto done; 3337c478bd9Sstevel@tonic-gate } 3347c478bd9Sstevel@tonic-gate yyunputc(n); 3357c478bd9Sstevel@tonic-gate rval = '-'; 3367c478bd9Sstevel@tonic-gate goto done; 3377c478bd9Sstevel@tonic-gate 3387c478bd9Sstevel@tonic-gate case '!' : 3397c478bd9Sstevel@tonic-gate if (isbuilding == 1) { 3407c478bd9Sstevel@tonic-gate yyunputc(c); 3417c478bd9Sstevel@tonic-gate goto done; 3427c478bd9Sstevel@tonic-gate } 3437c478bd9Sstevel@tonic-gate n = yygetc(); 3447c478bd9Sstevel@tonic-gate if (n == '=') { 3457c478bd9Sstevel@tonic-gate rval = YY_CMP_NE; 3467c478bd9Sstevel@tonic-gate goto done; 3477c478bd9Sstevel@tonic-gate } 3487c478bd9Sstevel@tonic-gate yyunputc(n); 3497c478bd9Sstevel@tonic-gate rval = '!'; 3507c478bd9Sstevel@tonic-gate goto done; 3517c478bd9Sstevel@tonic-gate 3527c478bd9Sstevel@tonic-gate case '<' : 3537c478bd9Sstevel@tonic-gate if (yyexpectaddr) 3547c478bd9Sstevel@tonic-gate break; 3557c478bd9Sstevel@tonic-gate if (isbuilding == 1) { 3567c478bd9Sstevel@tonic-gate yyunputc(c); 3577c478bd9Sstevel@tonic-gate goto done; 3587c478bd9Sstevel@tonic-gate } 3597c478bd9Sstevel@tonic-gate n = yygetc(); 3607c478bd9Sstevel@tonic-gate if (n == '=') { 3617c478bd9Sstevel@tonic-gate rval = YY_CMP_LE; 3627c478bd9Sstevel@tonic-gate goto done; 3637c478bd9Sstevel@tonic-gate } 3647c478bd9Sstevel@tonic-gate if (n == '>') { 3657c478bd9Sstevel@tonic-gate rval = YY_RANGE_OUT; 3667c478bd9Sstevel@tonic-gate goto done; 3677c478bd9Sstevel@tonic-gate } 3687c478bd9Sstevel@tonic-gate yyunputc(n); 3697c478bd9Sstevel@tonic-gate rval = YY_CMP_LT; 3707c478bd9Sstevel@tonic-gate goto done; 3717c478bd9Sstevel@tonic-gate 3727c478bd9Sstevel@tonic-gate case '>' : 3737c478bd9Sstevel@tonic-gate if (yyexpectaddr) 3747c478bd9Sstevel@tonic-gate break; 3757c478bd9Sstevel@tonic-gate if (isbuilding == 1) { 3767c478bd9Sstevel@tonic-gate yyunputc(c); 3777c478bd9Sstevel@tonic-gate goto done; 3787c478bd9Sstevel@tonic-gate } 3797c478bd9Sstevel@tonic-gate n = yygetc(); 3807c478bd9Sstevel@tonic-gate if (n == '=') { 3817c478bd9Sstevel@tonic-gate rval = YY_CMP_GE; 3827c478bd9Sstevel@tonic-gate goto done; 3837c478bd9Sstevel@tonic-gate } 3847c478bd9Sstevel@tonic-gate if (n == '<') { 3857c478bd9Sstevel@tonic-gate rval = YY_RANGE_IN; 3867c478bd9Sstevel@tonic-gate goto done; 3877c478bd9Sstevel@tonic-gate } 3887c478bd9Sstevel@tonic-gate yyunputc(n); 3897c478bd9Sstevel@tonic-gate rval = YY_CMP_GT; 3907c478bd9Sstevel@tonic-gate goto done; 3917c478bd9Sstevel@tonic-gate } 3927c478bd9Sstevel@tonic-gate 3937c478bd9Sstevel@tonic-gate /* 3947c478bd9Sstevel@tonic-gate * Now for the reason this is here...IPv6 address parsing. 3957c478bd9Sstevel@tonic-gate * The longest string we can expect is of this form: 3967c478bd9Sstevel@tonic-gate * 0000:0000:0000:0000:0000:0000:000.000.000.000 3977c478bd9Sstevel@tonic-gate * not: 3987c478bd9Sstevel@tonic-gate * 0000:0000:0000:0000:0000:0000:0000:0000 3997c478bd9Sstevel@tonic-gate */ 4007c478bd9Sstevel@tonic-gate #ifdef USE_INET6 4017c478bd9Sstevel@tonic-gate if (yyexpectaddr == 1 && isbuilding == 0 && (ishex(c) || c == ':')) { 4027c478bd9Sstevel@tonic-gate char ipv6buf[45 + 1], *s, oc; 4037c478bd9Sstevel@tonic-gate int start; 4047c478bd9Sstevel@tonic-gate 4057c478bd9Sstevel@tonic-gate start = yypos; 4067c478bd9Sstevel@tonic-gate s = ipv6buf; 4077c478bd9Sstevel@tonic-gate oc = c; 4087c478bd9Sstevel@tonic-gate 4097c478bd9Sstevel@tonic-gate /* 4107c478bd9Sstevel@tonic-gate * Perhaps we should implement stricter controls on what we 4117c478bd9Sstevel@tonic-gate * swallow up here, but surely it would just be duplicating 4127c478bd9Sstevel@tonic-gate * the code in inet_pton() anyway. 4137c478bd9Sstevel@tonic-gate */ 4147c478bd9Sstevel@tonic-gate do { 4157c478bd9Sstevel@tonic-gate *s++ = c; 4167c478bd9Sstevel@tonic-gate c = yygetc(); 4177c478bd9Sstevel@tonic-gate } while ((ishex(c) || c == ':' || c == '.') && 4187c478bd9Sstevel@tonic-gate (s - ipv6buf < 46)); 4197c478bd9Sstevel@tonic-gate yyunputc(c); 4207c478bd9Sstevel@tonic-gate *s = '\0'; 4217c478bd9Sstevel@tonic-gate 4227c478bd9Sstevel@tonic-gate if (inet_pton(AF_INET6, ipv6buf, &yylval.ip6) == 1) { 4237c478bd9Sstevel@tonic-gate rval = YY_IPV6; 4247c478bd9Sstevel@tonic-gate yyexpectaddr = 0; 4257c478bd9Sstevel@tonic-gate goto done; 4267c478bd9Sstevel@tonic-gate } 4277c478bd9Sstevel@tonic-gate yypos = start; 4287c478bd9Sstevel@tonic-gate c = oc; 4297c478bd9Sstevel@tonic-gate } 4307c478bd9Sstevel@tonic-gate #endif 4317c478bd9Sstevel@tonic-gate 4327c478bd9Sstevel@tonic-gate if (c == ':') { 4337c478bd9Sstevel@tonic-gate if (isbuilding == 1) { 4347c478bd9Sstevel@tonic-gate yyunputc(c); 4357c478bd9Sstevel@tonic-gate goto done; 4367c478bd9Sstevel@tonic-gate } 4377c478bd9Sstevel@tonic-gate rval = ':'; 4387c478bd9Sstevel@tonic-gate goto done; 4397c478bd9Sstevel@tonic-gate } 4407c478bd9Sstevel@tonic-gate 4417c478bd9Sstevel@tonic-gate if (isbuilding == 0 && c == '0') { 4427c478bd9Sstevel@tonic-gate n = yygetc(); 4437c478bd9Sstevel@tonic-gate if (n == 'x') { 4447c478bd9Sstevel@tonic-gate do { 4457c478bd9Sstevel@tonic-gate n = yygetc(); 4467c478bd9Sstevel@tonic-gate } while (ishex(n)); 4477c478bd9Sstevel@tonic-gate yyunputc(n); 4487c478bd9Sstevel@tonic-gate rval = YY_HEX; 4497c478bd9Sstevel@tonic-gate goto done; 4507c478bd9Sstevel@tonic-gate } 4517c478bd9Sstevel@tonic-gate yyunputc(n); 4527c478bd9Sstevel@tonic-gate } 4537c478bd9Sstevel@tonic-gate 4547c478bd9Sstevel@tonic-gate /* 4557c478bd9Sstevel@tonic-gate * No negative numbers with leading - sign.. 4567c478bd9Sstevel@tonic-gate */ 457*ab25eeb5Syz155240 if (isbuilding == 0 && ISDIGIT(c)) { 4587c478bd9Sstevel@tonic-gate do { 4597c478bd9Sstevel@tonic-gate n = yygetc(); 460*ab25eeb5Syz155240 } while (ISDIGIT(n)); 4617c478bd9Sstevel@tonic-gate yyunputc(n); 4627c478bd9Sstevel@tonic-gate rval = YY_NUMBER; 4637c478bd9Sstevel@tonic-gate goto done; 4647c478bd9Sstevel@tonic-gate } 4657c478bd9Sstevel@tonic-gate 4667c478bd9Sstevel@tonic-gate isbuilding = 1; 4677c478bd9Sstevel@tonic-gate goto nextchar; 4687c478bd9Sstevel@tonic-gate 4697c478bd9Sstevel@tonic-gate done: 4707c478bd9Sstevel@tonic-gate yystr = yytexttostr(0, yypos); 4717c478bd9Sstevel@tonic-gate 4727c478bd9Sstevel@tonic-gate if (isbuilding == 1) { 4737c478bd9Sstevel@tonic-gate wordtab_t *w; 4747c478bd9Sstevel@tonic-gate 4757c478bd9Sstevel@tonic-gate w = NULL; 4767c478bd9Sstevel@tonic-gate isbuilding = 0; 4777c478bd9Sstevel@tonic-gate 4787c478bd9Sstevel@tonic-gate if ((yyvarnext == 0) && (nokey == 0)) { 4797c478bd9Sstevel@tonic-gate w = yyfindkey(yystr); 4807c478bd9Sstevel@tonic-gate if (w == NULL && yywordtab != NULL) { 4817c478bd9Sstevel@tonic-gate yyresetdict(); 4827c478bd9Sstevel@tonic-gate w = yyfindkey(yystr); 4837c478bd9Sstevel@tonic-gate } 4847c478bd9Sstevel@tonic-gate } else 4857c478bd9Sstevel@tonic-gate yyvarnext = 0; 4867c478bd9Sstevel@tonic-gate if (w != NULL) 4877c478bd9Sstevel@tonic-gate rval = w->w_value; 4887c478bd9Sstevel@tonic-gate else 4897c478bd9Sstevel@tonic-gate rval = YY_STR; 4907c478bd9Sstevel@tonic-gate } 4917c478bd9Sstevel@tonic-gate 4927c478bd9Sstevel@tonic-gate if (rval == YY_STR && yysavedepth > 0) 4937c478bd9Sstevel@tonic-gate yyresetdict(); 4947c478bd9Sstevel@tonic-gate 4957c478bd9Sstevel@tonic-gate yytokentype = rval; 4967c478bd9Sstevel@tonic-gate 4977c478bd9Sstevel@tonic-gate if (yydebug) 498*ab25eeb5Syz155240 printf("lexed(%s) [%d,%d,%d] => %d\n", yystr, string_start, 499*ab25eeb5Syz155240 string_end, pos, rval); 5007c478bd9Sstevel@tonic-gate 5017c478bd9Sstevel@tonic-gate switch (rval) 5027c478bd9Sstevel@tonic-gate { 5037c478bd9Sstevel@tonic-gate case YY_NUMBER : 504*ab25eeb5Syz155240 sscanf(yystr, "%u", &yylval.num); 5057c478bd9Sstevel@tonic-gate break; 5067c478bd9Sstevel@tonic-gate 5077c478bd9Sstevel@tonic-gate case YY_HEX : 5087c478bd9Sstevel@tonic-gate sscanf(yystr, "0x%x", (u_int *)&yylval.num); 5097c478bd9Sstevel@tonic-gate break; 5107c478bd9Sstevel@tonic-gate 5117c478bd9Sstevel@tonic-gate case YY_STR : 5127c478bd9Sstevel@tonic-gate yylval.str = strdup(yystr); 5137c478bd9Sstevel@tonic-gate break; 5147c478bd9Sstevel@tonic-gate 5157c478bd9Sstevel@tonic-gate default : 5167c478bd9Sstevel@tonic-gate break; 5177c478bd9Sstevel@tonic-gate } 5187c478bd9Sstevel@tonic-gate 5197c478bd9Sstevel@tonic-gate if (yylast > 0) { 5207c478bd9Sstevel@tonic-gate bcopy(yytext + yypos, yytext, 5217c478bd9Sstevel@tonic-gate sizeof(yytext[0]) * (yylast - yypos + 1)); 5227c478bd9Sstevel@tonic-gate yylast -= yypos; 5237c478bd9Sstevel@tonic-gate yypos = 0; 5247c478bd9Sstevel@tonic-gate } 5257c478bd9Sstevel@tonic-gate 5267c478bd9Sstevel@tonic-gate return rval; 5277c478bd9Sstevel@tonic-gate } 5287c478bd9Sstevel@tonic-gate 5297c478bd9Sstevel@tonic-gate 5307c478bd9Sstevel@tonic-gate static wordtab_t *yyfindkey(key) 5317c478bd9Sstevel@tonic-gate char *key; 5327c478bd9Sstevel@tonic-gate { 5337c478bd9Sstevel@tonic-gate wordtab_t *w; 5347c478bd9Sstevel@tonic-gate 5357c478bd9Sstevel@tonic-gate if (yywordtab == NULL) 5367c478bd9Sstevel@tonic-gate return NULL; 5377c478bd9Sstevel@tonic-gate 5387c478bd9Sstevel@tonic-gate for (w = yywordtab; w->w_word != 0; w++) 5397c478bd9Sstevel@tonic-gate if (strcasecmp(key, w->w_word) == 0) 5407c478bd9Sstevel@tonic-gate return w; 5417c478bd9Sstevel@tonic-gate return NULL; 5427c478bd9Sstevel@tonic-gate } 5437c478bd9Sstevel@tonic-gate 5447c478bd9Sstevel@tonic-gate 5457c478bd9Sstevel@tonic-gate char *yykeytostr(num) 5467c478bd9Sstevel@tonic-gate int num; 5477c478bd9Sstevel@tonic-gate { 5487c478bd9Sstevel@tonic-gate wordtab_t *w; 5497c478bd9Sstevel@tonic-gate 5507c478bd9Sstevel@tonic-gate if (yywordtab == NULL) 5517c478bd9Sstevel@tonic-gate return "<unknown>"; 5527c478bd9Sstevel@tonic-gate 5537c478bd9Sstevel@tonic-gate for (w = yywordtab; w->w_word; w++) 5547c478bd9Sstevel@tonic-gate if (w->w_value == num) 5557c478bd9Sstevel@tonic-gate return w->w_word; 5567c478bd9Sstevel@tonic-gate return "<unknown>"; 5577c478bd9Sstevel@tonic-gate } 5587c478bd9Sstevel@tonic-gate 5597c478bd9Sstevel@tonic-gate 5607c478bd9Sstevel@tonic-gate wordtab_t *yysettab(words) 5617c478bd9Sstevel@tonic-gate wordtab_t *words; 5627c478bd9Sstevel@tonic-gate { 5637c478bd9Sstevel@tonic-gate wordtab_t *save; 5647c478bd9Sstevel@tonic-gate 5657c478bd9Sstevel@tonic-gate save = yywordtab; 5667c478bd9Sstevel@tonic-gate yywordtab = words; 5677c478bd9Sstevel@tonic-gate return save; 5687c478bd9Sstevel@tonic-gate } 5697c478bd9Sstevel@tonic-gate 5707c478bd9Sstevel@tonic-gate 5717c478bd9Sstevel@tonic-gate void yyerror(msg) 5727c478bd9Sstevel@tonic-gate char *msg; 5737c478bd9Sstevel@tonic-gate { 5747c478bd9Sstevel@tonic-gate char *txt, letter[2]; 5757c478bd9Sstevel@tonic-gate int freetxt = 0; 5767c478bd9Sstevel@tonic-gate 5777c478bd9Sstevel@tonic-gate if (yytokentype < 256) { 5787c478bd9Sstevel@tonic-gate letter[0] = yytokentype; 5797c478bd9Sstevel@tonic-gate letter[1] = '\0'; 5807c478bd9Sstevel@tonic-gate txt = letter; 5817c478bd9Sstevel@tonic-gate } else if (yytokentype == YY_STR || yytokentype == YY_HEX || 5827c478bd9Sstevel@tonic-gate yytokentype == YY_NUMBER) { 5837c478bd9Sstevel@tonic-gate if (yystr == NULL) { 5847c478bd9Sstevel@tonic-gate txt = yytexttostr(yypos, YYBUFSIZ); 5855e985db5Sschuster if (txt == NULL) { 5865e985db5Sschuster fprintf(stderr, "sorry, out of memory," 5875e985db5Sschuster " bailing out\n"); 5885e985db5Sschuster exit(1); 5895e985db5Sschuster } 5907c478bd9Sstevel@tonic-gate freetxt = 1; 5917c478bd9Sstevel@tonic-gate } else 5927c478bd9Sstevel@tonic-gate txt = yystr; 5937c478bd9Sstevel@tonic-gate } else { 5947c478bd9Sstevel@tonic-gate txt = yykeytostr(yytokentype); 5955e985db5Sschuster if (txt == NULL) { 5965e985db5Sschuster fprintf(stderr, "sorry, out of memory," 5975e985db5Sschuster " bailing out\n"); 5985e985db5Sschuster exit(1); 5995e985db5Sschuster } 6007c478bd9Sstevel@tonic-gate } 6017c478bd9Sstevel@tonic-gate fprintf(stderr, "%s error at \"%s\", line %d\n", msg, txt, yylineNum); 6027c478bd9Sstevel@tonic-gate if (freetxt == 1) 6037c478bd9Sstevel@tonic-gate free(txt); 6047c478bd9Sstevel@tonic-gate exit(1); 6057c478bd9Sstevel@tonic-gate } 6067c478bd9Sstevel@tonic-gate 6077c478bd9Sstevel@tonic-gate 6087c478bd9Sstevel@tonic-gate void yysetdict(newdict) 6097c478bd9Sstevel@tonic-gate wordtab_t *newdict; 6107c478bd9Sstevel@tonic-gate { 6117c478bd9Sstevel@tonic-gate if (yysavedepth == sizeof(yysavewords)/sizeof(yysavewords[0])) { 6127c478bd9Sstevel@tonic-gate fprintf(stderr, "%d: at maximum dictionary depth\n", 6137c478bd9Sstevel@tonic-gate yylineNum); 6147c478bd9Sstevel@tonic-gate return; 6157c478bd9Sstevel@tonic-gate } 6167c478bd9Sstevel@tonic-gate 6177c478bd9Sstevel@tonic-gate yysavewords[yysavedepth++] = yysettab(newdict); 6187c478bd9Sstevel@tonic-gate if (yydebug) 6197c478bd9Sstevel@tonic-gate printf("yysavedepth++ => %d\n", yysavedepth); 6207c478bd9Sstevel@tonic-gate } 6217c478bd9Sstevel@tonic-gate 6227c478bd9Sstevel@tonic-gate void yyresetdict() 6237c478bd9Sstevel@tonic-gate { 6247c478bd9Sstevel@tonic-gate if (yysavedepth > 0) { 6257c478bd9Sstevel@tonic-gate yysettab(yysavewords[--yysavedepth]); 6267c478bd9Sstevel@tonic-gate if (yydebug) 6277c478bd9Sstevel@tonic-gate printf("yysavedepth-- => %d\n", yysavedepth); 6287c478bd9Sstevel@tonic-gate } 6297c478bd9Sstevel@tonic-gate } 6307c478bd9Sstevel@tonic-gate 6317c478bd9Sstevel@tonic-gate 6327c478bd9Sstevel@tonic-gate 6337c478bd9Sstevel@tonic-gate #ifdef TEST_LEXER 6347c478bd9Sstevel@tonic-gate int main(argc, argv) 6357c478bd9Sstevel@tonic-gate int argc; 6367c478bd9Sstevel@tonic-gate char *argv[]; 6377c478bd9Sstevel@tonic-gate { 6387c478bd9Sstevel@tonic-gate int n; 6397c478bd9Sstevel@tonic-gate 6407c478bd9Sstevel@tonic-gate yyin = stdin; 6417c478bd9Sstevel@tonic-gate 6427c478bd9Sstevel@tonic-gate while ((n = yylex()) != 0) 6437c478bd9Sstevel@tonic-gate printf("%d.n = %d [%s] %d %d\n", 6447c478bd9Sstevel@tonic-gate yylineNum, n, yystr, yypos, yylast); 6457c478bd9Sstevel@tonic-gate } 6467c478bd9Sstevel@tonic-gate #endif 647