2 * We don't use input, so don't generate code for it.
7 * We don't use unput, so don't generate code for it.
12 * We don't read interactively from the terminal.
14 %option never-interactive
17 * We want to stop processing when we get to the end of the input.
22 * The language we're scanning is case-insensitive.
27 * Prefix scanner routines with "Dtd_PreParse_" rather than "yy", so this
28 * scanner can coexist with other scanners.
30 %option prefix="Dtd_PreParse_"
32 %option outfile="dtd_preparse.c"
38 * an XML dissector for wireshark
40 * DTD Preparser - import a dtd file into a GString
41 * including files, removing comments
42 * and resolving %entities;
44 * Copyright 2004, Luis E. Garcia Ontanon <luis@ontanon.org>
46 * Wireshark - Network traffic analyzer
47 * By Gerald Combs <gerald@wireshark.org>
48 * Copyright 1998 Gerald Combs
50 * This program is free software; you can redistribute it and/or
51 * modify it under the terms of the GNU General Public License
52 * as published by the Free Software Foundation; either version 2
53 * of the License, or (at your option) any later version.
55 * This program is distributed in the hope that it will be useful,
56 * but WITHOUT ANY WARRANTY; without even the implied warranty of
57 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
58 * GNU General Public License for more details.
60 * You should have received a copy of the GNU General Public License
61 * along with this program; if not, write to the Free Software
62 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
72 #include "dtd_preparse_lex.h"
73 #include <wsutil/file_util.h>
75 #define ECHO g_string_append(current,yytext);
77 static GString* current;
78 static GString* output;
79 static GHashTable* entities;
80 static gchar* entity_name;
81 static GString* error;
83 static const gchar* dtd_dirname;
84 static const gchar* filename;
87 static const gchar* replace_entity(gchar* s);
88 static const gchar* location(void);
91 * Flex (v 2.5.35) uses this symbol to "exclude" unistd.h
94 #define YY_NO_UNISTD_H
98 /* disable Windows VC compiler warning "signed/unsigned mismatch" associated */
99 /* with YY_INPUT code generated by flex versions such as 2.5.35. */
100 #pragma warning (disable:4018)
113 entity_start "<!"[[:blank:]\n]*entity[[:blank:]\n]*"%"
118 name [A-Za-z][-:A-Za-z0-9_\.]*
127 entity [%&][A-Za-z][-A-Za-z0-9_]*;
129 whitespace [[blank:]]+
131 %START OUTSIDE IN_COMMENT IN_ENTITY NAMED_ENTITY IN_QUOTE ENTITY_DONE XMLPI
135 {entity} if (current) g_string_append_printf(current,"%s\n%s\n",replace_entity(yytext),location());
137 {whitespace} if (current) g_string_append(current," ");
139 <OUTSIDE>{xmlpi_start} { g_string_append(current,yytext); BEGIN XMLPI; }
140 <XMLPI>{xmlpi_chars} { g_string_append(current,yytext); }
141 <XMLPI>{newline} { g_string_append(current,yytext); }
142 <XMLPI>{xmlpi_stop} { g_string_append(current,yytext); BEGIN OUTSIDE; }
144 <OUTSIDE>{comment_start} { current = NULL; BEGIN IN_COMMENT; }
147 <IN_COMMENT>{comment_stop} { current = output; BEGIN OUTSIDE; }
151 if (current) g_string_append_printf(current,"%s\n",location());
155 <OUTSIDE>{entity_start} { BEGIN IN_ENTITY; }
156 <IN_ENTITY>{name} { entity_name = g_strdup_printf("%%%s;",yytext); BEGIN NAMED_ENTITY; }
157 <NAMED_ENTITY>{quote} { current = g_string_new(location()); BEGIN IN_QUOTE; }
158 <IN_QUOTE>{quote} { g_hash_table_insert(entities,entity_name,current); BEGIN ENTITY_DONE; }
159 <IN_QUOTE>{percent} |
160 <IN_QUOTE>{non_quote} |
161 <IN_QUOTE>{escaped_quote} g_string_append(current,yytext);
162 <NAMED_ENTITY>{system} {
163 g_string_append_printf(error,"at %s:%u: file inclusion is not supported!", filename, linenum);
166 <ENTITY_DONE>{special_stop} { current = output; g_string_append(current,"\n"); BEGIN OUTSIDE; }
170 static const gchar* replace_entity(gchar* entity) {
171 GString* replacement;
175 replacement = (GString*)g_hash_table_lookup(entities,entity);
178 return replacement->str;
180 g_string_append_printf(error,"dtd_preparse: in file '%s': entity %s does not exists\n", filename, entity);
186 static const gchar* location(void) {
187 static gchar* loc = NULL;
189 if (loc) g_free(loc);
191 loc = g_strdup_printf("<? wireshark:location %s:%u ?>", filename, linenum);
196 static gboolean free_gstring_hash_items(gpointer k,gpointer v,gpointer p _U_) {
198 g_string_free((GString*)v,TRUE);
202 extern GString* dtd_preparse(const gchar* dname,const gchar* fname, GString* err) {
203 gchar* fullname = g_strdup_printf("%s%c%s",dname,G_DIR_SEPARATOR,fname);
209 yyin = ws_fopen(fullname,"r");
213 g_string_append_printf(err, "Could not open file: '%s', error: %s",fullname,g_strerror(errno));
220 entities = g_hash_table_new(g_str_hash,g_str_equal);
221 current = output = g_string_new(location());
231 g_hash_table_foreach_remove(entities,free_gstring_hash_items,NULL);
232 g_hash_table_destroy(entities);