forked from osmocom/wireshark
Create a more modular type system for the FT_* types. Put them
into epan/ftypes. Re-write display filter routines using Lemon parser instead of yacc. Besides using a different tool, the new grammar is much simpler, while the display filter engine itself is more powerful and more easily extended. Add dftest executable, to test display filter "bytecode" generation. Add option to "configure" to build dftest or randpkt, both of which are not built by default. Implement Ed Warnicke's ideas about dranges in the new display filter and ftype code. Remove type FT_TEXT_ONLY in favor of FT_NONE, and have protocols registered as FT_PROTOCOL. Thus, FT_NONE is used only for simple labels in the proto tree, while FT_PROTOCOL is used for protocols. This was necessary for being able to make byte slices (ranges) out of protocols, like "frame[0:3]" Win32 Makefile.nmake's will be added tonight. svn path=/trunk/; revision=2967
This commit is contained in:
parent
07a925ef8b
commit
8f1fff2e6a
|
@ -14,6 +14,7 @@ configure
|
|||
dfilter-grammar.c
|
||||
dfilter-grammar.h
|
||||
dfilter-scanner.c
|
||||
dftest
|
||||
editcap
|
||||
editcap.1
|
||||
ethereal
|
||||
|
|
50
Makefile.am
50
Makefile.am
|
@ -1,7 +1,7 @@
|
|||
# Makefile.am
|
||||
# Automake file for Ethereal
|
||||
#
|
||||
# $Id: Makefile.am,v 1.280 2001/01/28 04:43:24 guy Exp $
|
||||
# $Id: Makefile.am,v 1.281 2001/02/01 20:21:13 gram Exp $
|
||||
#
|
||||
# Ethereal - Network traffic analyzer
|
||||
# By Gerald Combs <gerald@zing.org>
|
||||
|
@ -61,11 +61,11 @@ ACLOCAL_AMFLAGS = `./aclocal-flags`
|
|||
# automake will arrange that the Makefile define it as the union of all
|
||||
# the "man{section}_MANS" variables.
|
||||
#
|
||||
bin_PROGRAMS = @ethereal_bin@ @editcap_bin@ @tethereal_bin@
|
||||
bin_PROGRAMS = @ethereal_bin@ @editcap_bin@ @tethereal_bin@ @dftest_bin@ @randpkt_bin@
|
||||
man1_MANS = @ethereal_man@ @editcap_man@ @tethereal_man@
|
||||
man_MANS =
|
||||
|
||||
EXTRA_PROGRAMS = ethereal ethereal_static tethereal tethereal_static editcap
|
||||
EXTRA_PROGRAMS = ethereal ethereal_static tethereal tethereal_static editcap dftest
|
||||
|
||||
sysconf_DATA = manuf
|
||||
|
||||
|
@ -388,7 +388,12 @@ ethereal_optional_objects = @SNPRINTF_O@ @STRERROR_O@ \
|
|||
|
||||
# Additional libs that I know how to build. These will be
|
||||
# linked into the ethereal executable.
|
||||
ethereal_additional_libs = wiretap/libwiretap.a gtk/libui.a epan/libethereal.a
|
||||
ethereal_additional_libs = \
|
||||
wiretap/libwiretap.a \
|
||||
gtk/libui.a \
|
||||
epan/libethereal.a \
|
||||
epan/ftypes/libftypes.a \
|
||||
epan/dfilter/libdfilter.a
|
||||
|
||||
# This is the automake dependency variable for the executable
|
||||
ethereal_DEPENDENCIES = \
|
||||
|
@ -435,7 +440,11 @@ tethereal_static_SOURCES = \
|
|||
|
||||
# Additional libs that I know how to build. These will be
|
||||
# linked into the tethereal executable.
|
||||
tethereal_additional_libs = wiretap/libwiretap.a epan/libethereal.a
|
||||
tethereal_additional_libs = \
|
||||
wiretap/libwiretap.a \
|
||||
epan/libethereal.a \
|
||||
epan/ftypes/libftypes.a \
|
||||
epan/dfilter/libdfilter.a
|
||||
|
||||
# This is the automake dependency variable for the executable
|
||||
tethereal_DEPENDENCIES = \
|
||||
|
@ -516,6 +525,35 @@ randpkt.o: randpkt.c
|
|||
randpkt: randpkt.o wiretap/libwiretap.a
|
||||
$(LINK) -o randpkt randpkt.o wiretap/libwiretap.a `glib-config --libs` -lz
|
||||
|
||||
|
||||
dftest_SOURCES = \
|
||||
$(DISSECTOR_SOURCES) \
|
||||
$(ETHEREAL_COMMON_SOURCES) \
|
||||
dftest.c
|
||||
|
||||
dftest_additional_libs = \
|
||||
epan/libethereal.a \
|
||||
epan/ftypes/libftypes.a \
|
||||
epan/dfilter/libdfilter.a
|
||||
|
||||
dftest_DEPENDENCIES = \
|
||||
$(ethereal_optional_objects) \
|
||||
$(dftest_additional_libs) \
|
||||
plugins/gryphon/gryphon.la \
|
||||
plugins/mgcp/mgcp.la
|
||||
|
||||
# This automake variable adds to the link-line for the executable
|
||||
dftest_LDADD = \
|
||||
$(ethereal_optional_objects) \
|
||||
$(dftest_additional_libs) \
|
||||
@SNMP_LIBS@ \
|
||||
"-dlopen" self \
|
||||
"-dlopen" plugins/gryphon/gryphon.la @GLIB_LIBS@ -lm \
|
||||
"-dlopen" plugins/mgcp/mgcp.la @GLIB_LIBS@ -lm \
|
||||
@PCAP_LIBS@ @SOCKET_LIBS@ @NSL_LIBS@
|
||||
|
||||
dftest_LDFLAGS = -export-dynamic
|
||||
|
||||
DISTCLEANFILES = \
|
||||
register.c \
|
||||
rdps \
|
||||
|
@ -608,7 +646,7 @@ endif
|
|||
#dist-hook:
|
||||
# @rm -f $(distdir)/register.c
|
||||
|
||||
SUBDIRS = wiretap epan plugins packaging @ethereal_SUBDIRS@
|
||||
SUBDIRS = tools wiretap epan plugins packaging @ethereal_SUBDIRS@
|
||||
|
||||
ethereal.1: ethereal doc/ethereal.pod.template
|
||||
(cd doc ; \
|
||||
|
|
36
configure.in
36
configure.in
|
@ -1,4 +1,4 @@
|
|||
# $Id: configure.in,v 1.112 2001/01/18 09:54:09 guy Exp $
|
||||
# $Id: configure.in,v 1.113 2001/02/01 20:21:13 gram Exp $
|
||||
dnl
|
||||
dnl Process this file with autoconf 2.13 or later to produce a
|
||||
dnl configure script; 2.12 doesn't generate a "configure" script that
|
||||
|
@ -237,6 +237,31 @@ AC_SUBST(editcap_bin)
|
|||
AC_SUBST(editcap_man)
|
||||
|
||||
|
||||
# Enable/disable dftest
|
||||
|
||||
AC_ARG_ENABLE(dftest,
|
||||
[ --enable-dftest build dftest. [default=no]],,enable_dftest=no)
|
||||
|
||||
if test "x$enable_dftest" = "xyes" ; then
|
||||
dftest_bin="dftest"
|
||||
else
|
||||
dftest_bin=""
|
||||
fi
|
||||
AC_SUBST(dftest_bin)
|
||||
|
||||
|
||||
# Enable/disable randpkt
|
||||
|
||||
AC_ARG_ENABLE(randpkt,
|
||||
[ --enable-randpkt build randpkt. [default=no]],,enable_randpkt=no)
|
||||
|
||||
if test "x$enable_randpkt" = "xyes" ; then
|
||||
randpkt_bin="randpkt"
|
||||
else
|
||||
randpkt_bin=""
|
||||
fi
|
||||
AC_SUBST(randpkt_bin)
|
||||
|
||||
|
||||
dnl Checks for "gethostbyname()" - and "-lnsl", if we need it to get
|
||||
dnl "gethostbyname()".
|
||||
|
@ -478,7 +503,6 @@ AC_CONFIG_SUBDIRS(epan wiretap)
|
|||
AC_OUTPUT(
|
||||
Makefile
|
||||
doc/Makefile
|
||||
epan/Makefile
|
||||
gtk/Makefile
|
||||
packaging/Makefile
|
||||
packaging/rpm/Makefile
|
||||
|
@ -488,7 +512,10 @@ AC_OUTPUT(
|
|||
packaging/svr4/pkginfo
|
||||
plugins/Makefile
|
||||
plugins/gryphon/Makefile
|
||||
plugins/mgcp/Makefile)
|
||||
plugins/mgcp/Makefile
|
||||
tools/Makefile
|
||||
tools/lemon/Makefile
|
||||
,)
|
||||
|
||||
|
||||
# Pretty messages
|
||||
|
@ -512,6 +539,9 @@ echo "The Ethereal package has been configured with the following options."
|
|||
echo " Build ethereal : $enable_ethereal"
|
||||
echo " Build tethereal : $enable_tethereal"
|
||||
echo " Build editcap : $enable_editcap"
|
||||
echo " Build randpkt : $enable_randpkt"
|
||||
echo " Build dftest : $enable_dftest"
|
||||
echo ""
|
||||
echo " Install setuid : $setuid_message"
|
||||
echo " Use pcap library : $enable_pcap"
|
||||
echo " Use zlib library : $enable_zlib"
|
||||
|
|
|
@ -0,0 +1,150 @@
|
|||
/* dftest.c.c
|
||||
*
|
||||
* $Id: dftest.c,v 1.1 2001/02/01 20:21:13 gram Exp $
|
||||
*
|
||||
* Ethereal - Network traffic analyzer
|
||||
* By Gerald Combs <gerald@zing.org>
|
||||
* Copyright 1998 Gerald Combs
|
||||
*
|
||||
* Shows display filter byte-code, for debugging dfilter routines.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
# include "config.h"
|
||||
#endif
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <locale.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#if 0
|
||||
|
||||
#ifdef HAVE_UNISTD_H
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef HAVE_SYS_TYPES_H
|
||||
#include <sys/types.h>
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_SYS_STAT_H
|
||||
#include <sys/stat.h>
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_FCNTL_H
|
||||
#include <fcntl.h>
|
||||
#endif
|
||||
|
||||
#include <signal.h>
|
||||
|
||||
#ifdef NEED_SNPRINTF_H
|
||||
# include "snprintf.h"
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef NEED_STRERROR_H
|
||||
#include "strerror.h"
|
||||
#endif
|
||||
|
||||
#include <glib.h>
|
||||
#include <epan.h>
|
||||
|
||||
#if 0
|
||||
#include "globals.h"
|
||||
#include "packet.h"
|
||||
#include "file.h"
|
||||
#include "column.h"
|
||||
#include "print.h"
|
||||
#include "resolv.h"
|
||||
#include "conversation.h"
|
||||
#endif
|
||||
#include "timestamp.h"
|
||||
#include "plugins.h"
|
||||
#include "prefs.h"
|
||||
#include "util.h"
|
||||
#include "epan/dfilter/dfilter.h"
|
||||
|
||||
packet_info pi;
|
||||
ts_type timestamp_type = RELATIVE;
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
char *text;
|
||||
char *gpf_path, *pf_path;
|
||||
int gpf_open_errno, pf_open_errno;
|
||||
e_prefs *prefs;
|
||||
dfilter_t *df;
|
||||
|
||||
/* register all dissectors; we must do this before checking for the
|
||||
"-g" flag, as the "-g" flag dumps a list of fields registered
|
||||
by the dissectors, and we must do it before we read the preferences,
|
||||
in case any dissectors register preferences. */
|
||||
epan_init(PLUGIN_DIR);
|
||||
|
||||
/* now register the preferences for any non-dissector modules.
|
||||
we must do that before we read the preferences as well. */
|
||||
prefs_register_modules();
|
||||
|
||||
/* set the c-language locale to the native environment. */
|
||||
setlocale(LC_ALL, "");
|
||||
|
||||
prefs = read_prefs(&gpf_open_errno, &gpf_path, &pf_open_errno, &pf_path);
|
||||
if (gpf_path != NULL) {
|
||||
fprintf(stderr, "can't open global preferences file \"%s\": %s.\n",
|
||||
pf_path, strerror(gpf_open_errno));
|
||||
}
|
||||
if (pf_path != NULL) {
|
||||
fprintf(stderr, "can't open your preferences file \"%s\": %s.\n",
|
||||
pf_path, strerror(pf_open_errno));
|
||||
}
|
||||
|
||||
/* notify all registered modules that have had any of their preferences
|
||||
changed either from one of the preferences file or from the command
|
||||
line that its preferences have changed. */
|
||||
prefs_apply_all();
|
||||
|
||||
/* Check for filter on command line */
|
||||
if (argc <= 1) {
|
||||
fprintf(stderr, "Usage: dftest filter\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* Get filter text */
|
||||
text = get_args_as_string(argc, argv, 1);
|
||||
|
||||
printf("Filter: \"%s\"\n", text);
|
||||
|
||||
/* Compile it */
|
||||
if (!dfilter_compile(text, &df)) {
|
||||
fprintf(stderr, "dftest: %s\n", dfilter_error_msg);
|
||||
epan_cleanup();
|
||||
exit(2);
|
||||
}
|
||||
printf("dfilter ptr = 0x%08x\n", (unsigned int) df);
|
||||
|
||||
printf("\n\n");
|
||||
|
||||
dfilter_dump(df);
|
||||
|
||||
epan_cleanup();
|
||||
exit(0);
|
||||
}
|
|
@ -2,7 +2,7 @@
|
|||
# Automake file for the EPAN library
|
||||
# (Ethereal Protocol ANalyzer Library)
|
||||
#
|
||||
# $Id: Makefile.am,v 1.15 2001/01/17 06:13:39 guy Exp $
|
||||
# $Id: Makefile.am,v 1.16 2001/02/01 20:21:15 gram Exp $
|
||||
#
|
||||
# Ethereal - Network traffic analyzer
|
||||
# By Gerald Combs <gerald@zing.org>
|
||||
|
@ -23,9 +23,7 @@
|
|||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
|
||||
|
||||
# Any POSIX-compatible YACC should honor the -p flag
|
||||
YFLAGS=-d -p dfilter_
|
||||
SUBDIRS = ftypes dfilter
|
||||
|
||||
# EPAN will eventually be a shared library. While I move source code around,
|
||||
# however, it is an archive library.
|
||||
|
@ -41,11 +39,6 @@ libethereal_a_SOURCES = \
|
|||
bitswap.h \
|
||||
conversation.c \
|
||||
conversation.h \
|
||||
dfilter-int.h \
|
||||
dfilter-grammar.y \
|
||||
dfilter-scanner.l \
|
||||
dfilter.c \
|
||||
dfilter.h \
|
||||
epan.c \
|
||||
epan.h \
|
||||
except.c \
|
||||
|
@ -53,6 +46,7 @@ libethereal_a_SOURCES = \
|
|||
exceptions.h \
|
||||
filesystem.c \
|
||||
filesystem.h \
|
||||
gdebug.h \
|
||||
ipv4.c \
|
||||
ipv4.h \
|
||||
packet.c \
|
||||
|
@ -78,9 +72,6 @@ EXTRA_libethereal_a_SOURCES = \
|
|||
|
||||
EXTRA_DIST = \
|
||||
config.h.win32 \
|
||||
dfilter-grammar.c \
|
||||
dfilter-grammar.h \
|
||||
dfilter-scanner.c \
|
||||
Makefile.nmake \
|
||||
tvbtest.c
|
||||
|
||||
|
@ -94,8 +85,6 @@ CLEANFILES = \
|
|||
libethereal_a_LIBADD = @INET_ATON_O@ @INET_PTON_O@ @INET_NTOP_O@
|
||||
libethereal_a_DEPENDENCIES = @INET_ATON_O@ @INET_PTON_O@ @INET_NTOP_O@
|
||||
|
||||
dfilter-scanner.c : dfilter-scanner.l
|
||||
$(LEX) -Pdfilter_ -odfilter-scanner.c $(srcdir)/dfilter-scanner.l
|
||||
|
||||
tvbtest: tvbtest.o tvbuff.o except.o strutil.o
|
||||
$(LINK) -o tvbtest tvbtest.o tvbuff.o except.o strutil.o `glib-config --libs`
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
# $Id: configure.in,v 1.3 2001/01/12 04:18:49 gram Exp $
|
||||
# $Id: configure.in,v 1.4 2001/02/01 20:21:15 gram Exp $
|
||||
dnl
|
||||
dnl Process this file with autoconf 2.13 or later to produce a
|
||||
dnl configure script; 2.12 doesn't generate a "configure" script that
|
||||
|
@ -143,4 +143,8 @@ fi
|
|||
AC_SUBST(INET_NTOP_C)
|
||||
AC_SUBST(INET_NTOP_O)
|
||||
|
||||
AC_OUTPUT(Makefile)
|
||||
AC_OUTPUT(
|
||||
Makefile
|
||||
dfilter/Makefile
|
||||
ftypes/Makefile
|
||||
)
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -1,148 +0,0 @@
|
|||
/* dfilter-int.h
|
||||
* Definitions for routines common to multiple modules in the display
|
||||
* filter code, but not used outside that code.
|
||||
*
|
||||
* $Id: dfilter-int.h,v 1.2 2000/12/22 12:05:36 gram Exp $
|
||||
*
|
||||
* Ethereal - Network traffic analyzer
|
||||
* By Gerald Combs <gerald@zing.org>
|
||||
* 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
*/
|
||||
|
||||
#ifndef __DFILTER_INT_H__
|
||||
#define __DFILTER_INT_H__
|
||||
|
||||
#ifndef __IPV4_H__
|
||||
#include "ipv4.h"
|
||||
#endif
|
||||
|
||||
/* in dfilter-scanner.l */
|
||||
GByteArray *byte_str_to_guint8_array(const char *s);
|
||||
void dfilter_scanner_text(char*);
|
||||
void dfilter_scanner_cleanup(void);
|
||||
|
||||
/* in dfilter-grammar.y */
|
||||
extern dfilter *global_df;
|
||||
|
||||
/* Here we provide interfaces to make our scanner act and look like lex */
|
||||
int dfilter_lex(void);
|
||||
void dfilter_error(char *s);
|
||||
|
||||
/* Report an error during compilation of a filter; this is called by code
|
||||
* other than parser code, so all it does is record that an error occurred,
|
||||
* so that even if the filter is nominally syntactically valid, we still
|
||||
* fail.
|
||||
*/
|
||||
#if __GNUC__ == 2
|
||||
void dfilter_fail(char *fmt, ...)
|
||||
__attribute__((format (printf, 1, 2)));
|
||||
#else
|
||||
void dfilter_fail(char *fmt, ...);
|
||||
#endif
|
||||
|
||||
/* functions that dfilter-grammar.y needs during parsing*/
|
||||
gboolean check_relation_numeric(gint operand, GArray *a, GArray *b);
|
||||
gboolean check_relation_floating(gint operand, GArray *a, GArray *b);
|
||||
gboolean check_relation_ether(gint operand, GArray *a, GArray *b);
|
||||
gboolean check_relation_ipv4(gint operand, GArray *a, GArray *b);
|
||||
gboolean check_relation_ipv6(gint operand, GArray *a, GArray *b);
|
||||
gboolean check_relation_bytes(gint operand, GArray *a, GArray *b);
|
||||
gboolean check_relation_string(gint operand, GArray *a, GArray *b);
|
||||
|
||||
void fill_array_numeric_variable(field_info*, GArray*, const guint8*);
|
||||
void fill_array_floating_variable(field_info*, GArray*, const guint8*);
|
||||
void fill_array_ether_variable(field_info*, GArray*, const guint8*);
|
||||
void fill_array_ipv4_variable(field_info*, GArray*, const guint8*);
|
||||
void fill_array_ipv6_variable(field_info*, GArray*, const guint8*);
|
||||
void fill_array_bytes_variable(field_info*, GArray*, const guint8*);
|
||||
void fill_array_string_variable(field_info*, GArray*, const guint8*);
|
||||
|
||||
gboolean fill_array_numeric_value(GNode *gnode, gpointer data);
|
||||
gboolean fill_array_floating_value(GNode *gnode, gpointer data);
|
||||
gboolean fill_array_ether_value(GNode *gnode, gpointer data);
|
||||
gboolean fill_array_ipv4_value(GNode *gnode, gpointer data);
|
||||
gboolean fill_array_ipv6_value(GNode *gnode, gpointer data);
|
||||
gboolean fill_array_bytes_value(GNode *gnode, gpointer data);
|
||||
gboolean fill_array_string_value(GNode *gnode, gpointer data);
|
||||
|
||||
#ifdef WIN32
|
||||
#define boolean truth_value
|
||||
#endif
|
||||
|
||||
enum node_type {
|
||||
relation, /* eq, ne, gt, ge, lt, le */
|
||||
logical, /* and, or, not, xor */
|
||||
variable, /* protocol or header field id */
|
||||
existence, /* existence of a variable (protocol or hf) */
|
||||
alternation, /* &, | */
|
||||
boolean, /* true, false */
|
||||
numeric, /* uint8, uint16, or uint32 value */
|
||||
floating, /* double */
|
||||
abs_time,
|
||||
string,
|
||||
ether,
|
||||
bytes,
|
||||
ipv4,
|
||||
ipv6,
|
||||
ipxnet
|
||||
};
|
||||
|
||||
typedef gboolean(*CheckRelationFunc) (gint operand, GArray *a, GArray *b);
|
||||
typedef void(*FillArrayFunc) (field_info*, GArray*, const guint8*);
|
||||
|
||||
/* This struct is the parse tree node created by this grammary and used
|
||||
* directly in the display filter routines to filter packets.
|
||||
*/
|
||||
typedef struct dfilter_node {
|
||||
enum node_type ntype; /* from dfilter-grammar.h */
|
||||
int elem_size; /* computed at dfilter parse time rather than
|
||||
when finding elements for each packet. Saves time
|
||||
in get_values_from_ptree() */
|
||||
CheckRelationFunc check_relation_func;
|
||||
FillArrayFunc fill_array_variable_func;
|
||||
GNodeTraverseFunc fill_array_value_func;
|
||||
|
||||
/* copied from proto.h */
|
||||
union {
|
||||
gint relation; /* if type == relation (eq, ne, gt, ge, lt, le) */
|
||||
gint logical; /* if type == logical (and, or, not, xor) */
|
||||
gint variable; /* if type == variable (protocol or header field abbrev) */
|
||||
gint alternation; /* if type == alternation (& or |) */
|
||||
|
||||
guint32 numeric;
|
||||
double floating;
|
||||
struct timeval abs_time; /* the whole struct, not a pointer */
|
||||
gchar *string;
|
||||
guint8 ether[6];
|
||||
ipv4_addr ipv4; /* the whole struct, not a pointer */
|
||||
guint8 ipv6[16];
|
||||
GByteArray *bytes;
|
||||
} value;
|
||||
|
||||
/* used for byte-ranges */
|
||||
gint offset;
|
||||
guint length;
|
||||
|
||||
/* used to indicate range should go to end of sequence */
|
||||
gboolean to_the_end;
|
||||
} dfilter_node;
|
||||
|
||||
/* lookup an abbreviation in our token hash, returing the ID # */
|
||||
int dfilter_lookup_token(char *abbrev);
|
||||
|
||||
#endif /* ! __DFILTER_INT_H__ */
|
|
@ -1,366 +0,0 @@
|
|||
%{
|
||||
|
||||
/* dfilter-scanner.l
|
||||
* Scanner for display filters
|
||||
*
|
||||
* $Id: dfilter-scanner.l,v 1.1 2000/09/27 04:54:48 gram Exp $
|
||||
*
|
||||
* Ethereal - Network traffic analyzer
|
||||
* By Gerald Combs <gerald@zing.org>
|
||||
* 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
# include "config.h"
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_IO_H
|
||||
#include <io.h> /* for isatty() on win32 */
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_SYS_TYPES_H
|
||||
# include <sys/types.h>
|
||||
#endif
|
||||
|
||||
#ifndef _STDIO_H
|
||||
#include <stdio.h>
|
||||
#endif
|
||||
|
||||
#ifndef _STRING_H
|
||||
#include <string.h>
|
||||
#endif
|
||||
|
||||
#ifndef __G_LIB_H__
|
||||
#include <glib.h>
|
||||
#endif
|
||||
|
||||
#ifndef __PROTO_H__
|
||||
#include "proto.h"
|
||||
#endif
|
||||
|
||||
#ifndef __DFILTER_H__
|
||||
#include "dfilter.h"
|
||||
#endif
|
||||
|
||||
#include "dfilter-int.h"
|
||||
|
||||
#include "dfilter-grammar.h"
|
||||
|
||||
/* Flex has a few routines which help us get the scanner to read
|
||||
* from a string rather than from a file. POSIX lex only provides
|
||||
* for reading from a file; any method of reading from a string
|
||||
* is inherently non-portable. Besides reading from a string,
|
||||
* we have to worry about resetting the scanner after a bad
|
||||
* parse; this too is non-portable. Combine the reset with
|
||||
* a string input, and you have major non-portability. I'll provide
|
||||
* the routines for flex here. If you really want to modify the
|
||||
* scanner and use a non-flex lex implementation, you may
|
||||
* add more ifdef's below.
|
||||
*/
|
||||
#ifdef FLEX_SCANNER
|
||||
|
||||
/* Flex has built-in support for using a string as an input source
|
||||
* instead of using a file. Nice!
|
||||
*/
|
||||
YY_BUFFER_STATE string_input_buffer;
|
||||
|
||||
/* We don't need yyunput, so use this macro to get it out of the
|
||||
* generated C file, avoiding a compiler warning about its lack of use */
|
||||
#define YY_NO_UNPUT 1
|
||||
|
||||
#else
|
||||
|
||||
static char *in_buffer;
|
||||
#undef getc
|
||||
#define getc(fp) (*in_buffer == 0 ? EOF : *in_buffer++)
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
%}
|
||||
|
||||
whitespace [\t ]
|
||||
hex [A-Fa-f0-9]{1,2}
|
||||
hexsep [-:\.]
|
||||
minus [-]
|
||||
plus [+]
|
||||
|
||||
%%
|
||||
|
||||
[\t\n ]+ /* ignore whitespace */
|
||||
|
||||
|
||||
and|\&\& { dfilter_lval.operand = TOK_AND; return TOK_AND; }
|
||||
or|\|\| { dfilter_lval.operand = TOK_OR; return TOK_OR; }
|
||||
not|\! { dfilter_lval.operand = TOK_NOT; return TOK_NOT; }
|
||||
xor|\^\^ { dfilter_lval.operand = TOK_XOR; return TOK_XOR; }
|
||||
eq|\=\= { dfilter_lval.operand = TOK_EQ; return TOK_EQ; }
|
||||
ne|\!\= { dfilter_lval.operand = TOK_NE; return TOK_NE; }
|
||||
gt|\> { dfilter_lval.operand = TOK_GT; return TOK_GT; }
|
||||
ge|\>\= { dfilter_lval.operand = TOK_GE; return TOK_GE; }
|
||||
lt|\< { dfilter_lval.operand = TOK_LT; return TOK_LT; }
|
||||
le|\<\= { dfilter_lval.operand = TOK_LE; return TOK_LE; }
|
||||
|
||||
\[{whitespace}*-?[0-9]+{whitespace}*:{whitespace}*[0-9]+{whitespace}*\] { /* range [ x : y ] */
|
||||
|
||||
char *byterange_string = g_strdup(yytext);
|
||||
char *s = byterange_string + 1; /* I don't want the first '[' */
|
||||
char *p;
|
||||
|
||||
/* Get the offset from the string */
|
||||
if ((p = strtok(s, ":"))) {
|
||||
dfilter_lval.byte_range.offset = strtol(p, NULL, 10);
|
||||
}
|
||||
else {
|
||||
g_free(byterange_string);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Get the Length from the string */
|
||||
if ((p = strtok(NULL, "]"))) {
|
||||
dfilter_lval.byte_range.length = strtoul(p, NULL, 10);
|
||||
}
|
||||
else {
|
||||
g_free(byterange_string);
|
||||
return 0;
|
||||
}
|
||||
g_free(byterange_string);
|
||||
return T_VAL_BYTE_RANGE;
|
||||
}
|
||||
|
||||
\[{whitespace}*-?[0-9]+{whitespace}*\] { /* range [ x ] */
|
||||
|
||||
char *byterange_string = g_strdup(yytext);
|
||||
char *s = byterange_string + 1; /* I don't want the first '[' */
|
||||
char *p;
|
||||
|
||||
/* Get the offset from the string */
|
||||
if ((p = strtok(s, "]"))) {
|
||||
dfilter_lval.byte_range.offset = strtol(p, NULL, 10);
|
||||
}
|
||||
else {
|
||||
g_free(byterange_string);
|
||||
return 0;
|
||||
}
|
||||
|
||||
dfilter_lval.byte_range.length = 0;
|
||||
g_free(byterange_string);
|
||||
return T_VAL_BYTE_RANGE;
|
||||
}
|
||||
|
||||
{hex}({hexsep}{hex})+ { /* byte string, any length */
|
||||
dfilter_lval.string = g_strdup(yytext);
|
||||
return T_VAL_BYTE_STRING;
|
||||
}
|
||||
|
||||
|
||||
0[xX][A-Fa-f0-9]+ { /* hex values */
|
||||
dfilter_lval.string = g_strdup(yytext);
|
||||
return T_VAL_UNQUOTED_STRING;
|
||||
}
|
||||
|
||||
[A-Za-z0-9\:][A-Za-z0-9\.\_\-\:]+ {
|
||||
/* looks like a protocol, field name, or hostname */
|
||||
|
||||
int retval = 0;
|
||||
enum ftenum ftype;
|
||||
dfilter_lval.variable.id = dfilter_lookup_token(yytext);
|
||||
if (dfilter_lval.variable.id < 0) {
|
||||
dfilter_lval.string = g_strdup(yytext);
|
||||
return T_VAL_UNQUOTED_STRING;
|
||||
}
|
||||
|
||||
ftype = proto_registrar_get_ftype(dfilter_lval.variable.id);
|
||||
switch (ftype) {
|
||||
case FT_NONE:
|
||||
retval = T_FT_NONE;
|
||||
break;
|
||||
case FT_BOOLEAN:
|
||||
retval = T_FT_BOOLEAN;
|
||||
break;
|
||||
case FT_UINT8:
|
||||
retval = T_FT_UINT8;
|
||||
break;
|
||||
case FT_UINT16:
|
||||
retval = T_FT_UINT16;
|
||||
break;
|
||||
case FT_UINT24:
|
||||
retval = T_FT_UINT24;
|
||||
break;
|
||||
case FT_UINT32:
|
||||
retval = T_FT_UINT32;
|
||||
break;
|
||||
case FT_INT8:
|
||||
retval = T_FT_INT8;
|
||||
break;
|
||||
case FT_INT16:
|
||||
retval = T_FT_INT16;
|
||||
break;
|
||||
case FT_INT24:
|
||||
retval = T_FT_INT24;
|
||||
break;
|
||||
case FT_INT32:
|
||||
retval = T_FT_INT32;
|
||||
break;
|
||||
case FT_DOUBLE:
|
||||
retval = T_FT_DOUBLE;
|
||||
break;
|
||||
case FT_ABSOLUTE_TIME:
|
||||
dfilter_fail("Sorry, you can't filter on field \"%s\", as we don't yet support filtering on time-of-day values.",
|
||||
yytext);
|
||||
retval = 0;
|
||||
break;
|
||||
case FT_RELATIVE_TIME:
|
||||
dfilter_fail("Sorry, you can't filter on field \"%s\", as we don't yet support filtering on time-delta values.",
|
||||
yytext);
|
||||
retval = 0;
|
||||
break;
|
||||
case FT_STRING:
|
||||
case FT_STRINGZ:
|
||||
case FT_UINT_STRING:
|
||||
retval = T_FT_STRING;
|
||||
break;
|
||||
case FT_ETHER:
|
||||
retval = T_FT_ETHER;
|
||||
break;
|
||||
case FT_BYTES:
|
||||
retval = T_FT_BYTES;
|
||||
break;
|
||||
case FT_IPv4:
|
||||
retval = T_FT_IPv4;
|
||||
break;
|
||||
case FT_IPv6:
|
||||
retval = T_FT_IPv6;
|
||||
break;
|
||||
case FT_IPXNET:
|
||||
retval = T_FT_IPXNET;
|
||||
break;
|
||||
default:
|
||||
printf("ftype for %s is %d\n", yytext, ftype);
|
||||
g_assert_not_reached();
|
||||
retval = 0;
|
||||
break;
|
||||
}
|
||||
dfilter_lval.variable.type = retval;
|
||||
return retval;
|
||||
}
|
||||
|
||||
({plus}|{minus})?[0-9]+ { /* decimal and octal integers */
|
||||
dfilter_lval.string = g_strdup(yytext);
|
||||
return T_VAL_UNQUOTED_STRING;
|
||||
}
|
||||
|
||||
({plus}|{minus})?([0-9]+|[0-9]+\.[0-9]+|\.[0-9]+)([eE]({plus}|{minus})?[0-9]+)? {
|
||||
/* I'm trying to capture all floating points here, and
|
||||
* am using the strtod manpage as the description of
|
||||
* valid formats */
|
||||
dfilter_lval.string = g_strdup(yytext);
|
||||
return T_VAL_UNQUOTED_STRING;
|
||||
}
|
||||
|
||||
[0-9\:\.]+ {
|
||||
dfilter_lval.string = g_strdup(yytext);
|
||||
return T_VAL_UNQUOTED_STRING;
|
||||
}
|
||||
|
||||
\"[^"]+\" {
|
||||
int length;
|
||||
|
||||
/* Don't copy the first quote. */
|
||||
dfilter_lval.string = g_strdup(&yytext[1]);
|
||||
|
||||
/* Chop of the final quote mark. */
|
||||
length = strlen(dfilter_lval.string);
|
||||
g_assert(length > 0);
|
||||
dfilter_lval.string[length - 1] = 0;
|
||||
|
||||
return T_VAL_QUOTED_STRING;
|
||||
}
|
||||
|
||||
. return yytext[0];
|
||||
%%
|
||||
|
||||
/* Resets scanner and assigns the char* argument
|
||||
* as the text to scan
|
||||
*/
|
||||
void
|
||||
dfilter_scanner_text(char *text)
|
||||
{
|
||||
#ifdef FLEX_SCANNER
|
||||
string_input_buffer = yy_scan_string(text);
|
||||
#else
|
||||
in_buffer = text;
|
||||
#endif
|
||||
}
|
||||
|
||||
void
|
||||
dfilter_scanner_cleanup(void)
|
||||
{
|
||||
#ifdef FLEX_SCANNER
|
||||
yy_delete_buffer(string_input_buffer);
|
||||
#else
|
||||
/* There is no standard way to reset a lex scanner.
|
||||
* This is necessary after a failed parse on a syntactically
|
||||
* incorrect display filter. You have to reset the scanner
|
||||
* so that yy_lex() doesn't start scanning from the middle
|
||||
* of the previous input string.
|
||||
*/
|
||||
#endif
|
||||
}
|
||||
|
||||
/* Flex has an option '%option noyywrap' so that I don't have to
|
||||
* provide this yywrap function, but in order to maintain portability,
|
||||
* I'll just use this yywrap() function.
|
||||
*/
|
||||
int
|
||||
yywrap()
|
||||
{
|
||||
return 1; /* stop at EOF, instead of looking for next file */
|
||||
}
|
||||
|
||||
/* converts a string representing a byte array
|
||||
* to a guint8 array.
|
||||
*
|
||||
* Returns a non-null GByteArray pointer on success, NULL on failure.
|
||||
*/
|
||||
GByteArray*
|
||||
byte_str_to_guint8_array(const char *s)
|
||||
{
|
||||
GByteArray *barray;
|
||||
guint8 val;
|
||||
char *byte_str;
|
||||
char *p, *str;
|
||||
|
||||
barray = g_byte_array_new();
|
||||
/* XXX - don't use global_df, but pass in pointer to GSList* */
|
||||
global_df->list_of_byte_arrays = g_slist_append(global_df->list_of_byte_arrays, barray);
|
||||
|
||||
/* Local copy of string, since strtok will munge it */
|
||||
byte_str = g_strdup(s);
|
||||
str = byte_str;
|
||||
while ((p = strtok(str, "-:."))) {
|
||||
val = (guint8) strtoul(p, NULL, 16);
|
||||
g_byte_array_append(barray, &val, 1);
|
||||
|
||||
/* subsequent calls to strtok() require NULL as arg 1 */
|
||||
str = NULL;
|
||||
}
|
||||
|
||||
g_free(byte_str);
|
||||
return barray;
|
||||
}
|
1086
epan/dfilter.c
1086
epan/dfilter.c
File diff suppressed because it is too large
Load Diff
|
@ -1,70 +0,0 @@
|
|||
/* dfilter.h
|
||||
* Definitions for display filters
|
||||
*
|
||||
* $Id: dfilter.h,v 1.1 2000/09/27 04:54:49 gram Exp $
|
||||
*
|
||||
* Ethereal - Network traffic analyzer
|
||||
* By Gerald Combs <gerald@zing.org>
|
||||
* 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
*/
|
||||
|
||||
#ifndef __DFILTER_H__
|
||||
#define __DFILTER_H__
|
||||
|
||||
#include "proto.h"
|
||||
|
||||
/* dfilter_error_msg is NULL if there was no error during dfilter_compile,
|
||||
* otherwise it points to a displayable error message. */
|
||||
extern gchar *dfilter_error_msg;
|
||||
extern gchar dfilter_error_msg_buf[1024];
|
||||
|
||||
typedef struct {
|
||||
|
||||
GNode *dftree;
|
||||
|
||||
/* space for dfilter_nodes */
|
||||
GMemChunk *node_memchunk;
|
||||
|
||||
/* list of byte arrays we allocate during parse. We can traverse this list
|
||||
* faster than the tree when we go back and free the byte arrays */
|
||||
GSList *list_of_byte_arrays;
|
||||
|
||||
/* List of strings allocated during parse. */
|
||||
GSList *list_of_strings;
|
||||
|
||||
} dfilter;
|
||||
|
||||
/* Initialization of the symbol table. Called once during program startup */
|
||||
void dfilter_init(void);
|
||||
|
||||
/* Free the memory used by the symbol table. Called at program shutdown */
|
||||
void dfilter_cleanup(void);
|
||||
|
||||
/* Allocate and initialize new dfilter struct. Returns pointer to new dfilter */
|
||||
dfilter* dfilter_new(void);
|
||||
|
||||
/* Frees all memory used by dfilter, and frees dfilter itself */
|
||||
void dfilter_destroy(dfilter *df);
|
||||
|
||||
/* Compile display filter text */
|
||||
int dfilter_compile(gchar* dfilter_text, dfilter** dfp);
|
||||
|
||||
/* Apply compiled dfilter to a proto_tree */
|
||||
gboolean dfilter_apply(dfilter *df, proto_tree *ptree, const guint8* pd, guint pd_len);
|
||||
|
||||
#endif /* ! __DFILTER_H__ */
|
|
@ -0,0 +1,8 @@
|
|||
.cvsignore
|
||||
.deps
|
||||
scanner.c
|
||||
Makefile
|
||||
grammar.h
|
||||
grammar.out
|
||||
Makefile.in
|
||||
grammar.c
|
|
@ -0,0 +1,81 @@
|
|||
# Makefile.am
|
||||
# Automake file for the GTK interface routines for Ethereal
|
||||
#
|
||||
# $Id: Makefile.am,v 1.1 2001/02/01 20:21:18 gram Exp $
|
||||
#
|
||||
# Ethereal - Network traffic analyzer
|
||||
# By Gerald Combs <gerald@zing.org>
|
||||
# Copyright 2001 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.
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
|
||||
# We produce an archive library. In the future, when libethereal is a
|
||||
# shared library, this will be linked into libethereal. While libethereal
|
||||
# is an archive library, any executable linking against libethereal will
|
||||
# also need to link against libftypes.
|
||||
noinst_LIBRARIES = libdfilter.a
|
||||
|
||||
CLEANFILES = \
|
||||
libdfilter.a \
|
||||
*~
|
||||
|
||||
INCLUDES = -I$(srcdir)/../.. -I$(srcdir)/$(LEMON)
|
||||
|
||||
libdfilter_a_SOURCES = \
|
||||
cppmagic.h \
|
||||
dfilter.c \
|
||||
dfilter.h \
|
||||
dfilter-int.h \
|
||||
dfvm.c \
|
||||
dfvm.h \
|
||||
gencode.c \
|
||||
gencode.h \
|
||||
glib-util.c \
|
||||
glib-util.h \
|
||||
grammar.c \
|
||||
grammar.h \
|
||||
scanner.c \
|
||||
semcheck.c \
|
||||
semcheck.h \
|
||||
sttype-pointer.c \
|
||||
sttype-range.c \
|
||||
sttype-range.h \
|
||||
sttype-string.c \
|
||||
sttype-test.c \
|
||||
sttype-test.h \
|
||||
syntax-tree.c \
|
||||
syntax-tree.h
|
||||
|
||||
# Makefile.nmake
|
||||
EXTRA_DIST = \
|
||||
grammar.lemon \
|
||||
lemonflex-head.inc \
|
||||
lemonflex-tail.inc \
|
||||
lemon.c \
|
||||
lempar.c \
|
||||
scanner.l
|
||||
|
||||
scanner.c : scanner.l
|
||||
$(LEX) -Pdf_ -oscanner.c $(srcdir)/scanner.l
|
||||
|
||||
scanner.o : scanner.c grammar.h
|
||||
|
||||
LEMON=../../tools/lemon
|
||||
|
||||
grammar.c grammar.h : grammar.lemon
|
||||
$(LEMON)/lemon t=$(srcdir)/$(LEMON)/lempar.c $(srcdir)/grammar.lemon || \
|
||||
(rm -f grammar.c grammar.h ; false)
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
/* $Id: cppmagic.h,v 1.1 2001/02/01 20:21:18 gram Exp $ */
|
||||
|
||||
/**************************************** CPP definitions ***************/
|
||||
|
||||
/* CPP magic: Concatenate two strings or macros that resolve to strings.
|
||||
* Use CONCAT(), not _CONCAT() */
|
||||
#define _CONCAT(a,b) a ## b
|
||||
#define CONCAT(a,b) _CONCAT(a,b)
|
||||
|
||||
/* CPP magic: Surround a string or a macro that resolves to a string with
|
||||
* double quotes. */
|
||||
#define _STRINGIFY(a) # a
|
||||
#define STRINGIFY(a) _STRINGIFY(a)
|
||||
|
|
@ -0,0 +1,52 @@
|
|||
/* dfilter-int.h
|
||||
* Header information for use by multiple files in the dfilter submodule.
|
||||
*
|
||||
* $Id: dfilter-int.h,v 1.1 2001/02/01 20:21:18 gram Exp $
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef DFILTER_INT_H
|
||||
#define DFILTER_INT_H
|
||||
|
||||
#include "dfilter.h"
|
||||
#include "syntax-tree.h"
|
||||
|
||||
#include "proto.h"
|
||||
|
||||
/* Passed back to user */
|
||||
struct _dfilter_t {
|
||||
GPtrArray *insns;
|
||||
int num_registers;
|
||||
GList **registers;
|
||||
gboolean *attempted_load;
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
/* Syntax Tree stuff */
|
||||
stnode_t *st_root;
|
||||
gboolean syntax_error;
|
||||
GPtrArray *insns;
|
||||
GHashTable *loaded_fields;
|
||||
int next_insn_id;
|
||||
int next_register;
|
||||
} dfwork_t;
|
||||
|
||||
/* Constructor/Destructor prototypes for Lemon Parser */
|
||||
void *DfilterAlloc(void* (*)());
|
||||
void DfilterFree(void*, void (*)());
|
||||
void Dfilter(void*, int, stnode_t*, dfwork_t*);
|
||||
|
||||
/* Scanner's lval */
|
||||
extern stnode_t *df_lval;
|
||||
|
||||
/* Given a field abbreviation, returns the proto ID, or -1 if
|
||||
* it doesn't exist. */
|
||||
header_field_info*
|
||||
dfilter_lookup_token(char *abbrev);
|
||||
|
||||
/* Set dfilter_error_msg_buf and dfilter_error_msg */
|
||||
void
|
||||
dfilter_fail(char *format, ...);
|
||||
|
||||
|
||||
#endif
|
|
@ -0,0 +1,352 @@
|
|||
/* dfilter.c
|
||||
* Main entry point for dfilter routines
|
||||
*
|
||||
* $Id: dfilter.c,v 1.1 2001/02/01 20:21:18 gram Exp $
|
||||
*
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#ifdef NEED_SNPRINTF_H
|
||||
#include "snprintf.h"
|
||||
#endif
|
||||
|
||||
#include "dfilter-int.h"
|
||||
#include "syntax-tree.h"
|
||||
#include "gencode.h"
|
||||
#include "semcheck.h"
|
||||
#include "dfvm.h"
|
||||
|
||||
|
||||
/* Balanced tree of abbreviations and IDs */
|
||||
GTree *dfilter_tokens = NULL;
|
||||
|
||||
#define DFILTER_TOKEN_ID_OFFSET 1
|
||||
|
||||
/* Comparision function for tree insertion. A wrapper around strcmp() */
|
||||
static int g_strcmp(gconstpointer a, gconstpointer b);
|
||||
|
||||
/* Global error message space for dfilter_compile errors */
|
||||
gchar dfilter_error_msg_buf[1024];
|
||||
gchar *dfilter_error_msg; /* NULL when no error resulted */
|
||||
|
||||
/* In proto.c */
|
||||
extern int hf_text_only;
|
||||
|
||||
/* From scanner.c */
|
||||
void df_scanner_text(const char *text);
|
||||
void df_scanner_file(FILE *fh);
|
||||
void df_scanner_cleanup(void);
|
||||
int df_lex(void);
|
||||
|
||||
/* Holds the singular instance of our Lemon parser object */
|
||||
void* ParserObj = NULL;
|
||||
|
||||
void
|
||||
dfilter_fail(char *format, ...)
|
||||
{
|
||||
va_list args;
|
||||
|
||||
/* If we've already reported one error, don't overwite it */
|
||||
if (dfilter_error_msg != NULL)
|
||||
return;
|
||||
|
||||
va_start(args, format);
|
||||
|
||||
vsnprintf(dfilter_error_msg_buf, sizeof(dfilter_error_msg_buf),
|
||||
format, args);
|
||||
dfilter_error_msg = dfilter_error_msg_buf;
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
|
||||
/* Initialize the dfilter module */
|
||||
void
|
||||
dfilter_init(void)
|
||||
{
|
||||
int id, num_symbols;
|
||||
char *abbrev;
|
||||
header_field_info *hfinfo, *same_name_hfinfo;
|
||||
|
||||
num_symbols = proto_registrar_n();
|
||||
|
||||
if (dfilter_tokens) {
|
||||
/* XXX - needed? */
|
||||
g_message("I expected hf_ids to be NULL\n");
|
||||
g_tree_destroy(dfilter_tokens);
|
||||
|
||||
/* Make sure the hfinfo->same_name links are broken */
|
||||
for (id = 0; id < num_symbols; id++) {
|
||||
hfinfo = proto_registrar_get_nth(id);
|
||||
hfinfo->same_name = NULL;
|
||||
}
|
||||
}
|
||||
dfilter_tokens = g_tree_new(g_strcmp);
|
||||
|
||||
/* Populate the abbrev/ID GTree (header-field symbol table) */
|
||||
|
||||
|
||||
for (id = 0; id < num_symbols; id++) {
|
||||
if (id == hf_text_only) {
|
||||
continue;
|
||||
}
|
||||
abbrev = proto_registrar_get_abbrev(id);
|
||||
hfinfo = proto_registrar_get_nth(id);
|
||||
|
||||
g_assert(abbrev); /* Not Null */
|
||||
g_assert(abbrev[0] != 0); /* Not empty string */
|
||||
|
||||
/* We allow multiple hfinfo's to be registered under the same
|
||||
* abbreviation. This was done for X.25 */
|
||||
same_name_hfinfo = g_tree_lookup(dfilter_tokens, abbrev);
|
||||
if (same_name_hfinfo) {
|
||||
/* Set the "same_name" pointer in the hfinfo, then
|
||||
* allow the code after this if{} block to replace the
|
||||
* old hfinfo with the new hfinfo in the GTree. Thus,
|
||||
* we end up with a linked-list of same-named hfinfo's,
|
||||
* with the root of the list being the hfinfo in the GTree */
|
||||
hfinfo->same_name = same_name_hfinfo;
|
||||
|
||||
}
|
||||
g_tree_insert(dfilter_tokens, abbrev, hfinfo);
|
||||
}
|
||||
|
||||
if (ParserObj) {
|
||||
g_message("I expected ParserObj to be NULL\n");
|
||||
/* Free the Lemon Parser object */
|
||||
DfilterFree(ParserObj, g_free);
|
||||
}
|
||||
/* Allocate an instance of our Lemon-based parser */
|
||||
ParserObj = DfilterAlloc(g_malloc);
|
||||
|
||||
/* Initialize the syntax-tree sub-sub-system */
|
||||
sttype_init();
|
||||
}
|
||||
|
||||
/* Clean-up the dfilter module */
|
||||
void
|
||||
dfilter_cleanup(void)
|
||||
{
|
||||
/* Free the abbrev/ID GTree */
|
||||
if (dfilter_tokens) {
|
||||
g_tree_destroy(dfilter_tokens);
|
||||
dfilter_tokens = NULL;
|
||||
}
|
||||
|
||||
/* Free the Lemon Parser object */
|
||||
if (ParserObj) {
|
||||
DfilterFree(ParserObj, g_free);
|
||||
}
|
||||
|
||||
/* Clean up the syntax-tree sub-sub-system */
|
||||
sttype_cleanup();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* Lookup an abbreviation in our token tree, returing the ID #
|
||||
* If the abbreviation doesn't exit, returns -1 */
|
||||
header_field_info*
|
||||
dfilter_lookup_token(char *abbrev)
|
||||
{
|
||||
g_assert(abbrev != NULL);
|
||||
return g_tree_lookup(dfilter_tokens, abbrev);
|
||||
}
|
||||
|
||||
/* String comparison func for dfilter_token GTree */
|
||||
static int
|
||||
g_strcmp(gconstpointer a, gconstpointer b)
|
||||
{
|
||||
return strcmp((const char*)a, (const char*)b);
|
||||
}
|
||||
|
||||
static dfilter_t*
|
||||
dfilter_new(void)
|
||||
{
|
||||
dfilter_t *df;
|
||||
|
||||
df = g_new(dfilter_t, 1);
|
||||
df->insns = NULL;
|
||||
|
||||
return df;
|
||||
}
|
||||
|
||||
/* Given a GPtrArray of instructions (dfvm_insn_t),
|
||||
* free them. */
|
||||
static void
|
||||
free_insns(GPtrArray *insns)
|
||||
{
|
||||
int i;
|
||||
dfvm_insn_t *insn;
|
||||
|
||||
for (i = 0; i < insns->len; i++) {
|
||||
insn = g_ptr_array_index(insns, i);
|
||||
dfvm_insn_free(insn);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
dfilter_free(dfilter_t *df)
|
||||
{
|
||||
if (df->insns) {
|
||||
free_insns(df->insns);
|
||||
}
|
||||
|
||||
g_free(df->registers);
|
||||
g_free(df->attempted_load);
|
||||
g_free(df);
|
||||
}
|
||||
|
||||
|
||||
static dfwork_t*
|
||||
dfwork_new(void)
|
||||
{
|
||||
dfwork_t *dfw;
|
||||
|
||||
dfw = g_new(dfwork_t, 1);
|
||||
|
||||
dfw->st_root = NULL;
|
||||
dfw->syntax_error = FALSE;
|
||||
dfw->insns = NULL;
|
||||
dfw->loaded_fields = NULL;
|
||||
dfw->next_insn_id = 0;
|
||||
dfw->next_register = 0;
|
||||
|
||||
return dfw;
|
||||
}
|
||||
|
||||
static void
|
||||
dfwork_free(dfwork_t *dfw)
|
||||
{
|
||||
if (dfw->st_root) {
|
||||
stnode_free(dfw->st_root);
|
||||
}
|
||||
|
||||
if (dfw->loaded_fields) {
|
||||
g_hash_table_destroy(dfw->loaded_fields);
|
||||
}
|
||||
|
||||
if (dfw->insns) {
|
||||
free_insns(dfw->insns);
|
||||
}
|
||||
|
||||
g_free(dfw);
|
||||
}
|
||||
|
||||
|
||||
gboolean
|
||||
dfilter_compile(gchar *text, dfilter_t **dfp)
|
||||
{
|
||||
int token;
|
||||
dfilter_t *dfilter;
|
||||
dfwork_t *dfw;
|
||||
|
||||
dfilter_error_msg = NULL;
|
||||
|
||||
dfw = dfwork_new();
|
||||
|
||||
df_scanner_text(text);
|
||||
|
||||
while (1) {
|
||||
df_lval = stnode_new(STTYPE_UNINITIALIZED, NULL);
|
||||
token = df_lex();
|
||||
|
||||
/* Check for end-of-input */
|
||||
if (token == 0) {
|
||||
/* Tell the parser that we have reached the end of input */
|
||||
Dfilter(ParserObj, 0, NULL, dfw);
|
||||
|
||||
/* Free the stnode_t that we just generated, since
|
||||
* the parser doesn't know about it and won't free it
|
||||
* for us. */
|
||||
stnode_free(df_lval);
|
||||
df_lval = NULL;
|
||||
break;
|
||||
}
|
||||
|
||||
/* Give the token to the parser */
|
||||
Dfilter(ParserObj, token, df_lval, dfw);
|
||||
|
||||
if (dfw->syntax_error) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* One last check for syntax error (after EOF) */
|
||||
if (dfw->syntax_error) {
|
||||
goto FAILURE;
|
||||
}
|
||||
|
||||
|
||||
/* Success, but was it an empty filter? If so, discard
|
||||
* it and set *dfp to NULL */
|
||||
if (dfw->st_root == NULL) {
|
||||
*dfp = NULL;
|
||||
}
|
||||
else {
|
||||
|
||||
/* Check semantics and do necessary type conversion*/
|
||||
if (!dfw_semcheck(dfw)) {
|
||||
goto FAILURE;
|
||||
}
|
||||
|
||||
/* Create bytecode */
|
||||
dfw_gencode(dfw);
|
||||
|
||||
/* Tuck away the bytecode in the dfilter_t */
|
||||
dfilter = dfilter_new();
|
||||
dfilter->insns = dfw->insns;
|
||||
dfw->insns = NULL;
|
||||
|
||||
/* Initialize run-time space */
|
||||
dfilter->num_registers = dfw->next_register;
|
||||
dfilter->registers = g_new0(GList*, dfilter->num_registers);
|
||||
dfilter->attempted_load = g_new0(gboolean, dfilter->num_registers);
|
||||
|
||||
/* And give it to the user. */
|
||||
*dfp = dfilter;
|
||||
}
|
||||
/* SUCCESS */
|
||||
dfwork_free(dfw);
|
||||
|
||||
/* Reset flex */
|
||||
df_scanner_cleanup();
|
||||
|
||||
return TRUE;
|
||||
|
||||
FAILURE:
|
||||
if (dfw) {
|
||||
dfwork_free(dfw);
|
||||
}
|
||||
dfilter_fail("Unable to parse filter string \"%s\".", text);
|
||||
*dfp = NULL;
|
||||
|
||||
/* Reset flex */
|
||||
df_scanner_cleanup();
|
||||
return FALSE;
|
||||
|
||||
}
|
||||
|
||||
|
||||
gboolean
|
||||
dfilter_apply(dfilter_t *df, tvbuff_t *tvb, proto_tree *tree)
|
||||
{
|
||||
return dfvm_apply(df, tvb, tree);
|
||||
}
|
||||
|
||||
gboolean
|
||||
dfilter_apply_edt(dfilter_t *df, epan_dissect_t* edt)
|
||||
{
|
||||
return dfvm_apply(df, edt->tvb, edt->tree);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
dfilter_dump(dfilter_t *df)
|
||||
{
|
||||
dfvm_dump(stdout, df->insns);
|
||||
}
|
|
@ -0,0 +1,65 @@
|
|||
/* dfilter.h
|
||||
*
|
||||
* $Id: dfilter.h,v 1.1 2001/02/01 20:21:18 gram Exp $
|
||||
*/
|
||||
|
||||
#ifndef DFILTER_H
|
||||
#define DFILTER_H
|
||||
|
||||
#include <glib.h>
|
||||
#include "epan.h"
|
||||
#include "proto.h"
|
||||
|
||||
/* Passed back to user */
|
||||
typedef struct _dfilter_t dfilter_t;
|
||||
|
||||
/* Module-level initialization */
|
||||
void
|
||||
dfilter_init(void);
|
||||
|
||||
/* Module-level cleanup */
|
||||
void
|
||||
dfilter_cleanup(void);
|
||||
|
||||
/* Compiles a string to a dfilter_t.
|
||||
* On success, sets the dfilter* pointed to by dfp
|
||||
* to either a NULL pointer (if the filter is a null
|
||||
* filter, as generated by an all-blank string) or to
|
||||
* a pointer to the newly-allocated dfilter_t
|
||||
* structure.
|
||||
*
|
||||
* On failure, dfilter_error_msg points to an
|
||||
* appropriate error message. This error message is
|
||||
* a global string, so another invocation of
|
||||
* dfilter_compile() will clear it. The dfilter*
|
||||
* will be set to NULL after a failure.
|
||||
*
|
||||
* Returns TRUE on success, FALSE on failure.
|
||||
*/
|
||||
gboolean
|
||||
dfilter_compile(gchar *text, dfilter_t **dfp);
|
||||
|
||||
/* Frees all memory used by dfilter, and frees
|
||||
* the dfilter itself. */
|
||||
void
|
||||
dfilter_free(dfilter_t *df);
|
||||
|
||||
|
||||
/* dfilter_error_msg is NULL if there was no error during dfilter_compile,
|
||||
* otherwise it points to a displayable error message. */
|
||||
extern gchar *dfilter_error_msg;
|
||||
|
||||
/* Apply compiled dfilter */
|
||||
gboolean
|
||||
dfilter_apply_edt(dfilter_t *df, epan_dissect_t* edt);
|
||||
|
||||
/* Apply compiled dfilter */
|
||||
gboolean
|
||||
dfilter_apply(dfilter_t *df, tvbuff_t *tvb, proto_tree *tree);
|
||||
|
||||
|
||||
/* Print bytecode of dfilter to stdout */
|
||||
void
|
||||
dfilter_dump(dfilter_t *df);
|
||||
|
||||
#endif
|
|
@ -0,0 +1,395 @@
|
|||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
#include "dfvm.h"
|
||||
|
||||
dfvm_insn_t*
|
||||
dfvm_insn_new(dfvm_opcode_t op)
|
||||
{
|
||||
dfvm_insn_t *insn;
|
||||
|
||||
insn = g_new(dfvm_insn_t, 1);
|
||||
insn->op = op;
|
||||
insn->arg1 = NULL;
|
||||
insn->arg2 = NULL;
|
||||
insn->arg3 = NULL;
|
||||
insn->arg4 = NULL;
|
||||
return insn;
|
||||
}
|
||||
|
||||
void
|
||||
dfvm_insn_free(dfvm_insn_t *insn)
|
||||
{
|
||||
if (insn->arg1) {
|
||||
dfvm_value_free(insn->arg1);
|
||||
}
|
||||
if (insn->arg2) {
|
||||
dfvm_value_free(insn->arg2);
|
||||
}
|
||||
if (insn->arg3) {
|
||||
dfvm_value_free(insn->arg3);
|
||||
}
|
||||
if (insn->arg4) {
|
||||
dfvm_value_free(insn->arg4);
|
||||
}
|
||||
g_free(insn);
|
||||
}
|
||||
|
||||
|
||||
|
||||
dfvm_value_t*
|
||||
dfvm_value_new(dfvm_value_type_t type)
|
||||
{
|
||||
dfvm_value_t *v;
|
||||
|
||||
v = g_new(dfvm_value_t, 1);
|
||||
v->type = type;
|
||||
return v;
|
||||
}
|
||||
|
||||
void
|
||||
dfvm_value_free(dfvm_value_t *v)
|
||||
{
|
||||
switch (v->type) {
|
||||
case FVALUE:
|
||||
fvalue_free(v->value.fvalue);
|
||||
break;
|
||||
default:
|
||||
/* nothing */
|
||||
;
|
||||
}
|
||||
g_free(v);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
dfvm_dump(FILE *f, GPtrArray *insns)
|
||||
{
|
||||
int id, length;
|
||||
dfvm_insn_t *insn;
|
||||
dfvm_value_t *arg1;
|
||||
dfvm_value_t *arg2;
|
||||
dfvm_value_t *arg3;
|
||||
dfvm_value_t *arg4;
|
||||
|
||||
length = insns->len;
|
||||
|
||||
for (id = 0; id < length; id++) {
|
||||
|
||||
insn = g_ptr_array_index(insns, id);
|
||||
arg1 = insn->arg1;
|
||||
arg2 = insn->arg2;
|
||||
arg3 = insn->arg3;
|
||||
arg4 = insn->arg4;
|
||||
|
||||
switch (insn->op) {
|
||||
case CHECK_EXISTS:
|
||||
fprintf(f, "%05d CHECK_EXISTS\t%s\n",
|
||||
id, proto_registrar_get_abbrev(arg1->value.numeric));
|
||||
break;
|
||||
|
||||
case READ_TREE:
|
||||
fprintf(f, "%05d READ_TREE\t\t%s -> reg#%d\n",
|
||||
id, proto_registrar_get_abbrev(arg1->value.numeric),
|
||||
arg2->value.numeric);
|
||||
break;
|
||||
|
||||
case PUT_FVALUE:
|
||||
fprintf(f, "%05d PUT_FVALUE\t<%s> -> reg#%d\n",
|
||||
id, fvalue_type_name(arg1->value.fvalue),
|
||||
arg2->value.numeric);
|
||||
break;
|
||||
|
||||
case MK_RANGE:
|
||||
fprintf(f, "%05d MK_RANGE\t\treg#%d[%d:%d] -> reg#%d\n",
|
||||
id,
|
||||
arg1->value.numeric,
|
||||
arg3->value.numeric,
|
||||
arg4->value.numeric,
|
||||
arg2->value.numeric);
|
||||
break;
|
||||
|
||||
case ANY_EQ:
|
||||
fprintf(f, "%05d ANY_EQ\t\treg#%d == reg#%d\n",
|
||||
id, arg1->value.numeric, arg2->value.numeric);
|
||||
break;
|
||||
|
||||
case ANY_NE:
|
||||
fprintf(f, "%05d ANY_NE\t\treg#%d == reg#%d\n",
|
||||
id, arg1->value.numeric, arg2->value.numeric);
|
||||
break;
|
||||
|
||||
case ANY_GT:
|
||||
fprintf(f, "%05d ANY_GT\t\treg#%d == reg#%d\n",
|
||||
id, arg1->value.numeric, arg2->value.numeric);
|
||||
break;
|
||||
|
||||
case ANY_GE:
|
||||
fprintf(f, "%05d ANY_GE\t\treg#%d == reg#%d\n",
|
||||
id, arg1->value.numeric, arg2->value.numeric);
|
||||
break;
|
||||
|
||||
case ANY_LT:
|
||||
fprintf(f, "%05d ANY_LT\t\treg#%d == reg#%d\n",
|
||||
id, arg1->value.numeric, arg2->value.numeric);
|
||||
break;
|
||||
|
||||
case ANY_LE:
|
||||
fprintf(f, "%05d ANY_LE\t\treg#%d == reg#%d\n",
|
||||
id, arg1->value.numeric, arg2->value.numeric);
|
||||
break;
|
||||
|
||||
case NOT:
|
||||
fprintf(f, "%05d NOT\n", id);
|
||||
break;
|
||||
|
||||
case RETURN:
|
||||
fprintf(f, "%05d RETURN\n", id);
|
||||
break;
|
||||
|
||||
case IF_TRUE_GOTO:
|
||||
fprintf(f, "%05d IF-TRUE-GOTO\t%d\n",
|
||||
id, arg1->value.numeric);
|
||||
break;
|
||||
|
||||
case IF_FALSE_GOTO:
|
||||
fprintf(f, "%05d IF-FALSE-GOTO\t%d\n",
|
||||
id, arg1->value.numeric);
|
||||
break;
|
||||
|
||||
default:
|
||||
g_assert_not_reached();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Reads a field from the proto_tree and loads the fvalues into a register,
|
||||
* if that field has not already been read. */
|
||||
static gboolean
|
||||
read_tree(dfilter_t *df, proto_tree *tree, int field_id, int reg)
|
||||
{
|
||||
GPtrArray *finfos;
|
||||
field_info *finfo;
|
||||
int i, len;
|
||||
GList *fvalues = NULL;
|
||||
|
||||
/* Already loaded in this run of the dfilter? */
|
||||
if (df->attempted_load[reg]) {
|
||||
if (df->registers[reg]) {
|
||||
return TRUE;
|
||||
}
|
||||
else {
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
df->attempted_load[reg] = TRUE;
|
||||
|
||||
finfos = proto_get_finfo_ptr_array(tree, field_id);
|
||||
if (!finfos) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
len = finfos->len;
|
||||
for (i = 0; i < len; i++) {
|
||||
finfo = g_ptr_array_index(finfos, i);
|
||||
fvalues = g_list_prepend(fvalues, finfo->value);
|
||||
}
|
||||
fvalues = g_list_reverse(fvalues);
|
||||
|
||||
df->registers[reg] = fvalues;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
static gboolean
|
||||
put_fvalue(dfilter_t *df, fvalue_t *fv, int reg)
|
||||
{
|
||||
df->registers[reg] = g_list_append(NULL, fv);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
typedef gboolean (*FvalueCmpFunc)(fvalue_t*, fvalue_t*);
|
||||
|
||||
static gboolean
|
||||
any_test(dfilter_t *df, FvalueCmpFunc cmp, int reg1, int reg2)
|
||||
{
|
||||
GList *list_a, *list_b;
|
||||
|
||||
list_a = df->registers[reg1];
|
||||
|
||||
while (list_a) {
|
||||
list_b = df->registers[reg2];
|
||||
while (list_b) {
|
||||
if (cmp(list_a->data, list_b->data)) {
|
||||
return TRUE;
|
||||
}
|
||||
list_b = g_list_next(list_b);
|
||||
}
|
||||
list_a = g_list_next(list_a);
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
|
||||
/* Free the list nodes w/o freeing the memory that each
|
||||
* list node points to. */
|
||||
static void
|
||||
free_register_overhead(dfilter_t* df)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < df->num_registers; i++) {
|
||||
if (df->registers[i]) {
|
||||
g_list_free(df->registers[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Takes the list of fvalue_t's in a register, uses fvalue_slice()
|
||||
* to make a new list of fvalue_t's (which are ranges, or byte-slices),
|
||||
* and puts the new list into a new register. */
|
||||
static void
|
||||
mk_range(dfilter_t *df, int from_reg, int to_reg, int start, int end)
|
||||
{
|
||||
GList *from_list, *to_list;
|
||||
fvalue_t *old_fv, *new_fv;
|
||||
|
||||
to_list = NULL;
|
||||
from_list = df->registers[from_reg];
|
||||
|
||||
while (from_list) {
|
||||
old_fv = from_list->data;
|
||||
new_fv = fvalue_slice(old_fv, start, end);
|
||||
/* Assert there because semcheck.c should have
|
||||
* already caught the cases in which a slice
|
||||
* cannot be made. */
|
||||
g_assert(new_fv);
|
||||
to_list = g_list_append(to_list, new_fv);
|
||||
|
||||
from_list = g_list_next(from_list);
|
||||
}
|
||||
|
||||
df->registers[to_reg] = to_list;
|
||||
}
|
||||
|
||||
|
||||
|
||||
gboolean
|
||||
dfvm_apply(dfilter_t *df, tvbuff_t *tvb, proto_tree *tree)
|
||||
{
|
||||
int i, id, length;
|
||||
gboolean accum = TRUE;
|
||||
dfvm_insn_t *insn;
|
||||
dfvm_value_t *arg1;
|
||||
dfvm_value_t *arg2;
|
||||
dfvm_value_t *arg3;
|
||||
dfvm_value_t *arg4;
|
||||
|
||||
g_assert(tvb);
|
||||
g_assert(tree);
|
||||
|
||||
|
||||
/* Clear registers */
|
||||
for (i = 0; i < df->num_registers; i++) {
|
||||
df->registers[i] = NULL;
|
||||
df->attempted_load[i] = FALSE;
|
||||
}
|
||||
|
||||
length = df->insns->len;
|
||||
|
||||
for (id = 0; id < length; id++) {
|
||||
|
||||
AGAIN:
|
||||
insn = g_ptr_array_index(df->insns, id);
|
||||
arg1 = insn->arg1;
|
||||
arg2 = insn->arg2;
|
||||
|
||||
switch (insn->op) {
|
||||
case CHECK_EXISTS:
|
||||
accum = proto_check_for_protocol_or_field(tree,
|
||||
arg1->value.numeric);
|
||||
break;
|
||||
|
||||
case READ_TREE:
|
||||
accum = read_tree(df, tree,
|
||||
arg1->value.numeric, arg2->value.numeric);
|
||||
break;
|
||||
|
||||
case PUT_FVALUE:
|
||||
accum = put_fvalue(df,
|
||||
arg1->value.fvalue, arg2->value.numeric);
|
||||
break;
|
||||
|
||||
case MK_RANGE:
|
||||
arg3 = insn->arg3;
|
||||
arg4 = insn->arg4;
|
||||
mk_range(df,
|
||||
arg1->value.numeric, arg2->value.numeric,
|
||||
arg3->value.numeric, arg4->value.numeric);
|
||||
break;
|
||||
|
||||
case ANY_EQ:
|
||||
accum = any_test(df, fvalue_eq,
|
||||
arg1->value.numeric, arg2->value.numeric);
|
||||
break;
|
||||
|
||||
case ANY_NE:
|
||||
accum = any_test(df, fvalue_ne,
|
||||
arg1->value.numeric, arg2->value.numeric);
|
||||
break;
|
||||
|
||||
case ANY_GT:
|
||||
accum = any_test(df, fvalue_gt,
|
||||
arg1->value.numeric, arg2->value.numeric);
|
||||
break;
|
||||
|
||||
case ANY_GE:
|
||||
accum = any_test(df, fvalue_ge,
|
||||
arg1->value.numeric, arg2->value.numeric);
|
||||
break;
|
||||
|
||||
case ANY_LT:
|
||||
accum = any_test(df, fvalue_lt,
|
||||
arg1->value.numeric, arg2->value.numeric);
|
||||
break;
|
||||
|
||||
case ANY_LE:
|
||||
accum = any_test(df, fvalue_le,
|
||||
arg1->value.numeric, arg2->value.numeric);
|
||||
break;
|
||||
|
||||
case NOT:
|
||||
accum = !accum;
|
||||
break;
|
||||
|
||||
case RETURN:
|
||||
free_register_overhead(df);
|
||||
return accum;
|
||||
|
||||
case IF_TRUE_GOTO:
|
||||
if (accum) {
|
||||
id = arg1->value.numeric;
|
||||
goto AGAIN;
|
||||
}
|
||||
break;
|
||||
|
||||
case IF_FALSE_GOTO:
|
||||
if (!accum) {
|
||||
id = arg1->value.numeric;
|
||||
goto AGAIN;
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
default:
|
||||
g_assert_not_reached();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
g_assert_not_reached();
|
||||
return FALSE; /* to appease the compiler */
|
||||
}
|
|
@ -0,0 +1,77 @@
|
|||
#ifndef DFVM_H
|
||||
#define DFVM_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include "proto.h"
|
||||
#include "dfilter-int.h"
|
||||
#include "syntax-tree.h"
|
||||
|
||||
typedef enum {
|
||||
EMPTY,
|
||||
FVALUE,
|
||||
FIELD_ID,
|
||||
INSN_NUMBER,
|
||||
REGISTER,
|
||||
INTEGER
|
||||
} dfvm_value_type_t;
|
||||
|
||||
typedef struct {
|
||||
dfvm_value_type_t type;
|
||||
|
||||
union {
|
||||
fvalue_t *fvalue;
|
||||
guint32 numeric;
|
||||
} value;
|
||||
|
||||
} dfvm_value_t;
|
||||
|
||||
|
||||
typedef enum {
|
||||
|
||||
IF_TRUE_GOTO,
|
||||
IF_FALSE_GOTO,
|
||||
CHECK_EXISTS,
|
||||
NOT,
|
||||
RETURN,
|
||||
READ_TREE,
|
||||
PUT_FVALUE,
|
||||
ANY_EQ,
|
||||
ANY_NE,
|
||||
ANY_GT,
|
||||
ANY_GE,
|
||||
ANY_LT,
|
||||
ANY_LE,
|
||||
MK_RANGE
|
||||
|
||||
} dfvm_opcode_t;
|
||||
|
||||
typedef struct {
|
||||
int id;
|
||||
int LHS;
|
||||
dfvm_opcode_t op;
|
||||
dfvm_value_t *arg1;
|
||||
dfvm_value_t *arg2;
|
||||
dfvm_value_t *arg3;
|
||||
dfvm_value_t *arg4;
|
||||
} dfvm_insn_t;
|
||||
|
||||
dfvm_insn_t*
|
||||
dfvm_insn_new(dfvm_opcode_t op);
|
||||
|
||||
void
|
||||
dfvm_insn_free(dfvm_insn_t *insn);
|
||||
|
||||
dfvm_value_t*
|
||||
dfvm_value_new(dfvm_value_type_t type);
|
||||
|
||||
void
|
||||
dfvm_value_free(dfvm_value_t *v);
|
||||
|
||||
void
|
||||
dfvm_dump(FILE *f, GPtrArray *insns);
|
||||
|
||||
gboolean
|
||||
dfvm_apply(dfilter_t *df, tvbuff_t *tvb, proto_tree *tree);
|
||||
|
||||
|
||||
#endif
|
|
@ -0,0 +1,299 @@
|
|||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
#include "dfilter-int.h"
|
||||
#include "dfvm.h"
|
||||
#include "syntax-tree.h"
|
||||
#include "sttype-range.h"
|
||||
#include "sttype-test.h"
|
||||
#include "ftypes/ftypes.h"
|
||||
#include "gdebug.h"
|
||||
|
||||
static void
|
||||
gencode(dfwork_t *dfw, stnode_t *st_node);
|
||||
|
||||
static void
|
||||
dfw_append_insn(dfwork_t *dfw, dfvm_insn_t *insn)
|
||||
{
|
||||
insn->id = dfw->next_insn_id;
|
||||
dfw->next_insn_id++;
|
||||
g_ptr_array_add(dfw->insns, insn);
|
||||
}
|
||||
|
||||
/* returns register number */
|
||||
static int
|
||||
dfw_append_read_tree(dfwork_t *dfw, int field_id)
|
||||
{
|
||||
dfvm_insn_t *insn;
|
||||
dfvm_value_t *val1, *val2;
|
||||
int reg = -1;
|
||||
|
||||
/* Keep track of which registers
|
||||
* were used for which field_id's so that we
|
||||
* can re-use registers. */
|
||||
reg = GPOINTER_TO_UINT(
|
||||
g_hash_table_lookup(dfw->loaded_fields,
|
||||
GUINT_TO_POINTER(field_id)));
|
||||
if (reg) {
|
||||
/* Reg's are stored in has as reg+1, so
|
||||
* that the non-existence of a field_id in
|
||||
* the hash, or 0, can be differentiated from
|
||||
* a field_id being loaded into register #0. */
|
||||
reg--;
|
||||
}
|
||||
else {
|
||||
reg = dfw->next_register++;
|
||||
g_hash_table_insert(dfw->loaded_fields,
|
||||
GUINT_TO_POINTER(field_id),
|
||||
GUINT_TO_POINTER(reg + 1));
|
||||
}
|
||||
|
||||
insn = dfvm_insn_new(READ_TREE);
|
||||
val1 = dfvm_value_new(FIELD_ID);
|
||||
val1->value.numeric = field_id;
|
||||
val2 = dfvm_value_new(REGISTER);
|
||||
val2->value.numeric = reg;
|
||||
|
||||
insn->arg1 = val1;
|
||||
insn->arg2 = val2;
|
||||
dfw_append_insn(dfw, insn);
|
||||
|
||||
return reg;
|
||||
}
|
||||
|
||||
/* returns register number */
|
||||
static int
|
||||
dfw_append_put_fvalue(dfwork_t *dfw, fvalue_t *fv)
|
||||
{
|
||||
dfvm_insn_t *insn;
|
||||
dfvm_value_t *val1, *val2;
|
||||
int reg;
|
||||
|
||||
insn = dfvm_insn_new(PUT_FVALUE);
|
||||
val1 = dfvm_value_new(FVALUE);
|
||||
val1->value.fvalue = fv;
|
||||
val2 = dfvm_value_new(REGISTER);
|
||||
reg = dfw->next_register++;
|
||||
val2->value.numeric = reg;
|
||||
insn->arg1 = val1;
|
||||
insn->arg2 = val2;
|
||||
dfw_append_insn(dfw, insn);
|
||||
|
||||
return reg;
|
||||
}
|
||||
|
||||
/* returns register number */
|
||||
static int
|
||||
dfw_append_mk_range(dfwork_t *dfw, stnode_t *node)
|
||||
{
|
||||
int hf_reg, reg;
|
||||
header_field_info *hfinfo;
|
||||
dfvm_insn_t *insn;
|
||||
dfvm_value_t *val;
|
||||
|
||||
hfinfo = sttype_range_hfinfo(node);
|
||||
hf_reg = dfw_append_read_tree(dfw, hfinfo->id);
|
||||
|
||||
insn = dfvm_insn_new(MK_RANGE);
|
||||
|
||||
val = dfvm_value_new(REGISTER);
|
||||
val->value.numeric = hf_reg;
|
||||
insn->arg1 = val;
|
||||
|
||||
val = dfvm_value_new(REGISTER);
|
||||
reg =dfw->next_register++;
|
||||
val->value.numeric = reg;
|
||||
insn->arg2 = val;
|
||||
|
||||
val = dfvm_value_new(INTEGER);
|
||||
val->value.numeric = sttype_range_start(node);
|
||||
insn->arg3 = val;
|
||||
|
||||
val = dfvm_value_new(INTEGER);
|
||||
val->value.numeric = sttype_range_end(node);
|
||||
insn->arg4 = val;
|
||||
|
||||
dfw_append_insn(dfw, insn);
|
||||
|
||||
return reg;
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
gen_relation(dfwork_t *dfw, dfvm_opcode_t op, stnode_t *st_arg1, stnode_t *st_arg2)
|
||||
{
|
||||
sttype_id_t type1, type2;
|
||||
dfvm_insn_t *insn;
|
||||
dfvm_value_t *val1, *val2;
|
||||
dfvm_value_t *jmp1 = NULL, *jmp2 = NULL;
|
||||
int reg1 = -1, reg2 = -1;
|
||||
header_field_info *hfinfo;
|
||||
|
||||
fvalue_t *junk = NULL;
|
||||
|
||||
type1 = stnode_type_id(st_arg1);
|
||||
type2 = stnode_type_id(st_arg2);
|
||||
|
||||
if (type1 == STTYPE_FIELD) {
|
||||
hfinfo = stnode_data(st_arg1);
|
||||
reg1 = dfw_append_read_tree(dfw, hfinfo->id);
|
||||
|
||||
insn = dfvm_insn_new(IF_FALSE_GOTO);
|
||||
jmp1 = dfvm_value_new(INSN_NUMBER);
|
||||
insn->arg1 = jmp1;
|
||||
dfw_append_insn(dfw, insn);
|
||||
}
|
||||
else if (type1 == STTYPE_FVALUE) {
|
||||
reg1 = dfw_append_put_fvalue(dfw, stnode_data(st_arg1));
|
||||
}
|
||||
else if (type1 == STTYPE_RANGE) {
|
||||
reg1 = dfw_append_mk_range(dfw, st_arg1);
|
||||
}
|
||||
else {
|
||||
g_assert_not_reached();
|
||||
}
|
||||
|
||||
if (type2 == STTYPE_FIELD) {
|
||||
hfinfo = stnode_data(st_arg2);
|
||||
reg2 = dfw_append_read_tree(dfw, hfinfo->id);
|
||||
|
||||
insn = dfvm_insn_new(IF_FALSE_GOTO);
|
||||
jmp2 = dfvm_value_new(INSN_NUMBER);
|
||||
insn->arg1 = jmp2;
|
||||
dfw_append_insn(dfw, insn);
|
||||
}
|
||||
else if (type2 == STTYPE_FVALUE) {
|
||||
reg2 = dfw_append_put_fvalue(dfw, stnode_data(st_arg2));
|
||||
}
|
||||
else {
|
||||
g_assert_not_reached();
|
||||
}
|
||||
|
||||
insn = dfvm_insn_new(op);
|
||||
val1 = dfvm_value_new(REGISTER);
|
||||
val1->value.numeric = reg1;
|
||||
val2 = dfvm_value_new(REGISTER);
|
||||
val2->value.numeric = reg2;
|
||||
insn->arg1 = val1;
|
||||
insn->arg2 = val2;
|
||||
dfw_append_insn(dfw, insn);
|
||||
|
||||
if (jmp1) {
|
||||
jmp1->value.numeric = dfw->next_insn_id;
|
||||
}
|
||||
|
||||
if (jmp2) {
|
||||
jmp2->value.numeric = dfw->next_insn_id;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
gen_test(dfwork_t *dfw, stnode_t *st_node)
|
||||
{
|
||||
test_op_t st_op;
|
||||
stnode_t *st_arg1, *st_arg2;
|
||||
dfvm_value_t *val1;
|
||||
dfvm_insn_t *insn;
|
||||
|
||||
header_field_info *hfinfo;
|
||||
|
||||
sttype_test_get(st_node, &st_op, &st_arg1, &st_arg2);
|
||||
|
||||
switch (st_op) {
|
||||
case TEST_OP_UNINITIALIZED:
|
||||
g_assert_not_reached();
|
||||
break;
|
||||
|
||||
case TEST_OP_EXISTS:
|
||||
val1 = dfvm_value_new(FIELD_ID);
|
||||
hfinfo = stnode_data(st_arg1);
|
||||
val1->value.numeric = hfinfo->id;
|
||||
insn = dfvm_insn_new(CHECK_EXISTS);
|
||||
insn->arg1 = val1;
|
||||
dfw_append_insn(dfw, insn);
|
||||
break;
|
||||
|
||||
case TEST_OP_NOT:
|
||||
gencode(dfw, st_arg1);
|
||||
insn = dfvm_insn_new(NOT);
|
||||
dfw_append_insn(dfw, insn);
|
||||
break;
|
||||
|
||||
case TEST_OP_AND:
|
||||
gencode(dfw, st_arg1);
|
||||
|
||||
insn = dfvm_insn_new(IF_FALSE_GOTO);
|
||||
val1 = dfvm_value_new(INSN_NUMBER);
|
||||
insn->arg1 = val1;
|
||||
dfw_append_insn(dfw, insn);
|
||||
|
||||
gencode(dfw, st_arg2);
|
||||
val1->value.numeric = dfw->next_insn_id;
|
||||
break;
|
||||
|
||||
case TEST_OP_OR:
|
||||
gencode(dfw, st_arg1);
|
||||
|
||||
insn = dfvm_insn_new(IF_TRUE_GOTO);
|
||||
val1 = dfvm_value_new(INSN_NUMBER);
|
||||
insn->arg1 = val1;
|
||||
dfw_append_insn(dfw, insn);
|
||||
|
||||
gencode(dfw, st_arg2);
|
||||
val1->value.numeric = dfw->next_insn_id;
|
||||
break;
|
||||
|
||||
case TEST_OP_EQ:
|
||||
gen_relation(dfw, ANY_EQ, st_arg1, st_arg2);
|
||||
break;
|
||||
|
||||
case TEST_OP_NE:
|
||||
gen_relation(dfw, ANY_NE, st_arg1, st_arg2);
|
||||
break;
|
||||
|
||||
case TEST_OP_GT:
|
||||
gen_relation(dfw, ANY_GT, st_arg1, st_arg2);
|
||||
break;
|
||||
|
||||
case TEST_OP_GE:
|
||||
gen_relation(dfw, ANY_GE, st_arg1, st_arg2);
|
||||
break;
|
||||
|
||||
case TEST_OP_LT:
|
||||
gen_relation(dfw, ANY_LT, st_arg1, st_arg2);
|
||||
break;
|
||||
|
||||
case TEST_OP_LE:
|
||||
gen_relation(dfw, ANY_LE, st_arg1, st_arg2);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
gencode(dfwork_t *dfw, stnode_t *st_node)
|
||||
{
|
||||
const char *name;
|
||||
|
||||
name = stnode_type_name(st_node);
|
||||
|
||||
switch (stnode_type_id(st_node)) {
|
||||
case STTYPE_TEST:
|
||||
gen_test(dfw, st_node);
|
||||
break;
|
||||
default:
|
||||
g_assert_not_reached();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
dfw_gencode(dfwork_t *dfw)
|
||||
{
|
||||
dfw->insns = g_ptr_array_new();
|
||||
dfw->loaded_fields = g_hash_table_new(g_direct_hash, g_direct_equal);
|
||||
gencode(dfw, dfw->st_root);
|
||||
dfw_append_insn(dfw, dfvm_insn_new(RETURN));
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
#ifndef GENCODE_H
|
||||
#define GENCODE_H
|
||||
|
||||
void
|
||||
dfw_gencode(dfwork_t *dfw);
|
||||
|
||||
#endif
|
|
@ -0,0 +1,47 @@
|
|||
/* $Id: glib-util.c,v 1.1 2001/02/01 20:21:18 gram Exp $ */
|
||||
|
||||
#include <string.h>
|
||||
#include <glib.h>
|
||||
|
||||
|
||||
#include <glib-util.h>
|
||||
|
||||
char*
|
||||
g_substrdup(const char *s, int start, int len)
|
||||
{
|
||||
int s_len, abs_start, abs_len;
|
||||
char *newstring;
|
||||
|
||||
|
||||
s_len = strlen(s);
|
||||
|
||||
if (start < 0) {
|
||||
abs_start = s_len + start;
|
||||
if (abs_start < 0) {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
else {
|
||||
abs_start = start;
|
||||
}
|
||||
|
||||
if (len < 0) {
|
||||
abs_len = s_len + len + 1 - abs_start;
|
||||
if (abs_len < 0) {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
else {
|
||||
abs_len = len;
|
||||
}
|
||||
|
||||
|
||||
if (abs_start + abs_len > s_len) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
newstring = g_strndup(s + abs_start, abs_len + 1);
|
||||
newstring[abs_len] = 0;
|
||||
|
||||
return newstring;
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
/* $Id: glib-util.h,v 1.1 2001/02/01 20:21:18 gram Exp $ */
|
||||
|
||||
char*
|
||||
g_substrdup(const char *s, int start, int len);
|
|
@ -0,0 +1,185 @@
|
|||
/* $Id: grammar.lemon,v 1.1 2001/02/01 20:21:18 gram Exp $ */
|
||||
|
||||
%include {
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
#include "dfilter-int.h"
|
||||
#include "syntax-tree.h"
|
||||
#include "sttype-range.h"
|
||||
#include "sttype-test.h"
|
||||
|
||||
/*extern char *df_text;*/
|
||||
|
||||
/* End of C code */
|
||||
}
|
||||
|
||||
/* Parser Information */
|
||||
%name Dfilter
|
||||
%token_prefix TOKEN_
|
||||
%extra_argument {dfwork_t *dfw}
|
||||
|
||||
/* Terminal and Non-Terminal types and destructors */
|
||||
%token_type {stnode_t*}
|
||||
%token_destructor {stnode_free($$);}
|
||||
|
||||
%type sentence {stnode_t*}
|
||||
%destructor sentence {stnode_free($$);}
|
||||
|
||||
%type expr {stnode_t*}
|
||||
%destructor expr {stnode_free($$);}
|
||||
|
||||
%type entity {stnode_t*}
|
||||
%destructor entity {stnode_free($$);}
|
||||
|
||||
%type relation_test {stnode_t*}
|
||||
%destructor relation_test {stnode_free($$);}
|
||||
|
||||
%type logical_test {stnode_t*}
|
||||
%destructor logical_test {stnode_free($$);}
|
||||
|
||||
%type rel_op2 {test_op_t}
|
||||
|
||||
%type range {stnode_t*}
|
||||
%destructor range {stnode_free($$);}
|
||||
|
||||
/* This is called as soon as a syntax error happens. After that,
|
||||
any "error" symbols are shifted, if possible. */
|
||||
%syntax_error {
|
||||
|
||||
header_field_info *hfinfo;
|
||||
|
||||
if (!TOKEN) {
|
||||
dfilter_fail("Unexpected end of filter string.");
|
||||
return;
|
||||
}
|
||||
|
||||
switch(stnode_type_id(TOKEN)) {
|
||||
case STTYPE_UNINITIALIZED:
|
||||
dfilter_fail("Syntax error.");
|
||||
break;
|
||||
case STTYPE_TEST:
|
||||
dfilter_fail("Syntax error, TEST.");
|
||||
break;
|
||||
case STTYPE_STRING:
|
||||
dfilter_fail("The string \"%s\" was unexpected in this context.",
|
||||
stnode_data(TOKEN));
|
||||
break;
|
||||
case STTYPE_FIELD:
|
||||
hfinfo = stnode_data(TOKEN);
|
||||
dfilter_fail("Syntax error near \"%s\".", hfinfo->abbrev);
|
||||
break;
|
||||
|
||||
/* These aren't handed to use as terminal tokens from
|
||||
the scanner, so was can assert that we'll never
|
||||
see them here. */
|
||||
case STTYPE_NUM_TYPES:
|
||||
case STTYPE_RANGE:
|
||||
case STTYPE_FVALUE:
|
||||
g_assert_not_reached();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* When a parse fails, mark an error. This occurs after
|
||||
the above syntax_error code and after the parser fails to
|
||||
use error recovery, shifting an "error" symbol and successfully
|
||||
shifting 3 more symbols. */
|
||||
%parse_failure {
|
||||
dfw->syntax_error = TRUE;
|
||||
}
|
||||
|
||||
/* ----------------- The grammar -------------- */
|
||||
|
||||
/* Associativity */
|
||||
%left TEST_AND.
|
||||
%left TEST_OR.
|
||||
%nonassoc TEST_EQ TEST_NE TEST_LT TEST_LE TEST_GT TEST_GE.
|
||||
%right TEST_NOT.
|
||||
|
||||
/* Top-level targets */
|
||||
sentence ::= expr(X). { dfw->st_root = X; }
|
||||
sentence ::= . { dfw->st_root = NULL; }
|
||||
|
||||
expr(X) ::= relation_test(R). { X = R; }
|
||||
expr(X) ::= logical_test(L). { X = L; }
|
||||
|
||||
expr(X) ::= LPAREN expr(Y) RPAREN.
|
||||
{
|
||||
X = Y;
|
||||
}
|
||||
|
||||
|
||||
/* Logical tests */
|
||||
logical_test(T) ::= expr(E) TEST_AND expr(F).
|
||||
{
|
||||
T = stnode_new(STTYPE_TEST, NULL);
|
||||
sttype_test_set2(T, TEST_OP_AND, E, F);
|
||||
}
|
||||
|
||||
logical_test(T) ::= expr(E) TEST_OR expr(F).
|
||||
{
|
||||
T = stnode_new(STTYPE_TEST, NULL);
|
||||
sttype_test_set2(T, TEST_OP_OR, E, F);
|
||||
}
|
||||
|
||||
logical_test(T) ::= TEST_NOT expr(E).
|
||||
{
|
||||
T = stnode_new(STTYPE_TEST, NULL);
|
||||
sttype_test_set1(T, TEST_OP_NOT, E);
|
||||
}
|
||||
|
||||
logical_test(T) ::= FIELD(F).
|
||||
{
|
||||
T = stnode_new(STTYPE_TEST, NULL);
|
||||
sttype_test_set1(T, TEST_OP_EXISTS, F);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* Entities, or things that can be compared/tested/checked */
|
||||
entity(E) ::= FIELD(F). { E = F; }
|
||||
entity(E) ::= STRING(S). { E = S; }
|
||||
entity(E) ::= range(R). { E = R; }
|
||||
|
||||
range(R) ::= FIELD(F) LBRACKET STRING(X) COLON STRING(Y) RBRACKET.
|
||||
{
|
||||
R = stnode_new(STTYPE_RANGE, NULL);
|
||||
sttype_range_set(R, F, X, Y);
|
||||
}
|
||||
|
||||
range(R) ::= FIELD(F) LBRACKET STRING(X) COLON RBRACKET.
|
||||
{
|
||||
R = stnode_new(STTYPE_RANGE, NULL);
|
||||
sttype_range_set(R, F, X, NULL);
|
||||
}
|
||||
|
||||
range(R) ::= FIELD(F) LBRACKET COLON STRING(Y) RBRACKET.
|
||||
{
|
||||
R = stnode_new(STTYPE_RANGE, NULL);
|
||||
sttype_range_set(R, F, NULL, Y);
|
||||
}
|
||||
|
||||
range(R) ::= FIELD(F) LBRACKET STRING(Y) RBRACKET.
|
||||
{
|
||||
R = stnode_new(STTYPE_RANGE, NULL);
|
||||
sttype_range_set1(R, F, Y);
|
||||
}
|
||||
|
||||
/* Relational tests */
|
||||
relation_test(T) ::= entity(E) rel_op2(O) entity(F).
|
||||
{
|
||||
T = stnode_new(STTYPE_TEST, NULL);
|
||||
sttype_test_set2(T, O, E, F);
|
||||
}
|
||||
|
||||
rel_op2(O) ::= TEST_EQ. { O = TEST_OP_EQ; }
|
||||
rel_op2(O) ::= TEST_NE. { O = TEST_OP_NE; }
|
||||
rel_op2(O) ::= TEST_GT. { O = TEST_OP_GT; }
|
||||
rel_op2(O) ::= TEST_GE. { O = TEST_OP_GE; }
|
||||
rel_op2(O) ::= TEST_LT. { O = TEST_OP_LT; }
|
||||
rel_op2(O) ::= TEST_LE. { O = TEST_OP_LE; }
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,157 @@
|
|||
%{
|
||||
/* scanner.l
|
||||
* Scanner for Ethereal's dfilter language
|
||||
*
|
||||
* $Id: scanner.l,v 1.1 2001/02/01 20:21:18 gram Exp $
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
#include "glib-util.h"
|
||||
#include "dfilter-int.h"
|
||||
#include "syntax-tree.h"
|
||||
#include "grammar.h"
|
||||
|
||||
#define LVAL df_lval
|
||||
#define LVAL_TYPE stnode_t*
|
||||
#define LVAL_INIT_VAL NULL
|
||||
#define MODNAME df
|
||||
|
||||
#include <lemonflex-head.inc>
|
||||
|
||||
/*#undef YY_NO_UNPUT*/
|
||||
|
||||
int set_lval(int token, gpointer data);
|
||||
int simple(int token);
|
||||
|
||||
%}
|
||||
|
||||
%x RANGE
|
||||
|
||||
BWCHARS [[:alnum:]\[\]\-_.+!@#%^&*=/:]
|
||||
INITVAR [_A-Za-z]
|
||||
VARCHARS [[:alnum:]_]
|
||||
|
||||
|
||||
%%
|
||||
|
||||
[[:blank:]\n]+ /* ignore whitespace */
|
||||
|
||||
|
||||
|
||||
"(" return simple(TOKEN_LPAREN);
|
||||
")" return simple(TOKEN_RPAREN);
|
||||
|
||||
"==" return simple(TOKEN_TEST_EQ);
|
||||
"eq" return simple(TOKEN_TEST_EQ);
|
||||
"!=" return simple(TOKEN_TEST_NE);
|
||||
"ne" return simple(TOKEN_TEST_NE);
|
||||
">" return simple(TOKEN_TEST_GT);
|
||||
"gt" return simple(TOKEN_TEST_GT);
|
||||
">=" return simple(TOKEN_TEST_GE);
|
||||
"ge" return simple(TOKEN_TEST_GE);
|
||||
"<" return simple(TOKEN_TEST_LT);
|
||||
"lt" return simple(TOKEN_TEST_LT);
|
||||
"<=" return simple(TOKEN_TEST_LE);
|
||||
"le" return simple(TOKEN_TEST_LE);
|
||||
|
||||
"!" return simple(TOKEN_TEST_NOT);
|
||||
"not" return simple(TOKEN_TEST_NOT);
|
||||
"&&" return simple(TOKEN_TEST_AND);
|
||||
"and" return simple(TOKEN_TEST_AND);
|
||||
"||" return simple(TOKEN_TEST_OR);
|
||||
"or" return simple(TOKEN_TEST_OR);
|
||||
|
||||
|
||||
"[" {
|
||||
BEGIN(RANGE);
|
||||
return simple(TOKEN_LBRACKET);
|
||||
}
|
||||
|
||||
<RANGE>[+-]?[[:digit:]]+ {
|
||||
return set_lval(TOKEN_STRING, g_strdup(yytext));
|
||||
}
|
||||
<RANGE>[+-]?0x[[:xdigit:]]+ {
|
||||
return set_lval(TOKEN_STRING, g_strdup(yytext));
|
||||
}
|
||||
<RANGE>":" return simple(TOKEN_COLON);
|
||||
|
||||
<RANGE>"]" {
|
||||
BEGIN(INITIAL);
|
||||
return simple(TOKEN_RBRACKET);
|
||||
}
|
||||
|
||||
|
||||
\"[^"]*\" {
|
||||
return set_lval(TOKEN_STRING, g_substrdup(yytext, 1, -2));
|
||||
}
|
||||
|
||||
|
||||
|
||||
[[:alnum:]_.:]+ {
|
||||
/* Is it a field name? */
|
||||
header_field_info *hfinfo;
|
||||
|
||||
hfinfo = dfilter_lookup_token(yytext);
|
||||
if (hfinfo) {
|
||||
/* Yes, it's a field name */
|
||||
return set_lval(TOKEN_FIELD, hfinfo);
|
||||
}
|
||||
else {
|
||||
/* No, so treat it as a string */
|
||||
return set_lval(TOKEN_STRING, g_strdup(yytext));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
%%
|
||||
|
||||
int
|
||||
simple(int token)
|
||||
{
|
||||
switch (token) {
|
||||
case TOKEN_LPAREN:
|
||||
case TOKEN_RPAREN:
|
||||
case TOKEN_LBRACKET:
|
||||
case TOKEN_RBRACKET:
|
||||
case TOKEN_COLON:
|
||||
case TOKEN_TEST_EQ:
|
||||
case TOKEN_TEST_NE:
|
||||
case TOKEN_TEST_GT:
|
||||
case TOKEN_TEST_GE:
|
||||
case TOKEN_TEST_LT:
|
||||
case TOKEN_TEST_LE:
|
||||
case TOKEN_TEST_NOT:
|
||||
case TOKEN_TEST_AND:
|
||||
case TOKEN_TEST_OR:
|
||||
break;
|
||||
default:
|
||||
g_assert_not_reached();
|
||||
}
|
||||
return token;
|
||||
}
|
||||
|
||||
int
|
||||
set_lval(int token, gpointer data)
|
||||
{
|
||||
sttype_id_t type_id = STTYPE_UNINITIALIZED;
|
||||
|
||||
switch (token) {
|
||||
case TOKEN_STRING:
|
||||
type_id = STTYPE_STRING;
|
||||
break;
|
||||
case TOKEN_FIELD:
|
||||
type_id = STTYPE_FIELD;
|
||||
break;
|
||||
default:
|
||||
g_assert_not_reached();
|
||||
}
|
||||
|
||||
stnode_init(df_lval, type_id, data);
|
||||
return token;
|
||||
}
|
||||
|
||||
#include <lemonflex-tail.inc>
|
|
@ -0,0 +1,472 @@
|
|||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "dfilter-int.h"
|
||||
#include "syntax-tree.h"
|
||||
#include "sttype-range.h"
|
||||
#include "sttype-test.h"
|
||||
|
||||
#include "exceptions.h"
|
||||
|
||||
static void
|
||||
semcheck(dfwork_t *dfw, stnode_t *st_node);
|
||||
|
||||
typedef gboolean (*FtypeCanFunc)(enum ftenum);
|
||||
|
||||
/* Compares to ftenum_t's and decides if they're
|
||||
* compatible or not (if they're the same basic type) */
|
||||
static gboolean
|
||||
compatible_ftypes(ftenum_t a, ftenum_t b)
|
||||
{
|
||||
switch (a) {
|
||||
case FT_NONE:
|
||||
case FT_PROTOCOL:
|
||||
case FT_DOUBLE:
|
||||
case FT_ABSOLUTE_TIME:
|
||||
case FT_RELATIVE_TIME:
|
||||
case FT_IPv4:
|
||||
case FT_IPv6:
|
||||
case FT_IPXNET:
|
||||
return a == b;
|
||||
|
||||
case FT_ETHER:
|
||||
case FT_BYTES:
|
||||
return (b == FT_ETHER || b == FT_BYTES);
|
||||
|
||||
case FT_BOOLEAN:
|
||||
case FT_UINT8:
|
||||
case FT_UINT16:
|
||||
case FT_UINT24:
|
||||
case FT_UINT32:
|
||||
case FT_INT8:
|
||||
case FT_INT16:
|
||||
case FT_INT24:
|
||||
case FT_INT32:
|
||||
switch (b) {
|
||||
case FT_BOOLEAN:
|
||||
case FT_UINT8:
|
||||
case FT_UINT16:
|
||||
case FT_UINT24:
|
||||
case FT_UINT32:
|
||||
case FT_INT8:
|
||||
case FT_INT16:
|
||||
case FT_INT24:
|
||||
case FT_INT32:
|
||||
return TRUE;
|
||||
default:
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
case FT_STRING:
|
||||
case FT_STRINGZ:
|
||||
case FT_UINT_STRING:
|
||||
switch (b) {
|
||||
case FT_STRING:
|
||||
case FT_STRINGZ:
|
||||
case FT_UINT_STRING:
|
||||
return TRUE;
|
||||
default:
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
case FT_NUM_TYPES:
|
||||
g_assert_not_reached();
|
||||
}
|
||||
|
||||
g_assert_not_reached();
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/* Creates a FT_UINT32 fvalue with a given value. */
|
||||
static fvalue_t*
|
||||
mk_uint32_fvalue(guint32 val)
|
||||
{
|
||||
fvalue_t *fv;
|
||||
|
||||
fv = fvalue_new(FT_UINT32);
|
||||
fvalue_set_integer(fv, val);
|
||||
|
||||
return fv;
|
||||
}
|
||||
|
||||
|
||||
/* Try to make an fvalue from a string using a value_string or true_false_string.
|
||||
* This works only for ftypes that are integers. Returns the created fvalue_t*
|
||||
* or NULL if impossible. */
|
||||
static fvalue_t*
|
||||
mk_fvalue_from_val_string(header_field_info *hfinfo, char *s)
|
||||
{
|
||||
static true_false_string default_tf = { "True", "False" };
|
||||
true_false_string *tf = &default_tf;
|
||||
value_string *vals;
|
||||
|
||||
/* Early return? */
|
||||
switch(hfinfo->type) {
|
||||
case FT_NONE:
|
||||
case FT_PROTOCOL:
|
||||
case FT_DOUBLE:
|
||||
case FT_ABSOLUTE_TIME:
|
||||
case FT_RELATIVE_TIME:
|
||||
case FT_IPv4:
|
||||
case FT_IPv6:
|
||||
case FT_IPXNET:
|
||||
case FT_ETHER:
|
||||
case FT_BYTES:
|
||||
case FT_STRING:
|
||||
case FT_STRINGZ:
|
||||
case FT_UINT_STRING:
|
||||
return FALSE;
|
||||
|
||||
case FT_BOOLEAN:
|
||||
case FT_UINT8:
|
||||
case FT_UINT16:
|
||||
case FT_UINT24:
|
||||
case FT_UINT32:
|
||||
case FT_INT8:
|
||||
case FT_INT16:
|
||||
case FT_INT24:
|
||||
case FT_INT32:
|
||||
break;
|
||||
|
||||
case FT_NUM_TYPES:
|
||||
g_assert_not_reached();
|
||||
}
|
||||
|
||||
/* Reset the dfilter error message, since *something* interesting
|
||||
* will happen, and the error message will be more interesting than
|
||||
* any error message I happen to have now. */
|
||||
dfilter_error_msg = NULL;
|
||||
|
||||
/* TRUE/FALSE *always* exist for FT_BOOLEAN. */
|
||||
if (hfinfo->type == FT_BOOLEAN) {
|
||||
if (hfinfo->strings) {
|
||||
tf = hfinfo->strings;
|
||||
}
|
||||
|
||||
if (strcasecmp(s, tf->true_string) == 0) {
|
||||
return mk_uint32_fvalue(TRUE);
|
||||
}
|
||||
else if (strcasecmp(s, tf->false_string) == 0) {
|
||||
return mk_uint32_fvalue(FALSE);
|
||||
}
|
||||
else {
|
||||
dfilter_fail("\"%s\" cannot be found among the possible values for %s.",
|
||||
s, hfinfo->abbrev);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/* Do val_strings exist? */
|
||||
if (!hfinfo->strings) {
|
||||
dfilter_fail("%s cannot accept strings as values.",
|
||||
hfinfo->abbrev);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
vals = hfinfo->strings;
|
||||
while (vals->strptr != NULL) {
|
||||
if (strcasecmp(s, vals->strptr) == 0) {
|
||||
return mk_uint32_fvalue(vals->value);
|
||||
}
|
||||
vals++;
|
||||
}
|
||||
dfilter_fail("\"%s\" cannot be found among the possible values for %s.",
|
||||
s, hfinfo->abbrev);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
|
||||
static gboolean
|
||||
is_bytes_type(enum ftenum type)
|
||||
{
|
||||
switch(type) {
|
||||
case FT_ETHER:
|
||||
case FT_BYTES:
|
||||
case FT_IPv6:
|
||||
return TRUE;
|
||||
|
||||
case FT_NONE:
|
||||
case FT_PROTOCOL:
|
||||
case FT_DOUBLE:
|
||||
case FT_ABSOLUTE_TIME:
|
||||
case FT_RELATIVE_TIME:
|
||||
case FT_IPv4:
|
||||
case FT_IPXNET:
|
||||
case FT_STRING:
|
||||
case FT_STRINGZ:
|
||||
case FT_UINT_STRING:
|
||||
case FT_BOOLEAN:
|
||||
case FT_UINT8:
|
||||
case FT_UINT16:
|
||||
case FT_UINT24:
|
||||
case FT_UINT32:
|
||||
case FT_INT8:
|
||||
case FT_INT16:
|
||||
case FT_INT24:
|
||||
case FT_INT32:
|
||||
return FALSE;
|
||||
|
||||
case FT_NUM_TYPES:
|
||||
g_assert_not_reached();
|
||||
}
|
||||
|
||||
g_assert_not_reached();
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/* This could really be split up... it's too big. */
|
||||
static void
|
||||
check_relation(dfwork_t *dfw, FtypeCanFunc can_func, stnode_t *st_node,
|
||||
stnode_t *st_arg1, stnode_t *st_arg2)
|
||||
{
|
||||
stnode_t *new_st;
|
||||
sttype_id_t type1, type2;
|
||||
header_field_info *hfinfo1, *hfinfo2;
|
||||
ftenum_t ftype1, ftype2;
|
||||
fvalue_t *fvalue;
|
||||
char *s;
|
||||
|
||||
type1 = stnode_type_id(st_arg1);
|
||||
type2 = stnode_type_id(st_arg2);
|
||||
if (type1 == STTYPE_FIELD) {
|
||||
hfinfo1 = stnode_data(st_arg1);
|
||||
ftype1 = hfinfo1->type;
|
||||
|
||||
if (!can_func(ftype1)) {
|
||||
dfilter_fail("%s (type=%s) cannot participate in specified comparison.",
|
||||
hfinfo1->abbrev, ftype_pretty_name(ftype1));
|
||||
THROW(TypeError);
|
||||
}
|
||||
|
||||
|
||||
if (type2 == STTYPE_FIELD) {
|
||||
hfinfo2 = stnode_data(st_arg2);
|
||||
ftype2 = hfinfo2->type;
|
||||
|
||||
if (!compatible_ftypes(ftype1, ftype2)) {
|
||||
dfilter_fail("%s and %s are not of compatible types.",
|
||||
hfinfo1->abbrev, hfinfo2->abbrev);
|
||||
THROW(TypeError);
|
||||
}
|
||||
/* Do this check even though you'd think that if
|
||||
* they're compatible, then can_func() would pass. */
|
||||
if (!can_func(ftype2)) {
|
||||
dfilter_fail("%s (type=%s) cannot participate in specified comparison.",
|
||||
hfinfo2->abbrev, ftype_pretty_name(ftype2));
|
||||
THROW(TypeError);
|
||||
}
|
||||
}
|
||||
else if (type2 == STTYPE_STRING) {
|
||||
s = stnode_data(st_arg2);
|
||||
fvalue = fvalue_from_string(ftype1, s, dfilter_fail);
|
||||
if (!fvalue) {
|
||||
/* check value_string */
|
||||
fvalue = mk_fvalue_from_val_string(hfinfo1, s);
|
||||
if (!fvalue) {
|
||||
THROW(TypeError);
|
||||
}
|
||||
}
|
||||
|
||||
new_st = stnode_new(STTYPE_FVALUE, fvalue);
|
||||
sttype_test_set2_args(st_node, st_arg1, new_st);
|
||||
stnode_free(st_arg2);
|
||||
}
|
||||
else if (type2 == STTYPE_RANGE) {
|
||||
if (!is_bytes_type(ftype1)) {
|
||||
if (!ftype_can_slice(ftype1)) {
|
||||
dfilter_fail("\"%s\" is a %s and cannot be converted into a sequence of bytes.",
|
||||
hfinfo1->abbrev,
|
||||
ftype_pretty_name(ftype1));
|
||||
THROW(TypeError);
|
||||
}
|
||||
|
||||
/* Convert entire field to bytes */
|
||||
new_st = stnode_new(STTYPE_RANGE, NULL);
|
||||
|
||||
/* st_arg1 is freed in this step */
|
||||
sttype_range_set(new_st, st_arg1, NULL, NULL);
|
||||
|
||||
sttype_test_set2_args(st_node, new_st, st_arg2);
|
||||
}
|
||||
}
|
||||
else {
|
||||
g_assert_not_reached();
|
||||
}
|
||||
}
|
||||
else if (type1 == STTYPE_STRING) {
|
||||
|
||||
if (type2 == STTYPE_FIELD) {
|
||||
hfinfo2 = stnode_data(st_arg2);
|
||||
ftype2 = hfinfo2->type;
|
||||
|
||||
s = stnode_data(st_arg1);
|
||||
fvalue = fvalue_from_string(ftype2, s, dfilter_fail);
|
||||
if (!fvalue) {
|
||||
/* check value_string */
|
||||
fvalue = mk_fvalue_from_val_string(hfinfo2, s);
|
||||
if (!fvalue) {
|
||||
THROW(TypeError);
|
||||
}
|
||||
}
|
||||
|
||||
new_st = stnode_new(STTYPE_FVALUE, fvalue);
|
||||
sttype_test_set2_args(st_node, new_st, st_arg2);
|
||||
stnode_free(st_arg1);
|
||||
}
|
||||
else if (type2 == STTYPE_STRING) {
|
||||
/* Well now that's silly... */
|
||||
dfilter_fail("Neither \"%s\" nor \"%s\" are field or protocol names.",
|
||||
stnode_data(st_arg1),
|
||||
stnode_data(st_arg2));
|
||||
THROW(TypeError);
|
||||
}
|
||||
else if (type2 == STTYPE_RANGE) {
|
||||
s = stnode_data(st_arg1);
|
||||
fvalue = fvalue_from_string(FT_BYTES, s, dfilter_fail);
|
||||
if (!fvalue) {
|
||||
THROW(TypeError);
|
||||
}
|
||||
new_st = stnode_new(STTYPE_FVALUE, fvalue);
|
||||
sttype_test_set2_args(st_node, new_st, st_arg2);
|
||||
stnode_free(st_arg1);
|
||||
}
|
||||
else {
|
||||
g_assert_not_reached();
|
||||
}
|
||||
}
|
||||
else if (type1 == STTYPE_RANGE) {
|
||||
hfinfo1 = sttype_range_hfinfo(st_arg1);
|
||||
ftype1 = hfinfo1->type;
|
||||
|
||||
if (!ftype_can_slice(ftype1)) {
|
||||
dfilter_fail("\"%s\" is a %s and cannot be sliced into a sequence of bytes.",
|
||||
hfinfo1->abbrev, ftype_pretty_name(ftype1));
|
||||
THROW(TypeError);
|
||||
}
|
||||
|
||||
|
||||
if (type2 == STTYPE_FIELD) {
|
||||
hfinfo2 = sttype_range_hfinfo(st_arg2);
|
||||
ftype2 = hfinfo2->type;
|
||||
|
||||
if (!is_bytes_type(ftype2)) {
|
||||
if (!ftype_can_slice(ftype2)) {
|
||||
dfilter_fail("\"%s\" is a %s and cannot be converted into a sequence of bytes.",
|
||||
hfinfo2->abbrev,
|
||||
ftype_pretty_name(ftype2));
|
||||
THROW(TypeError);
|
||||
}
|
||||
|
||||
/* Convert entire field to bytes */
|
||||
new_st = stnode_new(STTYPE_RANGE, NULL);
|
||||
|
||||
/* st_arg2 is freed in this step */
|
||||
sttype_range_set(new_st, st_arg2, NULL, NULL);
|
||||
|
||||
sttype_test_set2_args(st_node, st_arg1, new_st);
|
||||
}
|
||||
}
|
||||
else if (type2 == STTYPE_STRING) {
|
||||
s = stnode_data(st_arg2);
|
||||
fvalue = fvalue_from_string(FT_BYTES, s, dfilter_fail);
|
||||
if (!fvalue) {
|
||||
THROW(TypeError);
|
||||
}
|
||||
new_st = stnode_new(STTYPE_FVALUE, fvalue);
|
||||
sttype_test_set2_args(st_node, st_arg1, new_st);
|
||||
stnode_free(st_arg2);
|
||||
}
|
||||
else if (type2 == STTYPE_RANGE) {
|
||||
/* XXX - check lengths of both ranges */
|
||||
}
|
||||
else {
|
||||
g_assert_not_reached();
|
||||
}
|
||||
}
|
||||
else {
|
||||
g_assert_not_reached();
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
check_test(dfwork_t *dfw, stnode_t *st_node)
|
||||
{
|
||||
test_op_t st_op;
|
||||
stnode_t *st_arg1, *st_arg2;
|
||||
|
||||
sttype_test_get(st_node, &st_op, &st_arg1, &st_arg2);
|
||||
|
||||
switch (st_op) {
|
||||
case TEST_OP_UNINITIALIZED:
|
||||
g_assert_not_reached();
|
||||
break;
|
||||
|
||||
case TEST_OP_EXISTS:
|
||||
/* nothing */
|
||||
break;
|
||||
|
||||
case TEST_OP_NOT:
|
||||
semcheck(dfw, st_arg1);
|
||||
break;
|
||||
|
||||
case TEST_OP_AND:
|
||||
case TEST_OP_OR:
|
||||
semcheck(dfw, st_arg1);
|
||||
semcheck(dfw, st_arg2);
|
||||
break;
|
||||
|
||||
case TEST_OP_EQ:
|
||||
check_relation(dfw, ftype_can_eq, st_node, st_arg1, st_arg2);
|
||||
break;
|
||||
case TEST_OP_NE:
|
||||
check_relation(dfw, ftype_can_ne, st_node, st_arg1, st_arg2);
|
||||
break;
|
||||
case TEST_OP_GT:
|
||||
check_relation(dfw, ftype_can_gt, st_node, st_arg1, st_arg2);
|
||||
break;
|
||||
case TEST_OP_GE:
|
||||
check_relation(dfw, ftype_can_ge, st_node, st_arg1, st_arg2);
|
||||
break;
|
||||
case TEST_OP_LT:
|
||||
check_relation(dfw, ftype_can_lt, st_node, st_arg1, st_arg2);
|
||||
break;
|
||||
case TEST_OP_LE:
|
||||
check_relation(dfw, ftype_can_le, st_node, st_arg1, st_arg2);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
semcheck(dfwork_t *dfw, stnode_t *st_node)
|
||||
{
|
||||
const char *name;
|
||||
|
||||
name = stnode_type_name(st_node);
|
||||
|
||||
switch (stnode_type_id(st_node)) {
|
||||
case STTYPE_TEST:
|
||||
check_test(dfw, st_node);
|
||||
break;
|
||||
default:
|
||||
g_assert_not_reached();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
gboolean
|
||||
dfw_semcheck(dfwork_t *dfw)
|
||||
{
|
||||
TRY {
|
||||
semcheck(dfw, dfw->st_root);
|
||||
}
|
||||
CATCH(TypeError) {
|
||||
return FALSE;
|
||||
}
|
||||
ENDTRY;
|
||||
|
||||
return TRUE;
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
#ifndef SEMCHECK_H
|
||||
#define SEMCHECK_H
|
||||
|
||||
|
||||
|
||||
gboolean
|
||||
dfw_semcheck(dfwork_t *dfw);
|
||||
|
||||
|
||||
#endif
|
|
@ -0,0 +1,28 @@
|
|||
/* $Id: sttype-pointer.c,v 1.1 2001/02/01 20:21:18 gram Exp $ */
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
#include "ftypes/ftypes.h"
|
||||
#include "syntax-tree.h"
|
||||
|
||||
void
|
||||
sttype_register_pointer(void)
|
||||
{
|
||||
static sttype_t field_type = {
|
||||
STTYPE_FIELD,
|
||||
"FIELD",
|
||||
NULL,
|
||||
NULL,
|
||||
};
|
||||
static sttype_t fvalue_type = {
|
||||
STTYPE_FVALUE,
|
||||
"FVALUE",
|
||||
NULL,
|
||||
NULL,
|
||||
};
|
||||
|
||||
sttype_register(&field_type);
|
||||
sttype_register(&fvalue_type);
|
||||
}
|
|
@ -0,0 +1,174 @@
|
|||
/* $Id: sttype-range.c,v 1.1 2001/02/01 20:21:18 gram Exp $ */
|
||||
|
||||
/* The ideas in this code came from Ed Warnicke's original implementation
|
||||
* of dranges for the old display filter code (Ethereal 0.8.15 and before).
|
||||
* The code is different, but definitely inspired by his code.
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include "proto.h"
|
||||
#include "sttype-range.h"
|
||||
|
||||
typedef struct {
|
||||
guint32 magic;
|
||||
header_field_info *hfinfo;
|
||||
gint start;
|
||||
gint end;
|
||||
char *start_error;
|
||||
char *end_error;
|
||||
} range_t;
|
||||
|
||||
#define RANGE_MAGIC 0xec0990ce
|
||||
|
||||
static gpointer
|
||||
range_new(gpointer junk)
|
||||
{
|
||||
range_t *range;
|
||||
|
||||
g_assert(junk == NULL);
|
||||
|
||||
range = g_new(range_t, 1);
|
||||
|
||||
range->magic = RANGE_MAGIC;
|
||||
range->hfinfo = NULL;
|
||||
range->start = 0;
|
||||
range->end = -1;
|
||||
range->start_error = NULL;
|
||||
range->end_error = NULL;
|
||||
|
||||
return (gpointer) range;
|
||||
}
|
||||
|
||||
static void
|
||||
range_free(gpointer value)
|
||||
{
|
||||
range_t *range = value;
|
||||
assert_magic(range, RANGE_MAGIC);
|
||||
|
||||
if (range->start_error)
|
||||
g_free(range->start_error);
|
||||
if (range->end_error)
|
||||
g_free(range->end_error);
|
||||
|
||||
g_free(range);
|
||||
}
|
||||
|
||||
static gint
|
||||
string_to_gint(char *s, gboolean *success)
|
||||
{
|
||||
char *endptr;
|
||||
gint val;
|
||||
|
||||
*success = TRUE;
|
||||
val = strtol(s, &endptr, 0);
|
||||
|
||||
if (endptr == s || *endptr != '\0') {
|
||||
*success = FALSE;
|
||||
}
|
||||
else if (errno == ERANGE) {
|
||||
*success = FALSE;
|
||||
}
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
static void
|
||||
range_set(stnode_t *node, stnode_t *field, char *start, char *end)
|
||||
{
|
||||
range_t *range;
|
||||
gboolean success;
|
||||
|
||||
range = stnode_data(node);
|
||||
assert_magic(range, RANGE_MAGIC);
|
||||
|
||||
range->hfinfo = stnode_data(field);
|
||||
stnode_free(field);
|
||||
|
||||
if (start) {
|
||||
range->start = string_to_gint(start, &success);
|
||||
if (!success) {
|
||||
/* Save the error-causing string for later reporting */
|
||||
range->start_error = g_strdup(start);
|
||||
}
|
||||
}
|
||||
else {
|
||||
range->start = 0;
|
||||
}
|
||||
|
||||
if (end) {
|
||||
range->end = string_to_gint(end, &success);
|
||||
|
||||
if (!success) {
|
||||
/* Save the error-causing string for later reporting */
|
||||
range->end_error = g_strdup(end);
|
||||
}
|
||||
}
|
||||
else {
|
||||
range->end = G_MAXINT;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
sttype_range_set(stnode_t *node, stnode_t *field, stnode_t *start, stnode_t *end)
|
||||
{
|
||||
char *start_str, *end_str;
|
||||
|
||||
if (start) {
|
||||
start_str = stnode_data(start);
|
||||
}
|
||||
else {
|
||||
start_str = NULL;
|
||||
}
|
||||
|
||||
if (end) {
|
||||
end_str = stnode_data(end);
|
||||
}
|
||||
else {
|
||||
end_str = NULL;
|
||||
}
|
||||
|
||||
range_set(node, field, start_str, end_str);
|
||||
|
||||
if (start)
|
||||
stnode_free(start);
|
||||
if (end)
|
||||
stnode_free(end);
|
||||
}
|
||||
|
||||
void
|
||||
sttype_range_set1(stnode_t *node, stnode_t *field, stnode_t *offset)
|
||||
{
|
||||
char *offset_str;
|
||||
|
||||
g_assert(offset);
|
||||
|
||||
offset_str = stnode_data(offset);
|
||||
range_set(node, field, offset_str, "1");
|
||||
stnode_free(offset);
|
||||
}
|
||||
|
||||
|
||||
STTYPE_ACCESSOR(header_field_info*, range, hfinfo, RANGE_MAGIC)
|
||||
STTYPE_ACCESSOR(gint, range, start, RANGE_MAGIC)
|
||||
STTYPE_ACCESSOR(gint, range, end, RANGE_MAGIC)
|
||||
STTYPE_ACCESSOR(char*, range, start_error, RANGE_MAGIC)
|
||||
STTYPE_ACCESSOR(char*, range, end_error, RANGE_MAGIC)
|
||||
|
||||
|
||||
void
|
||||
sttype_register_range(void)
|
||||
{
|
||||
static sttype_t range_type = {
|
||||
STTYPE_RANGE,
|
||||
"RANGE",
|
||||
range_new,
|
||||
range_free,
|
||||
};
|
||||
|
||||
sttype_register(&range_type);
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
#ifndef STTYPE_RANGE_H
|
||||
#define STTYPE_RANGE_H
|
||||
|
||||
#include "syntax-tree.h"
|
||||
|
||||
STTYPE_ACCESSOR_PROTOTYPE(header_field_info*, range, hfinfo)
|
||||
STTYPE_ACCESSOR_PROTOTYPE(gint, range, start)
|
||||
STTYPE_ACCESSOR_PROTOTYPE(gint, range, end)
|
||||
STTYPE_ACCESSOR_PROTOTYPE(char*, range, start_error)
|
||||
STTYPE_ACCESSOR_PROTOTYPE(char*, range, end_error)
|
||||
|
||||
/* Set a range, [x:y], [:y], [x:] */
|
||||
void
|
||||
sttype_range_set(stnode_t *node, stnode_t *field, stnode_t *start, stnode_t *end);
|
||||
|
||||
/* Set a single-byte lookup, [x] */
|
||||
void
|
||||
sttype_range_set1(stnode_t *node, stnode_t *field, stnode_t *offset);
|
||||
|
||||
#endif
|
|
@ -0,0 +1,29 @@
|
|||
/* $Id: sttype-string.c,v 1.1 2001/02/01 20:21:18 gram Exp $ */
|
||||
|
||||
#include "syntax-tree.h"
|
||||
|
||||
static gpointer
|
||||
string_new(gpointer string)
|
||||
{
|
||||
return (gpointer) g_strdup((char*) string);
|
||||
}
|
||||
|
||||
static void
|
||||
string_free(gpointer value)
|
||||
{
|
||||
g_free(value);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
sttype_register_string(void)
|
||||
{
|
||||
static sttype_t string_type = {
|
||||
STTYPE_STRING,
|
||||
"STRING",
|
||||
string_new,
|
||||
string_free,
|
||||
};
|
||||
|
||||
sttype_register(&string_type);
|
||||
}
|
|
@ -0,0 +1,144 @@
|
|||
/* $Id: sttype-test.c,v 1.1 2001/02/01 20:21:18 gram Exp $ */
|
||||
|
||||
#include "syntax-tree.h"
|
||||
#include "sttype-test.h"
|
||||
|
||||
typedef struct {
|
||||
guint32 magic;
|
||||
test_op_t op;
|
||||
stnode_t *val1;
|
||||
stnode_t *val2;
|
||||
} test_t;
|
||||
|
||||
#define TEST_MAGIC 0xab9009ba
|
||||
|
||||
static gpointer
|
||||
test_new(gpointer junk)
|
||||
{
|
||||
test_t *test;
|
||||
|
||||
g_assert(junk == NULL);
|
||||
|
||||
test = g_new(test_t, 1);
|
||||
|
||||
test->magic = TEST_MAGIC;
|
||||
test->op = TEST_OP_UNINITIALIZED;
|
||||
test->val1 = NULL;
|
||||
test->val2 = NULL;
|
||||
|
||||
return (gpointer) test;
|
||||
}
|
||||
|
||||
static void
|
||||
test_free(gpointer value)
|
||||
{
|
||||
test_t *test = value;
|
||||
assert_magic(test, TEST_MAGIC);
|
||||
|
||||
if (test->val1)
|
||||
stnode_free(test->val1);
|
||||
if (test->val2)
|
||||
stnode_free(test->val2);
|
||||
|
||||
g_free(test);
|
||||
}
|
||||
|
||||
static int
|
||||
num_operands(test_op_t op)
|
||||
{
|
||||
switch(op) {
|
||||
case TEST_OP_UNINITIALIZED:
|
||||
g_assert_not_reached();
|
||||
case TEST_OP_EXISTS:
|
||||
return 1;
|
||||
case TEST_OP_NOT:
|
||||
return 1;
|
||||
case TEST_OP_AND:
|
||||
return 2;
|
||||
case TEST_OP_OR:
|
||||
return 2;
|
||||
case TEST_OP_EQ:
|
||||
return 2;
|
||||
case TEST_OP_NE:
|
||||
return 2;
|
||||
case TEST_OP_GT:
|
||||
return 2;
|
||||
case TEST_OP_GE:
|
||||
return 2;
|
||||
case TEST_OP_LT:
|
||||
return 2;
|
||||
case TEST_OP_LE:
|
||||
return 2;
|
||||
}
|
||||
g_assert_not_reached();
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
sttype_test_set1(stnode_t *node, test_op_t op, stnode_t *val1)
|
||||
{
|
||||
test_t *test;
|
||||
|
||||
test = stnode_data(node);
|
||||
assert_magic(test, TEST_MAGIC);
|
||||
|
||||
g_assert(num_operands(op) == 1);
|
||||
test->op = op;
|
||||
test->val1 = val1;
|
||||
}
|
||||
|
||||
void
|
||||
sttype_test_set2(stnode_t *node, test_op_t op, stnode_t *val1, stnode_t *val2)
|
||||
{
|
||||
test_t *test;
|
||||
|
||||
test = stnode_data(node);
|
||||
assert_magic(test, TEST_MAGIC);
|
||||
|
||||
g_assert(num_operands(op) == 2);
|
||||
test->op = op;
|
||||
test->val1 = val1;
|
||||
test->val2 = val2;
|
||||
}
|
||||
|
||||
void
|
||||
sttype_test_set2_args(stnode_t *node, stnode_t *val1, stnode_t *val2)
|
||||
{
|
||||
test_t *test;
|
||||
|
||||
test = stnode_data(node);
|
||||
assert_magic(test, TEST_MAGIC);
|
||||
|
||||
if (num_operands(test->op) == 1) {
|
||||
g_assert(val2 == NULL);
|
||||
}
|
||||
test->val1 = val1;
|
||||
test->val2 = val2;
|
||||
}
|
||||
|
||||
void
|
||||
sttype_test_get(stnode_t *node, test_op_t *p_op, stnode_t **p_val1, stnode_t **p_val2)
|
||||
{
|
||||
test_t *test;
|
||||
|
||||
test = stnode_data(node);
|
||||
assert_magic(test, TEST_MAGIC);
|
||||
|
||||
*p_op = test->op;
|
||||
*p_val1 = test->val1;
|
||||
*p_val2 = test->val2;
|
||||
}
|
||||
|
||||
void
|
||||
sttype_register_test(void)
|
||||
{
|
||||
static sttype_t test_type = {
|
||||
STTYPE_TEST,
|
||||
"TEST",
|
||||
test_new,
|
||||
test_free,
|
||||
};
|
||||
|
||||
sttype_register(&test_type);
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
#ifndef STTYPE_TEST_H
|
||||
#define STTYPE_TEST_H
|
||||
|
||||
typedef enum {
|
||||
TEST_OP_UNINITIALIZED,
|
||||
TEST_OP_EXISTS,
|
||||
TEST_OP_NOT,
|
||||
TEST_OP_AND,
|
||||
TEST_OP_OR,
|
||||
TEST_OP_EQ,
|
||||
TEST_OP_NE,
|
||||
TEST_OP_GT,
|
||||
TEST_OP_GE,
|
||||
TEST_OP_LT,
|
||||
TEST_OP_LE
|
||||
} test_op_t;
|
||||
|
||||
void
|
||||
sttype_test_set1(stnode_t *node, test_op_t op, stnode_t *val1);
|
||||
|
||||
void
|
||||
sttype_test_set2(stnode_t *node, test_op_t op, stnode_t *val1, stnode_t *val2);
|
||||
|
||||
void
|
||||
sttype_test_set2_args(stnode_t *node, stnode_t *val1, stnode_t *val2);
|
||||
|
||||
void
|
||||
sttype_test_get(stnode_t *node, test_op_t *p_op, stnode_t **p_val1, stnode_t **p_val2);
|
||||
|
||||
#endif
|
|
@ -0,0 +1,169 @@
|
|||
/* syntax-tree.c
|
||||
*
|
||||
* $Id: syntax-tree.c,v 1.1 2001/02/01 20:21:18 gram Exp $
|
||||
*
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
#include "syntax-tree.h"
|
||||
|
||||
/* Keep track of sttype_t's via their sttype_id_t number */
|
||||
static sttype_t* type_list[STTYPE_NUM_TYPES];
|
||||
|
||||
/* These are the sttype_t registration function prototypes. */
|
||||
void sttype_register_pointer(void);
|
||||
void sttype_register_range(void);
|
||||
void sttype_register_string(void);
|
||||
void sttype_register_test(void);
|
||||
|
||||
|
||||
#define STNODE_MAGIC 0xe9b00b9e
|
||||
|
||||
|
||||
void
|
||||
sttype_init(void)
|
||||
{
|
||||
sttype_register_pointer();
|
||||
sttype_register_range();
|
||||
sttype_register_string();
|
||||
sttype_register_test();
|
||||
}
|
||||
|
||||
void
|
||||
sttype_cleanup(void)
|
||||
{
|
||||
/* nothing to do */
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
sttype_register(sttype_t *type)
|
||||
{
|
||||
sttype_id_t type_id;
|
||||
|
||||
type_id = type->id;
|
||||
|
||||
/* Check input */
|
||||
g_assert(type_id >= 0);
|
||||
g_assert(type_id < STTYPE_NUM_TYPES);
|
||||
|
||||
/* Don't re-register. */
|
||||
g_assert(type_list[type_id] == NULL);
|
||||
|
||||
type_list[type_id] = type;
|
||||
}
|
||||
|
||||
static sttype_t*
|
||||
sttype_lookup(sttype_id_t type_id)
|
||||
{
|
||||
sttype_t *result;
|
||||
|
||||
/* Check input */
|
||||
g_assert(type_id >= 0);
|
||||
g_assert(type_id < STTYPE_NUM_TYPES);
|
||||
|
||||
result = type_list[type_id];
|
||||
|
||||
/* Check output. */
|
||||
g_assert(result != NULL);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
stnode_t*
|
||||
stnode_new(sttype_id_t type_id, gpointer data)
|
||||
{
|
||||
sttype_t *type;
|
||||
stnode_t *node;
|
||||
|
||||
node = g_new(stnode_t, 1);
|
||||
node->magic = STNODE_MAGIC;
|
||||
|
||||
if (type_id == STTYPE_UNINITIALIZED) {
|
||||
node->type = NULL;
|
||||
node->data = NULL;
|
||||
}
|
||||
else {
|
||||
type = sttype_lookup(type_id);
|
||||
g_assert(type);
|
||||
node->type = type;
|
||||
if (type->func_new) {
|
||||
node->data = type->func_new(data);
|
||||
}
|
||||
else {
|
||||
node->data = data;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
void
|
||||
stnode_init(stnode_t *node, sttype_id_t type_id, gpointer data)
|
||||
{
|
||||
sttype_t *type;
|
||||
|
||||
assert_magic(node, STNODE_MAGIC);
|
||||
g_assert(!node->type);
|
||||
g_assert(!node->data);
|
||||
|
||||
type = sttype_lookup(type_id);
|
||||
g_assert(type);
|
||||
node->type = type;
|
||||
if (type->func_new) {
|
||||
node->data = type->func_new(data);
|
||||
}
|
||||
else {
|
||||
node->data = data;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
stnode_free(stnode_t *node)
|
||||
{
|
||||
assert_magic(node, STNODE_MAGIC);
|
||||
if (node->type) {
|
||||
if (node->type->func_free) {
|
||||
node->type->func_free(node->data);
|
||||
}
|
||||
}
|
||||
else {
|
||||
g_assert(!node->data);
|
||||
}
|
||||
g_free(node);
|
||||
}
|
||||
|
||||
const char*
|
||||
stnode_type_name(stnode_t *node)
|
||||
{
|
||||
assert_magic(node, STNODE_MAGIC);
|
||||
if (node->type)
|
||||
return node->type->name;
|
||||
else
|
||||
return "UNINITIALIZED";
|
||||
}
|
||||
|
||||
sttype_id_t
|
||||
stnode_type_id(stnode_t *node)
|
||||
{
|
||||
assert_magic(node, STNODE_MAGIC);
|
||||
if (node->type)
|
||||
return node->type->id;
|
||||
else
|
||||
return STTYPE_UNINITIALIZED;
|
||||
}
|
||||
|
||||
gpointer
|
||||
stnode_data(stnode_t *node)
|
||||
{
|
||||
assert_magic(node, STNODE_MAGIC);
|
||||
if (node)
|
||||
return node->data;
|
||||
else
|
||||
return NULL;
|
||||
}
|
|
@ -0,0 +1,95 @@
|
|||
/* syntax-tree.h
|
||||
*
|
||||
* $Id: syntax-tree.h,v 1.1 2001/02/01 20:21:18 gram Exp $
|
||||
*
|
||||
*/
|
||||
#ifndef SYNTAX_TREE_H
|
||||
#define SYNTAX_TREE_H
|
||||
|
||||
#include <glib.h>
|
||||
#include "cppmagic.h"
|
||||
|
||||
typedef enum {
|
||||
STTYPE_UNINITIALIZED,
|
||||
STTYPE_TEST,
|
||||
STTYPE_STRING,
|
||||
STTYPE_FIELD,
|
||||
STTYPE_FVALUE,
|
||||
STTYPE_RANGE,
|
||||
STTYPE_NUM_TYPES
|
||||
} sttype_id_t;
|
||||
|
||||
typedef gpointer (*STTypeNewFunc)(gpointer);
|
||||
typedef void (*STTypeFreeFunc)(gpointer);
|
||||
|
||||
|
||||
/* Type information */
|
||||
typedef struct {
|
||||
sttype_id_t id;
|
||||
const char *name;
|
||||
STTypeNewFunc func_new;
|
||||
STTypeFreeFunc func_free;
|
||||
} sttype_t;
|
||||
|
||||
/* Node (type instance) information */
|
||||
typedef struct {
|
||||
guint32 magic;
|
||||
sttype_t *type;
|
||||
gpointer data;
|
||||
|
||||
} stnode_t;
|
||||
|
||||
void
|
||||
sttype_init(void);
|
||||
|
||||
void
|
||||
sttype_cleanup(void);
|
||||
|
||||
void
|
||||
sttype_register(sttype_t *type);
|
||||
|
||||
stnode_t*
|
||||
stnode_new(sttype_id_t type_id, gpointer data);
|
||||
|
||||
void
|
||||
stnode_init(stnode_t *node, sttype_id_t type_id, gpointer data);
|
||||
|
||||
void
|
||||
stnode_free(stnode_t *node);
|
||||
|
||||
const char*
|
||||
stnode_type_name(stnode_t *node);
|
||||
|
||||
sttype_id_t
|
||||
stnode_type_id(stnode_t *node);
|
||||
|
||||
gpointer
|
||||
stnode_data(stnode_t *node);
|
||||
|
||||
#define assert_magic(obj, mnum) \
|
||||
g_assert((obj)); \
|
||||
if ((obj)->magic != (mnum)) { \
|
||||
g_print("\nMagic num is 0x%08x, but should be 0x%08x", \
|
||||
(obj)->magic, (mnum)); \
|
||||
g_assert((obj)->magic == (mnum)); \
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
#define STTYPE_ACCESSOR(ret,type,attr,magicnum) \
|
||||
ret \
|
||||
CONCAT(CONCAT(CONCAT(sttype_,type),_),attr) (stnode_t *node) \
|
||||
{\
|
||||
CONCAT(type,_t) *value; \
|
||||
value = stnode_data(node);\
|
||||
assert_magic(value, magicnum); \
|
||||
return value->attr; \
|
||||
}
|
||||
|
||||
#define STTYPE_ACCESSOR_PROTOTYPE(ret,type,attr) \
|
||||
ret \
|
||||
CONCAT(CONCAT(CONCAT(sttype_,type),_),attr) (stnode_t *node);
|
||||
|
||||
|
||||
#endif
|
11
epan/epan.c
11
epan/epan.c
|
@ -1,6 +1,6 @@
|
|||
/* epan.h
|
||||
*
|
||||
* $Id: epan.c,v 1.5 2001/01/26 06:14:50 guy Exp $
|
||||
* $Id: epan.c,v 1.6 2001/02/01 20:21:16 gram Exp $
|
||||
*
|
||||
* Ethereal Protocol Analyzer Library
|
||||
*
|
||||
|
@ -14,7 +14,7 @@
|
|||
#include <epan.h>
|
||||
|
||||
#include "conversation.h"
|
||||
#include "dfilter.h"
|
||||
#include "dfilter/dfilter.h"
|
||||
#include "except.h"
|
||||
#include "packet.h"
|
||||
#include "proto.h"
|
||||
|
@ -69,11 +69,6 @@ epan_conversation_init(void)
|
|||
}
|
||||
|
||||
|
||||
struct epan_dissect {
|
||||
|
||||
tvbuff_t *tvb;
|
||||
proto_tree *tree;
|
||||
};
|
||||
|
||||
epan_dissect_t*
|
||||
epan_dissect_new(void* pseudo_header, const guint8* data, frame_data *fd, proto_tree *tree)
|
||||
|
@ -83,6 +78,8 @@ epan_dissect_new(void* pseudo_header, const guint8* data, frame_data *fd, proto_
|
|||
edt = g_new(epan_dissect_t, 1);
|
||||
|
||||
/* XXX - init tree */
|
||||
edt->tree = tree;
|
||||
|
||||
dissect_packet(&edt->tvb, pseudo_header, data, fd, tree);
|
||||
|
||||
return edt;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/* epan.h
|
||||
*
|
||||
* $Id: epan.h,v 1.4 2000/10/16 23:17:39 guy Exp $
|
||||
* $Id: epan.h,v 1.5 2001/02/01 20:21:16 gram Exp $
|
||||
*
|
||||
* Ethereal Protocol Analyzer Library
|
||||
*
|
||||
|
@ -44,7 +44,10 @@ epan_free(epan_t*);
|
|||
* as the structures that the epan_dissect_t contains might have pointers
|
||||
* to addresses in your byte array.
|
||||
*/
|
||||
typedef struct epan_dissect epan_dissect_t;
|
||||
typedef struct {
|
||||
tvbuff_t *tvb;
|
||||
proto_tree *tree;
|
||||
} epan_dissect_t;
|
||||
|
||||
epan_dissect_t*
|
||||
epan_dissect_new(void* pseudo_header, const guint8* data, frame_data *fd, proto_tree *tree);
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
/* Ethereal's exceptions */
|
||||
#define BoundsError 1 /* Index is out of range */
|
||||
#define ReportedBoundsError 2 /* Index is beyond reported length (not cap_len) */
|
||||
#define TypeError 3 /* During dfilter parsing */
|
||||
|
||||
/* Usage:
|
||||
*
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
.cvsignore
|
||||
.deps
|
||||
Makefile
|
||||
Makefile.in
|
|
@ -0,0 +1,52 @@
|
|||
# Makefile.am
|
||||
# Automake file for the GTK interface routines for Ethereal
|
||||
#
|
||||
# $Id: Makefile.am,v 1.1 2001/02/01 20:21:19 gram Exp $
|
||||
#
|
||||
# Ethereal - Network traffic analyzer
|
||||
# By Gerald Combs <gerald@zing.org>
|
||||
# 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.
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
|
||||
# We produce an archive library. In the future, when libethereal is a
|
||||
# shared library, this will be linked into libethereal. While libethereal
|
||||
# is an archive library, any executable linking against libethereal will
|
||||
# also need to link against libftypes.
|
||||
noinst_LIBRARIES = libftypes.a
|
||||
|
||||
CLEANFILES = \
|
||||
libftypes.a \
|
||||
*~
|
||||
|
||||
INCLUDES = -I$(srcdir)/../..
|
||||
|
||||
libftypes_a_SOURCES = \
|
||||
ftypes.c \
|
||||
ftypes.h \
|
||||
ftypes-int.h \
|
||||
ftype-bytes.c \
|
||||
ftype-double.c \
|
||||
ftype-integer.c \
|
||||
ftype-ipv4.c \
|
||||
ftype-none.c \
|
||||
ftype-string.c \
|
||||
ftype-time.c \
|
||||
ftype-tvbuff.c
|
||||
|
||||
#EXTRA_DIST = \
|
||||
# Makefile.nmake
|
||||
|
|
@ -0,0 +1,386 @@
|
|||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
#include <ftypes-int.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include "resolv.h"
|
||||
|
||||
#define ETHER_LEN 6
|
||||
#define IPv6_LEN 16
|
||||
|
||||
static void
|
||||
ftype_from_tvbuff(field_info *fi, tvbuff_t *tvb, int start, int length,
|
||||
gboolean little_endian)
|
||||
{
|
||||
/* XXX */
|
||||
g_assert_not_reached();
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
bytes_fvalue_new(fvalue_t *fv)
|
||||
{
|
||||
fv->value.bytes = NULL;
|
||||
}
|
||||
|
||||
void
|
||||
bytes_fvalue_free(fvalue_t *fv)
|
||||
{
|
||||
if (fv->value.bytes) {
|
||||
g_byte_array_free(fv->value.bytes, TRUE);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
bytes_fvalue_set(fvalue_t *fv, gpointer value, gboolean already_copied)
|
||||
{
|
||||
g_assert(already_copied);
|
||||
fv->value.bytes = value;
|
||||
}
|
||||
|
||||
static void
|
||||
common_fvalue_set(fvalue_t *fv, guint8* data, guint len)
|
||||
{
|
||||
fv->value.bytes = g_byte_array_new();
|
||||
g_byte_array_append(fv->value.bytes, data, len);
|
||||
}
|
||||
|
||||
static void
|
||||
ether_fvalue_set(fvalue_t *fv, gpointer value, gboolean already_copied)
|
||||
{
|
||||
g_assert(!already_copied);
|
||||
common_fvalue_set(fv, value, ETHER_LEN);
|
||||
}
|
||||
|
||||
static void
|
||||
ipv6_fvalue_set(fvalue_t *fv, gpointer value, gboolean already_copied)
|
||||
{
|
||||
g_assert(!already_copied);
|
||||
common_fvalue_set(fv, value, IPv6_LEN);
|
||||
}
|
||||
|
||||
static gpointer
|
||||
value_get(fvalue_t *fv)
|
||||
{
|
||||
return fv->value.bytes->data;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
is_byte_sep(guint8 c)
|
||||
{
|
||||
return (c == '-' || c == ':' || c == '.');
|
||||
}
|
||||
|
||||
static gboolean
|
||||
val_from_string(fvalue_t *fv, char *s, LogFunc log)
|
||||
{
|
||||
GByteArray *bytes;
|
||||
guint8 val;
|
||||
char *p, *q, *punct;
|
||||
char two_digits[3];
|
||||
char one_digit[2];
|
||||
gboolean fail = FALSE;
|
||||
|
||||
bytes = g_byte_array_new();
|
||||
|
||||
p = s;
|
||||
while (*p) {
|
||||
q = p+1;
|
||||
if (*q && isxdigit(*p) && isxdigit(*q)) {
|
||||
two_digits[0] = *p;
|
||||
two_digits[1] = *q;
|
||||
two_digits[2] = '\0';
|
||||
|
||||
val = (guint8) strtoul(two_digits, NULL, 16);
|
||||
g_byte_array_append(bytes, &val, 1);
|
||||
punct = q + 1;
|
||||
if (*punct) {
|
||||
if (is_byte_sep(*punct)) {
|
||||
p = punct + 1;
|
||||
continue;
|
||||
}
|
||||
else {
|
||||
fail = TRUE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else {
|
||||
p = punct;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else if (*q && isxdigit(*p) && is_byte_sep(*q)) {
|
||||
one_digit[0] = *p;
|
||||
one_digit[1] = '\0';
|
||||
|
||||
val = (guint8) strtoul(one_digit, NULL, 16);
|
||||
g_byte_array_append(bytes, &val, 1);
|
||||
p = q + 1;
|
||||
continue;
|
||||
}
|
||||
else if (!*q && isxdigit(*p)) {
|
||||
one_digit[0] = *p;
|
||||
one_digit[1] = '\0';
|
||||
|
||||
val = (guint8) strtoul(one_digit, NULL, 16);
|
||||
g_byte_array_append(bytes, &val, 1);
|
||||
p = q;
|
||||
continue;
|
||||
}
|
||||
else {
|
||||
fail = TRUE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (fail) {
|
||||
g_byte_array_free(bytes, TRUE);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
fv->value.bytes = bytes;
|
||||
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
ether_from_string(fvalue_t *fv, char *s, LogFunc log)
|
||||
{
|
||||
guint8 *mac;
|
||||
|
||||
if (val_from_string(fv, s, log)) {
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
mac = get_ether_addr(s);
|
||||
if (!mac) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
ether_fvalue_set(fv, mac, FALSE);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
ipv6_from_string(fvalue_t *fv, char *s, LogFunc log)
|
||||
{
|
||||
guint8 buffer[16];
|
||||
|
||||
if (!get_host_ipaddr6(s, (struct e_in6_addr*)buffer)) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
ipv6_fvalue_set(fv, buffer, FALSE);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static guint
|
||||
len(fvalue_t *fv)
|
||||
{
|
||||
return fv->value.bytes->len;
|
||||
}
|
||||
|
||||
static void
|
||||
slice(fvalue_t *fv, GByteArray *bytes, guint offset, guint length)
|
||||
{
|
||||
guint8* data;
|
||||
|
||||
data = fv->value.bytes->data + offset;
|
||||
|
||||
g_byte_array_append(bytes, data, length);
|
||||
}
|
||||
|
||||
|
||||
static gboolean
|
||||
cmp_eq(fvalue_t *fv_a, fvalue_t *fv_b)
|
||||
{
|
||||
GByteArray *a = fv_a->value.bytes;
|
||||
GByteArray *b = fv_b->value.bytes;
|
||||
|
||||
if (a->len != b->len) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
return (memcmp(a->data, b->data, a->len) == 0);
|
||||
}
|
||||
|
||||
|
||||
static gboolean
|
||||
cmp_ne(fvalue_t *fv_a, fvalue_t *fv_b)
|
||||
{
|
||||
GByteArray *a = fv_a->value.bytes;
|
||||
GByteArray *b = fv_b->value.bytes;
|
||||
|
||||
if (a->len != b->len) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
return (memcmp(a->data, b->data, a->len) != 0);
|
||||
}
|
||||
|
||||
|
||||
static gboolean
|
||||
cmp_gt(fvalue_t *fv_a, fvalue_t *fv_b)
|
||||
{
|
||||
GByteArray *a = fv_a->value.bytes;
|
||||
GByteArray *b = fv_b->value.bytes;
|
||||
|
||||
if (a->len > b->len) {
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
if (a->len < b->len) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
return (memcmp(a->data, b->data, a->len) > 0);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
cmp_ge(fvalue_t *fv_a, fvalue_t *fv_b)
|
||||
{
|
||||
GByteArray *a = fv_a->value.bytes;
|
||||
GByteArray *b = fv_b->value.bytes;
|
||||
|
||||
if (a->len > b->len) {
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
if (a->len < b->len) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
return (memcmp(a->data, b->data, a->len) >= 0);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
cmp_lt(fvalue_t *fv_a, fvalue_t *fv_b)
|
||||
{
|
||||
GByteArray *a = fv_a->value.bytes;
|
||||
GByteArray *b = fv_b->value.bytes;
|
||||
|
||||
if (a->len < b->len) {
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
if (a->len > b->len) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
return (memcmp(a->data, b->data, a->len) < 0);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
cmp_le(fvalue_t *fv_a, fvalue_t *fv_b)
|
||||
{
|
||||
GByteArray *a = fv_a->value.bytes;
|
||||
GByteArray *b = fv_b->value.bytes;
|
||||
|
||||
if (a->len < b->len) {
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
if (a->len > b->len) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
return (memcmp(a->data, b->data, a->len) <= 0);
|
||||
}
|
||||
|
||||
void
|
||||
ftype_register_bytes(void)
|
||||
{
|
||||
|
||||
static ftype_t bytes_type = {
|
||||
"FT_BYTES",
|
||||
"sequence of bytes",
|
||||
0,
|
||||
bytes_fvalue_new,
|
||||
bytes_fvalue_free,
|
||||
ftype_from_tvbuff,
|
||||
val_from_string,
|
||||
|
||||
bytes_fvalue_set,
|
||||
NULL,
|
||||
NULL,
|
||||
|
||||
value_get,
|
||||
NULL,
|
||||
NULL,
|
||||
|
||||
cmp_eq,
|
||||
cmp_ne,
|
||||
cmp_gt,
|
||||
cmp_ge,
|
||||
cmp_lt,
|
||||
cmp_le,
|
||||
|
||||
len,
|
||||
slice,
|
||||
};
|
||||
|
||||
static ftype_t ether_type = {
|
||||
"FT_ETHER",
|
||||
"Ethernet or other MAC address",
|
||||
ETHER_LEN,
|
||||
bytes_fvalue_new,
|
||||
bytes_fvalue_free,
|
||||
ftype_from_tvbuff,
|
||||
ether_from_string,
|
||||
|
||||
ether_fvalue_set,
|
||||
NULL,
|
||||
NULL,
|
||||
|
||||
value_get,
|
||||
NULL,
|
||||
NULL,
|
||||
|
||||
cmp_eq,
|
||||
cmp_ne,
|
||||
cmp_gt,
|
||||
cmp_ge,
|
||||
cmp_lt,
|
||||
cmp_le,
|
||||
|
||||
len,
|
||||
slice,
|
||||
};
|
||||
|
||||
static ftype_t ipv6_type = {
|
||||
"FT_IPv6",
|
||||
"IPv6 address",
|
||||
IPv6_LEN,
|
||||
bytes_fvalue_new,
|
||||
bytes_fvalue_free,
|
||||
ftype_from_tvbuff,
|
||||
ipv6_from_string,
|
||||
|
||||
ipv6_fvalue_set,
|
||||
NULL,
|
||||
NULL,
|
||||
|
||||
value_get,
|
||||
NULL,
|
||||
NULL,
|
||||
|
||||
cmp_eq,
|
||||
cmp_ne,
|
||||
cmp_gt,
|
||||
cmp_ge,
|
||||
cmp_lt,
|
||||
cmp_le,
|
||||
|
||||
len,
|
||||
slice,
|
||||
};
|
||||
|
||||
ftype_register(FT_BYTES, &bytes_type);
|
||||
ftype_register(FT_ETHER, ðer_type);
|
||||
ftype_register(FT_IPv6, &ipv6_type);
|
||||
}
|
|
@ -0,0 +1,133 @@
|
|||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
#include <ftypes-int.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <errno.h>
|
||||
|
||||
static void
|
||||
ftype_from_tvbuff(field_info *fi, tvbuff_t *tvb, int start, int length,
|
||||
gboolean little_endian)
|
||||
{
|
||||
/* XXX */
|
||||
g_assert_not_reached();
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
double_fvalue_new(fvalue_t *fv)
|
||||
{
|
||||
fv->value.floating = 0.0;
|
||||
}
|
||||
|
||||
static void
|
||||
double_fvalue_set_floating(fvalue_t *fv, gdouble value)
|
||||
{
|
||||
fv->value.floating = value;
|
||||
}
|
||||
|
||||
static double
|
||||
value_get_floating(fvalue_t *fv)
|
||||
{
|
||||
return fv->value.floating;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
val_from_string(fvalue_t *fv, char *s, LogFunc log)
|
||||
{
|
||||
char *endptr = NULL;
|
||||
|
||||
fv->value.floating = strtod(s, &endptr);
|
||||
|
||||
if (endptr == s || *endptr != '\0') {
|
||||
/* This isn't a valid number. */
|
||||
log("\"%s\" is not a valid number.", s);
|
||||
return FALSE;
|
||||
}
|
||||
if (errno == ERANGE) {
|
||||
if (fv->value.floating == 0) {
|
||||
log("\"%s\" causes floating-point underflow.", s);
|
||||
}
|
||||
else if (fv->value.floating == HUGE_VAL) {
|
||||
log("\"%s\" causes floating-point overflow.", s);
|
||||
}
|
||||
else {
|
||||
log("\"%s\" is not a valid floating-point number.", s);
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
static gboolean
|
||||
cmp_eq(fvalue_t *a, fvalue_t *b)
|
||||
{
|
||||
return a->value.floating == b->value.floating;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
cmp_ne(fvalue_t *a, fvalue_t *b)
|
||||
{
|
||||
return a->value.floating != b->value.floating;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
cmp_gt(fvalue_t *a, fvalue_t *b)
|
||||
{
|
||||
return a->value.floating > b->value.floating;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
cmp_ge(fvalue_t *a, fvalue_t *b)
|
||||
{
|
||||
return a->value.floating >= b->value.floating;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
cmp_lt(fvalue_t *a, fvalue_t *b)
|
||||
{
|
||||
return a->value.floating < b->value.floating;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
cmp_le(fvalue_t *a, fvalue_t *b)
|
||||
{
|
||||
return a->value.floating <= b->value.floating;
|
||||
}
|
||||
|
||||
void
|
||||
ftype_register_double(void)
|
||||
{
|
||||
|
||||
static ftype_t double_type = {
|
||||
"FT_DOUBLE",
|
||||
"floating point",
|
||||
0,
|
||||
double_fvalue_new,
|
||||
NULL,
|
||||
ftype_from_tvbuff,
|
||||
val_from_string,
|
||||
|
||||
NULL,
|
||||
NULL,
|
||||
double_fvalue_set_floating,
|
||||
|
||||
NULL,
|
||||
NULL,
|
||||
value_get_floating,
|
||||
|
||||
cmp_eq,
|
||||
cmp_ne,
|
||||
cmp_gt,
|
||||
cmp_ge,
|
||||
cmp_lt,
|
||||
cmp_le,
|
||||
};
|
||||
|
||||
ftype_register(FT_DOUBLE, &double_type);
|
||||
}
|
|
@ -0,0 +1,417 @@
|
|||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef NEED_SNPRINTF_H
|
||||
#include "snprintf.h"
|
||||
#endif
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include "ftypes-int.h"
|
||||
#include "resolv.h"
|
||||
|
||||
|
||||
static void
|
||||
ftype_from_tvbuff(field_info *fi, tvbuff_t *tvb, int start, int length,
|
||||
gboolean little_endian)
|
||||
{
|
||||
/* XXX */
|
||||
g_assert_not_reached();
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
int_fvalue_new(fvalue_t *fv)
|
||||
{
|
||||
fv->value.integer = 0;
|
||||
}
|
||||
|
||||
static void
|
||||
set_integer(fvalue_t *fv, guint32 value)
|
||||
{
|
||||
fv->value.integer = value;
|
||||
}
|
||||
|
||||
static guint32
|
||||
get_integer(fvalue_t *fv)
|
||||
{
|
||||
return fv->value.integer;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
val_from_string(fvalue_t *fv, char *s, LogFunc log)
|
||||
{
|
||||
char *endptr;
|
||||
|
||||
fv->value.integer = strtoul(s, &endptr, 0);
|
||||
|
||||
if (endptr == s || *endptr != '\0') {
|
||||
/* This isn't a valid number. */
|
||||
log("\"%s\" is not a valid number.", s);
|
||||
return FALSE;
|
||||
}
|
||||
if (errno == ERANGE) {
|
||||
if (fv->value.integer == ULONG_MAX) {
|
||||
log("\"%s\" causes an integer overflow.", s);
|
||||
}
|
||||
else {
|
||||
log("\"%s\" is not an integer.", s);
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
ipxnet_from_string(fvalue_t *fv, char *s, LogFunc log)
|
||||
{
|
||||
guint32 val;
|
||||
gboolean known;
|
||||
|
||||
if (val_from_string(fv, s, log)) {
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
val = get_ipxnet_addr(s, &known);
|
||||
if (known) {
|
||||
fv->value.integer = val;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
cmp_eq(fvalue_t *a, fvalue_t *b)
|
||||
{
|
||||
return a->value.integer == b->value.integer;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
cmp_ne(fvalue_t *a, fvalue_t *b)
|
||||
{
|
||||
return a->value.integer != b->value.integer;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
u_cmp_gt(fvalue_t *a, fvalue_t *b)
|
||||
{
|
||||
return (int)a->value.integer > (int)b->value.integer;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
u_cmp_ge(fvalue_t *a, fvalue_t *b)
|
||||
{
|
||||
return (int)a->value.integer >= (int)b->value.integer;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
u_cmp_lt(fvalue_t *a, fvalue_t *b)
|
||||
{
|
||||
return (int)a->value.integer < (int)b->value.integer;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
u_cmp_le(fvalue_t *a, fvalue_t *b)
|
||||
{
|
||||
return (int)a->value.integer <= (int)b->value.integer;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
s_cmp_gt(fvalue_t *a, fvalue_t *b)
|
||||
{
|
||||
return a->value.integer > b->value.integer;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
s_cmp_ge(fvalue_t *a, fvalue_t *b)
|
||||
{
|
||||
return a->value.integer >= b->value.integer;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
s_cmp_lt(fvalue_t *a, fvalue_t *b)
|
||||
{
|
||||
return a->value.integer < b->value.integer;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
s_cmp_le(fvalue_t *a, fvalue_t *b)
|
||||
{
|
||||
return a->value.integer <= b->value.integer;
|
||||
}
|
||||
|
||||
/* BOOLEAN-specific */
|
||||
|
||||
static void
|
||||
boolean_fvalue_new(fvalue_t *fv)
|
||||
{
|
||||
fv->value.integer = TRUE;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void
|
||||
ftype_register_integers(void)
|
||||
{
|
||||
|
||||
static ftype_t uint8_type = {
|
||||
"FT_UINT8",
|
||||
"unsigned, 1 byte",
|
||||
1,
|
||||
int_fvalue_new,
|
||||
NULL,
|
||||
ftype_from_tvbuff,
|
||||
val_from_string,
|
||||
|
||||
NULL,
|
||||
set_integer,
|
||||
NULL,
|
||||
|
||||
NULL,
|
||||
get_integer,
|
||||
NULL,
|
||||
|
||||
cmp_eq,
|
||||
cmp_ne,
|
||||
u_cmp_gt,
|
||||
u_cmp_ge,
|
||||
u_cmp_lt,
|
||||
u_cmp_le,
|
||||
};
|
||||
static ftype_t uint16_type = {
|
||||
"FT_UINT16",
|
||||
"unsigned, 2 bytes",
|
||||
2,
|
||||
int_fvalue_new,
|
||||
NULL,
|
||||
ftype_from_tvbuff,
|
||||
val_from_string,
|
||||
|
||||
NULL,
|
||||
set_integer,
|
||||
NULL,
|
||||
|
||||
NULL,
|
||||
get_integer,
|
||||
NULL,
|
||||
|
||||
cmp_eq,
|
||||
cmp_ne,
|
||||
u_cmp_gt,
|
||||
u_cmp_ge,
|
||||
u_cmp_lt,
|
||||
u_cmp_le,
|
||||
};
|
||||
static ftype_t uint24_type = {
|
||||
"FT_UINT24",
|
||||
"unsigned, 3 bytes",
|
||||
3,
|
||||
int_fvalue_new,
|
||||
NULL,
|
||||
ftype_from_tvbuff,
|
||||
val_from_string,
|
||||
|
||||
NULL,
|
||||
set_integer,
|
||||
NULL,
|
||||
|
||||
NULL,
|
||||
get_integer,
|
||||
NULL,
|
||||
|
||||
cmp_eq,
|
||||
cmp_ne,
|
||||
u_cmp_gt,
|
||||
u_cmp_ge,
|
||||
u_cmp_lt,
|
||||
u_cmp_le,
|
||||
};
|
||||
static ftype_t uint32_type = {
|
||||
"FT_UINT32",
|
||||
"unsigned, 4 bytes",
|
||||
4,
|
||||
int_fvalue_new,
|
||||
NULL,
|
||||
ftype_from_tvbuff,
|
||||
val_from_string,
|
||||
|
||||
NULL,
|
||||
set_integer,
|
||||
NULL,
|
||||
|
||||
NULL,
|
||||
get_integer,
|
||||
NULL,
|
||||
|
||||
cmp_eq,
|
||||
cmp_ne,
|
||||
u_cmp_gt,
|
||||
u_cmp_ge,
|
||||
u_cmp_lt,
|
||||
u_cmp_le,
|
||||
};
|
||||
static ftype_t int8_type = {
|
||||
"FT_INT8",
|
||||
"signed, 1 byte",
|
||||
1,
|
||||
int_fvalue_new,
|
||||
NULL,
|
||||
ftype_from_tvbuff,
|
||||
val_from_string,
|
||||
|
||||
NULL,
|
||||
set_integer,
|
||||
NULL,
|
||||
|
||||
NULL,
|
||||
get_integer,
|
||||
NULL,
|
||||
|
||||
cmp_eq,
|
||||
cmp_ne,
|
||||
s_cmp_gt,
|
||||
s_cmp_ge,
|
||||
s_cmp_lt,
|
||||
s_cmp_le,
|
||||
};
|
||||
static ftype_t int16_type = {
|
||||
"FT_INT16",
|
||||
"signed, 2 bytes",
|
||||
2,
|
||||
int_fvalue_new,
|
||||
NULL,
|
||||
ftype_from_tvbuff,
|
||||
val_from_string,
|
||||
|
||||
NULL,
|
||||
set_integer,
|
||||
NULL,
|
||||
|
||||
NULL,
|
||||
get_integer,
|
||||
NULL,
|
||||
|
||||
cmp_eq,
|
||||
cmp_ne,
|
||||
s_cmp_gt,
|
||||
s_cmp_ge,
|
||||
s_cmp_lt,
|
||||
s_cmp_le,
|
||||
};
|
||||
static ftype_t int24_type = {
|
||||
"FT_INT24",
|
||||
"signed, 3 bytes",
|
||||
3,
|
||||
int_fvalue_new,
|
||||
NULL,
|
||||
ftype_from_tvbuff,
|
||||
val_from_string,
|
||||
|
||||
NULL,
|
||||
set_integer,
|
||||
NULL,
|
||||
|
||||
NULL,
|
||||
get_integer,
|
||||
NULL,
|
||||
|
||||
cmp_eq,
|
||||
cmp_ne,
|
||||
s_cmp_gt,
|
||||
s_cmp_ge,
|
||||
s_cmp_lt,
|
||||
s_cmp_le,
|
||||
};
|
||||
static ftype_t int32_type = {
|
||||
"FT_INT32",
|
||||
"signed, 4 bytes",
|
||||
4,
|
||||
int_fvalue_new,
|
||||
NULL,
|
||||
ftype_from_tvbuff,
|
||||
val_from_string,
|
||||
|
||||
NULL,
|
||||
set_integer,
|
||||
NULL,
|
||||
|
||||
NULL,
|
||||
get_integer,
|
||||
NULL,
|
||||
|
||||
cmp_eq,
|
||||
cmp_ne,
|
||||
s_cmp_gt,
|
||||
s_cmp_ge,
|
||||
s_cmp_lt,
|
||||
s_cmp_le,
|
||||
};
|
||||
static ftype_t boolean_type = {
|
||||
"FT_BOOLEAN",
|
||||
"Boolean",
|
||||
0,
|
||||
boolean_fvalue_new,
|
||||
NULL,
|
||||
ftype_from_tvbuff,
|
||||
val_from_string,
|
||||
|
||||
NULL,
|
||||
set_integer,
|
||||
NULL,
|
||||
|
||||
NULL,
|
||||
get_integer,
|
||||
NULL,
|
||||
|
||||
cmp_eq,
|
||||
cmp_ne,
|
||||
u_cmp_gt,
|
||||
u_cmp_ge,
|
||||
u_cmp_lt,
|
||||
u_cmp_le,
|
||||
};
|
||||
|
||||
static ftype_t ipxnet_type = {
|
||||
"FT_IPXNET",
|
||||
"IPX network number",
|
||||
4,
|
||||
int_fvalue_new,
|
||||
NULL,
|
||||
ftype_from_tvbuff,
|
||||
ipxnet_from_string,
|
||||
|
||||
NULL,
|
||||
set_integer,
|
||||
NULL,
|
||||
|
||||
NULL,
|
||||
get_integer,
|
||||
NULL,
|
||||
|
||||
cmp_eq,
|
||||
cmp_ne,
|
||||
u_cmp_gt,
|
||||
u_cmp_ge,
|
||||
u_cmp_lt,
|
||||
u_cmp_le,
|
||||
};
|
||||
|
||||
|
||||
ftype_register(FT_UINT8, &uint8_type);
|
||||
ftype_register(FT_UINT16, &uint16_type);
|
||||
ftype_register(FT_UINT24, &uint24_type);
|
||||
ftype_register(FT_UINT32, &uint32_type);
|
||||
ftype_register(FT_INT8, &int8_type);
|
||||
ftype_register(FT_INT16, &int16_type);
|
||||
ftype_register(FT_INT24, &int24_type);
|
||||
ftype_register(FT_INT32, &int32_type);
|
||||
ftype_register(FT_BOOLEAN, &boolean_type);
|
||||
ftype_register(FT_IPXNET, &ipxnet_type);
|
||||
}
|
|
@ -0,0 +1,114 @@
|
|||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
#include <ftypes-int.h>
|
||||
#include "ipv4.h"
|
||||
#include "resolv.h"
|
||||
|
||||
static void
|
||||
ftype_from_tvbuff(field_info *fi, tvbuff_t *tvb, int start, int length,
|
||||
gboolean little_endian)
|
||||
{
|
||||
/* XXX */
|
||||
g_assert_not_reached();
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void
|
||||
set_integer(fvalue_t *fv, guint32 value)
|
||||
{
|
||||
ipv4_addr_set_net_order_addr(&(fv->value.ipv4), value);
|
||||
ipv4_addr_set_netmask_bits(&(fv->value.ipv4), 32);
|
||||
}
|
||||
|
||||
static gpointer
|
||||
value_get(fvalue_t *fv)
|
||||
{
|
||||
return &(fv->value.ipv4);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
val_from_string(fvalue_t *fv, char *s, LogFunc log)
|
||||
{
|
||||
guint32 addr;
|
||||
|
||||
if (!get_host_ipaddr(s, &addr)) {
|
||||
log("\"%s\" is not a valid hostname or IPv4 address.", s);
|
||||
return FALSE;
|
||||
}
|
||||
ipv4_addr_set_host_order_addr(&(fv->value.ipv4), addr);
|
||||
/*ipv4_addr_set_netmask_bits(&node->value.ipv4, nmask_bits);*/
|
||||
ipv4_addr_set_netmask_bits(&(fv->value.ipv4), 32);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
cmp_eq(fvalue_t *a, fvalue_t *b)
|
||||
{
|
||||
return ipv4_addr_eq(&a->value.ipv4, &b->value.ipv4);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
cmp_ne(fvalue_t *a, fvalue_t *b)
|
||||
{
|
||||
return ipv4_addr_ne(&a->value.ipv4, &b->value.ipv4);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
cmp_gt(fvalue_t *a, fvalue_t *b)
|
||||
{
|
||||
return ipv4_addr_gt(&a->value.ipv4, &b->value.ipv4);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
cmp_ge(fvalue_t *a, fvalue_t *b)
|
||||
{
|
||||
return ipv4_addr_ge(&a->value.ipv4, &b->value.ipv4);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
cmp_lt(fvalue_t *a, fvalue_t *b)
|
||||
{
|
||||
return ipv4_addr_lt(&a->value.ipv4, &b->value.ipv4);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
cmp_le(fvalue_t *a, fvalue_t *b)
|
||||
{
|
||||
return ipv4_addr_le(&a->value.ipv4, &b->value.ipv4);
|
||||
}
|
||||
|
||||
void
|
||||
ftype_register_ipv4(void)
|
||||
{
|
||||
|
||||
static ftype_t ipv4_type = {
|
||||
"FT_IPv4",
|
||||
"IPv4 address",
|
||||
4,
|
||||
NULL,
|
||||
NULL,
|
||||
ftype_from_tvbuff,
|
||||
val_from_string,
|
||||
|
||||
NULL,
|
||||
set_integer,
|
||||
NULL,
|
||||
|
||||
value_get,
|
||||
NULL,
|
||||
NULL,
|
||||
|
||||
cmp_eq,
|
||||
cmp_ne,
|
||||
cmp_gt,
|
||||
cmp_ge,
|
||||
cmp_lt,
|
||||
cmp_le,
|
||||
};
|
||||
|
||||
ftype_register(FT_IPv4, &ipv4_type);
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
#include <ftypes-int.h>
|
||||
|
||||
|
||||
void
|
||||
ftype_register_none(void)
|
||||
{
|
||||
|
||||
static ftype_t none_type = {
|
||||
"FT_NONE",
|
||||
"label",
|
||||
0,
|
||||
};
|
||||
|
||||
ftype_register(FT_NONE, &none_type);
|
||||
}
|
|
@ -0,0 +1,198 @@
|
|||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
#include <ftypes-int.h>
|
||||
#include <string.h>
|
||||
|
||||
static void
|
||||
ftype_from_tvbuff(field_info *fi, tvbuff_t *tvb, int start, int length,
|
||||
gboolean little_endian)
|
||||
{
|
||||
/* XXX */
|
||||
g_assert_not_reached();
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
string_fvalue_new(fvalue_t *fv)
|
||||
{
|
||||
fv->value.string = NULL;
|
||||
}
|
||||
|
||||
static void
|
||||
string_fvalue_free(fvalue_t *fv)
|
||||
{
|
||||
if (fv->value.string) {
|
||||
g_free(fv->value.string);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
string_fvalue_set(fvalue_t *fv, gpointer value, gboolean already_copied)
|
||||
{
|
||||
if (already_copied) {
|
||||
fv->value.string = value;
|
||||
}
|
||||
else {
|
||||
fv->value.string = g_strdup(value);
|
||||
}
|
||||
}
|
||||
|
||||
static gpointer
|
||||
value_get(fvalue_t *fv)
|
||||
{
|
||||
return fv->value.string;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
val_from_string(fvalue_t *fv, char *s, LogFunc log)
|
||||
{
|
||||
fv->value.string = g_strdup(s);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static guint
|
||||
len(fvalue_t *fv)
|
||||
{
|
||||
return strlen(fv->value.string);
|
||||
}
|
||||
|
||||
static void
|
||||
slice(fvalue_t *fv, GByteArray *bytes, guint offset, guint length)
|
||||
{
|
||||
guint8* data;
|
||||
|
||||
data = fv->value.string + offset;
|
||||
|
||||
g_byte_array_append(bytes, data, length);
|
||||
}
|
||||
|
||||
|
||||
static gboolean
|
||||
cmp_eq(fvalue_t *a, fvalue_t *b)
|
||||
{
|
||||
return (strcmp(a->value.string, b->value.string) == 0);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
cmp_ne(fvalue_t *a, fvalue_t *b)
|
||||
{
|
||||
return (strcmp(a->value.string, b->value.string) != 0);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
cmp_gt(fvalue_t *a, fvalue_t *b)
|
||||
{
|
||||
return (strcmp(a->value.string, b->value.string) > 0);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
cmp_ge(fvalue_t *a, fvalue_t *b)
|
||||
{
|
||||
return (strcmp(a->value.string, b->value.string) >= 0);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
cmp_lt(fvalue_t *a, fvalue_t *b)
|
||||
{
|
||||
return (strcmp(a->value.string, b->value.string) < 0);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
cmp_le(fvalue_t *a, fvalue_t *b)
|
||||
{
|
||||
return (strcmp(a->value.string, b->value.string) <= 0);
|
||||
}
|
||||
|
||||
void
|
||||
ftype_register_string(void)
|
||||
{
|
||||
|
||||
static ftype_t string_type = {
|
||||
"FT_STRING",
|
||||
"character string",
|
||||
0,
|
||||
string_fvalue_new,
|
||||
string_fvalue_free,
|
||||
ftype_from_tvbuff,
|
||||
val_from_string,
|
||||
|
||||
string_fvalue_set,
|
||||
NULL,
|
||||
NULL,
|
||||
|
||||
value_get,
|
||||
NULL,
|
||||
NULL,
|
||||
|
||||
cmp_eq,
|
||||
cmp_ne,
|
||||
cmp_gt,
|
||||
cmp_ge,
|
||||
cmp_lt,
|
||||
cmp_le,
|
||||
|
||||
len,
|
||||
slice,
|
||||
};
|
||||
static ftype_t stringz_type = {
|
||||
"FT_STRINGZ",
|
||||
"character string",
|
||||
0,
|
||||
string_fvalue_new,
|
||||
string_fvalue_free,
|
||||
ftype_from_tvbuff,
|
||||
val_from_string,
|
||||
|
||||
string_fvalue_set,
|
||||
NULL,
|
||||
NULL,
|
||||
|
||||
value_get,
|
||||
NULL,
|
||||
NULL,
|
||||
|
||||
cmp_eq,
|
||||
cmp_ne,
|
||||
cmp_gt,
|
||||
cmp_ge,
|
||||
cmp_lt,
|
||||
cmp_le,
|
||||
|
||||
len,
|
||||
slice,
|
||||
};
|
||||
static ftype_t uint_string_type = {
|
||||
"FT_UINT_STRING",
|
||||
"character string",
|
||||
0,
|
||||
string_fvalue_new,
|
||||
string_fvalue_free,
|
||||
ftype_from_tvbuff,
|
||||
val_from_string,
|
||||
|
||||
string_fvalue_set,
|
||||
NULL,
|
||||
NULL,
|
||||
|
||||
value_get,
|
||||
NULL,
|
||||
NULL,
|
||||
|
||||
cmp_eq,
|
||||
cmp_ne,
|
||||
cmp_gt,
|
||||
cmp_ge,
|
||||
cmp_lt,
|
||||
cmp_le,
|
||||
|
||||
len,
|
||||
slice,
|
||||
};
|
||||
|
||||
ftype_register(FT_STRING, &string_type);
|
||||
ftype_register(FT_STRINGZ, &stringz_type);
|
||||
ftype_register(FT_UINT_STRING, &uint_string_type);
|
||||
}
|
|
@ -0,0 +1,78 @@
|
|||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
#include <ftypes-int.h>
|
||||
|
||||
static void
|
||||
ftype_from_tvbuff(field_info *fi, tvbuff_t *tvb, int start, int length,
|
||||
gboolean little_endian)
|
||||
{
|
||||
/* XXX */
|
||||
g_assert_not_reached();
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
time_fvalue_new(fvalue_t *fv)
|
||||
{
|
||||
fv->value.time.tv_sec = 0;
|
||||
fv->value.time.tv_usec = 0;
|
||||
}
|
||||
|
||||
static void
|
||||
time_fvalue_set(fvalue_t *fv, gpointer value, gboolean already_copied)
|
||||
{
|
||||
g_assert(!already_copied);
|
||||
memcpy(&(fv->value.time), value, sizeof(struct timeval));
|
||||
}
|
||||
|
||||
static gpointer
|
||||
value_get(fvalue_t *fv)
|
||||
{
|
||||
return &(fv->value.time);
|
||||
}
|
||||
|
||||
void
|
||||
ftype_register_time(void)
|
||||
{
|
||||
|
||||
static ftype_t abstime_type = {
|
||||
"FT_ABSOLUTE_TIME",
|
||||
"date/time",
|
||||
0,
|
||||
time_fvalue_new,
|
||||
NULL,
|
||||
ftype_from_tvbuff,
|
||||
NULL,
|
||||
|
||||
time_fvalue_set,
|
||||
NULL,
|
||||
NULL,
|
||||
|
||||
value_get,
|
||||
NULL,
|
||||
NULL
|
||||
};
|
||||
static ftype_t reltime_type = {
|
||||
"FT_RELATIVE_TIME",
|
||||
"time offset",
|
||||
0,
|
||||
time_fvalue_new,
|
||||
NULL,
|
||||
ftype_from_tvbuff,
|
||||
NULL,
|
||||
|
||||
time_fvalue_set,
|
||||
NULL,
|
||||
NULL,
|
||||
|
||||
value_get,
|
||||
NULL,
|
||||
NULL
|
||||
};
|
||||
|
||||
ftype_register(FT_ABSOLUTE_TIME, &abstime_type);
|
||||
ftype_register(FT_RELATIVE_TIME, &reltime_type);
|
||||
}
|
|
@ -0,0 +1,84 @@
|
|||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
#include <ftypes-int.h>
|
||||
#include "gdebug.h"
|
||||
|
||||
static void
|
||||
value_new(fvalue_t *fv)
|
||||
{
|
||||
fv->value.tvb = NULL;
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
value_set(fvalue_t *fv, gpointer value, gboolean already_copied)
|
||||
{
|
||||
g_assert(already_copied);
|
||||
fv->value.tvb = value;
|
||||
}
|
||||
|
||||
static gpointer
|
||||
value_get(fvalue_t *fv)
|
||||
{
|
||||
return fv->value.tvb;
|
||||
}
|
||||
|
||||
static guint
|
||||
len(fvalue_t *fv)
|
||||
{
|
||||
if (fv->value.tvb)
|
||||
return tvb_length(fv->value.tvb);
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
slice(fvalue_t *fv, GByteArray *bytes, guint offset, guint length)
|
||||
{
|
||||
guint8* data;
|
||||
|
||||
if (fv->value.tvb) {
|
||||
data = tvb_get_ptr(fv->value.tvb, offset, length);
|
||||
g_byte_array_append(bytes, data, length);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
ftype_register_tvbuff(void)
|
||||
{
|
||||
|
||||
static ftype_t protocol_type = {
|
||||
"FT_PROTOCOL",
|
||||
"protocol",
|
||||
0,
|
||||
value_new,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
|
||||
value_set,
|
||||
NULL,
|
||||
NULL,
|
||||
|
||||
value_get,
|
||||
NULL,
|
||||
NULL,
|
||||
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
|
||||
len,
|
||||
slice,
|
||||
|
||||
};
|
||||
|
||||
|
||||
ftype_register(FT_PROTOCOL, &protocol_type);
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
#ifndef FTYPES_INT_H
|
||||
#define FTYPES_INT_H
|
||||
|
||||
#include "packet.h"
|
||||
#include "ftypes.h"
|
||||
|
||||
typedef void (*FtypeFromTvbuffFunc)(field_info*, tvbuff_t*, int, int, gboolean);
|
||||
typedef void (*FvalueNewFunc)(fvalue_t*);
|
||||
typedef void (*FvalueFreeFunc)(fvalue_t*);
|
||||
|
||||
typedef gboolean (*FvalueFromString)(fvalue_t*, char*, LogFunc);
|
||||
|
||||
typedef void (*FvalueSetFunc)(fvalue_t*, gpointer, gboolean);
|
||||
typedef void (*FvalueSetIntegerFunc)(fvalue_t*, guint32);
|
||||
typedef void (*FvalueSetFloatingFunc)(fvalue_t*, gdouble);
|
||||
|
||||
typedef gpointer (*FvalueGetFunc)(fvalue_t*);
|
||||
typedef guint32 (*FvalueGetIntegerFunc)(fvalue_t*);
|
||||
typedef double (*FvalueGetFloatingFunc)(fvalue_t*);
|
||||
|
||||
typedef gboolean (*FvalueCmp)(fvalue_t*, fvalue_t*);
|
||||
|
||||
typedef guint (*FvalueLen)(fvalue_t*);
|
||||
typedef void (*FvalueSlice)(fvalue_t*, GByteArray *, guint, guint);
|
||||
|
||||
struct _ftype_t {
|
||||
const char *name;
|
||||
const char *pretty_name;
|
||||
int wire_size;
|
||||
FvalueNewFunc new_value;
|
||||
FvalueFreeFunc free_value;
|
||||
FtypeFromTvbuffFunc from_tvbuff;
|
||||
FvalueFromString val_from_string;
|
||||
|
||||
/* could be union */
|
||||
FvalueSetFunc set_value;
|
||||
FvalueSetIntegerFunc set_value_integer;
|
||||
FvalueSetFloatingFunc set_value_floating;
|
||||
|
||||
/* could be union */
|
||||
FvalueGetFunc get_value;
|
||||
FvalueGetIntegerFunc get_value_integer;
|
||||
FvalueGetFloatingFunc get_value_floating;
|
||||
|
||||
FvalueCmp cmp_eq;
|
||||
FvalueCmp cmp_ne;
|
||||
FvalueCmp cmp_gt;
|
||||
FvalueCmp cmp_ge;
|
||||
FvalueCmp cmp_lt;
|
||||
FvalueCmp cmp_le;
|
||||
|
||||
FvalueLen len;
|
||||
FvalueSlice slice;
|
||||
};
|
||||
|
||||
|
||||
void
|
||||
ftype_register(enum ftenum ftype, ftype_t *ft);
|
||||
|
||||
#endif
|
|
@ -0,0 +1,410 @@
|
|||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
#include <ftypes-int.h>
|
||||
|
||||
/* Keep track of ftype_t's via their ftenum number */
|
||||
static ftype_t* type_list[FT_NUM_TYPES];
|
||||
|
||||
/* Space for quickly allocating/de-allocating fvalue_t's */
|
||||
static GMemChunk *gmc_fvalue = NULL;
|
||||
|
||||
/* These are the ftype registration functions that need to be called.
|
||||
* This list and the initialization function could be produced
|
||||
* via a script, like the dissector registration, but there's so few
|
||||
* that I don't mind doing it by hand for now. */
|
||||
void ftype_register_bytes(void);
|
||||
void ftype_register_double(void);
|
||||
void ftype_register_integers(void);
|
||||
void ftype_register_ipv4(void);
|
||||
void ftype_register_none(void);
|
||||
void ftype_register_string(void);
|
||||
void ftype_register_time(void);
|
||||
void ftype_register_tvbuff(void);
|
||||
|
||||
/* Initialize the ftype module. */
|
||||
void
|
||||
ftypes_initialize(void)
|
||||
{
|
||||
ftype_register_bytes();
|
||||
ftype_register_double();
|
||||
ftype_register_integers();
|
||||
ftype_register_ipv4();
|
||||
ftype_register_none();
|
||||
ftype_register_string();
|
||||
ftype_register_time();
|
||||
ftype_register_tvbuff();
|
||||
|
||||
if (gmc_fvalue)
|
||||
g_mem_chunk_destroy(gmc_fvalue);
|
||||
|
||||
gmc_fvalue = g_mem_chunk_new("gmc_fvalue", sizeof(fvalue_t),
|
||||
200 * sizeof(fvalue_t), G_ALLOC_AND_FREE);
|
||||
}
|
||||
|
||||
void
|
||||
ftypes_cleanup(void)
|
||||
{
|
||||
if (gmc_fvalue)
|
||||
g_mem_chunk_destroy(gmc_fvalue);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* Each ftype_t is registered via this function */
|
||||
void
|
||||
ftype_register(enum ftenum ftype, ftype_t *ft)
|
||||
{
|
||||
/* Check input */
|
||||
g_assert(ftype >= 0);
|
||||
g_assert(ftype < FT_NUM_TYPES);
|
||||
|
||||
/* Don't re-register. */
|
||||
g_assert(type_list[ftype] == NULL);
|
||||
|
||||
type_list[ftype] = ft;
|
||||
}
|
||||
|
||||
/* Given an ftenum number, return an ftype_t* */
|
||||
static ftype_t*
|
||||
ftype_lookup(enum ftenum ftype)
|
||||
{
|
||||
ftype_t* result;
|
||||
|
||||
/* Check input */
|
||||
g_assert(ftype >= 0);
|
||||
g_assert(ftype < FT_NUM_TYPES);
|
||||
|
||||
result = type_list[ftype];
|
||||
|
||||
/* Check output. */
|
||||
g_assert(result != NULL);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/* Returns a string representing the name of the type. Useful
|
||||
* for glossary production. */
|
||||
const char*
|
||||
ftype_name(enum ftenum ftype)
|
||||
{
|
||||
ftype_t *ft;
|
||||
|
||||
ft = ftype_lookup(ftype);
|
||||
return ft->name;
|
||||
}
|
||||
|
||||
const char*
|
||||
ftype_pretty_name(enum ftenum ftype)
|
||||
{
|
||||
ftype_t *ft;
|
||||
|
||||
ft = ftype_lookup(ftype);
|
||||
return ft->pretty_name;
|
||||
}
|
||||
|
||||
int
|
||||
ftype_length(enum ftenum ftype)
|
||||
{
|
||||
ftype_t *ft;
|
||||
|
||||
ft = ftype_lookup(ftype);
|
||||
return ft->wire_size;
|
||||
}
|
||||
|
||||
gboolean
|
||||
ftype_can_slice(enum ftenum ftype)
|
||||
{
|
||||
ftype_t *ft;
|
||||
|
||||
ft = ftype_lookup(ftype);
|
||||
return ft->slice ? TRUE : FALSE;
|
||||
}
|
||||
|
||||
gboolean
|
||||
ftype_can_eq(enum ftenum ftype)
|
||||
{
|
||||
ftype_t *ft;
|
||||
|
||||
ft = ftype_lookup(ftype);
|
||||
return ft->cmp_eq ? TRUE : FALSE;
|
||||
}
|
||||
|
||||
gboolean
|
||||
ftype_can_ne(enum ftenum ftype)
|
||||
{
|
||||
ftype_t *ft;
|
||||
|
||||
ft = ftype_lookup(ftype);
|
||||
return ft->cmp_ne ? TRUE : FALSE;
|
||||
}
|
||||
|
||||
gboolean
|
||||
ftype_can_gt(enum ftenum ftype)
|
||||
{
|
||||
ftype_t *ft;
|
||||
|
||||
ft = ftype_lookup(ftype);
|
||||
return ft->cmp_gt ? TRUE : FALSE;
|
||||
}
|
||||
|
||||
gboolean
|
||||
ftype_can_ge(enum ftenum ftype)
|
||||
{
|
||||
ftype_t *ft;
|
||||
|
||||
ft = ftype_lookup(ftype);
|
||||
return ft->cmp_ge ? TRUE : FALSE;
|
||||
}
|
||||
|
||||
gboolean
|
||||
ftype_can_lt(enum ftenum ftype)
|
||||
{
|
||||
ftype_t *ft;
|
||||
|
||||
ft = ftype_lookup(ftype);
|
||||
return ft->cmp_lt ? TRUE : FALSE;
|
||||
}
|
||||
|
||||
gboolean
|
||||
ftype_can_le(enum ftenum ftype)
|
||||
{
|
||||
ftype_t *ft;
|
||||
|
||||
ft = ftype_lookup(ftype);
|
||||
return ft->cmp_le ? TRUE : FALSE;
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------- */
|
||||
|
||||
/* Allocate and initialize an fvalue_t, given an ftype */
|
||||
fvalue_t*
|
||||
fvalue_new(ftenum_t ftype)
|
||||
{
|
||||
fvalue_t *fv;
|
||||
ftype_t *ft;
|
||||
FvalueNewFunc new_value;
|
||||
|
||||
fv = g_mem_chunk_alloc(gmc_fvalue);
|
||||
|
||||
ft = ftype_lookup(ftype);
|
||||
fv->ftype = ft;
|
||||
|
||||
new_value = ft->new_value;
|
||||
if (new_value) {
|
||||
new_value(fv);
|
||||
}
|
||||
|
||||
return fv;
|
||||
}
|
||||
|
||||
/* Free all memory used by an fvalue_t */
|
||||
void
|
||||
fvalue_free(fvalue_t *fv)
|
||||
{
|
||||
FvalueFreeFunc free_value;
|
||||
|
||||
free_value = fv->ftype->free_value;
|
||||
if (free_value) {
|
||||
free_value(fv);
|
||||
}
|
||||
|
||||
g_mem_chunk_free(gmc_fvalue, fv);
|
||||
}
|
||||
|
||||
|
||||
|
||||
fvalue_t*
|
||||
fvalue_from_string(ftenum_t ftype, char *s, LogFunc log)
|
||||
{
|
||||
fvalue_t *fv;
|
||||
|
||||
fv = fvalue_new(ftype);
|
||||
if (fv->ftype->val_from_string) {
|
||||
if (fv->ftype->val_from_string(fv, s, log)) {
|
||||
return fv;
|
||||
}
|
||||
}
|
||||
else {
|
||||
log("\"%s\" cannot be converted to %s.",
|
||||
s, ftype_pretty_name(ftype));
|
||||
}
|
||||
fvalue_free(fv);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const char*
|
||||
fvalue_type_name(fvalue_t *fv)
|
||||
{
|
||||
return fv->ftype->name;
|
||||
}
|
||||
|
||||
|
||||
guint
|
||||
fvalue_length(fvalue_t *fv)
|
||||
{
|
||||
if (fv->ftype->len)
|
||||
return fv->ftype->len(fv);
|
||||
else
|
||||
return fv->ftype->wire_size;
|
||||
}
|
||||
|
||||
/* Returns a new FT_BYTES fvalue_t* if possible, otherwise NULL */
|
||||
fvalue_t*
|
||||
fvalue_slice(fvalue_t *fv, gint start, gint end)
|
||||
{
|
||||
GByteArray *bytes;
|
||||
guint data_length, abs_end;
|
||||
guint offset=0, length=0;
|
||||
fvalue_t *new_fv;
|
||||
|
||||
if (!fv->ftype->slice) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
data_length = fvalue_length(fv);
|
||||
bytes = g_byte_array_new();
|
||||
|
||||
/* Find absolute start position (offset) */
|
||||
if (start < 0) {
|
||||
start = data_length + start;
|
||||
if (start < 0) {
|
||||
offset = 0;
|
||||
}
|
||||
else {
|
||||
offset = start;
|
||||
}
|
||||
}
|
||||
else {
|
||||
offset = start;
|
||||
}
|
||||
|
||||
/* Limit the offset value */
|
||||
if (offset > data_length) {
|
||||
offset = data_length;
|
||||
}
|
||||
|
||||
/* Find absolute end position (abs_end) */
|
||||
if (end < 0) {
|
||||
end = data_length + end;
|
||||
if (end < 0) {
|
||||
abs_end = 0;
|
||||
}
|
||||
else {
|
||||
abs_end = end;
|
||||
}
|
||||
}
|
||||
else {
|
||||
abs_end = end;
|
||||
}
|
||||
|
||||
/* Limit the abs_end value */
|
||||
if (abs_end > data_length) {
|
||||
abs_end = data_length;
|
||||
}
|
||||
|
||||
/* Does end position occur *after* start position? */
|
||||
if (abs_end > offset) {
|
||||
length = abs_end - offset;
|
||||
fv->ftype->slice(fv, bytes, offset, length);
|
||||
}
|
||||
|
||||
new_fv = fvalue_new(FT_BYTES);
|
||||
fvalue_set(new_fv, bytes, TRUE);
|
||||
return new_fv;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
fvalue_set(fvalue_t *fv, gpointer value, gboolean already_copied)
|
||||
{
|
||||
g_assert(fv->ftype->set_value);
|
||||
fv->ftype->set_value(fv, value, already_copied);
|
||||
}
|
||||
|
||||
void
|
||||
fvalue_set_integer(fvalue_t *fv, guint32 value)
|
||||
{
|
||||
g_assert(fv->ftype->set_value_integer);
|
||||
fv->ftype->set_value_integer(fv, value);
|
||||
}
|
||||
|
||||
void
|
||||
fvalue_set_floating(fvalue_t *fv, gdouble value)
|
||||
{
|
||||
g_assert(fv->ftype->set_value_floating);
|
||||
fv->ftype->set_value_floating(fv, value);
|
||||
}
|
||||
|
||||
|
||||
gpointer
|
||||
fvalue_get(fvalue_t *fv)
|
||||
{
|
||||
g_assert(fv->ftype->get_value);
|
||||
return fv->ftype->get_value(fv);
|
||||
}
|
||||
|
||||
guint32
|
||||
fvalue_get_integer(fvalue_t *fv)
|
||||
{
|
||||
g_assert(fv->ftype->get_value_integer);
|
||||
return fv->ftype->get_value_integer(fv);
|
||||
}
|
||||
|
||||
double
|
||||
fvalue_get_floating(fvalue_t *fv)
|
||||
{
|
||||
g_assert(fv->ftype->get_value_floating);
|
||||
return fv->ftype->get_value_floating(fv);
|
||||
}
|
||||
|
||||
gboolean
|
||||
fvalue_eq(fvalue_t *a, fvalue_t *b)
|
||||
{
|
||||
/* XXX - check compatibility of a and b */
|
||||
g_assert(a->ftype->cmp_eq);
|
||||
return a->ftype->cmp_eq(a, b);
|
||||
}
|
||||
|
||||
gboolean
|
||||
fvalue_ne(fvalue_t *a, fvalue_t *b)
|
||||
{
|
||||
/* XXX - check compatibility of a and b */
|
||||
g_assert(a->ftype->cmp_ne);
|
||||
return a->ftype->cmp_ne(a, b);
|
||||
}
|
||||
|
||||
gboolean
|
||||
fvalue_gt(fvalue_t *a, fvalue_t *b)
|
||||
{
|
||||
/* XXX - check compatibility of a and b */
|
||||
g_assert(a->ftype->cmp_gt);
|
||||
return a->ftype->cmp_gt(a, b);
|
||||
}
|
||||
|
||||
gboolean
|
||||
fvalue_ge(fvalue_t *a, fvalue_t *b)
|
||||
{
|
||||
/* XXX - check compatibility of a and b */
|
||||
g_assert(a->ftype->cmp_ge);
|
||||
return a->ftype->cmp_ge(a, b);
|
||||
}
|
||||
|
||||
gboolean
|
||||
fvalue_lt(fvalue_t *a, fvalue_t *b)
|
||||
{
|
||||
/* XXX - check compatibility of a and b */
|
||||
g_assert(a->ftype->cmp_lt);
|
||||
return a->ftype->cmp_lt(a, b);
|
||||
}
|
||||
|
||||
gboolean
|
||||
fvalue_le(fvalue_t *a, fvalue_t *b)
|
||||
{
|
||||
/* XXX - check compatibility of a and b */
|
||||
g_assert(a->ftype->cmp_le);
|
||||
return a->ftype->cmp_le(a, b);
|
||||
}
|
|
@ -0,0 +1,197 @@
|
|||
/* ftypes.h
|
||||
* Definitions for field types
|
||||
*
|
||||
* $Id: ftypes.h,v 1.1 2001/02/01 20:21:19 gram Exp $
|
||||
*
|
||||
* Ethereal - Network traffic analyzer
|
||||
* By Gerald Combs <gerald@zing.org>
|
||||
* Copyright 2001 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef FTYPES_H
|
||||
#define FTYPES_H
|
||||
|
||||
#include <glib.h>
|
||||
|
||||
|
||||
/* field types */
|
||||
enum ftenum {
|
||||
FT_NONE, /* used for text labels with no value */
|
||||
FT_PROTOCOL,
|
||||
FT_BOOLEAN, /* TRUE and FALSE come from <glib.h> */
|
||||
FT_UINT8,
|
||||
FT_UINT16,
|
||||
FT_UINT24, /* really a UINT32, but displayed as 3 hex-digits if FD_HEX*/
|
||||
FT_UINT32,
|
||||
FT_INT8,
|
||||
FT_INT16,
|
||||
FT_INT24,
|
||||
FT_INT32,
|
||||
FT_DOUBLE,
|
||||
FT_ABSOLUTE_TIME,
|
||||
FT_RELATIVE_TIME,
|
||||
FT_STRING,
|
||||
FT_STRINGZ, /* for use with proto_tree_add_item() */
|
||||
FT_UINT_STRING, /* for use with proto_tree_add_item() */
|
||||
FT_ETHER,
|
||||
FT_BYTES,
|
||||
FT_IPv4,
|
||||
FT_IPv6,
|
||||
FT_IPXNET,
|
||||
/* FT_TEXT_ONLY,*/ /* non-filterable, used when converting ethereal
|
||||
from old-style proto_tree to new-style proto_tree */
|
||||
FT_NUM_TYPES /* last item number plus one */
|
||||
};
|
||||
|
||||
typedef enum ftenum ftenum_t;
|
||||
typedef struct _ftype_t ftype_t;
|
||||
|
||||
/* Initialize the ftypes subsytem. Called once. */
|
||||
void
|
||||
ftypes_initialize(void);
|
||||
|
||||
/* Cleanup the ftypes subsystem. Called once. */
|
||||
void
|
||||
ftypes_cleanup(void);
|
||||
|
||||
|
||||
/* ---------------- FTYPE ----------------- */
|
||||
|
||||
/* Return a string representing the name of the type */
|
||||
const char*
|
||||
ftype_name(ftenum_t ftype);
|
||||
|
||||
/* Return a string presenting a "pretty" representation of the
|
||||
* name of the type. The pretty name means more to the user than
|
||||
* that "FT_*" name. */
|
||||
const char*
|
||||
ftype_pretty_name(ftenum_t ftype);
|
||||
|
||||
/* Returns length of field in packet, or 0 if not determinable/defined. */
|
||||
int
|
||||
ftype_length(ftenum_t ftype);
|
||||
|
||||
gboolean
|
||||
ftype_can_slice(enum ftenum ftype);
|
||||
|
||||
gboolean
|
||||
ftype_can_eq(enum ftenum ftype);
|
||||
|
||||
gboolean
|
||||
ftype_can_ne(enum ftenum ftype);
|
||||
|
||||
gboolean
|
||||
ftype_can_gt(enum ftenum ftype);
|
||||
|
||||
gboolean
|
||||
ftype_can_ge(enum ftenum ftype);
|
||||
|
||||
gboolean
|
||||
ftype_can_lt(enum ftenum ftype);
|
||||
|
||||
gboolean
|
||||
ftype_can_le(enum ftenum ftype);
|
||||
|
||||
/* ---------------- FVALUE ----------------- */
|
||||
|
||||
#include "ipv4.h"
|
||||
|
||||
#ifdef HAVE_SYS_TIME_H
|
||||
#include <sys/time.h>
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_WINSOCK_H
|
||||
#include <winsock.h>
|
||||
#endif
|
||||
|
||||
#include "tvbuff.h"
|
||||
|
||||
typedef struct {
|
||||
ftype_t *ftype;
|
||||
union {
|
||||
/* Put a few basic types in here */
|
||||
gpointer pointer;
|
||||
guint32 integer;
|
||||
gdouble floating;
|
||||
gchar *string;
|
||||
GByteArray *bytes;
|
||||
ipv4_addr ipv4;
|
||||
guint8 ipv6[16];
|
||||
struct timeval time;
|
||||
tvbuff_t *tvb;
|
||||
} value;
|
||||
} fvalue_t;
|
||||
|
||||
fvalue_t*
|
||||
fvalue_new(ftenum_t ftype);
|
||||
|
||||
void
|
||||
fvalue_free(fvalue_t *fv);
|
||||
|
||||
typedef void (*LogFunc)(char*,...);
|
||||
|
||||
fvalue_t*
|
||||
fvalue_from_string(ftenum_t ftype, char *s, LogFunc log);
|
||||
|
||||
const char*
|
||||
fvalue_type_name(fvalue_t *fv);
|
||||
|
||||
void
|
||||
fvalue_set(fvalue_t *fv, gpointer value, gboolean already_copied);
|
||||
|
||||
void
|
||||
fvalue_set_integer(fvalue_t *fv, guint32 value);
|
||||
|
||||
void
|
||||
fvalue_set_floating(fvalue_t *fv, gdouble value);
|
||||
|
||||
gpointer
|
||||
fvalue_get(fvalue_t *fv);
|
||||
|
||||
guint32
|
||||
fvalue_get_integer(fvalue_t *fv);
|
||||
|
||||
double
|
||||
fvalue_get_floating(fvalue_t *fv);
|
||||
|
||||
gboolean
|
||||
fvalue_eq(fvalue_t *a, fvalue_t *b);
|
||||
|
||||
gboolean
|
||||
fvalue_ne(fvalue_t *a, fvalue_t *b);
|
||||
|
||||
gboolean
|
||||
fvalue_gt(fvalue_t *a, fvalue_t *b);
|
||||
|
||||
gboolean
|
||||
fvalue_ge(fvalue_t *a, fvalue_t *b);
|
||||
|
||||
gboolean
|
||||
fvalue_lt(fvalue_t *a, fvalue_t *b);
|
||||
|
||||
gboolean
|
||||
fvalue_le(fvalue_t *a, fvalue_t *b);
|
||||
|
||||
guint
|
||||
fvalue_length(fvalue_t *fv);
|
||||
|
||||
fvalue_t*
|
||||
fvalue_slice(fvalue_t *fv, gint start, gint end);
|
||||
|
||||
#endif /* ftypes.h */
|
|
@ -0,0 +1,38 @@
|
|||
/* $Id: gdebug.h,v 1.1 2001/02/01 20:21:16 gram Exp $ */
|
||||
|
||||
#ifndef GDEBUG_H
|
||||
#define GDEBUG_H
|
||||
|
||||
#ifdef __GNUC__
|
||||
|
||||
/* The last "%s" in g_log() is for the empty-string arg that
|
||||
* g_debug() always passes. */
|
||||
#define _g_debug(format, args...) \
|
||||
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
|
||||
"%s():%s +%d: " format "%s", \
|
||||
G_GNUC_PRETTY_FUNCTION, __FILE__, __LINE__, ##args) ;
|
||||
|
||||
/* Always pass a empty-string argument to _g_debug() so that g_debug will always
|
||||
* have at least 2 arguments. If user passes 1 arg to g_debug() (i.e., only
|
||||
* a format string), _g_debug() will still work. */
|
||||
#define g_debug(args...) \
|
||||
_g_debug(args, "")
|
||||
|
||||
|
||||
#else
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
static void
|
||||
g_debug(const char* format, ...)
|
||||
{
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
vfprintf(stderr, format, args);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
#endif /* __GNUC__ */
|
||||
|
||||
#endif /* GDEBUG_H */
|
|
@ -5,7 +5,7 @@
|
|||
*
|
||||
* Gilbert Ramirez <gram@xiexie.org>
|
||||
*
|
||||
* $Id: ipv4.h,v 1.1 2000/09/28 03:52:12 gram Exp $
|
||||
* $Id: ipv4.h,v 1.2 2001/02/01 20:21:16 gram Exp $
|
||||
*
|
||||
* Ethereal - Network traffic analyzer
|
||||
* By Gerald Combs <gerald@zing.org>
|
||||
|
@ -30,6 +30,8 @@
|
|||
#ifndef __IPV4_H__
|
||||
#define __IPV4_H__
|
||||
|
||||
#include <glib.h>
|
||||
|
||||
typedef struct {
|
||||
guint32 addr; /* stored in host order */
|
||||
guint32 nmask; /* stored in host order */
|
||||
|
|
570
epan/proto.c
570
epan/proto.c
File diff suppressed because it is too large
Load Diff
76
epan/proto.h
76
epan/proto.h
|
@ -1,7 +1,7 @@
|
|||
/* proto.h
|
||||
* Definitions for protocol display
|
||||
*
|
||||
* $Id: proto.h,v 1.5 2001/02/01 07:34:30 guy Exp $
|
||||
* $Id: proto.h,v 1.6 2001/02/01 20:21:16 gram Exp $
|
||||
*
|
||||
* Ethereal - Network traffic analyzer
|
||||
* By Gerald Combs <gerald@zing.org>
|
||||
|
@ -43,6 +43,7 @@
|
|||
|
||||
#include "ipv4.h"
|
||||
#include "tvbuff.h"
|
||||
#include "ftypes/ftypes.h"
|
||||
|
||||
/* needs glib.h */
|
||||
typedef GNode proto_tree;
|
||||
|
@ -73,34 +74,6 @@ struct value_string;
|
|||
} \
|
||||
}
|
||||
|
||||
/* field types */
|
||||
enum ftenum {
|
||||
FT_NONE, /* used for protocol labels (thus no field type) */
|
||||
FT_BOOLEAN, /* TRUE and FALSE come from <glib.h> */
|
||||
FT_UINT8,
|
||||
FT_UINT16,
|
||||
FT_UINT24, /* really a UINT32, but displayed as 3 hex-digits if FD_HEX*/
|
||||
FT_UINT32,
|
||||
FT_INT8,
|
||||
FT_INT16,
|
||||
FT_INT24,
|
||||
FT_INT32,
|
||||
FT_DOUBLE,
|
||||
FT_ABSOLUTE_TIME,
|
||||
FT_RELATIVE_TIME,
|
||||
FT_STRING,
|
||||
FT_STRINGZ, /* for use with proto_tree_add_item() */
|
||||
FT_UINT_STRING, /* for use with proto_tree_add_item() */
|
||||
FT_ETHER,
|
||||
FT_BYTES,
|
||||
FT_IPv4,
|
||||
FT_IPv6,
|
||||
FT_IPXNET,
|
||||
FT_TEXT_ONLY, /* non-filterable, used when converting ethereal
|
||||
from old-style proto_tree to new-style proto_tree */
|
||||
NUM_FIELD_TYPES /* last item number plus one */
|
||||
};
|
||||
|
||||
enum {
|
||||
BASE_NONE,
|
||||
BASE_DEC,
|
||||
|
@ -109,8 +82,11 @@ enum {
|
|||
BASE_BIN
|
||||
};
|
||||
|
||||
typedef struct _header_field_info header_field_info;
|
||||
|
||||
/* information describing a header field */
|
||||
typedef struct header_field_info {
|
||||
struct _header_field_info {
|
||||
/* ---------- set by dissector --------- */
|
||||
char *name;
|
||||
char *abbrev;
|
||||
enum ftenum type;
|
||||
|
@ -119,10 +95,12 @@ typedef struct header_field_info {
|
|||
guint32 bitmask;
|
||||
char *blurb; /* Brief description of field. */
|
||||
|
||||
int id; /* assigned by registration function, not programmer */
|
||||
/* ---------- set by proto routines --------- */
|
||||
int id; /* Field ID */
|
||||
int parent; /* parent protocol */
|
||||
int bitshift; /* bits to shift */
|
||||
} header_field_info;
|
||||
header_field_info *same_name; /* Link to next hfinfo with same abbrev*/
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
@ -135,22 +113,13 @@ typedef struct hf_register_info {
|
|||
|
||||
/* Info stored in each proto_item GNode */
|
||||
typedef struct field_info {
|
||||
struct header_field_info *hfinfo;
|
||||
header_field_info *hfinfo;
|
||||
gint start;
|
||||
gint length;
|
||||
gint tree_type; /* ETT_* */
|
||||
char *representation; /* for GUI tree */
|
||||
int visible;
|
||||
union {
|
||||
guint32 numeric;
|
||||
struct timeval time; /* the whole struct, not a pointer */
|
||||
gdouble floating;
|
||||
gchar *string;
|
||||
guint8 *bytes;
|
||||
guint8 ether[6];
|
||||
ipv4_addr ipv4;
|
||||
guint8 ipv6[16];
|
||||
} value;
|
||||
fvalue_t *value;
|
||||
} field_info;
|
||||
|
||||
|
||||
|
@ -213,6 +182,18 @@ proto_tree_add_item_hidden(proto_tree *tree, int hfindex, tvbuff_t *tvb,
|
|||
/* Add a FT_NONE to a proto_tree */
|
||||
#if __GNUC__ == 2
|
||||
proto_item *
|
||||
proto_tree_add_none_format(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
|
||||
gint length, const char *format, ...)
|
||||
__attribute__((format (printf, 6, 7)));
|
||||
#else
|
||||
proto_item *
|
||||
proto_tree_add_none_format(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
|
||||
gint length, const char *format, ...);
|
||||
#endif
|
||||
|
||||
/* Add a FT_PROTOCOL to a proto_tree */
|
||||
#if __GNUC__ == 2
|
||||
proto_item *
|
||||
proto_tree_add_protocol_format(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
|
||||
gint length, const char *format, ...)
|
||||
__attribute__((format (printf, 6, 7)));
|
||||
|
@ -487,7 +468,7 @@ char* proto_registrar_get_name(int n);
|
|||
char* proto_registrar_get_abbrev(int n);
|
||||
|
||||
/* get the header field information based upon a field or protocol id */
|
||||
struct header_field_info* proto_registrar_get_nth(int hfindex);
|
||||
header_field_info* proto_registrar_get_nth(int hfindex);
|
||||
|
||||
/* Returns enum ftenum for item # n */
|
||||
int proto_registrar_get_ftype(int n);
|
||||
|
@ -569,7 +550,10 @@ extern int num_tree_types;
|
|||
#define g_ptr_array_len(a) ((a)->len)
|
||||
#endif
|
||||
|
||||
/* Returns a string representing the field type */
|
||||
const char* proto_registrar_ftype_name(enum ftenum ftype);
|
||||
int
|
||||
hfinfo_bitwidth(header_field_info *hfinfo);
|
||||
|
||||
char*
|
||||
proto_alloc_dfilter_string(field_info *finfo, guint8 *pd);
|
||||
|
||||
#endif /* proto.h */
|
||||
|
|
29
file.c
29
file.c
|
@ -1,7 +1,7 @@
|
|||
/* file.c
|
||||
* File I/O routines
|
||||
*
|
||||
* $Id: file.c,v 1.228 2001/01/28 21:17:26 guy Exp $
|
||||
* $Id: file.c,v 1.229 2001/02/01 20:21:13 gram Exp $
|
||||
*
|
||||
* Ethereal - Network traffic analyzer
|
||||
* By Gerald Combs <gerald@zing.org>
|
||||
|
@ -84,7 +84,7 @@
|
|||
#include "ui_util.h"
|
||||
#include "prefs.h"
|
||||
#include "gtk/proto_draw.h"
|
||||
#include "dfilter.h"
|
||||
#include "dfilter/dfilter.h"
|
||||
#include "conversation.h"
|
||||
#include "globals.h"
|
||||
#include "gtk/colors.h"
|
||||
|
@ -224,7 +224,7 @@ close_cap_file(capture_file *cf, void *w)
|
|||
cf->plist_chunk = NULL;
|
||||
}
|
||||
if (cf->rfcode != NULL) {
|
||||
dfilter_destroy(cf->rfcode);
|
||||
dfilter_free(cf->rfcode);
|
||||
cf->rfcode = NULL;
|
||||
}
|
||||
cf->plist = NULL;
|
||||
|
@ -575,9 +575,8 @@ finish_tail_cap_file(capture_file *cf, int *err)
|
|||
|
||||
typedef struct {
|
||||
color_filter_t *colorf;
|
||||
tvbuff_t *tvb;
|
||||
proto_tree *protocol_tree;
|
||||
const guint8 *pd;
|
||||
frame_data *fdata;
|
||||
} apply_color_filter_args;
|
||||
|
||||
/*
|
||||
|
@ -592,7 +591,7 @@ apply_color_filter(gpointer filter_arg, gpointer argp)
|
|||
apply_color_filter_args *args = argp;
|
||||
|
||||
if (colorf->c_colorfilter != NULL && args->colorf == NULL) {
|
||||
if (dfilter_apply(colorf->c_colorfilter, args->protocol_tree, args->pd, args->fdata->cap_len))
|
||||
if (dfilter_apply(colorf->c_colorfilter, args->tvb, args->protocol_tree))
|
||||
args->colorf = colorf;
|
||||
}
|
||||
}
|
||||
|
@ -647,7 +646,7 @@ add_packet_to_packet_list(frame_data *fdata, capture_file *cf,
|
|||
if (cf->dfcode != NULL) {
|
||||
if (refilter) {
|
||||
if (cf->dfcode != NULL)
|
||||
fdata->flags.passed_dfilter = dfilter_apply(cf->dfcode, protocol_tree, buf, fdata->cap_len) ? 1 : 0;
|
||||
fdata->flags.passed_dfilter = dfilter_apply(cf->dfcode, edt->tvb, protocol_tree) ? 1 : 0;
|
||||
else
|
||||
fdata->flags.passed_dfilter = 1;
|
||||
}
|
||||
|
@ -658,9 +657,8 @@ add_packet_to_packet_list(frame_data *fdata, capture_file *cf,
|
|||
the color filters. */
|
||||
if (fdata->flags.passed_dfilter) {
|
||||
if (filter_list != NULL) {
|
||||
args.tvb = edt->tvb;
|
||||
args.protocol_tree = protocol_tree;
|
||||
args.pd = buf;
|
||||
args.fdata = fdata;
|
||||
g_slist_foreach(filter_list, apply_color_filter, &args);
|
||||
}
|
||||
}
|
||||
|
@ -782,7 +780,7 @@ read_packet(capture_file *cf, int offset)
|
|||
if (cf->rfcode) {
|
||||
protocol_tree = proto_tree_create_root();
|
||||
edt = epan_dissect_new(pseudo_header, buf, fdata, protocol_tree);
|
||||
passed = dfilter_apply(cf->rfcode, protocol_tree, buf, fdata->cap_len);
|
||||
passed = dfilter_apply(cf->rfcode, edt->tvb, protocol_tree);
|
||||
proto_tree_free(protocol_tree);
|
||||
epan_dissect_free(edt);
|
||||
}
|
||||
|
@ -814,7 +812,7 @@ read_packet(capture_file *cf, int offset)
|
|||
int
|
||||
filter_packets(capture_file *cf, gchar *dftext)
|
||||
{
|
||||
dfilter *dfcode;
|
||||
dfilter_t *dfcode;
|
||||
|
||||
if (dftext == NULL) {
|
||||
/* The new filter is an empty filter (i.e., display all packets). */
|
||||
|
@ -823,7 +821,7 @@ filter_packets(capture_file *cf, gchar *dftext)
|
|||
/*
|
||||
* We have a filter; try to compile it.
|
||||
*/
|
||||
if (dfilter_compile(dftext, &dfcode) != 0) {
|
||||
if (!dfilter_compile(dftext, &dfcode)) {
|
||||
/* The attempt failed; report an error. */
|
||||
simple_dialog(ESD_TYPE_CRIT, NULL, dfilter_error_msg);
|
||||
return 0;
|
||||
|
@ -842,7 +840,7 @@ filter_packets(capture_file *cf, gchar *dftext)
|
|||
g_free(cf->dfilter);
|
||||
cf->dfilter = dftext;
|
||||
if (cf->dfcode != NULL)
|
||||
dfilter_destroy(cf->dfcode);
|
||||
dfilter_free(cf->dfcode);
|
||||
cf->dfcode = dfcode;
|
||||
|
||||
/* Now rescan the packet list, applying the new filter, but not
|
||||
|
@ -1372,7 +1370,7 @@ clear_tree_and_hex_views(void)
|
|||
}
|
||||
|
||||
gboolean
|
||||
find_packet(capture_file *cf, dfilter *sfcode)
|
||||
find_packet(capture_file *cf, dfilter_t *sfcode)
|
||||
{
|
||||
frame_data *start_fd;
|
||||
frame_data *fdata;
|
||||
|
@ -1451,7 +1449,7 @@ find_packet(capture_file *cf, dfilter *sfcode)
|
|||
wtap_seek_read(cf->wth, fdata->file_off, &cf->pseudo_header,
|
||||
cf->pd, fdata->cap_len);
|
||||
edt = epan_dissect_new(&cf->pseudo_header, cf->pd, fdata, protocol_tree);
|
||||
frame_matched = dfilter_apply(sfcode, protocol_tree, cf->pd, fdata->cap_len);
|
||||
frame_matched = dfilter_apply(sfcode, edt->tvb, protocol_tree);
|
||||
proto_tree_free(protocol_tree);
|
||||
epan_dissect_free(edt);
|
||||
if (frame_matched) {
|
||||
|
@ -2077,3 +2075,4 @@ copy_binary_file(char *from_filename, char *to_filename)
|
|||
done:
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
|
|
13
file.h
13
file.h
|
@ -1,7 +1,7 @@
|
|||
/* file.h
|
||||
* Definitions for file structures and routines
|
||||
*
|
||||
* $Id: file.h,v 1.79 2001/01/28 23:56:27 guy Exp $
|
||||
* $Id: file.h,v 1.80 2001/02/01 20:21:13 gram Exp $
|
||||
*
|
||||
* Ethereal - Network traffic analyzer
|
||||
* By Gerald Combs <gerald@zing.org>
|
||||
|
@ -31,12 +31,9 @@
|
|||
#endif
|
||||
|
||||
#include "wiretap/wtap.h"
|
||||
|
||||
#include "dfilter.h"
|
||||
#include "dfilter/dfilter.h"
|
||||
#include "print.h"
|
||||
|
||||
#include <errno.h>
|
||||
|
||||
#include <epan.h>
|
||||
|
||||
#ifdef HAVE_LIBZ
|
||||
|
@ -81,10 +78,10 @@ typedef struct _capture_file {
|
|||
gchar *save_file; /* File that user saved capture to */
|
||||
int save_file_fd; /* File descriptor for saved file */
|
||||
wtap *wth; /* Wiretap session */
|
||||
dfilter *rfcode; /* Compiled read filter program */
|
||||
dfilter_t *rfcode; /* Compiled read filter program */
|
||||
gchar *dfilter; /* Display filter string */
|
||||
struct _colfilter *colors; /* Colors for colorizing packet window */
|
||||
dfilter *dfcode; /* Compiled display filter program */
|
||||
dfilter_t *dfcode; /* Compiled display filter program */
|
||||
#ifdef HAVE_LIBPCAP
|
||||
gchar *cfilter; /* Capture filter string */
|
||||
#endif
|
||||
|
@ -126,7 +123,7 @@ void colorize_packets(capture_file *);
|
|||
void redissect_packets(capture_file *cf);
|
||||
int print_packets(capture_file *cf, print_args_t *print_args);
|
||||
void change_time_formats(capture_file *);
|
||||
gboolean find_packet(capture_file *cf, dfilter *sfcode);
|
||||
gboolean find_packet(capture_file *cf, dfilter_t *sfcode);
|
||||
|
||||
typedef enum {
|
||||
FOUND_FRAME, /* found the frame */
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/* color_dlg.c
|
||||
* Definitions for dialog boxes for color filters
|
||||
*
|
||||
* $Id: color_dlg.c,v 1.8 2000/08/24 13:21:29 deniel Exp $
|
||||
* $Id: color_dlg.c,v 1.9 2001/02/01 20:21:21 gram Exp $
|
||||
*
|
||||
* Ethereal - Network traffic analyzer
|
||||
* By Gerald Combs <gerald@zing.org>
|
||||
|
@ -43,7 +43,7 @@
|
|||
#include "colors.h"
|
||||
#include "color_dlg.h"
|
||||
#include "file.h"
|
||||
#include "dfilter.h"
|
||||
#include "dfilter/dfilter.h"
|
||||
#include "simple_dialog.h"
|
||||
#include "dlg_utils.h"
|
||||
#include "ui_util.h"
|
||||
|
@ -884,7 +884,7 @@ edit_color_filter_ok_cb (GtkButton *button,
|
|||
gchar *filter_name;
|
||||
gchar *filter_text;
|
||||
color_filter_t *colorf;
|
||||
dfilter *compiled_filter;
|
||||
dfilter_t *compiled_filter;
|
||||
GtkWidget *color_filters;
|
||||
|
||||
dialog = (GtkWidget *)user_data;
|
||||
|
@ -904,7 +904,7 @@ edit_color_filter_ok_cb (GtkButton *button,
|
|||
return;
|
||||
}
|
||||
|
||||
if(dfilter_compile(filter_text, &compiled_filter) != 0 ){
|
||||
if(!dfilter_compile(filter_text, &compiled_filter)) {
|
||||
simple_dialog(ESD_TYPE_CRIT, NULL, "Filter \"%s\" did not compile correctly.\n"
|
||||
" Please try again. Filter unchanged.\n%s\n", filter_name,
|
||||
dfilter_error_msg);
|
||||
|
@ -927,7 +927,7 @@ edit_color_filter_ok_cb (GtkButton *button,
|
|||
gtk_clist_set_background(GTK_CLIST(color_filters),
|
||||
cfile.colors->row_selected, &new_bg_color);
|
||||
if(colorf->c_colorfilter != NULL)
|
||||
dfilter_destroy(colorf->c_colorfilter);
|
||||
dfilter_free(colorf->c_colorfilter);
|
||||
colorf->c_colorfilter = compiled_filter;
|
||||
/* gtk_clist_set_text frees old text (if any) and allocates new space */
|
||||
gtk_clist_set_text(GTK_CLIST(color_filters),
|
||||
|
|
14
gtk/colors.c
14
gtk/colors.c
|
@ -1,7 +1,7 @@
|
|||
/* colors.c
|
||||
* Definitions for color structures and routines
|
||||
*
|
||||
* $Id: colors.c,v 1.6 2000/09/28 03:16:29 gram Exp $
|
||||
* $Id: colors.c,v 1.7 2001/02/01 20:21:21 gram Exp $
|
||||
*
|
||||
* Ethereal - Network traffic analyzer
|
||||
* By Gerald Combs <gerald@zing.org>
|
||||
|
@ -44,7 +44,7 @@
|
|||
#include "packet.h"
|
||||
#include "colors.h"
|
||||
#include "file.h"
|
||||
#include "dfilter.h"
|
||||
#include "dfilter/dfilter.h"
|
||||
#include "simple_dialog.h"
|
||||
#include "util.h"
|
||||
|
||||
|
@ -120,8 +120,8 @@ colfilter_new(void)
|
|||
default_colors[i].proto);
|
||||
colorf->bg_color = color;
|
||||
|
||||
if (dfilter_compile(default_colors[i].proto,
|
||||
&colorf->c_colorfilter) != 0) {
|
||||
if (!dfilter_compile(default_colors[i].proto,
|
||||
&colorf->c_colorfilter)) {
|
||||
simple_dialog(ESD_TYPE_WARN, NULL,
|
||||
"Cannot compile default color filter %s.\n%s",
|
||||
default_colors[i].proto, dfilter_error_msg);
|
||||
|
@ -158,7 +158,7 @@ delete_color_filter(color_filter_t *colorf)
|
|||
if (colorf->filter_text != NULL)
|
||||
g_free(colorf->filter_text);
|
||||
if (colorf->c_colorfilter != NULL)
|
||||
dfilter_destroy(colorf->c_colorfilter);
|
||||
dfilter_free(colorf->c_colorfilter);
|
||||
filter_list = g_slist_remove(filter_list, colorf);
|
||||
g_free(colorf);
|
||||
}
|
||||
|
@ -177,7 +177,7 @@ read_filters(colfilter *filter)
|
|||
FILE *f;
|
||||
gchar *path;
|
||||
gchar *fname = PF_DIR "/colorfilters";
|
||||
dfilter *temp_dfilter;
|
||||
dfilter_t *temp_dfilter;
|
||||
|
||||
/* decide what file to open (from dfilter code) */
|
||||
|
||||
|
@ -217,7 +217,7 @@ read_filters(colfilter *filter)
|
|||
name, filter_exp, &bg_r, &bg_g, &bg_b, &fg_r, &fg_g, &fg_b) == 8){
|
||||
/* we got a filter */
|
||||
|
||||
if(dfilter_compile(filter_exp, &temp_dfilter) != 0){
|
||||
if(!dfilter_compile(filter_exp, &temp_dfilter)) {
|
||||
simple_dialog(ESD_TYPE_CRIT, NULL,
|
||||
"Could not compile color filter %s from saved filters.\n%s",
|
||||
name, dfilter_error_msg);
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/* colors.h
|
||||
* Definitions for color structures and routines
|
||||
*
|
||||
* $Id: colors.h,v 1.3 2000/08/11 13:33:00 deniel Exp $
|
||||
* $Id: colors.h,v 1.4 2001/02/01 20:21:21 gram Exp $
|
||||
*
|
||||
* Ethereal - Network traffic analyzer
|
||||
* By Gerald Combs <gerald@zing.org>
|
||||
|
@ -26,7 +26,7 @@
|
|||
#define __COLORS_H__
|
||||
|
||||
#include "proto.h"
|
||||
#include "dfilter.h"
|
||||
#include "dfilter/dfilter.h"
|
||||
#include <gtk/gtk.h>
|
||||
|
||||
#define MAXCOLORS 255
|
||||
|
@ -45,7 +45,7 @@ typedef struct _color_filter {
|
|||
gchar *filter_text; /* text of the filter expression */
|
||||
GdkColor bg_color; /* background color for packets that match */
|
||||
GdkColor fg_color; /* foreground color for packets that match */
|
||||
dfilter *c_colorfilter; /* compiled filter expression */
|
||||
dfilter_t *c_colorfilter; /* compiled filter expression */
|
||||
GtkWidget *edit_dialog; /* if filter is being edited, dialog box for it */
|
||||
} color_filter_t;
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
* Copyright 2000, Jeffrey C. Foster<jfoste@woodward.com> and
|
||||
* Guy Harris <guy@alum.mit.edu>
|
||||
*
|
||||
* $Id: dfilter_expr_dlg.c,v 1.5 2001/01/10 23:36:35 guy Exp $
|
||||
* $Id: dfilter_expr_dlg.c,v 1.6 2001/02/01 20:21:21 gram Exp $
|
||||
*
|
||||
* Ethereal - Network traffic analyzer
|
||||
* By Gerald Combs <gerald@zing.org>
|
||||
|
@ -139,7 +139,7 @@ field_select_row_cb(GtkWidget *tree, GList *node, gint column,
|
|||
E_DFILTER_EXPR_ACCEPT_BT_KEY);
|
||||
header_field_info *hfinfo, *cur_hfinfo;
|
||||
guint32 relations;
|
||||
char *value_type;
|
||||
const char *value_type;
|
||||
char value_label_string[1024+1]; /* XXX - should be large enough */
|
||||
|
||||
hfinfo = gtk_ctree_node_get_row_data(GTK_CTREE(tree),
|
||||
|
@ -246,95 +246,7 @@ field_select_row_cb(GtkWidget *tree, GList *node, gint column,
|
|||
* Set the label for the value to indicate what type of value
|
||||
* it is.
|
||||
*/
|
||||
switch (hfinfo->type) {
|
||||
|
||||
case FT_NONE:
|
||||
/*
|
||||
* You can only test for the field's presence; hide
|
||||
* the value stuff.
|
||||
*/
|
||||
value_type = NULL;
|
||||
break;
|
||||
|
||||
case FT_BOOLEAN:
|
||||
value_type = "Boolean";
|
||||
break;
|
||||
|
||||
case FT_UINT8:
|
||||
value_type = "unsigned, byte";
|
||||
break;
|
||||
|
||||
case FT_UINT16:
|
||||
value_type = "unsigned, 2 bytes";
|
||||
break;
|
||||
|
||||
case FT_UINT24:
|
||||
value_type = "unsigned, 3 bytes";
|
||||
break;
|
||||
|
||||
case FT_UINT32:
|
||||
value_type = "unsigned, 4 bytes";
|
||||
break;
|
||||
|
||||
case FT_INT8:
|
||||
value_type = "signed, byte";
|
||||
break;
|
||||
|
||||
case FT_INT16:
|
||||
value_type = "signed, 2 bytes";
|
||||
break;
|
||||
|
||||
case FT_INT24:
|
||||
value_type = "signed, 3 bytes";
|
||||
break;
|
||||
|
||||
case FT_INT32:
|
||||
value_type = "signed, 4 bytes";
|
||||
break;
|
||||
|
||||
case FT_DOUBLE:
|
||||
value_type = "floating point";
|
||||
break;
|
||||
|
||||
case FT_ABSOLUTE_TIME:
|
||||
value_type = "date and time";
|
||||
break;
|
||||
|
||||
case FT_RELATIVE_TIME:
|
||||
value_type = "time";
|
||||
break;
|
||||
|
||||
case FT_STRING:
|
||||
case FT_STRINGZ:
|
||||
case FT_UINT_STRING:
|
||||
value_type = "character string";
|
||||
break;
|
||||
|
||||
case FT_ETHER:
|
||||
value_type = "Ethernet or other MAC address";
|
||||
break;
|
||||
|
||||
case FT_BYTES:
|
||||
value_type = "sequence of bytes";
|
||||
break;
|
||||
|
||||
case FT_IPv4:
|
||||
value_type = "IPv4 address";
|
||||
break;
|
||||
|
||||
case FT_IPv6:
|
||||
value_type = "IPv6 address";
|
||||
break;
|
||||
|
||||
case FT_IPXNET:
|
||||
value_type = "IPX network address";
|
||||
break;
|
||||
|
||||
default:
|
||||
g_assert_not_reached();
|
||||
value_type = NULL;
|
||||
break;
|
||||
}
|
||||
value_type = ftype_pretty_name(hfinfo->type);
|
||||
if (value_type != NULL) {
|
||||
/*
|
||||
* Indicate what type of value it is.
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/* file_dlg.c
|
||||
* Dialog boxes for handling files
|
||||
*
|
||||
* $Id: file_dlg.c,v 1.35 2001/01/21 02:27:24 guy Exp $
|
||||
* $Id: file_dlg.c,v 1.36 2001/02/01 20:21:21 gram Exp $
|
||||
*
|
||||
* Ethereal - Network traffic analyzer
|
||||
* By Gerald Combs <gerald@zing.org>
|
||||
|
@ -149,13 +149,13 @@ static void
|
|||
file_open_ok_cb(GtkWidget *w, GtkFileSelection *fs) {
|
||||
gchar *cf_name, *rfilter, *s;
|
||||
GtkWidget *filter_te, *resolv_cb;
|
||||
dfilter *rfcode = NULL;
|
||||
dfilter_t *rfcode = NULL;
|
||||
int err;
|
||||
|
||||
cf_name = g_strdup(gtk_file_selection_get_filename(GTK_FILE_SELECTION (fs)));
|
||||
filter_te = gtk_object_get_data(GTK_OBJECT(w), E_RFILTER_TE_KEY);
|
||||
rfilter = gtk_entry_get_text(GTK_ENTRY(filter_te));
|
||||
if (dfilter_compile(rfilter, &rfcode) != 0) {
|
||||
if (!dfilter_compile(rfilter, &rfcode)) {
|
||||
simple_dialog(ESD_TYPE_CRIT, NULL, dfilter_error_msg);
|
||||
return;
|
||||
}
|
||||
|
@ -178,7 +178,7 @@ file_open_ok_cb(GtkWidget *w, GtkFileSelection *fs) {
|
|||
dismiss the alert box popped up for the open error,
|
||||
try again. */
|
||||
if (rfcode != NULL)
|
||||
dfilter_destroy(rfcode);
|
||||
dfilter_free(rfcode);
|
||||
g_free(cf_name);
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/* find_dlg.c
|
||||
* Routines for "find frame" window
|
||||
*
|
||||
* $Id: find_dlg.c,v 1.18 2001/01/21 02:27:24 guy Exp $
|
||||
* $Id: find_dlg.c,v 1.19 2001/02/01 20:21:21 gram Exp $
|
||||
*
|
||||
* Ethereal - Network traffic analyzer
|
||||
* By Gerald Combs <gerald@zing.org>
|
||||
|
@ -37,7 +37,7 @@
|
|||
#include <glib.h>
|
||||
|
||||
#include "proto.h"
|
||||
#include "dfilter.h"
|
||||
#include "dfilter/dfilter.h"
|
||||
#include "globals.h"
|
||||
|
||||
#include "ui_util.h"
|
||||
|
@ -187,7 +187,7 @@ find_frame_ok_cb(GtkWidget *ok_bt, gpointer parent_w)
|
|||
{
|
||||
GtkWidget *filter_te, *backward_rb;
|
||||
gchar *filter_text;
|
||||
dfilter *sfcode;
|
||||
dfilter_t *sfcode;
|
||||
|
||||
filter_te = (GtkWidget *) gtk_object_get_data(GTK_OBJECT(parent_w), E_FIND_FILT_KEY);
|
||||
backward_rb = (GtkWidget *) gtk_object_get_data(GTK_OBJECT(parent_w), E_FIND_BACKWARD_KEY);
|
||||
|
@ -197,7 +197,7 @@ find_frame_ok_cb(GtkWidget *ok_bt, gpointer parent_w)
|
|||
/*
|
||||
* Try to compile the filter.
|
||||
*/
|
||||
if (dfilter_compile(filter_text, &sfcode) != 0) {
|
||||
if (!dfilter_compile(filter_text, &sfcode)) {
|
||||
/* The attempt failed; report an error. */
|
||||
simple_dialog(ESD_TYPE_CRIT, NULL, dfilter_error_msg);
|
||||
return;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/* help_dlg.c
|
||||
*
|
||||
* $Id: help_dlg.c,v 1.12 2000/11/22 04:07:00 gram Exp $
|
||||
* $Id: help_dlg.c,v 1.13 2001/02/01 20:21:21 gram Exp $
|
||||
*
|
||||
* Laurent Deniel <deniel@worldnet.fr>
|
||||
*
|
||||
|
@ -378,7 +378,7 @@ static void set_help_text(GtkWidget *w, help_type_t type)
|
|||
nb_lines += 2;
|
||||
} else {
|
||||
|
||||
type_name = proto_registrar_ftype_name(hfinfo->type);
|
||||
type_name = ftype_name(hfinfo->type);
|
||||
snprintf(buffer, BUFF_LEN, "%s%s%s%s(%s)\n",
|
||||
hfinfo->abbrev,
|
||||
&blanks[B_LEN - (maxlen - strlen(hfinfo->abbrev)) - 2],
|
||||
|
|
190
gtk/main.c
190
gtk/main.c
|
@ -1,6 +1,6 @@
|
|||
/* main.c
|
||||
*
|
||||
* $Id: main.c,v 1.176 2001/01/28 23:56:29 guy Exp $
|
||||
* $Id: main.c,v 1.177 2001/02/01 20:21:21 gram Exp $
|
||||
*
|
||||
* Ethereal - Network traffic analyzer
|
||||
* By Gerald Combs <gerald@ethereal.com>
|
||||
|
@ -120,7 +120,7 @@
|
|||
#include "util.h"
|
||||
#include "simple_dialog.h"
|
||||
#include "proto_draw.h"
|
||||
#include "dfilter.h"
|
||||
#include "dfilter/dfilter.h"
|
||||
#include "keys.h"
|
||||
#include "packet_win.h"
|
||||
#include "gtkglobals.h"
|
||||
|
@ -146,7 +146,6 @@ GtkStyle *item_style;
|
|||
/* Specifies the field currently selected in the GUI protocol tree */
|
||||
field_info *finfo_selected = NULL;
|
||||
|
||||
static char* hfinfo_numeric_format(header_field_info *hfinfo);
|
||||
static void create_main_window(gint, gint, gint, e_prefs*);
|
||||
|
||||
/* About Ethereal window */
|
||||
|
@ -171,10 +170,6 @@ match_selected_cb(GtkWidget *w, gpointer data)
|
|||
{
|
||||
char *buf;
|
||||
GtkWidget *filter_te;
|
||||
char *ptr, *format, *stringified;
|
||||
int i, dfilter_len, abbrev_len;
|
||||
guint8 *c;
|
||||
header_field_info *hfinfo;
|
||||
|
||||
filter_te = gtk_object_get_data(GTK_OBJECT(w), E_DFILTER_TE_KEY);
|
||||
|
||||
|
@ -186,123 +181,7 @@ match_selected_cb(GtkWidget *w, gpointer data)
|
|||
return;
|
||||
}
|
||||
|
||||
hfinfo = finfo_selected->hfinfo;
|
||||
g_assert(hfinfo);
|
||||
abbrev_len = strlen(hfinfo->abbrev);
|
||||
|
||||
switch(hfinfo->type) {
|
||||
|
||||
case FT_BOOLEAN:
|
||||
dfilter_len = abbrev_len + 2;
|
||||
buf = g_malloc0(dfilter_len);
|
||||
snprintf(buf, dfilter_len, "%s%s", finfo_selected->value.numeric ? "" : "!",
|
||||
hfinfo->abbrev);
|
||||
break;
|
||||
|
||||
case FT_UINT8:
|
||||
case FT_UINT16:
|
||||
case FT_UINT24:
|
||||
case FT_UINT32:
|
||||
case FT_INT8:
|
||||
case FT_INT16:
|
||||
case FT_INT24:
|
||||
case FT_INT32:
|
||||
dfilter_len = abbrev_len + 20;
|
||||
buf = g_malloc0(dfilter_len);
|
||||
format = hfinfo_numeric_format(hfinfo);
|
||||
snprintf(buf, dfilter_len, format, hfinfo->abbrev, finfo_selected->value.numeric);
|
||||
break;
|
||||
|
||||
case FT_IPv4:
|
||||
dfilter_len = abbrev_len + 4 + 15 + 1;
|
||||
buf = g_malloc0(dfilter_len);
|
||||
snprintf(buf, dfilter_len, "%s == %s", hfinfo->abbrev,
|
||||
ipv4_addr_str(&(finfo_selected->value.ipv4)));
|
||||
break;
|
||||
|
||||
case FT_IPXNET:
|
||||
dfilter_len = abbrev_len + 15;
|
||||
buf = g_malloc0(dfilter_len);
|
||||
snprintf(buf, dfilter_len, "%s == 0x%08x", hfinfo->abbrev,
|
||||
finfo_selected->value.numeric);
|
||||
break;
|
||||
|
||||
case FT_IPv6:
|
||||
stringified = ip6_to_str((struct e_in6_addr*) &(finfo_selected->value.ipv6));
|
||||
dfilter_len = abbrev_len + 4 + strlen(stringified) + 1;
|
||||
buf = g_malloc0(dfilter_len);
|
||||
snprintf(buf, dfilter_len, "%s == %s", hfinfo->abbrev,
|
||||
stringified);
|
||||
break;
|
||||
|
||||
case FT_DOUBLE:
|
||||
dfilter_len = abbrev_len + 30;
|
||||
buf = g_malloc0(dfilter_len);
|
||||
snprintf(buf, dfilter_len, "%s == %f", hfinfo->abbrev,
|
||||
finfo_selected->value.floating);
|
||||
break;
|
||||
|
||||
case FT_ETHER:
|
||||
dfilter_len = abbrev_len + 22;
|
||||
buf = g_malloc0(dfilter_len);
|
||||
snprintf(buf, dfilter_len, "%s == %s",
|
||||
hfinfo->abbrev,
|
||||
ether_to_str(finfo_selected->value.ether));
|
||||
break;
|
||||
#if 0
|
||||
|
||||
case FT_ABSOLUTE_TIME:
|
||||
case FT_RELATIVE_TIME:
|
||||
memcpy(&fi->value.time, va_arg(ap, struct timeval*),
|
||||
sizeof(struct timeval));
|
||||
break;
|
||||
|
||||
case FT_TEXT_ONLY:
|
||||
; /* nothing */
|
||||
break;
|
||||
#endif
|
||||
|
||||
case FT_STRING:
|
||||
dfilter_len = abbrev_len +
|
||||
strlen(finfo_selected->value.string) + 7;
|
||||
buf = g_malloc0(dfilter_len);
|
||||
snprintf(buf, dfilter_len, "%s == \"%s\"",
|
||||
hfinfo->abbrev,
|
||||
finfo_selected->value.string);
|
||||
break;
|
||||
|
||||
case FT_BYTES:
|
||||
dfilter_len = finfo_selected->length*3 - 1;
|
||||
dfilter_len += abbrev_len + 7;
|
||||
buf = g_malloc0(dfilter_len);
|
||||
snprintf(buf, dfilter_len, "%s == %s",
|
||||
hfinfo->abbrev,
|
||||
bytes_to_str_punct(finfo_selected->value.bytes, finfo_selected->length,':'));
|
||||
break;
|
||||
default:
|
||||
c = cfile.pd + finfo_selected->start;
|
||||
buf = g_malloc0(32 + finfo_selected->length * 3);
|
||||
ptr = buf;
|
||||
|
||||
sprintf(ptr, "frame[%d] == ", finfo_selected->start);
|
||||
ptr = buf+strlen(buf);
|
||||
|
||||
if (finfo_selected->length == 1) {
|
||||
sprintf(ptr, "0x%02x", *c++);
|
||||
}
|
||||
else {
|
||||
for (i=0;i<finfo_selected->length; i++) {
|
||||
if (i == 0 ) {
|
||||
sprintf(ptr, "%02x", *c++);
|
||||
}
|
||||
else {
|
||||
sprintf(ptr, ":%02x", *c++);
|
||||
}
|
||||
ptr = buf+strlen(buf);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
buf = proto_alloc_dfilter_string(finfo_selected, cfile.pd);
|
||||
|
||||
/* create a new one and set the display filter entry accordingly */
|
||||
gtk_entry_set_text(GTK_ENTRY(filter_te), buf);
|
||||
|
@ -313,60 +192,6 @@ match_selected_cb(GtkWidget *w, gpointer data)
|
|||
/* Don't g_free(buf) here. filter_packets() will do it the next time it's called */
|
||||
}
|
||||
|
||||
static char*
|
||||
hfinfo_numeric_format(header_field_info *hfinfo)
|
||||
{
|
||||
char *format = NULL;
|
||||
|
||||
/* Pick the proper format string */
|
||||
switch(hfinfo->display) {
|
||||
case BASE_DEC:
|
||||
case BASE_NONE:
|
||||
case BASE_OCT: /* I'm lazy */
|
||||
case BASE_BIN: /* I'm lazy */
|
||||
switch(hfinfo->type) {
|
||||
case FT_UINT8:
|
||||
case FT_UINT16:
|
||||
case FT_UINT24:
|
||||
case FT_UINT32:
|
||||
format = "%s == %u";
|
||||
break;
|
||||
case FT_INT8:
|
||||
case FT_INT16:
|
||||
case FT_INT24:
|
||||
case FT_INT32:
|
||||
format = "%s == %d";
|
||||
break;
|
||||
default:
|
||||
g_assert_not_reached();
|
||||
;
|
||||
}
|
||||
break;
|
||||
case BASE_HEX:
|
||||
switch(hfinfo->type) {
|
||||
case FT_UINT8:
|
||||
format = "%s == 0x%02x";
|
||||
break;
|
||||
case FT_UINT16:
|
||||
format = "%s == 0x%04x";
|
||||
break;
|
||||
case FT_UINT24:
|
||||
format = "%s == 0x%06x";
|
||||
break;
|
||||
case FT_UINT32:
|
||||
format = "%s == 0x%08x";
|
||||
break;
|
||||
default:
|
||||
g_assert_not_reached();
|
||||
;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
g_assert_not_reached();
|
||||
;
|
||||
}
|
||||
return format;
|
||||
}
|
||||
|
||||
|
||||
/* Run the current display filter on the current packet set, and
|
||||
|
@ -585,7 +410,8 @@ tree_view_select_row_cb(GtkCTree *ctree, GList *node, gint column, gpointer user
|
|||
|
||||
set_menus_for_selected_tree_row(TRUE);
|
||||
|
||||
if (finfo->hfinfo && finfo->hfinfo->type != FT_TEXT_ONLY) {
|
||||
/*if (finfo->hfinfo && finfo->hfinfo->type != FT_TEXT_ONLY) {*/
|
||||
if (finfo->hfinfo) {
|
||||
if (finfo->hfinfo->blurb != NULL &&
|
||||
finfo->hfinfo->blurb[0] != '\0') {
|
||||
has_blurb = TRUE;
|
||||
|
@ -872,7 +698,7 @@ main(int argc, char *argv[])
|
|||
#endif
|
||||
gint pl_size = 280, tv_size = 95, bv_size = 75;
|
||||
gchar *rc_file, *cf_name = NULL, *rfilter = NULL;
|
||||
dfilter *rfcode = NULL;
|
||||
dfilter_t *rfcode = NULL;
|
||||
gboolean rfilter_parse_failed = FALSE;
|
||||
e_prefs *prefs;
|
||||
char *bold_font_name;
|
||||
|
@ -1312,7 +1138,7 @@ main(int argc, char *argv[])
|
|||
up on top of us. */
|
||||
if (cf_name) {
|
||||
if (rfilter != NULL) {
|
||||
if (dfilter_compile(rfilter, &rfcode) != 0) {
|
||||
if (!dfilter_compile(rfilter, &rfcode)) {
|
||||
simple_dialog(ESD_TYPE_CRIT, NULL, dfilter_error_msg);
|
||||
rfilter_parse_failed = TRUE;
|
||||
}
|
||||
|
@ -1345,7 +1171,7 @@ main(int argc, char *argv[])
|
|||
if (s != NULL)
|
||||
last_open_dir = s;
|
||||
} else {
|
||||
dfilter_destroy(rfcode);
|
||||
dfilter_free(rfcode);
|
||||
cfile.rfcode = NULL;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
*
|
||||
* Copyright 2000, Jeffrey C. Foster <jfoste@woodward.com>
|
||||
*
|
||||
* $Id: packet_win.c,v 1.16 2000/10/06 10:11:40 gram Exp $
|
||||
* $Id: packet_win.c,v 1.17 2001/02/01 20:21:22 gram Exp $
|
||||
*
|
||||
* Ethereal - Network traffic analyzer
|
||||
* By Gerald Combs <gerald@zing.org>
|
||||
|
@ -64,7 +64,6 @@
|
|||
#include "simple_dialog.h"
|
||||
#include "ui_util.h"
|
||||
#include "proto_draw.h"
|
||||
#include "dfilter.h"
|
||||
#include "keys.h"
|
||||
#include "gtkglobals.h"
|
||||
#include "plugins.h"
|
||||
|
|
22
packet-x11.c
22
packet-x11.c
|
@ -2,7 +2,7 @@
|
|||
* Routines for X11 dissection
|
||||
* Copyright 2000, Christophe Tronche <ch.tronche@computer.org>
|
||||
*
|
||||
* $Id: packet-x11.c,v 1.16 2001/01/22 08:03:46 guy Exp $
|
||||
* $Id: packet-x11.c,v 1.17 2001/02/01 20:21:13 gram Exp $
|
||||
*
|
||||
* Ethereal - Network traffic analyzer
|
||||
* By Gerald Combs <gerald@unicom.net>
|
||||
|
@ -692,7 +692,7 @@ static void atom(tvbuff_t *tvb, proto_tree *t, int hf)
|
|||
else if (v)
|
||||
interpretation = "Not a predefined atom";
|
||||
else {
|
||||
struct header_field_info *hfi = proto_registrar_get_nth(hf);
|
||||
header_field_info *hfi = proto_registrar_get_nth(hf);
|
||||
if (hfi -> strings)
|
||||
interpretation = match_strval(v, cVALS(hfi -> strings));
|
||||
}
|
||||
|
@ -793,7 +793,7 @@ static void listOfArc(tvbuff_t *tvb, int hf, int length)
|
|||
gint16 angle1 = VALUE16(tvb, cur_offset + 8);
|
||||
gint16 angle2 = VALUE16(tvb, cur_offset + 10);
|
||||
|
||||
proto_tree *ttt = proto_tree_add_protocol_format(tt, hf_x11_arc, tvb, cur_offset, 12,
|
||||
proto_tree *ttt = proto_tree_add_none_format(tt, hf_x11_arc, tvb, cur_offset, 12,
|
||||
"arc: %dx%d+%d+%d, angle %d -> %d (%f° -> %f°)",
|
||||
width, height, x, y, angle1, angle2,
|
||||
angle1 / 64.0, angle2 / 64.0);
|
||||
|
@ -868,7 +868,7 @@ static void listOfColorItem(tvbuff_t *tvb, int hf, int length)
|
|||
if (do_red_green_blue & 0x2) { bp += sprintf(bp, "%sgreen = %d", sep, green); sep = ", "; }
|
||||
if (do_red_green_blue & 0x4) bp += sprintf(bp, "%sblue = %d", sep, blue);
|
||||
|
||||
ttt = proto_tree_add_protocol_format(tt, hf_x11_coloritem, tvb, cur_offset, 12, "%s", buffer);
|
||||
ttt = proto_tree_add_none_format(tt, hf_x11_coloritem, tvb, cur_offset, 12, "%s", buffer);
|
||||
proto_tree_add_item(ttt, hf_x11_coloritem_pixel, tvb, cur_offset, 4, little_endian); cur_offset += 4;
|
||||
proto_tree_add_item(ttt, hf_x11_coloritem_red, tvb, cur_offset, 2, little_endian); cur_offset += 2;
|
||||
proto_tree_add_item(ttt, hf_x11_coloritem_green, tvb, cur_offset, 2, little_endian); cur_offset += 2;
|
||||
|
@ -945,7 +945,7 @@ static void listOfKeysyms(tvbuff_t *tvb, int hf, int hf_item, int keycode_count,
|
|||
bp += sprintf(bp, " %s", keysymString(VALUE32(tvb, cur_offset + i * 4)));
|
||||
}
|
||||
*bp = '\0';
|
||||
ttt = proto_tree_add_protocol_format(tt, hf_item, tvb, cur_offset, keysyms_per_keycode * 4,
|
||||
ttt = proto_tree_add_none_format(tt, hf_item, tvb, cur_offset, keysyms_per_keycode * 4,
|
||||
"%s", buffer);
|
||||
for(i = keysyms_per_keycode; i; i--) {
|
||||
guint32 v = VALUE32(tvb, cur_offset);
|
||||
|
@ -970,7 +970,7 @@ static void listOfPoint(tvbuff_t *tvb, int hf, int length)
|
|||
x = VALUE16(tvb, cur_offset);
|
||||
y = VALUE16(tvb, cur_offset + 2);
|
||||
|
||||
ttt = proto_tree_add_protocol_format(tt, hf_x11_point, tvb, cur_offset, 4, "point: (%d,%d)", x, y);
|
||||
ttt = proto_tree_add_none_format(tt, hf_x11_point, tvb, cur_offset, 4, "point: (%d,%d)", x, y);
|
||||
proto_tree_add_int(ttt, hf_x11_point_x, tvb, cur_offset, 2, x); cur_offset += 2;
|
||||
proto_tree_add_int(ttt, hf_x11_point_y, tvb, cur_offset, 2, y); cur_offset += 2;
|
||||
}
|
||||
|
@ -993,7 +993,7 @@ static void listOfRectangle(tvbuff_t *tvb, int hf, int length)
|
|||
width = VALUE16(tvb, cur_offset + 4);
|
||||
height = VALUE16(tvb, cur_offset + 6);
|
||||
|
||||
ttt = proto_tree_add_protocol_format(tt, hf_x11_rectangle, tvb, cur_offset, 8,
|
||||
ttt = proto_tree_add_none_format(tt, hf_x11_rectangle, tvb, cur_offset, 8,
|
||||
"rectangle: %dx%d+%d+%d", width, height, x, y);
|
||||
proto_tree_add_int(ttt, hf_x11_rectangle_x, tvb, cur_offset, 2, x); cur_offset += 2;
|
||||
proto_tree_add_int(ttt, hf_x11_rectangle_y, tvb, cur_offset, 2, y); cur_offset += 2;
|
||||
|
@ -1018,7 +1018,7 @@ static void listOfSegment(tvbuff_t *tvb, int hf, int length)
|
|||
x2 = VALUE16(tvb, cur_offset + 4);
|
||||
y2 = VALUE16(tvb, cur_offset + 6);
|
||||
|
||||
ttt = proto_tree_add_protocol_format(tt, hf_x11_segment, tvb, cur_offset, 8,
|
||||
ttt = proto_tree_add_none_format(tt, hf_x11_segment, tvb, cur_offset, 8,
|
||||
"segment: (%d,%d)-(%d,%d)", x1, y1, x2, y2);
|
||||
proto_tree_add_item(ttt, hf_x11_segment_x1, tvb, cur_offset, 2, little_endian); cur_offset += 2;
|
||||
proto_tree_add_item(ttt, hf_x11_segment_y1, tvb, cur_offset, 2, little_endian); cur_offset += 2;
|
||||
|
@ -1170,7 +1170,7 @@ static void listOfTextItem(tvbuff_t *tvb, int hf, int sizeIs16)
|
|||
allocated = l + 1;
|
||||
}
|
||||
stringCopy(s, tvb_get_ptr(tvb, cur_offset + 2, l), l);
|
||||
ttt = proto_tree_add_protocol_format(tt, hf_x11_textitem_string, tvb, cur_offset, l + 2,
|
||||
ttt = proto_tree_add_none_format(tt, hf_x11_textitem_string, tvb, cur_offset, l + 2,
|
||||
"textitem (string): delta = %d, \"%s\"",
|
||||
delta, s);
|
||||
proto_tree_add_item(ttt, hf_x11_textitem_string_delta, tvb, cur_offset + 1, 1, little_endian);
|
||||
|
@ -1191,7 +1191,7 @@ static void listOfTextItem(tvbuff_t *tvb, int hf, int sizeIs16)
|
|||
static guint32 field8(tvbuff_t *tvb, int hf)
|
||||
{
|
||||
guint32 v = VALUE8(tvb, cur_offset);
|
||||
struct header_field_info *hfi = proto_registrar_get_nth(hf);
|
||||
header_field_info *hfi = proto_registrar_get_nth(hf);
|
||||
gchar *enumValue = NULL;
|
||||
gchar *nameAsChar = hfi -> name;
|
||||
|
||||
|
@ -1216,7 +1216,7 @@ static guint32 field16(tvbuff_t *tvb, int hf)
|
|||
static guint32 field32(tvbuff_t *tvb, int hf)
|
||||
{
|
||||
guint32 v = VALUE32(tvb, cur_offset);
|
||||
struct header_field_info *hfi = proto_registrar_get_nth(hf);
|
||||
header_field_info *hfi = proto_registrar_get_nth(hf);
|
||||
gchar *enumValue = NULL;
|
||||
gchar *nameAsChar = hfi -> name;
|
||||
|
||||
|
|
10
tethereal.c
10
tethereal.c
|
@ -1,6 +1,6 @@
|
|||
/* tethereal.c
|
||||
*
|
||||
* $Id: tethereal.c,v 1.63 2001/01/29 00:09:38 guy Exp $
|
||||
* $Id: tethereal.c,v 1.64 2001/02/01 20:21:13 gram Exp $
|
||||
*
|
||||
* Ethereal - Network traffic analyzer
|
||||
* By Gerald Combs <gerald@zing.org>
|
||||
|
@ -192,7 +192,7 @@ main(int argc, char *argv[])
|
|||
#endif
|
||||
int out_file_type = WTAP_FILE_PCAP;
|
||||
gchar *cf_name = NULL, *rfilter = NULL;
|
||||
dfilter *rfcode = NULL;
|
||||
dfilter_t *rfcode = NULL;
|
||||
e_prefs *prefs;
|
||||
|
||||
/* Register all dissectors; we must do this before checking for the
|
||||
|
@ -470,7 +470,7 @@ main(int argc, char *argv[])
|
|||
cfile.snap = MIN_PACKET_SIZE;
|
||||
|
||||
if (rfilter != NULL) {
|
||||
if (dfilter_compile(rfilter, &rfcode) != 0) {
|
||||
if (!dfilter_compile(rfilter, &rfcode)) {
|
||||
fprintf(stderr, "tethereal: %s\n", dfilter_error_msg);
|
||||
epan_cleanup();
|
||||
exit(2);
|
||||
|
@ -908,7 +908,7 @@ wtap_dispatch_cb_write(u_char *user, const struct wtap_pkthdr *phdr, int offset,
|
|||
fill_in_fdata(&fdata, cf, phdr, pseudo_header, offset);
|
||||
protocol_tree = proto_tree_create_root();
|
||||
edt = epan_dissect_new(pseudo_header, buf, &fdata, protocol_tree);
|
||||
passed = dfilter_apply(cf->rfcode, protocol_tree, buf, fdata.cap_len);
|
||||
passed = dfilter_apply_edt(cf->rfcode, edt);
|
||||
} else {
|
||||
protocol_tree = NULL;
|
||||
passed = TRUE;
|
||||
|
@ -952,7 +952,7 @@ wtap_dispatch_cb_print(u_char *user, const struct wtap_pkthdr *phdr, int offset,
|
|||
protocol_tree = NULL;
|
||||
edt = epan_dissect_new(pseudo_header, buf, &fdata, protocol_tree);
|
||||
if (cf->rfcode)
|
||||
passed = dfilter_apply(cf->rfcode, protocol_tree, buf, fdata.cap_len);
|
||||
passed = dfilter_apply_edt(cf->rfcode, edt);
|
||||
if (passed) {
|
||||
/* The packet passed the read filter. */
|
||||
if (verbose) {
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
.cvsignore
|
||||
Makefile
|
||||
Makefile.in
|
|
@ -0,0 +1 @@
|
|||
SUBDIRS = lemon
|
|
@ -0,0 +1,8 @@
|
|||
.cvsignore
|
||||
.deps
|
||||
Makefile
|
||||
lemon
|
||||
lemon.exe
|
||||
lemon.obj
|
||||
.libs
|
||||
Makefile.in
|
|
@ -0,0 +1,44 @@
|
|||
# Makefile.am
|
||||
#
|
||||
# $Id: Makefile.am,v 1.1 2001/02/01 20:21:24 gram Exp $
|
||||
#
|
||||
# Ethereal - Network traffic analyzer
|
||||
# By Gerald Combs <gerald@zing.org>
|
||||
# Copyright 2001 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.
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
|
||||
# We produce an archive library. In the future, when libethereal is a
|
||||
# shared library, this will be linked into libethereal. While libethereal
|
||||
# is an archive library, any executable linking against libethereal will
|
||||
# also need to link against libftypes.
|
||||
|
||||
noinst_PROGRAMS = lemon
|
||||
|
||||
CLEANFILES = \
|
||||
*~
|
||||
|
||||
lemon_SOURCES = \
|
||||
lemon.c
|
||||
|
||||
# Makefile.nmake
|
||||
EXTRA_DIST = \
|
||||
lemon.html \
|
||||
lemonflex-head.inc \
|
||||
lemonflex-tail.inc \
|
||||
lempar.c \
|
||||
README
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
$Id: README,v 1.1 2001/02/01 20:21:25 gram Exp $
|
||||
|
||||
The Lemon Parser Generator's home page is:
|
||||
|
||||
http://www.hwaci.com/sw/lemon/index.html
|
||||
|
||||
The file in this directory, lemon.html, was obtained from:
|
||||
|
||||
http://www.hwaci.com/sw/lemon/lemon.html
|
||||
|
||||
lemon.c has been modified to include the t= and d= command-line
|
||||
arguments. The changes were submitted to the Lemon maintainer,
|
||||
but have not appeared in the official Lemon distribution.
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,861 @@
|
|||
<html>
|
||||
<head>
|
||||
<title>The Lemon Parser Generator</title>
|
||||
</head>
|
||||
<body bgcolor=white>
|
||||
<h1 align=center>The Lemon Parser Generator</h1>
|
||||
|
||||
<p>Lemon is an LALR(1) parser generator for C or C++.
|
||||
It does the same job as ``bison'' and ``yacc''.
|
||||
But lemon is not another bison or yacc clone. It
|
||||
uses a different grammar syntax which is designed to
|
||||
reduce the number of coding errors. Lemon also uses a more
|
||||
sophisticated parsing engine that is faster than yacc and
|
||||
bison and which is both reentrant and thread-safe.
|
||||
Furthermore, Lemon implements features that can be used
|
||||
to eliminate resource leaks, making is suitable for use
|
||||
in long-running programs such as graphical user interfaces
|
||||
or embedded controllers.</p>
|
||||
|
||||
<p>This document is an introduction to the Lemon
|
||||
parser generator.</p>
|
||||
|
||||
<h2>Theory of Operation</h2>
|
||||
|
||||
<p>The main goal of Lemon is to translate a context free grammar (CFG)
|
||||
for a particular language into C code that implements a parser for
|
||||
that language.
|
||||
The program has two inputs:
|
||||
<ul>
|
||||
<li>The grammar specification.
|
||||
<li>A parser template file.
|
||||
</ul>
|
||||
Typically, only the grammar specification is supplied by the programmer.
|
||||
Lemon comes with a default parser template which works fine for most
|
||||
applications. But the user is free to substitute a different parser
|
||||
template if desired.</p>
|
||||
|
||||
<p>Depending on command-line options, Lemon will generate between
|
||||
one and three files of outputs.
|
||||
<ul>
|
||||
<li>C code to implement the parser.
|
||||
<li>A header file defining an integer ID for each terminal symbol.
|
||||
<li>An information file that describes the states of the generated parser
|
||||
automaton.
|
||||
</ul>
|
||||
By default, all three of these output files are generated.
|
||||
The header file is suppressed if the ``-m'' command-line option is
|
||||
used and the report file is omitted when ``-q'' is selected.</p>
|
||||
|
||||
<p>The grammar specification file uses a ``.y'' suffix, by convention.
|
||||
In the examples used in this document, we'll assume the name of the
|
||||
grammar file is ``gram.y''. A typical use of Lemon would be the
|
||||
following command:
|
||||
<pre>
|
||||
lemon gram.y
|
||||
</pre>
|
||||
This command will generate three output files named ``gram.c'',
|
||||
``gram.h'' and ``gram.out''.
|
||||
The first is C code to implement the parser. The second
|
||||
is the header file that defines numerical values for all
|
||||
terminal symbols, and the last is the report that explains
|
||||
the states used by the parser automaton.</p>
|
||||
|
||||
<h3>Command Line Options</h3>
|
||||
|
||||
<p>The behavior of Lemon can be modified using command-line options.
|
||||
You can obtain a list of the available command-line options together
|
||||
with a brief explanation of what each does by typing
|
||||
<pre>
|
||||
lemon -?
|
||||
</pre>
|
||||
As of this writing, the following command-line options are supported:
|
||||
<ul>
|
||||
<li><tt>-b</tt>
|
||||
<li><tt>-c</tt>
|
||||
<li><tt>-g</tt>
|
||||
<li><tt>-m</tt>
|
||||
<li><tt>-q</tt>
|
||||
<li><tt>-s</tt>
|
||||
<li><tt>-x</tt>
|
||||
</ul>
|
||||
The ``-b'' option reduces the amount of text in the report file by
|
||||
printing only the basis of each parser state, rather than the full
|
||||
configuration.
|
||||
The ``-c'' option suppresses action table compression. Using -c
|
||||
will make the parser a little larger and slower but it will detect
|
||||
syntax errors sooner.
|
||||
The ``-g'' option causes no output files to be generated at all.
|
||||
Instead, the input grammar file is printed on standard output but
|
||||
with all comments, actions and other extraneous text deleted. This
|
||||
is a useful way to get a quick summary of a grammar.
|
||||
The ``-m'' option causes the output C source file to be compatible
|
||||
with the ``makeheaders'' program.
|
||||
Makeheaders is a program that automatically generates header files
|
||||
from C source code. When the ``-m'' option is used, the header
|
||||
file is not output since the makeheaders program will take care
|
||||
of generated all header files automatically.
|
||||
The ``-q'' option suppresses the report file.
|
||||
Using ``-s'' causes a brief summary of parser statistics to be
|
||||
printed. Like this:
|
||||
<pre>
|
||||
Parser statistics: 74 terminals, 70 nonterminals, 179 rules
|
||||
340 states, 2026 parser table entries, 0 conflicts
|
||||
</pre>
|
||||
Finally, the ``-x'' option causes Lemon to print its version number
|
||||
and copyright information
|
||||
and then stop without attempting to read the grammar or generate a parser.</p>
|
||||
|
||||
<h3>The Parser Interface</h3>
|
||||
|
||||
<p>Lemon doesn't generate a complete, working program. It only generates
|
||||
a few subroutines that implement a parser. This section describes
|
||||
the interface to those subroutines. It is up to the programmer to
|
||||
call these subroutines in an appropriate way in order to produce a
|
||||
complete system.</p>
|
||||
|
||||
<p>Before a program begins using a Lemon-generated parser, the program
|
||||
must first create the parser.
|
||||
A new parser is created as follows:
|
||||
<pre>
|
||||
void *pParser = ParseAlloc( malloc );
|
||||
</pre>
|
||||
The ParseAlloc() routine allocates and initializes a new parser and
|
||||
returns a pointer to it.
|
||||
The actual data structure used to represent a parser is opaque --
|
||||
its internal structure is not visible or usable by the calling routine.
|
||||
For this reason, the ParseAlloc() routine returns a pointer to void
|
||||
rather than a pointer to some particular structure.
|
||||
The sole argument to the ParseAlloc() routine is a pointer to the
|
||||
subroutine used to allocate memory. Typically this means ``malloc()''.</p>
|
||||
|
||||
<p>After a program is finished using a parser, it can reclaim all
|
||||
memory allocated by that parser by calling
|
||||
<pre>
|
||||
ParseFree(pParser, free);
|
||||
</pre>
|
||||
The first argument is the same pointer returned by ParseAlloc(). The
|
||||
second argument is a pointer to the function used to release bulk
|
||||
memory back to the system.</p>
|
||||
|
||||
<p>After a parser has been allocated using ParseAlloc(), the programmer
|
||||
must supply the parser with a sequence of tokens (terminal symbols) to
|
||||
be parsed. This is accomplished by calling the following function
|
||||
once for each token:
|
||||
<pre>
|
||||
Parse(pParser, hTokenID, sTokenData, pArg);
|
||||
</pre>
|
||||
The first argument to the Parse() routine is the pointer returned by
|
||||
ParseAlloc().
|
||||
The second argument is a small positive integer that tells the parse the
|
||||
type of the next token in the data stream.
|
||||
There is one token type for each terminal symbol in the grammar.
|
||||
The gram.h file generated by Lemon contains #define statements that
|
||||
map symbolic terminal symbol names into appropriate integer values.
|
||||
(A value of 0 for the second argument is a special flag to the
|
||||
parser to indicate that the end of input has been reached.)
|
||||
The third argument is the value of the given token. By default,
|
||||
the type of the third argument is integer, but the grammar will
|
||||
usually redefine this type to be some kind of structure.
|
||||
Typically the second argument will be a broad category of tokens
|
||||
such as ``identifier'' or ``number'' and the third argument will
|
||||
be the name of the identifier or the value of the number.</p>
|
||||
|
||||
<p>The Parse() function may have either three or four arguments,
|
||||
depending on the grammar. If the grammar specification file request
|
||||
it, the Parse() function will have a fourth parameter that can be
|
||||
of any type chosen by the programmer. The parser doesn't do anything
|
||||
with this argument except to pass it through to action routines.
|
||||
This is a convenient mechanism for passing state information down
|
||||
to the action routines without having to use global variables.</p>
|
||||
|
||||
<p>A typical use of a Lemon parser might look something like the
|
||||
following:
|
||||
<pre>
|
||||
01 ParseTree *ParseFile(const char *zFilename){
|
||||
02 Tokenizer *pTokenizer;
|
||||
03 void *pParser;
|
||||
04 Token sToken;
|
||||
05 int hTokenId;
|
||||
06 ParserState sState;
|
||||
07
|
||||
08 pTokenizer = TokenizerCreate(zFilename);
|
||||
09 pParser = ParseAlloc( malloc );
|
||||
10 InitParserState(&sState);
|
||||
11 while( GetNextToken(pTokenizer, &hTokenId, &sToken) ){
|
||||
12 Parse(pParser, hTokenId, sToken, &sState);
|
||||
13 }
|
||||
14 Parse(pParser, 0, sToken, &sState);
|
||||
15 ParseFree(pParser, free );
|
||||
16 TokenizerFree(pTokenizer);
|
||||
17 return sState.treeRoot;
|
||||
18 }
|
||||
</pre>
|
||||
This example shows a user-written routine that parses a file of
|
||||
text and returns a pointer to the parse tree.
|
||||
(We've omitted all error-handling from this example to keep it
|
||||
simple.)
|
||||
We assume the existence of some kind of tokenizer which is created
|
||||
using TokenizerCreate() on line 8 and deleted by TokenizerFree()
|
||||
on line 16. The GetNextToken() function on line 11 retrieves the
|
||||
next token from the input file and puts its type in the
|
||||
integer variable hTokenId. The sToken variable is assumed to be
|
||||
some kind of structure that contains details about each token,
|
||||
such as its complete text, what line it occurs on, etc. </p>
|
||||
|
||||
<p>This example also assumes the existence of structure of type
|
||||
ParserState that holds state information about a particular parse.
|
||||
An instance of such a structure is created on line 6 and initialized
|
||||
on line 10. A pointer to this structure is passed into the Parse()
|
||||
routine as the optional 4th argument.
|
||||
The action routine specified by the grammar for the parser can use
|
||||
the ParserState structure to hold whatever information is useful and
|
||||
appropriate. In the example, we note that the treeRoot field of
|
||||
the ParserState structure is left pointing to the root of the parse
|
||||
tree.</p>
|
||||
|
||||
<p>The core of this example as it relates to Lemon is as follows:
|
||||
<pre>
|
||||
ParseFile(){
|
||||
pParser = ParseAlloc( malloc );
|
||||
while( GetNextToken(pTokenizer,&hTokenId, &sToken) ){
|
||||
Parse(pParser, hTokenId, sToken);
|
||||
}
|
||||
Parse(pParser, 0, sToken);
|
||||
ParseFree(pParser, free );
|
||||
}
|
||||
</pre>
|
||||
Basically, what a program has to do to use a Lemon-generated parser
|
||||
is first create the parser, then send it lots of tokens obtained by
|
||||
tokenizing an input source. When the end of input is reached, the
|
||||
Parse() routine should be called one last time with a token type
|
||||
of 0. This step is necessary to inform the parser that the end of
|
||||
input has been reached. Finally, we reclaim memory used by the
|
||||
parser by calling ParseFree().</p>
|
||||
|
||||
<p>There is one other interface routine that should be mentioned
|
||||
before we move on.
|
||||
The ParseTrace() function can be used to generate debugging output
|
||||
from the parser. A prototype for this routine is as follows:
|
||||
<pre>
|
||||
ParseTrace(FILE *stream, char *zPrefix);
|
||||
</pre>
|
||||
After this routine is called, a short (one-line) message is written
|
||||
to the designated output stream every time the parser changes states
|
||||
or calls an action routine. Each such message is prefaced using
|
||||
the text given by zPrefix. This debugging output can be turned off
|
||||
by calling ParseTrace() again with a first argument of NULL (0).</p>
|
||||
|
||||
<h3>Differences With YACC and BISON</h3>
|
||||
|
||||
<p>Programmers who have previously used the yacc or bison parser
|
||||
generator will notice several important differences between yacc and/or
|
||||
bison and Lemon.
|
||||
<ul>
|
||||
<li>In yacc and bison, the parser calls the tokenizer. In Lemon,
|
||||
the tokenizer calls the parser.
|
||||
<li>Lemon uses no global variables. Yacc and bison use global variables
|
||||
to pass information between the tokenizer and parser.
|
||||
<li>Lemon allows multiple parsers to be running simultaneously. Yacc
|
||||
and bison do not.
|
||||
</ul>
|
||||
These differences may cause some initial confusion for programmers
|
||||
with prior yacc and bison experience.
|
||||
But after years of experience using Lemon, I firmly
|
||||
believe that the Lemon way of doing things is better.</p>
|
||||
|
||||
<h2>Input File Syntax</h2>
|
||||
|
||||
<p>The main purpose of the grammar specification file for Lemon is
|
||||
to define the grammar for the parser. But the input file also
|
||||
specifies additional information Lemon requires to do its job.
|
||||
Most of the work in using Lemon is in writing an appropriate
|
||||
grammar file.</p>
|
||||
|
||||
<p>The grammar file for lemon is, for the most part, free format.
|
||||
It does not have sections or divisions like yacc or bison. Any
|
||||
declaration can occur at any point in the file.
|
||||
Lemon ignores whitespace (except where it is needed to separate
|
||||
tokens) and it honors the same commenting conventions as C and C++.</p>
|
||||
|
||||
<h3>Terminals and Nonterminals</h3>
|
||||
|
||||
<p>A terminal symbol (token) is any string of alphanumeric
|
||||
and underscore characters
|
||||
that begins with an upper case letter.
|
||||
A terminal can contain lower class letters after the first character,
|
||||
but the usual convention is to make terminals all upper case.
|
||||
A nonterminal, on the other hand, is any string of alphanumeric
|
||||
and underscore characters than begins with a lower case letter.
|
||||
Again, the usual convention is to make nonterminals use all lower
|
||||
case letters.</p>
|
||||
|
||||
<p>In Lemon, terminal and nonterminal symbols do not need to
|
||||
be declared or identified in a separate section of the grammar file.
|
||||
Lemon is able to generate a list of all terminals and nonterminals
|
||||
by examining the grammar rules, and it can always distinguish a
|
||||
terminal from a nonterminal by checking the case of the first
|
||||
character of the name.</p>
|
||||
|
||||
<p>Yacc and bison allow terminal symbols to have either alphanumeric
|
||||
names or to be individual characters included in single quotes, like
|
||||
this: ')' or '$'. Lemon does not allow this alternative form for
|
||||
terminal symbols. With Lemon, all symbols, terminals and nonterminals,
|
||||
must have alphanumeric names.</p>
|
||||
|
||||
<h3>Grammar Rules</h3>
|
||||
|
||||
<p>The main component of a Lemon grammar file is a sequence of grammar
|
||||
rules.
|
||||
Each grammar rule consists of a nonterminal symbol followed by
|
||||
the special symbol ``::='' and then a list of terminals and/or nonterminals.
|
||||
The rule is terminated by a period.
|
||||
The list of terminals and nonterminals on the right-hand side of the
|
||||
rule can be empty.
|
||||
Rules can occur in any order, except that the left-hand side of the
|
||||
first rule is assumed to be the start symbol for the grammar (unless
|
||||
specified otherwise using the <tt>%start</tt> directive described below.)
|
||||
A typical sequence of grammar rules might look something like this:
|
||||
<pre>
|
||||
expr ::= expr PLUS expr.
|
||||
expr ::= expr TIMES expr.
|
||||
expr ::= LPAREN expr RPAREN.
|
||||
expr ::= VALUE.
|
||||
</pre>
|
||||
</p>
|
||||
|
||||
<p>There is one non-terminal in this example, ``expr'', and five
|
||||
terminal symbols or tokens: ``PLUS'', ``TIMES'', ``LPAREN'',
|
||||
``RPAREN'' and ``VALUE''.</p>
|
||||
|
||||
<p>Like yacc and bison, Lemon allows the grammar to specify a block
|
||||
of C code that will be executed whenever a grammar rule is reduced
|
||||
by the parser.
|
||||
In Lemon, this action is specified by putting the C code (contained
|
||||
within curly braces <tt>{...}</tt>) immediately after the
|
||||
period that closes the rule.
|
||||
For example:
|
||||
<pre>
|
||||
expr ::= expr PLUS expr. { printf("Doing an addition...\n"); }
|
||||
</pre>
|
||||
</p>
|
||||
|
||||
<p>In order to be useful, grammar actions must normally be linked to
|
||||
their associated grammar rules.
|
||||
In yacc and bison, this is accomplished by embedding a ``$$'' in the
|
||||
action to stand for the value of the left-hand side of the rule and
|
||||
symbols ``$1'', ``$2'', and so forth to stand for the value of
|
||||
the terminal or nonterminal at position 1, 2 and so forth on the
|
||||
right-hand side of the rule.
|
||||
This idea is very powerful, but it is also very error-prone. The
|
||||
single most common source of errors in a yacc or bison grammar is
|
||||
to miscount the number of symbols on the right-hand side of a grammar
|
||||
rule and say ``$7'' when you really mean ``$8''.</p>
|
||||
|
||||
<p>Lemon avoids the need to count grammar symbols by assigning symbolic
|
||||
names to each symbol in a grammar rule and then using those symbolic
|
||||
names in the action.
|
||||
In yacc or bison, one would write this:
|
||||
<pre>
|
||||
expr -> expr PLUS expr { $$ = $1 + $3; };
|
||||
</pre>
|
||||
But in Lemon, the same rule becomes the following:
|
||||
<pre>
|
||||
expr(A) ::= expr(B) PLUS expr(C). { A = B+C; }
|
||||
</pre>
|
||||
In the Lemon rule, any symbol in parentheses after a grammar rule
|
||||
symbol becomes a place holder for that symbol in the grammar rule.
|
||||
This place holder can then be used in the associated C action to
|
||||
stand for the value of that symbol.<p>
|
||||
|
||||
<p>The Lemon notation for linking a grammar rule with its reduce
|
||||
action is superior to yacc/bison on several counts.
|
||||
First, as mentioned above, the Lemon method avoids the need to
|
||||
count grammar symbols.
|
||||
Secondly, if a terminal or nonterminal in a Lemon grammar rule
|
||||
includes a linking symbol in parentheses but that linking symbol
|
||||
is not actually used in the reduce action, then an error message
|
||||
is generated.
|
||||
For example, the rule
|
||||
<pre>
|
||||
expr(A) ::= expr(B) PLUS expr(C). { A = B; }
|
||||
</pre>
|
||||
will generate an error because the linking symbol ``C'' is used
|
||||
in the grammar rule but not in the reduce action.</p>
|
||||
|
||||
<p>The Lemon notation for linking grammar rules to reduce actions
|
||||
also facilitates the use of destructors for reclaiming memory
|
||||
allocated by the values of terminals and nonterminals on the
|
||||
right-hand side of a rule.</p>
|
||||
|
||||
<h3>Precedence Rules</h3>
|
||||
|
||||
<p>Lemon resolves parsing ambiguities in exactly the same way as
|
||||
yacc and bison. A shift-reduce conflict is resolved in favor
|
||||
of the shift, and a reduce-reduce conflict is resolved by reducing
|
||||
whichever rule comes first in the grammar file.</p>
|
||||
|
||||
<p>Just like in
|
||||
yacc and bison, Lemon allows a measure of control
|
||||
over the resolution of paring conflicts using precedence rules.
|
||||
A precedence value can be assigned to any terminal symbol
|
||||
using the %left, %right or %nonassoc directives. Terminal symbols
|
||||
mentioned in earlier directives have a lower precedence that
|
||||
terminal symbols mentioned in later directives. For example:</p>
|
||||
|
||||
<p><pre>
|
||||
%left AND.
|
||||
%left OR.
|
||||
%nonassoc EQ NE GT GE LT LE.
|
||||
%left PLUS MINUS.
|
||||
%left TIMES DIVIDE MOD.
|
||||
%right EXP NOT.
|
||||
</pre></p>
|
||||
|
||||
<p>In the preceding sequence of directives, the AND operator is
|
||||
defined to have the lowest precedence. The OR operator is one
|
||||
precedence level higher. And so forth. Hence, the grammar would
|
||||
attempt to group the ambiguous expression
|
||||
<pre>
|
||||
a AND b OR c
|
||||
</pre>
|
||||
like this
|
||||
<pre>
|
||||
a AND (b OR c).
|
||||
</pre>
|
||||
The associativity (left, right or nonassoc) is used to determine
|
||||
the grouping when the precedence is the same. AND is left-associative
|
||||
in our example, so
|
||||
<pre>
|
||||
a AND b AND c
|
||||
</pre>
|
||||
is parsed like this
|
||||
<pre>
|
||||
(a AND b) AND c.
|
||||
</pre>
|
||||
The EXP operator is right-associative, though, so
|
||||
<pre>
|
||||
a EXP b EXP c
|
||||
</pre>
|
||||
is parsed like this
|
||||
<pre>
|
||||
a EXP (b EXP c).
|
||||
</pre>
|
||||
The nonassoc precedence is used for non-associative operators.
|
||||
So
|
||||
<pre>
|
||||
a EQ b EQ c
|
||||
</pre>
|
||||
is an error.</p>
|
||||
|
||||
<p>The precedence of non-terminals is transferred to rules as follows:
|
||||
The precedence of a grammar rule is equal to the precedence of the
|
||||
left-most terminal symbol in the rule for which a precedence is
|
||||
defined. This is normally what you want, but in those cases where
|
||||
you want to precedence of a grammar rule to be something different,
|
||||
you can specify an alternative precedence symbol by putting the
|
||||
symbol in square braces after the period at the end of the rule and
|
||||
before any C-code. For example:</p>
|
||||
|
||||
<p><pre>
|
||||
expr = MINUS expr. [NOT]
|
||||
</pre></p>
|
||||
|
||||
<p>This rule has a precedence equal to that of the NOT symbol, not the
|
||||
MINUS symbol as would have been the case by default.</p>
|
||||
|
||||
<p>With the knowledge of how precedence is assigned to terminal
|
||||
symbols and individual
|
||||
grammar rules, we can now explain precisely how parsing conflicts
|
||||
are resolved in Lemon. Shift-reduce conflicts are resolved
|
||||
as follows:
|
||||
<ul>
|
||||
<li> If either the token to be shifted or the rule to be reduced
|
||||
lacks precedence information, then resolve in favor of the
|
||||
shift, but report a parsing conflict.
|
||||
<li> If the precedence of the token to be shifted is greater than
|
||||
the precedence of the rule to reduce, then resolve in favor
|
||||
of the shift. No parsing conflict is reported.
|
||||
<li> If the precedence of the token it be shifted is less than the
|
||||
precedence of the rule to reduce, then resolve in favor of the
|
||||
reduce action. No parsing conflict is reported.
|
||||
<li> If the precedences are the same and the shift token is
|
||||
right-associative, then resolve in favor of the shift.
|
||||
No parsing conflict is reported.
|
||||
<li> If the precedences are the same the the shift token is
|
||||
left-associative, then resolve in favor of the reduce.
|
||||
No parsing conflict is reported.
|
||||
<li> Otherwise, resolve the conflict by doing the shift and
|
||||
report the parsing conflict.
|
||||
</ul>
|
||||
Reduce-reduce conflicts are resolved this way:
|
||||
<ul>
|
||||
<li> If either reduce rule
|
||||
lacks precedence information, then resolve in favor of the
|
||||
rule that appears first in the grammar and report a parsing
|
||||
conflict.
|
||||
<li> If both rules have precedence and the precedence is different
|
||||
then resolve the dispute in favor of the rule with the highest
|
||||
precedence and do not report a conflict.
|
||||
<li> Otherwise, resolve the conflict by reducing by the rule that
|
||||
appears first in the grammar and report a parsing conflict.
|
||||
</ul>
|
||||
|
||||
<h3>Special Directives</h3>
|
||||
|
||||
<p>The input grammar to Lemon consists of grammar rules and special
|
||||
directives. We've described all the grammar rules, so now we'll
|
||||
talk about the special directives.</p>
|
||||
|
||||
<p>Directives in lemon can occur in any order. You can put them before
|
||||
the grammar rules, or after the grammar rules, or in the mist of the
|
||||
grammar rules. It doesn't matter. The relative order of
|
||||
directives used to assign precedence to terminals is important, but
|
||||
other than that, the order of directives in Lemon is arbitrary.</p>
|
||||
|
||||
<p>Lemon supports the following special directives:
|
||||
<ul>
|
||||
<li><tt>%destructor</tt>
|
||||
<li><tt>%extra_argument</tt>
|
||||
<li><tt>%include</tt>
|
||||
<li><tt>%left</tt>
|
||||
<li><tt>%name</tt>
|
||||
<li><tt>%nonassoc</tt>
|
||||
<li><tt>%parse_accept</tt>
|
||||
<li><tt>%parse_failure </tt>
|
||||
<li><tt>%right</tt>
|
||||
<li><tt>%stack_overflow</tt>
|
||||
<li><tt>%stack_size</tt>
|
||||
<li><tt>%start_symbol</tt>
|
||||
<li><tt>%syntax_error</tt>
|
||||
<li><tt>%token_destructor</tt>
|
||||
<li><tt>%token_prefix</tt>
|
||||
<li><tt>%token_type</tt>
|
||||
<li><tt>%type</tt>
|
||||
</ul>
|
||||
Each of these directives will be described separately in the
|
||||
following sections:</p>
|
||||
|
||||
<h4>The <tt>%destructor</tt> directive</h4>
|
||||
|
||||
<p>The %destructor directive is used to specify a destructor for
|
||||
a non-terminal symbol.
|
||||
(See also the %token_destructor directive which is used to
|
||||
specify a destructor for terminal symbols.)</p>
|
||||
|
||||
<p>A non-terminal's destructor is called to dispose of the
|
||||
non-terminal's value whenever the non-terminal is popped from
|
||||
the stack. This includes all of the following circumstances:
|
||||
<ul>
|
||||
<li> When a rule reduces and the value of a non-terminal on
|
||||
the right-hand side is not linked to C code.
|
||||
<li> When the stack is popped during error processing.
|
||||
<li> When the ParseFree() function runs.
|
||||
</ul>
|
||||
The destructor can do whatever it wants with the value of
|
||||
the non-terminal, but its design is to deallocate memory
|
||||
or other resources held by that non-terminal.</p>
|
||||
|
||||
<p>Consider an example:
|
||||
<pre>
|
||||
%type nt {void*}
|
||||
%destructor nt { free($$); }
|
||||
nt(A) ::= ID NUM. { A = malloc( 100 ); }
|
||||
</pre>
|
||||
This example is a bit contrived but it serves to illustrate how
|
||||
destructors work. The example shows a non-terminal named
|
||||
``nt'' that holds values of type ``void*''. When the rule for
|
||||
an ``nt'' reduces, it sets the value of the non-terminal to
|
||||
space obtained from malloc(). Later, when the nt non-terminal
|
||||
is popped from the stack, the destructor will fire and call
|
||||
free() on this malloced space, thus avoiding a memory leak.
|
||||
(Note that the symbol ``$$'' in the destructor code is replaced
|
||||
by the value of the non-terminal.)</p>
|
||||
|
||||
<p>It is important to note that the value of a non-terminal is passed
|
||||
to the destructor whenever the non-terminal is removed from the
|
||||
stack, unless the non-terminal is used in a C-code action. If
|
||||
the non-terminal is used by C-code, then it is assumed that the
|
||||
C-code will take care of destroying it if it should really
|
||||
be destroyed. More commonly, the value is used to build some
|
||||
larger structure and we don't want to destroy it, which is why
|
||||
the destructor is not called in this circumstance.</p>
|
||||
|
||||
<p>By appropriate use of destructors, it is possible to
|
||||
build a parser using Lemon that can be used within a long-running
|
||||
program, such as a GUI, that will not leak memory or other resources.
|
||||
To do the same using yacc or bison is much more difficult.</p>
|
||||
|
||||
<h4>The <tt>%extra_argument</tt> directive</h4>
|
||||
|
||||
The %extra_argument directive instructs Lemon to add a 4th parameter
|
||||
to the parameter list of the Parse() function it generates. Lemon
|
||||
doesn't do anything itself with this extra argument, but it does
|
||||
make the argument available to C-code action routines, destructors,
|
||||
and so forth. For example, if the grammar file contains:</p>
|
||||
|
||||
<p><pre>
|
||||
%extra_argument { MyStruct *pAbc }
|
||||
</pre></p>
|
||||
|
||||
<p>Then the Parse() function generated will have an 4th parameter
|
||||
of type ``MyStruct*'' and all action routines will have access to
|
||||
a variable named ``pAbc'' that is the value of the 4th parameter
|
||||
in the most recent call to Parse().</p>
|
||||
|
||||
<h4>The <tt>%include</tt> directive</h4>
|
||||
|
||||
<p>The %include directive specifies C code that is included at the
|
||||
top of the generated parser. You can include any text you want --
|
||||
the Lemon parser generator copies to blindly. If you have multiple
|
||||
%include directives in your grammar file, their values are concatenated
|
||||
before being put at the beginning of the generated parser.</p>
|
||||
|
||||
<p>The %include directive is very handy for getting some extra #include
|
||||
preprocessor statements at the beginning of the generated parser.
|
||||
For example:</p>
|
||||
|
||||
<p><pre>
|
||||
%include {#include <unistd.h>}
|
||||
</pre></p>
|
||||
|
||||
<p>This might be needed, for example, if some of the C actions in the
|
||||
grammar call functions that are prototyed in unistd.h.</p>
|
||||
|
||||
<h4>The <tt>%left</tt> directive</h4>
|
||||
|
||||
The %left directive is used (along with the %right and
|
||||
%nonassoc directives) to declare precedences of terminal
|
||||
symbols. Every terminal symbol whose name appears after
|
||||
a %left directive but before the next period (``.'') is
|
||||
given the same left-associative precedence value. Subsequent
|
||||
%left directives have higher precedence. For example:</p>
|
||||
|
||||
<p><pre>
|
||||
%left AND.
|
||||
%left OR.
|
||||
%nonassoc EQ NE GT GE LT LE.
|
||||
%left PLUS MINUS.
|
||||
%left TIMES DIVIDE MOD.
|
||||
%right EXP NOT.
|
||||
</pre></p>
|
||||
|
||||
<p>Note the period that terminates each %left, %right or %nonassoc
|
||||
directive.</p>
|
||||
|
||||
<p>LALR(1) grammars can get into a situation where they require
|
||||
a large amount of stack space if you make heavy use or right-associative
|
||||
operators. For this reason, it is recommended that you use %left
|
||||
rather than %right whenever possible.</p>
|
||||
|
||||
<h4>The <tt>%name</tt> directive</h4>
|
||||
|
||||
<p>By default, the functions generated by Lemon all begin with the
|
||||
five-character string ``Parse''. You can change this string to something
|
||||
different using the %name directive. For instance:</p>
|
||||
|
||||
<p><pre>
|
||||
%name Abcde
|
||||
</pre></p>
|
||||
|
||||
<p>Putting this directive in the grammar file will cause Lemon to generate
|
||||
functions named
|
||||
<ul>
|
||||
<li> AbcdeAlloc(),
|
||||
<li> AbcdeFree(),
|
||||
<li> AbcdeTrace(), and
|
||||
<li> Abcde().
|
||||
</ul>
|
||||
The %name directive allows you to generator two or more different
|
||||
parsers and link them all into the same executable.
|
||||
</p>
|
||||
|
||||
<h4>The <tt>%nonassoc</tt> directive</h4>
|
||||
|
||||
<p>This directive is used to assign non-associative precedence to
|
||||
one or more terminal symbols. See the section on precedence rules
|
||||
or on the %left directive for additional information.</p>
|
||||
|
||||
<h4>The <tt>%parse_accept</tt> directive</h4>
|
||||
|
||||
<p>The %parse_accept directive specifies a block of C code that is
|
||||
executed whenever the parser accepts its input string. To ``accept''
|
||||
an input string means that the parser was able to process all tokens
|
||||
without error.</p>
|
||||
|
||||
<p>For example:</p>
|
||||
|
||||
<p><pre>
|
||||
%parse_accept {
|
||||
printf("parsing complete!\n");
|
||||
}
|
||||
</pre></p>
|
||||
|
||||
|
||||
<h4>The <tt>%parse_failure</tt> directive</h4>
|
||||
|
||||
<p>The %parse_failure directive specifies a block of C code that
|
||||
is executed whenever the parser fails complete. This code is not
|
||||
executed until the parser has tried and failed to resolve an input
|
||||
error using is usual error recovery strategy. The routine is
|
||||
only invoked when parsing is unable to continue.</p>
|
||||
|
||||
<p><pre>
|
||||
%parse_failure {
|
||||
fprintf(stderr,"Giving up. Parser is hopelessly lost...\n");
|
||||
}
|
||||
</pre></p>
|
||||
|
||||
<h4>The <tt>%right</tt> directive</h4>
|
||||
|
||||
<p>This directive is used to assign right-associative precedence to
|
||||
one or more terminal symbols. See the section on precedence rules
|
||||
or on the %left directive for additional information.</p>
|
||||
|
||||
<h4>The <tt>%stack_overflow</tt> directive</h4>
|
||||
|
||||
<p>The %stack_overflow directive specifies a block of C code that
|
||||
is executed if the parser's internal stack ever overflows. Typically
|
||||
this just prints an error message. After a stack overflow, the parser
|
||||
will be unable to continue and must be reset.</p>
|
||||
|
||||
<p><pre>
|
||||
%stack_overflow {
|
||||
fprintf(stderr,"Giving up. Parser stack overflow\n");
|
||||
}
|
||||
</pre></p>
|
||||
|
||||
<p>You can help prevent parser stack overflows by avoiding the use
|
||||
of right recursion and right-precedence operators in your grammar.
|
||||
Use left recursion and and left-precedence operators instead, to
|
||||
encourage rules to reduce sooner and keep the stack size down.
|
||||
For example, do rules like this:
|
||||
<pre>
|
||||
list ::= list element. // left-recursion. Good!
|
||||
list ::= .
|
||||
</pre>
|
||||
Not like this:
|
||||
<pre>
|
||||
list ::= element list. // right-recursion. Bad!
|
||||
list ::= .
|
||||
</pre>
|
||||
|
||||
<h4>The <tt>%stack_size</tt> directive</h4>
|
||||
|
||||
<p>If stack overflow is a problem and you can't resolve the trouble
|
||||
by using left-recursion, then you might want to increase the size
|
||||
of the parser's stack using this directive. Put an positive integer
|
||||
after the %stack_size directive and Lemon will generate a parse
|
||||
with a stack of the requested size. The default value is 100.</p>
|
||||
|
||||
<p><pre>
|
||||
%stack_size 2000
|
||||
</pre></p>
|
||||
|
||||
<h4>The <tt>%start_symbol</tt> directive</h4>
|
||||
|
||||
<p>By default, the start-symbol for the grammar that Lemon generates
|
||||
is the first non-terminal that appears in the grammar file. But you
|
||||
can choose a different start-symbol using the %start_symbol directive.</p>
|
||||
|
||||
<p><pre>
|
||||
%start_symbol prog
|
||||
</pre></p>
|
||||
|
||||
<h4>The <tt>%token_destructor</tt> directive</h4>
|
||||
|
||||
<p>The %destructor directive assigns a destructor to a non-terminal
|
||||
symbol. (See the description of the %destructor directive above.)
|
||||
This directive does the same thing for all terminal symbols.</p>
|
||||
|
||||
<p>Unlike non-terminal symbols which may each have a different data type
|
||||
for their values, terminals all use the same data type (defined by
|
||||
the %token_type directive) and so they use a common destructor. Other
|
||||
than that, the token destructor works just like the non-terminal
|
||||
destructors.</p>
|
||||
|
||||
<h4>The <tt>%token_prefix</tt> directive</h4>
|
||||
|
||||
<p>Lemon generates #defines that assign small integer constants
|
||||
to each terminal symbol in the grammar. If desired, Lemon will
|
||||
add a prefix specified by this directive
|
||||
to each of the #defines it generates.
|
||||
So if the default output of Lemon looked like this:
|
||||
<pre>
|
||||
#define AND 1
|
||||
#define MINUS 2
|
||||
#define OR 3
|
||||
#define PLUS 4
|
||||
</pre>
|
||||
You can insert a statement into the grammar like this:
|
||||
<pre>
|
||||
%token_prefix TOKEN_
|
||||
</pre>
|
||||
to cause Lemon to produce these symbols instead:
|
||||
<pre>
|
||||
#define TOKEN_AND 1
|
||||
#define TOKEN_MINUS 2
|
||||
#define TOKEN_OR 3
|
||||
#define TOKEN_PLUS 4
|
||||
</pre>
|
||||
|
||||
<h4>The <tt>%token_type</tt> and <tt>%type</tt> directives</h4>
|
||||
|
||||
<p>These directives are used to specify the data types for values
|
||||
on the parser's stack associated with terminal and non-terminal
|
||||
symbols. The values of all terminal symbols must be of the same
|
||||
type. This turns out to be the same data type as the 3rd parameter
|
||||
to the Parse() function generated by Lemon. Typically, you will
|
||||
make the value of a terminal symbol by a pointer to some kind of
|
||||
token structure. Like this:</p>
|
||||
|
||||
<p><pre>
|
||||
%token_type {Token*}
|
||||
</pre></p>
|
||||
|
||||
<p>If the data type of terminals is not specified, the default value
|
||||
is ``int''.</p>
|
||||
|
||||
<p>Non-terminal symbols can each have their own data types. Typically
|
||||
the data type of a non-terminal is a pointer to the root of a parse-tree
|
||||
structure that contains all information about that non-terminal.
|
||||
For example:</p>
|
||||
|
||||
<p><pre>
|
||||
%type expr {Expr*}
|
||||
</pre></p>
|
||||
|
||||
<p>Each entry on the parser's stack is actually a union containing
|
||||
instances of all data types for every non-terminal and terminal symbol.
|
||||
Lemon will automatically use the correct element of this union depending
|
||||
on what the corresponding non-terminal or terminal symbol is. But
|
||||
the grammar designer should keep in mind that the size of the union
|
||||
will be the size of its largest element. So if you have a single
|
||||
non-terminal whose data type requires 1K of storage, then your 100
|
||||
entry parser stack will require 100K of heap space. If you are willing
|
||||
and able to pay that price, fine. You just need to know.</p>
|
||||
|
||||
<h3>Error Processing</h3>
|
||||
|
||||
<p>After extensive experimentation over several years, it has been
|
||||
discovered that the error recovery strategy used by yacc is about
|
||||
as good as it gets. And so that is what Lemon uses.</p>
|
||||
|
||||
<p>When a Lemon-generated parser encounters a syntax error, it
|
||||
first invokes the code specified by the %syntax_error directive, if
|
||||
any. It then enters its error recovery strategy. The error recovery
|
||||
strategy is to begin popping the parsers stack until it enters a
|
||||
state where it is permitted to shift a special non-terminal symbol
|
||||
named ``error''. It then shifts this non-terminal and continues
|
||||
parsing. But the %syntax_error routine will not be called again
|
||||
until at least three new tokens have been successfully shifted.</p>
|
||||
|
||||
<p>If the parser pops its stack until the stack is empty, and it still
|
||||
is unable to shift the error symbol, then the %parse_failed routine
|
||||
is invoked and the parser resets itself to its start state, ready
|
||||
to begin parsing a new file. This is what will happen at the very
|
||||
first syntax error, of course, if there are no instances of the
|
||||
``error'' non-terminal in your grammar.</p>
|
||||
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,34 @@
|
|||
/* $Id: lemonflex-head.inc,v 1.1 2001/02/01 20:21:25 gram Exp $ */
|
||||
|
||||
/* This file is #include'd at the top of a Lex/Flex scanner
|
||||
for use with the Lemon parser. You must have #define'd:
|
||||
|
||||
LVAL name of lval variable
|
||||
LVAL_TYPE type of lval variable
|
||||
LVAL_INIT_VAL Initial value of lval variable
|
||||
|
||||
*/
|
||||
|
||||
/* Flex has a few routines which help us get the scanner to read
|
||||
* from a string rather than from a file. POSIX lex only provides
|
||||
* for reading from a file; any method of reading from a string
|
||||
* is inherently non-portable. Besides reading from a string,
|
||||
* we have to worry about resetting the scanner after a bad
|
||||
* parse; this too is non-portable. Combine the reset with
|
||||
* a string input, and you have major non-portability. I'll provide
|
||||
* the routines for flex here. If you really want to modify the
|
||||
* scanner and use a non-flex lex implementation, you may
|
||||
* add more ifdef's below.
|
||||
*/
|
||||
|
||||
|
||||
/* If we don't need yyunput, use this macro to get it out of the
|
||||
* generated C file, avoiding a compiler warning about its lack of use */
|
||||
#define YY_NO_UNPUT 1
|
||||
|
||||
|
||||
/* Yup, I'm using a non-standard type for lval, unlike usual lex/yacc implementations.
|
||||
* I can do so because I'm *not* using yacc, I'm using Lemon, where I have
|
||||
* more control of the interaction between scanner and parser. */
|
||||
LVAL_TYPE LVAL = LVAL_INIT_VAL;
|
||||
|
|
@ -0,0 +1,59 @@
|
|||
/* $Id: lemonflex-tail.inc,v 1.1 2001/02/01 20:21:25 gram Exp $ */
|
||||
|
||||
/* This file is #include'd at the bottom of a Lex/Flex scanner
|
||||
for use with the Lemon parser. You must have #define'd:
|
||||
|
||||
MODNAME module name for creating function names:
|
||||
|
||||
Prototypes:
|
||||
|
||||
void MODNAME_scanner_text(char *text);
|
||||
void MODNAME_scanner_cleanup(void);
|
||||
int MODNAME_wrap(void);
|
||||
*/
|
||||
|
||||
#include <cppmagic.h>
|
||||
|
||||
#define TEXT_FUNC CONCAT(MODNAME,_scanner_text)
|
||||
#define FILE_FUNC CONCAT(MODNAME,_scanner_file)
|
||||
#define CLEANUP_FUNC CONCAT(MODNAME,_scanner_cleanup)
|
||||
#define WRAP_FUNC CONCAT(MODNAME,_wrap)
|
||||
|
||||
|
||||
/* Resets scanner and assigns the char* argument
|
||||
* as the text to scan
|
||||
*/
|
||||
void
|
||||
TEXT_FUNC (const char *text)
|
||||
{
|
||||
yy_scan_string(text);
|
||||
}
|
||||
|
||||
void
|
||||
FILE_FUNC (FILE* fh)
|
||||
{
|
||||
YY_BUFFER_STATE new_buffer;
|
||||
|
||||
new_buffer = yy_create_buffer(fh, YY_BUF_SIZE);
|
||||
yy_switch_to_buffer(new_buffer);
|
||||
}
|
||||
|
||||
void
|
||||
CLEANUP_FUNC (void)
|
||||
{
|
||||
BEGIN(INITIAL);
|
||||
yy_delete_buffer(YY_CURRENT_BUFFER);
|
||||
}
|
||||
|
||||
/* Flex has an option '%option noyywrap' so that I don't have to
|
||||
* provide this yywrap function, but in order to maintain portability,
|
||||
* I'll just use this yywrap() function.
|
||||
*/
|
||||
int
|
||||
WRAP_FUNC ()
|
||||
{
|
||||
return 1; /* stop at EOF, instead of looking for next file */
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,598 @@
|
|||
/* Driver template for the LEMON parser generator.
|
||||
** Copyright 1991-1995 by D. Richard Hipp.
|
||||
**
|
||||
** This library is free software; you can redistribute it and/or
|
||||
** modify it under the terms of the GNU Library General Public
|
||||
** License as published by the Free Software Foundation; either
|
||||
** version 2 of the License, or (at your option) any later version.
|
||||
**
|
||||
** This library 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
|
||||
** Library General Public License for more details.
|
||||
**
|
||||
** You should have received a copy of the GNU Library General Public
|
||||
** License along with this library; if not, write to the
|
||||
** Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
||||
** Boston, MA 02111-1307, USA.
|
||||
**
|
||||
** Modified 1997 to make it suitable for use with makeheaders.
|
||||
*/
|
||||
/* First off, code is include which follows the "include" declaration
|
||||
** in the input file. */
|
||||
#include <stdio.h>
|
||||
%%
|
||||
/* Next is all token values, in a form suitable for use by makeheaders.
|
||||
** This section will be null unless lemon is run with the -m switch.
|
||||
*/
|
||||
/*
|
||||
** These constants (all generated automatically by the parser generator)
|
||||
** specify the various kinds of tokens (terminals) that the parser
|
||||
** understands.
|
||||
**
|
||||
** Each symbol here is a terminal symbol in the grammar.
|
||||
*/
|
||||
%%
|
||||
/* Make sure the INTERFACE macro is defined.
|
||||
*/
|
||||
#ifndef INTERFACE
|
||||
# define INTERFACE 1
|
||||
#endif
|
||||
/* The next thing included is series of defines which control
|
||||
** various aspects of the generated parser.
|
||||
** YYCODETYPE is the data type used for storing terminal
|
||||
** and nonterminal numbers. "unsigned char" is
|
||||
** used if there are fewer than 250 terminals
|
||||
** and nonterminals. "int" is used otherwise.
|
||||
** YYNOCODE is a number of type YYCODETYPE which corresponds
|
||||
** to no legal terminal or nonterminal number. This
|
||||
** number is used to fill in empty slots of the hash
|
||||
** table.
|
||||
** YYACTIONTYPE is the data type used for storing terminal
|
||||
** and nonterminal numbers. "unsigned char" is
|
||||
** used if there are fewer than 250 rules and
|
||||
** states combined. "int" is used otherwise.
|
||||
** ParseTOKENTYPE is the data type used for minor tokens given
|
||||
** directly to the parser from the tokenizer.
|
||||
** YYMINORTYPE is the data type used for all minor tokens.
|
||||
** This is typically a union of many types, one of
|
||||
** which is ParseTOKENTYPE. The entry in the union
|
||||
** for base tokens is called "yy0".
|
||||
** YYSTACKDEPTH is the maximum depth of the parser's stack.
|
||||
** ParseARGDECL is a declaration of a 3rd argument to the
|
||||
** parser, or null if there is no extra argument.
|
||||
** ParseKRARGDECL A version of ParseARGDECL for K&R C.
|
||||
** ParseANSIARGDECL A version of ParseARGDECL for ANSI C.
|
||||
** YYNSTATE the combined number of states.
|
||||
** YYNRULE the number of rules in the grammar
|
||||
** YYERRORSYMBOL is the code number of the error symbol. If not
|
||||
** defined, then do no error processing.
|
||||
*/
|
||||
%%
|
||||
#define YY_NO_ACTION (YYNSTATE+YYNRULE+2)
|
||||
#define YY_ACCEPT_ACTION (YYNSTATE+YYNRULE+1)
|
||||
#define YY_ERROR_ACTION (YYNSTATE+YYNRULE)
|
||||
/* Next is the action table. Each entry in this table contains
|
||||
**
|
||||
** + An integer which is the number representing the look-ahead
|
||||
** token
|
||||
**
|
||||
** + An integer indicating what action to take. Number (N) between
|
||||
** 0 and YYNSTATE-1 mean shift the look-ahead and go to state N.
|
||||
** Numbers between YYNSTATE and YYNSTATE+YYNRULE-1 mean reduce by
|
||||
** rule N-YYNSTATE. Number YYNSTATE+YYNRULE means that a syntax
|
||||
** error has occurred. Number YYNSTATE+YYNRULE+1 means the parser
|
||||
** accepts its input.
|
||||
**
|
||||
** + A pointer to the next entry with the same hash value.
|
||||
**
|
||||
** The action table is really a series of hash tables. Each hash
|
||||
** table contains a number of entries which is a power of two. The
|
||||
** "state" table (which follows) contains information about the starting
|
||||
** point and size of each hash table.
|
||||
*/
|
||||
struct yyActionEntry {
|
||||
YYCODETYPE lookahead; /* The value of the look-ahead token */
|
||||
YYACTIONTYPE action; /* Action to take for this look-ahead */
|
||||
struct yyActionEntry *next; /* Next look-ahead with the same hash, or NULL */
|
||||
};
|
||||
static struct yyActionEntry yyActionTable[] = {
|
||||
%%
|
||||
};
|
||||
|
||||
/* The state table contains information needed to look up the correct
|
||||
** action in the action table, given the current state of the parser.
|
||||
** Information needed includes:
|
||||
**
|
||||
** + A pointer to the start of the action hash table in yyActionTable.
|
||||
**
|
||||
** + A mask used to hash the look-ahead token. The mask is an integer
|
||||
** which is one less than the size of the hash table.
|
||||
**
|
||||
** + The default action. This is the action to take if no entry for
|
||||
** the given look-ahead is found in the action hash table.
|
||||
*/
|
||||
struct yyStateEntry {
|
||||
struct yyActionEntry *hashtbl; /* Start of the hash table in yyActionTable */
|
||||
int mask; /* Mask used for hashing the look-ahead */
|
||||
YYACTIONTYPE actionDefault; /* Default action if look-ahead not found */
|
||||
};
|
||||
static struct yyStateEntry yyStateTable[] = {
|
||||
%%
|
||||
};
|
||||
|
||||
/* The following structure represents a single element of the
|
||||
** parser's stack. Information stored includes:
|
||||
**
|
||||
** + The state number for the parser at this level of the stack.
|
||||
**
|
||||
** + The value of the token stored at this level of the stack.
|
||||
** (In other words, the "major" token.)
|
||||
**
|
||||
** + The semantic value stored at this level of the stack. This is
|
||||
** the information used by the action routines in the grammar.
|
||||
** It is sometimes called the "minor" token.
|
||||
*/
|
||||
struct yyStackEntry {
|
||||
int stateno; /* The state-number */
|
||||
int major; /* The major token value. This is the code
|
||||
** number for the token at this stack level */
|
||||
YYMINORTYPE minor; /* The user-supplied minor token value. This
|
||||
** is the value of the token */
|
||||
};
|
||||
|
||||
/* The state of the parser is completely contained in an instance of
|
||||
** the following structure */
|
||||
struct yyParser {
|
||||
int idx; /* Index of top element in stack */
|
||||
int errcnt; /* Shifts left before out of the error */
|
||||
struct yyStackEntry *top; /* Pointer to the top stack element */
|
||||
struct yyStackEntry stack[YYSTACKDEPTH]; /* The parser's stack */
|
||||
};
|
||||
typedef struct yyParser yyParser;
|
||||
|
||||
#ifndef NDEBUG
|
||||
#include <stdio.h>
|
||||
static FILE *yyTraceFILE = 0;
|
||||
static char *yyTracePrompt = 0;
|
||||
|
||||
/*
|
||||
** Turn parser tracing on by giving a stream to which to write the trace
|
||||
** and a prompt to preface each trace message. Tracing is turned off
|
||||
** by making either argument NULL
|
||||
**
|
||||
** Inputs:
|
||||
** <ul>
|
||||
** <li> A FILE* to which trace output should be written.
|
||||
** If NULL, then tracing is turned off.
|
||||
** <li> A prefix string written at the beginning of every
|
||||
** line of trace output. If NULL, then tracing is
|
||||
** turned off.
|
||||
** </ul>
|
||||
**
|
||||
** Outputs:
|
||||
** None.
|
||||
*/
|
||||
void ParseTrace(FILE *TraceFILE, char *zTracePrompt){
|
||||
yyTraceFILE = TraceFILE;
|
||||
yyTracePrompt = zTracePrompt;
|
||||
if( yyTraceFILE==0 ) yyTracePrompt = 0;
|
||||
else if( yyTracePrompt==0 ) yyTraceFILE = 0;
|
||||
}
|
||||
|
||||
/* For tracing shifts, the names of all terminals and nonterminals
|
||||
** are required. The following table supplies these names */
|
||||
static char *yyTokenName[] = {
|
||||
%%
|
||||
};
|
||||
#define YYTRACE(X) if( yyTraceFILE ) fprintf(yyTraceFILE,"%sReduce [%s].\n",yyTracePrompt,X);
|
||||
#else
|
||||
#define YYTRACE(X)
|
||||
#endif
|
||||
|
||||
/*
|
||||
** This function allocates a new parser.
|
||||
** The only argument is a pointer to a function which works like
|
||||
** malloc.
|
||||
**
|
||||
** Inputs:
|
||||
** A pointer to the function used to allocate memory.
|
||||
**
|
||||
** Outputs:
|
||||
** A pointer to a parser. This pointer is used in subsequent calls
|
||||
** to Parse and ParseFree.
|
||||
*/
|
||||
void *ParseAlloc(void *(*mallocProc)()){
|
||||
yyParser *pParser;
|
||||
pParser = (yyParser*)(*mallocProc)( sizeof(yyParser), __FILE__, __LINE__ );
|
||||
if( pParser ){
|
||||
pParser->idx = -1;
|
||||
}
|
||||
return pParser;
|
||||
}
|
||||
|
||||
/* The following function deletes the value associated with a
|
||||
** symbol. The symbol can be either a terminal or nonterminal.
|
||||
** "yymajor" is the symbol code, and "yypminor" is a pointer to
|
||||
** the value.
|
||||
*/
|
||||
static void yy_destructor(YYCODETYPE yymajor, YYMINORTYPE *yypminor){
|
||||
switch( yymajor ){
|
||||
/* Here is inserted the actions which take place when a
|
||||
** terminal or non-terminal is destroyed. This can happen
|
||||
** when the symbol is popped from the stack during a
|
||||
** reduce or during error processing or when a parser is
|
||||
** being destroyed before it is finished parsing.
|
||||
**
|
||||
** Note: during a reduce, the only symbols destroyed are those
|
||||
** which appear on the RHS of the rule, but which are not used
|
||||
** inside the C code.
|
||||
*/
|
||||
%%
|
||||
default: break; /* If no destructor action specified: do nothing */
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** Pop the parser's stack once.
|
||||
**
|
||||
** If there is a destructor routine associated with the token which
|
||||
** is popped from the stack, then call it.
|
||||
**
|
||||
** Return the major token number for the symbol popped.
|
||||
*/
|
||||
static int yy_pop_parser_stack(yyParser *pParser){
|
||||
YYCODETYPE yymajor;
|
||||
|
||||
if( pParser->idx<0 ) return 0;
|
||||
#ifndef NDEBUG
|
||||
if( yyTraceFILE && pParser->idx>=0 ){
|
||||
fprintf(yyTraceFILE,"%sPopping %s\n",
|
||||
yyTracePrompt,
|
||||
yyTokenName[pParser->top->major]);
|
||||
}
|
||||
#endif
|
||||
yymajor = pParser->top->major;
|
||||
yy_destructor( yymajor, &pParser->top->minor);
|
||||
pParser->idx--;
|
||||
pParser->top--;
|
||||
return yymajor;
|
||||
}
|
||||
|
||||
/*
|
||||
** Deallocate and destroy a parser. Destructors are all called for
|
||||
** all stack elements before shutting the parser down.
|
||||
**
|
||||
** Inputs:
|
||||
** <ul>
|
||||
** <li> A pointer to the parser. This should be a pointer
|
||||
** obtained from ParseAlloc.
|
||||
** <li> A pointer to a function used to reclaim memory obtained
|
||||
** from malloc.
|
||||
** </ul>
|
||||
*/
|
||||
void ParseFree(
|
||||
void *p, /* The parser to be deleted */
|
||||
void (*freeProc)() /* Function used to reclaim memory */
|
||||
){
|
||||
yyParser *pParser = (yyParser*)p;
|
||||
if( pParser==0 ) return;
|
||||
while( pParser->idx>=0 ) yy_pop_parser_stack(pParser);
|
||||
(*freeProc)(pParser, __FILE__, __LINE__);
|
||||
}
|
||||
|
||||
/*
|
||||
** Find the appropriate action for a parser given the look-ahead token.
|
||||
**
|
||||
** If the look-ahead token is YYNOCODE, then check to see if the action is
|
||||
** independent of the look-ahead. If it is, return the action, otherwise
|
||||
** return YY_NO_ACTION.
|
||||
*/
|
||||
static int yy_find_parser_action(
|
||||
yyParser *pParser, /* The parser */
|
||||
int iLookAhead /* The look-ahead token */
|
||||
){
|
||||
struct yyStateEntry *pState; /* Appropriate entry in the state table */
|
||||
struct yyActionEntry *pAction; /* Action appropriate for the look-ahead */
|
||||
|
||||
/* if( pParser->idx<0 ) return YY_NO_ACTION; */
|
||||
pState = &yyStateTable[pParser->top->stateno];
|
||||
if( iLookAhead!=YYNOCODE ){
|
||||
pAction = &pState->hashtbl[iLookAhead & pState->mask];
|
||||
while( pAction ){
|
||||
if( pAction->lookahead==iLookAhead ) return pAction->action;
|
||||
pAction = pAction->next;
|
||||
}
|
||||
}else if( pState->mask!=0 || pState->hashtbl->lookahead!=YYNOCODE ){
|
||||
return YY_NO_ACTION;
|
||||
}
|
||||
return pState->actionDefault;
|
||||
}
|
||||
|
||||
/*
|
||||
** Perform a shift action.
|
||||
*/
|
||||
static void yy_shift(
|
||||
yyParser *yypParser, /* The parser to be shifted */
|
||||
int yyNewState, /* The new state to shift in */
|
||||
int yyMajor, /* The major token to shift in */
|
||||
YYMINORTYPE *yypMinor /* Pointer ot the minor token to shift in */
|
||||
){
|
||||
yypParser->idx++;
|
||||
yypParser->top++;
|
||||
if( yypParser->idx>=YYSTACKDEPTH ){
|
||||
yypParser->idx--;
|
||||
yypParser->top--;
|
||||
#ifndef NDEBUG
|
||||
if( yyTraceFILE ){
|
||||
fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt);
|
||||
}
|
||||
#endif
|
||||
while( yypParser->idx>=0 ) yy_pop_parser_stack(yypParser);
|
||||
/* Here code is inserted which will execute if the parser
|
||||
** stack every overflows */
|
||||
%%
|
||||
return;
|
||||
}
|
||||
yypParser->top->stateno = yyNewState;
|
||||
yypParser->top->major = yyMajor;
|
||||
yypParser->top->minor = *yypMinor;
|
||||
#ifndef NDEBUG
|
||||
if( yyTraceFILE && yypParser->idx>0 ){
|
||||
int i;
|
||||
fprintf(yyTraceFILE,"%sShift %d\n",yyTracePrompt,yyNewState);
|
||||
fprintf(yyTraceFILE,"%sStack:",yyTracePrompt);
|
||||
for(i=1; i<=yypParser->idx; i++)
|
||||
fprintf(yyTraceFILE," %s",yyTokenName[yypParser->stack[i].major]);
|
||||
fprintf(yyTraceFILE,"\n");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/* The following table contains information about every rule that
|
||||
** is used during the reduce.
|
||||
*/
|
||||
static struct {
|
||||
YYCODETYPE lhs; /* Symbol on the left-hand side of the rule */
|
||||
unsigned char nrhs; /* Number of right-hand side symbols in the rule */
|
||||
} yyRuleInfo[] = {
|
||||
%%
|
||||
};
|
||||
|
||||
static void yy_accept(); /* Forward declaration */
|
||||
|
||||
/*
|
||||
** Perform a reduce action and the shift that must immediately
|
||||
** follow the reduce.
|
||||
*/
|
||||
static void yy_reduce(
|
||||
yyParser *yypParser, /* The parser */
|
||||
int yyruleno /* Number of the rule by which to reduce */
|
||||
ParseANSIARGDECL
|
||||
){
|
||||
int yygoto; /* The next state */
|
||||
int yyact; /* The next action */
|
||||
YYMINORTYPE yygotominor; /* The LHS of the rule reduced */
|
||||
struct yyStackEntry *yymsp; /* The top of the parser's stack */
|
||||
int yysize; /* Amount to pop the stack */
|
||||
yymsp = yypParser->top;
|
||||
switch( yyruleno ){
|
||||
/* Beginning here are the reduction cases. A typical example
|
||||
** follows:
|
||||
** case 0:
|
||||
** YYTRACE("<text of the rule>");
|
||||
** #line <lineno> <grammarfile>
|
||||
** { ... } // User supplied code
|
||||
** #line <lineno> <thisfile>
|
||||
** break;
|
||||
*/
|
||||
%%
|
||||
};
|
||||
yygoto = yyRuleInfo[yyruleno].lhs;
|
||||
yysize = yyRuleInfo[yyruleno].nrhs;
|
||||
yypParser->idx -= yysize;
|
||||
yypParser->top -= yysize;
|
||||
yyact = yy_find_parser_action(yypParser,yygoto);
|
||||
if( yyact < YYNSTATE ){
|
||||
yy_shift(yypParser,yyact,yygoto,&yygotominor);
|
||||
}else if( yyact == YYNSTATE + YYNRULE + 1 ){
|
||||
yy_accept(yypParser ParseARGDECL);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** The following code executes when the parse fails
|
||||
*/
|
||||
static void yy_parse_failed(
|
||||
yyParser *yypParser /* The parser */
|
||||
ParseANSIARGDECL /* Extra arguments (if any) */
|
||||
){
|
||||
#ifndef NDEBUG
|
||||
if( yyTraceFILE ){
|
||||
fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt);
|
||||
}
|
||||
#endif
|
||||
while( yypParser->idx>=0 ) yy_pop_parser_stack(yypParser);
|
||||
/* Here code is inserted which will be executed whenever the
|
||||
** parser fails */
|
||||
%%
|
||||
}
|
||||
|
||||
/*
|
||||
** The following code executes when a syntax error first occurs.
|
||||
*/
|
||||
static void yy_syntax_error(
|
||||
yyParser *yypParser, /* The parser */
|
||||
int yymajor, /* The major type of the error token */
|
||||
YYMINORTYPE yyminor /* The minor type of the error token */
|
||||
ParseANSIARGDECL /* Extra arguments (if any) */
|
||||
){
|
||||
#define TOKEN (yyminor.yy0)
|
||||
%%
|
||||
}
|
||||
|
||||
/*
|
||||
** The following is executed when the parser accepts
|
||||
*/
|
||||
static void yy_accept(
|
||||
yyParser *yypParser /* The parser */
|
||||
ParseANSIARGDECL /* Extra arguments (if any) */
|
||||
){
|
||||
#ifndef NDEBUG
|
||||
if( yyTraceFILE ){
|
||||
fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt);
|
||||
}
|
||||
#endif
|
||||
while( yypParser->idx>=0 ) yy_pop_parser_stack(yypParser);
|
||||
/* Here code is inserted which will be executed whenever the
|
||||
** parser accepts */
|
||||
%%
|
||||
}
|
||||
|
||||
/* The main parser program.
|
||||
** The first argument is a pointer to a structure obtained from
|
||||
** "ParseAlloc" which describes the current state of the parser.
|
||||
** The second argument is the major token number. The third is
|
||||
** the minor token. The fourth optional argument is whatever the
|
||||
** user wants (and specified in the grammar) and is available for
|
||||
** use by the action routines.
|
||||
**
|
||||
** Inputs:
|
||||
** <ul>
|
||||
** <li> A pointer to the parser (an opaque structure.)
|
||||
** <li> The major token number.
|
||||
** <li> The minor token number.
|
||||
** <li> An option argument of a grammar-specified type.
|
||||
** </ul>
|
||||
**
|
||||
** Outputs:
|
||||
** None.
|
||||
*/
|
||||
void Parse(
|
||||
void *yyp, /* The parser */
|
||||
int yymajor, /* The major token code number */
|
||||
ParseTOKENTYPE yyminor /* The value for the token */
|
||||
ParseANSIARGDECL
|
||||
){
|
||||
YYMINORTYPE yyminorunion;
|
||||
int yyact; /* The parser action. */
|
||||
int yyendofinput; /* True if we are at the end of input */
|
||||
int yyerrorhit = 0; /* True if yymajor has invoked an error */
|
||||
yyParser *yypParser; /* The parser */
|
||||
|
||||
/* (re)initialize the parser, if necessary */
|
||||
yypParser = (yyParser*)yyp;
|
||||
if( yypParser->idx<0 ){
|
||||
if( yymajor==0 ) return;
|
||||
yypParser->idx = 0;
|
||||
yypParser->errcnt = -1;
|
||||
yypParser->top = &yypParser->stack[0];
|
||||
yypParser->top->stateno = 0;
|
||||
yypParser->top->major = 0;
|
||||
}
|
||||
yyminorunion.yy0 = yyminor;
|
||||
yyendofinput = (yymajor==0);
|
||||
|
||||
#ifndef NDEBUG
|
||||
if( yyTraceFILE ){
|
||||
fprintf(yyTraceFILE,"%sInput %s\n",yyTracePrompt,yyTokenName[yymajor]);
|
||||
}
|
||||
#endif
|
||||
|
||||
do{
|
||||
yyact = yy_find_parser_action(yypParser,yymajor);
|
||||
if( yyact<YYNSTATE ){
|
||||
yy_shift(yypParser,yyact,yymajor,&yyminorunion);
|
||||
yypParser->errcnt--;
|
||||
if( yyendofinput && yypParser->idx>=0 ){
|
||||
yymajor = 0;
|
||||
}else{
|
||||
yymajor = YYNOCODE;
|
||||
}
|
||||
}else if( yyact < YYNSTATE + YYNRULE ){
|
||||
yy_reduce(yypParser,yyact-YYNSTATE ParseARGDECL);
|
||||
}else if( yyact == YY_ERROR_ACTION ){
|
||||
#ifndef NDEBUG
|
||||
if( yyTraceFILE ){
|
||||
fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt);
|
||||
}
|
||||
#endif
|
||||
#ifdef YYERRORSYMBOL
|
||||
/* A syntax error has occurred.
|
||||
** The response to an error depends upon whether or not the
|
||||
** grammar defines an error token "ERROR".
|
||||
**
|
||||
** This is what we do if the grammar does define ERROR:
|
||||
**
|
||||
** * Call the %syntax_error function.
|
||||
**
|
||||
** * Begin popping the stack until we enter a state where
|
||||
** it is legal to shift the error symbol, then shift
|
||||
** the error symbol.
|
||||
**
|
||||
** * Set the error count to three.
|
||||
**
|
||||
** * Begin accepting and shifting new tokens. No new error
|
||||
** processing will occur until three tokens have been
|
||||
** shifted successfully.
|
||||
**
|
||||
*/
|
||||
if( yypParser->errcnt<0 ){
|
||||
yy_syntax_error(yypParser,yymajor,yyminorunion ParseARGDECL);
|
||||
}
|
||||
if( yypParser->top->major==YYERRORSYMBOL || yyerrorhit ){
|
||||
#ifndef NDEBUG
|
||||
if( yyTraceFILE ){
|
||||
fprintf(yyTraceFILE,"%sDiscard input token %s\n",
|
||||
yyTracePrompt,yyTokenName[yymajor]);
|
||||
}
|
||||
#endif
|
||||
yy_destructor(yymajor,&yyminorunion);
|
||||
yymajor = YYNOCODE;
|
||||
}else{
|
||||
while(
|
||||
yypParser->idx >= 0 &&
|
||||
yypParser->top->major != YYERRORSYMBOL &&
|
||||
(yyact = yy_find_parser_action(yypParser,YYERRORSYMBOL)) >= YYNSTATE
|
||||
){
|
||||
yy_pop_parser_stack(yypParser);
|
||||
}
|
||||
if( yypParser->idx < 0 || yymajor==0 ){
|
||||
yy_destructor(yymajor,&yyminorunion);
|
||||
yy_parse_failed(yypParser ParseARGDECL);
|
||||
yymajor = YYNOCODE;
|
||||
}else if( yypParser->top->major!=YYERRORSYMBOL ){
|
||||
YYMINORTYPE u2;
|
||||
u2.YYERRSYMDT = 0;
|
||||
yy_shift(yypParser,yyact,YYERRORSYMBOL,&u2);
|
||||
}
|
||||
}
|
||||
yypParser->errcnt = 3;
|
||||
yyerrorhit = 1;
|
||||
#else /* YYERRORSYMBOL is not defined */
|
||||
/* This is what we do if the grammar does not define ERROR:
|
||||
**
|
||||
** * Report an error message, and throw away the input token.
|
||||
**
|
||||
** * If the input token is $, then fail the parse.
|
||||
**
|
||||
** As before, subsequent error messages are suppressed until
|
||||
** three input tokens have been successfully shifted.
|
||||
*/
|
||||
if( yypParser->errcnt<=0 ){
|
||||
yy_syntax_error(yypParser,yymajor,yyminorunion ParseARGDECL);
|
||||
}
|
||||
yypParser->errcnt = 3;
|
||||
yy_destructor(yymajor,&yyminorunion);
|
||||
if( yyendofinput ){
|
||||
yy_parse_failed(yypParser ParseARGDECL);
|
||||
}
|
||||
yymajor = YYNOCODE;
|
||||
#endif
|
||||
}else{
|
||||
yy_accept(yypParser ParseARGDECL);
|
||||
yymajor = YYNOCODE;
|
||||
}
|
||||
}while( yymajor!=YYNOCODE && yypParser->idx>=0 );
|
||||
return;
|
||||
}
|
Loading…
Reference in New Issue