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