2 * Code for reading and writing the filters file.
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@ethereal.com>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
40 #include <epan/filesystem.h>
45 * Old filter file name.
47 #define FILTER_FILE_NAME "filters"
50 * Capture filter file name.
52 #define CFILTER_FILE_NAME "cfilters"
55 * Display filter file name.
57 #define DFILTER_FILE_NAME "dfilters"
60 * List of capture filters.
62 static GList *capture_filters = NULL;
65 * List of display filters.
67 static GList *display_filters = NULL;
70 * Read in a list of filters.
72 * On success, "*pref_path_return" is set to NULL.
73 * On error, "*pref_path_return" is set to point to the pathname of
74 * the file we tried to read - it should be freed by our caller -
75 * and "*errno_return" is set to the error.
78 #define INIT_BUF_SIZE 128
81 read_filter_list(filter_list_type_t list, char **pref_path_return,
84 char *ff_path, *ff_name;
90 char *filt_name, *filt_expr;
91 int filt_name_len, filt_expr_len;
92 int filt_name_index, filt_expr_index;
95 *pref_path_return = NULL; /* assume no error */
100 ff_name = CFILTER_FILE_NAME;
101 flp = &capture_filters;
105 ff_name = DFILTER_FILE_NAME;
106 flp = &display_filters;
110 g_assert_not_reached();
114 /* To do: generalize this */
115 ff_path = get_persconffile_path(ff_name, FALSE);
116 if ((ff = fopen(ff_path, "r")) == NULL) {
118 * Did that fail because we the file didn't exist?
120 if (errno != ENOENT) {
124 *pref_path_return = ff_path;
125 *errno_return = errno;
130 * Yes. See if there's a "filters" file; if so, read it.
131 * This means that a user will start out with their capture and
132 * display filter lists being identical; each list may contain
133 * filters that don't belong in that list. The user can edit
134 * the filter lists, and delete the ones that don't belong in
138 ff_path = get_persconffile_path(FILTER_FILE_NAME, FALSE);
139 if ((ff = fopen(ff_path, "r")) == NULL) {
141 * Well, that didn't work, either. Just give up.
142 * Return an error if the file existed but we couldn't open it.
144 if (errno != ENOENT) {
145 *pref_path_return = ff_path;
146 *errno_return = errno;
152 /* If we already have a list of filters, discard it. */
154 fl_ent = g_list_first(*flp);
155 while (fl_ent != NULL) {
156 filt = (filter_def *) fl_ent->data;
158 g_free(filt->strval);
160 fl_ent = fl_ent->next;
166 /* Allocate the filter name buffer. */
167 filt_name_len = INIT_BUF_SIZE;
168 filt_name = g_malloc(filt_name_len + 1);
169 filt_expr_len = INIT_BUF_SIZE;
170 filt_expr = g_malloc(filt_expr_len + 1);
172 for (line = 1; ; line++) {
173 /* Lines in a filter file are of the form
177 where "name" is a name, in quotes - backslashes in the name
178 escape the next character, so quotes and backslashes can appear
179 in the name - and "expression" is a filter expression, not in
180 quotes, running to the end of the line. */
182 /* Skip over leading white space, if any. */
183 while ((c = getc(ff)) != EOF && isspace(c)) {
191 break; /* Nothing more to read */
193 /* "c" is the first non-white-space character.
194 If it's not a quote, it's an error. */
196 g_warning("'%s' line %d doesn't have a quoted filter name.", ff_path,
199 c = getc(ff); /* skip to the end of the line */
203 /* Get the name of the filter. */
207 if (c == EOF || c == '\n')
208 break; /* End of line - or end of file */
211 if (filt_name_index >= filt_name_len) {
212 /* Filter name buffer isn't long enough; double its length. */
214 filt_name = g_realloc(filt_name, filt_name_len + 1);
216 filt_name[filt_name_index] = '\0';
220 /* Next character is escaped */
222 if (c == EOF || c == '\n')
223 break; /* End of line - or end of file */
225 /* Add this character to the filter name string. */
226 if (filt_name_index >= filt_name_len) {
227 /* Filter name buffer isn't long enough; double its length. */
229 filt_name = g_realloc(filt_name, filt_name_len + 1);
231 filt_name[filt_name_index] = c;
237 /* EOF, not error; no newline seen before EOF */
238 g_warning("'%s' line %d doesn't have a newline.", ff_path,
241 break; /* nothing more to read */
245 /* No newline seen before end-of-line */
246 g_warning("'%s' line %d doesn't have a closing quote.", ff_path,
251 /* Skip over separating white space, if any. */
252 while ((c = getc(ff)) != EOF && isspace(c)) {
259 /* EOF, not error; no newline seen before EOF */
260 g_warning("'%s' line %d doesn't have a newline.", ff_path,
263 break; /* nothing more to read */
267 /* No filter expression */
268 g_warning("'%s' line %d doesn't have a filter expression.", ff_path,
273 /* "c" is the first non-white-space character; it's the first
274 character of the filter expression. */
277 /* Add this character to the filter expression string. */
278 if (filt_expr_index >= filt_expr_len) {
279 /* Filter expressioin buffer isn't long enough; double its length. */
281 filt_expr = g_realloc(filt_expr, filt_expr_len + 1);
283 filt_expr[filt_expr_index] = c;
286 /* Get the next character. */
288 if (c == EOF || c == '\n')
294 /* EOF, not error; no newline seen before EOF */
295 g_warning("'%s' line %d doesn't have a newline.", ff_path,
298 break; /* nothing more to read */
301 /* We saw the ending newline; terminate the filter expression string */
302 if (filt_expr_index >= filt_expr_len) {
303 /* Filter expressioin buffer isn't long enough; double its length. */
305 filt_expr = g_realloc(filt_expr, filt_expr_len + 1);
307 filt_expr[filt_expr_index] = '\0';
309 /* Add the new filter to the list of filters */
310 filt = (filter_def *) g_malloc(sizeof(filter_def));
311 filt->name = g_strdup(filt_name);
312 filt->strval = g_strdup(filt_expr);
313 *flp = g_list_append(*flp, filt);
316 *pref_path_return = ff_path;
317 *errno_return = errno;
326 * Get a pointer to a list of filters.
329 get_filter_list(filter_list_type_t list)
336 flp = &capture_filters;
340 flp = &display_filters;
344 g_assert_not_reached();
351 * Get a pointer to the first entry in a filter list.
354 get_filter_list_first(filter_list_type_t list)
358 flp = get_filter_list(list);
359 return g_list_first(*flp);
363 * Add a new filter to the end of a list.
364 * Returns a pointer to the newly-added entry.
367 add_to_filter_list(filter_list_type_t list, const char *name,
368 const char *expression)
373 flp = get_filter_list(list);
374 filt = (filter_def *) g_malloc(sizeof(filter_def));
375 filt->name = g_strdup(name);
376 filt->strval = g_strdup(expression);
377 *flp = g_list_append(*flp, filt);
378 return g_list_last(*flp);
382 * Remove a filter from a list.
385 remove_from_filter_list(filter_list_type_t list, GList *fl_entry)
390 flp = get_filter_list(list);
391 filt = (filter_def *) fl_entry->data;
393 g_free(filt->strval);
395 *flp = g_list_remove_link(*flp, fl_entry);
399 * Write out a list of filters.
401 * On success, "*pref_path_return" is set to NULL.
402 * On error, "*pref_path_return" is set to point to the pathname of
403 * the file we tried to read - it should be freed by our caller -
404 * and "*errno_return" is set to the error.
407 save_filter_list(filter_list_type_t list, char **pref_path_return,
410 gchar *ff_path, *ff_path_new, *ff_name;
417 *pref_path_return = NULL; /* assume no error */
422 ff_name = CFILTER_FILE_NAME;
423 fl = capture_filters;
427 ff_name = DFILTER_FILE_NAME;
428 fl = display_filters;
432 g_assert_not_reached();
436 ff_path = get_persconffile_path(ff_name, TRUE);
438 /* Write to "XXX.new", and rename if that succeeds.
439 That means we don't trash the file if we fail to write it out
441 ff_path_new = (gchar *) g_malloc(strlen(ff_path) + 5);
442 sprintf(ff_path_new, "%s.new", ff_path);
444 if ((ff = fopen(ff_path_new, "w")) == NULL) {
445 *pref_path_return = ff_path;
446 *errno_return = errno;
450 flp = g_list_first(fl);
452 filt = (filter_def *) flp->data;
454 /* Write out the filter name as a quoted string; escape any quotes
457 for (p = (guchar *)filt->name; (c = *p) != '\0'; p++) {
458 if (c == '"' || c == '\\')
464 /* Separate the filter name and value with a space. */
467 /* Write out the filter expression and a newline. */
468 fprintf(ff, "%s\n", filt->strval);
470 *pref_path_return = ff_path;
471 *errno_return = errno;
479 if (fclose(ff) == EOF) {
480 *pref_path_return = ff_path;
481 *errno_return = errno;
488 /* ANSI C doesn't say whether "rename()" removes the target if it
489 exists; the Win32 call to rename files doesn't do so, which I
490 infer is the reason why the MSVC++ "rename()" doesn't do so.
491 We must therefore remove the target file first, on Windows. */
492 if (remove(ff_path) < 0 && errno != ENOENT) {
493 /* It failed for some reason other than "it's not there"; if
494 it's not there, we don't need to remove it, so we just
496 *pref_path_return = ff_path;
497 *errno_return = errno;
504 if (rename(ff_path_new, ff_path) < 0) {
505 *pref_path_return = ff_path;
506 *errno_return = errno;