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