avoid freeing the last location twice.
[obnox/wireshark/wip.git] / epan / dtd_parse.l
1 %option noyywrap
2 %option nounput 
3 %option outfile="dtd_parse.c"
4 %option prefix="Dtd_Parse_"
5 %option never-interactive
6
7 %{
8
9         /* dtd_parse.l
10         * an XML dissector for ethereal 
11         * lexical analyzer for DTDs
12         *
13         * Copyright 2004, Luis E. Garcia Ontanon <luis.ontanon@gmail.com>
14         *
15         * $Id$
16         *
17         * Ethereal - Network traffic analyzer
18         * By Gerald Combs <gerald@ethereal.com>
19         * Copyright 1998 Gerald Combs
20         *
21         * This program is free software; you can redistribute it and/or
22         * modify it under the terms of the GNU General Public License
23         * as published by the Free Software Foundation; either version 2
24         * of the License, or (at your option) any later version.
25         * 
26         * This program is distributed in the hope that it will be useful,
27         * but WITHOUT ANY WARRANTY; without even the implied warranty of
28         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29         * GNU General Public License for more details.
30         * 
31         * You should have received a copy of the GNU General Public License
32         * along with this program; if not, write to the Free Software
33         * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
34         */
35         
36 #include <glib.h>
37 #include <string.h>
38         
39 #include "dtd.h"
40 #include "dtd_grammar.h"
41 #include "dtd_parse.h"
42         
43         struct _proto_xmlpi_attr {
44                 gchar* name;
45                 void (*act)(gchar*);
46         };
47
48         void* pParser;
49         GString* input_string;  
50         guint offset;
51         guint len;
52         gchar* location;
53         gchar* attr_name;
54         
55         static int my_yyinput(char* buff,guint size);
56         
57         static dtd_token_data_t* new_token(gchar*);
58
59         static dtd_build_data_t* build_data;
60         
61         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); }
62         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); }
63         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); }
64         static void set_description (gchar* val) { if(build_data->description) g_free(build_data->description); build_data->description = g_strdup(val); }
65         static void set_recursive (gchar* val) { build_data->recursion = ( g_strcasecmp(val,"yes") == 0 ) ? TRUE : FALSE; }
66
67         struct _proto_xmlpi_attr proto_attrs[] =
68         {
69                 { "proto_name", set_proto_name },
70                 { "media", set_media_type },
71                 { "root", set_proto_root },
72                 { "description", set_description },
73         { "hierarchy", set_recursive },
74                 {NULL,NULL}
75         };
76         
77 #ifdef DEBUG_DTD_PARSER
78 #define DEBUG_DTD_TOKEN fprintf(stderr,"->%s (%i)%s\n",location,token_type,yytext)
79 #else
80 #define DEBUG_DTD_TOKEN
81 #endif
82     
83 #define DTD_PARSE(token_type) \
84         {   DEBUG_DTD_TOKEN; \
85         build_data->location = location; \
86                 DtdParse(pParser, (token_type), new_token(yytext), build_data); \
87                 if(build_data->error->len > 0) yyterminate(); \
88         }
89
90
91 #define YY_INPUT(buff,result,max_size) ( (result) = my_yyinput((buff),(max_size)) )
92
93 %}
94
95 comment_start "<!--"
96 comment_stop "-->"
97
98 start_xmlpi "<?"
99
100 location_xmlpi "ethereal:location"
101 protocol_xmlpi "ethereal:protocol"
102
103 get_attr_quote =[:blank:]*["]
104 avoid_editor_bug ["]
105
106 get_location_xmlpi  [^[:blank:]]+
107
108 stop_xmlpi "?>"
109
110 special_start  "<!"
111 special_stop   ">"
112 whitespace     [[:blank:]\r\n]+
113 newline        \n
114 attlist_kw     ATTLIST
115 doctype_kw     DOCTYPE
116 element_kw     ELEMENT
117
118 pcdata         #PCDATA
119 any            ANY
120 cdata          #CDATA
121
122 iD             ID
123 idref          IDREF
124 idrefs         IDREFS
125 nmtoken        NMTOKEN
126 nmtokens       NMTOKENS
127 entity         ENTITY
128 entities       ENTITIES
129 notation       NOTATION
130 cdata_t        CDATA
131
132 empty          EMPTY
133 defaulT        #DEFAULT
134 fixed          #FIXED
135 required       #REQUIRED
136 implied        #IMPLIED
137
138 star           "*"
139 question       "?"
140 plus           "+"
141 open_parens    "("
142 close_parens   ")"
143 open_bracket   "["
144 close_bracket  "]"
145 comma          ","
146 pipe           "|"
147 dquote         ["]
148
149 name           [a-z][-a-zA-Z0-9_]*
150 dquoted        ["][^\"]*["]
151 squoted        ['][^\']*[']
152
153 %START DTD XMLPI LOCATION DONE PROTOCOL GET_ATTR_QUOTE GET_ATTR_VAL GET_ATTR_CLOSE_QUOTE IN_COMMENT
154 %%
155
156 {whitespace}            ;
157
158
159 <DTD>{comment_start}            { BEGIN IN_COMMENT; }
160 <IN_COMMENT>[^-]?                               |
161 <IN_COMMENT>[-]                                 ;
162 <IN_COMMENT>{comment_stop}              { BEGIN DTD; }
163
164 <DTD>{start_xmlpi}              {
165         BEGIN XMLPI;
166 }
167
168 <XMLPI>{location_xmlpi} {
169         if(location) g_free(location);
170         BEGIN LOCATION;
171 }
172
173 <XMLPI>{protocol_xmlpi} {
174         BEGIN PROTOCOL;
175 }
176
177 <XMLPI><.> ;
178 <XMLPI>{stop_xmlpi} BEGIN DTD;
179
180 <LOCATION>{get_location_xmlpi} {
181         location = g_strdup(yytext);
182         BEGIN DONE;
183 }
184
185 <DONE>{stop_xmlpi}  BEGIN DTD;
186
187 <PROTOCOL>{name} {
188         attr_name = g_strdup(yytext);
189     g_strdown(attr_name);
190         BEGIN GET_ATTR_QUOTE;
191 }
192
193 <GET_ATTR_QUOTE>{get_attr_quote} { BEGIN GET_ATTR_VAL; }
194
195 <GET_ATTR_QUOTE>. {
196         g_string_sprintfa(build_data->error,
197                                         "error in ethereal:protocol xmpli at %s : could not find attribute value!",
198                                         location);
199         yyterminate();
200 }
201
202 <GET_ATTR_VAL>[^"]+ {
203         /*"*/
204         struct _proto_xmlpi_attr* pa;
205         gboolean got_it = FALSE;
206         
207         for(pa = proto_attrs; pa->name; pa++) {
208                 if (g_strcasecmp(attr_name,pa->name) == 0) {
209                         pa->act(yytext);
210                         got_it = TRUE;
211                         break;
212                 }
213         }
214         
215         if (! got_it) {
216                 g_string_sprintfa(build_data->error,
217                                                 "error in ethereal:protocol xmpli at %s : no such parameter %s!",
218                                                 location, attr_name);
219                 g_free(attr_name);
220                 yyterminate();
221         }
222         
223         g_free(attr_name);
224                 
225         BEGIN GET_ATTR_CLOSE_QUOTE;
226 }
227
228 <GET_ATTR_CLOSE_QUOTE>{dquote} { BEGIN PROTOCOL;}
229
230 <PROTOCOL>{stop_xmlpi} BEGIN DTD;
231
232 <DTD>{special_start}         { DTD_PARSE(TOKEN_TAG_START); }
233 <DTD>{special_stop}          { DTD_PARSE(TOKEN_TAG_STOP); }
234
235 <DTD>{attlist_kw}            { DTD_PARSE(TOKEN_ATTLIST_KW); }
236 <DTD>{element_kw}            { DTD_PARSE(TOKEN_ELEMENT_KW); }
237 <DTD>{doctype_kw}            { DTD_PARSE(TOKEN_DOCTYPE_KW); }
238
239 <DTD>{pcdata}                { DTD_PARSE(TOKEN_ELEM_DATA); } 
240 <DTD>{any}                   { DTD_PARSE(TOKEN_ELEM_DATA); }
241 <DTD>{cdata}                 { DTD_PARSE(TOKEN_ELEM_DATA); }
242 <DTD>{empty}                             { DTD_PARSE(TOKEN_EMPTY_KW); }
243
244 <DTD>{iD}                                { DTD_PARSE(TOKEN_ATT_TYPE); }
245 <DTD>{idref}                 { DTD_PARSE(TOKEN_ATT_TYPE); }
246 <DTD>{idrefs}                { DTD_PARSE(TOKEN_ATT_TYPE); }
247 <DTD>{nmtoken}               { DTD_PARSE(TOKEN_ATT_TYPE); }
248 <DTD>{nmtokens}              { DTD_PARSE(TOKEN_ATT_TYPE); }
249 <DTD>{entity}                { DTD_PARSE(TOKEN_ATT_TYPE); }
250 <DTD>{entities}              { DTD_PARSE(TOKEN_ATT_TYPE); }
251 <DTD>{notation}              { DTD_PARSE(TOKEN_ATT_TYPE); }
252 <DTD>{cdata_t}               { DTD_PARSE(TOKEN_ATT_TYPE); }
253 <DTD>{defaulT}               { DTD_PARSE(TOKEN_ATT_DEF_WITH_VALUE); }
254 <DTD>{fixed}                 { DTD_PARSE(TOKEN_ATT_DEF_WITH_VALUE); }
255 <DTD>{required}              { DTD_PARSE(TOKEN_ATT_DEF); }
256 <DTD>{implied}               { DTD_PARSE(TOKEN_ATT_DEF); }
257
258 <DTD>{star}                  { DTD_PARSE(TOKEN_STAR); }
259 <DTD>{question}              { DTD_PARSE(TOKEN_QUESTION); }
260 <DTD>{plus}                  { DTD_PARSE(TOKEN_PLUS); }
261 <DTD>{comma}                  { DTD_PARSE(TOKEN_COMMA); }
262 <DTD>{open_parens}           { DTD_PARSE(TOKEN_OPEN_PARENS); }
263 <DTD>{close_parens}          { DTD_PARSE(TOKEN_CLOSE_PARENS); }
264 <DTD>{open_bracket}          { DTD_PARSE(TOKEN_OPEN_BRACKET); }
265 <DTD>{close_bracket}         { DTD_PARSE(TOKEN_CLOSE_BRACKET); }
266 <DTD>{pipe}                  { DTD_PARSE(TOKEN_PIPE); }
267
268 <DTD>{dquoted}               |
269 <DTD>{squoted}               { DTD_PARSE(TOKEN_QUOTED); }
270 <DTD>{name}                  { DTD_PARSE(TOKEN_NAME); }
271
272 %%
273
274 static dtd_token_data_t* new_token(gchar* text) {
275         dtd_token_data_t* t = g_malloc(sizeof(dtd_token_data_t));
276         
277         t->text = g_strdup(text);
278         t->location = g_strdup(location);
279         
280         return t;
281 }
282
283
284
285 static int my_yyinput(char* buff, guint size) {
286
287         if (offset >= len ) {
288                 return YY_NULL;
289         } else if ( offset + size <= len ) {
290                 memcpy(buff, input_string->str + offset,size);
291                 offset += size;
292                 return size;
293         } else {
294                 size = len - offset;
295                 memcpy(buff, input_string->str + offset,size);
296                 offset = len;
297                 return size;
298         }
299 }
300
301 extern dtd_build_data_t* dtd_parse(GString* s) {
302
303         input_string = s;
304         offset = 0;
305         len = input_string->len;
306         
307         pParser = DtdParseAlloc(g_malloc);
308
309 #ifdef DEBUG_DTD_PARSER
310     DtdParseTrace(stderr, ">>");
311 #endif
312     
313         build_data = g_malloc(sizeof(dtd_build_data_t));
314
315         build_data->proto_name = NULL;
316         build_data->media_type = NULL;
317         build_data->description = NULL;
318         build_data->proto_root = NULL;
319         build_data->recursion = FALSE;
320     
321         build_data->elements = g_ptr_array_new();
322         build_data->attributes = g_ptr_array_new();
323
324         build_data->location = NULL;
325         build_data->error = g_string_new("");
326         
327     location = NULL;
328     
329         BEGIN DTD;
330         
331         yylex();
332
333         DtdParse(pParser, 0, NULL,build_data);
334
335         yyrestart(NULL);
336         
337     location = NULL;
338     
339         DtdParseFree(pParser, g_free );
340         
341         return build_data;
342 }