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,
91 char *filt_name, *filt_expr;
92 int filt_name_len, filt_expr_len;
93 int filt_name_index, filt_expr_index;
96 *pref_path_return = NULL; /* assume no error */
101 ff_name = CFILTER_FILE_NAME;
102 flp = &capture_filters;
106 ff_name = DFILTER_FILE_NAME;
107 flp = &display_filters;
111 g_assert_not_reached();
115 /* try to open personal "cfilters"/"dfilters" file */
116 ff_path = get_persconffile_path(ff_name, FALSE);
117 if ((ff = fopen(ff_path, "r")) == NULL) {
119 * Did that fail because the file didn't exist?
121 if (errno != ENOENT) {
125 *pref_path_return = ff_path;
126 *errno_return = errno;
131 * Yes. See if there's an "old style" personal "filters" file; if so, read it.
132 * This means that a user will start out with their capture and
133 * display filter lists being identical; each list may contain
134 * filters that don't belong in that list. The user can edit
135 * the filter lists, and delete the ones that don't belong in
139 ff_path = get_persconffile_path(FILTER_FILE_NAME, FALSE);
140 if ((ff = fopen(ff_path, "r")) == NULL) {
142 * Did that fail because the file didn't exist?
144 if (errno != ENOENT) {
148 *pref_path_return = ff_path;
149 *errno_return = errno;
154 * Try to open the global "cfilters/dfilters" file */
155 ff_path = get_datafile_path(ff_name);
156 if ((ff = fopen(ff_path, "r")) == NULL) {
159 * Well, that didn't work, either. Just give up.
160 * Return an error if the file existed but we couldn't open it.
162 if (errno != ENOENT) {
163 *pref_path_return = ff_path;
164 *errno_return = errno;
171 /* If we already have a list of filters, discard it. */
173 fl_ent = g_list_first(*flp);
174 while (fl_ent != NULL) {
175 filt = (filter_def *) fl_ent->data;
177 g_free(filt->strval);
179 fl_ent = fl_ent->next;
185 /* Allocate the filter name buffer. */
186 filt_name_len = INIT_BUF_SIZE;
187 filt_name = g_malloc(filt_name_len + 1);
188 filt_expr_len = INIT_BUF_SIZE;
189 filt_expr = g_malloc(filt_expr_len + 1);
191 for (line = 1; ; line++) {
192 /* Lines in a filter file are of the form
196 where "name" is a name, in quotes - backslashes in the name
197 escape the next character, so quotes and backslashes can appear
198 in the name - and "expression" is a filter expression, not in
199 quotes, running to the end of the line. */
201 /* Skip over leading white space, if any. */
202 while ((c = getc(ff)) != EOF && isspace(c)) {
210 break; /* Nothing more to read */
212 /* "c" is the first non-white-space character.
213 If it's not a quote, it's an error. */
215 g_warning("'%s' line %d doesn't have a quoted filter name.", ff_path,
218 c = getc(ff); /* skip to the end of the line */
222 /* Get the name of the filter. */
226 if (c == EOF || c == '\n')
227 break; /* End of line - or end of file */
230 if (filt_name_index >= filt_name_len) {
231 /* Filter name buffer isn't long enough; double its length. */
233 filt_name = g_realloc(filt_name, filt_name_len + 1);
235 filt_name[filt_name_index] = '\0';
239 /* Next character is escaped */
241 if (c == EOF || c == '\n')
242 break; /* End of line - or end of file */
244 /* Add this character to the filter name string. */
245 if (filt_name_index >= filt_name_len) {
246 /* Filter name buffer isn't long enough; double its length. */
248 filt_name = g_realloc(filt_name, filt_name_len + 1);
250 filt_name[filt_name_index] = c;
256 /* EOF, not error; no newline seen before EOF */
257 g_warning("'%s' line %d doesn't have a newline.", ff_path,
260 break; /* nothing more to read */
264 /* No newline seen before end-of-line */
265 g_warning("'%s' line %d doesn't have a closing quote.", ff_path,
270 /* Skip over separating white space, if any. */
271 while ((c = getc(ff)) != EOF && isspace(c)) {
278 /* EOF, not error; no newline seen before EOF */
279 g_warning("'%s' line %d doesn't have a newline.", ff_path,
282 break; /* nothing more to read */
286 /* No filter expression */
287 g_warning("'%s' line %d doesn't have a filter expression.", ff_path,
292 /* "c" is the first non-white-space character; it's the first
293 character of the filter expression. */
296 /* Add this character to the filter expression string. */
297 if (filt_expr_index >= filt_expr_len) {
298 /* Filter expressioin buffer isn't long enough; double its length. */
300 filt_expr = g_realloc(filt_expr, filt_expr_len + 1);
302 filt_expr[filt_expr_index] = c;
305 /* Get the next character. */
307 if (c == EOF || c == '\n')
313 /* EOF, not error; no newline seen before EOF */
314 g_warning("'%s' line %d doesn't have a newline.", ff_path,
317 break; /* nothing more to read */
320 /* We saw the ending newline; terminate the filter expression string */
321 if (filt_expr_index >= filt_expr_len) {
322 /* Filter expressioin buffer isn't long enough; double its length. */
324 filt_expr = g_realloc(filt_expr, filt_expr_len + 1);
326 filt_expr[filt_expr_index] = '\0';
328 /* Add the new filter to the list of filters */
329 filt = (filter_def *) g_malloc(sizeof(filter_def));
330 filt->name = g_strdup(filt_name);
331 filt->strval = g_strdup(filt_expr);
332 *flp = g_list_append(*flp, filt);
335 *pref_path_return = ff_path;
336 *errno_return = errno;
345 * Get a pointer to a list of filters.
348 get_filter_list(filter_list_type_t list)
355 flp = &capture_filters;
359 flp = &display_filters;
363 g_assert_not_reached();
370 * Get a pointer to the first entry in a filter list.
373 get_filter_list_first(filter_list_type_t list)
377 flp = get_filter_list(list);
378 return g_list_first(*flp);
382 * Add a new filter to the end of a list.
383 * Returns a pointer to the newly-added entry.
386 add_to_filter_list(filter_list_type_t list, const char *name,
387 const char *expression)
392 flp = get_filter_list(list);
393 filt = (filter_def *) g_malloc(sizeof(filter_def));
394 filt->name = g_strdup(name);
395 filt->strval = g_strdup(expression);
396 *flp = g_list_append(*flp, filt);
397 return g_list_last(*flp);
401 * Remove a filter from a list.
404 remove_from_filter_list(filter_list_type_t list, GList *fl_entry)
409 flp = get_filter_list(list);
410 filt = (filter_def *) fl_entry->data;
412 g_free(filt->strval);
414 *flp = g_list_remove_link(*flp, fl_entry);
418 * Write out a list of filters.
420 * On success, "*pref_path_return" is set to NULL.
421 * On error, "*pref_path_return" is set to point to the pathname of
422 * the file we tried to read - it should be freed by our caller -
423 * and "*errno_return" is set to the error.
426 save_filter_list(filter_list_type_t list, char **pref_path_return,
429 const gchar *ff_name;
430 gchar *ff_path, *ff_path_new;
437 *pref_path_return = NULL; /* assume no error */
442 ff_name = CFILTER_FILE_NAME;
443 fl = capture_filters;
447 ff_name = DFILTER_FILE_NAME;
448 fl = display_filters;
452 g_assert_not_reached();
456 ff_path = get_persconffile_path(ff_name, TRUE);
458 /* Write to "XXX.new", and rename if that succeeds.
459 That means we don't trash the file if we fail to write it out
461 ff_path_new = (gchar *) g_malloc(strlen(ff_path) + 5);
462 sprintf(ff_path_new, "%s.new", ff_path);
464 if ((ff = fopen(ff_path_new, "w")) == NULL) {
465 *pref_path_return = ff_path;
466 *errno_return = errno;
470 flp = g_list_first(fl);
472 filt = (filter_def *) flp->data;
474 /* Write out the filter name as a quoted string; escape any quotes
477 for (p = (guchar *)filt->name; (c = *p) != '\0'; p++) {
478 if (c == '"' || c == '\\')
484 /* Separate the filter name and value with a space. */
487 /* Write out the filter expression and a newline. */
488 fprintf(ff, "%s\n", filt->strval);
490 *pref_path_return = ff_path;
491 *errno_return = errno;
499 if (fclose(ff) == EOF) {
500 *pref_path_return = ff_path;
501 *errno_return = errno;
508 /* ANSI C doesn't say whether "rename()" removes the target if it
509 exists; the Win32 call to rename files doesn't do so, which I
510 infer is the reason why the MSVC++ "rename()" doesn't do so.
511 We must therefore remove the target file first, on Windows. */
512 if (remove(ff_path) < 0 && errno != ENOENT) {
513 /* It failed for some reason other than "it's not there"; if
514 it's not there, we don't need to remove it, so we just
516 *pref_path_return = ff_path;
517 *errno_return = errno;
524 if (rename(ff_path_new, ff_path) < 0) {
525 *pref_path_return = ff_path;
526 *errno_return = errno;