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