Witness: enum witness_notifyResponse_type
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24 /*
25  * Updated 1 Dec 10 jjm
26  */
27
28 #include "config.h"
29
30 #include <glib.h>
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <ctype.h>
35 #include <string.h>
36
37 #include <wsutil/filesystem.h>
38 #include <wsutil/file_util.h>
39
40 #include <epan/packet.h>
41 #include "color.h"
42 #include "color_filters.h"
43 #include "file.h"
44 #include <epan/dfilter/dfilter.h>
45 #include <epan/prefs.h>
46
47 #include "ui/simple_dialog.h"
48 #include "ui/ui_util.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->color_edit_dlg_info = 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     gulong          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",CONVERSATION_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",CONVERSATION_COLOR_PREFIX,i);
169         cfl = g_slist_find_custom(color_filter_list, 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(void)
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->color_edit_dlg_info = 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     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 \"%s\" in colorfilters file.\n%s",
575                           name, dfilter_error_msg);
576                 prefs.unknown_colorfilters = TRUE;
577
578                 skip_end_of_line = TRUE;
579                 continue;
580             }
581
582             if (!initialize_color(&fg_color, fg_r, fg_g, fg_b)) {
583                 /* oops */
584                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
585                               "Could not allocate foreground color "
586                               "specified in input file for %s.", name);
587                 dfilter_free(temp_dfilter);
588                 skip_end_of_line = TRUE;
589                 continue;
590             }
591             if (!initialize_color(&bg_color, bg_r, bg_g, bg_b)) {
592                 /* oops */
593                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
594                               "Could not allocate background color "
595                               "specified in input file for %s.", name);
596                 dfilter_free(temp_dfilter);
597                 skip_end_of_line = TRUE;
598                 continue;
599             }
600
601             colorf = color_filter_new(name, filter_exp, &bg_color,
602                                       &fg_color, disabled);
603             if(user_data == &color_filter_list) {
604                 GSList **cfl = (GSList **)user_data;
605
606                 /* internal call */
607                 colorf->c_colorfilter = temp_dfilter;
608                 *cfl = g_slist_append(*cfl, colorf);
609             } else {
610                 /* external call */
611                 /* just editing, don't need the compiled filter */
612                 dfilter_free(temp_dfilter);
613                 color_filter_add_cb (colorf, user_data);
614             }
615         }    /* if sscanf */
616
617         skip_end_of_line = TRUE;
618     }
619
620     g_free(name);
621     g_free(filter_exp);
622     return TRUE;
623 }
624
625 /* read filters from the user's filter file */
626 static gboolean
627 read_users_filters(GSList **cfl)
628 {
629     gchar    *path;
630     FILE     *f;
631     gboolean  ret;
632
633     /* decide what file to open (from dfilter code) */
634     path = get_persconffile_path("colorfilters", TRUE);
635     if ((f = ws_fopen(path, "r")) == NULL) {
636         if (errno != ENOENT) {
637             simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
638                           "Could not open filter file\n\"%s\": %s.", path,
639                           g_strerror(errno));
640         }
641         g_free(path);
642         return FALSE;
643     }
644     g_free(path);
645     path = NULL;
646
647     ret = read_filters_file(f, cfl);
648     fclose(f);
649     return ret;
650 }
651
652 /* read filters from the filter file */
653 gboolean
654 color_filters_read_globals(gpointer user_data)
655 {
656     gchar    *path;
657     FILE     *f;
658     gboolean  ret;
659
660     /* decide what file to open (from dfilter code) */
661     path = get_datafile_path("colorfilters");
662     if ((f = ws_fopen(path, "r")) == NULL) {
663         if (errno != ENOENT) {
664             simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
665                           "Could not open global filter file\n\"%s\": %s.", path,
666                           g_strerror(errno));
667         }
668         g_free(path);
669         return FALSE;
670     }
671     g_free(path);
672     path = NULL;
673
674     ret = read_filters_file(f, user_data);
675     fclose(f);
676     return ret;
677 }
678
679 /* read filters from some other filter file (import) */
680 gboolean
681 color_filters_import(gchar *path, gpointer user_data)
682 {
683     FILE     *f;
684     gboolean  ret;
685
686     if ((f = ws_fopen(path, "r")) == NULL) {
687         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
688                       "Could not open\n%s\nfor reading: %s.",
689                       path, g_strerror(errno));
690         return FALSE;
691     }
692
693     ret = read_filters_file(f, user_data);
694     fclose(f);
695     return ret;
696 }
697
698 struct write_filter_data
699 {
700     FILE     *f;
701     gboolean  only_selected;
702 };
703
704 /* save a single filter */
705 static void
706 write_filter(gpointer filter_arg, gpointer data_arg)
707 {
708     struct write_filter_data *data = (struct write_filter_data *)data_arg;
709     color_filter_t *colorf = (color_filter_t *)filter_arg;
710     FILE *f = data->f;
711
712     if ( (colorf->selected || !data->only_selected) &&
713          (strstr(colorf->filter_name,CONVERSATION_COLOR_PREFIX)==NULL) ) {
714         fprintf(f,"%s@%s@%s@[%d,%d,%d][%d,%d,%d]\n",
715                 colorf->disabled ? "!" : "",
716                 colorf->filter_name,
717                 colorf->filter_text,
718                 colorf->bg_color.red,
719                 colorf->bg_color.green,
720                 colorf->bg_color.blue,
721                 colorf->fg_color.red,
722                 colorf->fg_color.green,
723                 colorf->fg_color.blue);
724     }
725 }
726
727 /* save filters in a filter file */
728 static gboolean
729 write_filters_file(GSList *cfl, FILE *f, gboolean only_selected)
730 {
731     struct write_filter_data data;
732
733     data.f = f;
734     data.only_selected = only_selected;
735
736     fprintf(f,"# DO NOT EDIT THIS FILE!  It was created by Wireshark\n");
737     g_slist_foreach(cfl, write_filter, &data);
738     return TRUE;
739 }
740
741 /* save filters in users filter file */
742 gboolean
743 color_filters_write(GSList *cfl)
744 {
745     gchar *pf_dir_path;
746     gchar *path;
747     FILE  *f;
748
749     /* Create the directory that holds personal configuration files,
750        if necessary.  */
751     if (create_persconffile_dir(&pf_dir_path) == -1) {
752         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
753                       "Can't create directory\n\"%s\"\nfor color files: %s.",
754                       pf_dir_path, g_strerror(errno));
755         g_free(pf_dir_path);
756         return FALSE;
757     }
758
759     path = get_persconffile_path("colorfilters", TRUE);
760     if ((f = ws_fopen(path, "w+")) == NULL) {
761         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
762                       "Could not open\n%s\nfor writing: %s.",
763                       path, g_strerror(errno));
764         g_free(path);
765         return FALSE;
766     }
767     g_free(path);
768     write_filters_file(cfl, f, FALSE);
769     fclose(f);
770     return TRUE;
771 }
772
773 /* save filters in some other filter file (export) */
774 gboolean
775 color_filters_export(gchar *path, GSList *cfl, gboolean only_marked)
776 {
777     FILE *f;
778
779     if ((f = ws_fopen(path, "w+")) == NULL) {
780         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
781                       "Could not open\n%s\nfor writing: %s.",
782                       path, g_strerror(errno));
783         return FALSE;
784     }
785     write_filters_file(cfl, f, only_marked);
786     fclose(f);
787     return TRUE;
788 }
789
790 /*
791  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
792  *
793  * Local variables:
794  * c-basic-offset: 4
795  * tab-width: 8
796  * indent-tabs-mode: nil
797  * End:
798  *
799  * vi: set shiftwidth=4 tabstop=8 expandtab:
800  * :indentSize=4:tabSize=8:noTabs=true:
801  */