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