don't use fgets() as MSVC does not implement it. use fgetc instead.
[obnox/wireshark/wip.git] / epan / dtd_preparse.l
1 %option noyywrap
2 %option nounput
3 %option prefix="Dtd_PreParse_"
4 %option never-interactive
5 %option caseless
6 %option outfile="dtd_preparse.c"
7
8 %{
9         /*
10          * dtd_preparser.l
11          *
12          * an XML dissector for ethereal 
13          *
14          * DTD Preparser -  import a dtd file into a GString
15          *                                      including files, removing comments
16          *                  and resolving %entities;
17          * 
18          * Copyright 2004, Luis E. Garcia Ontanon <luis.ontanon@gmail.com>
19          *
20          * $Id$
21          *
22          * Ethereal - Network traffic analyzer
23          * By Gerald Combs <gerald@ethereal.com>
24          * Copyright 1998 Gerald Combs
25          *
26          * This program is free software; you can redistribute it and/or
27          * modify it under the terms of the GNU General Public License
28          * as published by the Free Software Foundation; either version 2
29          * of the License, or (at your option) any later version.
30          * 
31          * This program is distributed in the hope that it will be useful,
32          * but WITHOUT ANY WARRANTY; without even the implied warranty of
33          * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34          * GNU General Public License for more details.
35          * 
36          * You should have received a copy of the GNU General Public License
37          * along with this program; if not, write to the Free Software
38          * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
39          */
40                         
41 #include <glib.h>
42 #include <string.h>
43 #include <errno.h>
44 #include <stdio.h>
45 #include "dtd.h"
46
47 #define MAX_INCLUDE_DEPTH 10
48 YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH];
49 int include_stack_ptr = 0;
50
51 #define ECHO g_string_append(current,yytext);
52
53 GString* current;
54 GString* output;
55 GHashTable* entities;
56 gchar* entity_name;
57 GString* error;
58
59 gchar* dirname;
60 gchar* filename;
61 guint linenum;
62
63 static gchar* replace_entity(gchar* s);
64 static const gchar* location(void);
65 static gchar* load_entity_file(gchar* filename);
66                 /* [:blank:]+file[:blank:]*=[:blank:]*["] */
67
68 %}
69 xmlpi_start "<?"
70 xmlpi_stop  "?>"
71 xmlpi_chars .
72
73 comment_start "<!--"
74 comment_stop "-->"
75 special_start "<!"
76 special_stop ">"
77
78 entity_start     "<!"[[:blank:]\n]*entity[[:blank:]\n]*"%"
79 system     SYSTEM
80 filename   [^"]+
81
82
83 name [A-Za-z][-:A-Za-z0-9_]*
84
85 quote "\""
86 percent [%]
87 escaped_quote "\\\""
88 non_quote [^"%]+
89
90 avoid_editor_bug ["]
91
92 entity        [%&][A-Za-z][-A-Za-z0-9_]*;
93
94 whitespace [[blank:]]+
95 newline    \n
96 %START OUTSIDE IN_COMMENT IN_ENTITY NAMED_ENTITY IN_QUOTE ENTITY_DONE GET_FNAME_OPEN_QUOTE GET_FNAME GET_FNAME_CLOSE_QUOTE XMLPI
97 %%
98
99
100 {entity}                                                if (current) g_string_sprintfa(current,"%s\n%s\n",replace_entity(yytext),location());
101
102 {whitespace}                                    if (current) g_string_append(current," ");
103
104 <OUTSIDE>{xmlpi_start}                  { g_string_append(current,yytext); BEGIN XMLPI; }
105 <XMLPI>{xmlpi_chars}                    { g_string_append(current,yytext); }
106 <XMLPI>{newline}                                { g_string_append(current,yytext); }
107 <XMLPI>{xmlpi_stop}                             { g_string_append(current,yytext); BEGIN OUTSIDE; }
108
109 <OUTSIDE>{comment_start}                { current = NULL; BEGIN IN_COMMENT; }
110 <IN_COMMENT>[^-]?                               |
111 <IN_COMMENT>[-]                                 ;
112 <IN_COMMENT>{comment_stop}              { current = output; BEGIN OUTSIDE; }
113         
114 {newline}                                               {
115         linenum++;
116         if (current) g_string_sprintfa(current,"%s\n",location());
117 }
118
119
120 <OUTSIDE>{entity_start}                 { BEGIN IN_ENTITY; }
121 <IN_ENTITY>{name}                               { entity_name = g_strdup_printf("%%%s;",yytext); BEGIN NAMED_ENTITY; }
122 <NAMED_ENTITY>{quote}                   { current = g_string_new(location()); BEGIN IN_QUOTE; }
123 <IN_QUOTE>{quote}                               { g_hash_table_insert(entities,entity_name,current);  BEGIN ENTITY_DONE; }
124 <IN_QUOTE>{percent}                             |
125 <IN_QUOTE>{non_quote}                   |
126 <IN_QUOTE>{escaped_quote}               g_string_append(current,yytext);
127 <NAMED_ENTITY>{system}                  { BEGIN GET_FNAME_OPEN_QUOTE; }
128 <GET_FNAME_OPEN_QUOTE>{quote}   { BEGIN GET_FNAME; }
129 <GET_FNAME>{filename}               {  g_hash_table_insert(entities,entity_name,load_entity_file(yytext));  BEGIN GET_FNAME_CLOSE_QUOTE; }
130 <GET_FNAME_CLOSE_QUOTE>{quote}  { BEGIN ENTITY_DONE; }
131 <ENTITY_DONE>{special_stop}             { current = output; g_string_append(current,"\n"); BEGIN OUTSIDE; }
132
133 %%
134
135 static gchar* load_entity_file(gchar* fname) {
136         gchar* fullname = g_strdup_printf("%s%s",dirname,fname);
137         gchar* save_filename = filename;
138         guint save_linenum = linenum; 
139         FILE* fp = fopen(fullname,"r");
140         GString* filetext;
141         gchar* retstr;
142         gchar c;
143         
144         g_free(fullname);
145         
146         if (!fp) {
147                 g_string_sprintfa(error,"at %s:%u: could not load file %s: %s", filename, linenum, fname, strerror(errno));
148                 return "";
149         }
150         
151         filename = fname;
152         linenum = 1;
153         
154         filetext = g_string_new(location());
155         
156         while(( c = fgetc(fp) )) {
157                 g_string_append_c(filetext,c);
158                 if(c == '\n') {
159                         g_string_append(filetext,location());
160                         linenum++;
161                 }
162         }
163
164         retstr = filetext->str;
165         g_string_free(filetext,FALSE);
166
167         if ( ferror(fp) ) {
168                 g_string_sprintfa(error,"at %s:%u: problem reading file %s: %s", filename, linenum, fname, strerror(errno));
169         }
170
171         filename = save_filename;
172         save_linenum = linenum;
173         
174         return retstr;
175 }
176
177 static gchar* replace_entity(gchar* entity) {
178         GString* replacement;
179         
180         *entity = '%';
181         
182         replacement = g_hash_table_lookup(entities,entity);
183         
184         if (replacement) {
185                 return replacement->str;
186         } else {
187                 g_string_sprintfa(error,"dtd_preparse: in file '%s': %s does not exists\n", filename, entity);
188                 return "";
189         }
190         
191 }
192
193 static const gchar* location(void) {
194         static GString* loc = NULL;
195         guint i = include_stack_ptr + 1;
196         
197         if (loc) {
198                 g_string_truncate(loc,0);
199         } else {
200                 loc = g_string_new("");
201         }
202
203         g_string_sprintfa(loc,"<? ethereal:location ");
204
205         while (i--) {
206                         g_string_sprintfa(loc, "%s:%u from",
207                                                           filename,
208                                                           linenum);                     
209         }
210
211         g_string_truncate(loc,(loc->len) - 4);
212         
213         g_string_sprintfa(loc,"?>");
214         
215         return loc->str;
216 }
217
218 static gboolean free_gstring_hash_items(gpointer k,gpointer v,gpointer p _U_) {
219         g_free(k);
220         g_string_free(v,TRUE);
221         return TRUE;
222 }
223
224 extern GString* dtd_preparse(gchar* dname, gchar* fname, GString* err) {
225         gchar* fullname = g_strdup_printf("%s%s",dname,fname);
226
227         dirname = dname;
228         filename = fname;
229
230         yyin = fopen(fullname,"r");
231         
232         g_free(fullname);
233         
234         if (!yyin) {
235                 if (err)
236                         g_string_sprintfa(err, "Could not open file: '%s', error: %s",filename,strerror(errno));
237                         
238                 return NULL;
239         }
240         
241         filename = filename;
242         linenum = 1;
243         
244         error = err;
245         
246         entities = g_hash_table_new(g_str_hash,g_str_equal);
247         current = output = g_string_new(location());
248         
249         BEGIN OUTSIDE;
250
251         yylex();
252                 
253         yyrestart(NULL);
254
255         g_hash_table_foreach_remove(entities,free_gstring_hash_items,NULL);
256         g_hash_table_destroy(entities);
257
258         return output;
259 }