xref: /titanic_52/usr/src/cmd/ipf/tools/lexer.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright (C) 2004 by Darren Reed.
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * See the IPFILTER.LICENCE file for details on licencing.
5*7c478bd9Sstevel@tonic-gate  *
6*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
7*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
8*7c478bd9Sstevel@tonic-gate  */
9*7c478bd9Sstevel@tonic-gate 
10*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
11*7c478bd9Sstevel@tonic-gate 
12*7c478bd9Sstevel@tonic-gate #include <ctype.h>
13*7c478bd9Sstevel@tonic-gate #include "ipf.h"
14*7c478bd9Sstevel@tonic-gate #ifdef	IPFILTER_SCAN
15*7c478bd9Sstevel@tonic-gate # include "netinet/ip_scan.h"
16*7c478bd9Sstevel@tonic-gate #endif
17*7c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
18*7c478bd9Sstevel@tonic-gate #include <syslog.h>
19*7c478bd9Sstevel@tonic-gate #ifdef	TEST_LEXER
20*7c478bd9Sstevel@tonic-gate # define	NO_YACC
21*7c478bd9Sstevel@tonic-gate union	{
22*7c478bd9Sstevel@tonic-gate 	int		num;
23*7c478bd9Sstevel@tonic-gate 	char		*str;
24*7c478bd9Sstevel@tonic-gate 	struct in_addr	ipa;
25*7c478bd9Sstevel@tonic-gate 	i6addr_t	ip6;
26*7c478bd9Sstevel@tonic-gate } yylval;
27*7c478bd9Sstevel@tonic-gate #endif
28*7c478bd9Sstevel@tonic-gate #include "lexer.h"
29*7c478bd9Sstevel@tonic-gate #include "y.tab.h"
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate FILE *yyin;
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #define	ishex(c)	(isdigit(c) || ((c) >= 'a' && (c) <= 'f') || \
34*7c478bd9Sstevel@tonic-gate 			 ((c) >= 'A' && (c) <= 'F'))
35*7c478bd9Sstevel@tonic-gate #define	TOOLONG		-3
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate extern int	string_start;
38*7c478bd9Sstevel@tonic-gate extern int	string_end;
39*7c478bd9Sstevel@tonic-gate extern char	*string_val;
40*7c478bd9Sstevel@tonic-gate extern int	pos;
41*7c478bd9Sstevel@tonic-gate extern int	yydebug;
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate char		*yystr = NULL;
44*7c478bd9Sstevel@tonic-gate int		yytext[YYBUFSIZ+1];
45*7c478bd9Sstevel@tonic-gate int		yylineNum = 1;
46*7c478bd9Sstevel@tonic-gate int		yypos = 0;
47*7c478bd9Sstevel@tonic-gate int		yylast = -1;
48*7c478bd9Sstevel@tonic-gate int		yyexpectaddr = 0;
49*7c478bd9Sstevel@tonic-gate int		yybreakondot = 0;
50*7c478bd9Sstevel@tonic-gate int		yyvarnext = 0;
51*7c478bd9Sstevel@tonic-gate int		yytokentype = 0;
52*7c478bd9Sstevel@tonic-gate wordtab_t	*yywordtab = NULL;
53*7c478bd9Sstevel@tonic-gate int		yysavedepth = 0;
54*7c478bd9Sstevel@tonic-gate wordtab_t	*yysavewords[30];
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate static	wordtab_t	*yyfindkey __P((char *));
58*7c478bd9Sstevel@tonic-gate static	int		yygetc __P((void));
59*7c478bd9Sstevel@tonic-gate static	void		yyunputc __P((int));
60*7c478bd9Sstevel@tonic-gate static	int		yyswallow __P((int));
61*7c478bd9Sstevel@tonic-gate static	char		*yytexttostr __P((int, int));
62*7c478bd9Sstevel@tonic-gate static	void		yystrtotext __P((char *));
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate static int yygetc()
66*7c478bd9Sstevel@tonic-gate {
67*7c478bd9Sstevel@tonic-gate 	int c;
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate 	if (yypos < yylast) {
70*7c478bd9Sstevel@tonic-gate 		c = yytext[yypos++];
71*7c478bd9Sstevel@tonic-gate 		return c;
72*7c478bd9Sstevel@tonic-gate 	}
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate 	if (yypos == YYBUFSIZ)
75*7c478bd9Sstevel@tonic-gate 		return TOOLONG;
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate 	if (pos >= string_start && pos <= string_end) {
78*7c478bd9Sstevel@tonic-gate 		c = string_val[pos - string_start];
79*7c478bd9Sstevel@tonic-gate 		yypos++;
80*7c478bd9Sstevel@tonic-gate 	} else {
81*7c478bd9Sstevel@tonic-gate 		c = fgetc(yyin);
82*7c478bd9Sstevel@tonic-gate 		if (c == '\n')
83*7c478bd9Sstevel@tonic-gate 			yylineNum++;
84*7c478bd9Sstevel@tonic-gate 	}
85*7c478bd9Sstevel@tonic-gate 	yytext[yypos++] = c;
86*7c478bd9Sstevel@tonic-gate 	yylast = yypos;
87*7c478bd9Sstevel@tonic-gate 	yytext[yypos] = '\0';
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate 	return c;
90*7c478bd9Sstevel@tonic-gate }
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate static void yyunputc(c)
94*7c478bd9Sstevel@tonic-gate int c;
95*7c478bd9Sstevel@tonic-gate {
96*7c478bd9Sstevel@tonic-gate 	yytext[--yypos] = c;
97*7c478bd9Sstevel@tonic-gate }
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate static int yyswallow(last)
101*7c478bd9Sstevel@tonic-gate int last;
102*7c478bd9Sstevel@tonic-gate {
103*7c478bd9Sstevel@tonic-gate 	int c;
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate 	while (((c = yygetc()) > '\0') && (c != last))
106*7c478bd9Sstevel@tonic-gate 		;
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate 	if (c != EOF)
109*7c478bd9Sstevel@tonic-gate 		yyunputc(c);
110*7c478bd9Sstevel@tonic-gate 	if (c == last)
111*7c478bd9Sstevel@tonic-gate 		return 0;
112*7c478bd9Sstevel@tonic-gate 	return -1;
113*7c478bd9Sstevel@tonic-gate }
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate static void yystrtotext(str)
117*7c478bd9Sstevel@tonic-gate char *str;
118*7c478bd9Sstevel@tonic-gate {
119*7c478bd9Sstevel@tonic-gate 	int len;
120*7c478bd9Sstevel@tonic-gate 	char *s;
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate 	len = strlen(str);
123*7c478bd9Sstevel@tonic-gate 	if (len > YYBUFSIZ)
124*7c478bd9Sstevel@tonic-gate 		len = YYBUFSIZ;
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate 	for (s = str; *s != '\0' && len > 0; s++, len--)
127*7c478bd9Sstevel@tonic-gate 		yytext[yylast++] = *s;
128*7c478bd9Sstevel@tonic-gate 	yytext[yylast] = '\0';
129*7c478bd9Sstevel@tonic-gate }
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate static char *yytexttostr(offset, max)
133*7c478bd9Sstevel@tonic-gate int offset, max;
134*7c478bd9Sstevel@tonic-gate {
135*7c478bd9Sstevel@tonic-gate 	char *str;
136*7c478bd9Sstevel@tonic-gate 	int i;
137*7c478bd9Sstevel@tonic-gate 
138*7c478bd9Sstevel@tonic-gate 	if ((yytext[offset] == '\'' || yytext[offset] == '"') &&
139*7c478bd9Sstevel@tonic-gate 	    (yytext[offset] == yytext[offset + max - 1])) {
140*7c478bd9Sstevel@tonic-gate 		offset++;
141*7c478bd9Sstevel@tonic-gate 		max--;
142*7c478bd9Sstevel@tonic-gate 	}
143*7c478bd9Sstevel@tonic-gate 
144*7c478bd9Sstevel@tonic-gate 	if (max > yylast)
145*7c478bd9Sstevel@tonic-gate 		max = yylast;
146*7c478bd9Sstevel@tonic-gate 	str = malloc(max + 1);
147*7c478bd9Sstevel@tonic-gate 	if (str != NULL) {
148*7c478bd9Sstevel@tonic-gate 		for (i = offset; i < max; i++)
149*7c478bd9Sstevel@tonic-gate 			str[i - offset] = (char)(yytext[i] & 0xff);
150*7c478bd9Sstevel@tonic-gate 		str[i - offset] = '\0';
151*7c478bd9Sstevel@tonic-gate 	}
152*7c478bd9Sstevel@tonic-gate 	return str;
153*7c478bd9Sstevel@tonic-gate }
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate int yylex()
157*7c478bd9Sstevel@tonic-gate {
158*7c478bd9Sstevel@tonic-gate 	int c, n, isbuilding, rval, lnext, nokey = 0;
159*7c478bd9Sstevel@tonic-gate 	char *name;
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate 	isbuilding = 0;
162*7c478bd9Sstevel@tonic-gate 	lnext = 0;
163*7c478bd9Sstevel@tonic-gate 	rval = 0;
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate 	if (yystr != NULL) {
166*7c478bd9Sstevel@tonic-gate 		free(yystr);
167*7c478bd9Sstevel@tonic-gate 		yystr = NULL;
168*7c478bd9Sstevel@tonic-gate 	}
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate nextchar:
171*7c478bd9Sstevel@tonic-gate 	c = yygetc();
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate 	switch (c)
174*7c478bd9Sstevel@tonic-gate 	{
175*7c478bd9Sstevel@tonic-gate 	case '\n' :
176*7c478bd9Sstevel@tonic-gate 	case '\t' :
177*7c478bd9Sstevel@tonic-gate 	case '\r' :
178*7c478bd9Sstevel@tonic-gate 	case ' ' :
179*7c478bd9Sstevel@tonic-gate 		if (isbuilding == 1) {
180*7c478bd9Sstevel@tonic-gate 			yyunputc(c);
181*7c478bd9Sstevel@tonic-gate 			goto done;
182*7c478bd9Sstevel@tonic-gate 		}
183*7c478bd9Sstevel@tonic-gate 		if (yylast > yypos) {
184*7c478bd9Sstevel@tonic-gate 			bcopy(yytext + yypos, yytext,
185*7c478bd9Sstevel@tonic-gate 			      sizeof(yytext[0]) * (yylast - yypos + 1));
186*7c478bd9Sstevel@tonic-gate 		}
187*7c478bd9Sstevel@tonic-gate 		yylast -= yypos;
188*7c478bd9Sstevel@tonic-gate 		yypos = 0;
189*7c478bd9Sstevel@tonic-gate 		goto nextchar;
190*7c478bd9Sstevel@tonic-gate 
191*7c478bd9Sstevel@tonic-gate 	case '\\' :
192*7c478bd9Sstevel@tonic-gate 		if (lnext == 0) {
193*7c478bd9Sstevel@tonic-gate 			lnext = 1;
194*7c478bd9Sstevel@tonic-gate 			if (yylast == yypos) {
195*7c478bd9Sstevel@tonic-gate 				yylast--;
196*7c478bd9Sstevel@tonic-gate 				yypos--;
197*7c478bd9Sstevel@tonic-gate 			} else
198*7c478bd9Sstevel@tonic-gate 				yypos--;
199*7c478bd9Sstevel@tonic-gate 			if (yypos == 0)
200*7c478bd9Sstevel@tonic-gate 				nokey = 1;
201*7c478bd9Sstevel@tonic-gate 			goto nextchar;
202*7c478bd9Sstevel@tonic-gate 		}
203*7c478bd9Sstevel@tonic-gate 		break;
204*7c478bd9Sstevel@tonic-gate 	}
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate 	if (lnext == 1) {
207*7c478bd9Sstevel@tonic-gate 		lnext = 0;
208*7c478bd9Sstevel@tonic-gate 		goto nextchar;
209*7c478bd9Sstevel@tonic-gate 	}
210*7c478bd9Sstevel@tonic-gate 
211*7c478bd9Sstevel@tonic-gate 	switch (c)
212*7c478bd9Sstevel@tonic-gate 	{
213*7c478bd9Sstevel@tonic-gate 	case '#' :
214*7c478bd9Sstevel@tonic-gate 		if (isbuilding == 1) {
215*7c478bd9Sstevel@tonic-gate 			yyunputc(c);
216*7c478bd9Sstevel@tonic-gate 			goto done;
217*7c478bd9Sstevel@tonic-gate 		}
218*7c478bd9Sstevel@tonic-gate 		yyswallow('\n');
219*7c478bd9Sstevel@tonic-gate 		rval = YY_COMMENT;
220*7c478bd9Sstevel@tonic-gate 		goto done;
221*7c478bd9Sstevel@tonic-gate 
222*7c478bd9Sstevel@tonic-gate 	case '$' :
223*7c478bd9Sstevel@tonic-gate 		if (isbuilding == 1) {
224*7c478bd9Sstevel@tonic-gate 			yyunputc(c);
225*7c478bd9Sstevel@tonic-gate 			goto done;
226*7c478bd9Sstevel@tonic-gate 		}
227*7c478bd9Sstevel@tonic-gate 		n = yygetc();
228*7c478bd9Sstevel@tonic-gate 		if (n == '{') {
229*7c478bd9Sstevel@tonic-gate 			if (yyswallow('}') == -1) {
230*7c478bd9Sstevel@tonic-gate 				rval = -2;
231*7c478bd9Sstevel@tonic-gate 				goto done;
232*7c478bd9Sstevel@tonic-gate 			}
233*7c478bd9Sstevel@tonic-gate 			(void) yygetc();
234*7c478bd9Sstevel@tonic-gate 		} else {
235*7c478bd9Sstevel@tonic-gate 			if (!isalpha(n)) {
236*7c478bd9Sstevel@tonic-gate 				yyunputc(n);
237*7c478bd9Sstevel@tonic-gate 				break;
238*7c478bd9Sstevel@tonic-gate 			}
239*7c478bd9Sstevel@tonic-gate 			do {
240*7c478bd9Sstevel@tonic-gate 				n = yygetc();
241*7c478bd9Sstevel@tonic-gate 			} while (isalpha(n) || isdigit(n) || n == '_');
242*7c478bd9Sstevel@tonic-gate 			yyunputc(n);
243*7c478bd9Sstevel@tonic-gate 		}
244*7c478bd9Sstevel@tonic-gate 
245*7c478bd9Sstevel@tonic-gate 		name = yytexttostr(1, yypos);		/* skip $ */
246*7c478bd9Sstevel@tonic-gate 
247*7c478bd9Sstevel@tonic-gate 		if (name != NULL) {
248*7c478bd9Sstevel@tonic-gate 			string_val = get_variable(name, NULL, yylineNum);
249*7c478bd9Sstevel@tonic-gate 			free(name);
250*7c478bd9Sstevel@tonic-gate 			if (string_val != NULL) {
251*7c478bd9Sstevel@tonic-gate 				name = yytexttostr(yypos, yylast);
252*7c478bd9Sstevel@tonic-gate 				if (name != NULL) {
253*7c478bd9Sstevel@tonic-gate 					yypos = 0;
254*7c478bd9Sstevel@tonic-gate 					yylast = 0;
255*7c478bd9Sstevel@tonic-gate 					yystrtotext(string_val);
256*7c478bd9Sstevel@tonic-gate 					yystrtotext(name);
257*7c478bd9Sstevel@tonic-gate 					free(string_val);
258*7c478bd9Sstevel@tonic-gate 					free(name);
259*7c478bd9Sstevel@tonic-gate 					goto nextchar;
260*7c478bd9Sstevel@tonic-gate 				}
261*7c478bd9Sstevel@tonic-gate 				free(string_val);
262*7c478bd9Sstevel@tonic-gate 			}
263*7c478bd9Sstevel@tonic-gate 		}
264*7c478bd9Sstevel@tonic-gate 		break;
265*7c478bd9Sstevel@tonic-gate 
266*7c478bd9Sstevel@tonic-gate 	case '\'':
267*7c478bd9Sstevel@tonic-gate 	case '"' :
268*7c478bd9Sstevel@tonic-gate 		if (isbuilding == 1) {
269*7c478bd9Sstevel@tonic-gate 			goto done;
270*7c478bd9Sstevel@tonic-gate 		}
271*7c478bd9Sstevel@tonic-gate 		do {
272*7c478bd9Sstevel@tonic-gate 			n = yygetc();
273*7c478bd9Sstevel@tonic-gate 			if (n == EOF || n == TOOLONG) {
274*7c478bd9Sstevel@tonic-gate 				rval = -2;
275*7c478bd9Sstevel@tonic-gate 				goto done;
276*7c478bd9Sstevel@tonic-gate 			}
277*7c478bd9Sstevel@tonic-gate 			if (n == '\n') {
278*7c478bd9Sstevel@tonic-gate 				yyunputc(' ');
279*7c478bd9Sstevel@tonic-gate 				yypos++;
280*7c478bd9Sstevel@tonic-gate 			}
281*7c478bd9Sstevel@tonic-gate 		} while (n != c);
282*7c478bd9Sstevel@tonic-gate 		yyunputc(n);
283*7c478bd9Sstevel@tonic-gate 		break;
284*7c478bd9Sstevel@tonic-gate 
285*7c478bd9Sstevel@tonic-gate 	case EOF :
286*7c478bd9Sstevel@tonic-gate 		yylineNum = 1;
287*7c478bd9Sstevel@tonic-gate 		yypos = 0;
288*7c478bd9Sstevel@tonic-gate 		yylast = -1;
289*7c478bd9Sstevel@tonic-gate 		yyexpectaddr = 0;
290*7c478bd9Sstevel@tonic-gate 		yybreakondot = 0;
291*7c478bd9Sstevel@tonic-gate 		yyvarnext = 0;
292*7c478bd9Sstevel@tonic-gate 		yytokentype = 0;
293*7c478bd9Sstevel@tonic-gate 		yysavedepth = 0;
294*7c478bd9Sstevel@tonic-gate 		return 0;
295*7c478bd9Sstevel@tonic-gate 	}
296*7c478bd9Sstevel@tonic-gate 
297*7c478bd9Sstevel@tonic-gate 	if (strchr("=,/;{}()@", c) != NULL) {
298*7c478bd9Sstevel@tonic-gate 		if (isbuilding == 1) {
299*7c478bd9Sstevel@tonic-gate 			yyunputc(c);
300*7c478bd9Sstevel@tonic-gate 			goto done;
301*7c478bd9Sstevel@tonic-gate 		}
302*7c478bd9Sstevel@tonic-gate 		rval = c;
303*7c478bd9Sstevel@tonic-gate 		goto done;
304*7c478bd9Sstevel@tonic-gate 	} else if (c == '.') {
305*7c478bd9Sstevel@tonic-gate 		if (isbuilding == 0) {
306*7c478bd9Sstevel@tonic-gate 			rval = c;
307*7c478bd9Sstevel@tonic-gate 			goto done;
308*7c478bd9Sstevel@tonic-gate 		}
309*7c478bd9Sstevel@tonic-gate 		if (yybreakondot != 0) {
310*7c478bd9Sstevel@tonic-gate 			yyunputc(c);
311*7c478bd9Sstevel@tonic-gate 			goto done;
312*7c478bd9Sstevel@tonic-gate 		}
313*7c478bd9Sstevel@tonic-gate 	}
314*7c478bd9Sstevel@tonic-gate 
315*7c478bd9Sstevel@tonic-gate 	switch (c)
316*7c478bd9Sstevel@tonic-gate 	{
317*7c478bd9Sstevel@tonic-gate 	case '-' :
318*7c478bd9Sstevel@tonic-gate 		if (yyexpectaddr)
319*7c478bd9Sstevel@tonic-gate 			break;
320*7c478bd9Sstevel@tonic-gate 		if (isbuilding == 1)
321*7c478bd9Sstevel@tonic-gate 			break;
322*7c478bd9Sstevel@tonic-gate 		n = yygetc();
323*7c478bd9Sstevel@tonic-gate 		if (n == '>') {
324*7c478bd9Sstevel@tonic-gate 			isbuilding = 1;
325*7c478bd9Sstevel@tonic-gate 			goto done;
326*7c478bd9Sstevel@tonic-gate 		}
327*7c478bd9Sstevel@tonic-gate 		yyunputc(n);
328*7c478bd9Sstevel@tonic-gate 		rval = '-';
329*7c478bd9Sstevel@tonic-gate 		goto done;
330*7c478bd9Sstevel@tonic-gate 
331*7c478bd9Sstevel@tonic-gate 	case '!' :
332*7c478bd9Sstevel@tonic-gate 		if (isbuilding == 1) {
333*7c478bd9Sstevel@tonic-gate 			yyunputc(c);
334*7c478bd9Sstevel@tonic-gate 			goto done;
335*7c478bd9Sstevel@tonic-gate 		}
336*7c478bd9Sstevel@tonic-gate 		n = yygetc();
337*7c478bd9Sstevel@tonic-gate 		if (n == '=') {
338*7c478bd9Sstevel@tonic-gate 			rval = YY_CMP_NE;
339*7c478bd9Sstevel@tonic-gate 			goto done;
340*7c478bd9Sstevel@tonic-gate 		}
341*7c478bd9Sstevel@tonic-gate 		yyunputc(n);
342*7c478bd9Sstevel@tonic-gate 		rval = '!';
343*7c478bd9Sstevel@tonic-gate 		goto done;
344*7c478bd9Sstevel@tonic-gate 
345*7c478bd9Sstevel@tonic-gate 	case '<' :
346*7c478bd9Sstevel@tonic-gate 		if (yyexpectaddr)
347*7c478bd9Sstevel@tonic-gate 			break;
348*7c478bd9Sstevel@tonic-gate 		if (isbuilding == 1) {
349*7c478bd9Sstevel@tonic-gate 			yyunputc(c);
350*7c478bd9Sstevel@tonic-gate 			goto done;
351*7c478bd9Sstevel@tonic-gate 		}
352*7c478bd9Sstevel@tonic-gate 		n = yygetc();
353*7c478bd9Sstevel@tonic-gate 		if (n == '=') {
354*7c478bd9Sstevel@tonic-gate 			rval = YY_CMP_LE;
355*7c478bd9Sstevel@tonic-gate 			goto done;
356*7c478bd9Sstevel@tonic-gate 		}
357*7c478bd9Sstevel@tonic-gate 		if (n == '>') {
358*7c478bd9Sstevel@tonic-gate 			rval = YY_RANGE_OUT;
359*7c478bd9Sstevel@tonic-gate 			goto done;
360*7c478bd9Sstevel@tonic-gate 		}
361*7c478bd9Sstevel@tonic-gate 		yyunputc(n);
362*7c478bd9Sstevel@tonic-gate 		rval = YY_CMP_LT;
363*7c478bd9Sstevel@tonic-gate 		goto done;
364*7c478bd9Sstevel@tonic-gate 
365*7c478bd9Sstevel@tonic-gate 	case '>' :
366*7c478bd9Sstevel@tonic-gate 		if (yyexpectaddr)
367*7c478bd9Sstevel@tonic-gate 			break;
368*7c478bd9Sstevel@tonic-gate 		if (isbuilding == 1) {
369*7c478bd9Sstevel@tonic-gate 			yyunputc(c);
370*7c478bd9Sstevel@tonic-gate 			goto done;
371*7c478bd9Sstevel@tonic-gate 		}
372*7c478bd9Sstevel@tonic-gate 		n = yygetc();
373*7c478bd9Sstevel@tonic-gate 		if (n == '=') {
374*7c478bd9Sstevel@tonic-gate 			rval = YY_CMP_GE;
375*7c478bd9Sstevel@tonic-gate 			goto done;
376*7c478bd9Sstevel@tonic-gate 		}
377*7c478bd9Sstevel@tonic-gate 		if (n == '<') {
378*7c478bd9Sstevel@tonic-gate 			rval = YY_RANGE_IN;
379*7c478bd9Sstevel@tonic-gate 			goto done;
380*7c478bd9Sstevel@tonic-gate 		}
381*7c478bd9Sstevel@tonic-gate 		yyunputc(n);
382*7c478bd9Sstevel@tonic-gate 		rval = YY_CMP_GT;
383*7c478bd9Sstevel@tonic-gate 		goto done;
384*7c478bd9Sstevel@tonic-gate 	}
385*7c478bd9Sstevel@tonic-gate 
386*7c478bd9Sstevel@tonic-gate 	/*
387*7c478bd9Sstevel@tonic-gate 	 * Now for the reason this is here...IPv6 address parsing.
388*7c478bd9Sstevel@tonic-gate 	 * The longest string we can expect is of this form:
389*7c478bd9Sstevel@tonic-gate 	 * 0000:0000:0000:0000:0000:0000:000.000.000.000
390*7c478bd9Sstevel@tonic-gate 	 * not:
391*7c478bd9Sstevel@tonic-gate 	 * 0000:0000:0000:0000:0000:0000:0000:0000
392*7c478bd9Sstevel@tonic-gate 	 */
393*7c478bd9Sstevel@tonic-gate #ifdef	USE_INET6
394*7c478bd9Sstevel@tonic-gate 	if (yyexpectaddr == 1 && isbuilding == 0 && (ishex(c) || c == ':')) {
395*7c478bd9Sstevel@tonic-gate 		char ipv6buf[45 + 1], *s, oc;
396*7c478bd9Sstevel@tonic-gate 		int start;
397*7c478bd9Sstevel@tonic-gate 
398*7c478bd9Sstevel@tonic-gate 		start = yypos;
399*7c478bd9Sstevel@tonic-gate 		s = ipv6buf;
400*7c478bd9Sstevel@tonic-gate 		oc = c;
401*7c478bd9Sstevel@tonic-gate 
402*7c478bd9Sstevel@tonic-gate 		/*
403*7c478bd9Sstevel@tonic-gate 		 * Perhaps we should implement stricter controls on what we
404*7c478bd9Sstevel@tonic-gate 		 * swallow up here, but surely it would just be duplicating
405*7c478bd9Sstevel@tonic-gate 		 * the code in inet_pton() anyway.
406*7c478bd9Sstevel@tonic-gate 		 */
407*7c478bd9Sstevel@tonic-gate 		do {
408*7c478bd9Sstevel@tonic-gate 			*s++ = c;
409*7c478bd9Sstevel@tonic-gate 			c = yygetc();
410*7c478bd9Sstevel@tonic-gate 		} while ((ishex(c) || c == ':' || c == '.') &&
411*7c478bd9Sstevel@tonic-gate 			 (s - ipv6buf < 46));
412*7c478bd9Sstevel@tonic-gate 		yyunputc(c);
413*7c478bd9Sstevel@tonic-gate 		*s = '\0';
414*7c478bd9Sstevel@tonic-gate 
415*7c478bd9Sstevel@tonic-gate 		if (inet_pton(AF_INET6, ipv6buf, &yylval.ip6) == 1) {
416*7c478bd9Sstevel@tonic-gate 			rval = YY_IPV6;
417*7c478bd9Sstevel@tonic-gate 			yyexpectaddr = 0;
418*7c478bd9Sstevel@tonic-gate 			goto done;
419*7c478bd9Sstevel@tonic-gate 		}
420*7c478bd9Sstevel@tonic-gate 		yypos = start;
421*7c478bd9Sstevel@tonic-gate 		c = oc;
422*7c478bd9Sstevel@tonic-gate 	}
423*7c478bd9Sstevel@tonic-gate #endif
424*7c478bd9Sstevel@tonic-gate 
425*7c478bd9Sstevel@tonic-gate 	if (c == ':') {
426*7c478bd9Sstevel@tonic-gate 		if (isbuilding == 1) {
427*7c478bd9Sstevel@tonic-gate 			yyunputc(c);
428*7c478bd9Sstevel@tonic-gate 			goto done;
429*7c478bd9Sstevel@tonic-gate 		}
430*7c478bd9Sstevel@tonic-gate 		rval = ':';
431*7c478bd9Sstevel@tonic-gate 		goto done;
432*7c478bd9Sstevel@tonic-gate 	}
433*7c478bd9Sstevel@tonic-gate 
434*7c478bd9Sstevel@tonic-gate 	if (isbuilding == 0 && c == '0') {
435*7c478bd9Sstevel@tonic-gate 		n = yygetc();
436*7c478bd9Sstevel@tonic-gate 		if (n == 'x') {
437*7c478bd9Sstevel@tonic-gate 			do {
438*7c478bd9Sstevel@tonic-gate 				n = yygetc();
439*7c478bd9Sstevel@tonic-gate 			} while (ishex(n));
440*7c478bd9Sstevel@tonic-gate 			yyunputc(n);
441*7c478bd9Sstevel@tonic-gate 			rval = YY_HEX;
442*7c478bd9Sstevel@tonic-gate 			goto done;
443*7c478bd9Sstevel@tonic-gate 		}
444*7c478bd9Sstevel@tonic-gate 		yyunputc(n);
445*7c478bd9Sstevel@tonic-gate 	}
446*7c478bd9Sstevel@tonic-gate 
447*7c478bd9Sstevel@tonic-gate 	/*
448*7c478bd9Sstevel@tonic-gate 	 * No negative numbers with leading - sign..
449*7c478bd9Sstevel@tonic-gate 	 */
450*7c478bd9Sstevel@tonic-gate 	if (isbuilding == 0 && isdigit(c)) {
451*7c478bd9Sstevel@tonic-gate 		do {
452*7c478bd9Sstevel@tonic-gate 			n = yygetc();
453*7c478bd9Sstevel@tonic-gate 		} while (isdigit(n));
454*7c478bd9Sstevel@tonic-gate 		yyunputc(n);
455*7c478bd9Sstevel@tonic-gate 		rval = YY_NUMBER;
456*7c478bd9Sstevel@tonic-gate 		goto done;
457*7c478bd9Sstevel@tonic-gate 	}
458*7c478bd9Sstevel@tonic-gate 
459*7c478bd9Sstevel@tonic-gate 	isbuilding = 1;
460*7c478bd9Sstevel@tonic-gate 	goto nextchar;
461*7c478bd9Sstevel@tonic-gate 
462*7c478bd9Sstevel@tonic-gate done:
463*7c478bd9Sstevel@tonic-gate 	yystr = yytexttostr(0, yypos);
464*7c478bd9Sstevel@tonic-gate 
465*7c478bd9Sstevel@tonic-gate 	if (isbuilding == 1) {
466*7c478bd9Sstevel@tonic-gate 		wordtab_t *w;
467*7c478bd9Sstevel@tonic-gate 
468*7c478bd9Sstevel@tonic-gate 		w = NULL;
469*7c478bd9Sstevel@tonic-gate 		isbuilding = 0;
470*7c478bd9Sstevel@tonic-gate 
471*7c478bd9Sstevel@tonic-gate 		if ((yyvarnext == 0) && (nokey == 0)) {
472*7c478bd9Sstevel@tonic-gate 			w = yyfindkey(yystr);
473*7c478bd9Sstevel@tonic-gate 			if (w == NULL && yywordtab != NULL) {
474*7c478bd9Sstevel@tonic-gate 				yyresetdict();
475*7c478bd9Sstevel@tonic-gate 				w = yyfindkey(yystr);
476*7c478bd9Sstevel@tonic-gate 			}
477*7c478bd9Sstevel@tonic-gate 		} else
478*7c478bd9Sstevel@tonic-gate 			yyvarnext = 0;
479*7c478bd9Sstevel@tonic-gate 		if (w != NULL)
480*7c478bd9Sstevel@tonic-gate 			rval = w->w_value;
481*7c478bd9Sstevel@tonic-gate 		else
482*7c478bd9Sstevel@tonic-gate 			rval = YY_STR;
483*7c478bd9Sstevel@tonic-gate 	}
484*7c478bd9Sstevel@tonic-gate 
485*7c478bd9Sstevel@tonic-gate 	if (rval == YY_STR && yysavedepth > 0)
486*7c478bd9Sstevel@tonic-gate 		yyresetdict();
487*7c478bd9Sstevel@tonic-gate 
488*7c478bd9Sstevel@tonic-gate 	yytokentype = rval;
489*7c478bd9Sstevel@tonic-gate 
490*7c478bd9Sstevel@tonic-gate 	if (yydebug)
491*7c478bd9Sstevel@tonic-gate 		printf("lexed(%s) => %d\n", yystr, rval);
492*7c478bd9Sstevel@tonic-gate 
493*7c478bd9Sstevel@tonic-gate 	switch (rval)
494*7c478bd9Sstevel@tonic-gate 	{
495*7c478bd9Sstevel@tonic-gate 	case YY_NUMBER :
496*7c478bd9Sstevel@tonic-gate 		yylval.num = atoi(yystr);
497*7c478bd9Sstevel@tonic-gate 		break;
498*7c478bd9Sstevel@tonic-gate 
499*7c478bd9Sstevel@tonic-gate 	case YY_HEX :
500*7c478bd9Sstevel@tonic-gate 		sscanf(yystr, "0x%x", (u_int *)&yylval.num);
501*7c478bd9Sstevel@tonic-gate 		break;
502*7c478bd9Sstevel@tonic-gate 
503*7c478bd9Sstevel@tonic-gate 	case YY_STR :
504*7c478bd9Sstevel@tonic-gate 		yylval.str = strdup(yystr);
505*7c478bd9Sstevel@tonic-gate 		break;
506*7c478bd9Sstevel@tonic-gate 
507*7c478bd9Sstevel@tonic-gate 	default :
508*7c478bd9Sstevel@tonic-gate 		break;
509*7c478bd9Sstevel@tonic-gate 	}
510*7c478bd9Sstevel@tonic-gate 
511*7c478bd9Sstevel@tonic-gate 	if (yylast > 0) {
512*7c478bd9Sstevel@tonic-gate 		bcopy(yytext + yypos, yytext,
513*7c478bd9Sstevel@tonic-gate 		      sizeof(yytext[0]) * (yylast - yypos + 1));
514*7c478bd9Sstevel@tonic-gate 		yylast -= yypos;
515*7c478bd9Sstevel@tonic-gate 		yypos = 0;
516*7c478bd9Sstevel@tonic-gate 	}
517*7c478bd9Sstevel@tonic-gate 
518*7c478bd9Sstevel@tonic-gate 	return rval;
519*7c478bd9Sstevel@tonic-gate }
520*7c478bd9Sstevel@tonic-gate 
521*7c478bd9Sstevel@tonic-gate 
522*7c478bd9Sstevel@tonic-gate static wordtab_t *yyfindkey(key)
523*7c478bd9Sstevel@tonic-gate char *key;
524*7c478bd9Sstevel@tonic-gate {
525*7c478bd9Sstevel@tonic-gate 	wordtab_t *w;
526*7c478bd9Sstevel@tonic-gate 
527*7c478bd9Sstevel@tonic-gate 	if (yywordtab == NULL)
528*7c478bd9Sstevel@tonic-gate 		return NULL;
529*7c478bd9Sstevel@tonic-gate 
530*7c478bd9Sstevel@tonic-gate 	for (w = yywordtab; w->w_word != 0; w++)
531*7c478bd9Sstevel@tonic-gate 		if (strcasecmp(key, w->w_word) == 0)
532*7c478bd9Sstevel@tonic-gate 			return w;
533*7c478bd9Sstevel@tonic-gate 	return NULL;
534*7c478bd9Sstevel@tonic-gate }
535*7c478bd9Sstevel@tonic-gate 
536*7c478bd9Sstevel@tonic-gate 
537*7c478bd9Sstevel@tonic-gate char *yykeytostr(num)
538*7c478bd9Sstevel@tonic-gate int num;
539*7c478bd9Sstevel@tonic-gate {
540*7c478bd9Sstevel@tonic-gate 	wordtab_t *w;
541*7c478bd9Sstevel@tonic-gate 
542*7c478bd9Sstevel@tonic-gate 	if (yywordtab == NULL)
543*7c478bd9Sstevel@tonic-gate 		return "<unknown>";
544*7c478bd9Sstevel@tonic-gate 
545*7c478bd9Sstevel@tonic-gate 	for (w = yywordtab; w->w_word; w++)
546*7c478bd9Sstevel@tonic-gate 		if (w->w_value == num)
547*7c478bd9Sstevel@tonic-gate 			return w->w_word;
548*7c478bd9Sstevel@tonic-gate 	return "<unknown>";
549*7c478bd9Sstevel@tonic-gate }
550*7c478bd9Sstevel@tonic-gate 
551*7c478bd9Sstevel@tonic-gate 
552*7c478bd9Sstevel@tonic-gate wordtab_t *yysettab(words)
553*7c478bd9Sstevel@tonic-gate wordtab_t *words;
554*7c478bd9Sstevel@tonic-gate {
555*7c478bd9Sstevel@tonic-gate 	wordtab_t *save;
556*7c478bd9Sstevel@tonic-gate 
557*7c478bd9Sstevel@tonic-gate 	save = yywordtab;
558*7c478bd9Sstevel@tonic-gate 	yywordtab = words;
559*7c478bd9Sstevel@tonic-gate 	return save;
560*7c478bd9Sstevel@tonic-gate }
561*7c478bd9Sstevel@tonic-gate 
562*7c478bd9Sstevel@tonic-gate 
563*7c478bd9Sstevel@tonic-gate void yyerror(msg)
564*7c478bd9Sstevel@tonic-gate char *msg;
565*7c478bd9Sstevel@tonic-gate {
566*7c478bd9Sstevel@tonic-gate 	char *txt, letter[2];
567*7c478bd9Sstevel@tonic-gate 	int freetxt = 0;
568*7c478bd9Sstevel@tonic-gate 
569*7c478bd9Sstevel@tonic-gate 	if (yytokentype < 256) {
570*7c478bd9Sstevel@tonic-gate 		letter[0] = yytokentype;
571*7c478bd9Sstevel@tonic-gate 		letter[1] = '\0';
572*7c478bd9Sstevel@tonic-gate 		txt =  letter;
573*7c478bd9Sstevel@tonic-gate 	} else if (yytokentype == YY_STR || yytokentype == YY_HEX ||
574*7c478bd9Sstevel@tonic-gate 		   yytokentype == YY_NUMBER) {
575*7c478bd9Sstevel@tonic-gate 		if (yystr == NULL) {
576*7c478bd9Sstevel@tonic-gate 			txt = yytexttostr(yypos, YYBUFSIZ);
577*7c478bd9Sstevel@tonic-gate 			freetxt = 1;
578*7c478bd9Sstevel@tonic-gate 		} else
579*7c478bd9Sstevel@tonic-gate 			txt = yystr;
580*7c478bd9Sstevel@tonic-gate 	} else {
581*7c478bd9Sstevel@tonic-gate 		txt = yykeytostr(yytokentype);
582*7c478bd9Sstevel@tonic-gate 	}
583*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "%s error at \"%s\", line %d\n", msg, txt, yylineNum);
584*7c478bd9Sstevel@tonic-gate 	if (freetxt == 1)
585*7c478bd9Sstevel@tonic-gate 		free(txt);
586*7c478bd9Sstevel@tonic-gate 	exit(1);
587*7c478bd9Sstevel@tonic-gate }
588*7c478bd9Sstevel@tonic-gate 
589*7c478bd9Sstevel@tonic-gate 
590*7c478bd9Sstevel@tonic-gate void yysetdict(newdict)
591*7c478bd9Sstevel@tonic-gate wordtab_t *newdict;
592*7c478bd9Sstevel@tonic-gate {
593*7c478bd9Sstevel@tonic-gate 	if (yysavedepth == sizeof(yysavewords)/sizeof(yysavewords[0])) {
594*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%d: at maximum dictionary depth\n",
595*7c478bd9Sstevel@tonic-gate 			yylineNum);
596*7c478bd9Sstevel@tonic-gate 		return;
597*7c478bd9Sstevel@tonic-gate 	}
598*7c478bd9Sstevel@tonic-gate 
599*7c478bd9Sstevel@tonic-gate 	yysavewords[yysavedepth++] = yysettab(newdict);
600*7c478bd9Sstevel@tonic-gate 	if (yydebug)
601*7c478bd9Sstevel@tonic-gate 		printf("yysavedepth++ => %d\n", yysavedepth);
602*7c478bd9Sstevel@tonic-gate }
603*7c478bd9Sstevel@tonic-gate 
604*7c478bd9Sstevel@tonic-gate void yyresetdict()
605*7c478bd9Sstevel@tonic-gate {
606*7c478bd9Sstevel@tonic-gate 	if (yysavedepth > 0) {
607*7c478bd9Sstevel@tonic-gate 		yysettab(yysavewords[--yysavedepth]);
608*7c478bd9Sstevel@tonic-gate 		if (yydebug)
609*7c478bd9Sstevel@tonic-gate 			printf("yysavedepth-- => %d\n", yysavedepth);
610*7c478bd9Sstevel@tonic-gate 	}
611*7c478bd9Sstevel@tonic-gate }
612*7c478bd9Sstevel@tonic-gate 
613*7c478bd9Sstevel@tonic-gate 
614*7c478bd9Sstevel@tonic-gate 
615*7c478bd9Sstevel@tonic-gate #ifdef	TEST_LEXER
616*7c478bd9Sstevel@tonic-gate int main(argc, argv)
617*7c478bd9Sstevel@tonic-gate int argc;
618*7c478bd9Sstevel@tonic-gate char *argv[];
619*7c478bd9Sstevel@tonic-gate {
620*7c478bd9Sstevel@tonic-gate 	int n;
621*7c478bd9Sstevel@tonic-gate 
622*7c478bd9Sstevel@tonic-gate 	yyin = stdin;
623*7c478bd9Sstevel@tonic-gate 
624*7c478bd9Sstevel@tonic-gate 	while ((n = yylex()) != 0)
625*7c478bd9Sstevel@tonic-gate 		printf("%d.n = %d [%s] %d %d\n",
626*7c478bd9Sstevel@tonic-gate 			yylineNum, n, yystr, yypos, yylast);
627*7c478bd9Sstevel@tonic-gate }
628*7c478bd9Sstevel@tonic-gate #endif
629