2 * Routines for color filters
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
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.
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.
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.
25 * Updated 1 Dec 10 jjm
38 #include <epan/filesystem.h>
39 #include <wsutil/file_util.h>
41 #include <epan/packet.h>
43 #include "color_filters.h"
45 #include <epan/dfilter/dfilter.h>
46 #include <epan/prefs.h>
48 #include "ui/simple_dialog.h"
49 #include "ui/ui_util.h"
51 #define RED_COMPONENT(x) (guint16) (((((x) >> 16) & 0xff) * 65535 / 255))
52 #define GREEN_COMPONENT(x) (guint16) (((((x) >> 8) & 0xff) * 65535 / 255))
53 #define BLUE_COMPONENT(x) (guint16) ( (((x) & 0xff) * 65535 / 255))
55 static gboolean read_users_filters(GSList **cfl);
57 /* the currently active filters */
58 static GSList *color_filter_list = NULL;
60 /* keep "old" deleted filters in this list until
61 * the dissection no longer needs them (e.g. file is closed) */
62 static GSList *color_filter_deleted_list = NULL;
63 static GSList *color_filter_valid_list = NULL;
65 /* Color Filters can en-/disabled. */
66 static gboolean filters_enabled = TRUE;
68 /* Remember if there are temporary coloring filters set to
69 * add sensitivity to the "Reset Coloring 1-10" menu item
71 static gboolean tmp_colors_set = FALSE;
73 /* Create a new filter */
75 color_filter_new(const gchar *name, /* The name of the filter to create */
76 const gchar *filter_string, /* The string representing the filter */
77 color_t *bg_color, /* The background color */
78 color_t *fg_color, /* The foreground color */
79 gboolean disabled) /* Is the filter disabled? */
81 color_filter_t *colorf;
83 colorf = (color_filter_t *)g_malloc(sizeof (color_filter_t));
84 colorf->filter_name = g_strdup(name);
85 colorf->filter_text = g_strdup(filter_string);
86 colorf->bg_color = *bg_color;
87 colorf->fg_color = *fg_color;
88 colorf->disabled = disabled;
89 colorf->c_colorfilter = NULL;
90 colorf->edit_dialog = NULL;
91 colorf->selected = FALSE;
95 /* Add ten empty (temporary) colorfilters for easy coloring */
97 color_filters_add_tmp(GSList **cfl)
103 unsigned long int cval;
104 color_t bg_color, fg_color;
105 color_filter_t *colorf;
107 g_assert(strlen(prefs.gui_colorized_fg)==69);
108 g_assert(strlen(prefs.gui_colorized_bg)==69);
109 fg_colors = g_strsplit(prefs.gui_colorized_fg, ",", -1);
110 bg_colors = g_strsplit(prefs.gui_colorized_bg, ",", -1);
112 for ( i=1 ; i<=10 ; i++ ) {
113 name = g_strdup_printf("%s%02d",CONVERSATION_COLOR_PREFIX,i);
115 /* retrieve background and foreground colors */
116 cval = strtoul(fg_colors[i-1], NULL, 16);
117 initialize_color(&fg_color, RED_COMPONENT(cval),
118 GREEN_COMPONENT(cval),
119 BLUE_COMPONENT(cval) );
120 cval = strtoul(bg_colors[i-1], NULL, 16);
121 initialize_color(&bg_color, RED_COMPONENT(cval),
122 GREEN_COMPONENT(cval),
123 BLUE_COMPONENT(cval) );
124 colorf = color_filter_new(name, NULL, &bg_color, &fg_color, TRUE);
125 colorf->filter_text = g_strdup("frame");
126 colorf->c_colorfilter = NULL;
127 *cfl = g_slist_append(*cfl, colorf);
132 g_strfreev(fg_colors);
133 g_strfreev(bg_colors);
139 color_filters_find_by_name_cb(gconstpointer arg1, gconstpointer arg2)
141 const color_filter_t *colorf = (const color_filter_t *)arg1;
142 const gchar *name = (const gchar *)arg2;
144 return (strstr(colorf->filter_name, name)==NULL) ? -1 : 0 ;
148 /* Set the filter off a temporary colorfilters and enable it */
150 color_filters_set_tmp(guint8 filt_nr, gchar *filter, gboolean disabled)
153 const gchar *tmpfilter = NULL;
155 color_filter_t *colorf;
156 dfilter_t *compiled_filter;
159 /* Go through the tomporary filters and look for the same filter string.
160 * If found, clear it so that a filter can be "moved" up and down the list
162 for ( i=1 ; i<=10 ; i++ ) {
163 /* If we need to reset the temporary filter (filter==NULL), don't look
164 * for other rules with the same filter string
166 if( i!=filt_nr && filter==NULL )
169 name = g_strdup_printf("%s%02d",CONVERSATION_COLOR_PREFIX,i);
170 cfl = g_slist_find_custom(color_filter_list, name, color_filters_find_by_name_cb);
171 colorf = (color_filter_t *)cfl->data;
173 /* Only change the filter rule if this is the rule to change or if
174 * a matching filter string has been found
176 if(colorf && ( (i==filt_nr) || (strstr(filter,colorf->filter_text)!=NULL) ) ) {
177 /* set filter string to "frame" if we are resetting the rules
178 * or if we found a matching filter string which need to be cleared
180 tmpfilter = ( (filter==NULL) || (i!=filt_nr) ) ? "frame" : filter;
181 if (!dfilter_compile(tmpfilter, &compiled_filter)) {
182 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
183 "Could not compile color filter name: \"%s\""
184 " text: \"%s\".\n%s", name, filter, dfilter_error_msg);
186 if (colorf->filter_text != NULL)
187 g_free(colorf->filter_text);
188 if (colorf->c_colorfilter != NULL)
189 dfilter_free(colorf->c_colorfilter);
190 colorf->filter_text = g_strdup(tmpfilter);
191 colorf->c_colorfilter = compiled_filter;
192 colorf->disabled = ((i!=filt_nr) ? TRUE : disabled);
193 /* Remember that there are now temporary coloring filters set */
195 tmp_colors_set = TRUE;
203 /* Reset the temporary colorfilters */
205 color_filters_reset_tmp(void)
209 for ( i=1 ; i<=10 ; i++ ) {
210 color_filters_set_tmp(i, NULL, TRUE);
212 /* Remember that there are now *no* temporary coloring filters set */
213 tmp_colors_set = FALSE;
217 /* delete the specified filter */
219 color_filter_delete(color_filter_t *colorf)
221 if (colorf->filter_name != NULL)
222 g_free(colorf->filter_name);
223 if (colorf->filter_text != NULL)
224 g_free(colorf->filter_text);
225 if (colorf->c_colorfilter != NULL)
226 dfilter_free(colorf->c_colorfilter);
230 /* delete the specified filter (called from g_slist_foreach) */
232 color_filter_delete_cb(gpointer filter_arg, gpointer unused _U_)
234 color_filter_t *colorf = (color_filter_t *)filter_arg;
236 color_filter_delete(colorf);
239 /* delete the specified list */
241 color_filter_list_delete(GSList **cfl)
243 g_slist_foreach(*cfl, color_filter_delete_cb, NULL);
248 /* clone a single list entries from normal to edit list */
249 static color_filter_t *
250 color_filter_clone(color_filter_t *colorf)
252 color_filter_t *new_colorf;
254 new_colorf = (color_filter_t *)g_malloc(sizeof (color_filter_t));
255 new_colorf->filter_name = g_strdup(colorf->filter_name);
256 new_colorf->filter_text = g_strdup(colorf->filter_text);
257 new_colorf->bg_color = colorf->bg_color;
258 new_colorf->fg_color = colorf->fg_color;
259 new_colorf->disabled = colorf->disabled;
260 new_colorf->c_colorfilter = NULL;
261 new_colorf->edit_dialog = NULL;
262 new_colorf->selected = FALSE;
268 color_filter_list_clone_cb(gpointer filter_arg, gpointer cfl_arg)
270 GSList **cfl = (GSList **)cfl_arg;
271 color_filter_t *new_colorf;
273 new_colorf = color_filter_clone((color_filter_t *)filter_arg);
274 *cfl = g_slist_append(*cfl, new_colorf);
277 /* clone the specified list */
279 color_filter_list_clone(GSList *cfl)
281 GSList *new_list = NULL;
283 g_slist_foreach(cfl, color_filter_list_clone_cb, &new_list);
288 /* Initialize the filter structures (reading from file) for general running, including app startup */
290 color_filters_init(void)
292 /* delete all currently existing filters */
293 color_filter_list_delete(&color_filter_list);
295 /* start the list with the temporary colorizing rules */
296 color_filters_add_tmp(&color_filter_list);
298 /* try to read the users filters */
299 if (!read_users_filters(&color_filter_list))
300 /* if that failed, try to read the global filters */
301 color_filters_read_globals(&color_filter_list);
305 color_filters_reload(void)
307 /* "move" old entries to the deleted list
308 * we must keep them until the dissection no longer needs them */
309 color_filter_deleted_list = g_slist_concat(color_filter_deleted_list, color_filter_list);
310 color_filter_list = NULL;
312 /* start the list with the temporary colorizing rules */
313 color_filters_add_tmp(&color_filter_list);
315 /* try to read the users filters */
316 if (!read_users_filters(&color_filter_list))
317 /* if that failed, try to read the global filters */
318 color_filters_read_globals(&color_filter_list);
322 color_filters_cleanup(void)
324 /* delete the previously deleted filters */
325 color_filter_list_delete(&color_filter_deleted_list);
329 color_filters_clone_cb(gpointer filter_arg, gpointer user_data)
331 color_filter_t * new_colorf = color_filter_clone((color_filter_t *)filter_arg);
332 color_filter_add_cb (new_colorf, user_data);
336 color_filters_clone(gpointer user_data)
338 g_slist_foreach(color_filter_list, color_filters_clone_cb, user_data);
343 color_filter_compile_cb(gpointer filter_arg, gpointer unused _U_)
345 color_filter_t *colorf = (color_filter_t *)filter_arg;
347 g_assert(colorf->c_colorfilter == NULL);
348 if (!dfilter_compile(colorf->filter_text, &colorf->c_colorfilter)) {
349 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
350 "Could not compile color filter name: \"%s\" text: \"%s\".\n%s",
351 colorf->filter_name, colorf->filter_text, dfilter_error_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 */
359 color_filter_validate_cb(gpointer filter_arg, gpointer unused _U_)
361 color_filter_t *colorf = (color_filter_t *)filter_arg;
363 g_assert(colorf->c_colorfilter == NULL);
364 if (!dfilter_compile(colorf->filter_text, &colorf->c_colorfilter)) {
365 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
366 "Removing color filter name: \"%s\" text: \"%s\".\n%s",
367 colorf->filter_name, colorf->filter_text, dfilter_error_msg);
368 /* Delete the color filter from the list of color filters. */
369 color_filter_valid_list = g_slist_remove(color_filter_valid_list, colorf);
370 color_filter_delete(colorf);
374 /* apply changes from the edit list */
376 color_filters_apply(GSList *tmp_cfl, GSList *edit_cfl)
378 /* "move" old entries to the deleted list
379 * we must keep them until the dissection no longer needs them */
380 color_filter_deleted_list = g_slist_concat(color_filter_deleted_list, color_filter_list);
381 color_filter_list = NULL;
383 /* clone all list entries from tmp/edit to normal list */
384 color_filter_valid_list = NULL;
385 color_filter_valid_list = color_filter_list_clone(tmp_cfl);
386 color_filter_valid_list = g_slist_concat(color_filter_valid_list,
387 color_filter_list_clone(edit_cfl) );
389 /* compile all filter */
390 g_slist_foreach(color_filter_valid_list, color_filter_validate_cb, NULL);
392 /* clone all list entries from tmp/edit to normal list */
393 color_filter_list = color_filter_list_clone(color_filter_valid_list);
395 /* compile all filter */
396 g_slist_foreach(color_filter_list, color_filter_compile_cb, NULL);
400 color_filters_used(void)
402 return color_filter_list != NULL && filters_enabled;
406 tmp_color_filters_used(void)
408 return tmp_colors_set;
412 color_filters_enable(gboolean enable)
414 new_packet_list_enable_color(enable);
418 /* prepare the epan_dissect_t for the filter */
420 prime_edt(gpointer data, gpointer user_data)
422 color_filter_t *colorf = (color_filter_t *)data;
423 epan_dissect_t *edt = (epan_dissect_t *)user_data;
425 if (colorf->c_colorfilter != NULL)
426 epan_dissect_prime_dfilter(edt, colorf->c_colorfilter);
429 /* Prime the epan_dissect_t with all the compiler
430 * color filters in 'color_filter_list'. */
432 color_filters_prime_edt(epan_dissect_t *edt)
434 if (color_filters_used())
435 g_slist_foreach(color_filter_list, prime_edt, edt);
438 /* * Return the color_t for later use */
439 const color_filter_t *
440 color_filters_colorize_packet(epan_dissect_t *edt)
443 color_filter_t *colorf;
445 /* If we have color filters, "search" for the matching one. */
446 if (color_filters_used()) {
447 curr = color_filter_list;
449 while(curr != NULL) {
450 colorf = (color_filter_t *)curr->data;
451 if ( (!colorf->disabled) &&
452 (colorf->c_colorfilter != NULL) &&
453 dfilter_apply_edt(colorf->c_colorfilter, edt)) {
456 curr = g_slist_next(curr);
463 /* read filters from the given file */
464 /* XXX - Would it make more sense to use GStrings here instead of reallocing
467 read_filters_file(FILE *f, gpointer user_data)
469 #define INIT_BUF_SIZE 128
471 gchar *filter_exp = NULL;
472 guint32 name_len = INIT_BUF_SIZE;
473 guint32 filter_exp_len = INIT_BUF_SIZE;
476 guint16 fg_r, fg_g, fg_b, bg_r, bg_g, bg_b;
477 gboolean disabled = FALSE;
478 gboolean skip_end_of_line = FALSE;
480 name = (gchar *)g_malloc(name_len + 1);
481 filter_exp = (gchar *)g_malloc(filter_exp_len + 1);
485 if (skip_end_of_line) {
488 } while (c != EOF && c != '\n');
492 skip_end_of_line = FALSE;
495 while ((c = getc(f)) != EOF && isspace(c)) {
509 /* skip # comments and invalid lines */
511 skip_end_of_line = TRUE;
515 /* we get the @ delimiter.
517 * @name@filter expression@[background r,g,b][foreground r,g,b]
524 if (c == EOF || c == '@')
527 /* buffer isn't long enough; double its length.*/
529 name = (gchar *)g_realloc(name, name_len + 1);
538 skip_end_of_line = TRUE;
542 /* retrieve filter expression */
546 if (c == EOF || c == '@')
548 if (i >= filter_exp_len) {
549 /* buffer isn't long enough; double its length.*/
551 filter_exp = (gchar *)g_realloc(filter_exp, filter_exp_len + 1);
555 filter_exp[i] = '\0';
560 skip_end_of_line = TRUE;
564 /* retrieve background and foreground colors */
565 if (fscanf(f,"[%hu,%hu,%hu][%hu,%hu,%hu]",
566 &bg_r, &bg_g, &bg_b, &fg_r, &fg_g, &fg_b) == 6) {
568 /* we got a complete color filter */
570 color_t bg_color, fg_color;
571 color_filter_t *colorf;
572 dfilter_t *temp_dfilter;
574 if (!dfilter_compile(filter_exp, &temp_dfilter)) {
575 g_warning("Could not compile color filter \"%s\" from saved filters: %s",
576 name, dfilter_error_msg);
577 skip_end_of_line = TRUE;
581 if (!initialize_color(&fg_color, fg_r, fg_g, fg_b)) {
583 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
584 "Could not allocate foreground color "
585 "specified in input file for %s.", name);
586 dfilter_free(temp_dfilter);
587 skip_end_of_line = TRUE;
590 if (!initialize_color(&bg_color, bg_r, bg_g, bg_b)) {
592 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
593 "Could not allocate background color "
594 "specified in input file for %s.", name);
595 dfilter_free(temp_dfilter);
596 skip_end_of_line = TRUE;
600 colorf = color_filter_new(name, filter_exp, &bg_color,
601 &fg_color, disabled);
602 if(user_data == &color_filter_list) {
603 GSList **cfl = (GSList **)user_data;
606 colorf->c_colorfilter = temp_dfilter;
607 *cfl = g_slist_append(*cfl, colorf);
610 /* just editing, don't need the compiled filter */
611 dfilter_free(temp_dfilter);
612 color_filter_add_cb (colorf, user_data);
616 skip_end_of_line = TRUE;
624 /* read filters from the user's filter file */
626 read_users_filters(GSList **cfl)
632 /* decide what file to open (from dfilter code) */
633 path = get_persconffile_path("colorfilters", TRUE, FALSE);
634 if ((f = ws_fopen(path, "r")) == NULL) {
635 if (errno != ENOENT) {
636 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
637 "Could not open filter file\n\"%s\": %s.", path,
646 ret = read_filters_file(f, cfl);
651 /* read filters from the filter file */
653 color_filters_read_globals(gpointer user_data)
659 /* decide what file to open (from dfilter code) */
660 path = get_datafile_path("colorfilters");
661 if ((f = ws_fopen(path, "r")) == NULL) {
662 if (errno != ENOENT) {
663 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
664 "Could not open global filter file\n\"%s\": %s.", path,
673 ret = read_filters_file(f, user_data);
678 /* read filters from some other filter file (import) */
680 color_filters_import(gchar *path, gpointer user_data)
685 if ((f = ws_fopen(path, "r")) == NULL) {
686 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
687 "Could not open\n%s\nfor reading: %s.",
688 path, g_strerror(errno));
692 ret = read_filters_file(f, user_data);
697 struct write_filter_data
700 gboolean only_selected;
703 /* save a single filter */
705 write_filter(gpointer filter_arg, gpointer data_arg)
707 struct write_filter_data *data = (struct write_filter_data *)data_arg;
708 color_filter_t *colorf = (color_filter_t *)filter_arg;
711 if ( (colorf->selected || !data->only_selected) &&
712 (strstr(colorf->filter_name,CONVERSATION_COLOR_PREFIX)==NULL) ) {
713 fprintf(f,"%s@%s@%s@[%d,%d,%d][%d,%d,%d]\n",
714 colorf->disabled ? "!" : "",
717 colorf->bg_color.red,
718 colorf->bg_color.green,
719 colorf->bg_color.blue,
720 colorf->fg_color.red,
721 colorf->fg_color.green,
722 colorf->fg_color.blue);
726 /* save filters in a filter file */
728 write_filters_file(GSList *cfl, FILE *f, gboolean only_selected)
730 struct write_filter_data data;
733 data.only_selected = only_selected;
735 fprintf(f,"# DO NOT EDIT THIS FILE! It was created by Wireshark\n");
736 g_slist_foreach(cfl, write_filter, &data);
740 /* save filters in users filter file */
742 color_filters_write(GSList *cfl)
748 /* Create the directory that holds personal configuration files,
750 if (create_persconffile_dir(&pf_dir_path) == -1) {
751 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
752 "Can't create directory\n\"%s\"\nfor color files: %s.",
753 pf_dir_path, g_strerror(errno));
758 path = get_persconffile_path("colorfilters", TRUE, TRUE);
759 if ((f = ws_fopen(path, "w+")) == NULL) {
760 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
761 "Could not open\n%s\nfor writing: %s.",
762 path, g_strerror(errno));
767 write_filters_file(cfl, f, FALSE);
772 /* save filters in some other filter file (export) */
774 color_filters_export(gchar *path, GSList *cfl, gboolean only_marked)
778 if ((f = ws_fopen(path, "w+")) == NULL) {
779 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
780 "Could not open\n%s\nfor writing: %s.",
781 path, g_strerror(errno));
784 write_filters_file(cfl, f, only_marked);