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