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