regen pidl all: rm epan/dissectors/pidl/*-stamp; pushd epan/dissectors/pidl/ && make...
[wireshark-sm.git] / epan / dtd_preparse.l
blob3ac7e1519976c80562e3aa7bf46b7f80aeb71eeb
1 %top {
2 /* Include this before everything else, for various large-file definitions */
3 #include "config.h"
4 #include <wireshark.h>
7 /*
8  * We want a reentrant scanner.
9  */
10 %option reentrant
13  * We don't use input, so don't generate code for it.
14  */
15 %option noinput
18  * We don't use unput, so don't generate code for it.
19  */
20 %option nounput
23  * We don't read interactively from the terminal.
24  */
25 %option never-interactive
28  * We want to stop processing when we get to the end of the input.
29  */
30 %option noyywrap
33  * The type for the state we keep for a scanner.
34  */
35 %option extra-type="Dtd_PreParse_scanner_state_t *"
38  * The language we're scanning is case-insensitive.
39  */
40 %option caseless
43  * Prefix scanner routines with "Dtd_PreParse_" rather than "yy", so this
44  * scanner can coexist with other scanners.
45  */
46 %option prefix="Dtd_PreParse_"
49  * We have to override the memory allocators so that we don't get
50  * "unused argument" warnings from the yyscanner argument (which
51  * we don't use, as we have a global memory allocator).
52  *
53  * We provide, as macros, our own versions of the routines generated by Flex,
54  * which just call malloc()/realloc()/free() (as the Flex versions do),
55  * discarding the extra argument.
56  */
57 %option noyyalloc
58 %option noyyrealloc
59 %option noyyfree
62         /*
63          * dtd_preparse.l
64          *
65          * an XML dissector for wireshark
66          *
67          * DTD Preparser -  import a dtd file into a GString
68          *                  including files, removing comments
69          *                  and resolving %entities;
70          *
71          * Copyright 2004, Luis E. Garcia Ontanon <luis@ontanon.org>
72          *
73          * Wireshark - Network traffic analyzer
74          * By Gerald Combs <gerald@wireshark.org>
75          * Copyright 1998 Gerald Combs
76          *
77          * This program is free software; you can redistribute it and/or
78          * modify it under the terms of the GNU General Public License
79          * as published by the Free Software Foundation; either version 2
80          * of the License, or (at your option) any later version.
81          *
82          * This program is distributed in the hope that it will be useful,
83          * but WITHOUT ANY WARRANTY; without even the implied warranty of
84          * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
85          * GNU General Public License for more details.
86          *
87          * You should have received a copy of the GNU General Public License
88          * along with this program; if not, write to the Free Software
89          * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
90          */
92 #include <glib.h>
93 #include <string.h>
94 #include <errno.h>
95 #include <stdio.h>
96 #include "dtd.h"
97 #include <wsutil/file_util.h>
100  * Disable diagnostics in the code generated by Flex.
101  */
102 DIAG_OFF_FLEX()
104 #define ECHO g_string_append(yyextra->current,yytext);
106 struct _dtd_preparse_scanner_state {
107         const char* dtd_dirname;
108         const char* filename;
109         unsigned linenum;
111         GString* error;
113         GHashTable* entities;
114         GString* current;
115         GString* output;
116         char* entity_name;
119 static const char* replace_entity(Dtd_PreParse_scanner_state_t* state, char* s);
121 #define YY_USER_INIT { \
122         BEGIN OUTSIDE; \
126  * Flex (v 2.5.35) uses this symbol to "exclude" unistd.h
127  */
128 #ifdef _WIN32
129 #define YY_NO_UNISTD_H
130 #endif
133  * Sleazy hack to suppress compiler warnings in yy_fatal_error().
134  */
135 #define YY_EXIT_FAILURE ((void)yyscanner, 2)
138  * Macros for the allocators, to discard the extra argument.
139  */
140 #define Dtd_PreParse_alloc(size, yyscanner)             (void *)malloc(size)
141 #define Dtd_PreParse_realloc(ptr, size, yyscanner)      (void *)realloc((char *)(ptr), (size))
142 #define Dtd_PreParse_free(ptr, yyscanner)               free((char *)ptr)
145 xmlpi_start "<?"
146 xmlpi_stop  "?>"
147 xmlpi_chars .
149 comment_start "<!--"
150 comment_stop "-->"
151 special_start "<!"
152 special_stop ">"
154 entity_start     "<!"[[:blank:]\n]*entity[[:blank:]\n]*"%"
155 system     SYSTEM
156 filename   [^"]+
159 name [A-Za-z][-:A-Za-z0-9_\.]*
161 quote "\""
162 percent [%]
163 escaped_quote "\\\""
164 non_quote [^"%]+
166 avoid_editor_bug ["]
168 entity        [%&][A-Za-z][-A-Za-z0-9_]*;
170 whitespace [[blank:]]+
171 newline    \n
172 %START OUTSIDE IN_COMMENT IN_ENTITY NAMED_ENTITY IN_QUOTE ENTITY_DONE XMLPI
176 {entity}                                if (yyextra->current) g_string_append_printf(yyextra->current,"%s\n%s\n",replace_entity(yyextra, yytext),dtd_location(yyextra));
178 {whitespace}                            if (yyextra->current) g_string_append(yyextra->current," ");
180 <OUTSIDE>{xmlpi_start}                  { g_string_append(yyextra->current,yytext); BEGIN XMLPI; }
181 <XMLPI>{xmlpi_chars}                    { g_string_append(yyextra->current,yytext); }
182 <XMLPI>{newline}                        { g_string_append(yyextra->current,yytext); }
183 <XMLPI>{xmlpi_stop}                     { g_string_append(yyextra->current,yytext); BEGIN OUTSIDE; }
185 <OUTSIDE>{comment_start}                { yyextra->current = NULL; BEGIN IN_COMMENT; }
186 <IN_COMMENT>[^-]?                       |
187 <IN_COMMENT>[-]                         ;
188 <IN_COMMENT>{comment_stop}              { yyextra->current = yyextra->output; BEGIN OUTSIDE; }
190 {newline}                               {
191         yyextra->linenum++;
192         if (yyextra->current) g_string_append_printf(yyextra->current,"%s\n",dtd_location(yyextra));
196 <OUTSIDE>{entity_start}                 { BEGIN IN_ENTITY; }
197 <IN_ENTITY>{name}                       { yyextra->entity_name = ws_strdup_printf("%%%s;",yytext); BEGIN NAMED_ENTITY; }
198 <NAMED_ENTITY>{quote}                   { yyextra->current = g_string_new(dtd_location(yyextra)); BEGIN IN_QUOTE; }
199 <IN_QUOTE>{quote}                       { g_hash_table_insert(yyextra->entities,yyextra->entity_name,yyextra->current);  BEGIN ENTITY_DONE; }
200 <IN_QUOTE>{percent}                     |
201 <IN_QUOTE>{non_quote}                   |
202 <IN_QUOTE>{escaped_quote}               g_string_append(yyextra->current,yytext);
203 <NAMED_ENTITY>{system}                  {
204         g_string_append_printf(yyextra->error,"at %s:%u: file inclusion is not supported!", yyextra->filename, yyextra->linenum);
205         yyterminate();
207 <ENTITY_DONE>{special_stop}             { yyextra->current = yyextra->output; g_string_append(yyextra->current,"\n"); BEGIN OUTSIDE; }
212  * Turn diagnostics back on, so we check the code that we've written.
213  */
214 DIAG_ON_FLEX()
216 static const char* replace_entity(Dtd_PreParse_scanner_state_t* state, char* entity) {
217         GString* replacement;
219         *entity = '%';
221         replacement = (GString*)g_hash_table_lookup(state->entities,entity);
223         if (replacement) {
224                 return replacement->str;
225         } else {
226                 g_string_append_printf(state->error,"dtd_preparse: in file '%s': entity %s does not exists\n", state->filename, entity);
227                 return "";
228         }
232 const char* dtd_location(Dtd_PreParse_scanner_state_t* state) {
233         static char* loc = NULL;
235         g_free(loc);
237         if (!state) return NULL;
239         loc = ws_strdup_printf("<? wireshark:location %s:%u ?>", state->filename, state->linenum);
241         return loc;
244 static gboolean free_gstring_hash_items(void *k,void *v,void *p _U_) {
245         g_free(k);
246         g_string_free((GString*)v,true);
247         return true;
250 extern GString* dtd_preparse(const char* dname,const  char* fname, GString* err) {
251         char* fullname = ws_strdup_printf("%s%c%s",dname,G_DIR_SEPARATOR,fname);
252         FILE *in;
253         yyscan_t scanner;
254         Dtd_PreParse_scanner_state_t state;
256         in = ws_fopen(fullname,"r");
258         if (!in) {
259                 if (err)
260                         g_string_append_printf(err, "Could not open file: '%s', error: %s",fullname,g_strerror(errno));
261                 g_free(fullname);
262                 return NULL;
263         }
265         if (Dtd_PreParse_lex_init(&scanner) != 0) {
266                 if (err)
267                         g_string_append_printf(err, "Can't initialize scanner: %s",
268                             strerror(errno));
269                 fclose(in);
270                 g_free(fullname);
271                 return NULL;
272         }
274         Dtd_PreParse_set_in(in, scanner);
276         state.dtd_dirname = dname;
277         state.filename = fname;
278         state.linenum = 1;
280         state.error = err;
282         state.entities = g_hash_table_new(g_str_hash,g_str_equal);
283         state.current = state.output = g_string_new(dtd_location(&state));
284         state.entity_name = NULL;
286         /* Associate the state with the scanner */
287         Dtd_PreParse_set_extra(&state, scanner);
289         Dtd_PreParse_lex(scanner);
291         Dtd_PreParse_lex_destroy(scanner);
292         fclose(in);
294         g_hash_table_foreach_remove(state.entities,free_gstring_hash_items,NULL);
295         g_hash_table_destroy(state.entities);
297         g_free(fullname);
299         return state.output;