name change
[obnox/wireshark/wip.git] / plugins / mate / mate_parser.l
1 %option noyywrap
2 %option nounput 
3 %option never-interactive
4 %option prefix="Mate"
5
6 %{
7
8         /* mate_parser.l
9         * lexical analyzer for MATE configuration files
10         *
11         * Copyright 2004, Luis E. Garcia Ontanon <luis.ontanon@gmail.com>
12         *
13         * $Id$
14         *
15         * Wireshark - Network traffic analyzer
16         * By Gerald Combs <gerald@wireshark.org>
17         * Copyright 1998 Gerald Combs
18         *
19         * This program is free software; you can redistribute it and/or
20         * modify it under the terms of the GNU General Public License
21         * as published by the Free Software Foundation; either version 2
22         * of the License, or (at your option) any later version.
23         * 
24         * This program is distributed in the hope that it will be useful,
25         * but WITHOUT ANY WARRANTY; without even the implied warranty of
26         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27         * GNU General Public License for more details.
28         * 
29         * You should have received a copy of the GNU General Public License
30         * along with this program; if not, write to the Free Software
31         * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32         */
33         
34 #include "mate.h"       
35 #include "mate_grammar.h"
36         
37         void MateParser(void*,int, gchar*, mate_config* matecfg);
38         void *MateParserAlloc(void *(*)(gulong));
39         void MateParserFree( void*, void(*)(void*) );
40         void MateParseTrace(FILE*,char*);
41         
42 #define MAX_INCLUDE_DEPTH 10
43         static YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH];
44         static int include_stack_ptr = 0;
45         
46         static void* pParser;
47         static mate_config_frame* current_frame;
48         
49         static mate_config* mc;
50         
51 #define MATE_PARSE(token_type) MateParser(pParser, (token_type), g_strdup(yytext), mc );
52
53 %}
54
55 pdu_kw                          Pdu
56 gop_kw                          Gop
57 gog_kw                          Gog
58 transform_kw            Transform
59 match_kw                        Match
60 always_kw                       Always
61 strict_kw                       Strict
62 every_kw                        Every
63 loose_kw                        Loose
64 replace_kw                      Replace
65 insert_kw                       Insert
66 gop_tree_kw                     GopTree
67 member_kw                       Member
68 on_kw                           On
69 start_kw                        Start
70 stop_kw                         Stop
71 extra_kw                        Extra
72 show_tree_kw            ShowTree
73 show_times_kw           ShowTimes
74 expiration_kw           Expiration
75 idle_timeout_kw         IdleTimeout
76 lifetime_kw                     Lifetime
77 no_tree_kw                      NoTree
78 pdu_tree_kw                     PduTree
79 frame_tree_kw           FrameTree
80 basic_tree_kw           BasicTree
81 true_kw                         [Tt][Rr][Uu][Ee]
82 false_kw                        [Ff][Aa][Ll][Ss][Ee]
83 proto_kw                        Proto
84 payload_kw          Payload
85 transport_kw            Transport
86 criteria_kw                     Criteria
87 accept_kw                       Accept
88 reject_kw                       Reject
89 extract_kw                      Extract
90 from_kw                         From
91 drop_unassigned_kw  DropUnassigned
92 discard_pdu_data_kw DiscardPduData
93 last_pdu_kw                     LastPdu
94 done_kw                         Done
95 filename_kw         Filename
96 debug_kw            Debug
97 level_kw            Level
98 default_kw          Default
99
100
101 open_parens                     "("
102 close_parens            ")"
103 open_brace                      "{"
104 close_brace                     "}"
105 comma                           ","
106 semicolon                       ";"
107 slash                           "/"
108 pipe                            "|"
109
110 integer                         [0-9]+
111 floating                        ([0-9]+\.[0-9]+)
112 doted_ip                        [0-9][0-9]?[0-9]?\.[0-9][0-9]?[0-9]?\.[0-9][0-9]?[0-9]?\.[0-9][0-9]?[0-9]?
113 colonized                       [0-9A-Fa-f:]*[:][0-9A-Fa-f:]*
114
115 name                            [a-z][-\.a-zA-Z0-9_]*
116 avp_operator            [$^~=<>!]
117 quote                           ["]
118 not_quoted                      [^"]*
119
120 include                 "#include"
121 filename                [-A-Za-z0-9_/.]+
122
123 whitespace              [[:blank:]\r]+
124 newline                 \n
125
126 comment                 "//"[^\n]*\n
127
128 blk_cmnt_start  "/*"
129 cmnt_char               .
130 blk_cmnt_stop  "*/"
131  
132 %START OUTSIDE QUOTED INCLUDING COMMENT
133 %%
134
135 {newline}                                               current_frame->linenum++; 
136 {whitespace}                                    ;
137
138 <OUTSIDE>{include}                                      BEGIN INCLUDING;
139
140 <INCLUDING>{filename}                   {
141         if ( include_stack_ptr >= MAX_INCLUDE_DEPTH )
142                 g_error("dtd_preparse: include files nested to deeply");
143         
144         include_stack[include_stack_ptr++] = YY_CURRENT_BUFFER;
145         yyin = fopen( yytext, "r" );
146
147         if (!yyin) {
148                 yy_delete_buffer( YY_CURRENT_BUFFER );
149                 
150                 /* coverity[negative_sink] */
151                 yy_switch_to_buffer(include_stack[--include_stack_ptr] );
152                 
153                 if (errno)
154                         g_string_sprintfa(mc->config_error, "Mate parser: Could not open file: '%s': %s", yytext, strerror(errno) );
155                 
156         } else {
157                 
158                 current_frame = g_malloc(sizeof(mate_config_frame));
159                 current_frame->filename = g_strdup(yytext);
160                 current_frame->linenum = 1;
161                 
162                 g_ptr_array_add(mc->config_stack,current_frame);
163
164                 yy_switch_to_buffer(yy_create_buffer( yyin, YY_BUF_SIZE ) );
165         }
166         
167         BEGIN OUTSIDE;
168 }
169
170 <<EOF>> {       
171         /* coverity[check_after_sink] */
172         if ( --include_stack_ptr < 0 ) {
173                 yyterminate();
174         } else {
175                 yy_delete_buffer( YY_CURRENT_BUFFER );
176                 yy_switch_to_buffer( include_stack[include_stack_ptr] );
177                 
178                 g_free(current_frame->filename);
179                 g_free(current_frame);
180                 current_frame = g_ptr_array_remove_index(mc->config_stack,mc->config_stack->len-1);
181         }
182 }                               
183
184 <OUTSIDE>{comment}                                      ;
185
186 <OUTSIDE>{blk_cmnt_start}                       BEGIN COMMENT;
187 <COMMENT>{cmnt_char}                    ;
188 <COMMENT>{blk_cmnt_stop}                BEGIN OUTSIDE;
189
190 <OUTSIDE>{pdu_kw}                                       MATE_PARSE(TOKEN_PDU_KW);
191 <OUTSIDE>{gop_kw}                                       MATE_PARSE(TOKEN_GOP_KW);
192 <OUTSIDE>{gog_kw}                                       MATE_PARSE(TOKEN_GOG_KW);
193 <OUTSIDE>{transform_kw}                         MATE_PARSE(TOKEN_TRANSFORM_KW);
194 <OUTSIDE>{match_kw}                                     MATE_PARSE(TOKEN_MATCH_KW);
195 <OUTSIDE>{strict_kw}                            MATE_PARSE(TOKEN_STRICT_KW);
196 <OUTSIDE>{every_kw}                                     MATE_PARSE(TOKEN_EVERY_KW);
197 <OUTSIDE>{loose_kw}                                     MATE_PARSE(TOKEN_LOOSE_KW);
198 <OUTSIDE>{replace_kw}                           MATE_PARSE(TOKEN_REPLACE_KW);
199 <OUTSIDE>{insert_kw}                            MATE_PARSE(TOKEN_INSERT_KW);
200 <OUTSIDE>{gop_tree_kw}                          MATE_PARSE(TOKEN_GOP_TREE_KW);
201 <OUTSIDE>{member_kw}                            MATE_PARSE(TOKEN_MEMBER_KW);
202 <OUTSIDE>{on_kw}                                        MATE_PARSE(TOKEN_ON_KW);
203 <OUTSIDE>{start_kw}                                     MATE_PARSE(TOKEN_START_KW);
204 <OUTSIDE>{stop_kw}                                      MATE_PARSE(TOKEN_STOP_KW);
205 <OUTSIDE>{extra_kw}                                     MATE_PARSE(TOKEN_EXTRA_KW);
206 <OUTSIDE>{show_tree_kw}                         MATE_PARSE(TOKEN_SHOW_TREE_KW);
207 <OUTSIDE>{show_times_kw}                        MATE_PARSE(TOKEN_SHOW_TIMES_KW);
208 <OUTSIDE>{expiration_kw}                        MATE_PARSE(TOKEN_EXPIRATION_KW);
209 <OUTSIDE>{idle_timeout_kw}                      MATE_PARSE(TOKEN_IDLE_TIMEOUT_KW);
210 <OUTSIDE>{lifetime_kw}                          MATE_PARSE(TOKEN_LIFETIME_KW);
211 <OUTSIDE>{no_tree_kw}                           MATE_PARSE(TOKEN_NO_TREE_KW);
212 <OUTSIDE>{pdu_tree_kw}                          MATE_PARSE(TOKEN_PDU_TREE_KW);
213 <OUTSIDE>{frame_tree_kw}                        MATE_PARSE(TOKEN_FRAME_TREE_KW);
214 <OUTSIDE>{basic_tree_kw}                        MATE_PARSE(TOKEN_BASIC_TREE_KW);
215 <OUTSIDE>{true_kw}                                      MATE_PARSE(TOKEN_TRUE_KW);
216 <OUTSIDE>{false_kw}                                     MATE_PARSE(TOKEN_FALSE_KW);
217 <OUTSIDE>{proto_kw}                                     MATE_PARSE(TOKEN_PROTO_KW);
218 <OUTSIDE>{payload_kw}                           MATE_PARSE(TOKEN_PAYLOAD_KW);
219 <OUTSIDE>{transport_kw}                         MATE_PARSE(TOKEN_TRANSPORT_KW);
220 <OUTSIDE>{criteria_kw}                          MATE_PARSE(TOKEN_CRITERIA_KW);
221 <OUTSIDE>{accept_kw}                            MATE_PARSE(TOKEN_ACCEPT_KW);
222 <OUTSIDE>{reject_kw}                            MATE_PARSE(TOKEN_REJECT_KW);
223 <OUTSIDE>{extract_kw}                           MATE_PARSE(TOKEN_EXTRACT_KW);
224 <OUTSIDE>{from_kw}                                      MATE_PARSE(TOKEN_FROM_KW);
225 <OUTSIDE>{drop_unassigned_kw}           MATE_PARSE(TOKEN_DROP_UNASSIGNED_KW);
226 <OUTSIDE>{discard_pdu_data_kw}          MATE_PARSE(TOKEN_DISCARD_PDU_DATA_KW);
227 <OUTSIDE>{last_pdu_kw}                          MATE_PARSE(TOKEN_LAST_PDU_KW);
228 <OUTSIDE>{done_kw}                                      MATE_PARSE(TOKEN_DONE_KW);
229 <OUTSIDE>{filename_kw}                          MATE_PARSE(TOKEN_FILENAME_KW);
230 <OUTSIDE>{debug_kw}                                     MATE_PARSE(TOKEN_DEBUG_KW);
231 <OUTSIDE>{level_kw}                                     MATE_PARSE(TOKEN_LEVEL_KW);
232 <OUTSIDE>{default_kw}                           MATE_PARSE(TOKEN_DEFAULT_KW);
233
234 <OUTSIDE>{open_parens}                          MATE_PARSE(TOKEN_OPEN_PARENS);
235 <OUTSIDE>{close_parens}                         MATE_PARSE(TOKEN_CLOSE_PARENS);
236 <OUTSIDE>{open_brace}                           MATE_PARSE(TOKEN_OPEN_BRACE);
237 <OUTSIDE>{close_brace}                          MATE_PARSE(TOKEN_CLOSE_BRACE);
238 <OUTSIDE>{comma}                                        MATE_PARSE(TOKEN_COMMA);
239 <OUTSIDE>{semicolon}                            MATE_PARSE(TOKEN_SEMICOLON);
240 <OUTSIDE>{slash}                                        MATE_PARSE(TOKEN_SLASH);
241 <OUTSIDE>{pipe}                                         MATE_PARSE(TOKEN_PIPE);
242
243 <OUTSIDE>{integer}                                      MATE_PARSE(TOKEN_INTEGER);
244 <OUTSIDE>{floating}                                     MATE_PARSE(TOKEN_FLOATING);
245 <OUTSIDE>{doted_ip}                                     MATE_PARSE(TOKEN_DOTED_IP);
246 <OUTSIDE>{colonized}                            MATE_PARSE(TOKEN_COLONIZED);
247 <OUTSIDE>{name}                                         MATE_PARSE(TOKEN_NAME);
248 <OUTSIDE>{avp_operator}                         MATE_PARSE(TOKEN_AVP_OPERATOR);
249
250
251 <OUTSIDE>{quote}                                        BEGIN QUOTED;
252 <QUOTED>{not_quoted}                    MATE_PARSE(TOKEN_QUOTED);
253 <QUOTED>{quote}                                 BEGIN OUTSIDE;
254
255 %%
256
257 extern gboolean mate_load_config(const gchar* filename, mate_config* matecfg) {
258         volatile gboolean state = TRUE;
259         mc = matecfg;
260
261         yyin = fopen(filename,"r");
262         
263         if (!yyin) {
264                 g_string_sprintfa(mc->config_error,"Mate parser: Could not open file: '%s', error: %s", filename, strerror(errno) );
265                 return FALSE;
266         }
267         
268         mc->config_stack = g_ptr_array_new();
269         
270         current_frame = g_malloc(sizeof(mate_config_frame));
271         current_frame->filename = g_strdup(filename);
272         current_frame->linenum = 1;
273         
274         g_ptr_array_add(mc->config_stack,current_frame);
275
276         pParser = MateParserAlloc(g_malloc);
277         
278         /* MateParserTrace(stdout,""); */
279         
280         TRY {
281                 BEGIN OUTSIDE;
282
283                 yylex();
284
285                 MateParser(pParser, 0, NULL,mc);
286
287                 yyrestart(NULL);
288         
289                 MateParserFree(pParser, g_free );
290         
291                 g_free(current_frame->filename);
292                 g_free(current_frame);
293         
294                 g_ptr_array_free(mc->config_stack,FALSE);
295         } CATCH(MateConfigError) {
296                 state = FALSE;
297         } CATCH_ALL {
298                 state = FALSE;          
299                 g_string_sprintfa(mc->config_error,"An unexpected error occurred");
300         }
301         ENDTRY;
302         
303         return state;
304 }