17c478bd9Sstevel@tonic-gate /* 2d6c23f6fSyx160601 * Copyright (C) 2002-2008 by Darren Reed. 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * See the IPFILTER.LICENCE file for details on licencing. 57c478bd9Sstevel@tonic-gate * 633f2fefdSDarren Reed * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 77c478bd9Sstevel@tonic-gate * Use is subject to license terms. 87c478bd9Sstevel@tonic-gate */ 97c478bd9Sstevel@tonic-gate 107c478bd9Sstevel@tonic-gate #include <ctype.h> 117c478bd9Sstevel@tonic-gate #include "ipf.h" 127c478bd9Sstevel@tonic-gate #ifdef IPFILTER_SCAN 137c478bd9Sstevel@tonic-gate # include "netinet/ip_scan.h" 147c478bd9Sstevel@tonic-gate #endif 157c478bd9Sstevel@tonic-gate #include <sys/ioctl.h> 167c478bd9Sstevel@tonic-gate #include <syslog.h> 177c478bd9Sstevel@tonic-gate #ifdef TEST_LEXER 187c478bd9Sstevel@tonic-gate # define NO_YACC 197c478bd9Sstevel@tonic-gate union { 207c478bd9Sstevel@tonic-gate int num; 217c478bd9Sstevel@tonic-gate char *str; 227c478bd9Sstevel@tonic-gate struct in_addr ipa; 237c478bd9Sstevel@tonic-gate i6addr_t ip6; 247c478bd9Sstevel@tonic-gate } yylval; 257c478bd9Sstevel@tonic-gate #endif 267c478bd9Sstevel@tonic-gate #include "lexer.h" 277c478bd9Sstevel@tonic-gate #include "y.tab.h" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate FILE *yyin; 307c478bd9Sstevel@tonic-gate 31ab25eeb5Syz155240 #define ishex(c) (ISDIGIT(c) || ((c) >= 'a' && (c) <= 'f') || \ 327c478bd9Sstevel@tonic-gate ((c) >= 'A' && (c) <= 'F')) 337c478bd9Sstevel@tonic-gate #define TOOLONG -3 347c478bd9Sstevel@tonic-gate 357c478bd9Sstevel@tonic-gate extern int string_start; 367c478bd9Sstevel@tonic-gate extern int string_end; 377c478bd9Sstevel@tonic-gate extern char *string_val; 387c478bd9Sstevel@tonic-gate extern int pos; 397c478bd9Sstevel@tonic-gate extern int yydebug; 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate char *yystr = NULL; 427c478bd9Sstevel@tonic-gate int yytext[YYBUFSIZ+1]; 4333f2fefdSDarren Reed char yychars[YYBUFSIZ+1]; 447c478bd9Sstevel@tonic-gate int yylineNum = 1; 457c478bd9Sstevel@tonic-gate int yypos = 0; 467c478bd9Sstevel@tonic-gate int yylast = -1; 477c478bd9Sstevel@tonic-gate int yyexpectaddr = 0; 487c478bd9Sstevel@tonic-gate int yybreakondot = 0; 497c478bd9Sstevel@tonic-gate int yyvarnext = 0; 507c478bd9Sstevel@tonic-gate int yytokentype = 0; 517c478bd9Sstevel@tonic-gate wordtab_t *yywordtab = NULL; 527c478bd9Sstevel@tonic-gate int yysavedepth = 0; 537c478bd9Sstevel@tonic-gate wordtab_t *yysavewords[30]; 547c478bd9Sstevel@tonic-gate 557c478bd9Sstevel@tonic-gate 567c478bd9Sstevel@tonic-gate static wordtab_t *yyfindkey __P((char *)); 5733f2fefdSDarren Reed static int yygetc __P((int)); 587c478bd9Sstevel@tonic-gate static void yyunputc __P((int)); 597c478bd9Sstevel@tonic-gate static int yyswallow __P((int)); 607c478bd9Sstevel@tonic-gate static char *yytexttostr __P((int, int)); 617c478bd9Sstevel@tonic-gate static void yystrtotext __P((char *)); 6233f2fefdSDarren Reed static char *yytexttochar __P((void)); 637c478bd9Sstevel@tonic-gate 6433f2fefdSDarren Reed static int yygetc(docont) 6533f2fefdSDarren Reed int docont; 667c478bd9Sstevel@tonic-gate { 677c478bd9Sstevel@tonic-gate int c; 687c478bd9Sstevel@tonic-gate 697c478bd9Sstevel@tonic-gate if (yypos < yylast) { 707c478bd9Sstevel@tonic-gate c = yytext[yypos++]; 71ab25eeb5Syz155240 if (c == '\n') 72ab25eeb5Syz155240 yylineNum++; 737c478bd9Sstevel@tonic-gate return c; 747c478bd9Sstevel@tonic-gate } 757c478bd9Sstevel@tonic-gate 767c478bd9Sstevel@tonic-gate if (yypos == YYBUFSIZ) 777c478bd9Sstevel@tonic-gate return TOOLONG; 787c478bd9Sstevel@tonic-gate 797c478bd9Sstevel@tonic-gate if (pos >= string_start && pos <= string_end) { 807c478bd9Sstevel@tonic-gate c = string_val[pos - string_start]; 817c478bd9Sstevel@tonic-gate yypos++; 827c478bd9Sstevel@tonic-gate } else { 837c478bd9Sstevel@tonic-gate c = fgetc(yyin); 8433f2fefdSDarren Reed if (docont && (c == '\\')) { 8533f2fefdSDarren Reed c = fgetc(yyin); 8633f2fefdSDarren Reed if (c == '\n') { 8733f2fefdSDarren Reed yylineNum++; 8833f2fefdSDarren Reed c = fgetc(yyin); 8933f2fefdSDarren Reed } 9033f2fefdSDarren Reed } 91ab25eeb5Syz155240 } 927c478bd9Sstevel@tonic-gate if (c == '\n') 937c478bd9Sstevel@tonic-gate yylineNum++; 947c478bd9Sstevel@tonic-gate yytext[yypos++] = c; 957c478bd9Sstevel@tonic-gate yylast = yypos; 967c478bd9Sstevel@tonic-gate yytext[yypos] = '\0'; 977c478bd9Sstevel@tonic-gate 987c478bd9Sstevel@tonic-gate return c; 997c478bd9Sstevel@tonic-gate } 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate 1027c478bd9Sstevel@tonic-gate static void yyunputc(c) 1037c478bd9Sstevel@tonic-gate int c; 1047c478bd9Sstevel@tonic-gate { 105ab25eeb5Syz155240 if (c == '\n') 106ab25eeb5Syz155240 yylineNum--; 1077c478bd9Sstevel@tonic-gate yytext[--yypos] = c; 1087c478bd9Sstevel@tonic-gate } 1097c478bd9Sstevel@tonic-gate 1107c478bd9Sstevel@tonic-gate 1117c478bd9Sstevel@tonic-gate static int yyswallow(last) 1127c478bd9Sstevel@tonic-gate int last; 1137c478bd9Sstevel@tonic-gate { 1147c478bd9Sstevel@tonic-gate int c; 1157c478bd9Sstevel@tonic-gate 11633f2fefdSDarren Reed while (((c = yygetc(0)) > '\0') && (c != last)) 1177c478bd9Sstevel@tonic-gate ; 1187c478bd9Sstevel@tonic-gate 1197c478bd9Sstevel@tonic-gate if (c != EOF) 1207c478bd9Sstevel@tonic-gate yyunputc(c); 1217c478bd9Sstevel@tonic-gate if (c == last) 1227c478bd9Sstevel@tonic-gate return 0; 1237c478bd9Sstevel@tonic-gate return -1; 1247c478bd9Sstevel@tonic-gate } 1257c478bd9Sstevel@tonic-gate 1267c478bd9Sstevel@tonic-gate 12733f2fefdSDarren Reed static char *yytexttochar() 12833f2fefdSDarren Reed { 12933f2fefdSDarren Reed int i; 13033f2fefdSDarren Reed 13133f2fefdSDarren Reed for (i = 0; i < yypos; i++) 13233f2fefdSDarren Reed yychars[i] = (char)(yytext[i] & 0xff); 13333f2fefdSDarren Reed yychars[i] = '\0'; 13433f2fefdSDarren Reed return yychars; 13533f2fefdSDarren Reed } 13633f2fefdSDarren Reed 13733f2fefdSDarren Reed 1387c478bd9Sstevel@tonic-gate static void yystrtotext(str) 1397c478bd9Sstevel@tonic-gate char *str; 1407c478bd9Sstevel@tonic-gate { 1417c478bd9Sstevel@tonic-gate int len; 1427c478bd9Sstevel@tonic-gate char *s; 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate len = strlen(str); 1457c478bd9Sstevel@tonic-gate if (len > YYBUFSIZ) 1467c478bd9Sstevel@tonic-gate len = YYBUFSIZ; 1477c478bd9Sstevel@tonic-gate 1487c478bd9Sstevel@tonic-gate for (s = str; *s != '\0' && len > 0; s++, len--) 1497c478bd9Sstevel@tonic-gate yytext[yylast++] = *s; 1507c478bd9Sstevel@tonic-gate yytext[yylast] = '\0'; 1517c478bd9Sstevel@tonic-gate } 1527c478bd9Sstevel@tonic-gate 1537c478bd9Sstevel@tonic-gate 1547c478bd9Sstevel@tonic-gate static char *yytexttostr(offset, max) 1557c478bd9Sstevel@tonic-gate int offset, max; 1567c478bd9Sstevel@tonic-gate { 1577c478bd9Sstevel@tonic-gate char *str; 1587c478bd9Sstevel@tonic-gate int i; 1597c478bd9Sstevel@tonic-gate 1607c478bd9Sstevel@tonic-gate if ((yytext[offset] == '\'' || yytext[offset] == '"') && 1617c478bd9Sstevel@tonic-gate (yytext[offset] == yytext[offset + max - 1])) { 1627c478bd9Sstevel@tonic-gate offset++; 1637c478bd9Sstevel@tonic-gate max--; 1647c478bd9Sstevel@tonic-gate } 1657c478bd9Sstevel@tonic-gate 1667c478bd9Sstevel@tonic-gate if (max > yylast) 1677c478bd9Sstevel@tonic-gate max = yylast; 1687c478bd9Sstevel@tonic-gate str = malloc(max + 1); 1697c478bd9Sstevel@tonic-gate if (str != NULL) { 1707c478bd9Sstevel@tonic-gate for (i = offset; i < max; i++) 1717c478bd9Sstevel@tonic-gate str[i - offset] = (char)(yytext[i] & 0xff); 1727c478bd9Sstevel@tonic-gate str[i - offset] = '\0'; 1737c478bd9Sstevel@tonic-gate } 1747c478bd9Sstevel@tonic-gate return str; 1757c478bd9Sstevel@tonic-gate } 1767c478bd9Sstevel@tonic-gate 1777c478bd9Sstevel@tonic-gate 1787c478bd9Sstevel@tonic-gate int yylex() 1797c478bd9Sstevel@tonic-gate { 1807c478bd9Sstevel@tonic-gate int c, n, isbuilding, rval, lnext, nokey = 0; 1817c478bd9Sstevel@tonic-gate char *name; 1827c478bd9Sstevel@tonic-gate 1837c478bd9Sstevel@tonic-gate isbuilding = 0; 1847c478bd9Sstevel@tonic-gate lnext = 0; 1857c478bd9Sstevel@tonic-gate rval = 0; 1867c478bd9Sstevel@tonic-gate 1877c478bd9Sstevel@tonic-gate if (yystr != NULL) { 1887c478bd9Sstevel@tonic-gate free(yystr); 1897c478bd9Sstevel@tonic-gate yystr = NULL; 1907c478bd9Sstevel@tonic-gate } 1917c478bd9Sstevel@tonic-gate 1927c478bd9Sstevel@tonic-gate nextchar: 19333f2fefdSDarren Reed c = yygetc(0); 19433f2fefdSDarren Reed if (yydebug > 1) 19533f2fefdSDarren Reed printf("yygetc = (%x) %c [%*.*s]\n", c, c, yypos, yypos, 19633f2fefdSDarren Reed yytexttochar()); 1977c478bd9Sstevel@tonic-gate 1987c478bd9Sstevel@tonic-gate switch (c) 1997c478bd9Sstevel@tonic-gate { 2007c478bd9Sstevel@tonic-gate case '\n' : 20133f2fefdSDarren Reed lnext = 0; 20233f2fefdSDarren Reed nokey = 0; 2037c478bd9Sstevel@tonic-gate case '\t' : 2047c478bd9Sstevel@tonic-gate case '\r' : 2057c478bd9Sstevel@tonic-gate case ' ' : 2067c478bd9Sstevel@tonic-gate if (isbuilding == 1) { 2077c478bd9Sstevel@tonic-gate yyunputc(c); 2087c478bd9Sstevel@tonic-gate goto done; 2097c478bd9Sstevel@tonic-gate } 2107c478bd9Sstevel@tonic-gate if (yylast > yypos) { 2117c478bd9Sstevel@tonic-gate bcopy(yytext + yypos, yytext, 2127c478bd9Sstevel@tonic-gate sizeof(yytext[0]) * (yylast - yypos + 1)); 2137c478bd9Sstevel@tonic-gate } 2147c478bd9Sstevel@tonic-gate yylast -= yypos; 2157c478bd9Sstevel@tonic-gate yypos = 0; 216ab25eeb5Syz155240 lnext = 0; 217ab25eeb5Syz155240 nokey = 0; 2187c478bd9Sstevel@tonic-gate goto nextchar; 2197c478bd9Sstevel@tonic-gate 2207c478bd9Sstevel@tonic-gate case '\\' : 2217c478bd9Sstevel@tonic-gate if (lnext == 0) { 2227c478bd9Sstevel@tonic-gate lnext = 1; 2237c478bd9Sstevel@tonic-gate if (yylast == yypos) { 2247c478bd9Sstevel@tonic-gate yylast--; 2257c478bd9Sstevel@tonic-gate yypos--; 2267c478bd9Sstevel@tonic-gate } else 2277c478bd9Sstevel@tonic-gate yypos--; 2287c478bd9Sstevel@tonic-gate if (yypos == 0) 2297c478bd9Sstevel@tonic-gate nokey = 1; 2307c478bd9Sstevel@tonic-gate goto nextchar; 2317c478bd9Sstevel@tonic-gate } 2327c478bd9Sstevel@tonic-gate break; 2337c478bd9Sstevel@tonic-gate } 2347c478bd9Sstevel@tonic-gate 2357c478bd9Sstevel@tonic-gate if (lnext == 1) { 2367c478bd9Sstevel@tonic-gate lnext = 0; 237ab25eeb5Syz155240 if ((isbuilding == 0) && !ISALNUM(c)) { 238ab25eeb5Syz155240 return c; 239ab25eeb5Syz155240 } 2407c478bd9Sstevel@tonic-gate goto nextchar; 2417c478bd9Sstevel@tonic-gate } 2427c478bd9Sstevel@tonic-gate 2437c478bd9Sstevel@tonic-gate switch (c) 2447c478bd9Sstevel@tonic-gate { 2457c478bd9Sstevel@tonic-gate case '#' : 2467c478bd9Sstevel@tonic-gate if (isbuilding == 1) { 2477c478bd9Sstevel@tonic-gate yyunputc(c); 2487c478bd9Sstevel@tonic-gate goto done; 2497c478bd9Sstevel@tonic-gate } 2507c478bd9Sstevel@tonic-gate yyswallow('\n'); 2517c478bd9Sstevel@tonic-gate rval = YY_COMMENT; 252*22929378SDarren Reed goto done; 2537c478bd9Sstevel@tonic-gate 2547c478bd9Sstevel@tonic-gate case '$' : 2557c478bd9Sstevel@tonic-gate if (isbuilding == 1) { 2567c478bd9Sstevel@tonic-gate yyunputc(c); 2577c478bd9Sstevel@tonic-gate goto done; 2587c478bd9Sstevel@tonic-gate } 25933f2fefdSDarren Reed n = yygetc(0); 2607c478bd9Sstevel@tonic-gate if (n == '{') { 2617c478bd9Sstevel@tonic-gate if (yyswallow('}') == -1) { 2627c478bd9Sstevel@tonic-gate rval = -2; 2637c478bd9Sstevel@tonic-gate goto done; 2647c478bd9Sstevel@tonic-gate } 26533f2fefdSDarren Reed (void) yygetc(0); 2667c478bd9Sstevel@tonic-gate } else { 267ab25eeb5Syz155240 if (!ISALPHA(n)) { 2687c478bd9Sstevel@tonic-gate yyunputc(n); 2697c478bd9Sstevel@tonic-gate break; 2707c478bd9Sstevel@tonic-gate } 2717c478bd9Sstevel@tonic-gate do { 27233f2fefdSDarren Reed n = yygetc(1); 273ab25eeb5Syz155240 } while (ISALPHA(n) || ISDIGIT(n) || n == '_'); 2747c478bd9Sstevel@tonic-gate yyunputc(n); 2757c478bd9Sstevel@tonic-gate } 2767c478bd9Sstevel@tonic-gate 2777c478bd9Sstevel@tonic-gate name = yytexttostr(1, yypos); /* skip $ */ 2787c478bd9Sstevel@tonic-gate 2797c478bd9Sstevel@tonic-gate if (name != NULL) { 2807c478bd9Sstevel@tonic-gate string_val = get_variable(name, NULL, yylineNum); 2817c478bd9Sstevel@tonic-gate free(name); 2827c478bd9Sstevel@tonic-gate if (string_val != NULL) { 2837c478bd9Sstevel@tonic-gate name = yytexttostr(yypos, yylast); 2847c478bd9Sstevel@tonic-gate if (name != NULL) { 2857c478bd9Sstevel@tonic-gate yypos = 0; 2867c478bd9Sstevel@tonic-gate yylast = 0; 2877c478bd9Sstevel@tonic-gate yystrtotext(string_val); 2887c478bd9Sstevel@tonic-gate yystrtotext(name); 2897c478bd9Sstevel@tonic-gate free(string_val); 2907c478bd9Sstevel@tonic-gate free(name); 2917c478bd9Sstevel@tonic-gate goto nextchar; 2927c478bd9Sstevel@tonic-gate } 2937c478bd9Sstevel@tonic-gate free(string_val); 2947c478bd9Sstevel@tonic-gate } 2957c478bd9Sstevel@tonic-gate } 2967c478bd9Sstevel@tonic-gate break; 2977c478bd9Sstevel@tonic-gate 2987c478bd9Sstevel@tonic-gate case '\'': 2997c478bd9Sstevel@tonic-gate case '"' : 3007c478bd9Sstevel@tonic-gate if (isbuilding == 1) { 3017c478bd9Sstevel@tonic-gate goto done; 3027c478bd9Sstevel@tonic-gate } 3037c478bd9Sstevel@tonic-gate do { 30433f2fefdSDarren Reed n = yygetc(1); 3057c478bd9Sstevel@tonic-gate if (n == EOF || n == TOOLONG) { 3067c478bd9Sstevel@tonic-gate rval = -2; 3077c478bd9Sstevel@tonic-gate goto done; 3087c478bd9Sstevel@tonic-gate } 3097c478bd9Sstevel@tonic-gate if (n == '\n') { 3107c478bd9Sstevel@tonic-gate yyunputc(' '); 3117c478bd9Sstevel@tonic-gate yypos++; 3127c478bd9Sstevel@tonic-gate } 3137c478bd9Sstevel@tonic-gate } while (n != c); 31433f2fefdSDarren Reed rval = YY_STR; 31533f2fefdSDarren Reed goto done; 31633f2fefdSDarren Reed /* NOTREACHED */ 3177c478bd9Sstevel@tonic-gate 3187c478bd9Sstevel@tonic-gate case EOF : 3197c478bd9Sstevel@tonic-gate yylineNum = 1; 3207c478bd9Sstevel@tonic-gate yypos = 0; 3217c478bd9Sstevel@tonic-gate yylast = -1; 3227c478bd9Sstevel@tonic-gate yyexpectaddr = 0; 3237c478bd9Sstevel@tonic-gate yybreakondot = 0; 3247c478bd9Sstevel@tonic-gate yyvarnext = 0; 3257c478bd9Sstevel@tonic-gate yytokentype = 0; 3267c478bd9Sstevel@tonic-gate return 0; 3277c478bd9Sstevel@tonic-gate } 3287c478bd9Sstevel@tonic-gate 3297c478bd9Sstevel@tonic-gate if (strchr("=,/;{}()@", c) != NULL) { 3307c478bd9Sstevel@tonic-gate if (isbuilding == 1) { 3317c478bd9Sstevel@tonic-gate yyunputc(c); 3327c478bd9Sstevel@tonic-gate goto done; 3337c478bd9Sstevel@tonic-gate } 3347c478bd9Sstevel@tonic-gate rval = c; 3357c478bd9Sstevel@tonic-gate goto done; 3367c478bd9Sstevel@tonic-gate } else if (c == '.') { 3377c478bd9Sstevel@tonic-gate if (isbuilding == 0) { 3387c478bd9Sstevel@tonic-gate rval = c; 3397c478bd9Sstevel@tonic-gate goto done; 3407c478bd9Sstevel@tonic-gate } 3417c478bd9Sstevel@tonic-gate if (yybreakondot != 0) { 3427c478bd9Sstevel@tonic-gate yyunputc(c); 3437c478bd9Sstevel@tonic-gate goto done; 3447c478bd9Sstevel@tonic-gate } 3457c478bd9Sstevel@tonic-gate } 3467c478bd9Sstevel@tonic-gate 3477c478bd9Sstevel@tonic-gate switch (c) 3487c478bd9Sstevel@tonic-gate { 3497c478bd9Sstevel@tonic-gate case '-' : 3507c478bd9Sstevel@tonic-gate if (yyexpectaddr) 3517c478bd9Sstevel@tonic-gate break; 3527c478bd9Sstevel@tonic-gate if (isbuilding == 1) 3537c478bd9Sstevel@tonic-gate break; 35433f2fefdSDarren Reed n = yygetc(0); 3557c478bd9Sstevel@tonic-gate if (n == '>') { 3567c478bd9Sstevel@tonic-gate isbuilding = 1; 3577c478bd9Sstevel@tonic-gate goto done; 3587c478bd9Sstevel@tonic-gate } 3597c478bd9Sstevel@tonic-gate yyunputc(n); 3607c478bd9Sstevel@tonic-gate rval = '-'; 3617c478bd9Sstevel@tonic-gate goto done; 3627c478bd9Sstevel@tonic-gate 3637c478bd9Sstevel@tonic-gate case '!' : 3647c478bd9Sstevel@tonic-gate if (isbuilding == 1) { 3657c478bd9Sstevel@tonic-gate yyunputc(c); 3667c478bd9Sstevel@tonic-gate goto done; 3677c478bd9Sstevel@tonic-gate } 36833f2fefdSDarren Reed n = yygetc(0); 3697c478bd9Sstevel@tonic-gate if (n == '=') { 3707c478bd9Sstevel@tonic-gate rval = YY_CMP_NE; 3717c478bd9Sstevel@tonic-gate goto done; 3727c478bd9Sstevel@tonic-gate } 3737c478bd9Sstevel@tonic-gate yyunputc(n); 3747c478bd9Sstevel@tonic-gate rval = '!'; 3757c478bd9Sstevel@tonic-gate goto done; 3767c478bd9Sstevel@tonic-gate 3777c478bd9Sstevel@tonic-gate case '<' : 3787c478bd9Sstevel@tonic-gate if (yyexpectaddr) 3797c478bd9Sstevel@tonic-gate break; 3807c478bd9Sstevel@tonic-gate if (isbuilding == 1) { 3817c478bd9Sstevel@tonic-gate yyunputc(c); 3827c478bd9Sstevel@tonic-gate goto done; 3837c478bd9Sstevel@tonic-gate } 38433f2fefdSDarren Reed n = yygetc(0); 3857c478bd9Sstevel@tonic-gate if (n == '=') { 3867c478bd9Sstevel@tonic-gate rval = YY_CMP_LE; 3877c478bd9Sstevel@tonic-gate goto done; 3887c478bd9Sstevel@tonic-gate } 3897c478bd9Sstevel@tonic-gate if (n == '>') { 3907c478bd9Sstevel@tonic-gate rval = YY_RANGE_OUT; 3917c478bd9Sstevel@tonic-gate goto done; 3927c478bd9Sstevel@tonic-gate } 3937c478bd9Sstevel@tonic-gate yyunputc(n); 3947c478bd9Sstevel@tonic-gate rval = YY_CMP_LT; 3957c478bd9Sstevel@tonic-gate goto done; 3967c478bd9Sstevel@tonic-gate 3977c478bd9Sstevel@tonic-gate case '>' : 3987c478bd9Sstevel@tonic-gate if (yyexpectaddr) 3997c478bd9Sstevel@tonic-gate break; 4007c478bd9Sstevel@tonic-gate if (isbuilding == 1) { 4017c478bd9Sstevel@tonic-gate yyunputc(c); 4027c478bd9Sstevel@tonic-gate goto done; 4037c478bd9Sstevel@tonic-gate } 40433f2fefdSDarren Reed n = yygetc(0); 4057c478bd9Sstevel@tonic-gate if (n == '=') { 4067c478bd9Sstevel@tonic-gate rval = YY_CMP_GE; 4077c478bd9Sstevel@tonic-gate goto done; 4087c478bd9Sstevel@tonic-gate } 4097c478bd9Sstevel@tonic-gate if (n == '<') { 4107c478bd9Sstevel@tonic-gate rval = YY_RANGE_IN; 4117c478bd9Sstevel@tonic-gate goto done; 4127c478bd9Sstevel@tonic-gate } 4137c478bd9Sstevel@tonic-gate yyunputc(n); 4147c478bd9Sstevel@tonic-gate rval = YY_CMP_GT; 4157c478bd9Sstevel@tonic-gate goto done; 4167c478bd9Sstevel@tonic-gate } 4177c478bd9Sstevel@tonic-gate 4187c478bd9Sstevel@tonic-gate /* 4197c478bd9Sstevel@tonic-gate * Now for the reason this is here...IPv6 address parsing. 4207c478bd9Sstevel@tonic-gate * The longest string we can expect is of this form: 4217c478bd9Sstevel@tonic-gate * 0000:0000:0000:0000:0000:0000:000.000.000.000 4227c478bd9Sstevel@tonic-gate * not: 4237c478bd9Sstevel@tonic-gate * 0000:0000:0000:0000:0000:0000:0000:0000 4247c478bd9Sstevel@tonic-gate */ 4257c478bd9Sstevel@tonic-gate #ifdef USE_INET6 426d6c23f6fSyx160601 if (isbuilding == 0 && (ishex(c) || c == ':')) { 4277c478bd9Sstevel@tonic-gate char ipv6buf[45 + 1], *s, oc; 4287c478bd9Sstevel@tonic-gate int start; 4297c478bd9Sstevel@tonic-gate 4307c478bd9Sstevel@tonic-gate start = yypos; 4317c478bd9Sstevel@tonic-gate s = ipv6buf; 4327c478bd9Sstevel@tonic-gate oc = c; 4337c478bd9Sstevel@tonic-gate 4347c478bd9Sstevel@tonic-gate /* 4357c478bd9Sstevel@tonic-gate * Perhaps we should implement stricter controls on what we 4367c478bd9Sstevel@tonic-gate * swallow up here, but surely it would just be duplicating 4377c478bd9Sstevel@tonic-gate * the code in inet_pton() anyway. 4387c478bd9Sstevel@tonic-gate */ 4397c478bd9Sstevel@tonic-gate do { 4407c478bd9Sstevel@tonic-gate *s++ = c; 44133f2fefdSDarren Reed c = yygetc(1); 4427c478bd9Sstevel@tonic-gate } while ((ishex(c) || c == ':' || c == '.') && 4437c478bd9Sstevel@tonic-gate (s - ipv6buf < 46)); 4447c478bd9Sstevel@tonic-gate yyunputc(c); 4457c478bd9Sstevel@tonic-gate *s = '\0'; 4467c478bd9Sstevel@tonic-gate 4477c478bd9Sstevel@tonic-gate if (inet_pton(AF_INET6, ipv6buf, &yylval.ip6) == 1) { 4487c478bd9Sstevel@tonic-gate rval = YY_IPV6; 4497c478bd9Sstevel@tonic-gate yyexpectaddr = 0; 4507c478bd9Sstevel@tonic-gate goto done; 4517c478bd9Sstevel@tonic-gate } 4527c478bd9Sstevel@tonic-gate yypos = start; 4537c478bd9Sstevel@tonic-gate c = oc; 4547c478bd9Sstevel@tonic-gate } 4557c478bd9Sstevel@tonic-gate #endif 4567c478bd9Sstevel@tonic-gate 4577c478bd9Sstevel@tonic-gate if (c == ':') { 4587c478bd9Sstevel@tonic-gate if (isbuilding == 1) { 4597c478bd9Sstevel@tonic-gate yyunputc(c); 4607c478bd9Sstevel@tonic-gate goto done; 4617c478bd9Sstevel@tonic-gate } 4627c478bd9Sstevel@tonic-gate rval = ':'; 4637c478bd9Sstevel@tonic-gate goto done; 4647c478bd9Sstevel@tonic-gate } 4657c478bd9Sstevel@tonic-gate 4667c478bd9Sstevel@tonic-gate if (isbuilding == 0 && c == '0') { 46733f2fefdSDarren Reed n = yygetc(0); 4687c478bd9Sstevel@tonic-gate if (n == 'x') { 4697c478bd9Sstevel@tonic-gate do { 47033f2fefdSDarren Reed n = yygetc(1); 4717c478bd9Sstevel@tonic-gate } while (ishex(n)); 4727c478bd9Sstevel@tonic-gate yyunputc(n); 4737c478bd9Sstevel@tonic-gate rval = YY_HEX; 4747c478bd9Sstevel@tonic-gate goto done; 4757c478bd9Sstevel@tonic-gate } 4767c478bd9Sstevel@tonic-gate yyunputc(n); 4777c478bd9Sstevel@tonic-gate } 4787c478bd9Sstevel@tonic-gate 4797c478bd9Sstevel@tonic-gate /* 4807c478bd9Sstevel@tonic-gate * No negative numbers with leading - sign.. 4817c478bd9Sstevel@tonic-gate */ 482ab25eeb5Syz155240 if (isbuilding == 0 && ISDIGIT(c)) { 4837c478bd9Sstevel@tonic-gate do { 48433f2fefdSDarren Reed n = yygetc(1); 485ab25eeb5Syz155240 } while (ISDIGIT(n)); 4867c478bd9Sstevel@tonic-gate yyunputc(n); 4877c478bd9Sstevel@tonic-gate rval = YY_NUMBER; 4887c478bd9Sstevel@tonic-gate goto done; 4897c478bd9Sstevel@tonic-gate } 4907c478bd9Sstevel@tonic-gate 4917c478bd9Sstevel@tonic-gate isbuilding = 1; 4927c478bd9Sstevel@tonic-gate goto nextchar; 4937c478bd9Sstevel@tonic-gate 4947c478bd9Sstevel@tonic-gate done: 4957c478bd9Sstevel@tonic-gate yystr = yytexttostr(0, yypos); 4967c478bd9Sstevel@tonic-gate 49733f2fefdSDarren Reed if (yydebug) 49833f2fefdSDarren Reed printf("isbuilding %d yyvarnext %d nokey %d\n", 49933f2fefdSDarren Reed isbuilding, yyvarnext, nokey); 5007c478bd9Sstevel@tonic-gate if (isbuilding == 1) { 5017c478bd9Sstevel@tonic-gate wordtab_t *w; 5027c478bd9Sstevel@tonic-gate 5037c478bd9Sstevel@tonic-gate w = NULL; 5047c478bd9Sstevel@tonic-gate isbuilding = 0; 5057c478bd9Sstevel@tonic-gate 5067c478bd9Sstevel@tonic-gate if ((yyvarnext == 0) && (nokey == 0)) { 5077c478bd9Sstevel@tonic-gate w = yyfindkey(yystr); 5087c478bd9Sstevel@tonic-gate if (w == NULL && yywordtab != NULL) { 5097c478bd9Sstevel@tonic-gate yyresetdict(); 5107c478bd9Sstevel@tonic-gate w = yyfindkey(yystr); 5117c478bd9Sstevel@tonic-gate } 5127c478bd9Sstevel@tonic-gate } else 5137c478bd9Sstevel@tonic-gate yyvarnext = 0; 5147c478bd9Sstevel@tonic-gate if (w != NULL) 5157c478bd9Sstevel@tonic-gate rval = w->w_value; 5167c478bd9Sstevel@tonic-gate else 5177c478bd9Sstevel@tonic-gate rval = YY_STR; 5187c478bd9Sstevel@tonic-gate } 5197c478bd9Sstevel@tonic-gate 5207c478bd9Sstevel@tonic-gate if (rval == YY_STR && yysavedepth > 0) 5217c478bd9Sstevel@tonic-gate yyresetdict(); 5227c478bd9Sstevel@tonic-gate 5237c478bd9Sstevel@tonic-gate yytokentype = rval; 5247c478bd9Sstevel@tonic-gate 5257c478bd9Sstevel@tonic-gate if (yydebug) 52633f2fefdSDarren Reed printf("lexed(%s) [%d,%d,%d] => %d @%d\n", yystr, string_start, 52733f2fefdSDarren Reed string_end, pos, rval, yysavedepth); 5287c478bd9Sstevel@tonic-gate 5297c478bd9Sstevel@tonic-gate switch (rval) 5307c478bd9Sstevel@tonic-gate { 5317c478bd9Sstevel@tonic-gate case YY_NUMBER : 532ab25eeb5Syz155240 sscanf(yystr, "%u", &yylval.num); 5337c478bd9Sstevel@tonic-gate break; 5347c478bd9Sstevel@tonic-gate 5357c478bd9Sstevel@tonic-gate case YY_HEX : 5367c478bd9Sstevel@tonic-gate sscanf(yystr, "0x%x", (u_int *)&yylval.num); 5377c478bd9Sstevel@tonic-gate break; 5387c478bd9Sstevel@tonic-gate 5397c478bd9Sstevel@tonic-gate case YY_STR : 5407c478bd9Sstevel@tonic-gate yylval.str = strdup(yystr); 5417c478bd9Sstevel@tonic-gate break; 5427c478bd9Sstevel@tonic-gate 5437c478bd9Sstevel@tonic-gate default : 5447c478bd9Sstevel@tonic-gate break; 5457c478bd9Sstevel@tonic-gate } 5467c478bd9Sstevel@tonic-gate 5477c478bd9Sstevel@tonic-gate if (yylast > 0) { 5487c478bd9Sstevel@tonic-gate bcopy(yytext + yypos, yytext, 5497c478bd9Sstevel@tonic-gate sizeof(yytext[0]) * (yylast - yypos + 1)); 5507c478bd9Sstevel@tonic-gate yylast -= yypos; 5517c478bd9Sstevel@tonic-gate yypos = 0; 5527c478bd9Sstevel@tonic-gate } 5537c478bd9Sstevel@tonic-gate 5547c478bd9Sstevel@tonic-gate return rval; 5557c478bd9Sstevel@tonic-gate } 5567c478bd9Sstevel@tonic-gate 5577c478bd9Sstevel@tonic-gate 5587c478bd9Sstevel@tonic-gate static wordtab_t *yyfindkey(key) 5597c478bd9Sstevel@tonic-gate char *key; 5607c478bd9Sstevel@tonic-gate { 5617c478bd9Sstevel@tonic-gate wordtab_t *w; 5627c478bd9Sstevel@tonic-gate 5637c478bd9Sstevel@tonic-gate if (yywordtab == NULL) 5647c478bd9Sstevel@tonic-gate return NULL; 5657c478bd9Sstevel@tonic-gate 5667c478bd9Sstevel@tonic-gate for (w = yywordtab; w->w_word != 0; w++) 5677c478bd9Sstevel@tonic-gate if (strcasecmp(key, w->w_word) == 0) 5687c478bd9Sstevel@tonic-gate return w; 5697c478bd9Sstevel@tonic-gate return NULL; 5707c478bd9Sstevel@tonic-gate } 5717c478bd9Sstevel@tonic-gate 5727c478bd9Sstevel@tonic-gate 5737c478bd9Sstevel@tonic-gate char *yykeytostr(num) 5747c478bd9Sstevel@tonic-gate int num; 5757c478bd9Sstevel@tonic-gate { 5767c478bd9Sstevel@tonic-gate wordtab_t *w; 5777c478bd9Sstevel@tonic-gate 5787c478bd9Sstevel@tonic-gate if (yywordtab == NULL) 5797c478bd9Sstevel@tonic-gate return "<unknown>"; 5807c478bd9Sstevel@tonic-gate 5817c478bd9Sstevel@tonic-gate for (w = yywordtab; w->w_word; w++) 5827c478bd9Sstevel@tonic-gate if (w->w_value == num) 5837c478bd9Sstevel@tonic-gate return w->w_word; 5847c478bd9Sstevel@tonic-gate return "<unknown>"; 5857c478bd9Sstevel@tonic-gate } 5867c478bd9Sstevel@tonic-gate 5877c478bd9Sstevel@tonic-gate 5887c478bd9Sstevel@tonic-gate wordtab_t *yysettab(words) 5897c478bd9Sstevel@tonic-gate wordtab_t *words; 5907c478bd9Sstevel@tonic-gate { 5917c478bd9Sstevel@tonic-gate wordtab_t *save; 5927c478bd9Sstevel@tonic-gate 5937c478bd9Sstevel@tonic-gate save = yywordtab; 5947c478bd9Sstevel@tonic-gate yywordtab = words; 5957c478bd9Sstevel@tonic-gate return save; 5967c478bd9Sstevel@tonic-gate } 5977c478bd9Sstevel@tonic-gate 5987c478bd9Sstevel@tonic-gate 5997c478bd9Sstevel@tonic-gate void yyerror(msg) 6007c478bd9Sstevel@tonic-gate char *msg; 6017c478bd9Sstevel@tonic-gate { 6027c478bd9Sstevel@tonic-gate char *txt, letter[2]; 6037c478bd9Sstevel@tonic-gate int freetxt = 0; 6047c478bd9Sstevel@tonic-gate 6057c478bd9Sstevel@tonic-gate if (yytokentype < 256) { 6067c478bd9Sstevel@tonic-gate letter[0] = yytokentype; 6077c478bd9Sstevel@tonic-gate letter[1] = '\0'; 6087c478bd9Sstevel@tonic-gate txt = letter; 6097c478bd9Sstevel@tonic-gate } else if (yytokentype == YY_STR || yytokentype == YY_HEX || 6107c478bd9Sstevel@tonic-gate yytokentype == YY_NUMBER) { 6117c478bd9Sstevel@tonic-gate if (yystr == NULL) { 6127c478bd9Sstevel@tonic-gate txt = yytexttostr(yypos, YYBUFSIZ); 6135e985db5Sschuster if (txt == NULL) { 6145e985db5Sschuster fprintf(stderr, "sorry, out of memory," 6155e985db5Sschuster " bailing out\n"); 6165e985db5Sschuster exit(1); 6175e985db5Sschuster } 6187c478bd9Sstevel@tonic-gate freetxt = 1; 6197c478bd9Sstevel@tonic-gate } else 6207c478bd9Sstevel@tonic-gate txt = yystr; 6217c478bd9Sstevel@tonic-gate } else { 6227c478bd9Sstevel@tonic-gate txt = yykeytostr(yytokentype); 6235e985db5Sschuster if (txt == NULL) { 6245e985db5Sschuster fprintf(stderr, "sorry, out of memory," 6255e985db5Sschuster " bailing out\n"); 6265e985db5Sschuster exit(1); 6275e985db5Sschuster } 6287c478bd9Sstevel@tonic-gate } 6297c478bd9Sstevel@tonic-gate fprintf(stderr, "%s error at \"%s\", line %d\n", msg, txt, yylineNum); 6307c478bd9Sstevel@tonic-gate if (freetxt == 1) 6317c478bd9Sstevel@tonic-gate free(txt); 6327c478bd9Sstevel@tonic-gate exit(1); 6337c478bd9Sstevel@tonic-gate } 6347c478bd9Sstevel@tonic-gate 6357c478bd9Sstevel@tonic-gate 6367c478bd9Sstevel@tonic-gate void yysetdict(newdict) 6377c478bd9Sstevel@tonic-gate wordtab_t *newdict; 6387c478bd9Sstevel@tonic-gate { 6397c478bd9Sstevel@tonic-gate if (yysavedepth == sizeof(yysavewords)/sizeof(yysavewords[0])) { 6407c478bd9Sstevel@tonic-gate fprintf(stderr, "%d: at maximum dictionary depth\n", 6417c478bd9Sstevel@tonic-gate yylineNum); 6427c478bd9Sstevel@tonic-gate return; 6437c478bd9Sstevel@tonic-gate } 6447c478bd9Sstevel@tonic-gate 6457c478bd9Sstevel@tonic-gate yysavewords[yysavedepth++] = yysettab(newdict); 6467c478bd9Sstevel@tonic-gate if (yydebug) 6477c478bd9Sstevel@tonic-gate printf("yysavedepth++ => %d\n", yysavedepth); 6487c478bd9Sstevel@tonic-gate } 6497c478bd9Sstevel@tonic-gate 6507c478bd9Sstevel@tonic-gate void yyresetdict() 6517c478bd9Sstevel@tonic-gate { 65233f2fefdSDarren Reed if (yydebug) 65333f2fefdSDarren Reed printf("yyresetdict(%d)\n", yysavedepth); 6547c478bd9Sstevel@tonic-gate if (yysavedepth > 0) { 6557c478bd9Sstevel@tonic-gate yysettab(yysavewords[--yysavedepth]); 6567c478bd9Sstevel@tonic-gate if (yydebug) 6577c478bd9Sstevel@tonic-gate printf("yysavedepth-- => %d\n", yysavedepth); 6587c478bd9Sstevel@tonic-gate } 6597c478bd9Sstevel@tonic-gate } 6607c478bd9Sstevel@tonic-gate 6617c478bd9Sstevel@tonic-gate 6627c478bd9Sstevel@tonic-gate 6637c478bd9Sstevel@tonic-gate #ifdef TEST_LEXER 6647c478bd9Sstevel@tonic-gate int main(argc, argv) 6657c478bd9Sstevel@tonic-gate int argc; 6667c478bd9Sstevel@tonic-gate char *argv[]; 6677c478bd9Sstevel@tonic-gate { 6687c478bd9Sstevel@tonic-gate int n; 6697c478bd9Sstevel@tonic-gate 6707c478bd9Sstevel@tonic-gate yyin = stdin; 6717c478bd9Sstevel@tonic-gate 6727c478bd9Sstevel@tonic-gate while ((n = yylex()) != 0) 6737c478bd9Sstevel@tonic-gate printf("%d.n = %d [%s] %d %d\n", 6747c478bd9Sstevel@tonic-gate yylineNum, n, yystr, yypos, yylast); 6757c478bd9Sstevel@tonic-gate } 6767c478bd9Sstevel@tonic-gate #endif 677