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