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