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
yygetc(int docont)64*1a90c98dSToomas Soome static int yygetc(int docont)
657c478bd9Sstevel@tonic-gate {
667c478bd9Sstevel@tonic-gate int c;
677c478bd9Sstevel@tonic-gate
687c478bd9Sstevel@tonic-gate if (yypos < yylast) {
697c478bd9Sstevel@tonic-gate c = yytext[yypos++];
70ab25eeb5Syz155240 if (c == '\n')
71ab25eeb5Syz155240 yylineNum++;
72*1a90c98dSToomas Soome return (c);
737c478bd9Sstevel@tonic-gate }
747c478bd9Sstevel@tonic-gate
757c478bd9Sstevel@tonic-gate if (yypos == YYBUFSIZ)
76*1a90c98dSToomas Soome 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);
8333f2fefdSDarren Reed if (docont && (c == '\\')) {
8433f2fefdSDarren Reed c = fgetc(yyin);
8533f2fefdSDarren Reed if (c == '\n') {
8633f2fefdSDarren Reed yylineNum++;
8733f2fefdSDarren Reed c = fgetc(yyin);
8833f2fefdSDarren Reed }
8933f2fefdSDarren Reed }
90ab25eeb5Syz155240 }
917c478bd9Sstevel@tonic-gate if (c == '\n')
927c478bd9Sstevel@tonic-gate yylineNum++;
937c478bd9Sstevel@tonic-gate yytext[yypos++] = c;
947c478bd9Sstevel@tonic-gate yylast = yypos;
957c478bd9Sstevel@tonic-gate yytext[yypos] = '\0';
967c478bd9Sstevel@tonic-gate
97*1a90c98dSToomas Soome return (c);
987c478bd9Sstevel@tonic-gate }
997c478bd9Sstevel@tonic-gate
1007c478bd9Sstevel@tonic-gate
yyunputc(int c)101*1a90c98dSToomas Soome static void yyunputc(int c)
1027c478bd9Sstevel@tonic-gate {
103ab25eeb5Syz155240 if (c == '\n')
104ab25eeb5Syz155240 yylineNum--;
1057c478bd9Sstevel@tonic-gate yytext[--yypos] = c;
1067c478bd9Sstevel@tonic-gate }
1077c478bd9Sstevel@tonic-gate
1087c478bd9Sstevel@tonic-gate
yyswallow(int last)109*1a90c98dSToomas Soome static int yyswallow(int last)
1107c478bd9Sstevel@tonic-gate {
1117c478bd9Sstevel@tonic-gate int c;
1127c478bd9Sstevel@tonic-gate
11333f2fefdSDarren Reed while (((c = yygetc(0)) > '\0') && (c != last))
1147c478bd9Sstevel@tonic-gate ;
1157c478bd9Sstevel@tonic-gate
1167c478bd9Sstevel@tonic-gate if (c != EOF)
1177c478bd9Sstevel@tonic-gate yyunputc(c);
1187c478bd9Sstevel@tonic-gate if (c == last)
119*1a90c98dSToomas Soome return (0);
120*1a90c98dSToomas Soome return (-1);
1217c478bd9Sstevel@tonic-gate }
1227c478bd9Sstevel@tonic-gate
1237c478bd9Sstevel@tonic-gate
yytexttochar(void)124*1a90c98dSToomas Soome static char *yytexttochar(void)
12533f2fefdSDarren Reed {
12633f2fefdSDarren Reed int i;
12733f2fefdSDarren Reed
12833f2fefdSDarren Reed for (i = 0; i < yypos; i++)
12933f2fefdSDarren Reed yychars[i] = (char)(yytext[i] & 0xff);
13033f2fefdSDarren Reed yychars[i] = '\0';
131*1a90c98dSToomas Soome return (yychars);
13233f2fefdSDarren Reed }
13333f2fefdSDarren Reed
13433f2fefdSDarren Reed
yystrtotext(char * str)135*1a90c98dSToomas Soome static void yystrtotext(char *str)
1367c478bd9Sstevel@tonic-gate {
1377c478bd9Sstevel@tonic-gate int len;
1387c478bd9Sstevel@tonic-gate char *s;
1397c478bd9Sstevel@tonic-gate
1407c478bd9Sstevel@tonic-gate len = strlen(str);
1417c478bd9Sstevel@tonic-gate if (len > YYBUFSIZ)
1427c478bd9Sstevel@tonic-gate len = YYBUFSIZ;
1437c478bd9Sstevel@tonic-gate
1447c478bd9Sstevel@tonic-gate for (s = str; *s != '\0' && len > 0; s++, len--)
1457c478bd9Sstevel@tonic-gate yytext[yylast++] = *s;
1467c478bd9Sstevel@tonic-gate yytext[yylast] = '\0';
1477c478bd9Sstevel@tonic-gate }
1487c478bd9Sstevel@tonic-gate
1497c478bd9Sstevel@tonic-gate
yytexttostr(int offset,int max)150*1a90c98dSToomas Soome static char *yytexttostr(int offset, int max)
1517c478bd9Sstevel@tonic-gate {
1527c478bd9Sstevel@tonic-gate char *str;
1537c478bd9Sstevel@tonic-gate int i;
1547c478bd9Sstevel@tonic-gate
1557c478bd9Sstevel@tonic-gate if ((yytext[offset] == '\'' || yytext[offset] == '"') &&
1567c478bd9Sstevel@tonic-gate (yytext[offset] == yytext[offset + max - 1])) {
1577c478bd9Sstevel@tonic-gate offset++;
1587c478bd9Sstevel@tonic-gate max--;
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate
1617c478bd9Sstevel@tonic-gate if (max > yylast)
1627c478bd9Sstevel@tonic-gate max = yylast;
1637c478bd9Sstevel@tonic-gate str = malloc(max + 1);
1647c478bd9Sstevel@tonic-gate if (str != NULL) {
1657c478bd9Sstevel@tonic-gate for (i = offset; i < max; i++)
1667c478bd9Sstevel@tonic-gate str[i - offset] = (char)(yytext[i] & 0xff);
1677c478bd9Sstevel@tonic-gate str[i - offset] = '\0';
1687c478bd9Sstevel@tonic-gate }
169*1a90c98dSToomas Soome return (str);
1707c478bd9Sstevel@tonic-gate }
1717c478bd9Sstevel@tonic-gate
1727c478bd9Sstevel@tonic-gate
173*1a90c98dSToomas Soome int
yylex(void)174*1a90c98dSToomas Soome yylex(void)
1757c478bd9Sstevel@tonic-gate {
1767c478bd9Sstevel@tonic-gate int c, n, isbuilding, rval, lnext, nokey = 0;
1777c478bd9Sstevel@tonic-gate char *name;
1787c478bd9Sstevel@tonic-gate
1797c478bd9Sstevel@tonic-gate isbuilding = 0;
1807c478bd9Sstevel@tonic-gate lnext = 0;
1817c478bd9Sstevel@tonic-gate rval = 0;
1827c478bd9Sstevel@tonic-gate
1837c478bd9Sstevel@tonic-gate if (yystr != NULL) {
1847c478bd9Sstevel@tonic-gate free(yystr);
1857c478bd9Sstevel@tonic-gate yystr = NULL;
1867c478bd9Sstevel@tonic-gate }
1877c478bd9Sstevel@tonic-gate
1887c478bd9Sstevel@tonic-gate nextchar:
18933f2fefdSDarren Reed c = yygetc(0);
19033f2fefdSDarren Reed if (yydebug > 1)
19133f2fefdSDarren Reed printf("yygetc = (%x) %c [%*.*s]\n", c, c, yypos, yypos,
19233f2fefdSDarren Reed yytexttochar());
1937c478bd9Sstevel@tonic-gate
194*1a90c98dSToomas Soome switch (c) {
1957c478bd9Sstevel@tonic-gate case '\n' :
19633f2fefdSDarren Reed lnext = 0;
19733f2fefdSDarren Reed nokey = 0;
198af5f29ddSToomas Soome /* FALLTHROUGH */
1997c478bd9Sstevel@tonic-gate case '\t' :
2007c478bd9Sstevel@tonic-gate case '\r' :
2017c478bd9Sstevel@tonic-gate case ' ' :
2027c478bd9Sstevel@tonic-gate if (isbuilding == 1) {
2037c478bd9Sstevel@tonic-gate yyunputc(c);
2047c478bd9Sstevel@tonic-gate goto done;
2057c478bd9Sstevel@tonic-gate }
2067c478bd9Sstevel@tonic-gate if (yylast > yypos) {
2077c478bd9Sstevel@tonic-gate bcopy(yytext + yypos, yytext,
2087c478bd9Sstevel@tonic-gate sizeof (yytext[0]) * (yylast - yypos + 1));
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate yylast -= yypos;
2117c478bd9Sstevel@tonic-gate yypos = 0;
212ab25eeb5Syz155240 lnext = 0;
213ab25eeb5Syz155240 nokey = 0;
2147c478bd9Sstevel@tonic-gate goto nextchar;
2157c478bd9Sstevel@tonic-gate
2167c478bd9Sstevel@tonic-gate case '\\' :
2177c478bd9Sstevel@tonic-gate if (lnext == 0) {
2187c478bd9Sstevel@tonic-gate lnext = 1;
2197c478bd9Sstevel@tonic-gate if (yylast == yypos) {
2207c478bd9Sstevel@tonic-gate yylast--;
2217c478bd9Sstevel@tonic-gate yypos--;
2227c478bd9Sstevel@tonic-gate } else
2237c478bd9Sstevel@tonic-gate yypos--;
2247c478bd9Sstevel@tonic-gate if (yypos == 0)
2257c478bd9Sstevel@tonic-gate nokey = 1;
2267c478bd9Sstevel@tonic-gate goto nextchar;
2277c478bd9Sstevel@tonic-gate }
2287c478bd9Sstevel@tonic-gate break;
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate
2317c478bd9Sstevel@tonic-gate if (lnext == 1) {
2327c478bd9Sstevel@tonic-gate lnext = 0;
233ab25eeb5Syz155240 if ((isbuilding == 0) && !ISALNUM(c)) {
234*1a90c98dSToomas Soome return (c);
235ab25eeb5Syz155240 }
2367c478bd9Sstevel@tonic-gate goto nextchar;
2377c478bd9Sstevel@tonic-gate }
2387c478bd9Sstevel@tonic-gate
239*1a90c98dSToomas Soome switch (c) {
2407c478bd9Sstevel@tonic-gate case '#' :
2417c478bd9Sstevel@tonic-gate if (isbuilding == 1) {
2427c478bd9Sstevel@tonic-gate yyunputc(c);
2437c478bd9Sstevel@tonic-gate goto done;
2447c478bd9Sstevel@tonic-gate }
2457c478bd9Sstevel@tonic-gate yyswallow('\n');
2467c478bd9Sstevel@tonic-gate rval = YY_COMMENT;
24722929378SDarren Reed goto done;
2487c478bd9Sstevel@tonic-gate
2497c478bd9Sstevel@tonic-gate case '$' :
2507c478bd9Sstevel@tonic-gate if (isbuilding == 1) {
2517c478bd9Sstevel@tonic-gate yyunputc(c);
2527c478bd9Sstevel@tonic-gate goto done;
2537c478bd9Sstevel@tonic-gate }
25433f2fefdSDarren Reed n = yygetc(0);
2557c478bd9Sstevel@tonic-gate if (n == '{') {
2567c478bd9Sstevel@tonic-gate if (yyswallow('}') == -1) {
2577c478bd9Sstevel@tonic-gate rval = -2;
2587c478bd9Sstevel@tonic-gate goto done;
2597c478bd9Sstevel@tonic-gate }
26033f2fefdSDarren Reed (void) yygetc(0);
2617c478bd9Sstevel@tonic-gate } else {
262ab25eeb5Syz155240 if (!ISALPHA(n)) {
2637c478bd9Sstevel@tonic-gate yyunputc(n);
2647c478bd9Sstevel@tonic-gate break;
2657c478bd9Sstevel@tonic-gate }
2667c478bd9Sstevel@tonic-gate do {
26733f2fefdSDarren Reed n = yygetc(1);
268ab25eeb5Syz155240 } while (ISALPHA(n) || ISDIGIT(n) || n == '_');
2697c478bd9Sstevel@tonic-gate yyunputc(n);
2707c478bd9Sstevel@tonic-gate }
2717c478bd9Sstevel@tonic-gate
2727c478bd9Sstevel@tonic-gate name = yytexttostr(1, yypos); /* skip $ */
2737c478bd9Sstevel@tonic-gate
2747c478bd9Sstevel@tonic-gate if (name != NULL) {
2757c478bd9Sstevel@tonic-gate string_val = get_variable(name, NULL, yylineNum);
2767c478bd9Sstevel@tonic-gate free(name);
2777c478bd9Sstevel@tonic-gate if (string_val != NULL) {
2787c478bd9Sstevel@tonic-gate name = yytexttostr(yypos, yylast);
2797c478bd9Sstevel@tonic-gate if (name != NULL) {
2807c478bd9Sstevel@tonic-gate yypos = 0;
2817c478bd9Sstevel@tonic-gate yylast = 0;
2827c478bd9Sstevel@tonic-gate yystrtotext(string_val);
2837c478bd9Sstevel@tonic-gate yystrtotext(name);
2847c478bd9Sstevel@tonic-gate free(string_val);
2857c478bd9Sstevel@tonic-gate free(name);
2867c478bd9Sstevel@tonic-gate goto nextchar;
2877c478bd9Sstevel@tonic-gate }
2887c478bd9Sstevel@tonic-gate free(string_val);
2897c478bd9Sstevel@tonic-gate }
2907c478bd9Sstevel@tonic-gate }
2917c478bd9Sstevel@tonic-gate break;
2927c478bd9Sstevel@tonic-gate
2937c478bd9Sstevel@tonic-gate case '\'':
2947c478bd9Sstevel@tonic-gate case '"' :
2957c478bd9Sstevel@tonic-gate if (isbuilding == 1) {
2967c478bd9Sstevel@tonic-gate goto done;
2977c478bd9Sstevel@tonic-gate }
2987c478bd9Sstevel@tonic-gate do {
29933f2fefdSDarren Reed n = yygetc(1);
3007c478bd9Sstevel@tonic-gate if (n == EOF || n == TOOLONG) {
3017c478bd9Sstevel@tonic-gate rval = -2;
3027c478bd9Sstevel@tonic-gate goto done;
3037c478bd9Sstevel@tonic-gate }
3047c478bd9Sstevel@tonic-gate if (n == '\n') {
3057c478bd9Sstevel@tonic-gate yyunputc(' ');
3067c478bd9Sstevel@tonic-gate yypos++;
3077c478bd9Sstevel@tonic-gate }
3087c478bd9Sstevel@tonic-gate } while (n != c);
30933f2fefdSDarren Reed rval = YY_STR;
31033f2fefdSDarren Reed goto done;
31133f2fefdSDarren Reed /* NOTREACHED */
3127c478bd9Sstevel@tonic-gate
3137c478bd9Sstevel@tonic-gate case EOF :
3147c478bd9Sstevel@tonic-gate yylineNum = 1;
3157c478bd9Sstevel@tonic-gate yypos = 0;
3167c478bd9Sstevel@tonic-gate yylast = -1;
3177c478bd9Sstevel@tonic-gate yyexpectaddr = 0;
3187c478bd9Sstevel@tonic-gate yybreakondot = 0;
3197c478bd9Sstevel@tonic-gate yyvarnext = 0;
3207c478bd9Sstevel@tonic-gate yytokentype = 0;
321*1a90c98dSToomas Soome return (0);
3227c478bd9Sstevel@tonic-gate }
3237c478bd9Sstevel@tonic-gate
3247c478bd9Sstevel@tonic-gate if (strchr("=,/;{}()@", c) != NULL) {
3257c478bd9Sstevel@tonic-gate if (isbuilding == 1) {
3267c478bd9Sstevel@tonic-gate yyunputc(c);
3277c478bd9Sstevel@tonic-gate goto done;
3287c478bd9Sstevel@tonic-gate }
3297c478bd9Sstevel@tonic-gate rval = c;
3307c478bd9Sstevel@tonic-gate goto done;
3317c478bd9Sstevel@tonic-gate } else if (c == '.') {
3327c478bd9Sstevel@tonic-gate if (isbuilding == 0) {
3337c478bd9Sstevel@tonic-gate rval = c;
3347c478bd9Sstevel@tonic-gate goto done;
3357c478bd9Sstevel@tonic-gate }
3367c478bd9Sstevel@tonic-gate if (yybreakondot != 0) {
3377c478bd9Sstevel@tonic-gate yyunputc(c);
3387c478bd9Sstevel@tonic-gate goto done;
3397c478bd9Sstevel@tonic-gate }
3407c478bd9Sstevel@tonic-gate }
3417c478bd9Sstevel@tonic-gate
342*1a90c98dSToomas Soome switch (c) {
3437c478bd9Sstevel@tonic-gate case '-' :
3447c478bd9Sstevel@tonic-gate if (yyexpectaddr)
3457c478bd9Sstevel@tonic-gate break;
3467c478bd9Sstevel@tonic-gate if (isbuilding == 1)
3477c478bd9Sstevel@tonic-gate break;
34833f2fefdSDarren Reed n = yygetc(0);
3497c478bd9Sstevel@tonic-gate if (n == '>') {
3507c478bd9Sstevel@tonic-gate isbuilding = 1;
3517c478bd9Sstevel@tonic-gate goto done;
3527c478bd9Sstevel@tonic-gate }
3537c478bd9Sstevel@tonic-gate yyunputc(n);
3547c478bd9Sstevel@tonic-gate rval = '-';
3557c478bd9Sstevel@tonic-gate goto done;
3567c478bd9Sstevel@tonic-gate
3577c478bd9Sstevel@tonic-gate case '!' :
3587c478bd9Sstevel@tonic-gate if (isbuilding == 1) {
3597c478bd9Sstevel@tonic-gate yyunputc(c);
3607c478bd9Sstevel@tonic-gate goto done;
3617c478bd9Sstevel@tonic-gate }
36233f2fefdSDarren Reed n = yygetc(0);
3637c478bd9Sstevel@tonic-gate if (n == '=') {
3647c478bd9Sstevel@tonic-gate rval = YY_CMP_NE;
3657c478bd9Sstevel@tonic-gate goto done;
3667c478bd9Sstevel@tonic-gate }
3677c478bd9Sstevel@tonic-gate yyunputc(n);
3687c478bd9Sstevel@tonic-gate rval = '!';
3697c478bd9Sstevel@tonic-gate goto done;
3707c478bd9Sstevel@tonic-gate
3717c478bd9Sstevel@tonic-gate case '<' :
3727c478bd9Sstevel@tonic-gate if (yyexpectaddr)
3737c478bd9Sstevel@tonic-gate break;
3747c478bd9Sstevel@tonic-gate if (isbuilding == 1) {
3757c478bd9Sstevel@tonic-gate yyunputc(c);
3767c478bd9Sstevel@tonic-gate goto done;
3777c478bd9Sstevel@tonic-gate }
37833f2fefdSDarren Reed n = yygetc(0);
3797c478bd9Sstevel@tonic-gate if (n == '=') {
3807c478bd9Sstevel@tonic-gate rval = YY_CMP_LE;
3817c478bd9Sstevel@tonic-gate goto done;
3827c478bd9Sstevel@tonic-gate }
3837c478bd9Sstevel@tonic-gate if (n == '>') {
3847c478bd9Sstevel@tonic-gate rval = YY_RANGE_OUT;
3857c478bd9Sstevel@tonic-gate goto done;
3867c478bd9Sstevel@tonic-gate }
3877c478bd9Sstevel@tonic-gate yyunputc(n);
3887c478bd9Sstevel@tonic-gate rval = YY_CMP_LT;
3897c478bd9Sstevel@tonic-gate goto done;
3907c478bd9Sstevel@tonic-gate
3917c478bd9Sstevel@tonic-gate case '>' :
3927c478bd9Sstevel@tonic-gate if (yyexpectaddr)
3937c478bd9Sstevel@tonic-gate break;
3947c478bd9Sstevel@tonic-gate if (isbuilding == 1) {
3957c478bd9Sstevel@tonic-gate yyunputc(c);
3967c478bd9Sstevel@tonic-gate goto done;
3977c478bd9Sstevel@tonic-gate }
39833f2fefdSDarren Reed n = yygetc(0);
3997c478bd9Sstevel@tonic-gate if (n == '=') {
4007c478bd9Sstevel@tonic-gate rval = YY_CMP_GE;
4017c478bd9Sstevel@tonic-gate goto done;
4027c478bd9Sstevel@tonic-gate }
4037c478bd9Sstevel@tonic-gate if (n == '<') {
4047c478bd9Sstevel@tonic-gate rval = YY_RANGE_IN;
4057c478bd9Sstevel@tonic-gate goto done;
4067c478bd9Sstevel@tonic-gate }
4077c478bd9Sstevel@tonic-gate yyunputc(n);
4087c478bd9Sstevel@tonic-gate rval = YY_CMP_GT;
4097c478bd9Sstevel@tonic-gate goto done;
4107c478bd9Sstevel@tonic-gate }
4117c478bd9Sstevel@tonic-gate
4127c478bd9Sstevel@tonic-gate /*
4137c478bd9Sstevel@tonic-gate * Now for the reason this is here...IPv6 address parsing.
4147c478bd9Sstevel@tonic-gate * The longest string we can expect is of this form:
4157c478bd9Sstevel@tonic-gate * 0000:0000:0000:0000:0000:0000:000.000.000.000
4167c478bd9Sstevel@tonic-gate * not:
4177c478bd9Sstevel@tonic-gate * 0000:0000:0000:0000:0000:0000:0000:0000
4187c478bd9Sstevel@tonic-gate */
4197c478bd9Sstevel@tonic-gate #ifdef USE_INET6
420d6c23f6fSyx160601 if (isbuilding == 0 && (ishex(c) || c == ':')) {
4217c478bd9Sstevel@tonic-gate char ipv6buf[45 + 1], *s, oc;
4227c478bd9Sstevel@tonic-gate int start;
4237c478bd9Sstevel@tonic-gate
4247c478bd9Sstevel@tonic-gate start = yypos;
4257c478bd9Sstevel@tonic-gate s = ipv6buf;
4267c478bd9Sstevel@tonic-gate oc = c;
4277c478bd9Sstevel@tonic-gate
4287c478bd9Sstevel@tonic-gate /*
4297c478bd9Sstevel@tonic-gate * Perhaps we should implement stricter controls on what we
4307c478bd9Sstevel@tonic-gate * swallow up here, but surely it would just be duplicating
4317c478bd9Sstevel@tonic-gate * the code in inet_pton() anyway.
4327c478bd9Sstevel@tonic-gate */
4337c478bd9Sstevel@tonic-gate do {
4347c478bd9Sstevel@tonic-gate *s++ = c;
43533f2fefdSDarren Reed c = yygetc(1);
4367c478bd9Sstevel@tonic-gate } while ((ishex(c) || c == ':' || c == '.') &&
4377c478bd9Sstevel@tonic-gate (s - ipv6buf < 46));
4387c478bd9Sstevel@tonic-gate yyunputc(c);
4397c478bd9Sstevel@tonic-gate *s = '\0';
4407c478bd9Sstevel@tonic-gate
4417c478bd9Sstevel@tonic-gate if (inet_pton(AF_INET6, ipv6buf, &yylval.ip6) == 1) {
4427c478bd9Sstevel@tonic-gate rval = YY_IPV6;
4437c478bd9Sstevel@tonic-gate yyexpectaddr = 0;
4447c478bd9Sstevel@tonic-gate goto done;
4457c478bd9Sstevel@tonic-gate }
4467c478bd9Sstevel@tonic-gate yypos = start;
4477c478bd9Sstevel@tonic-gate c = oc;
4487c478bd9Sstevel@tonic-gate }
4497c478bd9Sstevel@tonic-gate #endif
4507c478bd9Sstevel@tonic-gate
4517c478bd9Sstevel@tonic-gate if (c == ':') {
4527c478bd9Sstevel@tonic-gate if (isbuilding == 1) {
4537c478bd9Sstevel@tonic-gate yyunputc(c);
4547c478bd9Sstevel@tonic-gate goto done;
4557c478bd9Sstevel@tonic-gate }
4567c478bd9Sstevel@tonic-gate rval = ':';
4577c478bd9Sstevel@tonic-gate goto done;
4587c478bd9Sstevel@tonic-gate }
4597c478bd9Sstevel@tonic-gate
4607c478bd9Sstevel@tonic-gate if (isbuilding == 0 && c == '0') {
46133f2fefdSDarren Reed n = yygetc(0);
4627c478bd9Sstevel@tonic-gate if (n == 'x') {
4637c478bd9Sstevel@tonic-gate do {
46433f2fefdSDarren Reed n = yygetc(1);
4657c478bd9Sstevel@tonic-gate } while (ishex(n));
4667c478bd9Sstevel@tonic-gate yyunputc(n);
4677c478bd9Sstevel@tonic-gate rval = YY_HEX;
4687c478bd9Sstevel@tonic-gate goto done;
4697c478bd9Sstevel@tonic-gate }
4707c478bd9Sstevel@tonic-gate yyunputc(n);
4717c478bd9Sstevel@tonic-gate }
4727c478bd9Sstevel@tonic-gate
4737c478bd9Sstevel@tonic-gate /*
4747c478bd9Sstevel@tonic-gate * No negative numbers with leading - sign..
4757c478bd9Sstevel@tonic-gate */
476ab25eeb5Syz155240 if (isbuilding == 0 && ISDIGIT(c)) {
4777c478bd9Sstevel@tonic-gate do {
47833f2fefdSDarren Reed n = yygetc(1);
479ab25eeb5Syz155240 } while (ISDIGIT(n));
4807c478bd9Sstevel@tonic-gate yyunputc(n);
4817c478bd9Sstevel@tonic-gate rval = YY_NUMBER;
4827c478bd9Sstevel@tonic-gate goto done;
4837c478bd9Sstevel@tonic-gate }
4847c478bd9Sstevel@tonic-gate
4857c478bd9Sstevel@tonic-gate isbuilding = 1;
4867c478bd9Sstevel@tonic-gate goto nextchar;
4877c478bd9Sstevel@tonic-gate
4887c478bd9Sstevel@tonic-gate done:
4897c478bd9Sstevel@tonic-gate yystr = yytexttostr(0, yypos);
4907c478bd9Sstevel@tonic-gate
49133f2fefdSDarren Reed if (yydebug)
49233f2fefdSDarren Reed printf("isbuilding %d yyvarnext %d nokey %d\n",
49333f2fefdSDarren Reed isbuilding, yyvarnext, nokey);
4947c478bd9Sstevel@tonic-gate if (isbuilding == 1) {
4957c478bd9Sstevel@tonic-gate wordtab_t *w;
4967c478bd9Sstevel@tonic-gate
4977c478bd9Sstevel@tonic-gate w = NULL;
4987c478bd9Sstevel@tonic-gate isbuilding = 0;
4997c478bd9Sstevel@tonic-gate
5007c478bd9Sstevel@tonic-gate if ((yyvarnext == 0) && (nokey == 0)) {
5017c478bd9Sstevel@tonic-gate w = yyfindkey(yystr);
5027c478bd9Sstevel@tonic-gate if (w == NULL && yywordtab != NULL) {
5037c478bd9Sstevel@tonic-gate yyresetdict();
5047c478bd9Sstevel@tonic-gate w = yyfindkey(yystr);
5057c478bd9Sstevel@tonic-gate }
5067c478bd9Sstevel@tonic-gate } else
5077c478bd9Sstevel@tonic-gate yyvarnext = 0;
5087c478bd9Sstevel@tonic-gate if (w != NULL)
5097c478bd9Sstevel@tonic-gate rval = w->w_value;
5107c478bd9Sstevel@tonic-gate else
5117c478bd9Sstevel@tonic-gate rval = YY_STR;
5127c478bd9Sstevel@tonic-gate }
5137c478bd9Sstevel@tonic-gate
5147c478bd9Sstevel@tonic-gate if (rval == YY_STR && yysavedepth > 0)
5157c478bd9Sstevel@tonic-gate yyresetdict();
5167c478bd9Sstevel@tonic-gate
5177c478bd9Sstevel@tonic-gate yytokentype = rval;
5187c478bd9Sstevel@tonic-gate
5197c478bd9Sstevel@tonic-gate if (yydebug)
52033f2fefdSDarren Reed printf("lexed(%s) [%d,%d,%d] => %d @%d\n", yystr, string_start,
52133f2fefdSDarren Reed string_end, pos, rval, yysavedepth);
5227c478bd9Sstevel@tonic-gate
523*1a90c98dSToomas Soome switch (rval) {
5247c478bd9Sstevel@tonic-gate case YY_NUMBER :
525ab25eeb5Syz155240 sscanf(yystr, "%u", &yylval.num);
5267c478bd9Sstevel@tonic-gate break;
5277c478bd9Sstevel@tonic-gate
5287c478bd9Sstevel@tonic-gate case YY_HEX :
529*1a90c98dSToomas Soome sscanf(yystr, "0x%x", (uint_t *)&yylval.num);
5307c478bd9Sstevel@tonic-gate break;
5317c478bd9Sstevel@tonic-gate
5327c478bd9Sstevel@tonic-gate case YY_STR :
5337c478bd9Sstevel@tonic-gate yylval.str = strdup(yystr);
5347c478bd9Sstevel@tonic-gate break;
5357c478bd9Sstevel@tonic-gate
5367c478bd9Sstevel@tonic-gate default :
5377c478bd9Sstevel@tonic-gate break;
5387c478bd9Sstevel@tonic-gate }
5397c478bd9Sstevel@tonic-gate
5407c478bd9Sstevel@tonic-gate if (yylast > 0) {
5417c478bd9Sstevel@tonic-gate bcopy(yytext + yypos, yytext,
5427c478bd9Sstevel@tonic-gate sizeof (yytext[0]) * (yylast - yypos + 1));
5437c478bd9Sstevel@tonic-gate yylast -= yypos;
5447c478bd9Sstevel@tonic-gate yypos = 0;
5457c478bd9Sstevel@tonic-gate }
5467c478bd9Sstevel@tonic-gate
547*1a90c98dSToomas Soome return (rval);
5487c478bd9Sstevel@tonic-gate }
5497c478bd9Sstevel@tonic-gate
5507c478bd9Sstevel@tonic-gate
yyfindkey(char * key)551*1a90c98dSToomas Soome static wordtab_t *yyfindkey(char *key)
5527c478bd9Sstevel@tonic-gate {
5537c478bd9Sstevel@tonic-gate wordtab_t *w;
5547c478bd9Sstevel@tonic-gate
5557c478bd9Sstevel@tonic-gate if (yywordtab == NULL)
556*1a90c98dSToomas Soome return (NULL);
5577c478bd9Sstevel@tonic-gate
5587c478bd9Sstevel@tonic-gate for (w = yywordtab; w->w_word != 0; w++)
5597c478bd9Sstevel@tonic-gate if (strcasecmp(key, w->w_word) == 0)
560*1a90c98dSToomas Soome return (w);
561*1a90c98dSToomas Soome return (NULL);
5627c478bd9Sstevel@tonic-gate }
5637c478bd9Sstevel@tonic-gate
5647c478bd9Sstevel@tonic-gate
565*1a90c98dSToomas Soome char *
yykeytostr(int num)566*1a90c98dSToomas Soome yykeytostr(int num)
5677c478bd9Sstevel@tonic-gate {
5687c478bd9Sstevel@tonic-gate wordtab_t *w;
5697c478bd9Sstevel@tonic-gate
5707c478bd9Sstevel@tonic-gate if (yywordtab == NULL)
571*1a90c98dSToomas Soome return ("<unknown>");
5727c478bd9Sstevel@tonic-gate
5737c478bd9Sstevel@tonic-gate for (w = yywordtab; w->w_word; w++)
5747c478bd9Sstevel@tonic-gate if (w->w_value == num)
575*1a90c98dSToomas Soome return (w->w_word);
576*1a90c98dSToomas Soome return ("<unknown>");
5777c478bd9Sstevel@tonic-gate }
5787c478bd9Sstevel@tonic-gate
5797c478bd9Sstevel@tonic-gate
580*1a90c98dSToomas Soome wordtab_t *
yysettab(wordtab_t * words)581*1a90c98dSToomas Soome yysettab(wordtab_t *words)
5827c478bd9Sstevel@tonic-gate {
5837c478bd9Sstevel@tonic-gate wordtab_t *save;
5847c478bd9Sstevel@tonic-gate
5857c478bd9Sstevel@tonic-gate save = yywordtab;
5867c478bd9Sstevel@tonic-gate yywordtab = words;
587*1a90c98dSToomas Soome return (save);
5887c478bd9Sstevel@tonic-gate }
5897c478bd9Sstevel@tonic-gate
5907c478bd9Sstevel@tonic-gate
591*1a90c98dSToomas Soome int
yyerror(const char * msg)592*1a90c98dSToomas Soome yyerror(const char *msg)
5937c478bd9Sstevel@tonic-gate {
5947c478bd9Sstevel@tonic-gate char *txt, letter[2];
5957c478bd9Sstevel@tonic-gate int freetxt = 0;
5967c478bd9Sstevel@tonic-gate
5977c478bd9Sstevel@tonic-gate if (yytokentype < 256) {
5987c478bd9Sstevel@tonic-gate letter[0] = yytokentype;
5997c478bd9Sstevel@tonic-gate letter[1] = '\0';
6007c478bd9Sstevel@tonic-gate txt = letter;
6017c478bd9Sstevel@tonic-gate } else if (yytokentype == YY_STR || yytokentype == YY_HEX ||
6027c478bd9Sstevel@tonic-gate yytokentype == YY_NUMBER) {
6037c478bd9Sstevel@tonic-gate if (yystr == NULL) {
6047c478bd9Sstevel@tonic-gate txt = yytexttostr(yypos, YYBUFSIZ);
6055e985db5Sschuster if (txt == NULL) {
6065e985db5Sschuster fprintf(stderr, "sorry, out of memory,"
6075e985db5Sschuster " bailing out\n");
6085e985db5Sschuster exit(1);
6095e985db5Sschuster }
6107c478bd9Sstevel@tonic-gate freetxt = 1;
6117c478bd9Sstevel@tonic-gate } else
6127c478bd9Sstevel@tonic-gate txt = yystr;
6137c478bd9Sstevel@tonic-gate } else {
6147c478bd9Sstevel@tonic-gate txt = yykeytostr(yytokentype);
6155e985db5Sschuster if (txt == NULL) {
6165e985db5Sschuster fprintf(stderr, "sorry, out of memory,"
6175e985db5Sschuster " bailing out\n");
6185e985db5Sschuster exit(1);
6195e985db5Sschuster }
6207c478bd9Sstevel@tonic-gate }
6217c478bd9Sstevel@tonic-gate fprintf(stderr, "%s error at \"%s\", line %d\n", msg, txt, yylineNum);
6227c478bd9Sstevel@tonic-gate if (freetxt == 1)
6237c478bd9Sstevel@tonic-gate free(txt);
6247c478bd9Sstevel@tonic-gate exit(1);
6257c478bd9Sstevel@tonic-gate }
6267c478bd9Sstevel@tonic-gate
6277c478bd9Sstevel@tonic-gate
628*1a90c98dSToomas Soome void
yysetdict(wordtab_t * newdict)629*1a90c98dSToomas Soome yysetdict(wordtab_t *newdict)
6307c478bd9Sstevel@tonic-gate {
6317c478bd9Sstevel@tonic-gate if (yysavedepth == sizeof (yysavewords) / sizeof (yysavewords[0])) {
6327c478bd9Sstevel@tonic-gate fprintf(stderr, "%d: at maximum dictionary depth\n",
6337c478bd9Sstevel@tonic-gate yylineNum);
6347c478bd9Sstevel@tonic-gate return;
6357c478bd9Sstevel@tonic-gate }
6367c478bd9Sstevel@tonic-gate
6377c478bd9Sstevel@tonic-gate yysavewords[yysavedepth++] = yysettab(newdict);
6387c478bd9Sstevel@tonic-gate if (yydebug)
6397c478bd9Sstevel@tonic-gate printf("yysavedepth++ => %d\n", yysavedepth);
6407c478bd9Sstevel@tonic-gate }
6417c478bd9Sstevel@tonic-gate
642*1a90c98dSToomas Soome void
yyresetdict(void)643*1a90c98dSToomas Soome yyresetdict(void)
6447c478bd9Sstevel@tonic-gate {
64533f2fefdSDarren Reed if (yydebug)
64633f2fefdSDarren Reed printf("yyresetdict(%d)\n", yysavedepth);
6477c478bd9Sstevel@tonic-gate if (yysavedepth > 0) {
6487c478bd9Sstevel@tonic-gate yysettab(yysavewords[--yysavedepth]);
6497c478bd9Sstevel@tonic-gate if (yydebug)
6507c478bd9Sstevel@tonic-gate printf("yysavedepth-- => %d\n", yysavedepth);
6517c478bd9Sstevel@tonic-gate }
6527c478bd9Sstevel@tonic-gate }
6537c478bd9Sstevel@tonic-gate
6547c478bd9Sstevel@tonic-gate
6557c478bd9Sstevel@tonic-gate
6567c478bd9Sstevel@tonic-gate #ifdef TEST_LEXER
657*1a90c98dSToomas Soome int
main(int argc,char * argv[])658*1a90c98dSToomas Soome main(int argc, char *argv[])
6597c478bd9Sstevel@tonic-gate {
6607c478bd9Sstevel@tonic-gate int n;
6617c478bd9Sstevel@tonic-gate
6627c478bd9Sstevel@tonic-gate yyin = stdin;
6637c478bd9Sstevel@tonic-gate
6647c478bd9Sstevel@tonic-gate while ((n = yylex()) != 0)
6657c478bd9Sstevel@tonic-gate printf("%d.n = %d [%s] %d %d\n",
6667c478bd9Sstevel@tonic-gate yylineNum, n, yystr, yypos, yylast);
6677c478bd9Sstevel@tonic-gate }
6687c478bd9Sstevel@tonic-gate #endif
669