wslua: fix nstime memory leak after passing unknown encoding to TvbRange_nstime()
[metze/wireshark/wip.git] / epan / color_filters.c
1 /* color_filters.c
2  * Routines for color filters
3  *
4  * Wireshark - Network traffic analyzer
5  * By Gerald Combs <gerald@wireshark.org>
6  * Copyright 1998 Gerald Combs
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22 /*
23  * Updated 1 Dec 10 jjm
24  */
25
26 #include <config.h>
27
28 #include <glib.h>
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include <wsutil/filesystem.h>
35 #include <wsutil/file_util.h>
36 #include <wsutil/ws_printf.h> /* ws_g_warning */
37
38 #include <epan/packet.h>
39 #include "color_filters.h"
40 #include "file.h"
41 #include <epan/dfilter/dfilter.h>
42 #include <epan/prefs.h>
43 #include <epan/epan_dissect.h>
44
45 #define RED_COMPONENT(x)   (guint16) (((((x) >> 16) & 0xff) * 65535 / 255))
46 #define GREEN_COMPONENT(x) (guint16) (((((x) >>  8) & 0xff) * 65535 / 255))
47 #define BLUE_COMPONENT(x)  (guint16) ( (((x)        & 0xff) * 65535 / 255))
48
49 static int read_filters_file(const gchar *path, FILE *f, gpointer user_data, color_filter_add_cb_func add_cb);
50
51 /* the currently active filters */
52 static GSList *color_filter_list = NULL;
53
54 /* keep "old" deleted filters in this list until
55  * the dissection no longer needs them (e.g. file is closed) */
56 static GSList *color_filter_deleted_list = NULL;
57 static GSList *color_filter_valid_list   = NULL;
58
59 /* Color Filters can en-/disabled. */
60 static gboolean filters_enabled = TRUE;
61
62 /* Remember if there are temporary coloring filters set to
63  * add sensitivity to the "Reset Coloring 1-10" menu item
64  */
65 static gboolean tmp_colors_set = FALSE;
66
67 /* Create a new filter */
68 color_filter_t *
69 color_filter_new(const gchar *name,          /* The name of the filter to create */
70                  const gchar *filter_string, /* The string representing the filter */
71                  color_t     *bg_color,      /* The background color */
72                  color_t     *fg_color,      /* The foreground color */
73                  gboolean     disabled)      /* Is the filter disabled? */
74 {
75     color_filter_t *colorf;
76
77     colorf                      = (color_filter_t *)g_malloc0(sizeof (color_filter_t));
78     colorf->filter_name         = g_strdup(name);
79     colorf->filter_text         = g_strdup(filter_string);
80     colorf->bg_color            = *bg_color;
81     colorf->fg_color            = *fg_color;
82     colorf->disabled            = disabled;
83     return colorf;
84 }
85
86 /* Add ten empty (temporary) colorfilters for easy coloring */
87 static void
88 color_filters_add_tmp(GSList **cfl)
89 {
90     gchar          *name = NULL;
91     guint32         i;
92     gchar**         bg_colors;
93     gchar**         fg_colors;
94     gulong          cval;
95     color_t         bg_color, fg_color;
96     color_filter_t *colorf;
97
98     g_assert(strlen(prefs.gui_colorized_fg)==69);
99     g_assert(strlen(prefs.gui_colorized_bg)==69);
100     fg_colors = g_strsplit(prefs.gui_colorized_fg, ",", -1);
101     bg_colors = g_strsplit(prefs.gui_colorized_bg, ",", -1);
102
103     for ( i=1 ; i<=10 ; i++ ) {
104         name = g_strdup_printf("%s%02d",CONVERSATION_COLOR_PREFIX,i);
105
106         /* retrieve background and foreground colors */
107         cval = strtoul(fg_colors[i-1], NULL, 16);
108         fg_color.red = RED_COMPONENT(cval);
109         fg_color.green = GREEN_COMPONENT(cval);
110         fg_color.blue = BLUE_COMPONENT(cval);
111         cval = strtoul(bg_colors[i-1], NULL, 16);
112         bg_color.red = RED_COMPONENT(cval);
113         bg_color.green = GREEN_COMPONENT(cval);
114         bg_color.blue = BLUE_COMPONENT(cval);
115         colorf = color_filter_new(name, NULL, &bg_color, &fg_color, TRUE);
116         colorf->filter_text = g_strdup("frame");
117         *cfl = g_slist_append(*cfl, colorf);
118
119         g_free(name);
120     }
121
122     g_strfreev(fg_colors);
123     g_strfreev(bg_colors);
124
125     return;
126 }
127
128 static gint
129 color_filters_find_by_name_cb(gconstpointer arg1, gconstpointer arg2)
130 {
131     const color_filter_t *colorf = (const color_filter_t *)arg1;
132     const gchar          *name   = (const gchar *)arg2;
133
134     return strcmp(colorf->filter_name, name);
135 }
136
137
138 /* Set the filter off a temporary colorfilters and enable it */
139 gboolean
140 color_filters_set_tmp(guint8 filt_nr, const gchar *filter, gboolean disabled, gchar **err_msg)
141 {
142     gchar          *name = NULL;
143     const gchar    *tmpfilter = NULL;
144     GSList         *cfl;
145     color_filter_t *colorf;
146     dfilter_t      *compiled_filter;
147     guint8         i;
148     gchar          *local_err_msg = NULL;
149     /* Go through the temporary filters and look for the same filter string.
150      * If found, clear it so that a filter can be "moved" up and down the list
151      */
152     for ( i=1 ; i<=10 ; i++ ) {
153         /* If we need to reset the temporary filter (filter==NULL), don't look
154          * for other rules with the same filter string
155          */
156         if( i!=filt_nr && filter==NULL )
157             continue;
158
159         name = g_strdup_printf("%s%02d",CONVERSATION_COLOR_PREFIX,i);
160         cfl = g_slist_find_custom(color_filter_list, name, color_filters_find_by_name_cb);
161         colorf = (color_filter_t *)cfl->data;
162
163         /* Only change the filter rule if this is the rule to change or if
164          * a matching filter string has been found
165          */
166         if(colorf && ( (i==filt_nr) || (!strcmp(filter, colorf->filter_text)) ) ) {
167             /* set filter string to "frame" if we are resetting the rules
168              * or if we found a matching filter string which need to be cleared
169              */
170             tmpfilter = ( (filter==NULL) || (i!=filt_nr) ) ? "frame" : filter;
171             if (!dfilter_compile(tmpfilter, &compiled_filter, &local_err_msg)) {
172                 *err_msg = g_strdup_printf( "Could not compile color filter name: \"%s\" text: \"%s\".\n%s", name, filter, local_err_msg);
173                 g_free(local_err_msg);
174                 return FALSE;
175             } else {
176                 g_free(colorf->filter_text);
177                 dfilter_free(colorf->c_colorfilter);
178                 colorf->filter_text = g_strdup(tmpfilter);
179                 colorf->c_colorfilter = compiled_filter;
180                 colorf->disabled = ((i!=filt_nr) ? TRUE : disabled);
181                 /* Remember that there are now temporary coloring filters set */
182                 if( filter )
183                     tmp_colors_set = TRUE;
184             }
185         }
186         g_free(name);
187     }
188     return TRUE;
189 }
190
191 const color_filter_t *
192 color_filters_tmp_color(guint8 filter_num) {
193     gchar          *name;
194     color_filter_t *colorf = NULL;
195     GSList         *cfl;
196
197     name = g_strdup_printf("%s%02d", CONVERSATION_COLOR_PREFIX, filter_num);
198     cfl = g_slist_find_custom(color_filter_list, name, color_filters_find_by_name_cb);
199     if (cfl) {
200         colorf = (color_filter_t *)cfl->data;
201     }
202     g_free(name);
203
204     return colorf;
205 }
206
207 /* Reset the temporary colorfilters */
208 gboolean
209 color_filters_reset_tmp(gchar **err_msg)
210 {
211     guint8 i;
212
213     for ( i=1 ; i<=10 ; i++ ) {
214         if (!color_filters_set_tmp(i, NULL, TRUE, err_msg))
215             return FALSE;
216     }
217     /* Remember that there are now *no* temporary coloring filters set */
218     tmp_colors_set = FALSE;
219     return TRUE;
220 }
221
222 /* delete the specified filter */
223 void
224 color_filter_delete(color_filter_t *colorf)
225 {
226     g_free(colorf->filter_name);
227     g_free(colorf->filter_text);
228     dfilter_free(colorf->c_colorfilter);
229     g_free(colorf);
230 }
231
232 /* delete the specified filter (called from g_slist_foreach) */
233 static void
234 color_filter_delete_cb(gpointer filter_arg, gpointer unused _U_)
235 {
236     color_filter_t *colorf = (color_filter_t *)filter_arg;
237
238     color_filter_delete(colorf);
239 }
240
241 /* delete the specified list */
242 void
243 color_filter_list_delete(GSList **cfl)
244 {
245     g_slist_foreach(*cfl, color_filter_delete_cb, NULL);
246     g_slist_free(*cfl);
247     *cfl = NULL;
248 }
249
250 /* clone a single list entries from normal to edit list */
251 static color_filter_t *
252 color_filter_clone(color_filter_t *colorf)
253 {
254     color_filter_t *new_colorf;
255
256     new_colorf                      = (color_filter_t *)g_malloc(sizeof (color_filter_t));
257     new_colorf->filter_name         = g_strdup(colorf->filter_name);
258     new_colorf->filter_text         = g_strdup(colorf->filter_text);
259     new_colorf->bg_color            = colorf->bg_color;
260     new_colorf->fg_color            = colorf->fg_color;
261     new_colorf->disabled            = colorf->disabled;
262     new_colorf->c_colorfilter       = NULL;
263     new_colorf->color_edit_dlg_info = NULL;
264     new_colorf->selected            = FALSE;
265
266     return new_colorf;
267 }
268
269 static void
270 color_filter_list_clone_cb(gpointer filter_arg, gpointer cfl_arg)
271 {
272     GSList **cfl = (GSList **)cfl_arg;
273     color_filter_t *new_colorf;
274
275     new_colorf = color_filter_clone((color_filter_t *)filter_arg);
276     *cfl = g_slist_append(*cfl, new_colorf);
277 }
278
279 /* clone the specified list */
280 static GSList *
281 color_filter_list_clone(GSList *cfl)
282 {
283     GSList *new_list = NULL;
284
285     g_slist_foreach(cfl, color_filter_list_clone_cb, &new_list);
286
287     return new_list;
288 }
289
290 static gboolean
291 color_filters_get(gchar** err_msg, color_filter_add_cb_func add_cb)
292 {
293     gchar    *path;
294     FILE     *f;
295     int       ret;
296
297     /* start the list with the temporary colorizing rules */
298     color_filters_add_tmp(&color_filter_list);
299
300     /*
301      * Try to get the user's filters.
302      *
303      * Get the path for the file that would have their filters, and
304      * try to open it.
305      */
306     path = get_persconffile_path("colorfilters", TRUE);
307     if ((f = ws_fopen(path, "r")) == NULL) {
308         if (errno != ENOENT) {
309             /* Error trying to open the file; give up. */
310             *err_msg = g_strdup_printf("Could not open filter file\n\"%s\": %s.", path,
311                                        g_strerror(errno));
312             g_free(path);
313             return FALSE;
314         }
315         /* They don't have any filters; try to read the global filters */
316         g_free(path);
317         return color_filters_read_globals(&color_filter_list, err_msg, add_cb);
318     }
319
320     /*
321      * We've opened it; try to read it.
322      */
323     ret = read_filters_file(path, f, &color_filter_list, add_cb);
324     if (ret != 0) {
325         *err_msg = g_strdup_printf("Error reading filter file\n\"%s\": %s.",
326                                    path, g_strerror(errno));
327         fclose(f);
328         g_free(path);
329         return FALSE;
330     }
331
332     /* Success. */
333     fclose(f);
334     g_free(path);
335     return TRUE;
336 }
337
338 /* Initialize the filter structures (reading from file) for general running, including app startup */
339 gboolean
340 color_filters_init(gchar** err_msg, color_filter_add_cb_func add_cb)
341 {
342     /* delete all currently existing filters */
343     color_filter_list_delete(&color_filter_list);
344
345     /* now try to construct the filters list */
346     return color_filters_get(err_msg, add_cb);
347 }
348
349 gboolean
350 color_filters_reload(gchar** err_msg, color_filter_add_cb_func add_cb)
351 {
352     /* "move" old entries to the deleted list
353      * we must keep them until the dissection no longer needs them */
354     color_filter_deleted_list = g_slist_concat(color_filter_deleted_list, color_filter_list);
355     color_filter_list = NULL;
356
357     /* now try to construct the filters list */
358     return color_filters_get(err_msg, add_cb);
359 }
360
361 void
362 color_filters_cleanup(void)
363 {
364     /* delete the previously deleted filters */
365     color_filter_list_delete(&color_filter_deleted_list);
366 }
367
368 typedef struct _color_clone
369 {
370     gpointer user_data;
371     color_filter_add_cb_func add_cb;
372 } color_clone_t;
373
374 static void
375 color_filters_clone_cb(gpointer filter_arg, gpointer user_data)
376 {
377     color_clone_t* clone_data = (color_clone_t*)user_data;
378     color_filter_t * new_colorf = color_filter_clone((color_filter_t *)filter_arg);
379
380     clone_data->add_cb (new_colorf, clone_data->user_data);
381 }
382
383 void
384 color_filters_clone(gpointer user_data, color_filter_add_cb_func add_cb)
385 {
386     color_clone_t clone_data;
387
388     clone_data.user_data = user_data;
389     clone_data.add_cb = add_cb;
390     g_slist_foreach(color_filter_list, color_filters_clone_cb, &clone_data);
391 }
392
393
394 static void
395 color_filter_compile_cb(gpointer filter_arg, gpointer err)
396 {
397     color_filter_t *colorf = (color_filter_t *)filter_arg;
398     gchar **err_msg = (gchar**)err;
399     gchar *local_err_msg = NULL;
400
401     g_assert(colorf->c_colorfilter == NULL);
402
403     /* If the filter is disabled it doesn't matter if it compiles or not. */
404     if (colorf->disabled) return;
405
406     if (!dfilter_compile(colorf->filter_text, &colorf->c_colorfilter, &local_err_msg)) {
407         *err_msg = g_strdup_printf("Could not compile color filter name: \"%s\" text: \"%s\".\n%s",
408                       colorf->filter_name, colorf->filter_text, local_err_msg);
409         g_free(local_err_msg);
410         /* this filter was compilable before, so this should never happen */
411         /* except if the OK button of the parent window has been clicked */
412         /* so don't use g_assert_not_reached() but check the filters again */
413     }
414 }
415
416 static void
417 color_filter_validate_cb(gpointer filter_arg, gpointer err)
418 {
419     color_filter_t *colorf = (color_filter_t *)filter_arg;
420     gchar **err_msg = (gchar**)err;
421     gchar *local_err_msg;
422
423     g_assert(colorf->c_colorfilter == NULL);
424
425     /* If the filter is disabled it doesn't matter if it compiles or not. */
426     if (colorf->disabled) return;
427
428     if (!dfilter_compile(colorf->filter_text, &colorf->c_colorfilter, &local_err_msg)) {
429         *err_msg = g_strdup_printf("Removing color filter name: \"%s\" text: \"%s\".\n%s",
430                       colorf->filter_name, colorf->filter_text, local_err_msg);
431         g_free(local_err_msg);
432         /* Delete the color filter from the list of color filters. */
433         color_filter_valid_list = g_slist_remove(color_filter_valid_list, colorf);
434         color_filter_delete(colorf);
435     }
436 }
437
438 /* apply changes from the edit list */
439 gboolean
440 color_filters_apply(GSList *tmp_cfl, GSList *edit_cfl, gchar** err_msg)
441 {
442     gboolean ret = TRUE;
443
444     *err_msg = NULL;
445
446     /* "move" old entries to the deleted list
447      * we must keep them until the dissection no longer needs them */
448     color_filter_deleted_list = g_slist_concat(color_filter_deleted_list, color_filter_list);
449     color_filter_list = NULL;
450
451     /* clone all list entries from tmp/edit to normal list */
452     color_filter_valid_list = NULL;
453     color_filter_valid_list = color_filter_list_clone(tmp_cfl);
454     color_filter_valid_list = g_slist_concat(color_filter_valid_list,
455                                              color_filter_list_clone(edit_cfl) );
456
457     /* compile all filter */
458     g_slist_foreach(color_filter_valid_list, color_filter_validate_cb, err_msg);
459     if (*err_msg != NULL) {
460         ret = FALSE;
461     }
462
463     /* clone all list entries from tmp/edit to normal list */
464     color_filter_list = color_filter_list_clone(color_filter_valid_list);
465
466     /* compile all filter */
467     g_slist_foreach(color_filter_list, color_filter_compile_cb, err_msg);
468     if (*err_msg != NULL) {
469         ret = FALSE;
470     }
471
472     return ret;
473 }
474
475 gboolean
476 color_filters_used(void)
477 {
478     return color_filter_list != NULL && filters_enabled;
479 }
480
481 gboolean
482 tmp_color_filters_used(void)
483 {
484     return tmp_colors_set;
485 }
486
487 /* prepare the epan_dissect_t for the filter */
488 static void
489 prime_edt(gpointer data, gpointer user_data)
490 {
491     color_filter_t *colorf = (color_filter_t *)data;
492     epan_dissect_t *edt    = (epan_dissect_t *)user_data;
493
494     if (colorf->c_colorfilter != NULL)
495         epan_dissect_prime_with_dfilter(edt, colorf->c_colorfilter);
496 }
497
498 /* Prime the epan_dissect_t with all the compiler
499  * color filters in 'color_filter_list'. */
500 void
501 color_filters_prime_edt(epan_dissect_t *edt)
502 {
503     if (color_filters_used())
504         g_slist_foreach(color_filter_list, prime_edt, edt);
505 }
506
507 /* * Return the color_t for later use */
508 const color_filter_t *
509 color_filters_colorize_packet(epan_dissect_t *edt)
510 {
511     GSList         *curr;
512     color_filter_t *colorf;
513
514     /* If we have color filters, "search" for the matching one. */
515     if ((edt->tree != NULL) && (color_filters_used())) {
516         curr = color_filter_list;
517
518         while(curr != NULL) {
519             colorf = (color_filter_t *)curr->data;
520             if ( (!colorf->disabled) &&
521                  (colorf->c_colorfilter != NULL) &&
522                  dfilter_apply_edt(colorf->c_colorfilter, edt)) {
523                 return colorf;
524             }
525             curr = g_slist_next(curr);
526         }
527     }
528
529     return NULL;
530 }
531
532 /* read filters from the given file */
533 /* XXX - Would it make more sense to use GStrings here instead of reallocing
534    our buffers? */
535 static int
536 read_filters_file(const gchar *path, FILE *f, gpointer user_data, color_filter_add_cb_func add_cb)
537 {
538 #define INIT_BUF_SIZE 128
539     gchar    *name             = NULL;
540     gchar    *filter_exp       = NULL;
541     guint32   name_len         = INIT_BUF_SIZE;
542     guint32   filter_exp_len   = INIT_BUF_SIZE;
543     guint32   i                = 0;
544     int       c;
545     guint16   fg_r, fg_g, fg_b, bg_r, bg_g, bg_b;
546     gboolean  disabled         = FALSE;
547     gboolean  skip_end_of_line = FALSE;
548     int       ret = 0;
549
550     name = (gchar *)g_malloc(name_len + 1);
551     filter_exp = (gchar *)g_malloc(filter_exp_len + 1);
552
553     while (1) {
554
555         if (skip_end_of_line) {
556             do {
557                 c = ws_getc_unlocked(f);
558             } while (c != EOF && c != '\n');
559             if (c == EOF)
560                 break;
561             disabled = FALSE;
562             skip_end_of_line = FALSE;
563         }
564
565         while ((c = ws_getc_unlocked(f)) != EOF && g_ascii_isspace(c)) {
566             if (c == '\n') {
567                 continue;
568             }
569         }
570
571         if (c == EOF)
572             break;
573
574         if (c == '!') {
575             disabled = TRUE;
576             continue;
577         }
578
579         /* skip # comments and invalid lines */
580         if (c != '@') {
581             skip_end_of_line = TRUE;
582             continue;
583         }
584
585         /* we get the @ delimiter.
586          * Format is:
587          * @name@filter expression@[background r,g,b][foreground r,g,b]
588          */
589
590         /* retrieve name */
591         i = 0;
592         while (1) {
593             c = ws_getc_unlocked(f);
594             if (c == EOF || c == '@')
595                 break;
596             if (i >= name_len) {
597                 /* buffer isn't long enough; double its length.*/
598                 name_len *= 2;
599                 name = (gchar *)g_realloc(name, name_len + 1);
600             }
601             name[i++] = c;
602         }
603         name[i] = '\0';
604
605         if (c == EOF) {
606             break;
607         } else if (i == 0) {
608             skip_end_of_line = TRUE;
609             continue;
610         }
611
612         /* retrieve filter expression */
613         i = 0;
614         while (1) {
615             c = ws_getc_unlocked(f);
616             if (c == EOF || c == '@')
617                 break;
618             if (i >= filter_exp_len) {
619                 /* buffer isn't long enough; double its length.*/
620                 filter_exp_len *= 2;
621                 filter_exp = (gchar *)g_realloc(filter_exp, filter_exp_len + 1);
622             }
623             filter_exp[i++] = c;
624         }
625         filter_exp[i] = '\0';
626
627         if (c == EOF) {
628             break;
629         } else if (i == 0) {
630             skip_end_of_line = TRUE;
631             continue;
632         }
633
634         /* retrieve background and foreground colors */
635         if (fscanf(f,"[%hu,%hu,%hu][%hu,%hu,%hu]",
636                    &bg_r, &bg_g, &bg_b, &fg_r, &fg_g, &fg_b) == 6) {
637
638             /* we got a complete color filter */
639
640             color_t bg_color, fg_color;
641             color_filter_t *colorf;
642             dfilter_t *temp_dfilter = NULL;
643             gchar *local_err_msg = NULL;
644
645             if (!disabled && !dfilter_compile(filter_exp, &temp_dfilter, &local_err_msg)) {
646                 ws_g_warning("Could not compile \"%s\" in colorfilters file \"%s\".\n%s",
647                           name, path, local_err_msg);
648                 g_free(local_err_msg);
649                 prefs.unknown_colorfilters = TRUE;
650
651                 /* skip_end_of_line = TRUE; */
652                 disabled = TRUE;
653             }
654
655             fg_color.red = fg_r;
656             fg_color.green = fg_g;
657             fg_color.blue = fg_b;
658
659             bg_color.red = bg_r;
660             bg_color.green = bg_g;
661             bg_color.blue = bg_b;
662
663             colorf = color_filter_new(name, filter_exp, &bg_color,
664                                       &fg_color, disabled);
665             if(user_data == &color_filter_list) {
666                 GSList **cfl = (GSList **)user_data;
667
668                 /* internal call */
669                 colorf->c_colorfilter = temp_dfilter;
670                 *cfl = g_slist_append(*cfl, colorf);
671             } else {
672                 /* external call */
673                 /* just editing, don't need the compiled filter */
674                 dfilter_free(temp_dfilter);
675                 add_cb(colorf, user_data);
676             }
677         }    /* if sscanf */
678
679         skip_end_of_line = TRUE;
680     }
681
682     if (ferror(f))
683         ret = errno;
684
685     g_free(name);
686     g_free(filter_exp);
687     return ret;
688 }
689
690 /* read filters from the filter file */
691 gboolean
692 color_filters_read_globals(gpointer user_data, gchar** err_msg, color_filter_add_cb_func add_cb)
693 {
694     gchar    *path;
695     FILE     *f;
696     int       ret;
697
698     /*
699      * Try to get the global filters.
700      *
701      * Get the path for the file that would have the global filters, and
702      * try to open it.
703      */
704     path = get_datafile_path("colorfilters");
705     if ((f = ws_fopen(path, "r")) == NULL) {
706         if (errno != ENOENT) {
707             /* Error trying to open the file; give up. */
708             *err_msg = g_strdup_printf("Could not open global filter file\n\"%s\": %s.", path,
709                                        g_strerror(errno));
710             g_free(path);
711             return FALSE;
712         }
713
714         /*
715          * There is no global filter file; treat that as equivalent to
716          * that file existing bug being empty, and say we succeeded.
717          */
718         return TRUE;
719     }
720
721     ret = read_filters_file(path, f, user_data, add_cb);
722     if (ret != 0) {
723         *err_msg = g_strdup_printf("Error reading global filter file\n\"%s\": %s.",
724                                    path, g_strerror(errno));
725         fclose(f);
726         g_free(path);
727         return FALSE;
728     }
729
730     fclose(f);
731     g_free(path);
732     return TRUE;
733 }
734
735 /* read filters from some other filter file (import) */
736 gboolean
737 color_filters_import(const gchar *path, gpointer user_data, gchar **err_msg, color_filter_add_cb_func add_cb)
738 {
739     FILE     *f;
740     int       ret;
741
742     if ((f = ws_fopen(path, "r")) == NULL) {
743         *err_msg = g_strdup_printf("Could not open filter file\n%s\nfor reading: %s.",
744                       path, g_strerror(errno));
745         return FALSE;
746     }
747
748     ret = read_filters_file(path, f, user_data, add_cb);
749     if (ret != 0) {
750         *err_msg = g_strdup_printf("Error reading filter file\n\"%s\": %s.",
751                                    path, g_strerror(errno));
752         fclose(f);
753         return FALSE;
754     }
755
756     fclose(f);
757     return TRUE;
758 }
759
760 struct write_filter_data
761 {
762     FILE     *f;
763     gboolean  only_selected;
764 };
765
766 /* save a single filter */
767 static void
768 write_filter(gpointer filter_arg, gpointer data_arg)
769 {
770     struct write_filter_data *data = (struct write_filter_data *)data_arg;
771     color_filter_t *colorf = (color_filter_t *)filter_arg;
772     FILE *f = data->f;
773
774     if ( (colorf->selected || !data->only_selected) &&
775          (strstr(colorf->filter_name,CONVERSATION_COLOR_PREFIX)==NULL) ) {
776         fprintf(f,"%s@%s@%s@[%u,%u,%u][%u,%u,%u]\n",
777                 colorf->disabled ? "!" : "",
778                 colorf->filter_name,
779                 colorf->filter_text,
780                 colorf->bg_color.red,
781                 colorf->bg_color.green,
782                 colorf->bg_color.blue,
783                 colorf->fg_color.red,
784                 colorf->fg_color.green,
785                 colorf->fg_color.blue);
786     }
787 }
788
789 /* save filters in a filter file */
790 static gboolean
791 write_filters_file(GSList *cfl, FILE *f, gboolean only_selected)
792 {
793     struct write_filter_data data;
794
795     data.f = f;
796     data.only_selected = only_selected;
797
798     fprintf(f,"# DO NOT EDIT THIS FILE!  It was created by Wireshark\n");
799     g_slist_foreach(cfl, write_filter, &data);
800     return TRUE;
801 }
802
803 /* save filters in users filter file */
804 gboolean
805 color_filters_write(GSList *cfl, gchar** err_msg)
806 {
807     gchar *pf_dir_path;
808     gchar *path;
809     FILE  *f;
810
811     /* Create the directory that holds personal configuration files,
812        if necessary.  */
813     if (create_persconffile_dir(&pf_dir_path) == -1) {
814         *err_msg = g_strdup_printf("Can't create directory\n\"%s\"\nfor color files: %s.",
815                       pf_dir_path, g_strerror(errno));
816         g_free(pf_dir_path);
817         return FALSE;
818     }
819
820     path = get_persconffile_path("colorfilters", TRUE);
821     if ((f = ws_fopen(path, "w+")) == NULL) {
822         *err_msg = g_strdup_printf("Could not open\n%s\nfor writing: %s.",
823                       path, g_strerror(errno));
824         g_free(path);
825         return FALSE;
826     }
827     g_free(path);
828     write_filters_file(cfl, f, FALSE);
829     fclose(f);
830     return TRUE;
831 }
832
833 /* save filters in some other filter file (export) */
834 gboolean
835 color_filters_export(const gchar *path, GSList *cfl, gboolean only_marked, gchar** err_msg)
836 {
837     FILE *f;
838
839     if ((f = ws_fopen(path, "w+")) == NULL) {
840         *err_msg = g_strdup_printf("Could not open\n%s\nfor writing: %s.",
841                       path, g_strerror(errno));
842         return FALSE;
843     }
844     write_filters_file(cfl, f, only_marked);
845     fclose(f);
846     return TRUE;
847 }
848
849 /*
850  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
851  *
852  * Local variables:
853  * c-basic-offset: 4
854  * tab-width: 8
855  * indent-tabs-mode: nil
856  * End:
857  *
858  * vi: set shiftwidth=4 tabstop=8 expandtab:
859  * :indentSize=4:tabSize=8:noTabs=true:
860  */