"read_filter_list()" and "save_filter_list()" mallocate the path string
[obnox/wireshark/wip.git] / filters.c
1 /* filters.c
2  * Code for reading and writing the filters file.
3  *
4  * $Id$
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
8  * Copyright 1998 Gerald Combs
9  *
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.
14  *
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.
19  *
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.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <stdio.h>
30 #include <string.h>
31 #include <ctype.h>
32 #include <errno.h>
33
34 #ifdef HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
37
38 #include <glib.h>
39
40 #include <epan/filesystem.h>
41
42 #include "filters.h"
43
44 /*
45  * Old filter file name.
46  */
47 #define FILTER_FILE_NAME        "filters"
48
49 /*
50  * Capture filter file name.
51  */
52 #define CFILTER_FILE_NAME       "cfilters"
53
54 /*
55  * Display filter file name.
56  */
57 #define DFILTER_FILE_NAME       "dfilters"
58
59 /*
60  * List of capture filters.
61  */
62 static GList *capture_filters = NULL;
63
64 /*
65  * List of display filters.
66  */
67 static GList *display_filters = NULL;
68
69 /*
70  * Read in a list of filters.
71  *
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.
76  */
77
78 #define INIT_BUF_SIZE   128
79
80 void
81 read_filter_list(filter_list_type_t list, char **pref_path_return,
82     int *errno_return)
83 {
84   const char *ff_name;
85   char       *ff_path;
86   FILE       *ff;
87   GList      **flp;
88   GList      *fl_ent;
89   filter_def *filt;
90   int         c;
91   char       *filt_name, *filt_expr;
92   int         filt_name_len, filt_expr_len;
93   int         filt_name_index, filt_expr_index;
94   int         line = 1;
95
96   *pref_path_return = NULL;     /* assume no error */
97
98   switch (list) {
99
100   case CFILTER_LIST:
101     ff_name = CFILTER_FILE_NAME;
102     flp = &capture_filters;
103     break;
104
105   case DFILTER_LIST:
106     ff_name = DFILTER_FILE_NAME;
107     flp = &display_filters;
108     break;
109
110   default:
111     g_assert_not_reached();
112     return;
113   }
114
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) {
118     /*
119      * Did that fail because the file didn't exist?
120      */
121     if (errno != ENOENT) {
122       /*
123        * No.  Just give up.
124        */
125       *pref_path_return = ff_path;
126       *errno_return = errno;
127       return;
128     }
129
130     /*
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
136      * a particular list.
137      */
138     g_free(ff_path);
139     ff_path = get_persconffile_path(FILTER_FILE_NAME, FALSE);
140     if ((ff = fopen(ff_path, "r")) == NULL) {
141     /*
142      * Did that fail because the file didn't exist?
143      */
144       if (errno != ENOENT) {
145       /*
146        * No.  Just give up.
147        */
148         *pref_path_return = ff_path;
149         *errno_return = errno;
150     return;
151       }
152
153     /*
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) {
157
158       /*
159        * Well, that didn't work, either.  Just give up.
160        * Return an error if the file existed but we couldn't open it.
161        */
162       if (errno != ENOENT) {
163         *pref_path_return = ff_path;
164         *errno_return = errno;
165     }
166     return;
167     }
168     }
169   }
170
171   /* If we already have a list of filters, discard it. */
172   if (*flp != NULL) {
173     fl_ent = g_list_first(*flp);
174     while (fl_ent != NULL) {
175       filt = (filter_def *) fl_ent->data;
176       g_free(filt->name);
177       g_free(filt->strval);
178       g_free(filt);
179       fl_ent = fl_ent->next;
180     }
181     g_list_free(*flp);
182     *flp = NULL;
183   }
184
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);
190
191   for (line = 1; ; line++) {
192     /* Lines in a filter file are of the form
193
194         "name" expression
195
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. */
200
201     /* Skip over leading white space, if any. */
202     while ((c = getc(ff)) != EOF && isspace(c)) {
203       if (c == '\n') {
204         /* Blank line. */
205         continue;
206       }
207     }
208
209     if (c == EOF)
210       break;    /* Nothing more to read */
211
212     /* "c" is the first non-white-space character.
213        If it's not a quote, it's an error. */
214     if (c != '"') {
215       g_warning("'%s' line %d doesn't have a quoted filter name.", ff_path,
216                 line);
217       while (c != '\n')
218         c = getc(ff);   /* skip to the end of the line */
219       continue;
220     }
221
222     /* Get the name of the filter. */
223     filt_name_index = 0;
224     for (;;) {
225       c = getc(ff);
226       if (c == EOF || c == '\n')
227         break;  /* End of line - or end of file */
228       if (c == '"') {
229         /* Closing quote. */
230         if (filt_name_index >= filt_name_len) {
231           /* Filter name buffer isn't long enough; double its length. */
232           filt_name_len *= 2;
233           filt_name = g_realloc(filt_name, filt_name_len + 1);
234         }
235         filt_name[filt_name_index] = '\0';
236         break;
237       }
238       if (c == '\\') {
239         /* Next character is escaped */
240         c = getc(ff);
241         if (c == EOF || c == '\n')
242           break;        /* End of line - or end of file */
243       }
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. */
247         filt_name_len *= 2;
248         filt_name = g_realloc(filt_name, filt_name_len + 1);
249       }
250       filt_name[filt_name_index] = c;
251       filt_name_index++;
252     }
253
254     if (c == EOF) {
255       if (!ferror(ff)) {
256         /* EOF, not error; no newline seen before EOF */
257         g_warning("'%s' line %d doesn't have a newline.", ff_path,
258                   line);
259       }
260       break;    /* nothing more to read */
261     }
262
263     if (c != '"') {
264       /* No newline seen before end-of-line */
265       g_warning("'%s' line %d doesn't have a closing quote.", ff_path,
266                 line);
267       continue;
268     }
269
270     /* Skip over separating white space, if any. */
271     while ((c = getc(ff)) != EOF && isspace(c)) {
272       if (c == '\n')
273         break;
274     }
275
276     if (c == EOF) {
277       if (!ferror(ff)) {
278         /* EOF, not error; no newline seen before EOF */
279         g_warning("'%s' line %d doesn't have a newline.", ff_path,
280                   line);
281       }
282       break;    /* nothing more to read */
283     }
284
285     if (c == '\n') {
286       /* No filter expression */
287       g_warning("'%s' line %d doesn't have a filter expression.", ff_path,
288                 line);
289       continue;
290     }
291
292     /* "c" is the first non-white-space character; it's the first
293        character of the filter expression. */
294     filt_expr_index = 0;
295     for (;;) {
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. */
299         filt_expr_len *= 2;
300         filt_expr = g_realloc(filt_expr, filt_expr_len + 1);
301       }
302       filt_expr[filt_expr_index] = c;
303       filt_expr_index++;
304
305       /* Get the next character. */
306       c = getc(ff);
307       if (c == EOF || c == '\n')
308         break;
309     }
310
311     if (c == EOF) {
312       if (!ferror(ff)) {
313         /* EOF, not error; no newline seen before EOF */
314         g_warning("'%s' line %d doesn't have a newline.", ff_path,
315                   line);
316       }
317       break;    /* nothing more to read */
318     }
319
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. */
323       filt_expr_len *= 2;
324       filt_expr = g_realloc(filt_expr, filt_expr_len + 1);
325     }
326     filt_expr[filt_expr_index] = '\0';
327
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);
333   }
334   if (ferror(ff)) {
335     *pref_path_return = ff_path;
336     *errno_return = errno;
337   } else
338     g_free(ff_path);
339   fclose(ff);
340   g_free(filt_name);
341   g_free(filt_expr);
342 }
343
344 /*
345  * Get a pointer to a list of filters.
346  */
347 static GList **
348 get_filter_list(filter_list_type_t list)
349 {
350   GList **flp;
351
352   switch (list) {
353
354   case CFILTER_LIST:
355     flp = &capture_filters;
356     break;
357
358   case DFILTER_LIST:
359     flp = &display_filters;
360     break;
361
362   default:
363     g_assert_not_reached();
364     flp = NULL;
365   }
366   return flp;
367 }
368
369 /*
370  * Get a pointer to the first entry in a filter list.
371  */
372 GList *
373 get_filter_list_first(filter_list_type_t list)
374 {
375   GList      **flp;
376
377   flp = get_filter_list(list);
378   return g_list_first(*flp);
379 }
380
381 /*
382  * Add a new filter to the end of a list.
383  * Returns a pointer to the newly-added entry.
384  */
385 GList *
386 add_to_filter_list(filter_list_type_t list, const char *name,
387     const char *expression)
388 {
389   GList      **flp;
390   filter_def *filt;
391
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);
398 }
399
400 /*
401  * Remove a filter from a list.
402  */
403 void
404 remove_from_filter_list(filter_list_type_t list, GList *fl_entry)
405 {
406   GList      **flp;
407   filter_def *filt;
408
409   flp = get_filter_list(list);
410   filt = (filter_def *) fl_entry->data;
411   g_free(filt->name);
412   g_free(filt->strval);
413   g_free(filt);
414   *flp = g_list_remove_link(*flp, fl_entry);
415 }
416
417 /*
418  * Write out a list of filters.
419  *
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.
424  */
425 void
426 save_filter_list(filter_list_type_t list, char **pref_path_return,
427     int *errno_return)
428 {
429   const gchar *ff_name;
430   gchar      *ff_path, *ff_path_new;
431   GList      *fl;
432   GList      *flp;
433   filter_def *filt;
434   FILE       *ff;
435   guchar     *p, c;
436
437   *pref_path_return = NULL;     /* assume no error */
438
439   switch (list) {
440
441   case CFILTER_LIST:
442     ff_name = CFILTER_FILE_NAME;
443     fl = capture_filters;
444     break;
445
446   case DFILTER_LIST:
447     ff_name = DFILTER_FILE_NAME;
448     fl = display_filters;
449     break;
450
451   default:
452     g_assert_not_reached();
453     return;
454   }
455
456   ff_path = get_persconffile_path(ff_name, TRUE);
457
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
460      completely. */
461   ff_path_new = (gchar *) g_malloc(strlen(ff_path) + 5);
462   sprintf(ff_path_new, "%s.new", ff_path);
463
464   if ((ff = fopen(ff_path_new, "w")) == NULL) {
465     *pref_path_return = ff_path;
466     *errno_return = errno;
467     g_free(ff_path_new);
468     return;
469   }
470   flp = g_list_first(fl);
471   while (flp) {
472     filt = (filter_def *) flp->data;
473
474     /* Write out the filter name as a quoted string; escape any quotes
475        or backslashes. */
476     putc('"', ff);
477     for (p = (guchar *)filt->name; (c = *p) != '\0'; p++) {
478       if (c == '"' || c == '\\')
479         putc('\\', ff);
480       putc(c, ff);
481     }
482     putc('"', ff);
483
484     /* Separate the filter name and value with a space. */
485     putc(' ', ff);
486
487     /* Write out the filter expression and a newline. */
488     fprintf(ff, "%s\n", filt->strval);
489     if (ferror(ff)) {
490       *pref_path_return = ff_path;
491       *errno_return = errno;
492       fclose(ff);
493       unlink(ff_path_new);
494       g_free(ff_path_new);
495       return;
496     }
497     flp = flp->next;
498   }
499   if (fclose(ff) == EOF) {
500     *pref_path_return = ff_path;
501     *errno_return = errno;
502     unlink(ff_path_new);
503     g_free(ff_path_new);
504     return;
505   }
506
507 #ifdef _WIN32
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
515        drive on. */
516     *pref_path_return = ff_path;
517     *errno_return = errno;
518     unlink(ff_path_new);
519     g_free(ff_path_new);
520     return;
521   }
522 #endif
523
524   if (rename(ff_path_new, ff_path) < 0) {
525     *pref_path_return = ff_path;
526     *errno_return = errno;
527     unlink(ff_path_new);
528     g_free(ff_path_new);
529     return;
530   }
531   g_free(ff_path_new);
532   g_free(ff_path);
533 }