Give Steve Sommars' real name and work e-mail address.
[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   char       *ff_path, *ff_name;
85   FILE       *ff;
86   GList      **flp;
87   GList      *fl_ent;
88   filter_def *filt;
89   int         c;
90   char       *filt_name, *filt_expr;
91   int         filt_name_len, filt_expr_len;
92   int         filt_name_index, filt_expr_index;
93   int         line = 1;
94
95   *pref_path_return = NULL;     /* assume no error */
96
97   switch (list) {
98
99   case CFILTER_LIST:
100     ff_name = CFILTER_FILE_NAME;
101     flp = &capture_filters;
102     break;
103
104   case DFILTER_LIST:
105     ff_name = DFILTER_FILE_NAME;
106     flp = &display_filters;
107     break;
108
109   default:
110     g_assert_not_reached();
111     return;
112   }
113
114   /* To do: generalize this */
115   ff_path = get_persconffile_path(ff_name, FALSE);
116   if ((ff = fopen(ff_path, "r")) == NULL) {
117     /*
118      * Did that fail because we the file didn't exist?
119      */
120     if (errno != ENOENT) {
121       /*
122        * No.  Just give up.
123        */
124       *pref_path_return = ff_path;
125       *errno_return = errno;
126       return;
127     }
128
129     /*
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
135      * a particular list.
136      */
137     g_free(ff_path);
138     ff_path = get_persconffile_path(FILTER_FILE_NAME, FALSE);
139     if ((ff = fopen(ff_path, "r")) == NULL) {
140       /*
141        * Well, that didn't work, either.  Just give up.
142        * Return an error if the file existed but we couldn't open it.
143        */
144       if (errno != ENOENT) {
145         *pref_path_return = ff_path;
146         *errno_return = errno;
147       }
148       return;
149     }
150   }
151
152   /* If we already have a list of filters, discard it. */
153   if (*flp != NULL) {
154     fl_ent = g_list_first(*flp);
155     while (fl_ent != NULL) {
156       filt = (filter_def *) fl_ent->data;
157       g_free(filt->name);
158       g_free(filt->strval);
159       g_free(filt);
160       fl_ent = fl_ent->next;
161     }
162     g_list_free(*flp);
163     *flp = NULL;
164   }
165
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);
171
172   for (line = 1; ; line++) {
173     /* Lines in a filter file are of the form
174
175         "name" expression
176
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. */
181
182     /* Skip over leading white space, if any. */
183     while ((c = getc(ff)) != EOF && isspace(c)) {
184       if (c == '\n') {
185         /* Blank line. */
186         continue;
187       }
188     }
189
190     if (c == EOF)
191       break;    /* Nothing more to read */
192
193     /* "c" is the first non-white-space character.
194        If it's not a quote, it's an error. */
195     if (c != '"') {
196       g_warning("'%s' line %d doesn't have a quoted filter name.", ff_path,
197                 line);
198       while (c != '\n')
199         c = getc(ff);   /* skip to the end of the line */
200       continue;
201     }
202
203     /* Get the name of the filter. */
204     filt_name_index = 0;
205     for (;;) {
206       c = getc(ff);
207       if (c == EOF || c == '\n')
208         break;  /* End of line - or end of file */
209       if (c == '"') {
210         /* Closing quote. */
211         if (filt_name_index >= filt_name_len) {
212           /* Filter name buffer isn't long enough; double its length. */
213           filt_name_len *= 2;
214           filt_name = g_realloc(filt_name, filt_name_len + 1);
215         }
216         filt_name[filt_name_index] = '\0';
217         break;
218       }
219       if (c == '\\') {
220         /* Next character is escaped */
221         c = getc(ff);
222         if (c == EOF || c == '\n')
223           break;        /* End of line - or end of file */
224       }
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. */
228         filt_name_len *= 2;
229         filt_name = g_realloc(filt_name, filt_name_len + 1);
230       }
231       filt_name[filt_name_index] = c;
232       filt_name_index++;
233     }
234
235     if (c == EOF) {
236       if (!ferror(ff)) {
237         /* EOF, not error; no newline seen before EOF */
238         g_warning("'%s' line %d doesn't have a newline.", ff_path,
239                   line);
240       }
241       break;    /* nothing more to read */
242     }
243
244     if (c != '"') {
245       /* No newline seen before end-of-line */
246       g_warning("'%s' line %d doesn't have a closing quote.", ff_path,
247                 line);
248       continue;
249     }
250
251     /* Skip over separating white space, if any. */
252     while ((c = getc(ff)) != EOF && isspace(c)) {
253       if (c == '\n')
254         break;
255     }
256
257     if (c == EOF) {
258       if (!ferror(ff)) {
259         /* EOF, not error; no newline seen before EOF */
260         g_warning("'%s' line %d doesn't have a newline.", ff_path,
261                   line);
262       }
263       break;    /* nothing more to read */
264     }
265
266     if (c == '\n') {
267       /* No filter expression */
268       g_warning("'%s' line %d doesn't have a filter expression.", ff_path,
269                 line);
270       continue;
271     }
272
273     /* "c" is the first non-white-space character; it's the first
274        character of the filter expression. */
275     filt_expr_index = 0;
276     for (;;) {
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. */
280         filt_expr_len *= 2;
281         filt_expr = g_realloc(filt_expr, filt_expr_len + 1);
282       }
283       filt_expr[filt_expr_index] = c;
284       filt_expr_index++;
285
286       /* Get the next character. */
287       c = getc(ff);
288       if (c == EOF || c == '\n')
289         break;
290     }
291
292     if (c == EOF) {
293       if (!ferror(ff)) {
294         /* EOF, not error; no newline seen before EOF */
295         g_warning("'%s' line %d doesn't have a newline.", ff_path,
296                   line);
297       }
298       break;    /* nothing more to read */
299     }
300
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. */
304       filt_expr_len *= 2;
305       filt_expr = g_realloc(filt_expr, filt_expr_len + 1);
306     }
307     filt_expr[filt_expr_index] = '\0';
308
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);
314   }
315   if (ferror(ff)) {
316     *pref_path_return = ff_path;
317     *errno_return = errno;
318   } else
319     g_free(ff_path);
320   fclose(ff);
321   g_free(filt_name);
322   g_free(filt_expr);
323 }
324
325 /*
326  * Get a pointer to a list of filters.
327  */
328 static GList **
329 get_filter_list(filter_list_type_t list)
330 {
331   GList **flp;
332
333   switch (list) {
334
335   case CFILTER_LIST:
336     flp = &capture_filters;
337     break;
338
339   case DFILTER_LIST:
340     flp = &display_filters;
341     break;
342
343   default:
344     g_assert_not_reached();
345     flp = NULL;
346   }
347   return flp;
348 }
349
350 /*
351  * Get a pointer to the first entry in a filter list.
352  */
353 GList *
354 get_filter_list_first(filter_list_type_t list)
355 {
356   GList      **flp;
357
358   flp = get_filter_list(list);
359   return g_list_first(*flp);
360 }
361
362 /*
363  * Add a new filter to the end of a list.
364  * Returns a pointer to the newly-added entry.
365  */
366 GList *
367 add_to_filter_list(filter_list_type_t list, const char *name,
368     const char *expression)
369 {
370   GList      **flp;
371   filter_def *filt;
372
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);
379 }
380
381 /*
382  * Remove a filter from a list.
383  */
384 void
385 remove_from_filter_list(filter_list_type_t list, GList *fl_entry)
386 {
387   GList      **flp;
388   filter_def *filt;
389
390   flp = get_filter_list(list);
391   filt = (filter_def *) fl_entry->data;
392   g_free(filt->name);
393   g_free(filt->strval);
394   g_free(filt);
395   *flp = g_list_remove_link(*flp, fl_entry);
396 }
397
398 /*
399  * Write out a list of filters.
400  *
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.
405  */
406 void
407 save_filter_list(filter_list_type_t list, char **pref_path_return,
408     int *errno_return)
409 {
410   gchar      *ff_path, *ff_path_new, *ff_name;
411   GList      *fl;
412   GList      *flp;
413   filter_def *filt;
414   FILE       *ff;
415   guchar     *p, c;
416
417   *pref_path_return = NULL;     /* assume no error */
418
419   switch (list) {
420
421   case CFILTER_LIST:
422     ff_name = CFILTER_FILE_NAME;
423     fl = capture_filters;
424     break;
425
426   case DFILTER_LIST:
427     ff_name = DFILTER_FILE_NAME;
428     fl = display_filters;
429     break;
430
431   default:
432     g_assert_not_reached();
433     return;
434   }
435
436   ff_path = get_persconffile_path(ff_name, TRUE);
437
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
440      completely. */
441   ff_path_new = (gchar *) g_malloc(strlen(ff_path) + 5);
442   sprintf(ff_path_new, "%s.new", ff_path);
443
444   if ((ff = fopen(ff_path_new, "w")) == NULL) {
445     *pref_path_return = ff_path;
446     *errno_return = errno;
447     g_free(ff_path_new);
448     return;
449   }
450   flp = g_list_first(fl);
451   while (flp) {
452     filt = (filter_def *) flp->data;
453
454     /* Write out the filter name as a quoted string; escape any quotes
455        or backslashes. */
456     putc('"', ff);
457     for (p = (guchar *)filt->name; (c = *p) != '\0'; p++) {
458       if (c == '"' || c == '\\')
459         putc('\\', ff);
460       putc(c, ff);
461     }
462     putc('"', ff);
463
464     /* Separate the filter name and value with a space. */
465     putc(' ', ff);
466
467     /* Write out the filter expression and a newline. */
468     fprintf(ff, "%s\n", filt->strval);
469     if (ferror(ff)) {
470       *pref_path_return = ff_path;
471       *errno_return = errno;
472       fclose(ff);
473       unlink(ff_path_new);
474       g_free(ff_path_new);
475       return;
476     }
477     flp = flp->next;
478   }
479   if (fclose(ff) == EOF) {
480     *pref_path_return = ff_path;
481     *errno_return = errno;
482     unlink(ff_path_new);
483     g_free(ff_path_new);
484     return;
485   }
486
487 #ifdef _WIN32
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
495        drive on. */
496     *pref_path_return = ff_path;
497     *errno_return = errno;
498     unlink(ff_path_new);
499     g_free(ff_path_new);
500     return;
501   }
502 #endif
503
504   if (rename(ff_path_new, ff_path) < 0) {
505     *pref_path_return = ff_path;
506     *errno_return = errno;
507     unlink(ff_path_new);
508     g_free(ff_path_new);
509     return;
510   }
511   g_free(ff_path_new);
512   g_free(ff_path);
513 }