3 %option never-interactive
9 * lexical analyzer for MATE configuration files
11 * Copyright 2004, Luis E. Garcia Ontanon <luis.ontanon@gmail.com>
15 * Wireshark - Network traffic analyzer
16 * By Gerald Combs <gerald@wireshark.org>
17 * Copyright 1998 Gerald Combs
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.
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.
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.
35 #include "mate_grammar.h"
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*);
42 #define MAX_INCLUDE_DEPTH 10
43 static YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH];
44 static int include_stack_ptr = 0;
47 static mate_config_frame* current_frame;
49 static mate_config* mc;
51 #define MATE_PARSE(token_type) MateParser(pParser, (token_type), g_strdup(yytext), mc );
58 transform_kw Transform
73 show_times_kw ShowTimes
74 expiration_kw Expiration
75 idle_timeout_kw IdleTimeout
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]
85 transport_kw Transport
91 drop_unassigned_kw DropUnassigned
92 discard_pdu_data_kw DiscardPduData
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:]*
115 name [a-z][-\.a-zA-Z0-9_]*
116 avp_operator [$^~=<>!]
121 filename [-A-Za-z0-9_/.]+
123 whitespace [[:blank:]\r]+
132 %START OUTSIDE QUOTED INCLUDING COMMENT
135 {newline} current_frame->linenum++;
138 <OUTSIDE>{include} BEGIN INCLUDING;
140 <INCLUDING>{filename} {
141 if ( include_stack_ptr >= MAX_INCLUDE_DEPTH )
142 g_error("dtd_preparse: include files nested to deeply");
144 include_stack[include_stack_ptr++] = YY_CURRENT_BUFFER;
145 yyin = fopen( yytext, "r" );
148 yy_delete_buffer( YY_CURRENT_BUFFER );
150 /* coverity[negative_sink] */
151 yy_switch_to_buffer(include_stack[--include_stack_ptr] );
154 g_string_sprintfa(mc->config_error, "Mate parser: Could not open file: '%s': %s", yytext, strerror(errno) );
158 current_frame = g_malloc(sizeof(mate_config_frame));
159 current_frame->filename = g_strdup(yytext);
160 current_frame->linenum = 1;
162 g_ptr_array_add(mc->config_stack,current_frame);
164 yy_switch_to_buffer(yy_create_buffer( yyin, YY_BUF_SIZE ) );
171 /* coverity[check_after_sink] */
172 if ( --include_stack_ptr < 0 ) {
175 yy_delete_buffer( YY_CURRENT_BUFFER );
176 yy_switch_to_buffer( include_stack[include_stack_ptr] );
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);
186 <OUTSIDE>{blk_cmnt_start} BEGIN COMMENT;
187 <COMMENT>{cmnt_char} ;
188 <COMMENT>{blk_cmnt_stop} BEGIN OUTSIDE;
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);
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);
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);
251 <OUTSIDE>{quote} BEGIN QUOTED;
252 <QUOTED>{not_quoted} MATE_PARSE(TOKEN_QUOTED);
253 <QUOTED>{quote} BEGIN OUTSIDE;
257 extern gboolean mate_load_config(const gchar* filename, mate_config* matecfg) {
258 volatile gboolean state = TRUE;
261 yyin = fopen(filename,"r");
264 g_string_sprintfa(mc->config_error,"Mate parser: Could not open file: '%s', error: %s", filename, strerror(errno) );
268 mc->config_stack = g_ptr_array_new();
270 current_frame = g_malloc(sizeof(mate_config_frame));
271 current_frame->filename = g_strdup(filename);
272 current_frame->linenum = 1;
274 g_ptr_array_add(mc->config_stack,current_frame);
276 pParser = MateParserAlloc(g_malloc);
278 /* MateParserTrace(stdout,""); */
285 MateParser(pParser, 0, NULL,mc);
289 MateParserFree(pParser, g_free );
291 g_free(current_frame->filename);
292 g_free(current_frame);
294 g_ptr_array_free(mc->config_stack,FALSE);
295 } CATCH(MateConfigError) {
299 g_string_sprintfa(mc->config_error,"An unexpected error occurred");