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