npf_scan.l revision 1.4 1 /* $NetBSD: npf_scan.l,v 1.4 2012/07/01 23:21:07 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 return-rst return RETURNRST;
90 return-icmp return RETURNICMP;
91 return return RETURN;
92 from return FROM;
93 to return TO;
94 port return PORT;
95 flags return FLAGS;
96 icmp-type return ICMPTYPE;
97 code return CODE;
98 any return ANY;
99
100 "/" return SLASH;
101 "{" return CURLY_OPEN;
102 "}" return CURLY_CLOSE;
103 "(" return PAR_OPEN;
104 ")" return PAR_CLOSE;
105 "," return COMMA;
106 "=" return EQ;
107
108 "0x"[0-9a-fA-F]+ {
109 char *endp, *buf = zalloc(yyleng + 1);
110 buf[yyleng] = 0;
111 yylval.num = strtoul(buf+2, &endp, 16);
112 free(buf);
113 return HEX;
114 }
115
116 [0-9a-fA-F]+":"[0-9a-fA-F:]* {
117 yylval.str = xstrndup(yytext, yyleng);
118 return IPV6ADDR;
119 }
120
121 {NUMBER}"."[0-9][0-9.]* {
122 yylval.str = xstrndup(yytext, yyleng);
123 return IPV4ADDR;
124 }
125
126 {NUMBER} {
127 char *endp, *buf = xstrndup(yytext, yyleng);
128 yylval.num = strtoul(buf, &endp, 10);
129 free(buf);
130 return NUM;
131 }
132
133 "<"{NID}">" {
134 yylval.str = xstrndup(yytext + 1, yyleng - 2);
135 return TABLE_ID;
136 }
137
138 "$"{NID} {
139 yylval.str = xstrndup(yytext + 1, yyleng - 1);
140 return VAR_ID;
141 }
142
143 {ID} {
144 yylval.str = xstrndup(yytext, yyleng);
145 return IDENTIFIER;
146 }
147
148 \"[^\"]*\" {
149 yylval.str = xstrndup(yytext + 1, yyleng - 2);
150 return STRING;
151 }
152
153 #.*$ /* drop comment until end of line */
154 [ \t] /* eat whitespace */
155
156 : return COLON;
157