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