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