xref: /onnv-gate/usr/src/cmd/ipf/tools/lexer.c (revision 8813:156991416217)
10Sstevel@tonic-gate /*
27176Syx160601  * Copyright (C) 2002-2008 by Darren Reed.
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * See the IPFILTER.LICENCE file for details on licencing.
50Sstevel@tonic-gate  *
68624SDarren.Reed@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
70Sstevel@tonic-gate  * Use is subject to license terms.
80Sstevel@tonic-gate  */
90Sstevel@tonic-gate 
100Sstevel@tonic-gate #include <ctype.h>
110Sstevel@tonic-gate #include "ipf.h"
120Sstevel@tonic-gate #ifdef	IPFILTER_SCAN
130Sstevel@tonic-gate # include "netinet/ip_scan.h"
140Sstevel@tonic-gate #endif
150Sstevel@tonic-gate #include <sys/ioctl.h>
160Sstevel@tonic-gate #include <syslog.h>
170Sstevel@tonic-gate #ifdef	TEST_LEXER
180Sstevel@tonic-gate # define	NO_YACC
190Sstevel@tonic-gate union	{
200Sstevel@tonic-gate 	int		num;
210Sstevel@tonic-gate 	char		*str;
220Sstevel@tonic-gate 	struct in_addr	ipa;
230Sstevel@tonic-gate 	i6addr_t	ip6;
240Sstevel@tonic-gate } yylval;
250Sstevel@tonic-gate #endif
260Sstevel@tonic-gate #include "lexer.h"
270Sstevel@tonic-gate #include "y.tab.h"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate FILE *yyin;
300Sstevel@tonic-gate 
312393Syz155240 #define	ishex(c)	(ISDIGIT(c) || ((c) >= 'a' && (c) <= 'f') || \
320Sstevel@tonic-gate 			 ((c) >= 'A' && (c) <= 'F'))
330Sstevel@tonic-gate #define	TOOLONG		-3
340Sstevel@tonic-gate 
350Sstevel@tonic-gate extern int	string_start;
360Sstevel@tonic-gate extern int	string_end;
370Sstevel@tonic-gate extern char	*string_val;
380Sstevel@tonic-gate extern int	pos;
390Sstevel@tonic-gate extern int	yydebug;
400Sstevel@tonic-gate 
410Sstevel@tonic-gate char		*yystr = NULL;
420Sstevel@tonic-gate int		yytext[YYBUFSIZ+1];
438624SDarren.Reed@Sun.COM char		yychars[YYBUFSIZ+1];
440Sstevel@tonic-gate int		yylineNum = 1;
450Sstevel@tonic-gate int		yypos = 0;
460Sstevel@tonic-gate int		yylast = -1;
470Sstevel@tonic-gate int		yyexpectaddr = 0;
480Sstevel@tonic-gate int		yybreakondot = 0;
490Sstevel@tonic-gate int		yyvarnext = 0;
500Sstevel@tonic-gate int		yytokentype = 0;
510Sstevel@tonic-gate wordtab_t	*yywordtab = NULL;
520Sstevel@tonic-gate int		yysavedepth = 0;
530Sstevel@tonic-gate wordtab_t	*yysavewords[30];
540Sstevel@tonic-gate 
550Sstevel@tonic-gate 
560Sstevel@tonic-gate static	wordtab_t	*yyfindkey __P((char *));
578624SDarren.Reed@Sun.COM static	int		yygetc __P((int));
580Sstevel@tonic-gate static	void		yyunputc __P((int));
590Sstevel@tonic-gate static	int		yyswallow __P((int));
600Sstevel@tonic-gate static	char		*yytexttostr __P((int, int));
610Sstevel@tonic-gate static	void		yystrtotext __P((char *));
628624SDarren.Reed@Sun.COM static	char		*yytexttochar __P((void));
630Sstevel@tonic-gate 
yygetc(docont)648624SDarren.Reed@Sun.COM static int yygetc(docont)
658624SDarren.Reed@Sun.COM int docont;
660Sstevel@tonic-gate {
670Sstevel@tonic-gate 	int c;
680Sstevel@tonic-gate 
690Sstevel@tonic-gate 	if (yypos < yylast) {
700Sstevel@tonic-gate 		c = yytext[yypos++];
712393Syz155240 		if (c == '\n')
722393Syz155240 			yylineNum++;
730Sstevel@tonic-gate 		return c;
740Sstevel@tonic-gate 	}
750Sstevel@tonic-gate 
760Sstevel@tonic-gate 	if (yypos == YYBUFSIZ)
770Sstevel@tonic-gate 		return TOOLONG;
780Sstevel@tonic-gate 
790Sstevel@tonic-gate 	if (pos >= string_start && pos <= string_end) {
800Sstevel@tonic-gate 		c = string_val[pos - string_start];
810Sstevel@tonic-gate 		yypos++;
820Sstevel@tonic-gate 	} else {
830Sstevel@tonic-gate 		c = fgetc(yyin);
848624SDarren.Reed@Sun.COM 		if (docont && (c == '\\')) {
858624SDarren.Reed@Sun.COM 			c = fgetc(yyin);
868624SDarren.Reed@Sun.COM 			if (c == '\n') {
878624SDarren.Reed@Sun.COM 				yylineNum++;
888624SDarren.Reed@Sun.COM 				c = fgetc(yyin);
898624SDarren.Reed@Sun.COM 			}
908624SDarren.Reed@Sun.COM 		}
910Sstevel@tonic-gate 	}
922393Syz155240 	if (c == '\n')
932393Syz155240 		yylineNum++;
940Sstevel@tonic-gate 	yytext[yypos++] = c;
950Sstevel@tonic-gate 	yylast = yypos;
960Sstevel@tonic-gate 	yytext[yypos] = '\0';
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	return c;
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 
yyunputc(c)1020Sstevel@tonic-gate static void yyunputc(c)
1030Sstevel@tonic-gate int c;
1040Sstevel@tonic-gate {
1052393Syz155240 	if (c == '\n')
1062393Syz155240 		yylineNum--;
1070Sstevel@tonic-gate 	yytext[--yypos] = c;
1080Sstevel@tonic-gate }
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 
yyswallow(last)1110Sstevel@tonic-gate static int yyswallow(last)
1120Sstevel@tonic-gate int last;
1130Sstevel@tonic-gate {
1140Sstevel@tonic-gate 	int c;
1150Sstevel@tonic-gate 
1168624SDarren.Reed@Sun.COM 	while (((c = yygetc(0)) > '\0') && (c != last))
1170Sstevel@tonic-gate 		;
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	if (c != EOF)
1200Sstevel@tonic-gate 		yyunputc(c);
1210Sstevel@tonic-gate 	if (c == last)
1220Sstevel@tonic-gate 		return 0;
1230Sstevel@tonic-gate 	return -1;
1240Sstevel@tonic-gate }
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 
yytexttochar()1278624SDarren.Reed@Sun.COM static char *yytexttochar()
1288624SDarren.Reed@Sun.COM {
1298624SDarren.Reed@Sun.COM 	int i;
1308624SDarren.Reed@Sun.COM 
1318624SDarren.Reed@Sun.COM 	for (i = 0; i < yypos; i++)
1328624SDarren.Reed@Sun.COM 		yychars[i] = (char)(yytext[i] & 0xff);
1338624SDarren.Reed@Sun.COM 	yychars[i] = '\0';
1348624SDarren.Reed@Sun.COM 	return yychars;
1358624SDarren.Reed@Sun.COM }
1368624SDarren.Reed@Sun.COM 
1378624SDarren.Reed@Sun.COM 
yystrtotext(str)1380Sstevel@tonic-gate static void yystrtotext(str)
1390Sstevel@tonic-gate char *str;
1400Sstevel@tonic-gate {
1410Sstevel@tonic-gate 	int len;
1420Sstevel@tonic-gate 	char *s;
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	len = strlen(str);
1450Sstevel@tonic-gate 	if (len > YYBUFSIZ)
1460Sstevel@tonic-gate 		len = YYBUFSIZ;
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	for (s = str; *s != '\0' && len > 0; s++, len--)
1490Sstevel@tonic-gate 		yytext[yylast++] = *s;
1500Sstevel@tonic-gate 	yytext[yylast] = '\0';
1510Sstevel@tonic-gate }
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 
yytexttostr(offset,max)1540Sstevel@tonic-gate static char *yytexttostr(offset, max)
1550Sstevel@tonic-gate int offset, max;
1560Sstevel@tonic-gate {
1570Sstevel@tonic-gate 	char *str;
1580Sstevel@tonic-gate 	int i;
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	if ((yytext[offset] == '\'' || yytext[offset] == '"') &&
1610Sstevel@tonic-gate 	    (yytext[offset] == yytext[offset + max - 1])) {
1620Sstevel@tonic-gate 		offset++;
1630Sstevel@tonic-gate 		max--;
1640Sstevel@tonic-gate 	}
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 	if (max > yylast)
1670Sstevel@tonic-gate 		max = yylast;
1680Sstevel@tonic-gate 	str = malloc(max + 1);
1690Sstevel@tonic-gate 	if (str != NULL) {
1700Sstevel@tonic-gate 		for (i = offset; i < max; i++)
1710Sstevel@tonic-gate 			str[i - offset] = (char)(yytext[i] & 0xff);
1720Sstevel@tonic-gate 		str[i - offset] = '\0';
1730Sstevel@tonic-gate 	}
1740Sstevel@tonic-gate 	return str;
1750Sstevel@tonic-gate }
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 
yylex()1780Sstevel@tonic-gate int yylex()
1790Sstevel@tonic-gate {
1800Sstevel@tonic-gate 	int c, n, isbuilding, rval, lnext, nokey = 0;
1810Sstevel@tonic-gate 	char *name;
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	isbuilding = 0;
1840Sstevel@tonic-gate 	lnext = 0;
1850Sstevel@tonic-gate 	rval = 0;
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	if (yystr != NULL) {
1880Sstevel@tonic-gate 		free(yystr);
1890Sstevel@tonic-gate 		yystr = NULL;
1900Sstevel@tonic-gate 	}
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate nextchar:
1938624SDarren.Reed@Sun.COM 	c = yygetc(0);
1948624SDarren.Reed@Sun.COM 	if (yydebug > 1)
1958624SDarren.Reed@Sun.COM 		printf("yygetc = (%x) %c [%*.*s]\n", c, c, yypos, yypos,
1968624SDarren.Reed@Sun.COM 		       yytexttochar());
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 	switch (c)
1990Sstevel@tonic-gate 	{
2000Sstevel@tonic-gate 	case '\n' :
2018624SDarren.Reed@Sun.COM 		lnext = 0;
2028624SDarren.Reed@Sun.COM 		nokey = 0;
2030Sstevel@tonic-gate 	case '\t' :
2040Sstevel@tonic-gate 	case '\r' :
2050Sstevel@tonic-gate 	case ' ' :
2060Sstevel@tonic-gate 		if (isbuilding == 1) {
2070Sstevel@tonic-gate 			yyunputc(c);
2080Sstevel@tonic-gate 			goto done;
2090Sstevel@tonic-gate 		}
2100Sstevel@tonic-gate 		if (yylast > yypos) {
2110Sstevel@tonic-gate 			bcopy(yytext + yypos, yytext,
2120Sstevel@tonic-gate 			      sizeof(yytext[0]) * (yylast - yypos + 1));
2130Sstevel@tonic-gate 		}
2140Sstevel@tonic-gate 		yylast -= yypos;
2150Sstevel@tonic-gate 		yypos = 0;
2162393Syz155240 		lnext = 0;
2172393Syz155240 		nokey = 0;
2180Sstevel@tonic-gate 		goto nextchar;
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	case '\\' :
2210Sstevel@tonic-gate 		if (lnext == 0) {
2220Sstevel@tonic-gate 			lnext = 1;
2230Sstevel@tonic-gate 			if (yylast == yypos) {
2240Sstevel@tonic-gate 				yylast--;
2250Sstevel@tonic-gate 				yypos--;
2260Sstevel@tonic-gate 			} else
2270Sstevel@tonic-gate 				yypos--;
2280Sstevel@tonic-gate 			if (yypos == 0)
2290Sstevel@tonic-gate 				nokey = 1;
2300Sstevel@tonic-gate 			goto nextchar;
2310Sstevel@tonic-gate 		}
2320Sstevel@tonic-gate 		break;
2330Sstevel@tonic-gate 	}
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	if (lnext == 1) {
2360Sstevel@tonic-gate 		lnext = 0;
2372393Syz155240 		if ((isbuilding == 0) && !ISALNUM(c)) {
2382393Syz155240 			return c;
2392393Syz155240 		}
2400Sstevel@tonic-gate 		goto nextchar;
2410Sstevel@tonic-gate 	}
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 	switch (c)
2440Sstevel@tonic-gate 	{
2450Sstevel@tonic-gate 	case '#' :
2460Sstevel@tonic-gate 		if (isbuilding == 1) {
2470Sstevel@tonic-gate 			yyunputc(c);
2480Sstevel@tonic-gate 			goto done;
2490Sstevel@tonic-gate 		}
2500Sstevel@tonic-gate 		yyswallow('\n');
2510Sstevel@tonic-gate 		rval = YY_COMMENT;
252*8813SDarren.Reed@Sun.COM 		goto done;
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	case '$' :
2550Sstevel@tonic-gate 		if (isbuilding == 1) {
2560Sstevel@tonic-gate 			yyunputc(c);
2570Sstevel@tonic-gate 			goto done;
2580Sstevel@tonic-gate 		}
2598624SDarren.Reed@Sun.COM 		n = yygetc(0);
2600Sstevel@tonic-gate 		if (n == '{') {
2610Sstevel@tonic-gate 			if (yyswallow('}') == -1) {
2620Sstevel@tonic-gate 				rval = -2;
2630Sstevel@tonic-gate 				goto done;
2640Sstevel@tonic-gate 			}
2658624SDarren.Reed@Sun.COM 			(void) yygetc(0);
2660Sstevel@tonic-gate 		} else {
2672393Syz155240 			if (!ISALPHA(n)) {
2680Sstevel@tonic-gate 				yyunputc(n);
2690Sstevel@tonic-gate 				break;
2700Sstevel@tonic-gate 			}
2710Sstevel@tonic-gate 			do {
2728624SDarren.Reed@Sun.COM 				n = yygetc(1);
2732393Syz155240 			} while (ISALPHA(n) || ISDIGIT(n) || n == '_');
2740Sstevel@tonic-gate 			yyunputc(n);
2750Sstevel@tonic-gate 		}
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 		name = yytexttostr(1, yypos);		/* skip $ */
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 		if (name != NULL) {
2800Sstevel@tonic-gate 			string_val = get_variable(name, NULL, yylineNum);
2810Sstevel@tonic-gate 			free(name);
2820Sstevel@tonic-gate 			if (string_val != NULL) {
2830Sstevel@tonic-gate 				name = yytexttostr(yypos, yylast);
2840Sstevel@tonic-gate 				if (name != NULL) {
2850Sstevel@tonic-gate 					yypos = 0;
2860Sstevel@tonic-gate 					yylast = 0;
2870Sstevel@tonic-gate 					yystrtotext(string_val);
2880Sstevel@tonic-gate 					yystrtotext(name);
2890Sstevel@tonic-gate 					free(string_val);
2900Sstevel@tonic-gate 					free(name);
2910Sstevel@tonic-gate 					goto nextchar;
2920Sstevel@tonic-gate 				}
2930Sstevel@tonic-gate 				free(string_val);
2940Sstevel@tonic-gate 			}
2950Sstevel@tonic-gate 		}
2960Sstevel@tonic-gate 		break;
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	case '\'':
2990Sstevel@tonic-gate 	case '"' :
3000Sstevel@tonic-gate 		if (isbuilding == 1) {
3010Sstevel@tonic-gate 			goto done;
3020Sstevel@tonic-gate 		}
3030Sstevel@tonic-gate 		do {
3048624SDarren.Reed@Sun.COM 			n = yygetc(1);
3050Sstevel@tonic-gate 			if (n == EOF || n == TOOLONG) {
3060Sstevel@tonic-gate 				rval = -2;
3070Sstevel@tonic-gate 				goto done;
3080Sstevel@tonic-gate 			}
3090Sstevel@tonic-gate 			if (n == '\n') {
3100Sstevel@tonic-gate 				yyunputc(' ');
3110Sstevel@tonic-gate 				yypos++;
3120Sstevel@tonic-gate 			}
3130Sstevel@tonic-gate 		} while (n != c);
3148624SDarren.Reed@Sun.COM 		rval = YY_STR;
3158624SDarren.Reed@Sun.COM 		goto done;
3168624SDarren.Reed@Sun.COM 		/* NOTREACHED */
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 	case EOF :
3190Sstevel@tonic-gate 		yylineNum = 1;
3200Sstevel@tonic-gate 		yypos = 0;
3210Sstevel@tonic-gate 		yylast = -1;
3220Sstevel@tonic-gate 		yyexpectaddr = 0;
3230Sstevel@tonic-gate 		yybreakondot = 0;
3240Sstevel@tonic-gate 		yyvarnext = 0;
3250Sstevel@tonic-gate 		yytokentype = 0;
3260Sstevel@tonic-gate 		return 0;
3270Sstevel@tonic-gate 	}
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 	if (strchr("=,/;{}()@", c) != NULL) {
3300Sstevel@tonic-gate 		if (isbuilding == 1) {
3310Sstevel@tonic-gate 			yyunputc(c);
3320Sstevel@tonic-gate 			goto done;
3330Sstevel@tonic-gate 		}
3340Sstevel@tonic-gate 		rval = c;
3350Sstevel@tonic-gate 		goto done;
3360Sstevel@tonic-gate 	} else if (c == '.') {
3370Sstevel@tonic-gate 		if (isbuilding == 0) {
3380Sstevel@tonic-gate 			rval = c;
3390Sstevel@tonic-gate 			goto done;
3400Sstevel@tonic-gate 		}
3410Sstevel@tonic-gate 		if (yybreakondot != 0) {
3420Sstevel@tonic-gate 			yyunputc(c);
3430Sstevel@tonic-gate 			goto done;
3440Sstevel@tonic-gate 		}
3450Sstevel@tonic-gate 	}
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	switch (c)
3480Sstevel@tonic-gate 	{
3490Sstevel@tonic-gate 	case '-' :
3500Sstevel@tonic-gate 		if (yyexpectaddr)
3510Sstevel@tonic-gate 			break;
3520Sstevel@tonic-gate 		if (isbuilding == 1)
3530Sstevel@tonic-gate 			break;
3548624SDarren.Reed@Sun.COM 		n = yygetc(0);
3550Sstevel@tonic-gate 		if (n == '>') {
3560Sstevel@tonic-gate 			isbuilding = 1;
3570Sstevel@tonic-gate 			goto done;
3580Sstevel@tonic-gate 		}
3590Sstevel@tonic-gate 		yyunputc(n);
3600Sstevel@tonic-gate 		rval = '-';
3610Sstevel@tonic-gate 		goto done;
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 	case '!' :
3640Sstevel@tonic-gate 		if (isbuilding == 1) {
3650Sstevel@tonic-gate 			yyunputc(c);
3660Sstevel@tonic-gate 			goto done;
3670Sstevel@tonic-gate 		}
3688624SDarren.Reed@Sun.COM 		n = yygetc(0);
3690Sstevel@tonic-gate 		if (n == '=') {
3700Sstevel@tonic-gate 			rval = YY_CMP_NE;
3710Sstevel@tonic-gate 			goto done;
3720Sstevel@tonic-gate 		}
3730Sstevel@tonic-gate 		yyunputc(n);
3740Sstevel@tonic-gate 		rval = '!';
3750Sstevel@tonic-gate 		goto done;
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 	case '<' :
3780Sstevel@tonic-gate 		if (yyexpectaddr)
3790Sstevel@tonic-gate 			break;
3800Sstevel@tonic-gate 		if (isbuilding == 1) {
3810Sstevel@tonic-gate 			yyunputc(c);
3820Sstevel@tonic-gate 			goto done;
3830Sstevel@tonic-gate 		}
3848624SDarren.Reed@Sun.COM 		n = yygetc(0);
3850Sstevel@tonic-gate 		if (n == '=') {
3860Sstevel@tonic-gate 			rval = YY_CMP_LE;
3870Sstevel@tonic-gate 			goto done;
3880Sstevel@tonic-gate 		}
3890Sstevel@tonic-gate 		if (n == '>') {
3900Sstevel@tonic-gate 			rval = YY_RANGE_OUT;
3910Sstevel@tonic-gate 			goto done;
3920Sstevel@tonic-gate 		}
3930Sstevel@tonic-gate 		yyunputc(n);
3940Sstevel@tonic-gate 		rval = YY_CMP_LT;
3950Sstevel@tonic-gate 		goto done;
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	case '>' :
3980Sstevel@tonic-gate 		if (yyexpectaddr)
3990Sstevel@tonic-gate 			break;
4000Sstevel@tonic-gate 		if (isbuilding == 1) {
4010Sstevel@tonic-gate 			yyunputc(c);
4020Sstevel@tonic-gate 			goto done;
4030Sstevel@tonic-gate 		}
4048624SDarren.Reed@Sun.COM 		n = yygetc(0);
4050Sstevel@tonic-gate 		if (n == '=') {
4060Sstevel@tonic-gate 			rval = YY_CMP_GE;
4070Sstevel@tonic-gate 			goto done;
4080Sstevel@tonic-gate 		}
4090Sstevel@tonic-gate 		if (n == '<') {
4100Sstevel@tonic-gate 			rval = YY_RANGE_IN;
4110Sstevel@tonic-gate 			goto done;
4120Sstevel@tonic-gate 		}
4130Sstevel@tonic-gate 		yyunputc(n);
4140Sstevel@tonic-gate 		rval = YY_CMP_GT;
4150Sstevel@tonic-gate 		goto done;
4160Sstevel@tonic-gate 	}
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	/*
4190Sstevel@tonic-gate 	 * Now for the reason this is here...IPv6 address parsing.
4200Sstevel@tonic-gate 	 * The longest string we can expect is of this form:
4210Sstevel@tonic-gate 	 * 0000:0000:0000:0000:0000:0000:000.000.000.000
4220Sstevel@tonic-gate 	 * not:
4230Sstevel@tonic-gate 	 * 0000:0000:0000:0000:0000:0000:0000:0000
4240Sstevel@tonic-gate 	 */
4250Sstevel@tonic-gate #ifdef	USE_INET6
4267176Syx160601 	if (isbuilding == 0 && (ishex(c) || c == ':')) {
4270Sstevel@tonic-gate 		char ipv6buf[45 + 1], *s, oc;
4280Sstevel@tonic-gate 		int start;
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 		start = yypos;
4310Sstevel@tonic-gate 		s = ipv6buf;
4320Sstevel@tonic-gate 		oc = c;
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 		/*
4350Sstevel@tonic-gate 		 * Perhaps we should implement stricter controls on what we
4360Sstevel@tonic-gate 		 * swallow up here, but surely it would just be duplicating
4370Sstevel@tonic-gate 		 * the code in inet_pton() anyway.
4380Sstevel@tonic-gate 		 */
4390Sstevel@tonic-gate 		do {
4400Sstevel@tonic-gate 			*s++ = c;
4418624SDarren.Reed@Sun.COM 			c = yygetc(1);
4420Sstevel@tonic-gate 		} while ((ishex(c) || c == ':' || c == '.') &&
4430Sstevel@tonic-gate 			 (s - ipv6buf < 46));
4440Sstevel@tonic-gate 		yyunputc(c);
4450Sstevel@tonic-gate 		*s = '\0';
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate 		if (inet_pton(AF_INET6, ipv6buf, &yylval.ip6) == 1) {
4480Sstevel@tonic-gate 			rval = YY_IPV6;
4490Sstevel@tonic-gate 			yyexpectaddr = 0;
4500Sstevel@tonic-gate 			goto done;
4510Sstevel@tonic-gate 		}
4520Sstevel@tonic-gate 		yypos = start;
4530Sstevel@tonic-gate 		c = oc;
4540Sstevel@tonic-gate 	}
4550Sstevel@tonic-gate #endif
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 	if (c == ':') {
4580Sstevel@tonic-gate 		if (isbuilding == 1) {
4590Sstevel@tonic-gate 			yyunputc(c);
4600Sstevel@tonic-gate 			goto done;
4610Sstevel@tonic-gate 		}
4620Sstevel@tonic-gate 		rval = ':';
4630Sstevel@tonic-gate 		goto done;
4640Sstevel@tonic-gate 	}
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 	if (isbuilding == 0 && c == '0') {
4678624SDarren.Reed@Sun.COM 		n = yygetc(0);
4680Sstevel@tonic-gate 		if (n == 'x') {
4690Sstevel@tonic-gate 			do {
4708624SDarren.Reed@Sun.COM 				n = yygetc(1);
4710Sstevel@tonic-gate 			} while (ishex(n));
4720Sstevel@tonic-gate 			yyunputc(n);
4730Sstevel@tonic-gate 			rval = YY_HEX;
4740Sstevel@tonic-gate 			goto done;
4750Sstevel@tonic-gate 		}
4760Sstevel@tonic-gate 		yyunputc(n);
4770Sstevel@tonic-gate 	}
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 	/*
4800Sstevel@tonic-gate 	 * No negative numbers with leading - sign..
4810Sstevel@tonic-gate 	 */
4822393Syz155240 	if (isbuilding == 0 && ISDIGIT(c)) {
4830Sstevel@tonic-gate 		do {
4848624SDarren.Reed@Sun.COM 			n = yygetc(1);
4852393Syz155240 		} while (ISDIGIT(n));
4860Sstevel@tonic-gate 		yyunputc(n);
4870Sstevel@tonic-gate 		rval = YY_NUMBER;
4880Sstevel@tonic-gate 		goto done;
4890Sstevel@tonic-gate 	}
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 	isbuilding = 1;
4920Sstevel@tonic-gate 	goto nextchar;
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate done:
4950Sstevel@tonic-gate 	yystr = yytexttostr(0, yypos);
4960Sstevel@tonic-gate 
4978624SDarren.Reed@Sun.COM 	if (yydebug)
4988624SDarren.Reed@Sun.COM 		printf("isbuilding %d yyvarnext %d nokey %d\n",
4998624SDarren.Reed@Sun.COM 		       isbuilding, yyvarnext, nokey);
5000Sstevel@tonic-gate 	if (isbuilding == 1) {
5010Sstevel@tonic-gate 		wordtab_t *w;
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 		w = NULL;
5040Sstevel@tonic-gate 		isbuilding = 0;
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate 		if ((yyvarnext == 0) && (nokey == 0)) {
5070Sstevel@tonic-gate 			w = yyfindkey(yystr);
5080Sstevel@tonic-gate 			if (w == NULL && yywordtab != NULL) {
5090Sstevel@tonic-gate 				yyresetdict();
5100Sstevel@tonic-gate 				w = yyfindkey(yystr);
5110Sstevel@tonic-gate 			}
5120Sstevel@tonic-gate 		} else
5130Sstevel@tonic-gate 			yyvarnext = 0;
5140Sstevel@tonic-gate 		if (w != NULL)
5150Sstevel@tonic-gate 			rval = w->w_value;
5160Sstevel@tonic-gate 		else
5170Sstevel@tonic-gate 			rval = YY_STR;
5180Sstevel@tonic-gate 	}
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 	if (rval == YY_STR && yysavedepth > 0)
5210Sstevel@tonic-gate 		yyresetdict();
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 	yytokentype = rval;
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 	if (yydebug)
5268624SDarren.Reed@Sun.COM 		printf("lexed(%s) [%d,%d,%d] => %d @%d\n", yystr, string_start,
5278624SDarren.Reed@Sun.COM 			string_end, pos, rval, yysavedepth);
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 	switch (rval)
5300Sstevel@tonic-gate 	{
5310Sstevel@tonic-gate 	case YY_NUMBER :
5322393Syz155240 		sscanf(yystr, "%u", &yylval.num);
5330Sstevel@tonic-gate 		break;
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate 	case YY_HEX :
5360Sstevel@tonic-gate 		sscanf(yystr, "0x%x", (u_int *)&yylval.num);
5370Sstevel@tonic-gate 		break;
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 	case YY_STR :
5400Sstevel@tonic-gate 		yylval.str = strdup(yystr);
5410Sstevel@tonic-gate 		break;
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate 	default :
5440Sstevel@tonic-gate 		break;
5450Sstevel@tonic-gate 	}
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate 	if (yylast > 0) {
5480Sstevel@tonic-gate 		bcopy(yytext + yypos, yytext,
5490Sstevel@tonic-gate 		      sizeof(yytext[0]) * (yylast - yypos + 1));
5500Sstevel@tonic-gate 		yylast -= yypos;
5510Sstevel@tonic-gate 		yypos = 0;
5520Sstevel@tonic-gate 	}
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate 	return rval;
5550Sstevel@tonic-gate }
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate 
yyfindkey(key)5580Sstevel@tonic-gate static wordtab_t *yyfindkey(key)
5590Sstevel@tonic-gate char *key;
5600Sstevel@tonic-gate {
5610Sstevel@tonic-gate 	wordtab_t *w;
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate 	if (yywordtab == NULL)
5640Sstevel@tonic-gate 		return NULL;
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate 	for (w = yywordtab; w->w_word != 0; w++)
5670Sstevel@tonic-gate 		if (strcasecmp(key, w->w_word) == 0)
5680Sstevel@tonic-gate 			return w;
5690Sstevel@tonic-gate 	return NULL;
5700Sstevel@tonic-gate }
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 
yykeytostr(num)5730Sstevel@tonic-gate char *yykeytostr(num)
5740Sstevel@tonic-gate int num;
5750Sstevel@tonic-gate {
5760Sstevel@tonic-gate 	wordtab_t *w;
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate 	if (yywordtab == NULL)
5790Sstevel@tonic-gate 		return "<unknown>";
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate 	for (w = yywordtab; w->w_word; w++)
5820Sstevel@tonic-gate 		if (w->w_value == num)
5830Sstevel@tonic-gate 			return w->w_word;
5840Sstevel@tonic-gate 	return "<unknown>";
5850Sstevel@tonic-gate }
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 
yysettab(words)5880Sstevel@tonic-gate wordtab_t *yysettab(words)
5890Sstevel@tonic-gate wordtab_t *words;
5900Sstevel@tonic-gate {
5910Sstevel@tonic-gate 	wordtab_t *save;
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate 	save = yywordtab;
5940Sstevel@tonic-gate 	yywordtab = words;
5950Sstevel@tonic-gate 	return save;
5960Sstevel@tonic-gate }
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate 
yyerror(msg)5990Sstevel@tonic-gate void yyerror(msg)
6000Sstevel@tonic-gate char *msg;
6010Sstevel@tonic-gate {
6020Sstevel@tonic-gate 	char *txt, letter[2];
6030Sstevel@tonic-gate 	int freetxt = 0;
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 	if (yytokentype < 256) {
6060Sstevel@tonic-gate 		letter[0] = yytokentype;
6070Sstevel@tonic-gate 		letter[1] = '\0';
6080Sstevel@tonic-gate 		txt =  letter;
6090Sstevel@tonic-gate 	} else if (yytokentype == YY_STR || yytokentype == YY_HEX ||
6100Sstevel@tonic-gate 		   yytokentype == YY_NUMBER) {
6110Sstevel@tonic-gate 		if (yystr == NULL) {
6120Sstevel@tonic-gate 			txt = yytexttostr(yypos, YYBUFSIZ);
6131448Sschuster 			if (txt == NULL) {
6141448Sschuster 				fprintf(stderr, "sorry, out of memory,"
6151448Sschuster 					" bailing out\n");
6161448Sschuster 				exit(1);
6171448Sschuster 			}
6180Sstevel@tonic-gate 			freetxt = 1;
6190Sstevel@tonic-gate 		} else
6200Sstevel@tonic-gate 			txt = yystr;
6210Sstevel@tonic-gate 	} else {
6220Sstevel@tonic-gate 		txt = yykeytostr(yytokentype);
6231448Sschuster 		if (txt == NULL) {
6241448Sschuster 			fprintf(stderr, "sorry, out of memory,"
6251448Sschuster 				" bailing out\n");
6261448Sschuster 			exit(1);
6271448Sschuster 		}
6280Sstevel@tonic-gate 	}
6290Sstevel@tonic-gate 	fprintf(stderr, "%s error at \"%s\", line %d\n", msg, txt, yylineNum);
6300Sstevel@tonic-gate 	if (freetxt == 1)
6310Sstevel@tonic-gate 		free(txt);
6320Sstevel@tonic-gate 	exit(1);
6330Sstevel@tonic-gate }
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 
yysetdict(newdict)6360Sstevel@tonic-gate void yysetdict(newdict)
6370Sstevel@tonic-gate wordtab_t *newdict;
6380Sstevel@tonic-gate {
6390Sstevel@tonic-gate 	if (yysavedepth == sizeof(yysavewords)/sizeof(yysavewords[0])) {
6400Sstevel@tonic-gate 		fprintf(stderr, "%d: at maximum dictionary depth\n",
6410Sstevel@tonic-gate 			yylineNum);
6420Sstevel@tonic-gate 		return;
6430Sstevel@tonic-gate 	}
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 	yysavewords[yysavedepth++] = yysettab(newdict);
6460Sstevel@tonic-gate 	if (yydebug)
6470Sstevel@tonic-gate 		printf("yysavedepth++ => %d\n", yysavedepth);
6480Sstevel@tonic-gate }
6490Sstevel@tonic-gate 
yyresetdict()6500Sstevel@tonic-gate void yyresetdict()
6510Sstevel@tonic-gate {
6528624SDarren.Reed@Sun.COM 	if (yydebug)
6538624SDarren.Reed@Sun.COM 		printf("yyresetdict(%d)\n", yysavedepth);
6540Sstevel@tonic-gate 	if (yysavedepth > 0) {
6550Sstevel@tonic-gate 		yysettab(yysavewords[--yysavedepth]);
6560Sstevel@tonic-gate 		if (yydebug)
6570Sstevel@tonic-gate 			printf("yysavedepth-- => %d\n", yysavedepth);
6580Sstevel@tonic-gate 	}
6590Sstevel@tonic-gate }
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate 
6630Sstevel@tonic-gate #ifdef	TEST_LEXER
main(argc,argv)6640Sstevel@tonic-gate int main(argc, argv)
6650Sstevel@tonic-gate int argc;
6660Sstevel@tonic-gate char *argv[];
6670Sstevel@tonic-gate {
6680Sstevel@tonic-gate 	int n;
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate 	yyin = stdin;
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate 	while ((n = yylex()) != 0)
6730Sstevel@tonic-gate 		printf("%d.n = %d [%s] %d %d\n",
6740Sstevel@tonic-gate 			yylineNum, n, yystr, yypos, yylast);
6750Sstevel@tonic-gate }
6760Sstevel@tonic-gate #endif
677