No need to talk about obsolete tools
[obnox/wireshark/wip.git] / filters.c
1 /* filters.c
2  * Code for reading and writing the filters file.
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
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 #include "file_util.h"
44
45 /*
46  * Old filter file name.
47  */
48 #define FILTER_FILE_NAME        "filters"
49
50 /*
51  * Capture filter file name.
52  */
53 #define CFILTER_FILE_NAME       "cfilters"
54
55 /*
56  * Display filter file name.
57  */
58 #define DFILTER_FILE_NAME       "dfilters"
59
60 /*
61  * List of capture filters.
62  */
63 static GList *capture_filters = NULL;
64
65 /*
66  * List of display filters.
67  */
68 static GList *display_filters = NULL;
69
70 /*
71  * Read in a list of filters.
72  *
73  * On success, "*pref_path_return" is set to NULL.
74  * On error, "*pref_path_return" is set to point to the pathname of
75  * the file we tried to read - it should be freed by our caller -
76  * and "*errno_return" is set to the error.
77  */
78
79 #define INIT_BUF_SIZE   128
80
81 void
82 read_filter_list(filter_list_type_t list, char **pref_path_return,
83     int *errno_return)
84 {
85   const char *ff_name;
86   char       *ff_path;
87   FILE       *ff;
88   GList      **flp;
89   GList      *fl_ent;
90   filter_def *filt;
91   int         c;
92   char       *filt_name, *filt_expr;
93   int         filt_name_len, filt_expr_len;
94   int         filt_name_index, filt_expr_index;
95   int         line = 1;
96
97   *pref_path_return = NULL;     /* assume no error */
98
99   switch (list) {
100
101   case CFILTER_LIST:
102     ff_name = CFILTER_FILE_NAME;
103     flp = &capture_filters;
104     break;
105
106   case DFILTER_LIST:
107     ff_name = DFILTER_FILE_NAME;
108     flp = &display_filters;
109     break;
110
111   default:
112     g_assert_not_reached();
113     return;
114   }
115
116   /* try to open personal "cfilters"/"dfilters" file */
117   ff_path = get_persconffile_path(ff_name, FALSE);
118   if ((ff = eth_fopen(ff_path, "r")) == NULL) {
119     /*
120      * Did that fail because the file didn't exist?
121      */
122     if (errno != ENOENT) {
123       /*
124        * No.  Just give up.
125        */
126       *pref_path_return = ff_path;
127       *errno_return = errno;
128       return;
129     }
130
131     /*
132      * Yes.  See if there's an "old style" personal "filters" file; if so, read it.
133      * This means that a user will start out with their capture and
134      * display filter lists being identical; each list may contain
135      * filters that don't belong in that list.  The user can edit
136      * the filter lists, and delete the ones that don't belong in
137      * a particular list.
138      */
139     g_free(ff_path);
140     ff_path = get_persconffile_path(FILTER_FILE_NAME, FALSE);
141     if ((ff = eth_fopen(ff_path, "r")) == NULL) {
142     /*
143      * Did that fail because the file didn't exist?
144      */
145       if (errno != ENOENT) {
146       /*
147        * No.  Just give up.
148        */
149         *pref_path_return = ff_path;
150         *errno_return = errno;
151     return;
152       }
153
154     /*
155      * Try to open the global "cfilters/dfilters" file */
156     ff_path = get_datafile_path(ff_name);
157     if ((ff = eth_fopen(ff_path, "r")) == NULL) {
158
159       /*
160        * Well, that didn't work, either.  Just give up.
161        * Return an error if the file existed but we couldn't open it.
162        */
163       if (errno != ENOENT) {
164         *pref_path_return = ff_path;
165         *errno_return = errno;
166     }
167     return;
168     }
169     }
170   }
171
172   /* If we already have a list of filters, discard it. */
173   if (*flp != NULL) {
174     fl_ent = g_list_first(*flp);
175     while (fl_ent != NULL) {
176       filt = (filter_def *) fl_ent->data;
177       g_free(filt->name);
178       g_free(filt->strval);
179       g_free(filt);
180       fl_ent = fl_ent->next;
181     }
182     g_list_free(*flp);
183     *flp = NULL;
184   }
185
186   /* Allocate the filter name buffer. */
187   filt_name_len = INIT_BUF_SIZE;
188   filt_name = g_malloc(filt_name_len + 1);
189   filt_expr_len = INIT_BUF_SIZE;
190   filt_expr = g_malloc(filt_expr_len + 1);
191
192   for (line = 1; ; line++) {
193     /* Lines in a filter file are of the form
194
195         "name" expression
196
197        where "name" is a name, in quotes - backslashes in the name
198        escape the next character, so quotes and backslashes can appear
199        in the name - and "expression" is a filter expression, not in
200        quotes, running to the end of the line. */
201
202     /* Skip over leading white space, if any. */
203     while ((c = getc(ff)) != EOF && isspace(c)) {
204       if (c == '\n') {
205         /* Blank line. */
206         continue;
207       }
208     }
209
210     if (c == EOF)
211       break;    /* Nothing more to read */
212
213     /* "c" is the first non-white-space character.
214        If it's not a quote, it's an error. */
215     if (c != '"') {
216       g_warning("'%s' line %d doesn't have a quoted filter name.", ff_path,
217                 line);
218       while (c != '\n')
219         c = getc(ff);   /* skip to the end of the line */
220       continue;
221     }
222
223     /* Get the name of the filter. */
224     filt_name_index = 0;
225     for (;;) {
226       c = getc(ff);
227       if (c == EOF || c == '\n')
228         break;  /* End of line - or end of file */
229       if (c == '"') {
230         /* Closing quote. */
231         if (filt_name_index >= filt_name_len) {
232           /* Filter name buffer isn't long enough; double its length. */
233           filt_name_len *= 2;
234           filt_name = g_realloc(filt_name, filt_name_len + 1);
235         }
236         filt_name[filt_name_index] = '\0';
237         break;
238       }
239       if (c == '\\') {
240         /* Next character is escaped */
241         c = getc(ff);
242         if (c == EOF || c == '\n')
243           break;        /* End of line - or end of file */
244       }
245       /* Add this character to the filter name string. */
246       if (filt_name_index >= filt_name_len) {
247         /* Filter name buffer isn't long enough; double its length. */
248         filt_name_len *= 2;
249         filt_name = g_realloc(filt_name, filt_name_len + 1);
250       }
251       filt_name[filt_name_index] = c;
252       filt_name_index++;
253     }
254
255     if (c == EOF) {
256       if (!ferror(ff)) {
257         /* EOF, not error; no newline seen before EOF */
258         g_warning("'%s' line %d doesn't have a newline.", ff_path,
259                   line);
260       }
261       break;    /* nothing more to read */
262     }
263
264     if (c != '"') {
265       /* No newline seen before end-of-line */
266       g_warning("'%s' line %d doesn't have a closing quote.", ff_path,
267                 line);
268       continue;
269     }
270
271     /* Skip over separating white space, if any. */
272     while ((c = getc(ff)) != EOF && isspace(c)) {
273       if (c == '\n')
274         break;
275     }
276
277     if (c == EOF) {
278       if (!ferror(ff)) {
279         /* EOF, not error; no newline seen before EOF */
280         g_warning("'%s' line %d doesn't have a newline.", ff_path,
281                   line);
282       }
283       break;    /* nothing more to read */
284     }
285
286     if (c == '\n') {
287       /* No filter expression */
288       g_warning("'%s' line %d doesn't have a filter expression.", ff_path,
289                 line);
290       continue;
291     }
292
293     /* "c" is the first non-white-space character; it's the first
294        character of the filter expression. */
295     filt_expr_index = 0;
296     for (;;) {
297       /* Add this character to the filter expression string. */
298       if (filt_expr_index >= filt_expr_len) {
299         /* Filter expressioin buffer isn't long enough; double its length. */
300         filt_expr_len *= 2;
301         filt_expr = g_realloc(filt_expr, filt_expr_len + 1);
302       }
303       filt_expr[filt_expr_index] = c;
304       filt_expr_index++;
305
306       /* Get the next character. */
307       c = getc(ff);
308       if (c == EOF || c == '\n')
309         break;
310     }
311
312     if (c == EOF) {
313       if (!ferror(ff)) {
314         /* EOF, not error; no newline seen before EOF */
315         g_warning("'%s' line %d doesn't have a newline.", ff_path,
316                   line);
317       }
318       break;    /* nothing more to read */
319     }
320
321     /* We saw the ending newline; terminate the filter expression string */
322     if (filt_expr_index >= filt_expr_len) {
323       /* Filter expressioin buffer isn't long enough; double its length. */
324       filt_expr_len *= 2;
325       filt_expr = g_realloc(filt_expr, filt_expr_len + 1);
326     }
327     filt_expr[filt_expr_index] = '\0';
328
329     /* Add the new filter to the list of filters */
330     filt         = (filter_def *) g_malloc(sizeof(filter_def));
331     filt->name   = g_strdup(filt_name);
332     filt->strval = g_strdup(filt_expr);
333     *flp = g_list_append(*flp, filt);
334   }
335   if (ferror(ff)) {
336     *pref_path_return = ff_path;
337     *errno_return = errno;
338   } else
339     g_free(ff_path);
340   fclose(ff);
341   g_free(filt_name);
342   g_free(filt_expr);
343 }
344
345 /*
346  * Get a pointer to a list of filters.
347  */
348 static GList **
349 get_filter_list(filter_list_type_t list)
350 {
351   GList **flp;
352
353   switch (list) {
354
355   case CFILTER_LIST:
356     flp = &capture_filters;
357     break;
358
359   case DFILTER_LIST:
360     flp = &display_filters;
361     break;
362
363   default:
364     g_assert_not_reached();
365     flp = NULL;
366   }
367   return flp;
368 }
369
370 /*
371  * Get a pointer to the first entry in a filter list.
372  */
373 GList *
374 get_filter_list_first(filter_list_type_t list)
375 {
376   GList      **flp;
377
378   flp = get_filter_list(list);
379   return g_list_first(*flp);
380 }
381
382 /*
383  * Add a new filter to the end of a list.
384  * Returns a pointer to the newly-added entry.
385  */
386 GList *
387 add_to_filter_list(filter_list_type_t list, const char *name,
388     const char *expression)
389 {
390   GList      **flp;
391   filter_def *filt;
392
393   flp = get_filter_list(list);
394   filt = (filter_def *) g_malloc(sizeof(filter_def));
395   filt->name = g_strdup(name);
396   filt->strval = g_strdup(expression);
397   *flp = g_list_append(*flp, filt);
398   return g_list_last(*flp);
399 }
400
401 /*
402  * Remove a filter from a list.
403  */
404 void
405 remove_from_filter_list(filter_list_type_t list, GList *fl_entry)
406 {
407   GList      **flp;
408   filter_def *filt;
409
410   flp = get_filter_list(list);
411   filt = (filter_def *) fl_entry->data;
412   g_free(filt->name);
413   g_free(filt->strval);
414   g_free(filt);
415   *flp = g_list_remove_link(*flp, fl_entry);
416 }
417
418 /*
419  * Write out a list of filters.
420  *
421  * On success, "*pref_path_return" is set to NULL.
422  * On error, "*pref_path_return" is set to point to the pathname of
423  * the file we tried to read - it should be freed by our caller -
424  * and "*errno_return" is set to the error.
425  */
426 void
427 save_filter_list(filter_list_type_t list, char **pref_path_return,
428     int *errno_return)
429 {
430   const gchar *ff_name;
431   gchar      *ff_path, *ff_path_new;
432   GList      *fl;
433   GList      *flp;
434   filter_def *filt;
435   FILE       *ff;
436   guchar     *p, c;
437
438   *pref_path_return = NULL;     /* assume no error */
439
440   switch (list) {
441
442   case CFILTER_LIST:
443     ff_name = CFILTER_FILE_NAME;
444     fl = capture_filters;
445     break;
446
447   case DFILTER_LIST:
448     ff_name = DFILTER_FILE_NAME;
449     fl = display_filters;
450     break;
451
452   default:
453     g_assert_not_reached();
454     return;
455   }
456
457   ff_path = get_persconffile_path(ff_name, TRUE);
458
459   /* Write to "XXX.new", and rename if that succeeds.
460      That means we don't trash the file if we fail to write it out
461      completely. */
462   ff_path_new = g_strdup_printf("%s.new", ff_path);
463
464   if ((ff = eth_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       eth_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     eth_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 (eth_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     eth_unlink(ff_path_new);
519     g_free(ff_path_new);
520     return;
521   }
522 #endif
523
524   if (eth_rename(ff_path_new, ff_path) < 0) {
525     *pref_path_return = ff_path;
526     *errno_return = errno;
527     eth_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 }