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