npf_scan.l revision 1.3 1 /* $NetBSD: npf_scan.l,v 1.3 2012/06/15 23:24:08 rmind 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 <err.h>
35
36 #include "npfctl.h"
37 #include "npf_parse.h"
38
39 int yycolumn;
40
41 #define YY_USER_ACTION yycolumn += yyleng;
42
43 %}
44
45 %option noyywrap nounput noinput
46
47 ID [a-zA-Z_][a-zA-Z_0-9]*
48 NID [a-zA-Z_0-9]+
49 NUMBER [0-9]+
50
51 %%
52 table return TABLE;
53 type return TYPE;
54 hash return HASH;
55 tree return TREE;
56 static return TSTATIC;
57 dynamic return TDYNAMIC;
58 file return TFILE;
59 map return MAP;
60 "<->" return ARROWBOTH;
61 "<-" return ARROWLEFT;
62 "->" return ARROWRIGHT;
63 "-" return MINUS;
64 procedure return PROCEDURE;
65 \\\n yylineno++; yycolumn = 0;
66 \n yylineno++; yycolumn = 0; return SEPLINE;
67 ; return SEPLINE;
68 name return NAME;
69 group return GROUP;
70 default return DEFAULT;
71 in return IN;
72 out return OUT;
73 interface return INTERFACE;
74 all return ALL;
75 block return BLOCK;
76 pass return PASS;
77 stateful return STATEFUL;
78 apply return APPLY;
79 final return FINAL;
80 quick return FINAL;
81 on return ON;
82 inet6 return INET6;
83 inet4 return INET;
84 inet return INET;
85 proto return PROTO;
86 family return FAMILY;
87 tcp return TCP;
88 icmp return ICMP;
89 udp return UDP;
90 return-rst return RETURNRST;
91 return-icmp return RETURNICMP;
92 return return RETURN;
93 from return FROM;
94 to return TO;
95 port return PORT;
96 flags return FLAGS;
97 icmp-type return ICMPTYPE;
98 code return CODE;
99 any return ANY;
100
101 "/" return SLASH;
102 "{" return CURLY_OPEN;
103 "}" return CURLY_CLOSE;
104 "(" return PAR_OPEN;
105 ")" return PAR_CLOSE;
106 "," return COMMA;
107 "=" return EQ;
108
109 "0x"[0-9a-fA-F]+ {
110 char *endp, *buf = zalloc(yyleng + 1);
111 buf[yyleng] = 0;
112 yylval.num = strtoul(buf+2, &endp, 16);
113 free(buf);
114 return HEX;
115 }
116
117 [0-9a-fA-F]+":"[0-9a-fA-F:]* {
118 yylval.str = xstrndup(yytext, yyleng);
119 return IPV6ADDR;
120 }
121
122 {NUMBER}"."[0-9][0-9.]* {
123 yylval.str = xstrndup(yytext, yyleng);
124 return IPV4ADDR;
125 }
126
127 {NUMBER} {
128 char *endp, *buf = xstrndup(yytext, yyleng);
129 yylval.num = strtoul(buf, &endp, 10);
130 free(buf);
131 return NUM;
132 }
133
134 "<"{NID}">" {
135 yylval.str = xstrndup(yytext + 1, yyleng - 2);
136 return TABLE_ID;
137 }
138
139 "$"{NID} {
140 yylval.str = xstrndup(yytext + 1, yyleng - 1);
141 return VAR_ID;
142 }
143
144 {ID} {
145 yylval.str = xstrndup(yytext, yyleng);
146 return IDENTIFIER;
147 }
148
149 \"[^\"]*\" {
150 yylval.str = xstrndup(yytext + 1, yyleng - 2);
151 return STRING;
152 }
153
154 #.*$ /* drop comment until end of line */
155 [ \t] /* eat whitespace */
156
157 : return COLON;
158