3 %option prefix="Dtd_PreParse_"
4 %option never-interactive
6 %option outfile="dtd_preparse.c"
12 * an XML dissector for ethereal
14 * DTD Preparser - import a dtd file into a GString
15 * including files, removing comments
16 * and resolving %entities;
18 * Copyright 2004, Luis E. Garcia Ontanon <luis.ontanon@gmail.com>
22 * Ethereal - Network traffic analyzer
23 * By Gerald Combs <gerald@ethereal.com>
24 * Copyright 1998 Gerald Combs
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.
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.
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.
47 #define MAX_INCLUDE_DEPTH 10
48 YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH];
49 int include_stack_ptr = 0;
51 #define ECHO g_string_append(current,yytext);
60 const gchar* filename;
65 static gchar* replace_entity(gchar* s);
66 static const gchar* location(void);
78 entity_start "<!"[[:blank:]\n]*entity[[:blank:]\n]*"%"
83 name [A-Za-z][-:A-Za-z0-9_]*
92 entity [%&][A-Za-z][-A-Za-z0-9_]*;
94 whitespace [[blank:]]+
96 %START OUTSIDE IN_COMMENT IN_ENTITY NAMED_ENTITY IN_QUOTE ENTITY_DONE XMLPI
100 {entity} if (current) g_string_sprintfa(current,"%s\n%s\n",replace_entity(yytext),location());
102 {whitespace} if (current) g_string_append(current," ");
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; }
109 <OUTSIDE>{comment_start} { current = NULL; BEGIN IN_COMMENT; }
112 <IN_COMMENT>{comment_stop} { current = output; BEGIN OUTSIDE; }
116 if (current) g_string_sprintfa(current,"%s\n",location());
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} {
128 g_string_sprintfa(error,"at %s:%u: file inclusion is not supported!", filename, linenum);
131 <ENTITY_DONE>{special_stop} { current = output; g_string_append(current,"\n"); BEGIN OUTSIDE; }
135 static gchar* replace_entity(gchar* entity) {
136 GString* replacement;
140 replacement = g_hash_table_lookup(entities,entity);
143 return replacement->str;
145 g_string_sprintfa(error,"dtd_preparse: in file '%s': entity %s does not exists\n", filename, entity);
151 static const gchar* location(void) {
152 static GString* loc = NULL;
153 guint i = include_stack_ptr + 1;
156 g_string_truncate(loc,0);
158 loc = g_string_new("");
161 g_string_sprintfa(loc,"<? ethereal:location ");
164 g_string_sprintfa(loc, "%s:%u from",
169 g_string_truncate(loc,(loc->len) - 4);
171 g_string_sprintfa(loc,"?>");
176 static gboolean free_gstring_hash_items(gpointer k,gpointer v,gpointer p _U_) {
178 g_string_free(v,TRUE);
182 extern GString* dtd_preparse(const gchar* dname,const gchar* fname, GString* err) {
183 gchar* fullname = g_strdup_printf("%s%c%s",dname,G_DIR_SEPARATOR,fname);
188 yyin = fopen(fullname,"r");
194 g_string_sprintfa(err, "Could not open file: '%s', error: %s",filename,strerror(errno));
204 entities = g_hash_table_new(g_str_hash,g_str_equal);
205 current = output = g_string_new(location());
213 g_hash_table_foreach_remove(entities,free_gstring_hash_items,NULL);
214 g_hash_table_destroy(entities);