2013-02-10 19:13:07 +00:00
|
|
|
/*
|
|
|
|
* We don't use input, so don't generate code for it.
|
|
|
|
*/
|
|
|
|
%option noinput
|
|
|
|
|
2007-07-30 20:22:37 +00:00
|
|
|
/*
|
|
|
|
* We don't use unput, so don't generate code for it.
|
|
|
|
*/
|
2005-09-10 17:29:15 +00:00
|
|
|
%option nounput
|
2007-07-30 20:22:37 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We don't read from the terminal.
|
|
|
|
*/
|
2005-09-10 17:29:15 +00:00
|
|
|
%option never-interactive
|
2007-07-30 20:22:37 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The language we're scanning is case-insensitive.
|
|
|
|
*/
|
2005-09-10 17:29:15 +00:00
|
|
|
%option caseless
|
2007-07-30 20:22:37 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Prefix scanner routines with "Dtd_PreParse_" rather than "yy", so this
|
|
|
|
* scanner can coexist with other scanners.
|
|
|
|
*/
|
|
|
|
%option prefix="Dtd_PreParse_"
|
|
|
|
|
2005-09-10 17:29:15 +00:00
|
|
|
%option outfile="dtd_preparse.c"
|
|
|
|
|
|
|
|
%{
|
|
|
|
/*
|
|
|
|
* dtd_preparser.l
|
|
|
|
*
|
2008-05-22 15:46:27 +00:00
|
|
|
* an XML dissector for wireshark
|
2005-09-10 17:29:15 +00:00
|
|
|
*
|
|
|
|
* DTD Preparser - import a dtd file into a GString
|
|
|
|
* including files, removing comments
|
|
|
|
* and resolving %entities;
|
2008-05-22 15:46:27 +00:00
|
|
|
*
|
2008-08-05 21:03:46 +00:00
|
|
|
* Copyright 2004, Luis E. Garcia Ontanon <luis@ontanon.org>
|
2005-09-10 17:29:15 +00:00
|
|
|
*
|
2006-05-21 05:12:17 +00:00
|
|
|
* Wireshark - Network traffic analyzer
|
|
|
|
* By Gerald Combs <gerald@wireshark.org>
|
2005-09-10 17:29:15 +00:00
|
|
|
* Copyright 1998 Gerald Combs
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2008-05-22 15:46:27 +00:00
|
|
|
*
|
2005-09-10 17:29:15 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2008-05-22 15:46:27 +00:00
|
|
|
*
|
2005-09-10 17:29:15 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2007-07-30 20:22:37 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2005-09-10 17:29:15 +00:00
|
|
|
*/
|
2008-05-22 15:46:27 +00:00
|
|
|
|
2012-09-20 01:48:30 +00:00
|
|
|
#include "config.h"
|
2012-02-02 11:54:33 +00:00
|
|
|
|
2005-09-10 17:29:15 +00:00
|
|
|
#include <glib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "dtd.h"
|
2007-08-04 02:13:52 +00:00
|
|
|
#include "dtd_preparse_lex.h"
|
2008-05-22 15:46:27 +00:00
|
|
|
#include <wsutil/file_util.h>
|
2005-09-10 17:29:15 +00:00
|
|
|
|
|
|
|
#define ECHO g_string_append(current,yytext);
|
|
|
|
|
2007-01-09 18:34:43 +00:00
|
|
|
static GString* current;
|
|
|
|
static GString* output;
|
|
|
|
static GHashTable* entities;
|
|
|
|
static gchar* entity_name;
|
|
|
|
static GString* error;
|
2005-09-10 17:29:15 +00:00
|
|
|
|
2007-01-09 18:34:43 +00:00
|
|
|
static const gchar* dtd_dirname;
|
|
|
|
static const gchar* filename;
|
|
|
|
static guint linenum;
|
2005-09-10 17:29:15 +00:00
|
|
|
|
2013-02-08 08:56:55 +00:00
|
|
|
static const gchar* replace_entity(gchar* s);
|
2005-09-10 17:29:15 +00:00
|
|
|
static const gchar* location(void);
|
|
|
|
|
2008-04-25 17:40:29 +00:00
|
|
|
/*
|
|
|
|
* Flex (v 2.5.35) uses this symbol to "exclude" unistd.h
|
|
|
|
*/
|
|
|
|
#ifdef _WIN32
|
|
|
|
#define YY_NO_UNISTD_H
|
|
|
|
#endif
|
|
|
|
|
2008-04-25 18:26:54 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
/* disable Windows VC compiler warning "signed/unsigned mismatch" associated */
|
|
|
|
/* with YY_INPUT code generated by flex versions such as 2.5.35. */
|
|
|
|
#pragma warning (disable:4018)
|
|
|
|
#endif
|
|
|
|
|
2005-09-10 17:29:15 +00:00
|
|
|
%}
|
|
|
|
xmlpi_start "<?"
|
|
|
|
xmlpi_stop "?>"
|
|
|
|
xmlpi_chars .
|
|
|
|
|
|
|
|
comment_start "<!--"
|
|
|
|
comment_stop "-->"
|
|
|
|
special_start "<!"
|
|
|
|
special_stop ">"
|
|
|
|
|
|
|
|
entity_start "<!"[[:blank:]\n]*entity[[:blank:]\n]*"%"
|
|
|
|
system SYSTEM
|
|
|
|
filename [^"]+
|
|
|
|
|
|
|
|
|
2005-09-28 21:35:14 +00:00
|
|
|
name [A-Za-z][-:A-Za-z0-9_\.]*
|
2005-09-10 17:29:15 +00:00
|
|
|
|
|
|
|
quote "\""
|
|
|
|
percent [%]
|
|
|
|
escaped_quote "\\\""
|
|
|
|
non_quote [^"%]+
|
|
|
|
|
|
|
|
avoid_editor_bug ["]
|
|
|
|
|
|
|
|
entity [%&][A-Za-z][-A-Za-z0-9_]*;
|
|
|
|
|
|
|
|
whitespace [[blank:]]+
|
|
|
|
newline \n
|
2005-09-17 17:05:46 +00:00
|
|
|
%START OUTSIDE IN_COMMENT IN_ENTITY NAMED_ENTITY IN_QUOTE ENTITY_DONE XMLPI
|
2005-09-10 17:29:15 +00:00
|
|
|
%%
|
|
|
|
|
|
|
|
|
2008-05-11 18:33:49 +00:00
|
|
|
{entity} if (current) g_string_append_printf(current,"%s\n%s\n",replace_entity(yytext),location());
|
2005-09-10 17:29:15 +00:00
|
|
|
|
|
|
|
{whitespace} if (current) g_string_append(current," ");
|
|
|
|
|
|
|
|
<OUTSIDE>{xmlpi_start} { g_string_append(current,yytext); BEGIN XMLPI; }
|
|
|
|
<XMLPI>{xmlpi_chars} { g_string_append(current,yytext); }
|
|
|
|
<XMLPI>{newline} { g_string_append(current,yytext); }
|
|
|
|
<XMLPI>{xmlpi_stop} { g_string_append(current,yytext); BEGIN OUTSIDE; }
|
|
|
|
|
|
|
|
<OUTSIDE>{comment_start} { current = NULL; BEGIN IN_COMMENT; }
|
|
|
|
<IN_COMMENT>[^-]? |
|
|
|
|
<IN_COMMENT>[-] ;
|
|
|
|
<IN_COMMENT>{comment_stop} { current = output; BEGIN OUTSIDE; }
|
2008-05-22 15:46:27 +00:00
|
|
|
|
2005-09-10 17:29:15 +00:00
|
|
|
{newline} {
|
|
|
|
linenum++;
|
2008-05-11 18:33:49 +00:00
|
|
|
if (current) g_string_append_printf(current,"%s\n",location());
|
2005-09-10 17:29:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
<OUTSIDE>{entity_start} { BEGIN IN_ENTITY; }
|
|
|
|
<IN_ENTITY>{name} { entity_name = g_strdup_printf("%%%s;",yytext); BEGIN NAMED_ENTITY; }
|
|
|
|
<NAMED_ENTITY>{quote} { current = g_string_new(location()); BEGIN IN_QUOTE; }
|
|
|
|
<IN_QUOTE>{quote} { g_hash_table_insert(entities,entity_name,current); BEGIN ENTITY_DONE; }
|
|
|
|
<IN_QUOTE>{percent} |
|
|
|
|
<IN_QUOTE>{non_quote} |
|
|
|
|
<IN_QUOTE>{escaped_quote} g_string_append(current,yytext);
|
2005-09-17 17:05:46 +00:00
|
|
|
<NAMED_ENTITY>{system} {
|
2014-11-12 15:40:02 +00:00
|
|
|
g_string_append_printf(error,"at %s:%u: file inclusion is not supported!", filename, linenum);
|
|
|
|
yyterminate();
|
2005-09-10 18:59:02 +00:00
|
|
|
}
|
2005-09-10 17:29:15 +00:00
|
|
|
<ENTITY_DONE>{special_stop} { current = output; g_string_append(current,"\n"); BEGIN OUTSIDE; }
|
|
|
|
|
|
|
|
%%
|
|
|
|
|
2013-02-08 08:56:55 +00:00
|
|
|
static const gchar* replace_entity(gchar* entity) {
|
2005-09-10 17:29:15 +00:00
|
|
|
GString* replacement;
|
2008-05-22 15:46:27 +00:00
|
|
|
|
2005-09-10 17:29:15 +00:00
|
|
|
*entity = '%';
|
2008-05-22 15:46:27 +00:00
|
|
|
|
2013-03-16 08:52:09 +00:00
|
|
|
replacement = (GString*)g_hash_table_lookup(entities,entity);
|
2008-05-22 15:46:27 +00:00
|
|
|
|
2005-09-10 17:29:15 +00:00
|
|
|
if (replacement) {
|
|
|
|
return replacement->str;
|
|
|
|
} else {
|
2008-05-11 18:33:49 +00:00
|
|
|
g_string_append_printf(error,"dtd_preparse: in file '%s': entity %s does not exists\n", filename, entity);
|
2005-09-10 17:29:15 +00:00
|
|
|
return "";
|
|
|
|
}
|
2008-05-22 15:46:27 +00:00
|
|
|
|
2005-09-10 17:29:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const gchar* location(void) {
|
2005-10-06 18:55:06 +00:00
|
|
|
static gchar* loc = NULL;
|
2008-05-22 15:46:27 +00:00
|
|
|
|
2005-10-06 18:55:06 +00:00
|
|
|
if (loc) g_free(loc);
|
2008-05-22 15:46:27 +00:00
|
|
|
|
2006-05-29 20:44:06 +00:00
|
|
|
loc = g_strdup_printf("<? wireshark:location %s:%u ?>", filename, linenum);
|
2008-05-22 15:46:27 +00:00
|
|
|
|
2005-10-06 18:55:06 +00:00
|
|
|
return loc;
|
2005-09-10 17:29:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean free_gstring_hash_items(gpointer k,gpointer v,gpointer p _U_) {
|
|
|
|
g_free(k);
|
2013-03-16 08:52:09 +00:00
|
|
|
g_string_free((GString*)v,TRUE);
|
2005-09-10 17:29:15 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2005-09-17 17:05:46 +00:00
|
|
|
extern GString* dtd_preparse(const gchar* dname,const gchar* fname, GString* err) {
|
|
|
|
gchar* fullname = g_strdup_printf("%s%c%s",dname,G_DIR_SEPARATOR,fname);
|
2008-05-22 15:46:27 +00:00
|
|
|
|
2005-11-10 21:52:55 +00:00
|
|
|
dtd_dirname = dname;
|
2005-09-10 17:29:15 +00:00
|
|
|
filename = fname;
|
2005-10-06 18:55:06 +00:00
|
|
|
linenum = 1;
|
2005-09-10 17:29:15 +00:00
|
|
|
|
2008-05-22 15:46:27 +00:00
|
|
|
yyin = ws_fopen(fullname,"r");
|
|
|
|
|
2005-09-10 17:29:15 +00:00
|
|
|
if (!yyin) {
|
|
|
|
if (err)
|
2011-06-28 09:00:11 +00:00
|
|
|
g_string_append_printf(err, "Could not open file: '%s', error: %s",fullname,g_strerror(errno));
|
2008-05-22 15:46:27 +00:00
|
|
|
|
2005-09-10 17:29:15 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2008-05-22 15:46:27 +00:00
|
|
|
|
2005-09-10 17:29:15 +00:00
|
|
|
error = err;
|
2008-05-22 15:46:27 +00:00
|
|
|
|
2005-09-10 17:29:15 +00:00
|
|
|
entities = g_hash_table_new(g_str_hash,g_str_equal);
|
|
|
|
current = output = g_string_new(location());
|
2008-05-22 15:46:27 +00:00
|
|
|
|
2005-09-10 17:29:15 +00:00
|
|
|
BEGIN OUTSIDE;
|
|
|
|
|
|
|
|
yylex();
|
2008-05-22 15:46:27 +00:00
|
|
|
|
2006-03-11 18:19:48 +00:00
|
|
|
fclose(yyin);
|
2008-05-22 15:46:27 +00:00
|
|
|
|
2005-09-10 17:29:15 +00:00
|
|
|
yyrestart(NULL);
|
|
|
|
|
|
|
|
g_hash_table_foreach_remove(entities,free_gstring_hash_items,NULL);
|
|
|
|
g_hash_table_destroy(entities);
|
|
|
|
|
2014-11-12 15:40:02 +00:00
|
|
|
g_free(fullname);
|
2005-10-06 18:55:06 +00:00
|
|
|
|
2005-09-10 17:29:15 +00:00
|
|
|
return output;
|
|
|
|
}
|
2008-04-25 17:40:29 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We want to stop processing when we get to the end of the input.
|
2008-05-22 15:46:27 +00:00
|
|
|
* (%option noyywrap is not used because if used then
|
2008-04-25 17:40:29 +00:00
|
|
|
* some flex versions (eg: 2.5.35) generate code which causes
|
|
|
|
* warnings by the Windows VC compiler).
|
|
|
|
*/
|
|
|
|
|
|
|
|
int yywrap(void) {
|
2014-11-12 15:40:02 +00:00
|
|
|
return 1;
|
2008-04-25 17:40:29 +00:00
|
|
|
}
|