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