npf_scan.l revision 1.5.2.2 1 /* $NetBSD: npf_scan.l,v 1.5.2.2 2013/02/25 00:30:46 tls Exp $ */
2
3 /*-
4 * Copyright (c) 2011-2012 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Martin Husemann.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 %{
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <err.h>
36
37 #include "npfctl.h"
38 #include "npf_parse.h"
39
40 int yycolumn;
41
42 #define YY_USER_ACTION yycolumn += yyleng;
43
44 extern int yyparsetarget;
45 extern int yylineno;
46 extern const char * yyfilename;
47 extern int yyparse(void);
48 extern void yyrestart(FILE *);
49
50 void
51 npfctl_parse_file(const char *name)
52 {
53 FILE *fp;
54
55 fp = fopen(name, "r");
56 if (fp == NULL) {
57 err(EXIT_FAILURE, "open '%s'", name);
58 }
59 yyparsetarget = NPFCTL_PARSE_FILE;
60 yyrestart(fp);
61 yylineno = 1;
62 yycolumn = 0;
63 yyfilename = name;
64 yyparse();
65 fclose(fp);
66 }
67
68 void
69 npfctl_parse_string(const char *str)
70 {
71 YY_BUFFER_STATE bs;
72
73 yyparsetarget = NPFCTL_PARSE_STRING;
74 bs = yy_scan_string(str);
75 yyfilename = "stdin";
76 yyparse();
77 yy_delete_buffer(bs);
78 }
79
80 %}
81
82 %option noyywrap nounput noinput
83
84 ID [a-zA-Z_][a-zA-Z_0-9]*
85 NID [a-zA-Z_0-9]+
86 NUMBER [0-9]+
87
88 %%
89 table return TABLE;
90 type return TYPE;
91 hash return HASH;
92 tree return TREE;
93 static return TSTATIC;
94 dynamic return TDYNAMIC;
95 file return TFILE;
96 map return MAP;
97 "<->" return ARROWBOTH;
98 "<-" return ARROWLEFT;
99 "->" return ARROWRIGHT;
100 "-" return MINUS;
101 procedure return PROCEDURE;
102 \\\n yylineno++; yycolumn = 0;
103 \n yylineno++; yycolumn = 0; return SEPLINE;
104 ; return SEPLINE;
105 name return NAME;
106 group return GROUP;
107 default return DEFAULT;
108 in return IN;
109 out return OUT;
110 forw return FORW;
111 interface return INTERFACE;
112 all return ALL;
113 block return BLOCK;
114 pass return PASS;
115 stateful return STATEFUL;
116 apply return APPLY;
117 final return FINAL;
118 quick return FINAL;
119 on return ON;
120 ifnet return IFNET;
121 inet6 return INET6;
122 inet4 return INET;
123 inet return INET;
124 proto return PROTO;
125 family return FAMILY;
126 tcp return TCP;
127 icmp { yylval.num = IPPROTO_ICMP; return ICMP; }
128 ipv6-icmp { yylval.num = IPPROTO_ICMPV6; return ICMP6; }
129 \"ipv6-icmp\" { yylval.num = IPPROTO_ICMPV6; return ICMP6; }
130 return-rst return RETURNRST;
131 return-icmp return RETURNICMP;
132 return return RETURN;
133 ruleset return GROUP;
134 from return FROM;
135 to return TO;
136 port return PORT;
137 flags return FLAGS;
138 icmp-type return ICMPTYPE;
139 code return CODE;
140 any return ANY;
141
142 "/" return SLASH;
143 "{" return CURLY_OPEN;
144 "}" return CURLY_CLOSE;
145 "(" return PAR_OPEN;
146 ")" return PAR_CLOSE;
147 "," return COMMA;
148 "=" return EQ;
149
150 "0x"[0-9a-fA-F]+ {
151 char *endp, *buf = ecalloc(1, yyleng + 1);
152 buf[yyleng] = 0;
153 yylval.num = strtoul(buf+2, &endp, 16);
154 free(buf);
155 return HEX;
156 }
157
158 {NUMBER}"."{NUMBER} {
159 char *endp, *buf = estrndup(yytext, yyleng);
160 yylval.fpnum = strtod(buf, &endp);
161 free(buf);
162 return FPNUM;
163 }
164
165 [0-9a-fA-F]+":"[0-9a-fA-F:]* {
166 yylval.str = estrndup(yytext, yyleng);
167 return IPV6ADDR;
168 }
169
170 {NUMBER}"."[0-9][0-9.]* {
171 yylval.str = estrndup(yytext, yyleng);
172 return IPV4ADDR;
173 }
174
175 {NUMBER} {
176 char *endp, *buf = estrndup(yytext, yyleng);
177 yylval.num = strtoul(buf, &endp, 10);
178 free(buf);
179 return NUM;
180 }
181
182 "<"{NID}">" {
183 yylval.str = estrndup(yytext + 1, yyleng - 2);
184 return TABLE_ID;
185 }
186
187 "$"{NID} {
188 yylval.str = estrndup(yytext + 1, yyleng - 1);
189 return VAR_ID;
190 }
191
192 {ID} {
193 yylval.str = estrndup(yytext, yyleng);
194 return IDENTIFIER;
195 }
196
197 \"[^\"]*\" {
198 yylval.str = estrndup(yytext + 1, yyleng - 2);
199 return STRING;
200 }
201
202 #.*$ /* drop comment until end of line */
203 [ \t] /* eat whitespace */
204
205 : return COLON;
206