xref: /illumos-gate/usr/src/cmd/ipf/tools/lexer.c (revision af5f29dd)
17c478bd9Sstevel@tonic-gate /*
2d6c23f6fSyx  * 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 
31ab25eeb5Syz #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(docont)6433f2fefdSDarren Reed static int yygetc(docont)
6533f2fefdSDarren Reed int docont;
667c478bd9Sstevel@tonic-gate {
677c478bd9Sstevel@tonic-gate 	int c;
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 	if (yypos < yylast) {
707c478bd9Sstevel@tonic-gate 		c = yytext[yypos++];
71ab25eeb5Syz 		if (c == '\n')
72ab25eeb5Syz 			yylineNum++;
737c478bd9Sstevel@tonic-gate 		return c;
747c478bd9Sstevel@tonic-gate 	}
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	if (yypos == YYBUFSIZ)
777c478bd9Sstevel@tonic-gate 		return TOOLONG;
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	if (pos >= string_start && pos <= string_end) {
807c478bd9Sstevel@tonic-gate 		c = string_val[pos - string_start];
817c478bd9Sstevel@tonic-gate 		yypos++;
827c478bd9Sstevel@tonic-gate 	} else {
837c478bd9Sstevel@tonic-gate 		c = fgetc(yyin);
8433f2fefdSDarren Reed 		if (docont && (c == '\\')) {
8533f2fefdSDarren Reed 			c = fgetc(yyin);
8633f2fefdSDarren Reed 			if (c == '\n') {
8733f2fefdSDarren Reed 				yylineNum++;
8833f2fefdSDarren Reed 				c = fgetc(yyin);
8933f2fefdSDarren Reed 			}
9033f2fefdSDarren Reed 		}
917c478bd9Sstevel@tonic-gate 	}
92ab25eeb5Syz 	if (c == '\n')
93ab25eeb5Syz 		yylineNum++;
947c478bd9Sstevel@tonic-gate 	yytext[yypos++] = c;
957c478bd9Sstevel@tonic-gate 	yylast = yypos;
967c478bd9Sstevel@tonic-gate 	yytext[yypos] = '\0';
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	return c;
997c478bd9Sstevel@tonic-gate }
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 
yyunputc(c)1027c478bd9Sstevel@tonic-gate static void yyunputc(c)
1037c478bd9Sstevel@tonic-gate int c;
1047c478bd9Sstevel@tonic-gate {
105ab25eeb5Syz 	if (c == '\n')
106ab25eeb5Syz 		yylineNum--;
1077c478bd9Sstevel@tonic-gate 	yytext[--yypos] = c;
1087c478bd9Sstevel@tonic-gate }
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 
yyswallow(last)1117c478bd9Sstevel@tonic-gate static int yyswallow(last)
1127c478bd9Sstevel@tonic-gate int last;
1137c478bd9Sstevel@tonic-gate {
1147c478bd9Sstevel@tonic-gate 	int c;
1157c478bd9Sstevel@tonic-gate 
11633f2fefdSDarren Reed 	while (((c = yygetc(0)) > '\0') && (c != last))
1177c478bd9Sstevel@tonic-gate 		;
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	if (c != EOF)
1207c478bd9Sstevel@tonic-gate 		yyunputc(c);
1217c478bd9Sstevel@tonic-gate 	if (c == last)
1227c478bd9Sstevel@tonic-gate 		return 0;
1237c478bd9Sstevel@tonic-gate 	return -1;
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 
yytexttochar()12733f2fefdSDarren Reed static char *yytexttochar()
12833f2fefdSDarren Reed {
12933f2fefdSDarren Reed 	int i;
13033f2fefdSDarren Reed 
13133f2fefdSDarren Reed 	for (i = 0; i < yypos; i++)
13233f2fefdSDarren Reed 		yychars[i] = (char)(yytext[i] & 0xff);
13333f2fefdSDarren Reed 	yychars[i] = '\0';
13433f2fefdSDarren Reed 	return yychars;
13533f2fefdSDarren Reed }
13633f2fefdSDarren Reed 
13733f2fefdSDarren Reed 
yystrtotext(str)1387c478bd9Sstevel@tonic-gate static void yystrtotext(str)
1397c478bd9Sstevel@tonic-gate char *str;
1407c478bd9Sstevel@tonic-gate {
1417c478bd9Sstevel@tonic-gate 	int len;
1427c478bd9Sstevel@tonic-gate 	char *s;
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	len = strlen(str);
1457c478bd9Sstevel@tonic-gate 	if (len > YYBUFSIZ)
1467c478bd9Sstevel@tonic-gate 		len = YYBUFSIZ;
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	for (s = str; *s != '\0' && len > 0; s++, len--)
1497c478bd9Sstevel@tonic-gate 		yytext[yylast++] = *s;
1507c478bd9Sstevel@tonic-gate 	yytext[yylast] = '\0';
1517c478bd9Sstevel@tonic-gate }
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 
yytexttostr(offset,max)1547c478bd9Sstevel@tonic-gate static char *yytexttostr(offset, max)
1557c478bd9Sstevel@tonic-gate int offset, max;
1567c478bd9Sstevel@tonic-gate {
1577c478bd9Sstevel@tonic-gate 	char *str;
1587c478bd9Sstevel@tonic-gate 	int i;
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	if ((yytext[offset] == '\'' || yytext[offset] == '"') &&
1617c478bd9Sstevel@tonic-gate 	    (yytext[offset] == yytext[offset + max - 1])) {
1627c478bd9Sstevel@tonic-gate 		offset++;
1637c478bd9Sstevel@tonic-gate 		max--;
1647c478bd9Sstevel@tonic-gate 	}
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	if (max > yylast)
1677c478bd9Sstevel@tonic-gate 		max = yylast;
1687c478bd9Sstevel@tonic-gate 	str = malloc(max + 1);
1697c478bd9Sstevel@tonic-gate 	if (str != NULL) {
1707c478bd9Sstevel@tonic-gate 		for (i = offset; i < max; i++)
1717c478bd9Sstevel@tonic-gate 			str[i - offset] = (char)(yytext[i] & 0xff);
1727c478bd9Sstevel@tonic-gate 		str[i - offset] = '\0';
1737c478bd9Sstevel@tonic-gate 	}
1747c478bd9Sstevel@tonic-gate 	return str;
1757c478bd9Sstevel@tonic-gate }
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 
yylex()1787c478bd9Sstevel@tonic-gate int yylex()
1797c478bd9Sstevel@tonic-gate {
1807c478bd9Sstevel@tonic-gate 	int c, n, isbuilding, rval, lnext, nokey = 0;
1817c478bd9Sstevel@tonic-gate 	char *name;
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	isbuilding = 0;
1847c478bd9Sstevel@tonic-gate 	lnext = 0;
1857c478bd9Sstevel@tonic-gate 	rval = 0;
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	if (yystr != NULL) {
1887c478bd9Sstevel@tonic-gate 		free(yystr);
1897c478bd9Sstevel@tonic-gate 		yystr = NULL;
1907c478bd9Sstevel@tonic-gate 	}
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate nextchar:
19333f2fefdSDarren Reed 	c = yygetc(0);
19433f2fefdSDarren Reed 	if (yydebug > 1)
19533f2fefdSDarren Reed 		printf("yygetc = (%x) %c [%*.*s]\n", c, c, yypos, yypos,
19633f2fefdSDarren Reed 		       yytexttochar());
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	switch (c)
1997c478bd9Sstevel@tonic-gate 	{
2007c478bd9Sstevel@tonic-gate 	case '\n' :
20133f2fefdSDarren Reed 		lnext = 0;
20233f2fefdSDarren Reed 		nokey = 0;
203*af5f29ddSToomas Soome 		/* FALLTHROUGH */
2047c478bd9Sstevel@tonic-gate 	case '\t' :
2057c478bd9Sstevel@tonic-gate 	case '\r' :
2067c478bd9Sstevel@tonic-gate 	case ' ' :
2077c478bd9Sstevel@tonic-gate 		if (isbuilding == 1) {
2087c478bd9Sstevel@tonic-gate 			yyunputc(c);
2097c478bd9Sstevel@tonic-gate 			goto done;
2107c478bd9Sstevel@tonic-gate 		}
2117c478bd9Sstevel@tonic-gate 		if (yylast > yypos) {
2127c478bd9Sstevel@tonic-gate 			bcopy(yytext + yypos, yytext,
2137c478bd9Sstevel@tonic-gate 			      sizeof(yytext[0]) * (yylast - yypos + 1));
2147c478bd9Sstevel@tonic-gate 		}
2157c478bd9Sstevel@tonic-gate 		yylast -= yypos;
2167c478bd9Sstevel@tonic-gate 		yypos = 0;
217ab25eeb5Syz 		lnext = 0;
218ab25eeb5Syz 		nokey = 0;
2197c478bd9Sstevel@tonic-gate 		goto nextchar;
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	case '\\' :
2227c478bd9Sstevel@tonic-gate 		if (lnext == 0) {
2237c478bd9Sstevel@tonic-gate 			lnext = 1;
2247c478bd9Sstevel@tonic-gate 			if (yylast == yypos) {
2257c478bd9Sstevel@tonic-gate 				yylast--;
2267c478bd9Sstevel@tonic-gate 				yypos--;
2277c478bd9Sstevel@tonic-gate 			} else
2287c478bd9Sstevel@tonic-gate 				yypos--;
2297c478bd9Sstevel@tonic-gate 			if (yypos == 0)
2307c478bd9Sstevel@tonic-gate 				nokey = 1;
2317c478bd9Sstevel@tonic-gate 			goto nextchar;
2327c478bd9Sstevel@tonic-gate 		}
2337c478bd9Sstevel@tonic-gate 		break;
2347c478bd9Sstevel@tonic-gate 	}
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	if (lnext == 1) {
2377c478bd9Sstevel@tonic-gate 		lnext = 0;
238ab25eeb5Syz 		if ((isbuilding == 0) && !ISALNUM(c)) {
239ab25eeb5Syz 			return c;
240ab25eeb5Syz 		}
2417c478bd9Sstevel@tonic-gate 		goto nextchar;
2427c478bd9Sstevel@tonic-gate 	}
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	switch (c)
2457c478bd9Sstevel@tonic-gate 	{
2467c478bd9Sstevel@tonic-gate 	case '#' :
2477c478bd9Sstevel@tonic-gate 		if (isbuilding == 1) {
2487c478bd9Sstevel@tonic-gate 			yyunputc(c);
2497c478bd9Sstevel@tonic-gate 			goto done;
2507c478bd9Sstevel@tonic-gate 		}
2517c478bd9Sstevel@tonic-gate 		yyswallow('\n');
2527c478bd9Sstevel@tonic-gate 		rval = YY_COMMENT;
25322929378SDarren Reed 		goto done;
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	case '$' :
2567c478bd9Sstevel@tonic-gate 		if (isbuilding == 1) {
2577c478bd9Sstevel@tonic-gate 			yyunputc(c);
2587c478bd9Sstevel@tonic-gate 			goto done;
2597c478bd9Sstevel@tonic-gate 		}
26033f2fefdSDarren Reed 		n = yygetc(0);
2617c478bd9Sstevel@tonic-gate 		if (n == '{') {
2627c478bd9Sstevel@tonic-gate 			if (yyswallow('}') == -1) {
2637c478bd9Sstevel@tonic-gate 				rval = -2;
2647c478bd9Sstevel@tonic-gate 				goto done;
2657c478bd9Sstevel@tonic-gate 			}
26633f2fefdSDarren Reed 			(void) yygetc(0);
2677c478bd9Sstevel@tonic-gate 		} else {
268ab25eeb5Syz 			if (!ISALPHA(n)) {
2697c478bd9Sstevel@tonic-gate 				yyunputc(n);
2707c478bd9Sstevel@tonic-gate 				break;
2717c478bd9Sstevel@tonic-gate 			}
2727c478bd9Sstevel@tonic-gate 			do {
27333f2fefdSDarren Reed 				n = yygetc(1);
274ab25eeb5Syz 			} while (ISALPHA(n) || ISDIGIT(n) || n == '_');
2757c478bd9Sstevel@tonic-gate 			yyunputc(n);
2767c478bd9Sstevel@tonic-gate 		}
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 		name = yytexttostr(1, yypos);		/* skip $ */
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 		if (name != NULL) {
2817c478bd9Sstevel@tonic-gate 			string_val = get_variable(name, NULL, yylineNum);
2827c478bd9Sstevel@tonic-gate 			free(name);
2837c478bd9Sstevel@tonic-gate 			if (string_val != NULL) {
2847c478bd9Sstevel@tonic-gate 				name = yytexttostr(yypos, yylast);
2857c478bd9Sstevel@tonic-gate 				if (name != NULL) {
2867c478bd9Sstevel@tonic-gate 					yypos = 0;
2877c478bd9Sstevel@tonic-gate 					yylast = 0;
2887c478bd9Sstevel@tonic-gate 					yystrtotext(string_val);
2897c478bd9Sstevel@tonic-gate 					yystrtotext(name);
2907c478bd9Sstevel@tonic-gate 					free(string_val);
2917c478bd9Sstevel@tonic-gate 					free(name);
2927c478bd9Sstevel@tonic-gate 					goto nextchar;
2937c478bd9Sstevel@tonic-gate 				}
2947c478bd9Sstevel@tonic-gate 				free(string_val);
2957c478bd9Sstevel@tonic-gate 			}
2967c478bd9Sstevel@tonic-gate 		}
2977c478bd9Sstevel@tonic-gate 		break;
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	case '\'':
3007c478bd9Sstevel@tonic-gate 	case '"' :
3017c478bd9Sstevel@tonic-gate 		if (isbuilding == 1) {
3027c478bd9Sstevel@tonic-gate 			goto done;
3037c478bd9Sstevel@tonic-gate 		}
3047c478bd9Sstevel@tonic-gate 		do {
30533f2fefdSDarren Reed 			n = yygetc(1);
3067c478bd9Sstevel@tonic-gate 			if (n == EOF || n == TOOLONG) {
3077c478bd9Sstevel@tonic-gate 				rval = -2;
3087c478bd9Sstevel@tonic-gate 				goto done;
3097c478bd9Sstevel@tonic-gate 			}
3107c478bd9Sstevel@tonic-gate 			if (n == '\n') {
3117c478bd9Sstevel@tonic-gate 				yyunputc(' ');
3127c478bd9Sstevel@tonic-gate 				yypos++;
3137c478bd9Sstevel@tonic-gate 			}
3147c478bd9Sstevel@tonic-gate 		} while (n != c);
31533f2fefdSDarren Reed 		rval = YY_STR;
31633f2fefdSDarren Reed 		goto done;
31733f2fefdSDarren Reed 		/* NOTREACHED */
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	case EOF :
3207c478bd9Sstevel@tonic-gate 		yylineNum = 1;
3217c478bd9Sstevel@tonic-gate 		yypos = 0;
3227c478bd9Sstevel@tonic-gate 		yylast = -1;
3237c478bd9Sstevel@tonic-gate 		yyexpectaddr = 0;
3247c478bd9Sstevel@tonic-gate 		yybreakondot = 0;
3257c478bd9Sstevel@tonic-gate 		yyvarnext = 0;
3267c478bd9Sstevel@tonic-gate 		yytokentype = 0;
3277c478bd9Sstevel@tonic-gate 		return 0;
3287c478bd9Sstevel@tonic-gate 	}
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	if (strchr("=,/;{}()@", c) != NULL) {
3317c478bd9Sstevel@tonic-gate 		if (isbuilding == 1) {
3327c478bd9Sstevel@tonic-gate 			yyunputc(c);
3337c478bd9Sstevel@tonic-gate 			goto done;
3347c478bd9Sstevel@tonic-gate 		}
3357c478bd9Sstevel@tonic-gate 		rval = c;
3367c478bd9Sstevel@tonic-gate 		goto done;
3377c478bd9Sstevel@tonic-gate 	} else if (c == '.') {
3387c478bd9Sstevel@tonic-gate 		if (isbuilding == 0) {
3397c478bd9Sstevel@tonic-gate 			rval = c;
3407c478bd9Sstevel@tonic-gate 			goto done;
3417c478bd9Sstevel@tonic-gate 		}
3427c478bd9Sstevel@tonic-gate 		if (yybreakondot != 0) {
3437c478bd9Sstevel@tonic-gate 			yyunputc(c);
3447c478bd9Sstevel@tonic-gate 			goto done;
3457c478bd9Sstevel@tonic-gate 		}
3467c478bd9Sstevel@tonic-gate 	}
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	switch (c)
3497c478bd9Sstevel@tonic-gate 	{
3507c478bd9Sstevel@tonic-gate 	case '-' :
3517c478bd9Sstevel@tonic-gate 		if (yyexpectaddr)
3527c478bd9Sstevel@tonic-gate 			break;
3537c478bd9Sstevel@tonic-gate 		if (isbuilding == 1)
3547c478bd9Sstevel@tonic-gate 			break;
35533f2fefdSDarren Reed 		n = yygetc(0);
3567c478bd9Sstevel@tonic-gate 		if (n == '>') {
3577c478bd9Sstevel@tonic-gate 			isbuilding = 1;
3587c478bd9Sstevel@tonic-gate 			goto done;
3597c478bd9Sstevel@tonic-gate 		}
3607c478bd9Sstevel@tonic-gate 		yyunputc(n);
3617c478bd9Sstevel@tonic-gate 		rval = '-';
3627c478bd9Sstevel@tonic-gate 		goto done;
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	case '!' :
3657c478bd9Sstevel@tonic-gate 		if (isbuilding == 1) {
3667c478bd9Sstevel@tonic-gate 			yyunputc(c);
3677c478bd9Sstevel@tonic-gate 			goto done;
3687c478bd9Sstevel@tonic-gate 		}
36933f2fefdSDarren Reed 		n = yygetc(0);
3707c478bd9Sstevel@tonic-gate 		if (n == '=') {
3717c478bd9Sstevel@tonic-gate 			rval = YY_CMP_NE;
3727c478bd9Sstevel@tonic-gate 			goto done;
3737c478bd9Sstevel@tonic-gate 		}
3747c478bd9Sstevel@tonic-gate 		yyunputc(n);
3757c478bd9Sstevel@tonic-gate 		rval = '!';
3767c478bd9Sstevel@tonic-gate 		goto done;
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	case '<' :
3797c478bd9Sstevel@tonic-gate 		if (yyexpectaddr)
3807c478bd9Sstevel@tonic-gate 			break;
3817c478bd9Sstevel@tonic-gate 		if (isbuilding == 1) {
3827c478bd9Sstevel@tonic-gate 			yyunputc(c);
3837c478bd9Sstevel@tonic-gate 			goto done;
3847c478bd9Sstevel@tonic-gate 		}
38533f2fefdSDarren Reed 		n = yygetc(0);
3867c478bd9Sstevel@tonic-gate 		if (n == '=') {
3877c478bd9Sstevel@tonic-gate 			rval = YY_CMP_LE;
3887c478bd9Sstevel@tonic-gate 			goto done;
3897c478bd9Sstevel@tonic-gate 		}
3907c478bd9Sstevel@tonic-gate 		if (n == '>') {
3917c478bd9Sstevel@tonic-gate 			rval = YY_RANGE_OUT;
3927c478bd9Sstevel@tonic-gate 			goto done;
3937c478bd9Sstevel@tonic-gate 		}
3947c478bd9Sstevel@tonic-gate 		yyunputc(n);
3957c478bd9Sstevel@tonic-gate 		rval = YY_CMP_LT;
3967c478bd9Sstevel@tonic-gate 		goto done;
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	case '>' :
3997c478bd9Sstevel@tonic-gate 		if (yyexpectaddr)
4007c478bd9Sstevel@tonic-gate 			break;
4017c478bd9Sstevel@tonic-gate 		if (isbuilding == 1) {
4027c478bd9Sstevel@tonic-gate 			yyunputc(c);
4037c478bd9Sstevel@tonic-gate 			goto done;
4047c478bd9Sstevel@tonic-gate 		}
40533f2fefdSDarren Reed 		n = yygetc(0);
4067c478bd9Sstevel@tonic-gate 		if (n == '=') {
4077c478bd9Sstevel@tonic-gate 			rval = YY_CMP_GE;
4087c478bd9Sstevel@tonic-gate 			goto done;
4097c478bd9Sstevel@tonic-gate 		}
4107c478bd9Sstevel@tonic-gate 		if (n == '<') {
4117c478bd9Sstevel@tonic-gate 			rval = YY_RANGE_IN;
4127c478bd9Sstevel@tonic-gate 			goto done;
4137c478bd9Sstevel@tonic-gate 		}
4147c478bd9Sstevel@tonic-gate 		yyunputc(n);
4157c478bd9Sstevel@tonic-gate 		rval = YY_CMP_GT;
4167c478bd9Sstevel@tonic-gate 		goto done;
4177c478bd9Sstevel@tonic-gate 	}
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 	/*
4207c478bd9Sstevel@tonic-gate 	 * Now for the reason this is here...IPv6 address parsing.
4217c478bd9Sstevel@tonic-gate 	 * The longest string we can expect is of this form:
4227c478bd9Sstevel@tonic-gate 	 * 0000:0000:0000:0000:0000:0000:000.000.000.000
4237c478bd9Sstevel@tonic-gate 	 * not:
4247c478bd9Sstevel@tonic-gate 	 * 0000:0000:0000:0000:0000:0000:0000:0000
4257c478bd9Sstevel@tonic-gate 	 */
4267c478bd9Sstevel@tonic-gate #ifdef	USE_INET6
427d6c23f6fSyx 	if (isbuilding == 0 && (ishex(c) || c == ':')) {
4287c478bd9Sstevel@tonic-gate 		char ipv6buf[45 + 1], *s, oc;
4297c478bd9Sstevel@tonic-gate 		int start;
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 		start = yypos;
4327c478bd9Sstevel@tonic-gate 		s = ipv6buf;
4337c478bd9Sstevel@tonic-gate 		oc = c;
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 		/*
4367c478bd9Sstevel@tonic-gate 		 * Perhaps we should implement stricter controls on what we
4377c478bd9Sstevel@tonic-gate 		 * swallow up here, but surely it would just be duplicating
4387c478bd9Sstevel@tonic-gate 		 * the code in inet_pton() anyway.
4397c478bd9Sstevel@tonic-gate 		 */
4407c478bd9Sstevel@tonic-gate 		do {
4417c478bd9Sstevel@tonic-gate 			*s++ = c;
44233f2fefdSDarren Reed 			c = yygetc(1);
4437c478bd9Sstevel@tonic-gate 		} while ((ishex(c) || c == ':' || c == '.') &&
4447c478bd9Sstevel@tonic-gate 			 (s - ipv6buf < 46));
4457c478bd9Sstevel@tonic-gate 		yyunputc(c);
4467c478bd9Sstevel@tonic-gate 		*s = '\0';
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 		if (inet_pton(AF_INET6, ipv6buf, &yylval.ip6) == 1) {
4497c478bd9Sstevel@tonic-gate 			rval = YY_IPV6;
4507c478bd9Sstevel@tonic-gate 			yyexpectaddr = 0;
4517c478bd9Sstevel@tonic-gate 			goto done;
4527c478bd9Sstevel@tonic-gate 		}
4537c478bd9Sstevel@tonic-gate 		yypos = start;
4547c478bd9Sstevel@tonic-gate 		c = oc;
4557c478bd9Sstevel@tonic-gate 	}
4567c478bd9Sstevel@tonic-gate #endif
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	if (c == ':') {
4597c478bd9Sstevel@tonic-gate 		if (isbuilding == 1) {
4607c478bd9Sstevel@tonic-gate 			yyunputc(c);
4617c478bd9Sstevel@tonic-gate 			goto done;
4627c478bd9Sstevel@tonic-gate 		}
4637c478bd9Sstevel@tonic-gate 		rval = ':';
4647c478bd9Sstevel@tonic-gate 		goto done;
4657c478bd9Sstevel@tonic-gate 	}
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	if (isbuilding == 0 && c == '0') {
46833f2fefdSDarren Reed 		n = yygetc(0);
4697c478bd9Sstevel@tonic-gate 		if (n == 'x') {
4707c478bd9Sstevel@tonic-gate 			do {
47133f2fefdSDarren Reed 				n = yygetc(1);
4727c478bd9Sstevel@tonic-gate 			} while (ishex(n));
4737c478bd9Sstevel@tonic-gate 			yyunputc(n);
4747c478bd9Sstevel@tonic-gate 			rval = YY_HEX;
4757c478bd9Sstevel@tonic-gate 			goto done;
4767c478bd9Sstevel@tonic-gate 		}
4777c478bd9Sstevel@tonic-gate 		yyunputc(n);
4787c478bd9Sstevel@tonic-gate 	}
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	/*
4817c478bd9Sstevel@tonic-gate 	 * No negative numbers with leading - sign..
4827c478bd9Sstevel@tonic-gate 	 */
483ab25eeb5Syz 	if (isbuilding == 0 && ISDIGIT(c)) {
4847c478bd9Sstevel@tonic-gate 		do {
48533f2fefdSDarren Reed 			n = yygetc(1);
486ab25eeb5Syz 		} while (ISDIGIT(n));
4877c478bd9Sstevel@tonic-gate 		yyunputc(n);
4887c478bd9Sstevel@tonic-gate 		rval = YY_NUMBER;
4897c478bd9Sstevel@tonic-gate 		goto done;
4907c478bd9Sstevel@tonic-gate 	}
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 	isbuilding = 1;
4937c478bd9Sstevel@tonic-gate 	goto nextchar;
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate done:
4967c478bd9Sstevel@tonic-gate 	yystr = yytexttostr(0, yypos);
4977c478bd9Sstevel@tonic-gate 
49833f2fefdSDarren Reed 	if (yydebug)
49933f2fefdSDarren Reed 		printf("isbuilding %d yyvarnext %d nokey %d\n",
50033f2fefdSDarren Reed 		       isbuilding, yyvarnext, nokey);
5017c478bd9Sstevel@tonic-gate 	if (isbuilding == 1) {
5027c478bd9Sstevel@tonic-gate 		wordtab_t *w;
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 		w = NULL;
5057c478bd9Sstevel@tonic-gate 		isbuilding = 0;
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 		if ((yyvarnext == 0) && (nokey == 0)) {
5087c478bd9Sstevel@tonic-gate 			w = yyfindkey(yystr);
5097c478bd9Sstevel@tonic-gate 			if (w == NULL && yywordtab != NULL) {
5107c478bd9Sstevel@tonic-gate 				yyresetdict();
5117c478bd9Sstevel@tonic-gate 				w = yyfindkey(yystr);
5127c478bd9Sstevel@tonic-gate 			}
5137c478bd9Sstevel@tonic-gate 		} else
5147c478bd9Sstevel@tonic-gate 			yyvarnext = 0;
5157c478bd9Sstevel@tonic-gate 		if (w != NULL)
5167c478bd9Sstevel@tonic-gate 			rval = w->w_value;
5177c478bd9Sstevel@tonic-gate 		else
5187c478bd9Sstevel@tonic-gate 			rval = YY_STR;
5197c478bd9Sstevel@tonic-gate 	}
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	if (rval == YY_STR && yysavedepth > 0)
5227c478bd9Sstevel@tonic-gate 		yyresetdict();
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 	yytokentype = rval;
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 	if (yydebug)
52733f2fefdSDarren Reed 		printf("lexed(%s) [%d,%d,%d] => %d @%d\n", yystr, string_start,
52833f2fefdSDarren Reed 			string_end, pos, rval, yysavedepth);
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 	switch (rval)
5317c478bd9Sstevel@tonic-gate 	{
5327c478bd9Sstevel@tonic-gate 	case YY_NUMBER :
533ab25eeb5Syz 		sscanf(yystr, "%u", &yylval.num);
5347c478bd9Sstevel@tonic-gate 		break;
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	case YY_HEX :
5377c478bd9Sstevel@tonic-gate 		sscanf(yystr, "0x%x", (u_int *)&yylval.num);
5387c478bd9Sstevel@tonic-gate 		break;
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 	case YY_STR :
5417c478bd9Sstevel@tonic-gate 		yylval.str = strdup(yystr);
5427c478bd9Sstevel@tonic-gate 		break;
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	default :
5457c478bd9Sstevel@tonic-gate 		break;
5467c478bd9Sstevel@tonic-gate 	}
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 	if (yylast > 0) {
5497c478bd9Sstevel@tonic-gate 		bcopy(yytext + yypos, yytext,
5507c478bd9Sstevel@tonic-gate 		      sizeof(yytext[0]) * (yylast - yypos + 1));
5517c478bd9Sstevel@tonic-gate 		yylast -= yypos;
5527c478bd9Sstevel@tonic-gate 		yypos = 0;
5537c478bd9Sstevel@tonic-gate 	}
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 	return rval;
5567c478bd9Sstevel@tonic-gate }
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 
yyfindkey(key)5597c478bd9Sstevel@tonic-gate static wordtab_t *yyfindkey(key)
5607c478bd9Sstevel@tonic-gate char *key;
5617c478bd9Sstevel@tonic-gate {
5627c478bd9Sstevel@tonic-gate 	wordtab_t *w;
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 	if (yywordtab == NULL)
5657c478bd9Sstevel@tonic-gate 		return NULL;
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 	for (w = yywordtab; w->w_word != 0; w++)
5687c478bd9Sstevel@tonic-gate 		if (strcasecmp(key, w->w_word) == 0)
5697c478bd9Sstevel@tonic-gate 			return w;
5707c478bd9Sstevel@tonic-gate 	return NULL;
5717c478bd9Sstevel@tonic-gate }
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 
yykeytostr(num)5747c478bd9Sstevel@tonic-gate char *yykeytostr(num)
5757c478bd9Sstevel@tonic-gate int num;
5767c478bd9Sstevel@tonic-gate {
5777c478bd9Sstevel@tonic-gate 	wordtab_t *w;
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 	if (yywordtab == NULL)
5807c478bd9Sstevel@tonic-gate 		return "<unknown>";
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 	for (w = yywordtab; w->w_word; w++)
5837c478bd9Sstevel@tonic-gate 		if (w->w_value == num)
5847c478bd9Sstevel@tonic-gate 			return w->w_word;
5857c478bd9Sstevel@tonic-gate 	return "<unknown>";
5867c478bd9Sstevel@tonic-gate }
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 
yysettab(words)5897c478bd9Sstevel@tonic-gate wordtab_t *yysettab(words)
5907c478bd9Sstevel@tonic-gate wordtab_t *words;
5917c478bd9Sstevel@tonic-gate {
5927c478bd9Sstevel@tonic-gate 	wordtab_t *save;
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 	save = yywordtab;
5957c478bd9Sstevel@tonic-gate 	yywordtab = words;
5967c478bd9Sstevel@tonic-gate 	return save;
5977c478bd9Sstevel@tonic-gate }
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 
yyerror(msg)6007c478bd9Sstevel@tonic-gate void yyerror(msg)
6017c478bd9Sstevel@tonic-gate char *msg;
6027c478bd9Sstevel@tonic-gate {
6037c478bd9Sstevel@tonic-gate 	char *txt, letter[2];
6047c478bd9Sstevel@tonic-gate 	int freetxt = 0;
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 	if (yytokentype < 256) {
6077c478bd9Sstevel@tonic-gate 		letter[0] = yytokentype;
6087c478bd9Sstevel@tonic-gate 		letter[1] = '\0';
6097c478bd9Sstevel@tonic-gate 		txt =  letter;
6107c478bd9Sstevel@tonic-gate 	} else if (yytokentype == YY_STR || yytokentype == YY_HEX ||
6117c478bd9Sstevel@tonic-gate 		   yytokentype == YY_NUMBER) {
6127c478bd9Sstevel@tonic-gate 		if (yystr == NULL) {
6137c478bd9Sstevel@tonic-gate 			txt = yytexttostr(yypos, YYBUFSIZ);
6145e985db5Sschuster 			if (txt == NULL) {
6155e985db5Sschuster 				fprintf(stderr, "sorry, out of memory,"
6165e985db5Sschuster 					" bailing out\n");
6175e985db5Sschuster 				exit(1);
6185e985db5Sschuster 			}
6197c478bd9Sstevel@tonic-gate 			freetxt = 1;
6207c478bd9Sstevel@tonic-gate 		} else
6217c478bd9Sstevel@tonic-gate 			txt = yystr;
6227c478bd9Sstevel@tonic-gate 	} else {
6237c478bd9Sstevel@tonic-gate 		txt = yykeytostr(yytokentype);
6245e985db5Sschuster 		if (txt == NULL) {
6255e985db5Sschuster 			fprintf(stderr, "sorry, out of memory,"
6265e985db5Sschuster 				" bailing out\n");
6275e985db5Sschuster 			exit(1);
6285e985db5Sschuster 		}
6297c478bd9Sstevel@tonic-gate 	}
6307c478bd9Sstevel@tonic-gate 	fprintf(stderr, "%s error at \"%s\", line %d\n", msg, txt, yylineNum);
6317c478bd9Sstevel@tonic-gate 	if (freetxt == 1)
6327c478bd9Sstevel@tonic-gate 		free(txt);
6337c478bd9Sstevel@tonic-gate 	exit(1);
6347c478bd9Sstevel@tonic-gate }
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 
yysetdict(newdict)6377c478bd9Sstevel@tonic-gate void yysetdict(newdict)
6387c478bd9Sstevel@tonic-gate wordtab_t *newdict;
6397c478bd9Sstevel@tonic-gate {
6407c478bd9Sstevel@tonic-gate 	if (yysavedepth == sizeof(yysavewords)/sizeof(yysavewords[0])) {
6417c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%d: at maximum dictionary depth\n",
6427c478bd9Sstevel@tonic-gate 			yylineNum);
6437c478bd9Sstevel@tonic-gate 		return;
6447c478bd9Sstevel@tonic-gate 	}
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 	yysavewords[yysavedepth++] = yysettab(newdict);
6477c478bd9Sstevel@tonic-gate 	if (yydebug)
6487c478bd9Sstevel@tonic-gate 		printf("yysavedepth++ => %d\n", yysavedepth);
6497c478bd9Sstevel@tonic-gate }
6507c478bd9Sstevel@tonic-gate 
yyresetdict()6517c478bd9Sstevel@tonic-gate void yyresetdict()
6527c478bd9Sstevel@tonic-gate {
65333f2fefdSDarren Reed 	if (yydebug)
65433f2fefdSDarren Reed 		printf("yyresetdict(%d)\n", yysavedepth);
6557c478bd9Sstevel@tonic-gate 	if (yysavedepth > 0) {
6567c478bd9Sstevel@tonic-gate 		yysettab(yysavewords[--yysavedepth]);
6577c478bd9Sstevel@tonic-gate 		if (yydebug)
6587c478bd9Sstevel@tonic-gate 			printf("yysavedepth-- => %d\n", yysavedepth);
6597c478bd9Sstevel@tonic-gate 	}
6607c478bd9Sstevel@tonic-gate }
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate #ifdef	TEST_LEXER
main(argc,argv)6657c478bd9Sstevel@tonic-gate int main(argc, argv)
6667c478bd9Sstevel@tonic-gate int argc;
6677c478bd9Sstevel@tonic-gate char *argv[];
6687c478bd9Sstevel@tonic-gate {
6697c478bd9Sstevel@tonic-gate 	int n;
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 	yyin = stdin;
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 	while ((n = yylex()) != 0)
6747c478bd9Sstevel@tonic-gate 		printf("%d.n = %d [%s] %d %d\n",
6757c478bd9Sstevel@tonic-gate 			yylineNum, n, yystr, yypos, yylast);
6767c478bd9Sstevel@tonic-gate }
6777c478bd9Sstevel@tonic-gate #endif
678