2001-01-28 04:43:26 +00:00
|
|
|
/* filters.c
|
|
|
|
* Code for reading and writing the filters file.
|
|
|
|
*
|
2002-01-21 07:37:49 +00:00
|
|
|
* $Id: filters.c,v 1.14 2002/01/21 07:36:31 guy Exp $
|
2001-01-28 04:43:26 +00:00
|
|
|
*
|
|
|
|
* Ethereal - Network traffic analyzer
|
2001-10-21 21:48:00 +00:00
|
|
|
* By Gerald Combs <gerald@ethereal.com>
|
2001-01-28 04:43:26 +00:00
|
|
|
* Copyright 1998 Gerald Combs
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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 <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
Have separate capture and display filter lists; some filter dialog boxes
use the capture filter lists, and others use the display filter list, as
appropriate.
Have separate menu items for editing the capture and display filter
lists.
Have separate "~/.ethereal/cfilters" and "~/.ethereal/dfilters" files
for the two lists; if either of those files isn't found, we try
"~/.ethereal/filters", which means that you will start out with two
identical lists holding all your filters - if certain filters belong
only in one list, you'll have to delete them by hand from the other
list.
Do I/O error checking when reading and writing filter lists; when
writing a filter list, write it to a new file, and then rename the new
file on top of the old file, so that you don't lose your old filter list
if, for example, you run out of disk space or disk quota.
svn path=/trunk/; revision=2948
2001-01-28 09:13:10 +00:00
|
|
|
#include <errno.h>
|
2001-01-28 04:43:26 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
2002-01-21 07:37:49 +00:00
|
|
|
#include <epan/filesystem.h>
|
2001-01-28 04:43:26 +00:00
|
|
|
|
|
|
|
#include "filters.h"
|
|
|
|
|
Have separate capture and display filter lists; some filter dialog boxes
use the capture filter lists, and others use the display filter list, as
appropriate.
Have separate menu items for editing the capture and display filter
lists.
Have separate "~/.ethereal/cfilters" and "~/.ethereal/dfilters" files
for the two lists; if either of those files isn't found, we try
"~/.ethereal/filters", which means that you will start out with two
identical lists holding all your filters - if certain filters belong
only in one list, you'll have to delete them by hand from the other
list.
Do I/O error checking when reading and writing filter lists; when
writing a filter list, write it to a new file, and then rename the new
file on top of the old file, so that you don't lose your old filter list
if, for example, you run out of disk space or disk quota.
svn path=/trunk/; revision=2948
2001-01-28 09:13:10 +00:00
|
|
|
/*
|
|
|
|
* Old filter file name.
|
|
|
|
*/
|
|
|
|
#define FILTER_FILE_NAME "filters"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Capture filter file name.
|
|
|
|
*/
|
|
|
|
#define CFILTER_FILE_NAME "cfilters"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Display filter file name.
|
|
|
|
*/
|
|
|
|
#define DFILTER_FILE_NAME "dfilters"
|
|
|
|
|
2001-01-28 04:43:26 +00:00
|
|
|
/*
|
Have separate capture and display filter lists; some filter dialog boxes
use the capture filter lists, and others use the display filter list, as
appropriate.
Have separate menu items for editing the capture and display filter
lists.
Have separate "~/.ethereal/cfilters" and "~/.ethereal/dfilters" files
for the two lists; if either of those files isn't found, we try
"~/.ethereal/filters", which means that you will start out with two
identical lists holding all your filters - if certain filters belong
only in one list, you'll have to delete them by hand from the other
list.
Do I/O error checking when reading and writing filter lists; when
writing a filter list, write it to a new file, and then rename the new
file on top of the old file, so that you don't lose your old filter list
if, for example, you run out of disk space or disk quota.
svn path=/trunk/; revision=2948
2001-01-28 09:13:10 +00:00
|
|
|
* List of capture filters.
|
|
|
|
*/
|
|
|
|
static GList *capture_filters = NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* List of display filters.
|
2001-01-28 04:43:26 +00:00
|
|
|
*/
|
Have separate capture and display filter lists; some filter dialog boxes
use the capture filter lists, and others use the display filter list, as
appropriate.
Have separate menu items for editing the capture and display filter
lists.
Have separate "~/.ethereal/cfilters" and "~/.ethereal/dfilters" files
for the two lists; if either of those files isn't found, we try
"~/.ethereal/filters", which means that you will start out with two
identical lists holding all your filters - if certain filters belong
only in one list, you'll have to delete them by hand from the other
list.
Do I/O error checking when reading and writing filter lists; when
writing a filter list, write it to a new file, and then rename the new
file on top of the old file, so that you don't lose your old filter list
if, for example, you run out of disk space or disk quota.
svn path=/trunk/; revision=2948
2001-01-28 09:13:10 +00:00
|
|
|
static GList *display_filters = NULL;
|
2001-01-28 04:43:26 +00:00
|
|
|
|
Have separate capture and display filter lists; some filter dialog boxes
use the capture filter lists, and others use the display filter list, as
appropriate.
Have separate menu items for editing the capture and display filter
lists.
Have separate "~/.ethereal/cfilters" and "~/.ethereal/dfilters" files
for the two lists; if either of those files isn't found, we try
"~/.ethereal/filters", which means that you will start out with two
identical lists holding all your filters - if certain filters belong
only in one list, you'll have to delete them by hand from the other
list.
Do I/O error checking when reading and writing filter lists; when
writing a filter list, write it to a new file, and then rename the new
file on top of the old file, so that you don't lose your old filter list
if, for example, you run out of disk space or disk quota.
svn path=/trunk/; revision=2948
2001-01-28 09:13:10 +00:00
|
|
|
/*
|
|
|
|
* Read in a list of filters.
|
|
|
|
*
|
|
|
|
* On success, "*pref_path_return" is set to NULL.
|
|
|
|
* On error, "*pref_path_return" is set to point to the pathname of
|
|
|
|
* the file we tried to read - it should be freed by our caller -
|
|
|
|
* and "*errno_return" is set to the error.
|
|
|
|
*/
|
2001-02-03 06:03:42 +00:00
|
|
|
|
|
|
|
#define INIT_BUF_SIZE 128
|
|
|
|
|
2001-01-28 04:43:26 +00:00
|
|
|
void
|
Have separate capture and display filter lists; some filter dialog boxes
use the capture filter lists, and others use the display filter list, as
appropriate.
Have separate menu items for editing the capture and display filter
lists.
Have separate "~/.ethereal/cfilters" and "~/.ethereal/dfilters" files
for the two lists; if either of those files isn't found, we try
"~/.ethereal/filters", which means that you will start out with two
identical lists holding all your filters - if certain filters belong
only in one list, you'll have to delete them by hand from the other
list.
Do I/O error checking when reading and writing filter lists; when
writing a filter list, write it to a new file, and then rename the new
file on top of the old file, so that you don't lose your old filter list
if, for example, you run out of disk space or disk quota.
svn path=/trunk/; revision=2948
2001-01-28 09:13:10 +00:00
|
|
|
read_filter_list(filter_list_type_t list, char **pref_path_return,
|
|
|
|
int *errno_return)
|
2001-01-28 04:43:26 +00:00
|
|
|
{
|
2001-10-22 22:59:26 +00:00
|
|
|
char *ff_path, *ff_name;
|
2001-01-28 04:43:26 +00:00
|
|
|
FILE *ff;
|
Have separate capture and display filter lists; some filter dialog boxes
use the capture filter lists, and others use the display filter list, as
appropriate.
Have separate menu items for editing the capture and display filter
lists.
Have separate "~/.ethereal/cfilters" and "~/.ethereal/dfilters" files
for the two lists; if either of those files isn't found, we try
"~/.ethereal/filters", which means that you will start out with two
identical lists holding all your filters - if certain filters belong
only in one list, you'll have to delete them by hand from the other
list.
Do I/O error checking when reading and writing filter lists; when
writing a filter list, write it to a new file, and then rename the new
file on top of the old file, so that you don't lose your old filter list
if, for example, you run out of disk space or disk quota.
svn path=/trunk/; revision=2948
2001-01-28 09:13:10 +00:00
|
|
|
GList **flp;
|
|
|
|
GList *fl_ent;
|
|
|
|
filter_def *filt;
|
2001-02-03 06:03:42 +00:00
|
|
|
int c;
|
|
|
|
char *filt_name, *filt_expr;
|
|
|
|
int filt_name_len, filt_expr_len;
|
|
|
|
int filt_name_index, filt_expr_index;
|
|
|
|
int line = 1;
|
2001-01-28 04:43:26 +00:00
|
|
|
|
Have separate capture and display filter lists; some filter dialog boxes
use the capture filter lists, and others use the display filter list, as
appropriate.
Have separate menu items for editing the capture and display filter
lists.
Have separate "~/.ethereal/cfilters" and "~/.ethereal/dfilters" files
for the two lists; if either of those files isn't found, we try
"~/.ethereal/filters", which means that you will start out with two
identical lists holding all your filters - if certain filters belong
only in one list, you'll have to delete them by hand from the other
list.
Do I/O error checking when reading and writing filter lists; when
writing a filter list, write it to a new file, and then rename the new
file on top of the old file, so that you don't lose your old filter list
if, for example, you run out of disk space or disk quota.
svn path=/trunk/; revision=2948
2001-01-28 09:13:10 +00:00
|
|
|
*pref_path_return = NULL; /* assume no error */
|
|
|
|
|
|
|
|
switch (list) {
|
|
|
|
|
|
|
|
case CFILTER_LIST:
|
|
|
|
ff_name = CFILTER_FILE_NAME;
|
|
|
|
flp = &capture_filters;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DFILTER_LIST:
|
|
|
|
ff_name = DFILTER_FILE_NAME;
|
|
|
|
flp = &display_filters;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
g_assert_not_reached();
|
|
|
|
return;
|
2001-01-28 04:52:29 +00:00
|
|
|
}
|
|
|
|
|
2001-01-28 04:43:26 +00:00
|
|
|
/* To do: generalize this */
|
2001-10-24 07:18:39 +00:00
|
|
|
ff_path = get_persconffile_path(ff_name, FALSE);
|
2001-01-28 04:43:26 +00:00
|
|
|
if ((ff = fopen(ff_path, "r")) == NULL) {
|
Have separate capture and display filter lists; some filter dialog boxes
use the capture filter lists, and others use the display filter list, as
appropriate.
Have separate menu items for editing the capture and display filter
lists.
Have separate "~/.ethereal/cfilters" and "~/.ethereal/dfilters" files
for the two lists; if either of those files isn't found, we try
"~/.ethereal/filters", which means that you will start out with two
identical lists holding all your filters - if certain filters belong
only in one list, you'll have to delete them by hand from the other
list.
Do I/O error checking when reading and writing filter lists; when
writing a filter list, write it to a new file, and then rename the new
file on top of the old file, so that you don't lose your old filter list
if, for example, you run out of disk space or disk quota.
svn path=/trunk/; revision=2948
2001-01-28 09:13:10 +00:00
|
|
|
/*
|
|
|
|
* Did that fail because we the file didn't exist?
|
|
|
|
*/
|
|
|
|
if (errno != ENOENT) {
|
|
|
|
/*
|
|
|
|
* No. Just give up.
|
|
|
|
*/
|
|
|
|
*pref_path_return = ff_path;
|
|
|
|
*errno_return = errno;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Yes. See if there's a "filters" file; if so, read it.
|
|
|
|
* This means that a user will start out with their capture and
|
|
|
|
* display filter lists being identical; each list may contain
|
|
|
|
* filters that don't belong in that list. The user can edit
|
|
|
|
* the filter lists, and delete the ones that don't belong in
|
|
|
|
* a particular list.
|
|
|
|
*/
|
2001-10-24 07:18:39 +00:00
|
|
|
g_free(ff_path);
|
|
|
|
ff_path = get_persconffile_path(FILTER_FILE_NAME, FALSE);
|
Have separate capture and display filter lists; some filter dialog boxes
use the capture filter lists, and others use the display filter list, as
appropriate.
Have separate menu items for editing the capture and display filter
lists.
Have separate "~/.ethereal/cfilters" and "~/.ethereal/dfilters" files
for the two lists; if either of those files isn't found, we try
"~/.ethereal/filters", which means that you will start out with two
identical lists holding all your filters - if certain filters belong
only in one list, you'll have to delete them by hand from the other
list.
Do I/O error checking when reading and writing filter lists; when
writing a filter list, write it to a new file, and then rename the new
file on top of the old file, so that you don't lose your old filter list
if, for example, you run out of disk space or disk quota.
svn path=/trunk/; revision=2948
2001-01-28 09:13:10 +00:00
|
|
|
if ((ff = fopen(ff_path, "r")) == NULL) {
|
|
|
|
/*
|
|
|
|
* Well, that didn't work, either. Just give up.
|
|
|
|
* Return an error if the file existed but we couldn't open it.
|
|
|
|
*/
|
|
|
|
if (errno != ENOENT) {
|
|
|
|
*pref_path_return = ff_path;
|
|
|
|
*errno_return = errno;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we already have a list of filters, discard it. */
|
|
|
|
if (*flp != NULL) {
|
|
|
|
fl_ent = g_list_first(*flp);
|
|
|
|
while (fl_ent != NULL) {
|
|
|
|
filt = (filter_def *) fl_ent->data;
|
|
|
|
g_free(filt->name);
|
|
|
|
g_free(filt->strval);
|
|
|
|
g_free(filt);
|
|
|
|
fl_ent = fl_ent->next;
|
|
|
|
}
|
|
|
|
g_list_free(*flp);
|
|
|
|
*flp = NULL;
|
2001-01-28 04:43:26 +00:00
|
|
|
}
|
|
|
|
|
2001-02-03 06:03:42 +00:00
|
|
|
/* Allocate the filter name buffer. */
|
|
|
|
filt_name_len = INIT_BUF_SIZE;
|
|
|
|
filt_name = g_malloc(filt_name_len + 1);
|
|
|
|
filt_expr_len = INIT_BUF_SIZE;
|
|
|
|
filt_expr = g_malloc(filt_expr_len + 1);
|
|
|
|
|
2001-02-03 06:10:11 +00:00
|
|
|
for (line = 1; ; line++) {
|
2001-02-03 06:03:42 +00:00
|
|
|
/* Lines in a filter file are of the form
|
|
|
|
|
|
|
|
"name" expression
|
|
|
|
|
|
|
|
where "name" is a name, in quotes - backslashes in the name
|
|
|
|
escape the next character, so quotes and backslashes can appear
|
|
|
|
in the name - and "expression" is a filter expression, not in
|
|
|
|
quotes, running to the end of the line. */
|
|
|
|
|
|
|
|
/* Skip over leading white space, if any. */
|
|
|
|
while ((c = getc(ff)) != EOF && isspace(c)) {
|
|
|
|
if (c == '\n') {
|
|
|
|
/* Blank line. */
|
|
|
|
continue;
|
|
|
|
}
|
2001-01-28 04:43:26 +00:00
|
|
|
}
|
2001-02-03 06:03:42 +00:00
|
|
|
|
|
|
|
if (c == EOF)
|
|
|
|
break; /* Nothing more to read */
|
|
|
|
|
|
|
|
/* "c" is the first non-white-space character.
|
|
|
|
If it's not a quote, it's an error. */
|
|
|
|
if (c != '"') {
|
|
|
|
g_warning("'%s' line %d doesn't have a quoted filter name.", ff_path,
|
|
|
|
line);
|
|
|
|
while (c != '\n')
|
|
|
|
c = getc(ff); /* skip to the end of the line */
|
2001-01-28 04:43:26 +00:00
|
|
|
continue;
|
|
|
|
}
|
2001-02-03 06:03:42 +00:00
|
|
|
|
|
|
|
/* Get the name of the filter. */
|
|
|
|
filt_name_index = 0;
|
|
|
|
for (;;) {
|
|
|
|
c = getc(ff);
|
|
|
|
if (c == EOF || c == '\n')
|
|
|
|
break; /* End of line - or end of file */
|
|
|
|
if (c == '"') {
|
|
|
|
/* Closing quote. */
|
|
|
|
if (filt_name_index >= filt_name_len) {
|
|
|
|
/* Filter name buffer isn't long enough; double its length. */
|
|
|
|
filt_name_len *= 2;
|
|
|
|
filt_name = g_realloc(filt_name, filt_name_len + 1);
|
|
|
|
}
|
|
|
|
filt_name[filt_name_index] = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (c == '\\') {
|
|
|
|
/* Next character is escaped */
|
|
|
|
c = getc(ff);
|
|
|
|
if (c == EOF || c == '\n')
|
|
|
|
break; /* End of line - or end of file */
|
|
|
|
}
|
|
|
|
/* Add this character to the filter name string. */
|
|
|
|
if (filt_name_index >= filt_name_len) {
|
|
|
|
/* Filter name buffer isn't long enough; double its length. */
|
|
|
|
filt_name_len *= 2;
|
|
|
|
filt_name = g_realloc(filt_name, filt_name_len + 1);
|
|
|
|
}
|
|
|
|
filt_name[filt_name_index] = c;
|
|
|
|
filt_name_index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c == EOF) {
|
|
|
|
if (!ferror(ff)) {
|
|
|
|
/* EOF, not error; no newline seen before EOF */
|
|
|
|
g_warning("'%s' line %d doesn't have a newline.", ff_path,
|
|
|
|
line);
|
|
|
|
}
|
|
|
|
break; /* nothing more to read */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c != '"') {
|
|
|
|
/* No newline seen before end-of-line */
|
|
|
|
g_warning("'%s' line %d doesn't have a closing quote.", ff_path,
|
|
|
|
line);
|
2001-01-28 04:43:26 +00:00
|
|
|
continue;
|
|
|
|
}
|
2001-02-03 06:03:42 +00:00
|
|
|
|
|
|
|
/* Skip over separating white space, if any. */
|
|
|
|
while ((c = getc(ff)) != EOF && isspace(c)) {
|
2001-02-03 06:10:11 +00:00
|
|
|
if (c == '\n')
|
|
|
|
break;
|
2001-02-03 06:03:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (c == EOF) {
|
|
|
|
if (!ferror(ff)) {
|
|
|
|
/* EOF, not error; no newline seen before EOF */
|
|
|
|
g_warning("'%s' line %d doesn't have a newline.", ff_path,
|
|
|
|
line);
|
|
|
|
}
|
|
|
|
break; /* nothing more to read */
|
|
|
|
}
|
|
|
|
|
2001-02-03 06:10:11 +00:00
|
|
|
if (c == '\n') {
|
|
|
|
/* No filter expression */
|
|
|
|
g_warning("'%s' line %d doesn't have a filter expression.", ff_path,
|
|
|
|
line);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2001-02-03 06:03:42 +00:00
|
|
|
/* "c" is the first non-white-space character; it's the first
|
|
|
|
character of the filter expression. */
|
|
|
|
filt_expr_index = 0;
|
|
|
|
for (;;) {
|
|
|
|
/* Add this character to the filter expression string. */
|
|
|
|
if (filt_expr_index >= filt_expr_len) {
|
|
|
|
/* Filter expressioin buffer isn't long enough; double its length. */
|
|
|
|
filt_expr_len *= 2;
|
|
|
|
filt_expr = g_realloc(filt_expr, filt_expr_len + 1);
|
|
|
|
}
|
|
|
|
filt_expr[filt_expr_index] = c;
|
|
|
|
filt_expr_index++;
|
|
|
|
|
|
|
|
/* Get the next character. */
|
|
|
|
c = getc(ff);
|
|
|
|
if (c == EOF || c == '\n')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c == EOF) {
|
|
|
|
if (!ferror(ff)) {
|
|
|
|
/* EOF, not error; no newline seen before EOF */
|
|
|
|
g_warning("'%s' line %d doesn't have a newline.", ff_path,
|
|
|
|
line);
|
|
|
|
}
|
|
|
|
break; /* nothing more to read */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We saw the ending newline; terminate the filter expression string */
|
|
|
|
if (filt_expr_index >= filt_expr_len) {
|
|
|
|
/* Filter expressioin buffer isn't long enough; double its length. */
|
|
|
|
filt_expr_len *= 2;
|
|
|
|
filt_expr = g_realloc(filt_expr, filt_expr_len + 1);
|
|
|
|
}
|
|
|
|
filt_expr[filt_expr_index] = '\0';
|
|
|
|
|
|
|
|
/* Add the new filter to the list of filters */
|
2001-01-28 04:43:26 +00:00
|
|
|
filt = (filter_def *) g_malloc(sizeof(filter_def));
|
2001-02-03 06:03:42 +00:00
|
|
|
filt->name = g_strdup(filt_name);
|
|
|
|
filt->strval = g_strdup(filt_expr);
|
Have separate capture and display filter lists; some filter dialog boxes
use the capture filter lists, and others use the display filter list, as
appropriate.
Have separate menu items for editing the capture and display filter
lists.
Have separate "~/.ethereal/cfilters" and "~/.ethereal/dfilters" files
for the two lists; if either of those files isn't found, we try
"~/.ethereal/filters", which means that you will start out with two
identical lists holding all your filters - if certain filters belong
only in one list, you'll have to delete them by hand from the other
list.
Do I/O error checking when reading and writing filter lists; when
writing a filter list, write it to a new file, and then rename the new
file on top of the old file, so that you don't lose your old filter list
if, for example, you run out of disk space or disk quota.
svn path=/trunk/; revision=2948
2001-01-28 09:13:10 +00:00
|
|
|
*flp = g_list_append(*flp, filt);
|
2001-01-28 04:43:26 +00:00
|
|
|
}
|
Have separate capture and display filter lists; some filter dialog boxes
use the capture filter lists, and others use the display filter list, as
appropriate.
Have separate menu items for editing the capture and display filter
lists.
Have separate "~/.ethereal/cfilters" and "~/.ethereal/dfilters" files
for the two lists; if either of those files isn't found, we try
"~/.ethereal/filters", which means that you will start out with two
identical lists holding all your filters - if certain filters belong
only in one list, you'll have to delete them by hand from the other
list.
Do I/O error checking when reading and writing filter lists; when
writing a filter list, write it to a new file, and then rename the new
file on top of the old file, so that you don't lose your old filter list
if, for example, you run out of disk space or disk quota.
svn path=/trunk/; revision=2948
2001-01-28 09:13:10 +00:00
|
|
|
if (ferror(ff)) {
|
|
|
|
*pref_path_return = ff_path;
|
|
|
|
*errno_return = errno;
|
|
|
|
} else
|
|
|
|
g_free(ff_path);
|
2001-01-28 04:43:26 +00:00
|
|
|
fclose(ff);
|
2001-02-03 06:03:42 +00:00
|
|
|
g_free(filt_name);
|
|
|
|
g_free(filt_expr);
|
2001-01-28 04:43:26 +00:00
|
|
|
}
|
|
|
|
|
Have separate capture and display filter lists; some filter dialog boxes
use the capture filter lists, and others use the display filter list, as
appropriate.
Have separate menu items for editing the capture and display filter
lists.
Have separate "~/.ethereal/cfilters" and "~/.ethereal/dfilters" files
for the two lists; if either of those files isn't found, we try
"~/.ethereal/filters", which means that you will start out with two
identical lists holding all your filters - if certain filters belong
only in one list, you'll have to delete them by hand from the other
list.
Do I/O error checking when reading and writing filter lists; when
writing a filter list, write it to a new file, and then rename the new
file on top of the old file, so that you don't lose your old filter list
if, for example, you run out of disk space or disk quota.
svn path=/trunk/; revision=2948
2001-01-28 09:13:10 +00:00
|
|
|
/*
|
|
|
|
* Get a pointer to a list of filters.
|
|
|
|
*/
|
|
|
|
static GList **
|
|
|
|
get_filter_list(filter_list_type_t list)
|
|
|
|
{
|
|
|
|
GList **flp;
|
|
|
|
|
|
|
|
switch (list) {
|
|
|
|
|
|
|
|
case CFILTER_LIST:
|
|
|
|
flp = &capture_filters;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DFILTER_LIST:
|
|
|
|
flp = &display_filters;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
g_assert_not_reached();
|
|
|
|
flp = NULL;
|
|
|
|
}
|
|
|
|
return flp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get a pointer to the first entry in a filter list.
|
|
|
|
*/
|
|
|
|
GList *
|
|
|
|
get_filter_list_first(filter_list_type_t list)
|
|
|
|
{
|
|
|
|
GList **flp;
|
|
|
|
|
|
|
|
flp = get_filter_list(list);
|
|
|
|
return g_list_first(*flp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add a new filter to the end of a list.
|
|
|
|
* Returns a pointer to the newly-added entry.
|
|
|
|
*/
|
|
|
|
GList *
|
|
|
|
add_to_filter_list(filter_list_type_t list, char *name, char *expression)
|
|
|
|
{
|
|
|
|
GList **flp;
|
|
|
|
filter_def *filt;
|
|
|
|
|
|
|
|
flp = get_filter_list(list);
|
|
|
|
filt = (filter_def *) g_malloc(sizeof(filter_def));
|
|
|
|
filt->name = g_strdup(name);
|
|
|
|
filt->strval = g_strdup(expression);
|
|
|
|
*flp = g_list_append(*flp, filt);
|
|
|
|
return g_list_last(*flp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove a filter from a list.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
remove_from_filter_list(filter_list_type_t list, GList *fl_entry)
|
|
|
|
{
|
|
|
|
GList **flp;
|
|
|
|
filter_def *filt;
|
|
|
|
|
|
|
|
flp = get_filter_list(list);
|
|
|
|
filt = (filter_def *) fl_entry->data;
|
|
|
|
g_free(filt->name);
|
|
|
|
g_free(filt->strval);
|
|
|
|
g_free(filt);
|
|
|
|
*flp = g_list_remove_link(*flp, fl_entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write out a list of filters.
|
|
|
|
*
|
|
|
|
* On success, "*pref_path_return" is set to NULL.
|
|
|
|
* On error, "*pref_path_return" is set to point to the pathname of
|
|
|
|
* the file we tried to read - it should be freed by our caller -
|
|
|
|
* and "*errno_return" is set to the error.
|
|
|
|
*/
|
2001-01-28 04:43:26 +00:00
|
|
|
void
|
Have separate capture and display filter lists; some filter dialog boxes
use the capture filter lists, and others use the display filter list, as
appropriate.
Have separate menu items for editing the capture and display filter
lists.
Have separate "~/.ethereal/cfilters" and "~/.ethereal/dfilters" files
for the two lists; if either of those files isn't found, we try
"~/.ethereal/filters", which means that you will start out with two
identical lists holding all your filters - if certain filters belong
only in one list, you'll have to delete them by hand from the other
list.
Do I/O error checking when reading and writing filter lists; when
writing a filter list, write it to a new file, and then rename the new
file on top of the old file, so that you don't lose your old filter list
if, for example, you run out of disk space or disk quota.
svn path=/trunk/; revision=2948
2001-01-28 09:13:10 +00:00
|
|
|
save_filter_list(filter_list_type_t list, char **pref_path_return,
|
|
|
|
int *errno_return)
|
2001-01-28 04:43:26 +00:00
|
|
|
{
|
2001-10-22 22:59:26 +00:00
|
|
|
gchar *ff_path, *ff_path_new, *ff_name;
|
Have separate capture and display filter lists; some filter dialog boxes
use the capture filter lists, and others use the display filter list, as
appropriate.
Have separate menu items for editing the capture and display filter
lists.
Have separate "~/.ethereal/cfilters" and "~/.ethereal/dfilters" files
for the two lists; if either of those files isn't found, we try
"~/.ethereal/filters", which means that you will start out with two
identical lists holding all your filters - if certain filters belong
only in one list, you'll have to delete them by hand from the other
list.
Do I/O error checking when reading and writing filter lists; when
writing a filter list, write it to a new file, and then rename the new
file on top of the old file, so that you don't lose your old filter list
if, for example, you run out of disk space or disk quota.
svn path=/trunk/; revision=2948
2001-01-28 09:13:10 +00:00
|
|
|
GList *fl;
|
2001-01-28 04:52:29 +00:00
|
|
|
GList *flp;
|
|
|
|
filter_def *filt;
|
|
|
|
FILE *ff;
|
2001-02-03 06:03:42 +00:00
|
|
|
guchar *p, c;
|
2001-01-28 04:43:26 +00:00
|
|
|
|
Have separate capture and display filter lists; some filter dialog boxes
use the capture filter lists, and others use the display filter list, as
appropriate.
Have separate menu items for editing the capture and display filter
lists.
Have separate "~/.ethereal/cfilters" and "~/.ethereal/dfilters" files
for the two lists; if either of those files isn't found, we try
"~/.ethereal/filters", which means that you will start out with two
identical lists holding all your filters - if certain filters belong
only in one list, you'll have to delete them by hand from the other
list.
Do I/O error checking when reading and writing filter lists; when
writing a filter list, write it to a new file, and then rename the new
file on top of the old file, so that you don't lose your old filter list
if, for example, you run out of disk space or disk quota.
svn path=/trunk/; revision=2948
2001-01-28 09:13:10 +00:00
|
|
|
*pref_path_return = NULL; /* assume no error */
|
|
|
|
|
|
|
|
switch (list) {
|
|
|
|
|
|
|
|
case CFILTER_LIST:
|
|
|
|
ff_name = CFILTER_FILE_NAME;
|
|
|
|
fl = capture_filters;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DFILTER_LIST:
|
|
|
|
ff_name = DFILTER_FILE_NAME;
|
|
|
|
fl = display_filters;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
g_assert_not_reached();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-10-24 07:18:39 +00:00
|
|
|
ff_path = get_persconffile_path(ff_name, TRUE);
|
2001-01-28 04:43:26 +00:00
|
|
|
|
Have separate capture and display filter lists; some filter dialog boxes
use the capture filter lists, and others use the display filter list, as
appropriate.
Have separate menu items for editing the capture and display filter
lists.
Have separate "~/.ethereal/cfilters" and "~/.ethereal/dfilters" files
for the two lists; if either of those files isn't found, we try
"~/.ethereal/filters", which means that you will start out with two
identical lists holding all your filters - if certain filters belong
only in one list, you'll have to delete them by hand from the other
list.
Do I/O error checking when reading and writing filter lists; when
writing a filter list, write it to a new file, and then rename the new
file on top of the old file, so that you don't lose your old filter list
if, for example, you run out of disk space or disk quota.
svn path=/trunk/; revision=2948
2001-01-28 09:13:10 +00:00
|
|
|
/* Write to "XXX.new", and rename if that succeeds.
|
|
|
|
That means we don't trash the file if we fail to write it out
|
|
|
|
completely. */
|
2001-10-24 07:18:39 +00:00
|
|
|
ff_path_new = (gchar *) g_malloc(strlen(ff_path) + 5);
|
2001-10-22 22:59:26 +00:00
|
|
|
sprintf(ff_path_new, "%s.new", ff_path);
|
Have separate capture and display filter lists; some filter dialog boxes
use the capture filter lists, and others use the display filter list, as
appropriate.
Have separate menu items for editing the capture and display filter
lists.
Have separate "~/.ethereal/cfilters" and "~/.ethereal/dfilters" files
for the two lists; if either of those files isn't found, we try
"~/.ethereal/filters", which means that you will start out with two
identical lists holding all your filters - if certain filters belong
only in one list, you'll have to delete them by hand from the other
list.
Do I/O error checking when reading and writing filter lists; when
writing a filter list, write it to a new file, and then rename the new
file on top of the old file, so that you don't lose your old filter list
if, for example, you run out of disk space or disk quota.
svn path=/trunk/; revision=2948
2001-01-28 09:13:10 +00:00
|
|
|
|
|
|
|
if ((ff = fopen(ff_path_new, "w")) == NULL) {
|
|
|
|
*pref_path_return = ff_path;
|
|
|
|
*errno_return = errno;
|
|
|
|
g_free(ff_path_new);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
flp = g_list_first(fl);
|
|
|
|
while (flp) {
|
|
|
|
filt = (filter_def *) flp->data;
|
2001-02-03 06:03:42 +00:00
|
|
|
|
|
|
|
/* Write out the filter name as a quoted string; escape any quotes
|
|
|
|
or backslashes. */
|
|
|
|
putc('"', ff);
|
|
|
|
for (p = (guchar *)filt->name; (c = *p) != '\0'; p++) {
|
|
|
|
if (c == '"' || c == '\\')
|
|
|
|
putc('\\', ff);
|
|
|
|
putc(c, ff);
|
|
|
|
}
|
|
|
|
putc('"', ff);
|
|
|
|
|
|
|
|
/* Separate the filter name and value with a space. */
|
|
|
|
putc(' ', ff);
|
|
|
|
|
|
|
|
/* Write out the filter expression and a newline. */
|
|
|
|
fprintf(ff, "%s\n", filt->strval);
|
Have separate capture and display filter lists; some filter dialog boxes
use the capture filter lists, and others use the display filter list, as
appropriate.
Have separate menu items for editing the capture and display filter
lists.
Have separate "~/.ethereal/cfilters" and "~/.ethereal/dfilters" files
for the two lists; if either of those files isn't found, we try
"~/.ethereal/filters", which means that you will start out with two
identical lists holding all your filters - if certain filters belong
only in one list, you'll have to delete them by hand from the other
list.
Do I/O error checking when reading and writing filter lists; when
writing a filter list, write it to a new file, and then rename the new
file on top of the old file, so that you don't lose your old filter list
if, for example, you run out of disk space or disk quota.
svn path=/trunk/; revision=2948
2001-01-28 09:13:10 +00:00
|
|
|
if (ferror(ff)) {
|
|
|
|
*pref_path_return = ff_path;
|
|
|
|
*errno_return = errno;
|
|
|
|
fclose(ff);
|
|
|
|
unlink(ff_path_new);
|
|
|
|
g_free(ff_path_new);
|
|
|
|
return;
|
2001-01-28 04:43:26 +00:00
|
|
|
}
|
Have separate capture and display filter lists; some filter dialog boxes
use the capture filter lists, and others use the display filter list, as
appropriate.
Have separate menu items for editing the capture and display filter
lists.
Have separate "~/.ethereal/cfilters" and "~/.ethereal/dfilters" files
for the two lists; if either of those files isn't found, we try
"~/.ethereal/filters", which means that you will start out with two
identical lists holding all your filters - if certain filters belong
only in one list, you'll have to delete them by hand from the other
list.
Do I/O error checking when reading and writing filter lists; when
writing a filter list, write it to a new file, and then rename the new
file on top of the old file, so that you don't lose your old filter list
if, for example, you run out of disk space or disk quota.
svn path=/trunk/; revision=2948
2001-01-28 09:13:10 +00:00
|
|
|
flp = flp->next;
|
|
|
|
}
|
|
|
|
if (fclose(ff) == EOF) {
|
|
|
|
*pref_path_return = ff_path;
|
|
|
|
*errno_return = errno;
|
|
|
|
unlink(ff_path_new);
|
|
|
|
g_free(ff_path_new);
|
|
|
|
return;
|
2001-01-28 04:43:26 +00:00
|
|
|
}
|
|
|
|
|
2001-03-15 09:50:39 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
/* ANSI C doesn't say whether "rename()" removes the target if it
|
|
|
|
exists; the Win32 call to rename files doesn't do so, which I
|
|
|
|
infer is the reason why the MSVC++ "rename()" doesn't do so.
|
|
|
|
We must therefore remove the target file first, on Windows. */
|
|
|
|
if (remove(ff_path) < 0 && errno != ENOENT) {
|
|
|
|
/* It failed for some reason other than "it's not there"; if
|
|
|
|
it's not there, we don't need to remove it, so we just
|
|
|
|
drive on. */
|
|
|
|
*pref_path_return = ff_path;
|
|
|
|
*errno_return = errno;
|
|
|
|
unlink(ff_path_new);
|
|
|
|
g_free(ff_path_new);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
Have separate capture and display filter lists; some filter dialog boxes
use the capture filter lists, and others use the display filter list, as
appropriate.
Have separate menu items for editing the capture and display filter
lists.
Have separate "~/.ethereal/cfilters" and "~/.ethereal/dfilters" files
for the two lists; if either of those files isn't found, we try
"~/.ethereal/filters", which means that you will start out with two
identical lists holding all your filters - if certain filters belong
only in one list, you'll have to delete them by hand from the other
list.
Do I/O error checking when reading and writing filter lists; when
writing a filter list, write it to a new file, and then rename the new
file on top of the old file, so that you don't lose your old filter list
if, for example, you run out of disk space or disk quota.
svn path=/trunk/; revision=2948
2001-01-28 09:13:10 +00:00
|
|
|
if (rename(ff_path_new, ff_path) < 0) {
|
|
|
|
*pref_path_return = ff_path;
|
|
|
|
*errno_return = errno;
|
|
|
|
unlink(ff_path_new);
|
|
|
|
g_free(ff_path_new);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
g_free(ff_path_new);
|
2001-01-28 04:43:26 +00:00
|
|
|
g_free(ff_path);
|
|
|
|
}
|