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