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