2 * Routines for color filters
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
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.
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.
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.
23 * Updated 1 Dec 10 jjm
34 #include <wsutil/filesystem.h>
35 #include <wsutil/file_util.h>
37 #include <epan/packet.h>
39 #include "color_filters.h"
41 #include <epan/dfilter/dfilter.h>
42 #include <epan/prefs.h>
44 #include "ui/simple_dialog.h"
45 #include "ui/ui_util.h"
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))
51 static gboolean read_users_filters(GSList **cfl);
53 /* the currently active filters */
54 static GSList *color_filter_list = NULL;
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;
61 /* Color Filters can en-/disabled. */
62 static gboolean filters_enabled = TRUE;
64 /* Remember if there are temporary coloring filters set to
65 * add sensitivity to the "Reset Coloring 1-10" menu item
67 static gboolean tmp_colors_set = FALSE;
69 /* Create a new filter */
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? */
77 color_filter_t *colorf;
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;
91 /* Add ten empty (temporary) colorfilters for easy coloring */
93 color_filters_add_tmp(GSList **cfl)
100 color_t bg_color, fg_color;
101 color_filter_t *colorf;
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);
108 for ( i=1 ; i<=10 ; i++ ) {
109 name = g_strdup_printf("%s%02d",CONVERSATION_COLOR_PREFIX,i);
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);
128 g_strfreev(fg_colors);
129 g_strfreev(bg_colors);
135 color_filters_find_by_name_cb(gconstpointer arg1, gconstpointer arg2)
137 const color_filter_t *colorf = (const color_filter_t *)arg1;
138 const gchar *name = (const gchar *)arg2;
140 return (strstr(colorf->filter_name, name)==NULL) ? -1 : 0 ;
144 /* Set the filter off a temporary colorfilters and enable it */
146 color_filters_set_tmp(guint8 filt_nr, gchar *filter, gboolean disabled)
149 const gchar *tmpfilter = NULL;
151 color_filter_t *colorf;
152 dfilter_t *compiled_filter;
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
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
163 if( i!=filt_nr && filter==NULL )
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;
170 /* Only change the filter rule if this is the rule to change or if
171 * a matching filter string has been found
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
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);
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 */
193 tmp_colors_set = TRUE;
201 /* Reset the temporary colorfilters */
203 color_filters_reset_tmp(void)
207 for ( i=1 ; i<=10 ; i++ ) {
208 color_filters_set_tmp(i, NULL, TRUE);
210 /* Remember that there are now *no* temporary coloring filters set */
211 tmp_colors_set = FALSE;
215 /* delete the specified filter */
217 color_filter_delete(color_filter_t *colorf)
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);
228 /* delete the specified filter (called from g_slist_foreach) */
230 color_filter_delete_cb(gpointer filter_arg, gpointer unused _U_)
232 color_filter_t *colorf = (color_filter_t *)filter_arg;
234 color_filter_delete(colorf);
237 /* delete the specified list */
239 color_filter_list_delete(GSList **cfl)
241 g_slist_foreach(*cfl, color_filter_delete_cb, NULL);
246 /* clone a single list entries from normal to edit list */
247 static color_filter_t *
248 color_filter_clone(color_filter_t *colorf)
250 color_filter_t *new_colorf;
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;
266 color_filter_list_clone_cb(gpointer filter_arg, gpointer cfl_arg)
268 GSList **cfl = (GSList **)cfl_arg;
269 color_filter_t *new_colorf;
271 new_colorf = color_filter_clone((color_filter_t *)filter_arg);
272 *cfl = g_slist_append(*cfl, new_colorf);
275 /* clone the specified list */
277 color_filter_list_clone(GSList *cfl)
279 GSList *new_list = NULL;
281 g_slist_foreach(cfl, color_filter_list_clone_cb, &new_list);
286 /* Initialize the filter structures (reading from file) for general running, including app startup */
288 color_filters_init(void)
290 /* delete all currently existing filters */
291 color_filter_list_delete(&color_filter_list);
293 /* start the list with the temporary colorizing rules */
294 color_filters_add_tmp(&color_filter_list);
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);
303 color_filters_reload(void)
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;
310 /* start the list with the temporary colorizing rules */
311 color_filters_add_tmp(&color_filter_list);
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);
320 color_filters_cleanup(void)
322 /* delete the previously deleted filters */
323 color_filter_list_delete(&color_filter_deleted_list);
327 color_filters_clone_cb(gpointer filter_arg, gpointer user_data)
329 color_filter_t * new_colorf = color_filter_clone((color_filter_t *)filter_arg);
330 color_filter_add_cb (new_colorf, user_data);
334 color_filters_clone(gpointer user_data)
336 g_slist_foreach(color_filter_list, color_filters_clone_cb, user_data);
341 color_filter_compile_cb(gpointer filter_arg, gpointer unused _U_)
343 color_filter_t *colorf = (color_filter_t *)filter_arg;
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);
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;
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);
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);
376 /* apply changes from the edit list */
378 color_filters_apply(GSList *tmp_cfl, GSList *edit_cfl)
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;
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) );
391 /* compile all filter */
392 g_slist_foreach(color_filter_valid_list, color_filter_validate_cb, NULL);
394 /* clone all list entries from tmp/edit to normal list */
395 color_filter_list = color_filter_list_clone(color_filter_valid_list);
397 /* compile all filter */
398 g_slist_foreach(color_filter_list, color_filter_compile_cb, NULL);
402 color_filters_used(void)
404 return color_filter_list != NULL && filters_enabled;
408 tmp_color_filters_used(void)
410 return tmp_colors_set;
414 color_filters_enable(gboolean enable)
416 packet_list_enable_color(enable);
420 /* prepare the epan_dissect_t for the filter */
422 prime_edt(gpointer data, gpointer user_data)
424 color_filter_t *colorf = (color_filter_t *)data;
425 epan_dissect_t *edt = (epan_dissect_t *)user_data;
427 if (colorf->c_colorfilter != NULL)
428 epan_dissect_prime_dfilter(edt, colorf->c_colorfilter);
431 /* Prime the epan_dissect_t with all the compiler
432 * color filters in 'color_filter_list'. */
434 color_filters_prime_edt(epan_dissect_t *edt)
436 if (color_filters_used())
437 g_slist_foreach(color_filter_list, prime_edt, edt);
440 /* * Return the color_t for later use */
441 const color_filter_t *
442 color_filters_colorize_packet(epan_dissect_t *edt)
445 color_filter_t *colorf;
447 /* If we have color filters, "search" for the matching one. */
448 if (color_filters_used()) {
449 curr = color_filter_list;
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)) {
458 curr = g_slist_next(curr);
465 /* read filters from the given file */
466 /* XXX - Would it make more sense to use GStrings here instead of reallocing
469 read_filters_file(FILE *f, gpointer user_data)
471 #define INIT_BUF_SIZE 128
473 gchar *filter_exp = NULL;
474 guint32 name_len = INIT_BUF_SIZE;
475 guint32 filter_exp_len = INIT_BUF_SIZE;
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;
482 name = (gchar *)g_malloc(name_len + 1);
483 filter_exp = (gchar *)g_malloc(filter_exp_len + 1);
487 if (skip_end_of_line) {
490 } while (c != EOF && c != '\n');
494 skip_end_of_line = FALSE;
497 while ((c = getc(f)) != EOF && g_ascii_isspace(c)) {
511 /* skip # comments and invalid lines */
513 skip_end_of_line = TRUE;
517 /* we get the @ delimiter.
519 * @name@filter expression@[background r,g,b][foreground r,g,b]
526 if (c == EOF || c == '@')
529 /* buffer isn't long enough; double its length.*/
531 name = (gchar *)g_realloc(name, name_len + 1);
540 skip_end_of_line = TRUE;
544 /* retrieve filter expression */
548 if (c == EOF || c == '@')
550 if (i >= filter_exp_len) {
551 /* buffer isn't long enough; double its length.*/
553 filter_exp = (gchar *)g_realloc(filter_exp, filter_exp_len + 1);
557 filter_exp[i] = '\0';
562 skip_end_of_line = TRUE;
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) {
570 /* we got a complete color filter */
572 color_t bg_color, fg_color;
573 color_filter_t *colorf;
574 dfilter_t *temp_dfilter;
577 if (!dfilter_compile(filter_exp, &temp_dfilter, &err_msg)) {
578 g_warning("Could not compile \"%s\" in colorfilters file.\n%s",
581 prefs.unknown_colorfilters = TRUE;
583 skip_end_of_line = TRUE;
587 if (!initialize_color(&fg_color, fg_r, fg_g, fg_b)) {
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;
596 if (!initialize_color(&bg_color, bg_r, bg_g, bg_b)) {
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;
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;
612 colorf->c_colorfilter = temp_dfilter;
613 *cfl = g_slist_append(*cfl, colorf);
616 /* just editing, don't need the compiled filter */
617 dfilter_free(temp_dfilter);
618 color_filter_add_cb (colorf, user_data);
622 skip_end_of_line = TRUE;
630 /* read filters from the user's filter file */
632 read_users_filters(GSList **cfl)
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,
652 ret = read_filters_file(f, cfl);
657 /* read filters from the filter file */
659 color_filters_read_globals(gpointer user_data)
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,
679 ret = read_filters_file(f, user_data);
684 /* read filters from some other filter file (import) */
686 color_filters_import(gchar *path, gpointer user_data)
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));
698 ret = read_filters_file(f, user_data);
703 struct write_filter_data
706 gboolean only_selected;
709 /* save a single filter */
711 write_filter(gpointer filter_arg, gpointer data_arg)
713 struct write_filter_data *data = (struct write_filter_data *)data_arg;
714 color_filter_t *colorf = (color_filter_t *)filter_arg;
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 ? "!" : "",
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);
732 /* save filters in a filter file */
734 write_filters_file(GSList *cfl, FILE *f, gboolean only_selected)
736 struct write_filter_data data;
739 data.only_selected = only_selected;
741 fprintf(f,"# DO NOT EDIT THIS FILE! It was created by Wireshark\n");
742 g_slist_foreach(cfl, write_filter, &data);
746 /* save filters in users filter file */
748 color_filters_write(GSList *cfl)
754 /* Create the directory that holds personal configuration files,
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));
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));
773 write_filters_file(cfl, f, FALSE);
778 /* save filters in some other filter file (export) */
780 color_filters_export(gchar *path, GSList *cfl, gboolean only_marked)
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));
790 write_filters_file(cfl, f, only_marked);
796 * Editor modelines - http://www.wireshark.org/tools/modelines.html
801 * indent-tabs-mode: nil
804 * vi: set shiftwidth=4 tabstop=8 expandtab:
805 * :indentSize=4:tabSize=8:noTabs=true: