If we weren't started with special privileges, try to figure out if
[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 <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 <epan/prefs.h>
45
46 #include "ui/simple_dialog.h"
47 #include "ui/ui_util.h"
48
49 #define RED_COMPONENT(x)   (guint16) (((((x) >> 16) & 0xff) * 65535 / 255))
50 #define GREEN_COMPONENT(x) (guint16) (((((x) >>  8) & 0xff) * 65535 / 255))
51 #define BLUE_COMPONENT(x)  (guint16) ( (((x)        & 0xff) * 65535 / 255))
52
53 static gboolean read_users_filters(GSList **cfl);
54
55 /* the currently active filters */
56 static GSList *color_filter_list = NULL;
57
58 /* keep "old" deleted filters in this list until
59  * the dissection no longer needs them (e.g. file is closed) */
60 static GSList *color_filter_deleted_list = NULL;
61 static GSList *color_filter_valid_list   = NULL;
62
63 /* Color Filters can en-/disabled. */
64 static gboolean filters_enabled = TRUE;
65
66 /* Remember if there are temporary coloring filters set to
67  * add sensitivity to the "Reset Coloring 1-10" menu item
68  */
69 static gboolean tmp_colors_set = FALSE;
70
71 /* Create a new filter */
72 color_filter_t *
73 color_filter_new(const gchar *name,          /* The name of the filter to create */
74                  const gchar *filter_string, /* The string representing the filter */
75                  color_t     *bg_color,      /* The background color */
76                  color_t     *fg_color,      /* The foreground color */
77                  gboolean     disabled)      /* Is the filter disabled? */
78 {
79     color_filter_t *colorf;
80
81     colorf                      = (color_filter_t *)g_malloc(sizeof (color_filter_t));
82     colorf->filter_name         = g_strdup(name);
83     colorf->filter_text         = g_strdup(filter_string);
84     colorf->bg_color            = *bg_color;
85     colorf->fg_color            = *fg_color;
86     colorf->disabled            = disabled;
87     colorf->c_colorfilter       = NULL;
88     colorf->color_edit_dlg_info = NULL;
89     colorf->selected            = FALSE;
90     return colorf;
91 }
92
93 /* Add ten empty (temporary) colorfilters for easy coloring */
94 static void
95 color_filters_add_tmp(GSList **cfl)
96 {
97     gchar          *name = NULL;
98     guint32         i;
99     gchar**         bg_colors;
100     gchar**         fg_colors;
101     gulong          cval;
102     color_t         bg_color, fg_color;
103     color_filter_t *colorf;
104
105     g_assert(strlen(prefs.gui_colorized_fg)==69);
106     g_assert(strlen(prefs.gui_colorized_bg)==69);
107     fg_colors = g_strsplit(prefs.gui_colorized_fg, ",", -1);
108     bg_colors = g_strsplit(prefs.gui_colorized_bg, ",", -1);
109
110     for ( i=1 ; i<=10 ; i++ ) {
111         name = g_strdup_printf("%s%02d",CONVERSATION_COLOR_PREFIX,i);
112
113         /* retrieve background and foreground colors */
114         cval = strtoul(fg_colors[i-1], NULL, 16);
115         initialize_color(&fg_color, RED_COMPONENT(cval),
116                          GREEN_COMPONENT(cval),
117                          BLUE_COMPONENT(cval) );
118         cval = strtoul(bg_colors[i-1], NULL, 16);
119         initialize_color(&bg_color, RED_COMPONENT(cval),
120                          GREEN_COMPONENT(cval),
121                          BLUE_COMPONENT(cval) );
122         colorf = color_filter_new(name, NULL, &bg_color, &fg_color, TRUE);
123         colorf->filter_text = g_strdup("frame");
124         colorf->c_colorfilter = NULL;
125         *cfl = g_slist_append(*cfl, colorf);
126
127         g_free(name);
128     }
129
130     g_strfreev(fg_colors);
131     g_strfreev(bg_colors);
132
133     return;
134 }
135
136 static gint
137 color_filters_find_by_name_cb(gconstpointer arg1, gconstpointer arg2)
138 {
139     const color_filter_t *colorf = (const color_filter_t *)arg1;
140     const gchar          *name   = (const gchar *)arg2;
141
142     return (strstr(colorf->filter_name, name)==NULL) ? -1 : 0 ;
143 }
144
145
146 /* Set the filter off a temporary colorfilters and enable it */
147 void
148 color_filters_set_tmp(guint8 filt_nr, gchar *filter, gboolean disabled)
149 {
150     gchar          *name = NULL;
151     const gchar    *tmpfilter = NULL;
152     GSList         *cfl;
153     color_filter_t *colorf;
154     dfilter_t      *compiled_filter;
155     guint8         i;
156
157     /* Go through the tomporary filters and look for the same filter string.
158      * If found, clear it so that a filter can be "moved" up and down the list
159      */
160     for ( i=1 ; i<=10 ; i++ ) {
161         /* If we need to reset the temporary filter (filter==NULL), don't look
162          * for other rules with the same filter string
163          */
164         if( i!=filt_nr && filter==NULL )
165             continue;
166
167         name = g_strdup_printf("%s%02d",CONVERSATION_COLOR_PREFIX,i);
168         cfl = g_slist_find_custom(color_filter_list, name, color_filters_find_by_name_cb);
169         colorf = (color_filter_t *)cfl->data;
170
171         /* Only change the filter rule if this is the rule to change or if
172          * a matching filter string has been found
173          */
174         if(colorf && ( (i==filt_nr) || (strstr(filter,colorf->filter_text)!=NULL) ) ) {
175             /* set filter string to "frame" if we are resetting the rules
176              * or if we found a matching filter string which need to be cleared
177              */
178             tmpfilter = ( (filter==NULL) || (i!=filt_nr) ) ? "frame" : filter;
179             if (!dfilter_compile(tmpfilter, &compiled_filter)) {
180                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
181                               "Could not compile color filter name: \"%s\""
182                               " text: \"%s\".\n%s", name, filter, dfilter_error_msg);
183             } else {
184                 if (colorf->filter_text != NULL)
185                     g_free(colorf->filter_text);
186                 if (colorf->c_colorfilter != NULL)
187                     dfilter_free(colorf->c_colorfilter);
188                 colorf->filter_text = g_strdup(tmpfilter);
189                 colorf->c_colorfilter = compiled_filter;
190                 colorf->disabled = ((i!=filt_nr) ? TRUE : disabled);
191                 /* Remember that there are now temporary coloring filters set */
192                 if( filter )
193                     tmp_colors_set = TRUE;
194             }
195         }
196         g_free(name);
197     }
198     return;
199 }
200
201 /* Reset the temporary colorfilters */
202 void
203 color_filters_reset_tmp(void)
204 {
205     guint8 i;
206
207     for ( i=1 ; i<=10 ; i++ ) {
208         color_filters_set_tmp(i, NULL, TRUE);
209     }
210     /* Remember that there are now *no* temporary coloring filters set */
211     tmp_colors_set = FALSE;
212     return;
213 }
214
215 /* delete the specified filter */
216 void
217 color_filter_delete(color_filter_t *colorf)
218 {
219     if (colorf->filter_name != NULL)
220         g_free(colorf->filter_name);
221     if (colorf->filter_text != NULL)
222         g_free(colorf->filter_text);
223     if (colorf->c_colorfilter != NULL)
224         dfilter_free(colorf->c_colorfilter);
225     g_free(colorf);
226 }
227
228 /* delete the specified filter (called from g_slist_foreach) */
229 static void
230 color_filter_delete_cb(gpointer filter_arg, gpointer unused _U_)
231 {
232     color_filter_t *colorf = (color_filter_t *)filter_arg;
233
234     color_filter_delete(colorf);
235 }
236
237 /* delete the specified list */
238 void
239 color_filter_list_delete(GSList **cfl)
240 {
241     g_slist_foreach(*cfl, color_filter_delete_cb, NULL);
242     g_slist_free(*cfl);
243     *cfl = NULL;
244 }
245
246 /* clone a single list entries from normal to edit list */
247 static color_filter_t *
248 color_filter_clone(color_filter_t *colorf)
249 {
250     color_filter_t *new_colorf;
251
252     new_colorf                      = (color_filter_t *)g_malloc(sizeof (color_filter_t));
253     new_colorf->filter_name         = g_strdup(colorf->filter_name);
254     new_colorf->filter_text         = g_strdup(colorf->filter_text);
255     new_colorf->bg_color            = colorf->bg_color;
256     new_colorf->fg_color            = colorf->fg_color;
257     new_colorf->disabled            = colorf->disabled;
258     new_colorf->c_colorfilter       = NULL;
259     new_colorf->color_edit_dlg_info = NULL;
260     new_colorf->selected            = FALSE;
261
262     return new_colorf;
263 }
264
265 static void
266 color_filter_list_clone_cb(gpointer filter_arg, gpointer cfl_arg)
267 {
268     GSList **cfl = (GSList **)cfl_arg;
269     color_filter_t *new_colorf;
270
271     new_colorf = color_filter_clone((color_filter_t *)filter_arg);
272     *cfl = g_slist_append(*cfl, new_colorf);
273 }
274
275 /* clone the specified list */
276 static GSList *
277 color_filter_list_clone(GSList *cfl)
278 {
279     GSList *new_list = NULL;
280
281     g_slist_foreach(cfl, color_filter_list_clone_cb, &new_list);
282
283     return new_list;
284 }
285
286 /* Initialize the filter structures (reading from file) for general running, including app startup */
287 void
288 color_filters_init(void)
289 {
290     /* delete all currently existing filters */
291     color_filter_list_delete(&color_filter_list);
292
293     /* start the list with the temporary colorizing rules */
294     color_filters_add_tmp(&color_filter_list);
295
296     /* try to read the users filters */
297     if (!read_users_filters(&color_filter_list))
298         /* if that failed, try to read the global filters */
299         color_filters_read_globals(&color_filter_list);
300 }
301
302 void
303 color_filters_reload(void)
304 {
305     /* "move" old entries to the deleted list
306      * we must keep them until the dissection no longer needs them */
307     color_filter_deleted_list = g_slist_concat(color_filter_deleted_list, color_filter_list);
308     color_filter_list = NULL;
309
310     /* start the list with the temporary colorizing rules */
311     color_filters_add_tmp(&color_filter_list);
312
313     /* try to read the users filters */
314     if (!read_users_filters(&color_filter_list))
315         /* if that failed, try to read the global filters */
316         color_filters_read_globals(&color_filter_list);
317 }
318
319 void
320 color_filters_cleanup(void)
321 {
322     /* delete the previously deleted filters */
323     color_filter_list_delete(&color_filter_deleted_list);
324 }
325
326 static void
327 color_filters_clone_cb(gpointer filter_arg, gpointer user_data)
328 {
329     color_filter_t * new_colorf = color_filter_clone((color_filter_t *)filter_arg);
330     color_filter_add_cb (new_colorf, user_data);
331 }
332
333 void
334 color_filters_clone(gpointer user_data)
335 {
336     g_slist_foreach(color_filter_list, color_filters_clone_cb, user_data);
337 }
338
339
340 static void
341 color_filter_compile_cb(gpointer filter_arg, gpointer unused _U_)
342 {
343     color_filter_t *colorf = (color_filter_t *)filter_arg;
344
345     g_assert(colorf->c_colorfilter == NULL);
346     if (!dfilter_compile(colorf->filter_text, &colorf->c_colorfilter)) {
347         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
348                       "Could not compile color filter name: \"%s\" text: \"%s\".\n%s",
349                       colorf->filter_name, colorf->filter_text, dfilter_error_msg);
350         /* this filter was compilable before, so this should never happen */
351         /* except if the OK button of the parent window has been clicked */
352         /* so don't use g_assert_not_reached() but check the filters again */
353     }
354 }
355
356 static void
357 color_filter_validate_cb(gpointer filter_arg, gpointer unused _U_)
358 {
359     color_filter_t *colorf = (color_filter_t *)filter_arg;
360
361     g_assert(colorf->c_colorfilter == NULL);
362     if (!dfilter_compile(colorf->filter_text, &colorf->c_colorfilter)) {
363         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
364                       "Removing color filter name: \"%s\" text: \"%s\".\n%s",
365                       colorf->filter_name, colorf->filter_text, dfilter_error_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     gint32    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 && 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
572             if (!dfilter_compile(filter_exp, &temp_dfilter)) {
573                 g_warning("Could not compile \"%s\" in colorfilters file.\n%s",
574                           name, dfilter_error_msg);
575                 prefs.unknown_colorfilters = TRUE;
576
577                 skip_end_of_line = TRUE;
578                 continue;
579             }
580
581             if (!initialize_color(&fg_color, fg_r, fg_g, fg_b)) {
582                 /* oops */
583                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
584                               "Could not allocate foreground color "
585                               "specified in input file for %s.", name);
586                 dfilter_free(temp_dfilter);
587                 skip_end_of_line = TRUE;
588                 continue;
589             }
590             if (!initialize_color(&bg_color, bg_r, bg_g, bg_b)) {
591                 /* oops */
592                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
593                               "Could not allocate background color "
594                               "specified in input file for %s.", name);
595                 dfilter_free(temp_dfilter);
596                 skip_end_of_line = TRUE;
597                 continue;
598             }
599
600             colorf = color_filter_new(name, filter_exp, &bg_color,
601                                       &fg_color, disabled);
602             if(user_data == &color_filter_list) {
603                 GSList **cfl = (GSList **)user_data;
604
605                 /* internal call */
606                 colorf->c_colorfilter = temp_dfilter;
607                 *cfl = g_slist_append(*cfl, colorf);
608             } else {
609                 /* external call */
610                 /* just editing, don't need the compiled filter */
611                 dfilter_free(temp_dfilter);
612                 color_filter_add_cb (colorf, user_data);
613             }
614         }    /* if sscanf */
615
616         skip_end_of_line = TRUE;
617     }
618
619     g_free(name);
620     g_free(filter_exp);
621     return TRUE;
622 }
623
624 /* read filters from the user's filter file */
625 static gboolean
626 read_users_filters(GSList **cfl)
627 {
628     gchar    *path;
629     FILE     *f;
630     gboolean  ret;
631
632     /* decide what file to open (from dfilter code) */
633     path = get_persconffile_path("colorfilters", TRUE);
634     if ((f = ws_fopen(path, "r")) == NULL) {
635         if (errno != ENOENT) {
636             simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
637                           "Could not open filter file\n\"%s\": %s.", path,
638                           g_strerror(errno));
639         }
640         g_free(path);
641         return FALSE;
642     }
643     g_free(path);
644     path = NULL;
645
646     ret = read_filters_file(f, cfl);
647     fclose(f);
648     return ret;
649 }
650
651 /* read filters from the filter file */
652 gboolean
653 color_filters_read_globals(gpointer user_data)
654 {
655     gchar    *path;
656     FILE     *f;
657     gboolean  ret;
658
659     /* decide what file to open (from dfilter code) */
660     path = get_datafile_path("colorfilters");
661     if ((f = ws_fopen(path, "r")) == NULL) {
662         if (errno != ENOENT) {
663             simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
664                           "Could not open global filter file\n\"%s\": %s.", path,
665                           g_strerror(errno));
666         }
667         g_free(path);
668         return FALSE;
669     }
670     g_free(path);
671     path = NULL;
672
673     ret = read_filters_file(f, user_data);
674     fclose(f);
675     return ret;
676 }
677
678 /* read filters from some other filter file (import) */
679 gboolean
680 color_filters_import(gchar *path, gpointer user_data)
681 {
682     FILE     *f;
683     gboolean  ret;
684
685     if ((f = ws_fopen(path, "r")) == NULL) {
686         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
687                       "Could not open\n%s\nfor reading: %s.",
688                       path, g_strerror(errno));
689         return FALSE;
690     }
691
692     ret = read_filters_file(f, user_data);
693     fclose(f);
694     return ret;
695 }
696
697 struct write_filter_data
698 {
699     FILE     *f;
700     gboolean  only_selected;
701 };
702
703 /* save a single filter */
704 static void
705 write_filter(gpointer filter_arg, gpointer data_arg)
706 {
707     struct write_filter_data *data = (struct write_filter_data *)data_arg;
708     color_filter_t *colorf = (color_filter_t *)filter_arg;
709     FILE *f = data->f;
710
711     if ( (colorf->selected || !data->only_selected) &&
712          (strstr(colorf->filter_name,CONVERSATION_COLOR_PREFIX)==NULL) ) {
713         fprintf(f,"%s@%s@%s@[%d,%d,%d][%d,%d,%d]\n",
714                 colorf->disabled ? "!" : "",
715                 colorf->filter_name,
716                 colorf->filter_text,
717                 colorf->bg_color.red,
718                 colorf->bg_color.green,
719                 colorf->bg_color.blue,
720                 colorf->fg_color.red,
721                 colorf->fg_color.green,
722                 colorf->fg_color.blue);
723     }
724 }
725
726 /* save filters in a filter file */
727 static gboolean
728 write_filters_file(GSList *cfl, FILE *f, gboolean only_selected)
729 {
730     struct write_filter_data data;
731
732     data.f = f;
733     data.only_selected = only_selected;
734
735     fprintf(f,"# DO NOT EDIT THIS FILE!  It was created by Wireshark\n");
736     g_slist_foreach(cfl, write_filter, &data);
737     return TRUE;
738 }
739
740 /* save filters in users filter file */
741 gboolean
742 color_filters_write(GSList *cfl)
743 {
744     gchar *pf_dir_path;
745     gchar *path;
746     FILE  *f;
747
748     /* Create the directory that holds personal configuration files,
749        if necessary.  */
750     if (create_persconffile_dir(&pf_dir_path) == -1) {
751         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
752                       "Can't create directory\n\"%s\"\nfor color files: %s.",
753                       pf_dir_path, g_strerror(errno));
754         g_free(pf_dir_path);
755         return FALSE;
756     }
757
758     path = get_persconffile_path("colorfilters", TRUE);
759     if ((f = ws_fopen(path, "w+")) == NULL) {
760         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
761                       "Could not open\n%s\nfor writing: %s.",
762                       path, g_strerror(errno));
763         g_free(path);
764         return FALSE;
765     }
766     g_free(path);
767     write_filters_file(cfl, f, FALSE);
768     fclose(f);
769     return TRUE;
770 }
771
772 /* save filters in some other filter file (export) */
773 gboolean
774 color_filters_export(gchar *path, GSList *cfl, gboolean only_marked)
775 {
776     FILE *f;
777
778     if ((f = ws_fopen(path, "w+")) == NULL) {
779         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
780                       "Could not open\n%s\nfor writing: %s.",
781                       path, g_strerror(errno));
782         return FALSE;
783     }
784     write_filters_file(cfl, f, only_marked);
785     fclose(f);
786     return TRUE;
787 }
788
789 /*
790  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
791  *
792  * Local variables:
793  * c-basic-offset: 4
794  * tab-width: 8
795  * indent-tabs-mode: nil
796  * End:
797  *
798  * vi: set shiftwidth=4 tabstop=8 expandtab:
799  * :indentSize=4:tabSize=8:noTabs=true:
800  */