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