1 /* $NetBSD: token.l,v 1.2 2011/05/24 13:41:53 joerg Exp $ */
2 
3 /*-
4  * Copyright (c) 2010 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 
30 %{
31 #include <sys/cdefs.h>
32 
33 #ifndef lint
34 __RCSID("$NetBSD: token.l,v 1.2 2011/05/24 13:41:53 joerg Exp $");
35 #endif
36 
37 #include <stdlib.h>
38 #include <limits.h>
39 #include <inttypes.h>
40 #include <string.h>
41 #include <errno.h>
42 
43 #include <net/if.h>
44 #include <netinet/in.h>
45 #include <net/pfvar.h>
46 
47 #include "parse.h"
48 #include "parser.h"
49 
50 %}
51 
52 %option nounput
53 %option noinput
54 
55 %%
56 
57 state               { return STATE;}
58 on                            { return ON;}
59 out                           { return OUT;}
60 in                            { return IN;}
61 proto     { return PROTO;}
62 from                { return FROM;}
63 to                            { return TO;}
64 using     { return USING;}
65 id                            { return ID;}
66 cid                           { return CID;}
67 expire    { return EXPIRE;}
68 timeout     { return TIMEOUT;}
69 src                           { return SRC;}
70 dst                           { return DST;}
71 seq                           { return SEQ;}
72 max_win   { return MAX_WIN;}
73 wscale              { return WSCALE;}
74 mss                           { return MSS;}
75 no-scrub  { return NOSCRUB;}
76 scrub               { return SCRUB;}
77 flags               { return FLAGS;}
78 ttl                           { return TTL;}
79 mode                { return MODE;}
80 [0-9]+              { char *ep;
81                                 errno = 0;
82                                 yylval.num = strtoumax(yytext, &ep, 10);
83                                 if (errno == ERANGE && yylval.num == UINTMAX_MAX)
84                                                   yyfatal("Number out of range");
85                                 return NUMBER;
86                               }
87 
88 [A-Za-z0-9:\[][A-Za-z0-9\[\]_:%\.-]* { yylval.str = strdup(yytext);
89                                                                           if (yylval.str == NULL)
90                                                                                           yyfatal("Not enough memory");
91                                                                                 return STRING;
92                                                                         }
93 
94 
95 \n                            { lineno ++; }
96 
97 %%
98 
99 
100 void
101 yyfatal(const char *s)
102 {
103           yyerror(s);
104           exit(EXIT_FAILURE);
105 }
106 
107 void
108 yyerror(const char *s)
109 {
110           printf("line %d: %s at [%s]\n", lineno, s, yytext);
111 }
112 
113 
114 int
115 parse(FILE *fp, struct pfioc_states* s)
116 {
117           yyin = fp;
118 
119           lineno = 1;
120 
121           states = s;
122           allocated = 0;
123           memset(s, 0, sizeof(*s));
124 
125           if (yyparse()) {
126                     printf("parse failed, line %d.\n", lineno);
127                     return(-1);
128           }
129 
130           return(0);
131 }
132