3 %option prefix="Dtd_PreParse_"
4 %option never-interactive
6 %option outfile="dtd_preparse.c"
12 * an XML dissector for wireshark
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 * Wireshark - Network traffic analyzer
23 * By Gerald Combs <gerald@wireshark.org>
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 ECHO g_string_append(current,yytext);
49 static GString* current;
50 static GString* output;
51 static GHashTable* entities;
52 static gchar* entity_name;
53 static GString* error;
55 static const gchar* dtd_dirname;
56 static const gchar* filename;
59 static GString* textstr;
61 static gchar* replace_entity(gchar* s);
62 static const gchar* location(void);
74 entity_start "<!"[[:blank:]\n]*entity[[:blank:]\n]*"%"
79 name [A-Za-z][-:A-Za-z0-9_\.]*
88 entity [%&][A-Za-z][-A-Za-z0-9_]*;
90 whitespace [[blank:]]+
92 %START OUTSIDE IN_COMMENT IN_ENTITY NAMED_ENTITY IN_QUOTE ENTITY_DONE XMLPI
96 {entity} if (current) g_string_sprintfa(current,"%s\n%s\n",replace_entity(yytext),location());
98 {whitespace} if (current) g_string_append(current," ");
100 <OUTSIDE>{xmlpi_start} { g_string_append(current,yytext); BEGIN XMLPI; }
101 <XMLPI>{xmlpi_chars} { g_string_append(current,yytext); }
102 <XMLPI>{newline} { g_string_append(current,yytext); }
103 <XMLPI>{xmlpi_stop} { g_string_append(current,yytext); BEGIN OUTSIDE; }
105 <OUTSIDE>{comment_start} { current = NULL; BEGIN IN_COMMENT; }
108 <IN_COMMENT>{comment_stop} { current = output; BEGIN OUTSIDE; }
112 if (current) g_string_sprintfa(current,"%s\n",location());
116 <OUTSIDE>{entity_start} { BEGIN IN_ENTITY; }
117 <IN_ENTITY>{name} { entity_name = g_strdup_printf("%%%s;",yytext); BEGIN NAMED_ENTITY; }
118 <NAMED_ENTITY>{quote} { current = g_string_new(location()); BEGIN IN_QUOTE; }
119 <IN_QUOTE>{quote} { g_hash_table_insert(entities,entity_name,current); BEGIN ENTITY_DONE; }
120 <IN_QUOTE>{percent} |
121 <IN_QUOTE>{non_quote} |
122 <IN_QUOTE>{escaped_quote} g_string_append(current,yytext);
123 <NAMED_ENTITY>{system} {
124 g_string_sprintfa(error,"at %s:%u: file inclusion is not supported!", filename, linenum);
127 <ENTITY_DONE>{special_stop} { current = output; g_string_append(current,"\n"); BEGIN OUTSIDE; }
131 static gchar* replace_entity(gchar* entity) {
132 GString* replacement;
136 replacement = g_hash_table_lookup(entities,entity);
139 return replacement->str;
141 g_string_sprintfa(error,"dtd_preparse: in file '%s': entity %s does not exists\n", filename, entity);
147 static const gchar* location(void) {
148 static gchar* loc = NULL;
150 if (loc) g_free(loc);
152 loc = g_strdup_printf("<? wireshark:location %s:%u ?>", filename, linenum);
157 static gboolean free_gstring_hash_items(gpointer k,gpointer v,gpointer p _U_) {
159 g_string_free(v,TRUE);
163 extern GString* dtd_preparse(const gchar* dname,const gchar* fname, GString* err) {
164 gchar* fullname = g_strdup_printf("%s%c%s",dname,G_DIR_SEPARATOR,fname);
170 yyin = fopen(fullname,"r");
174 g_string_sprintfa(err, "Could not open file: '%s', error: %s",fullname,strerror(errno));
181 entities = g_hash_table_new(g_str_hash,g_str_equal);
182 current = output = g_string_new(location());
192 g_hash_table_foreach_remove(entities,free_gstring_hash_items,NULL);
193 g_hash_table_destroy(entities);