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