Enabled error reporting for bad ETHER values in display filters. A new

global variable, dfilter_error_msg is now available, being NULL when there
was no error, or pointing to a string when an error occurred. The three
places that dfilter_compile() is called now use this global variable to
report the error message to the user. A default error message is put
in that string if no context-specific error message is available (since
I only have one context-specifici error message, namely, ETHER values,
that will be most of the time).

svn path=/trunk/; revision=530
This commit is contained in:
Gilbert Ramirez 1999-08-20 20:37:47 +00:00
parent fa65ee1d1e
commit f0e5afe7a9
5 changed files with 43 additions and 30 deletions

View File

@ -3,7 +3,7 @@
/* dfilter-grammar.y
* Parser for display filters
*
* $Id: dfilter-grammar.y,v 1.11 1999/08/20 06:01:06 gram Exp $
* $Id: dfilter-grammar.y,v 1.12 1999/08/20 20:37:45 gram Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@zing.org>
@ -514,10 +514,9 @@ dfilter_mknode_ether_value(gchar *byte_string)
/* Rather than free the mem_chunk allocation, let it
* stay. It will be cleaned up in the next call to
* dfilter_clear() */
if (global_df->error_sample)
g_free(global_df->error_sample);
global_df->error_sample = g_strdup(byte_string);
global_df->error = DFILTER_ERR_BAD_ETHER_VAL;
dfilter_error_msg = &dfilter_error_msg_buf[0];
snprintf(dfilter_error_msg, sizeof(dfilter_error_msg_buf),
"\"%s\" is not a valid hardware address.", byte_string);
return NULL;
}

View File

@ -1,7 +1,7 @@
/* dfilter.c
* Routines for display filters
*
* $Id: dfilter.c,v 1.11 1999/08/20 06:01:07 gram Exp $
* $Id: dfilter.c,v 1.12 1999/08/20 20:37:46 gram Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@zing.org>
@ -50,6 +50,11 @@
#ifndef __DFILTER_H__
#include "dfilter.h"
#endif
#ifndef __UTIL_H__
#include "util.h"
#endif
#include "dfilter-int.h"
#include "dfilter-grammar.h"
@ -69,6 +74,10 @@ int bytes_length = 0;
YYSTYPE yylval;
/* Global error message space for dfilter_compile errors */
gchar dfilter_error_msg_buf[1024];
gchar *dfilter_error_msg; /* NULL when no error resulted */
static gboolean dfilter_apply_node(GNode *gnode, proto_tree *ptree, const guint8 *pd);
static gboolean check_relation(gint operand, GNode *a, GNode *b, proto_tree *ptree, const guint8 *pd);
static gboolean check_logical(gint operand, GNode *a, GNode *b, proto_tree *ptree, const guint8 *pd);
@ -108,6 +117,12 @@ dfilter_init(void)
* of operations to perform. Can be called multiple times, compiling a new
* display filter each time, without having to clear any memory used, since
* dfilter_compile will take care of that automatically.
*
* Returns 0 on success, non-zero on failure.
* If a 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. If the caller needs is stored, he
* needs to g_strdup it himself.
*/
int
dfilter_compile(dfilter *df, gchar *dfilter_text)
@ -124,6 +139,7 @@ dfilter_compile(dfilter *df, gchar *dfilter_text)
/* Assign global variable so yyparse knows which dfilter we're talking about */
global_df = df;
dfilter_error_msg = NULL;
/* The magic happens right here. */
retval = dfilter_parse();
@ -131,6 +147,17 @@ dfilter_compile(dfilter *df, gchar *dfilter_text)
/* clean up lex */
dfilter_scanner_cleanup();
/* If a parse error occurred, fill in a generic error message
* if one was not created during parsing. */
if (retval != 0) {
if (dfilter_error_msg == NULL) {
dfilter_error_msg = &dfilter_error_msg_buf[0];
snprintf(dfilter_error_msg, sizeof(dfilter_error_msg),
"Unable to parse filter string \"%s\".",
dfilter_text);
}
}
return retval;
}
@ -161,12 +188,6 @@ dfilter_clear_filter(dfilter *df)
df->dftext = NULL;
df->dftree = NULL;
df->list_of_byte_arrays = NULL;
if (df->error_sample) {
g_free(df->error_sample);
}
df->error_sample = NULL;
df->error = DFILTER_ERR_NONE;
}
/* Allocates new dfilter, initializes values, and returns pointer to dfilter */

View File

@ -1,7 +1,7 @@
/* dfilter.h
* Definitions for display filters
*
* $Id: dfilter.h,v 1.8 1999/08/20 06:01:07 gram Exp $
* $Id: dfilter.h,v 1.9 1999/08/20 20:37:46 gram Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@zing.org>
@ -28,6 +28,11 @@
#define DFILTER_CONTAINS_FILTER(x) ((x)->dftree)
/* 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;
@ -39,10 +44,6 @@ typedef struct {
/* 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;
/* Error code and text that produced error */
gint error;
gchar *error_sample;
} dfilter;
/* Initialization of the symbol table. Called once during program startup */
@ -63,9 +64,4 @@ gboolean dfilter_apply(dfilter *df, proto_tree *ptree, const guint8* pd);
/* Clears the current filter int the dfilter */
void dfilter_clear_filter(dfilter *df);
enum {
DFILTER_ERR_NONE,
DFILTER_ERR_BAD_ETHER_VAL
};
#endif /* ! __DFILTER_H__ */

View File

@ -1,6 +1,6 @@
/* ethereal.c
*
* $Id: ethereal.c,v 1.98 1999/08/20 19:43:10 gram Exp $
* $Id: ethereal.c,v 1.99 1999/08/20 20:37:47 gram Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@zing.org>
@ -439,8 +439,7 @@ file_open_ok_cb(GtkWidget *w, GtkFileSelection *fs) {
if (rfilter[0] != '\0') {
rfcode = dfilter_new();
if (dfilter_compile(rfcode, rfilter) != 0) {
simple_dialog(ESD_TYPE_WARN, NULL,
"Unable to parse filter string \"%s\".", rfilter);
simple_dialog(ESD_TYPE_WARN, NULL, dfilter_error_msg);
dfilter_destroy(rfcode);
return;
}
@ -1450,8 +1449,7 @@ main(int argc, char *argv[])
if (rfilter != NULL) {
rfcode = dfilter_new();
if (dfilter_compile(rfcode, rfilter) != 0) {
simple_dialog(ESD_TYPE_WARN, NULL,
"Unable to parse filter string \"%s\".", rfilter);
simple_dialog(ESD_TYPE_WARN, NULL, dfilter_error_msg);
dfilter_destroy(rfcode);
rfilter_parse_failed = TRUE;
}

5
file.c
View File

@ -1,7 +1,7 @@
/* file.c
* File I/O routines
*
* $Id: file.c,v 1.76 1999/08/20 06:55:07 guy Exp $
* $Id: file.c,v 1.77 1999/08/20 20:37:47 gram Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@zing.org>
@ -597,8 +597,7 @@ filter_packets(capture_file *cf)
* Compile the filter.
*/
if (dfilter_compile(cf->dfcode, cf->dfilter) != 0) {
simple_dialog(ESD_TYPE_WARN, NULL,
"Unable to parse filter string \"%s\".", cf->dfilter);
simple_dialog(ESD_TYPE_WARN, NULL, dfilter_error_msg);
return;
}
}