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