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