Fix up indentation
[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
34 #include <stdio.h>
35 #include <ctype.h>
36 #include <string.h>
37
38 #include <epan/filesystem.h>
39 #include <wsutil/file_util.h>
40
41 #include <epan/packet.h>
42 #include "color.h"
43 #include "color_filters.h"
44 #include "file.h"
45 #include <epan/dfilter/dfilter.h>
46 #include "simple_dialog.h"
47 #include "ui_util.h"
48 #include <epan/prefs.h>
49
50 #define RED_COMPONENT(x)   (guint16) (((((x) >> 16) & 0xff) * 65535 / 255))
51 #define GREEN_COMPONENT(x) (guint16) (((((x) >>  8) & 0xff) * 65535 / 255))
52 #define BLUE_COMPONENT(x)  (guint16) ( (((x)        & 0xff) * 65535 / 255))
53
54 static gboolean read_users_filters(GSList **cfl);
55
56 /* the currently active filters */
57 static GSList *color_filter_list = NULL;
58
59 /* keep "old" deleted filters in this list until
60  * the dissection no longer needs them (e.g. file is closed) */
61 static GSList *color_filter_deleted_list = NULL;
62 static GSList *color_filter_valid_list = NULL;
63
64 /* Color Filters can en-/disabled. */
65 static gboolean filters_enabled = TRUE;
66
67 /* Remember if there are temporary coloring filters set to
68  * add sensitivity to the "Reset Coloring 1-10" menu item
69  */
70 static gboolean tmp_colors_set = FALSE;
71
72 /* Create a new filter */
73 color_filter_t *
74 color_filter_new(const gchar *name,    /* The name of the filter to create */
75                  const gchar *filter_string, /* The string representing the filter */
76                  color_t *bg_color,    /* The background color */
77                  color_t *fg_color,    /* The foreground color */
78                  gboolean disabled)     /* Is the filter disabled? */
79 {
80         color_filter_t *colorf;
81
82         colorf = (color_filter_t *)g_malloc(sizeof (color_filter_t));
83         colorf->filter_name = g_strdup(name);
84         colorf->filter_text = g_strdup(filter_string);
85         colorf->bg_color = *bg_color;
86         colorf->fg_color = *fg_color;
87         colorf->disabled = disabled;
88         colorf->c_colorfilter = NULL;
89         colorf->edit_dialog = NULL;
90         colorf->selected = FALSE;
91         return colorf;
92 }
93
94 /* Add ten empty (temporary) colorfilters for easy coloring */
95 static void
96 color_filters_add_tmp(GSList **cfl)
97 {
98         gchar  *name = NULL;
99         guint32 i;
100         gchar** bg_colors;
101         gchar** fg_colors;
102         unsigned long int cval;
103         color_t bg_color, fg_color;
104         color_filter_t *colorf;
105
106         g_assert(strlen(prefs.gui_colorized_fg)==69);
107         g_assert(strlen(prefs.gui_colorized_bg)==69);
108         fg_colors = g_strsplit(prefs.gui_colorized_fg, ",", -1);
109         bg_colors = g_strsplit(prefs.gui_colorized_bg, ",", -1);
110
111         for ( i=1 ; i<=10 ; i++ ) {
112                 name = g_strdup_printf("%s%02d",TEMP_COLOR_PREFIX,i);
113
114                 /* retrieve background and foreground colors */
115                 cval = strtoul(fg_colors[i-1], NULL, 16);
116                 initialize_color(&fg_color, RED_COMPONENT(cval),
117                                             GREEN_COMPONENT(cval),
118                                             BLUE_COMPONENT(cval) );
119                 cval = strtoul(bg_colors[i-1], NULL, 16);
120                 initialize_color(&bg_color, RED_COMPONENT(cval),
121                                             GREEN_COMPONENT(cval),
122                                             BLUE_COMPONENT(cval) );
123                 colorf = color_filter_new(name, NULL, &bg_color, &fg_color, TRUE);
124                 colorf->filter_text = g_strdup("frame");
125                 colorf->c_colorfilter = NULL;
126                 *cfl = g_slist_append(*cfl, colorf);
127
128                 g_free(name);
129         }
130
131         g_strfreev(fg_colors);
132         g_strfreev(bg_colors);
133
134         return;
135 }
136
137 static gint
138 color_filters_find_by_name_cb(gconstpointer arg1, gconstpointer arg2)
139 {
140         const color_filter_t *colorf = (const color_filter_t *)arg1;
141         const gchar          *name   = (const gchar *)arg2;
142
143         return (strstr(colorf->filter_name, name)==NULL) ? -1 : 0 ;
144 }
145
146
147 /* Set the filter off a temporary colorfilters and enable it */
148 void
149 color_filters_set_tmp(guint8 filt_nr, gchar *filter, gboolean disabled)
150 {
151         gchar          *name = NULL;
152         const gchar    *tmpfilter = NULL;
153         GSList         *cfl;
154         color_filter_t *colorf;
155         dfilter_t      *compiled_filter;
156         guint8         i;
157
158         /* Go through the tomporary filters and look for the same filter string.
159          * If found, clear it so that a filter can be "moved" up and down the list
160          */
161         for ( i=1 ; i<=10 ; i++ ) {
162                 /* If we need to reset the temporary filter (filter==NULL), don't look
163                  * for other rules with the same filter string
164                  */
165                 if( i!=filt_nr && filter==NULL )
166                         continue;
167
168                 name = g_strdup_printf("%s%02d",TEMP_COLOR_PREFIX,i);
169                 cfl = g_slist_find_custom(color_filter_list, (gpointer *) name, color_filters_find_by_name_cb);
170                 colorf = (color_filter_t *)cfl->data;
171
172                 /* Only change the filter rule if this is the rule to change or if
173                  * a matching filter string has been found
174                  */
175                 if(colorf && ( (i==filt_nr) || (strstr(filter,colorf->filter_text)!=NULL) ) ) {
176                         /* set filter string to "frame" if we are resetting the rules
177                          * or if we found a matching filter string which need to be cleared
178                          */
179                         tmpfilter = ( (filter==NULL) || (i!=filt_nr) ) ? "frame" : filter;
180                         if (!dfilter_compile(tmpfilter, &compiled_filter)) {
181                                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
182                                     "Could not compile color filter name: \"%s\""
183                                     " text: \"%s\".\n%s", name, filter, dfilter_error_msg);
184                         } else {
185                                 if (colorf->filter_text != NULL)
186                                         g_free(colorf->filter_text);
187                                 if (colorf->c_colorfilter != NULL)
188                                         dfilter_free(colorf->c_colorfilter);
189                                 colorf->filter_text = g_strdup(tmpfilter);
190                                 colorf->c_colorfilter = compiled_filter;
191                                 colorf->disabled = ((i!=filt_nr) ? TRUE : disabled);
192                                 /* Remember that there are now temporary coloring filters set */
193                                 if( filter )
194                                         tmp_colors_set = TRUE;
195                         }
196                 }
197                 g_free(name);
198         }
199         return;
200 }
201
202 /* Reset the temporary colorfilters */
203 void
204 color_filters_reset_tmp()
205 {
206         guint8 i;
207
208         for ( i=1 ; i<=10 ; i++ ) {
209             color_filters_set_tmp(i, NULL, TRUE);
210         }
211         /* Remember that there are now *no* temporary coloring filters set */
212         tmp_colors_set = FALSE;
213         return;
214 }
215
216 /* delete the specified filter */
217 void
218 color_filter_delete(color_filter_t *colorf)
219 {
220         if (colorf->filter_name != NULL)
221                 g_free(colorf->filter_name);
222         if (colorf->filter_text != NULL)
223                 g_free(colorf->filter_text);
224         if (colorf->c_colorfilter != NULL)
225                 dfilter_free(colorf->c_colorfilter);
226         g_free(colorf);
227 }
228
229 /* delete the specified filter (called from g_slist_foreach) */
230 static void
231 color_filter_delete_cb(gpointer filter_arg, gpointer unused _U_)
232 {
233         color_filter_t *colorf = (color_filter_t *)filter_arg;
234
235         color_filter_delete(colorf);
236 }
237
238 /* delete the specified list */
239 void
240 color_filter_list_delete(GSList **cfl)
241 {
242         g_slist_foreach(*cfl, color_filter_delete_cb, NULL);
243         g_slist_free(*cfl);
244         *cfl = NULL;
245 }
246
247 /* clone a single list entries from normal to edit list */
248 static color_filter_t *
249 color_filter_clone(color_filter_t *colorf)
250 {
251         color_filter_t *new_colorf;
252
253         new_colorf = (color_filter_t *)g_malloc(sizeof (color_filter_t));
254         new_colorf->filter_name = g_strdup(colorf->filter_name);
255         new_colorf->filter_text = g_strdup(colorf->filter_text);
256         new_colorf->bg_color = colorf->bg_color;
257         new_colorf->fg_color = colorf->fg_color;
258         new_colorf->disabled = colorf->disabled;
259         new_colorf->c_colorfilter = NULL;
260         new_colorf->edit_dialog = NULL;
261         new_colorf->selected = FALSE;
262
263         return new_colorf;
264 }
265
266 static void
267 color_filter_list_clone_cb(gpointer filter_arg, gpointer cfl_arg)
268 {
269         GSList **cfl = (GSList **)cfl_arg;
270         color_filter_t *new_colorf;
271
272         new_colorf = color_filter_clone((color_filter_t *)filter_arg);
273         *cfl = g_slist_append(*cfl, new_colorf);
274 }
275
276 /* clone the specified list */
277 static GSList *
278 color_filter_list_clone(GSList *cfl)
279 {
280         GSList *new_list = NULL;
281
282         g_slist_foreach(cfl, color_filter_list_clone_cb, &new_list);
283
284         return new_list;
285 }
286
287 /* Initialize the filter structures (reading from file) for general running, including app startup */
288 void
289 color_filters_init(void)
290 {
291         /* delete all currently existing filters */
292         color_filter_list_delete(&color_filter_list);
293
294         /* start the list with the temporary colorizing rules */
295         color_filters_add_tmp(&color_filter_list);
296
297         /* try to read the users filters */
298         if (!read_users_filters(&color_filter_list))
299                 /* if that failed, try to read the global filters */
300                 color_filters_read_globals(&color_filter_list);
301 }
302
303 void
304 color_filters_reload(void)
305 {
306         /* "move" old entries to the deleted list
307          * we must keep them until the dissection no longer needs them */
308         color_filter_deleted_list = g_slist_concat(color_filter_deleted_list, color_filter_list);
309         color_filter_list = NULL;
310
311         /* start the list with the temporary colorizing rules */
312         color_filters_add_tmp(&color_filter_list);
313
314         /* try to read the users filters */
315         if (!read_users_filters(&color_filter_list))
316                 /* if that failed, try to read the global filters */
317                 color_filters_read_globals(&color_filter_list);
318 }
319
320 void
321 color_filters_cleanup(void)
322 {
323         /* delete the previously deleted filters */
324         color_filter_list_delete(&color_filter_deleted_list);
325 }
326
327 static void
328 color_filters_clone_cb(gpointer filter_arg, gpointer user_data)
329 {
330         color_filter_t * new_colorf = color_filter_clone((color_filter_t *)filter_arg);
331         color_filter_add_cb (new_colorf, user_data);
332 }
333
334 void
335 color_filters_clone(gpointer user_data)
336 {
337         g_slist_foreach(color_filter_list, color_filters_clone_cb, user_data);
338 }
339
340
341 static void
342 color_filter_compile_cb(gpointer filter_arg, gpointer unused _U_)
343 {
344         color_filter_t *colorf = (color_filter_t *)filter_arg;
345
346         g_assert(colorf->c_colorfilter == NULL);
347         if (!dfilter_compile(colorf->filter_text, &colorf->c_colorfilter)) {
348                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
349                 "Could not compile color filter name: \"%s\" text: \"%s\".\n%s",
350                               colorf->filter_name, colorf->filter_text, dfilter_error_msg);
351                 /* this filter was compilable before, so this should never happen */
352                 /* except if the OK button of the parent window has been clicked */
353                 /* so don't use g_assert_not_reached() but check the filters again */
354         }
355 }
356
357 static void
358 color_filter_validate_cb(gpointer filter_arg, gpointer unused _U_)
359 {
360         color_filter_t *colorf = (color_filter_t *)filter_arg;
361
362         g_assert(colorf->c_colorfilter == NULL);
363         if (!dfilter_compile(colorf->filter_text, &colorf->c_colorfilter)) {
364                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
365                 "Removing color filter name: \"%s\" text: \"%s\".\n%s",
366                               colorf->filter_name, colorf->filter_text, dfilter_error_msg);
367                 /* Delete the color filter from the list of color filters. */
368                 color_filter_valid_list = g_slist_remove(color_filter_valid_list, colorf);
369                 color_filter_delete(colorf);
370         }
371 }
372
373 /* apply changes from the edit list */
374 void
375 color_filters_apply(GSList *tmp_cfl, GSList *edit_cfl)
376 {
377         /* "move" old entries to the deleted list
378          * we must keep them until the dissection no longer needs them */
379         color_filter_deleted_list = g_slist_concat(color_filter_deleted_list, color_filter_list);
380         color_filter_list = NULL;
381
382         /* clone all list entries from tmp/edit to normal list */
383         color_filter_valid_list = NULL;
384         color_filter_valid_list = color_filter_list_clone(tmp_cfl);
385         color_filter_valid_list = g_slist_concat(color_filter_valid_list,
386                                                  color_filter_list_clone(edit_cfl) );
387
388         /* compile all filter */
389         g_slist_foreach(color_filter_valid_list, color_filter_validate_cb, NULL);
390
391         /* clone all list entries from tmp/edit to normal list */
392         color_filter_list = color_filter_list_clone(color_filter_valid_list);
393
394         /* compile all filter */
395         g_slist_foreach(color_filter_list, color_filter_compile_cb, NULL);
396 }
397
398 gboolean
399 color_filters_used(void)
400 {
401         return color_filter_list != NULL && filters_enabled;
402 }
403
404 gboolean
405 tmp_color_filters_used(void)
406 {
407         return tmp_colors_set;
408 }
409
410 void
411 color_filters_enable(gboolean enable)
412 {
413         new_packet_list_enable_color(enable);
414 }
415
416
417 /* prepare the epan_dissect_t for the filter */
418 static void
419 prime_edt(gpointer data, gpointer user_data)
420 {
421         color_filter_t  *colorf = (color_filter_t *)data;
422         epan_dissect_t   *edt = (epan_dissect_t *)user_data;
423
424         if (colorf->c_colorfilter != NULL)
425                 epan_dissect_prime_dfilter(edt, colorf->c_colorfilter);
426 }
427
428 /* Prime the epan_dissect_t with all the compiler
429  * color filters in 'color_filter_list'. */
430 void
431 color_filters_prime_edt(epan_dissect_t *edt)
432 {
433         if (color_filters_used())
434                 g_slist_foreach(color_filter_list, prime_edt, edt);
435 }
436
437 /* * Return the color_t for later use */
438 const color_filter_t *
439 color_filters_colorize_packet(epan_dissect_t *edt)
440 {
441         GSList *curr;
442         color_filter_t *colorf;
443
444         /* If we have color filters, "search" for the matching one. */
445         if (color_filters_used()) {
446                 curr = color_filter_list;
447
448                 while(curr != NULL) {
449                         colorf = (color_filter_t *)curr->data;
450                         if ( (!colorf->disabled) &&
451                              (colorf->c_colorfilter != NULL) &&
452                              dfilter_apply_edt(colorf->c_colorfilter, edt)) {
453                                 return colorf;
454                         }
455                         curr = g_slist_next(curr);
456                 }
457         }
458
459         return NULL;
460 }
461
462 /* read filters from the given file */
463 /* XXX - Would it make more sense to use GStrings here instead of reallocing
464    our buffers? */
465 static gboolean
466 read_filters_file(FILE *f, gpointer user_data)
467 {
468 #define INIT_BUF_SIZE 128
469         gchar  *name = NULL;
470         gchar  *filter_exp = NULL;
471         guint32 name_len = INIT_BUF_SIZE;
472         guint32 filter_exp_len = INIT_BUF_SIZE;
473         guint32 i = 0;
474         gint32  c;
475         guint16 fg_r, fg_g, fg_b, bg_r, bg_g, bg_b;
476         gboolean disabled = FALSE;
477         gboolean skip_end_of_line = FALSE;
478
479         name = (gchar *)g_malloc(name_len + 1);
480         filter_exp = (gchar *)g_malloc(filter_exp_len + 1);
481
482         while (1) {
483
484                 if (skip_end_of_line) {
485                         do {
486                                 c = getc(f);
487                         } while (c != EOF && c != '\n');
488                         if (c == EOF)
489                                 break;
490                         disabled = FALSE;
491                         skip_end_of_line = FALSE;
492                 }
493
494                 while ((c = getc(f)) != EOF && isspace(c)) {
495                         if (c == '\n') {
496                                 continue;
497                         }
498                 }
499
500                 if (c == EOF)
501                         break;
502
503                 if (c == '!') {
504                         disabled = TRUE;
505                         continue;
506                 }
507
508                 /* skip # comments and invalid lines */
509                 if (c != '@') {
510                         skip_end_of_line = TRUE;
511                         continue;
512                 }
513
514                 /* we get the @ delimiter.
515                  * Format is:
516                  * @name@filter expression@[background r,g,b][foreground r,g,b]
517                  */
518
519                 /* retrieve name */
520                 i = 0;
521                 while (1) {
522                         c = getc(f);
523                         if (c == EOF || c == '@')
524                                 break;
525                         if (i >= name_len) {
526                                 /* buffer isn't long enough; double its length.*/
527                                 name_len *= 2;
528                                 name = (gchar *)g_realloc(name, name_len + 1);
529                         }
530                         name[i++] = c;
531                 }
532                 name[i] = '\0';
533
534                 if (c == EOF) {
535                         break;
536                 } else if (i == 0) {
537                         skip_end_of_line = TRUE;
538                         continue;
539                 }
540
541                 /* retrieve filter expression */
542                 i = 0;
543                 while (1) {
544                         c = getc(f);
545                         if (c == EOF || c == '@')
546                                 break;
547                         if (i >= filter_exp_len) {
548                                 /* buffer isn't long enough; double its length.*/
549                                 filter_exp_len *= 2;
550                                 filter_exp = (gchar *)g_realloc(filter_exp, filter_exp_len + 1);
551                         }
552                         filter_exp[i++] = c;
553                 }
554                 filter_exp[i] = '\0';
555
556                 if (c == EOF) {
557                         break;
558                 } else if (i == 0) {
559                         skip_end_of_line = TRUE;
560                         continue;
561                 }
562
563                 /* retrieve background and foreground colors */
564                 if (fscanf(f,"[%hu,%hu,%hu][%hu,%hu,%hu]",
565                         &bg_r, &bg_g, &bg_b, &fg_r, &fg_g, &fg_b) == 6) {
566
567                         /* we got a complete color filter */
568
569                         color_t bg_color, fg_color;
570                         color_filter_t *colorf;
571                         dfilter_t *temp_dfilter;
572
573                         if (!dfilter_compile(filter_exp, &temp_dfilter)) {
574                                 g_warning("Could not compile color filter \"%s\" from saved filters: %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 = (struct write_filter_data *)data_arg;
707         color_filter_t *colorf = (color_filter_t *)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