packet-dcerpc: change transportsalt to guint64
[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_malloc(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     colorf->c_colorfilter       = NULL;
86     colorf->color_edit_dlg_info = NULL;
87     colorf->selected            = FALSE;
88     return colorf;
89 }
90
91 /* Add ten empty (temporary) colorfilters for easy coloring */
92 static void
93 color_filters_add_tmp(GSList **cfl)
94 {
95     gchar          *name = NULL;
96     guint32         i;
97     gchar**         bg_colors;
98     gchar**         fg_colors;
99     gulong          cval;
100     color_t         bg_color, fg_color;
101     color_filter_t *colorf;
102
103     g_assert(strlen(prefs.gui_colorized_fg)==69);
104     g_assert(strlen(prefs.gui_colorized_bg)==69);
105     fg_colors = g_strsplit(prefs.gui_colorized_fg, ",", -1);
106     bg_colors = g_strsplit(prefs.gui_colorized_bg, ",", -1);
107
108     for ( i=1 ; i<=10 ; i++ ) {
109         name = g_strdup_printf("%s%02d",CONVERSATION_COLOR_PREFIX,i);
110
111         /* retrieve background and foreground colors */
112         cval = strtoul(fg_colors[i-1], NULL, 16);
113         initialize_color(&fg_color, RED_COMPONENT(cval),
114                          GREEN_COMPONENT(cval),
115                          BLUE_COMPONENT(cval) );
116         cval = strtoul(bg_colors[i-1], NULL, 16);
117         initialize_color(&bg_color, RED_COMPONENT(cval),
118                          GREEN_COMPONENT(cval),
119                          BLUE_COMPONENT(cval) );
120         colorf = color_filter_new(name, NULL, &bg_color, &fg_color, TRUE);
121         colorf->filter_text = g_strdup("frame");
122         colorf->c_colorfilter = NULL;
123         *cfl = g_slist_append(*cfl, colorf);
124
125         g_free(name);
126     }
127
128     g_strfreev(fg_colors);
129     g_strfreev(bg_colors);
130
131     return;
132 }
133
134 static gint
135 color_filters_find_by_name_cb(gconstpointer arg1, gconstpointer arg2)
136 {
137     const color_filter_t *colorf = (const color_filter_t *)arg1;
138     const gchar          *name   = (const gchar *)arg2;
139
140     return (strstr(colorf->filter_name, name)==NULL) ? -1 : 0 ;
141 }
142
143
144 /* Set the filter off a temporary colorfilters and enable it */
145 void
146 color_filters_set_tmp(guint8 filt_nr, gchar *filter, gboolean disabled)
147 {
148     gchar          *name = NULL;
149     const gchar    *tmpfilter = NULL;
150     GSList         *cfl;
151     color_filter_t *colorf;
152     dfilter_t      *compiled_filter;
153     gchar          *err_msg;
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, &err_msg)) {
179                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
180                               "Could not compile color filter name: \"%s\""
181                               " text: \"%s\".\n%s", name, filter, err_msg);
182                 g_free(err_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     gchar *err_msg;
345
346     g_assert(colorf->c_colorfilter == NULL);
347     if (!dfilter_compile(colorf->filter_text, &colorf->c_colorfilter, &err_msg)) {
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, err_msg);
351         g_free(err_msg);
352         /* this filter was compilable before, so this should never happen */
353         /* except if the OK button of the parent window has been clicked */
354         /* so don't use g_assert_not_reached() but check the filters again */
355     }
356 }
357
358 static void
359 color_filter_validate_cb(gpointer filter_arg, gpointer unused _U_)
360 {
361     color_filter_t *colorf = (color_filter_t *)filter_arg;
362     gchar *err_msg;
363
364     g_assert(colorf->c_colorfilter == NULL);
365     if (!dfilter_compile(colorf->filter_text, &colorf->c_colorfilter, &err_msg)) {
366         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
367                       "Removing color filter name: \"%s\" text: \"%s\".\n%s",
368                       colorf->filter_name, colorf->filter_text, err_msg);
369         g_free(err_msg);
370         /* Delete the color filter from the list of color filters. */
371         color_filter_valid_list = g_slist_remove(color_filter_valid_list, colorf);
372         color_filter_delete(colorf);
373     }
374 }
375
376 /* apply changes from the edit list */
377 void
378 color_filters_apply(GSList *tmp_cfl, GSList *edit_cfl)
379 {
380     /* "move" old entries to the deleted list
381      * we must keep them until the dissection no longer needs them */
382     color_filter_deleted_list = g_slist_concat(color_filter_deleted_list, color_filter_list);
383     color_filter_list = NULL;
384
385     /* clone all list entries from tmp/edit to normal list */
386     color_filter_valid_list = NULL;
387     color_filter_valid_list = color_filter_list_clone(tmp_cfl);
388     color_filter_valid_list = g_slist_concat(color_filter_valid_list,
389                                              color_filter_list_clone(edit_cfl) );
390
391     /* compile all filter */
392     g_slist_foreach(color_filter_valid_list, color_filter_validate_cb, NULL);
393
394     /* clone all list entries from tmp/edit to normal list */
395     color_filter_list = color_filter_list_clone(color_filter_valid_list);
396
397     /* compile all filter */
398     g_slist_foreach(color_filter_list, color_filter_compile_cb, NULL);
399 }
400
401 gboolean
402 color_filters_used(void)
403 {
404     return color_filter_list != NULL && filters_enabled;
405 }
406
407 gboolean
408 tmp_color_filters_used(void)
409 {
410     return tmp_colors_set;
411 }
412
413 void
414 color_filters_enable(gboolean enable)
415 {
416     packet_list_enable_color(enable);
417 }
418
419
420 /* prepare the epan_dissect_t for the filter */
421 static void
422 prime_edt(gpointer data, gpointer user_data)
423 {
424     color_filter_t *colorf = (color_filter_t *)data;
425     epan_dissect_t *edt    = (epan_dissect_t *)user_data;
426
427     if (colorf->c_colorfilter != NULL)
428         epan_dissect_prime_dfilter(edt, colorf->c_colorfilter);
429 }
430
431 /* Prime the epan_dissect_t with all the compiler
432  * color filters in 'color_filter_list'. */
433 void
434 color_filters_prime_edt(epan_dissect_t *edt)
435 {
436     if (color_filters_used())
437         g_slist_foreach(color_filter_list, prime_edt, edt);
438 }
439
440 /* * Return the color_t for later use */
441 const color_filter_t *
442 color_filters_colorize_packet(epan_dissect_t *edt)
443 {
444     GSList         *curr;
445     color_filter_t *colorf;
446
447     /* If we have color filters, "search" for the matching one. */
448     if (color_filters_used()) {
449         curr = color_filter_list;
450
451         while(curr != NULL) {
452             colorf = (color_filter_t *)curr->data;
453             if ( (!colorf->disabled) &&
454                  (colorf->c_colorfilter != NULL) &&
455                  dfilter_apply_edt(colorf->c_colorfilter, edt)) {
456                 return colorf;
457             }
458             curr = g_slist_next(curr);
459         }
460     }
461
462     return NULL;
463 }
464
465 /* read filters from the given file */
466 /* XXX - Would it make more sense to use GStrings here instead of reallocing
467    our buffers? */
468 static gboolean
469 read_filters_file(FILE *f, gpointer user_data)
470 {
471 #define INIT_BUF_SIZE 128
472     gchar    *name             = NULL;
473     gchar    *filter_exp       = NULL;
474     guint32   name_len         = INIT_BUF_SIZE;
475     guint32   filter_exp_len   = INIT_BUF_SIZE;
476     guint32   i                = 0;
477     int       c;
478     guint16   fg_r, fg_g, fg_b, bg_r, bg_g, bg_b;
479     gboolean  disabled         = FALSE;
480     gboolean  skip_end_of_line = FALSE;
481
482     name = (gchar *)g_malloc(name_len + 1);
483     filter_exp = (gchar *)g_malloc(filter_exp_len + 1);
484
485     while (1) {
486
487         if (skip_end_of_line) {
488             do {
489                 c = getc(f);
490             } while (c != EOF && c != '\n');
491             if (c == EOF)
492                 break;
493             disabled = FALSE;
494             skip_end_of_line = FALSE;
495         }
496
497         while ((c = getc(f)) != EOF && g_ascii_isspace(c)) {
498             if (c == '\n') {
499                 continue;
500             }
501         }
502
503         if (c == EOF)
504             break;
505
506         if (c == '!') {
507             disabled = TRUE;
508             continue;
509         }
510
511         /* skip # comments and invalid lines */
512         if (c != '@') {
513             skip_end_of_line = TRUE;
514             continue;
515         }
516
517         /* we get the @ delimiter.
518          * Format is:
519          * @name@filter expression@[background r,g,b][foreground r,g,b]
520          */
521
522         /* retrieve name */
523         i = 0;
524         while (1) {
525             c = getc(f);
526             if (c == EOF || c == '@')
527                 break;
528             if (i >= name_len) {
529                 /* buffer isn't long enough; double its length.*/
530                 name_len *= 2;
531                 name = (gchar *)g_realloc(name, name_len + 1);
532             }
533             name[i++] = c;
534         }
535         name[i] = '\0';
536
537         if (c == EOF) {
538             break;
539         } else if (i == 0) {
540             skip_end_of_line = TRUE;
541             continue;
542         }
543
544         /* retrieve filter expression */
545         i = 0;
546         while (1) {
547             c = getc(f);
548             if (c == EOF || c == '@')
549                 break;
550             if (i >= filter_exp_len) {
551                 /* buffer isn't long enough; double its length.*/
552                 filter_exp_len *= 2;
553                 filter_exp = (gchar *)g_realloc(filter_exp, filter_exp_len + 1);
554             }
555             filter_exp[i++] = c;
556         }
557         filter_exp[i] = '\0';
558
559         if (c == EOF) {
560             break;
561         } else if (i == 0) {
562             skip_end_of_line = TRUE;
563             continue;
564         }
565
566         /* retrieve background and foreground colors */
567         if (fscanf(f,"[%hu,%hu,%hu][%hu,%hu,%hu]",
568                    &bg_r, &bg_g, &bg_b, &fg_r, &fg_g, &fg_b) == 6) {
569
570             /* we got a complete color filter */
571
572             color_t bg_color, fg_color;
573             color_filter_t *colorf;
574             dfilter_t *temp_dfilter;
575             gchar *err_msg;
576
577             if (!dfilter_compile(filter_exp, &temp_dfilter, &err_msg)) {
578                 g_warning("Could not compile \"%s\" in colorfilters file.\n%s",
579                           name, err_msg);
580                 g_free(err_msg);
581                 prefs.unknown_colorfilters = TRUE;
582
583                 skip_end_of_line = TRUE;
584                 continue;
585             }
586
587             if (!initialize_color(&fg_color, fg_r, fg_g, fg_b)) {
588                 /* oops */
589                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
590                               "Could not allocate foreground color "
591                               "specified in input file for %s.", name);
592                 dfilter_free(temp_dfilter);
593                 skip_end_of_line = TRUE;
594                 continue;
595             }
596             if (!initialize_color(&bg_color, bg_r, bg_g, bg_b)) {
597                 /* oops */
598                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
599                               "Could not allocate background color "
600                               "specified in input file for %s.", name);
601                 dfilter_free(temp_dfilter);
602                 skip_end_of_line = TRUE;
603                 continue;
604             }
605
606             colorf = color_filter_new(name, filter_exp, &bg_color,
607                                       &fg_color, disabled);
608             if(user_data == &color_filter_list) {
609                 GSList **cfl = (GSList **)user_data;
610
611                 /* internal call */
612                 colorf->c_colorfilter = temp_dfilter;
613                 *cfl = g_slist_append(*cfl, colorf);
614             } else {
615                 /* external call */
616                 /* just editing, don't need the compiled filter */
617                 dfilter_free(temp_dfilter);
618                 color_filter_add_cb (colorf, user_data);
619             }
620         }    /* if sscanf */
621
622         skip_end_of_line = TRUE;
623     }
624
625     g_free(name);
626     g_free(filter_exp);
627     return TRUE;
628 }
629
630 /* read filters from the user's filter file */
631 static gboolean
632 read_users_filters(GSList **cfl)
633 {
634     gchar    *path;
635     FILE     *f;
636     gboolean  ret;
637
638     /* decide what file to open (from dfilter code) */
639     path = get_persconffile_path("colorfilters", TRUE);
640     if ((f = ws_fopen(path, "r")) == NULL) {
641         if (errno != ENOENT) {
642             simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
643                           "Could not open filter file\n\"%s\": %s.", path,
644                           g_strerror(errno));
645         }
646         g_free(path);
647         return FALSE;
648     }
649     g_free(path);
650     path = NULL;
651
652     ret = read_filters_file(f, cfl);
653     fclose(f);
654     return ret;
655 }
656
657 /* read filters from the filter file */
658 gboolean
659 color_filters_read_globals(gpointer user_data)
660 {
661     gchar    *path;
662     FILE     *f;
663     gboolean  ret;
664
665     /* decide what file to open (from dfilter code) */
666     path = get_datafile_path("colorfilters");
667     if ((f = ws_fopen(path, "r")) == NULL) {
668         if (errno != ENOENT) {
669             simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
670                           "Could not open global filter file\n\"%s\": %s.", path,
671                           g_strerror(errno));
672         }
673         g_free(path);
674         return FALSE;
675     }
676     g_free(path);
677     path = NULL;
678
679     ret = read_filters_file(f, user_data);
680     fclose(f);
681     return ret;
682 }
683
684 /* read filters from some other filter file (import) */
685 gboolean
686 color_filters_import(gchar *path, gpointer user_data)
687 {
688     FILE     *f;
689     gboolean  ret;
690
691     if ((f = ws_fopen(path, "r")) == NULL) {
692         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
693                       "Could not open\n%s\nfor reading: %s.",
694                       path, g_strerror(errno));
695         return FALSE;
696     }
697
698     ret = read_filters_file(f, user_data);
699     fclose(f);
700     return ret;
701 }
702
703 struct write_filter_data
704 {
705     FILE     *f;
706     gboolean  only_selected;
707 };
708
709 /* save a single filter */
710 static void
711 write_filter(gpointer filter_arg, gpointer data_arg)
712 {
713     struct write_filter_data *data = (struct write_filter_data *)data_arg;
714     color_filter_t *colorf = (color_filter_t *)filter_arg;
715     FILE *f = data->f;
716
717     if ( (colorf->selected || !data->only_selected) &&
718          (strstr(colorf->filter_name,CONVERSATION_COLOR_PREFIX)==NULL) ) {
719         fprintf(f,"%s@%s@%s@[%d,%d,%d][%d,%d,%d]\n",
720                 colorf->disabled ? "!" : "",
721                 colorf->filter_name,
722                 colorf->filter_text,
723                 colorf->bg_color.red,
724                 colorf->bg_color.green,
725                 colorf->bg_color.blue,
726                 colorf->fg_color.red,
727                 colorf->fg_color.green,
728                 colorf->fg_color.blue);
729     }
730 }
731
732 /* save filters in a filter file */
733 static gboolean
734 write_filters_file(GSList *cfl, FILE *f, gboolean only_selected)
735 {
736     struct write_filter_data data;
737
738     data.f = f;
739     data.only_selected = only_selected;
740
741     fprintf(f,"# DO NOT EDIT THIS FILE!  It was created by Wireshark\n");
742     g_slist_foreach(cfl, write_filter, &data);
743     return TRUE;
744 }
745
746 /* save filters in users filter file */
747 gboolean
748 color_filters_write(GSList *cfl)
749 {
750     gchar *pf_dir_path;
751     gchar *path;
752     FILE  *f;
753
754     /* Create the directory that holds personal configuration files,
755        if necessary.  */
756     if (create_persconffile_dir(&pf_dir_path) == -1) {
757         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
758                       "Can't create directory\n\"%s\"\nfor color files: %s.",
759                       pf_dir_path, g_strerror(errno));
760         g_free(pf_dir_path);
761         return FALSE;
762     }
763
764     path = get_persconffile_path("colorfilters", TRUE);
765     if ((f = ws_fopen(path, "w+")) == NULL) {
766         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
767                       "Could not open\n%s\nfor writing: %s.",
768                       path, g_strerror(errno));
769         g_free(path);
770         return FALSE;
771     }
772     g_free(path);
773     write_filters_file(cfl, f, FALSE);
774     fclose(f);
775     return TRUE;
776 }
777
778 /* save filters in some other filter file (export) */
779 gboolean
780 color_filters_export(gchar *path, GSList *cfl, gboolean only_marked)
781 {
782     FILE *f;
783
784     if ((f = ws_fopen(path, "w+")) == NULL) {
785         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
786                       "Could not open\n%s\nfor writing: %s.",
787                       path, g_strerror(errno));
788         return FALSE;
789     }
790     write_filters_file(cfl, f, only_marked);
791     fclose(f);
792     return TRUE;
793 }
794
795 /*
796  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
797  *
798  * Local variables:
799  * c-basic-offset: 4
800  * tab-width: 8
801  * indent-tabs-mode: nil
802  * End:
803  *
804  * vi: set shiftwidth=4 tabstop=8 expandtab:
805  * :indentSize=4:tabSize=8:noTabs=true:
806  */