2 * We want to stop processing when we get to the end of the input.
7 * We don't use unput, so don't generate code for it.
12 * We don't read from the terminal.
14 %option never-interactive
17 * Prefix scanner routines with "Dtd_Parse_" rather than "yy", so this scanner
18 * can coexist with other scanners.
20 %option prefix="Dtd_Parse_"
22 %option outfile="dtd_parse.c"
27 * an XML dissector for Wireshark
28 * lexical analyzer for DTDs
30 * Copyright 2004, Luis E. Garcia Ontanon <luis.ontanon@gmail.com>
34 * Wireshark - Network traffic analyzer
35 * By Gerald Combs <gerald@wireshark.org>
36 * Copyright 1998 Gerald Combs
38 * This program is free software; you can redistribute it and/or
39 * modify it under the terms of the GNU General Public License
40 * as published by the Free Software Foundation; either version 2
41 * of the License, or (at your option) any later version.
43 * This program is distributed in the hope that it will be useful,
44 * but WITHOUT ANY WARRANTY; without even the implied warranty of
45 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
46 * GNU General Public License for more details.
48 * You should have received a copy of the GNU General Public License
49 * along with this program; if not, write to the Free Software
50 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
57 #include "dtd_grammar.h"
58 #include "dtd_parse.h"
59 #include "dtd_parse_lex.h"
61 struct _proto_xmlpi_attr {
67 static GString* input_string;
70 static gchar* location;
71 static gchar* attr_name;
73 static int my_yyinput(char* buff,guint size);
75 static dtd_token_data_t* new_token(gchar*);
77 static dtd_build_data_t* build_data;
79 static void set_proto_name (gchar* val) { if(build_data->proto_name) g_free(build_data->proto_name); build_data->proto_name = g_strdup(val); }
80 static void set_media_type (gchar* val) { if(build_data->media_type) g_free(build_data->media_type); build_data->media_type = g_strdup(val); }
81 static void set_proto_root (gchar* val) { if(build_data->proto_root) g_free(build_data->proto_root); build_data->proto_root = g_strdup(val); }
82 static void set_description (gchar* val) { if(build_data->description) g_free(build_data->description); build_data->description = g_strdup(val); }
83 static void set_recursive (gchar* val) { build_data->recursion = ( g_strcasecmp(val,"yes") == 0 ) ? TRUE : FALSE; }
85 static struct _proto_xmlpi_attr proto_attrs[] =
87 { "proto_name", set_proto_name },
88 { "media", set_media_type },
89 { "root", set_proto_root },
90 { "description", set_description },
91 { "hierarchy", set_recursive },
95 #ifdef DEBUG_DTD_PARSER
96 #define DEBUG_DTD_TOKEN fprintf(stderr,"->%s (%i)%s\n",location,token_type,yytext)
98 #define DEBUG_DTD_TOKEN
101 #define DTD_PARSE(token_type) \
103 DtdParse(pParser, (token_type), new_token(yytext), build_data); \
104 if(build_data->error->len > 0) yyterminate(); \
108 #define YY_INPUT(buff,result,max_size) ( (result) = my_yyinput((buff),(max_size)) )
117 location_xmlpi "wireshark:location"
118 protocol_xmlpi "wireshark:protocol"
120 get_attr_quote =[:blank:]*["]
123 get_location_xmlpi [^[:blank:]]+
127 notation_tag "<!"[:blank:]*NOTATION
131 whitespace [[:blank:]\r\n]+
168 name [A-Za-z0-9][-a-zA-Z0-9_]*
172 %START DTD XMLPI LOCATION DONE PROTOCOL GET_ATTR_QUOTE GET_ATTR_VAL GET_ATTR_CLOSE_QUOTE IN_COMMENT IN_NOTATION
178 <DTD>{comment_start} { BEGIN IN_COMMENT; }
181 <IN_COMMENT>{comment_stop} { BEGIN DTD; }
183 <DTD>{notation_tag} { BEGIN IN_NOTATION; }
185 <IN_NOTATION>{special_stop} { BEGIN DTD; }
191 <XMLPI>{location_xmlpi} {
195 <XMLPI>{protocol_xmlpi} {
200 <XMLPI>{stop_xmlpi} BEGIN DTD;
202 <LOCATION>{get_location_xmlpi} {
203 if(location) g_free(location);
204 location = g_strdup(yytext);
208 <DONE>{stop_xmlpi} BEGIN DTD;
211 attr_name = g_strdup(yytext);
212 g_strdown(attr_name);
213 BEGIN GET_ATTR_QUOTE;
216 <GET_ATTR_QUOTE>{get_attr_quote} { BEGIN GET_ATTR_VAL; }
219 g_string_sprintfa(build_data->error,
220 "error in wireshark:protocol xmpli at %s : could not find attribute value!",
225 <GET_ATTR_VAL>[^"]+ {
227 struct _proto_xmlpi_attr* pa;
228 gboolean got_it = FALSE;
230 for(pa = proto_attrs; pa->name; pa++) {
231 if (g_strcasecmp(attr_name,pa->name) == 0) {
239 g_string_sprintfa(build_data->error,
240 "error in wireshark:protocol xmpli at %s : no such parameter %s!",
241 location, attr_name);
248 BEGIN GET_ATTR_CLOSE_QUOTE;
251 <GET_ATTR_CLOSE_QUOTE>{dquote} { BEGIN PROTOCOL;}
253 <PROTOCOL>{stop_xmlpi} BEGIN DTD;
255 <DTD>{special_start} { DTD_PARSE(TOKEN_TAG_START); }
256 <DTD>{special_stop} { DTD_PARSE(TOKEN_TAG_STOP); }
258 <DTD>{attlist_kw} { DTD_PARSE(TOKEN_ATTLIST_KW); }
259 <DTD>{element_kw} { DTD_PARSE(TOKEN_ELEMENT_KW); }
260 <DTD>{doctype_kw} { DTD_PARSE(TOKEN_DOCTYPE_KW); }
262 <DTD>{pcdata} { DTD_PARSE(TOKEN_ELEM_DATA); }
263 <DTD>{any} { DTD_PARSE(TOKEN_ELEM_DATA); }
264 <DTD>{cdata} { DTD_PARSE(TOKEN_ELEM_DATA); }
265 <DTD>{empty} { DTD_PARSE(TOKEN_EMPTY_KW); }
267 <DTD>{iD} { DTD_PARSE(TOKEN_ATT_TYPE); }
268 <DTD>{idref} { DTD_PARSE(TOKEN_ATT_TYPE); }
269 <DTD>{idrefs} { DTD_PARSE(TOKEN_ATT_TYPE); }
270 <DTD>{nmtoken} { DTD_PARSE(TOKEN_ATT_TYPE); }
271 <DTD>{nmtokens} { DTD_PARSE(TOKEN_ATT_TYPE); }
272 <DTD>{entity} { DTD_PARSE(TOKEN_ATT_TYPE); }
273 <DTD>{entities} { DTD_PARSE(TOKEN_ATT_TYPE); }
274 <DTD>{notation} { DTD_PARSE(TOKEN_ATT_TYPE); }
275 <DTD>{cdata_t} { DTD_PARSE(TOKEN_ATT_TYPE); }
276 <DTD>{defaulT} { DTD_PARSE(TOKEN_ATT_DEF_WITH_VALUE); }
277 <DTD>{fixed} { DTD_PARSE(TOKEN_ATT_DEF_WITH_VALUE); }
278 <DTD>{required} { DTD_PARSE(TOKEN_ATT_DEF); }
279 <DTD>{implied} { DTD_PARSE(TOKEN_ATT_DEF); }
281 <DTD>{star} { DTD_PARSE(TOKEN_STAR); }
282 <DTD>{question} { DTD_PARSE(TOKEN_QUESTION); }
283 <DTD>{plus} { DTD_PARSE(TOKEN_PLUS); }
284 <DTD>{comma} { DTD_PARSE(TOKEN_COMMA); }
285 <DTD>{open_parens} { DTD_PARSE(TOKEN_OPEN_PARENS); }
286 <DTD>{close_parens} { DTD_PARSE(TOKEN_CLOSE_PARENS); }
287 <DTD>{open_bracket} { DTD_PARSE(TOKEN_OPEN_BRACKET); }
288 <DTD>{close_bracket} { DTD_PARSE(TOKEN_CLOSE_BRACKET); }
289 <DTD>{pipe} { DTD_PARSE(TOKEN_PIPE); }
292 <DTD>{squoted} { DTD_PARSE(TOKEN_QUOTED); }
293 <DTD>{name} { DTD_PARSE(TOKEN_NAME); }
297 static dtd_token_data_t* new_token(gchar* text) {
298 dtd_token_data_t* t = g_malloc(sizeof(dtd_token_data_t));
300 t->text = g_strdup(text);
301 t->location = g_strdup(location);
307 static int my_yyinput(char* buff, guint size) {
309 if (offset >= len ) {
311 } else if ( offset + size <= len ) {
312 memcpy(buff, input_string->str + offset,size);
317 memcpy(buff, input_string->str + offset,size);
323 extern dtd_build_data_t* dtd_parse(GString* s) {
327 len = input_string->len;
329 pParser = DtdParseAlloc(g_malloc);
331 #ifdef DEBUG_DTD_PARSER
332 DtdParseTrace(stderr, ">>");
335 build_data = g_malloc(sizeof(dtd_build_data_t));
337 build_data->proto_name = NULL;
338 build_data->media_type = NULL;
339 build_data->description = NULL;
340 build_data->proto_root = NULL;
341 build_data->recursion = FALSE;
343 build_data->elements = g_ptr_array_new();
344 build_data->attributes = g_ptr_array_new();
346 build_data->error = g_string_new("");
354 DtdParse(pParser, 0, NULL,build_data);
358 if (location) g_free(location);
362 DtdParseFree(pParser, g_free );