xref: /freebsd/usr.sbin/rrenumd/lexer.l (revision 18242d3b09dbc3f5e278e39baaa3c3b76624c901)
1 /*	$KAME: lexer.l,v 1.7 2000/11/08 02:40:53 itojun Exp $	*/
2 
3 /*
4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the project nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  * $FreeBSD$
32  */
33 
34 %{
35 #define YY_NO_UNPUT
36 
37 #include <sys/param.h>
38 #include <sys/ioctl.h>
39 #include <sys/socket.h>
40 #include <sys/queue.h>
41 
42 #include <string.h>
43 
44 #include <net/if.h>
45 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
46 #include <net/if_var.h>
47 #endif /* __FreeBSD__ >= 3 */
48 
49 #include <netinet/in.h>
50 #include <netinet/in_var.h>
51 #include <netinet/icmp6.h>
52 
53 #include <arpa/inet.h>
54 
55 #include "y.tab.h"
56 
57 int lineno = 1;
58 
59 #define LINEBUF_SIZE 1000
60 char linebuf[LINEBUF_SIZE];
61 
62 int parse __P((FILE **));
63 void yyerror __P((const char *));
64 int yylex __P((void));
65 %}
66 
67 /* common section */
68 nl		\n
69 ws		[ \t]+
70 digit		[0-9]
71 letter		[0-9A-Za-z]
72 hexdigit	[0-9A-Fa-f]
73 special		[()+\|\?\*,]
74 dot		\.
75 hyphen		\-
76 colon		\:
77 slash		\/
78 bcl		\{
79 ecl		\}
80 semi		\;
81 usec		{dot}{digit}{1,6}
82 comment		\#.*
83 qstring		\"[^"]*\"
84 decstring	{digit}+
85 hexpair		{hexdigit}{hexdigit}
86 hexstring	0[xX]{hexdigit}+
87 octetstring	{octet}({dot}{octet})+
88 ipv4addr	{digit}{1,3}({dot}{digit}{1,3}){0,3}
89 ipv6addr	{hexdigit}{0,4}({colon}{hexdigit}{0,4}){2,7}
90 ipaddrmask	{slash}{digit}{1,3}
91 keyword		{letter}{letter}+
92 name		{letter}(({letter}|{digit}|{hyphen})*({letter}|{digit}))*
93 hostname	{name}(({dot}{name})+{dot}?)?
94 
95 timeval		{digit}{0,2}
96 days		d{timeval}
97 hours		h{timeval}
98 minutes		m{timeval}
99 seconds		s{timeval}
100 
101 mprefix		match_prefix|match-prefix
102 uprefix		use_prefix|use-prefix
103 
104 %%
105 	/* rrenumd keywords */
106 debug		{
107 			return(DEBUG_CMD);
108 		}
109 dest		{
110 			return(DEST_CMD);
111 		}
112 retry		{
113 			return(RETRY_CMD);
114 		}
115 seqnum		{
116 			return(SEQNUM_CMD);
117 		}
118 add		{
119 			yylval.num = RPM_PCO_ADD;
120 			return(ADD);
121 		}
122 change		{
123 			yylval.num = RPM_PCO_CHANGE;
124 			return(CHANGE);
125 		 }
126 setglobal	{
127 			yylval.num = RPM_PCO_SETGLOBAL;
128 			return(SETGLOBAL);
129 		}
130 {mprefix}	{
131 			return(MATCH_PREFIX_CMD);
132 		}
133 maxlen		{
134 			return(MAXLEN_CMD);
135 		}
136 minlen		{
137 			return(MINLEN_CMD);
138 		}
139 {uprefix}	{
140 			return(USE_PREFIX_CMD);
141 		}
142 keeplen		{
143 			return(KEEPLEN_CMD);
144 		}
145 
146 vltime		{
147 			return(VLTIME_CMD);
148 		}
149 pltime		{
150 			return(PLTIME_CMD);
151 		}
152 raf_onlink	{
153 			return(RAF_ONLINK_CMD);
154 		}
155 raf_auto	{
156 			return(RAF_AUTO_CMD);
157 		}
158 rrf_decrvalid	{
159 			return(RAF_DECRVALID_CMD);
160 		}
161 rrf_decrprefd	{
162 			return(RAF_DECRPREFD_CMD);
163 		}
164 {days}		{
165 			yytext++;
166 			yylval.num = atoi(yytext);
167 			return(DAYS);
168 		}
169 {hours}		{
170 			yytext++;
171 			yylval.num = atoi(yytext);
172 			return(HOURS);
173 		}
174 {minutes}	{
175 			yytext++;
176 			yylval.num = atoi(yytext);
177 			return(MINUTES);
178 		}
179 {seconds}	{
180 			yytext++;
181 			yylval.num = atoi(yytext);
182 			return(SECONDS);
183 		}
184 infinity	{
185 			return(INFINITY);
186 		}
187 
188 on		{
189 			yylval.num = 1;
190 			return(ON);
191 		}
192 off		{
193 			yylval.num = 0;
194 			return(OFF);
195 		}
196 
197 	/* basic rules */
198 {ws}		;
199 {nl}		{
200 			lineno++;
201 		}
202 {semi}		{
203 			return EOS;
204 		}
205 {bcl}		{
206 			return BCL;
207 		}
208 {ecl}		{
209 			return ECL;
210 		}
211 {qstring}	{
212 			yylval.cs.cp = yytext;
213 			yylval.cs.len = yyleng;
214 			return QSTRING;
215 		}
216 {decstring}	{
217 			yylval.cs.cp = yytext;
218 			yylval.cs.len = yyleng;
219 			return DECSTRING;
220 		}
221 {name}		{
222 			yylval.cs.cp = yytext;
223 			yylval.cs.len = yyleng;
224 			return NAME;
225 		}
226 {ipv4addr}	{
227 			memset(&yylval.addr4, 0, sizeof(struct in_addr));
228 			if (inet_pton(AF_INET, yytext,
229 				      &yylval.addr4) == 1) {
230 				return IPV4ADDR;
231 			} else {
232 				return ERROR;
233 			}
234 		}
235 {ipv6addr}	{
236 			memset(&yylval.addr6, 0, sizeof(struct in6_addr));
237 			if (inet_pton(AF_INET6, yytext,
238 				      &yylval.addr6) == 1) {
239 				return IPV6ADDR;
240 			} else {
241 				return ERROR;
242 			}
243 		}
244 {ipaddrmask}	{
245 			yytext++;
246 			yylval.num = atoi(yytext);
247 			return(PREFIXLEN);
248 		}
249 {hostname}	{
250 			yylval.cs.cp = yytext;
251 			yylval.cs.len = yyleng;
252 			return HOSTNAME;
253 		}
254 %%
255 
256 int parse(FILE **fp)
257 {
258 	extern int yyparse __P((void));
259 
260 	yyin = *fp;
261 
262 	if (yyparse())
263 		return(-1);
264 
265 	return(0);
266 
267 }
268 
269 void
270 yyerror(const char *s)
271 {
272 	printf("%s: at %s in line %d\n", s, yytext, lineno);
273 }
274