From Kovarththanan Rajaratnam:
[metze/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 #ifdef NEW_PACKET_LIST
412         new_packet_list_enable_color(enable);
413 #else
414         filters_enabled = enable;
415 #endif
416 }
417
418
419 /* prepare the epan_dissect_t for the filter */
420 static void
421 prime_edt(gpointer data, gpointer user_data)
422 {
423         color_filter_t  *colorf = data;
424         epan_dissect_t   *edt = user_data;
425
426         if (colorf->c_colorfilter != NULL)
427                 epan_dissect_prime_dfilter(edt, colorf->c_colorfilter);
428 }
429
430 /* Prime the epan_dissect_t with all the compiler
431  * color filters in 'color_filter_list'. */
432 void
433 color_filters_prime_edt(epan_dissect_t *edt)
434 {
435         if (color_filters_used())
436                 g_slist_foreach(color_filter_list, prime_edt, edt);
437 }
438
439 /* Colorize a single packet of the packet list (old packet list)
440  * (row is only _U_ for the NEW_PACKET_LIST case
441  * Return the color_t for later use (new packet list) */
442 color_filter_t *
443 color_filters_colorize_packet(gint row _U_, epan_dissect_t *edt)
444 {
445     GSList *curr;
446     color_filter_t *colorf;
447
448     /* If we have color filters, "search" for the matching one. */
449     if (color_filters_used()) {
450         curr = color_filter_list;
451
452         while(curr != NULL) {
453             colorf = curr->data;
454             if ( (!colorf->disabled) &&
455                  (colorf->c_colorfilter != NULL) &&
456                  dfilter_apply_edt(colorf->c_colorfilter, edt)) {
457                     /* this is the filter to use, apply it to the packet list */
458 #ifndef NEW_PACKET_LIST
459                     /* We'll do this in the column cell function instead. */
460                     packet_list_set_colors(row, &(colorf->fg_color), &(colorf->bg_color));
461 #endif
462                     return colorf;
463             }
464             curr = g_slist_next(curr);
465         }
466     }
467
468     return NULL;
469 }
470
471 /* read filters from the given file */
472 /* XXX - Would it make more sense to use GStrings here instead of reallocing
473    our buffers? */
474 static gboolean
475 read_filters_file(FILE *f, gpointer user_data)
476 {
477 #define INIT_BUF_SIZE 128
478         gchar  *name = NULL;
479         gchar  *filter_exp = NULL;
480         guint32 name_len = INIT_BUF_SIZE;
481         guint32 filter_exp_len = INIT_BUF_SIZE;
482         guint32 i = 0;
483         gint32  c;
484         guint16 fg_r, fg_g, fg_b, bg_r, bg_g, bg_b;
485         gboolean disabled = FALSE;
486         gboolean skip_end_of_line = FALSE;
487
488         name = g_malloc(name_len + 1);
489         filter_exp = g_malloc(filter_exp_len + 1);
490
491         while (1) {
492
493                 if (skip_end_of_line) {
494                         do {
495                                 c = getc(f);
496                         } while (c != EOF && c != '\n');
497                         if (c == EOF)
498                                 break;
499                         disabled = FALSE;
500                         skip_end_of_line = FALSE;
501                 }
502
503                 while ((c = getc(f)) != EOF && isspace(c)) {
504                         if (c == '\n') {
505                                 continue;
506                         }
507                 }
508
509                 if (c == EOF)
510                         break;
511
512                 if (c == '!') {
513                         disabled = TRUE;
514                         continue;
515                 }
516
517                 /* skip # comments and invalid lines */
518                 if (c != '@') {
519                         skip_end_of_line = TRUE;
520                         continue;
521                 }
522
523                 /* we get the @ delimiter.
524                  * Format is:
525                  * @name@filter expression@[background r,g,b][foreground r,g,b]
526                  */
527
528                 /* retrieve name */
529                 i = 0;
530                 while (1) {
531                         c = getc(f);
532                         if (c == EOF || c == '@')
533                                 break;
534                         if (i >= name_len) {
535                                 /* buffer isn't long enough; double its length.*/
536                                 name_len *= 2;
537                                 name = g_realloc(name, name_len + 1);
538                         }
539                         name[i++] = c;
540                 }
541                 name[i] = '\0';
542
543                 if (c == EOF) {
544                         break;
545                 } else if (i == 0) {
546                         skip_end_of_line = TRUE;
547                         continue;
548                 }
549
550                 /* retrieve filter expression */
551                 i = 0;
552                 while (1) {
553                         c = getc(f);
554                         if (c == EOF || c == '@')
555                                 break;
556                         if (i >= filter_exp_len) {
557                                 /* buffer isn't long enough; double its length.*/
558                                 filter_exp_len *= 2;
559                                 filter_exp = g_realloc(filter_exp, filter_exp_len + 1);
560                         }
561                         filter_exp[i++] = c;
562                 }
563                 filter_exp[i] = '\0';
564
565                 if (c == EOF) {
566                         break;
567                 } else if (i == 0) {
568                         skip_end_of_line = TRUE;
569                         continue;
570                 }
571
572                 /* retrieve background and foreground colors */
573                 if (fscanf(f,"[%hu,%hu,%hu][%hu,%hu,%hu]",
574                         &bg_r, &bg_g, &bg_b, &fg_r, &fg_g, &fg_b) == 6) {
575
576                         /* we got a complete color filter */
577
578                         color_t bg_color, fg_color;
579                         color_filter_t *colorf;
580                         dfilter_t *temp_dfilter;
581
582                         if (!dfilter_compile(filter_exp, &temp_dfilter)) {
583                                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
584                                 "Could not compile color filter %s from saved filters.\n%s",
585                                               name, dfilter_error_msg);
586                                 skip_end_of_line = TRUE;
587                                 continue;
588                         }
589
590                         if (!initialize_color(&fg_color, fg_r, fg_g, fg_b)) {
591                                 /* oops */
592                                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
593                                     "Could not allocate foreground color "
594                                     "specified in input file for %s.", name);
595                                 dfilter_free(temp_dfilter);
596                                 skip_end_of_line = TRUE;
597                                 continue;
598                         }
599                         if (!initialize_color(&bg_color, bg_r, bg_g, bg_b)) {
600                                 /* oops */
601                                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
602                                     "Could not allocate background color "
603                                     "specified in input file for %s.", name);
604                                 dfilter_free(temp_dfilter);
605                                 skip_end_of_line = TRUE;
606                                 continue;
607                         }
608
609                         colorf = color_filter_new(name, filter_exp, &bg_color,
610                             &fg_color, disabled);
611                         if(user_data == &color_filter_list) {
612                                 GSList **cfl = (GSList **)user_data;
613
614                                 /* internal call */
615                                 colorf->c_colorfilter = temp_dfilter;
616                                 *cfl = g_slist_append(*cfl, colorf);
617                         } else {
618                                 /* external call */
619                                 /* just editing, don't need the compiled filter */
620                                 dfilter_free(temp_dfilter);
621                                 color_filter_add_cb (colorf, user_data);
622                         }
623                 }    /* if sscanf */
624
625                 skip_end_of_line = TRUE;
626         }
627
628         g_free(name);
629         g_free(filter_exp);
630         return TRUE;
631 }
632
633 /* read filters from the user's filter file */
634 static gboolean
635 read_users_filters(GSList **cfl)
636 {
637         gchar *path;
638         FILE *f;
639         gboolean ret;
640
641         /* decide what file to open (from dfilter code) */
642         path = get_persconffile_path("colorfilters", TRUE, FALSE);
643         if ((f = ws_fopen(path, "r")) == NULL) {
644                 if (errno != ENOENT) {
645                         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
646                             "Could not open filter file\n\"%s\": %s.", path,
647                             strerror(errno));
648                 }
649                 g_free(path);
650                 return FALSE;
651         }
652         g_free(path);
653         path = NULL;
654
655         ret = read_filters_file(f, cfl);
656         fclose(f);
657         return ret;
658 }
659
660 /* read filters from the filter file */
661 gboolean
662 color_filters_read_globals(gpointer user_data)
663 {
664         gchar *path;
665         FILE *f;
666         gboolean ret;
667
668         /* decide what file to open (from dfilter code) */
669         path = get_datafile_path("colorfilters");
670         if ((f = ws_fopen(path, "r")) == NULL) {
671                 if (errno != ENOENT) {
672                         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
673                             "Could not open global filter file\n\"%s\": %s.", path,
674                             strerror(errno));
675                 }
676                 g_free(path);
677                 return FALSE;
678         }
679         g_free(path);
680         path = NULL;
681
682         ret = read_filters_file(f, user_data);
683         fclose(f);
684         return ret;
685 }
686
687 /* read filters from some other filter file (import) */
688 gboolean
689 color_filters_import(gchar *path, gpointer user_data)
690 {
691         FILE *f;
692         gboolean ret;
693
694         if ((f = ws_fopen(path, "r")) == NULL) {
695                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
696                     "Could not open\n%s\nfor reading: %s.",
697                     path, strerror(errno));
698                 return FALSE;
699         }
700
701         ret = read_filters_file(f, user_data);
702         fclose(f);
703         return ret;
704 }
705
706 struct write_filter_data
707 {
708   FILE * f;
709   gboolean only_selected;
710 };
711
712 /* save a single filter */
713 static void
714 write_filter(gpointer filter_arg, gpointer data_arg)
715 {
716         struct write_filter_data *data = data_arg;
717         color_filter_t *colorf = filter_arg;
718         FILE *f = data->f;
719
720         if ( (colorf->selected || !data->only_selected) &&
721              (strstr(colorf->filter_name,TEMP_COLOR_PREFIX)==NULL) ) {
722                 fprintf(f,"%s@%s@%s@[%d,%d,%d][%d,%d,%d]\n",
723                     colorf->disabled ? "!" : "",
724                     colorf->filter_name,
725                     colorf->filter_text,
726                     colorf->bg_color.red,
727                     colorf->bg_color.green,
728                     colorf->bg_color.blue,
729                     colorf->fg_color.red,
730                     colorf->fg_color.green,
731                     colorf->fg_color.blue);
732         }
733 }
734
735 /* save filters in a filter file */
736 static gboolean
737 write_filters_file(GSList *cfl, FILE *f, gboolean only_selected)
738 {
739         struct write_filter_data data;
740
741         data.f = f;
742         data.only_selected = only_selected;
743
744         fprintf(f,"# DO NOT EDIT THIS FILE!  It was created by Wireshark\n");
745         g_slist_foreach(cfl, write_filter, &data);
746         return TRUE;
747 }
748
749 /* save filters in users filter file */
750 gboolean
751 color_filters_write(GSList *cfl)
752 {
753         gchar *pf_dir_path;
754         gchar *path;
755         FILE *f;
756
757         /* Create the directory that holds personal configuration files,
758            if necessary.  */
759         if (create_persconffile_dir(&pf_dir_path) == -1) {
760                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
761                     "Can't create directory\n\"%s\"\nfor color files: %s.",
762                     pf_dir_path, strerror(errno));
763                 g_free(pf_dir_path);
764                 return FALSE;
765         }
766
767         path = get_persconffile_path("colorfilters", TRUE, TRUE);
768         if ((f = ws_fopen(path, "w+")) == NULL) {
769                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
770                     "Could not open\n%s\nfor writing: %s.",
771                     path, strerror(errno));
772                 g_free(path);
773                 return FALSE;
774         }
775         g_free(path);
776         write_filters_file(cfl, f, FALSE);
777         fclose(f);
778         return TRUE;
779 }
780
781 /* save filters in some other filter file (export) */
782 gboolean
783 color_filters_export(gchar *path, GSList *cfl, gboolean only_marked)
784 {
785         FILE *f;
786
787         if ((f = ws_fopen(path, "w+")) == NULL) {
788                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
789                     "Could not open\n%s\nfor writing: %s.",
790                     path, strerror(errno));
791                 return FALSE;
792         }
793         write_filters_file(cfl, f, only_marked);
794         fclose(f);
795         return TRUE;
796 }
797