178064Sume/*	$KAME: lexer.l,v 1.7 2000/11/08 02:40:53 itojun Exp $	*/
262638Skris
355505Sshin/*
455505Sshin * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
555505Sshin * All rights reserved.
655505Sshin *
755505Sshin * Redistribution and use in source and binary forms, with or without
855505Sshin * modification, are permitted provided that the following conditions
955505Sshin * are met:
1055505Sshin * 1. Redistributions of source code must retain the above copyright
1155505Sshin *    notice, this list of conditions and the following disclaimer.
1255505Sshin * 2. Redistributions in binary form must reproduce the above copyright
1355505Sshin *    notice, this list of conditions and the following disclaimer in the
1455505Sshin *    documentation and/or other materials provided with the distribution.
1555505Sshin * 3. Neither the name of the project nor the names of its contributors
1655505Sshin *    may be used to endorse or promote products derived from this software
1755505Sshin *    without specific prior written permission.
1855505Sshin *
1955505Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
2055505Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2155505Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2255505Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2355505Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2455505Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2555505Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2655505Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2755505Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2855505Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2955505Sshin * SUCH DAMAGE.
3055505Sshin *
3155505Sshin * $FreeBSD$
3255505Sshin */
3355505Sshin
3455505Sshin%{
3555505Sshin#include <sys/param.h>
3655505Sshin#include <sys/ioctl.h>
3755505Sshin#include <sys/socket.h>
3878064Sume#include <sys/queue.h>
3955505Sshin
4055505Sshin#include <string.h>
4155505Sshin
4255505Sshin#include <net/if.h>
4362638Skris#if defined(__FreeBSD__) && __FreeBSD__ >= 3
4455505Sshin#include <net/if_var.h>
4562638Skris#endif /* __FreeBSD__ >= 3 */
4655505Sshin
4755505Sshin#include <netinet/in.h>
4855505Sshin#include <netinet/in_var.h>
4955505Sshin#include <netinet/icmp6.h>
5062638Skris
5162638Skris#include <arpa/inet.h>
5262638Skris
5355505Sshin#include "y.tab.h"
5455505Sshin
5555505Sshinint lineno = 1;
5655505Sshin
5762638Skris#define LINEBUF_SIZE 1000
5855505Sshinchar linebuf[LINEBUF_SIZE];
5978064Sume
60173412Skevloint parse(FILE **);
61173412Skevlovoid yyerror(const char *);
62173412Skevloint yylex(void);
6355505Sshin%}
6455505Sshin
65252181Smarius%option nounput
66252181Smarius
6755505Sshin/* common section */
6855505Sshinnl		\n
6955505Sshinws		[ \t]+
7055505Sshindigit		[0-9]
7155505Sshinletter		[0-9A-Za-z]
7255505Sshinhexdigit	[0-9A-Fa-f]
7355505Sshinspecial		[()+\|\?\*,]
7455505Sshindot		\.
7555505Sshinhyphen		\-
7655505Sshincolon		\:
7755505Sshinslash		\/
7855505Sshinbcl		\{
7955505Sshinecl		\}
8055505Sshinsemi		\;
8155505Sshinusec		{dot}{digit}{1,6}
8255505Sshincomment		\#.*
8355505Sshinqstring		\"[^"]*\"
8455505Sshindecstring	{digit}+
8555505Sshinhexpair		{hexdigit}{hexdigit}
8655505Sshinhexstring	0[xX]{hexdigit}+
8755505Sshinoctetstring	{octet}({dot}{octet})+
8855505Sshinipv4addr	{digit}{1,3}({dot}{digit}{1,3}){0,3}
8955505Sshinipv6addr	{hexdigit}{0,4}({colon}{hexdigit}{0,4}){2,7}
9055505Sshinipaddrmask	{slash}{digit}{1,3}
9155505Sshinkeyword		{letter}{letter}+
9255505Sshinname		{letter}(({letter}|{digit}|{hyphen})*({letter}|{digit}))*
9355505Sshinhostname	{name}(({dot}{name})+{dot}?)?
9455505Sshin
9555505Sshintimeval		{digit}{0,2}
9655505Sshindays		d{timeval}
9755505Sshinhours		h{timeval}
9855505Sshinminutes		m{timeval}
9955505Sshinseconds		s{timeval}
10055505Sshin
10155505Sshinmprefix		match_prefix|match-prefix
10255505Sshinuprefix		use_prefix|use-prefix
10355505Sshin
10455505Sshin%%
10555505Sshin	/* rrenumd keywords */
10655505Sshindebug		{
10755505Sshin			return(DEBUG_CMD);
10855505Sshin		}
10955505Sshindest		{
11055505Sshin			return(DEST_CMD);
11155505Sshin		}
11255505Sshinretry		{
11355505Sshin			return(RETRY_CMD);
11455505Sshin		}
11555505Sshinseqnum		{
11655505Sshin			return(SEQNUM_CMD);
11755505Sshin		}
11855505Sshinadd		{
11955505Sshin			yylval.num = RPM_PCO_ADD;
12055505Sshin			return(ADD);
12155505Sshin		}
12255505Sshinchange		{
12355505Sshin			yylval.num = RPM_PCO_CHANGE;
12455505Sshin			return(CHANGE);
12555505Sshin		 }
12655505Sshinsetglobal	{
12755505Sshin			yylval.num = RPM_PCO_SETGLOBAL;
12855505Sshin			return(SETGLOBAL);
12955505Sshin		}
13055505Sshin{mprefix}	{
13155505Sshin			return(MATCH_PREFIX_CMD);
13255505Sshin		}
13355505Sshinmaxlen		{
13455505Sshin			return(MAXLEN_CMD);
13555505Sshin		}
13655505Sshinminlen		{
13755505Sshin			return(MINLEN_CMD);
13855505Sshin		}
13955505Sshin{uprefix}	{
14055505Sshin			return(USE_PREFIX_CMD);
14155505Sshin		}
14255505Sshinkeeplen		{
14355505Sshin			return(KEEPLEN_CMD);
14455505Sshin		}
14555505Sshin
14655505Sshinvltime		{
14755505Sshin			return(VLTIME_CMD);
14855505Sshin		}
14955505Sshinpltime		{
15055505Sshin			return(PLTIME_CMD);
15155505Sshin		}
15255505Sshinraf_onlink	{
15355505Sshin			return(RAF_ONLINK_CMD);
15455505Sshin		}
15555505Sshinraf_auto	{
15655505Sshin			return(RAF_AUTO_CMD);
15755505Sshin		}
15855505Sshinrrf_decrvalid	{
15955505Sshin			return(RAF_DECRVALID_CMD);
16055505Sshin		}
16155505Sshinrrf_decrprefd	{
16255505Sshin			return(RAF_DECRPREFD_CMD);
16355505Sshin		}
16455505Sshin{days}		{
16555505Sshin			yytext++;
16655505Sshin			yylval.num = atoi(yytext);
16755505Sshin			return(DAYS);
16855505Sshin		}
16955505Sshin{hours}		{
17055505Sshin			yytext++;
17155505Sshin			yylval.num = atoi(yytext);
17255505Sshin			return(HOURS);
17355505Sshin		}
17455505Sshin{minutes}	{
17555505Sshin			yytext++;
17655505Sshin			yylval.num = atoi(yytext);
17755505Sshin			return(MINUTES);
17855505Sshin		}
17955505Sshin{seconds}	{
18055505Sshin			yytext++;
18155505Sshin			yylval.num = atoi(yytext);
18255505Sshin			return(SECONDS);
18355505Sshin		}
18455505Sshininfinity	{
18555505Sshin			return(INFINITY);
18655505Sshin		}
18755505Sshin
18855505Sshinon		{
18955505Sshin			yylval.num = 1;
19055505Sshin			return(ON);
19155505Sshin		}
19255505Sshinoff		{
19355505Sshin			yylval.num = 0;
19455505Sshin			return(OFF);
19555505Sshin		}
19655505Sshin
19755505Sshin	/* basic rules */
19855505Sshin{ws}		;
19955505Sshin{nl}		{
20055505Sshin			lineno++;
20155505Sshin		}
20255505Sshin{semi}		{
20355505Sshin			return EOS;
20455505Sshin		}
20555505Sshin{bcl}		{
20655505Sshin			return BCL;
20755505Sshin		}
20855505Sshin{ecl}		{
20955505Sshin			return ECL;
21055505Sshin		}
21155505Sshin{qstring}	{
21255505Sshin			yylval.cs.cp = yytext;
21355505Sshin			yylval.cs.len = yyleng;
21455505Sshin			return QSTRING;
21555505Sshin		}
21655505Sshin{decstring}	{
21755505Sshin			yylval.cs.cp = yytext;
21855505Sshin			yylval.cs.len = yyleng;
21955505Sshin			return DECSTRING;
22055505Sshin		}
22155505Sshin{name}		{
22255505Sshin			yylval.cs.cp = yytext;
22355505Sshin			yylval.cs.len = yyleng;
22455505Sshin			return NAME;
22555505Sshin		}
22662638Skris{ipv4addr}	{
22762638Skris			memset(&yylval.addr4, 0, sizeof(struct in_addr));
22862638Skris			if (inet_pton(AF_INET, yytext,
22962638Skris				      &yylval.addr4) == 1) {
23062638Skris				return IPV4ADDR;
23162638Skris			} else {
23262638Skris				return ERROR;
23362638Skris			}
23462638Skris		}
23555505Sshin{ipv6addr}	{
23655505Sshin			memset(&yylval.addr6, 0, sizeof(struct in6_addr));
23755505Sshin			if (inet_pton(AF_INET6, yytext,
23855505Sshin				      &yylval.addr6) == 1) {
23955505Sshin				return IPV6ADDR;
24055505Sshin			} else {
24155505Sshin				return ERROR;
24255505Sshin			}
24355505Sshin		}
24455505Sshin{ipaddrmask}	{
24555505Sshin			yytext++;
24655505Sshin			yylval.num = atoi(yytext);
24755505Sshin			return(PREFIXLEN);
24855505Sshin		}
24955505Sshin{hostname}	{
25055505Sshin			yylval.cs.cp = yytext;
25155505Sshin			yylval.cs.len = yyleng;
25255505Sshin			return HOSTNAME;
25355505Sshin		}
25455505Sshin%%
25555505Sshin
25655505Sshinint parse(FILE **fp)
25755505Sshin{
258173412Skevlo	extern int yyparse(void);
25978064Sume
26055505Sshin	yyin = *fp;
26155505Sshin
26278064Sume	if (yyparse())
26355505Sshin		return(-1);
26455505Sshin
26555505Sshin	return(0);
26655505Sshin
26755505Sshin}
26855505Sshin
26955505Sshinvoid
27055505Sshinyyerror(const char *s)
27155505Sshin{
27255505Sshin	printf("%s: at %s in line %d\n", s, yytext, lineno);
27355505Sshin}
274