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 gchar* replace_entity(gchar* s);
60 static const gchar* location(void);
72 entity_start "<!"[[:blank:]\n]*entity[[:blank:]\n]*"%"
77 name [A-Za-z][-:A-Za-z0-9_\.]*
86 entity [%&][A-Za-z][-A-Za-z0-9_]*;
88 whitespace [[blank:]]+
90 %START OUTSIDE IN_COMMENT IN_ENTITY NAMED_ENTITY IN_QUOTE ENTITY_DONE XMLPI
94 {entity} if (current) g_string_sprintfa(current,"%s\n%s\n",replace_entity(yytext),location());
96 {whitespace} if (current) g_string_append(current," ");
98 <OUTSIDE>{xmlpi_start} { g_string_append(current,yytext); BEGIN XMLPI; }
99 <XMLPI>{xmlpi_chars} { g_string_append(current,yytext); }
100 <XMLPI>{newline} { g_string_append(current,yytext); }
101 <XMLPI>{xmlpi_stop} { g_string_append(current,yytext); BEGIN OUTSIDE; }
103 <OUTSIDE>{comment_start} { current = NULL; BEGIN IN_COMMENT; }
106 <IN_COMMENT>{comment_stop} { current = output; BEGIN OUTSIDE; }
110 if (current) g_string_sprintfa(current,"%s\n",location());
114 <OUTSIDE>{entity_start} { BEGIN IN_ENTITY; }
115 <IN_ENTITY>{name} { entity_name = g_strdup_printf("%%%s;",yytext); BEGIN NAMED_ENTITY; }
116 <NAMED_ENTITY>{quote} { current = g_string_new(location()); BEGIN IN_QUOTE; }
117 <IN_QUOTE>{quote} { g_hash_table_insert(entities,entity_name,current); BEGIN ENTITY_DONE; }
118 <IN_QUOTE>{percent} |
119 <IN_QUOTE>{non_quote} |
120 <IN_QUOTE>{escaped_quote} g_string_append(current,yytext);
121 <NAMED_ENTITY>{system} {
122 g_string_sprintfa(error,"at %s:%u: file inclusion is not supported!", filename, linenum);
125 <ENTITY_DONE>{special_stop} { current = output; g_string_append(current,"\n"); BEGIN OUTSIDE; }
129 static gchar* replace_entity(gchar* entity) {
130 GString* replacement;
134 replacement = g_hash_table_lookup(entities,entity);
137 return replacement->str;
139 g_string_sprintfa(error,"dtd_preparse: in file '%s': entity %s does not exists\n", filename, entity);
145 static const gchar* location(void) {
146 static gchar* loc = NULL;
148 if (loc) g_free(loc);
150 loc = g_strdup_printf("<? wireshark:location %s:%u ?>", filename, linenum);
155 static gboolean free_gstring_hash_items(gpointer k,gpointer v,gpointer p _U_) {
157 g_string_free(v,TRUE);
161 extern GString* dtd_preparse(const gchar* dname,const gchar* fname, GString* err) {
162 gchar* fullname = g_strdup_printf("%s%c%s",dname,G_DIR_SEPARATOR,fname);
168 yyin = fopen(fullname,"r");
172 g_string_sprintfa(err, "Could not open file: '%s', error: %s",fullname,strerror(errno));
179 entities = g_hash_table_new(g_str_hash,g_str_equal);
180 current = output = g_string_new(location());
190 g_hash_table_foreach_remove(entities,free_gstring_hash_items,NULL);
191 g_hash_table_destroy(entities);