Put the "-s" option in the SYNOPSIS section.
[obnox/wireshark/wip.git] / filters.c
1 /* filters.c
2  * Code for reading and writing the filters file.
3  *
4  * $Id: filters.c,v 1.13 2001/10/24 07:18:36 guy Exp $
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 <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, char *name, char *expression)
368 {
369   GList      **flp;
370   filter_def *filt;
371   
372   flp = get_filter_list(list);
373   filt = (filter_def *) g_malloc(sizeof(filter_def));
374   filt->name = g_strdup(name);
375   filt->strval = g_strdup(expression);
376   *flp = g_list_append(*flp, filt);
377   return g_list_last(*flp);
378 }
379
380 /*
381  * Remove a filter from a list.
382  */
383 void
384 remove_from_filter_list(filter_list_type_t list, GList *fl_entry)
385 {
386   GList      **flp;
387   filter_def *filt;
388   
389   flp = get_filter_list(list);
390   filt = (filter_def *) fl_entry->data;
391   g_free(filt->name);
392   g_free(filt->strval);
393   g_free(filt);
394   *flp = g_list_remove_link(*flp, fl_entry);
395 }
396
397 /*
398  * Write out a list of filters.
399  *
400  * On success, "*pref_path_return" is set to NULL.
401  * On error, "*pref_path_return" is set to point to the pathname of
402  * the file we tried to read - it should be freed by our caller -
403  * and "*errno_return" is set to the error.
404  */
405 void
406 save_filter_list(filter_list_type_t list, char **pref_path_return,
407     int *errno_return)
408 {
409   gchar      *ff_path, *ff_path_new, *ff_name;
410   GList      *fl;
411   GList      *flp;
412   filter_def *filt;
413   FILE       *ff;
414   guchar     *p, c;
415   
416   *pref_path_return = NULL;     /* assume no error */
417
418   switch (list) {
419
420   case CFILTER_LIST:
421     ff_name = CFILTER_FILE_NAME;
422     fl = capture_filters;
423     break;
424
425   case DFILTER_LIST:
426     ff_name = DFILTER_FILE_NAME;
427     fl = display_filters;
428     break;
429
430   default:
431     g_assert_not_reached();
432     return;
433   }
434
435   ff_path = get_persconffile_path(ff_name, TRUE);
436
437   /* Write to "XXX.new", and rename if that succeeds.
438      That means we don't trash the file if we fail to write it out
439      completely. */
440   ff_path_new = (gchar *) g_malloc(strlen(ff_path) + 5);
441   sprintf(ff_path_new, "%s.new", ff_path);
442
443   if ((ff = fopen(ff_path_new, "w")) == NULL) {
444     *pref_path_return = ff_path;
445     *errno_return = errno;
446     g_free(ff_path_new);
447     return;
448   }
449   flp = g_list_first(fl);
450   while (flp) {
451     filt = (filter_def *) flp->data;
452
453     /* Write out the filter name as a quoted string; escape any quotes
454        or backslashes. */
455     putc('"', ff);
456     for (p = (guchar *)filt->name; (c = *p) != '\0'; p++) {
457       if (c == '"' || c == '\\')
458         putc('\\', ff);
459       putc(c, ff);
460     }
461     putc('"', ff);
462
463     /* Separate the filter name and value with a space. */
464     putc(' ', ff);
465
466     /* Write out the filter expression and a newline. */
467     fprintf(ff, "%s\n", filt->strval);
468     if (ferror(ff)) {
469       *pref_path_return = ff_path;
470       *errno_return = errno;
471       fclose(ff);
472       unlink(ff_path_new);
473       g_free(ff_path_new);
474       return;
475     }
476     flp = flp->next;
477   }
478   if (fclose(ff) == EOF) {
479     *pref_path_return = ff_path;
480     *errno_return = errno;
481     unlink(ff_path_new);
482     g_free(ff_path_new);
483     return;
484   }
485
486 #ifdef WIN32
487   /* ANSI C doesn't say whether "rename()" removes the target if it
488      exists; the Win32 call to rename files doesn't do so, which I
489      infer is the reason why the MSVC++ "rename()" doesn't do so.
490      We must therefore remove the target file first, on Windows. */
491   if (remove(ff_path) < 0 && errno != ENOENT) {
492     /* It failed for some reason other than "it's not there"; if
493        it's not there, we don't need to remove it, so we just
494        drive on. */
495     *pref_path_return = ff_path;
496     *errno_return = errno;
497     unlink(ff_path_new);
498     g_free(ff_path_new);
499     return;
500   }
501 #endif
502
503   if (rename(ff_path_new, ff_path) < 0) {
504     *pref_path_return = ff_path;
505     *errno_return = errno;
506     unlink(ff_path_new);
507     g_free(ff_path_new);
508     return;
509   }
510   g_free(ff_path_new);
511   g_free(ff_path);
512 }