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_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;
88 /* Add ten empty (temporary) colorfilters for easy coloring */
90 color_filters_add_tmp(GSList **cfl)
97 color_t bg_color, fg_color;
98 color_filter_t *colorf;
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);
105 for ( i=1 ; i<=10 ; i++ ) {
106 name = g_strdup_printf("%s%02d",CONVERSATION_COLOR_PREFIX,i);
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);
124 g_strfreev(fg_colors);
125 g_strfreev(bg_colors);
131 color_filters_find_by_name_cb(gconstpointer arg1, gconstpointer arg2)
133 const color_filter_t *colorf = (const color_filter_t *)arg1;
134 const gchar *name = (const gchar *)arg2;
136 return (strstr(colorf->filter_name, name)==NULL) ? -1 : 0 ;
140 /* Set the filter off a temporary colorfilters and enable it */
142 color_filters_set_tmp(guint8 filt_nr, const gchar *filter, gboolean disabled)
145 const gchar *tmpfilter = NULL;
147 color_filter_t *colorf;
148 dfilter_t *compiled_filter;
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
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
159 if( i!=filt_nr && filter==NULL )
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;
166 /* Only change the filter rule if this is the rule to change or if
167 * a matching filter string has been found
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
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);
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 */
189 tmp_colors_set = TRUE;
197 const color_filter_t *
198 color_filters_tmp_color(guint8 filter_num) {
200 color_filter_t *colorf = NULL;
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);
206 colorf = (color_filter_t *)cfl->data;
213 /* Reset the temporary colorfilters */
215 color_filters_reset_tmp(void)
219 for ( i=1 ; i<=10 ; i++ ) {
220 color_filters_set_tmp(i, NULL, TRUE);
222 /* Remember that there are now *no* temporary coloring filters set */
223 tmp_colors_set = FALSE;
227 /* delete the specified filter */
229 color_filter_delete(color_filter_t *colorf)
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);
240 /* delete the specified filter (called from g_slist_foreach) */
242 color_filter_delete_cb(gpointer filter_arg, gpointer unused _U_)
244 color_filter_t *colorf = (color_filter_t *)filter_arg;
246 color_filter_delete(colorf);
249 /* delete the specified list */
251 color_filter_list_delete(GSList **cfl)
253 g_slist_foreach(*cfl, color_filter_delete_cb, NULL);
258 /* clone a single list entries from normal to edit list */
259 static color_filter_t *
260 color_filter_clone(color_filter_t *colorf)
262 color_filter_t *new_colorf;
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;
278 color_filter_list_clone_cb(gpointer filter_arg, gpointer cfl_arg)
280 GSList **cfl = (GSList **)cfl_arg;
281 color_filter_t *new_colorf;
283 new_colorf = color_filter_clone((color_filter_t *)filter_arg);
284 *cfl = g_slist_append(*cfl, new_colorf);
287 /* clone the specified list */
289 color_filter_list_clone(GSList *cfl)
291 GSList *new_list = NULL;
293 g_slist_foreach(cfl, color_filter_list_clone_cb, &new_list);
298 /* Initialize the filter structures (reading from file) for general running, including app startup */
300 color_filters_init(void)
302 /* delete all currently existing filters */
303 color_filter_list_delete(&color_filter_list);
305 /* start the list with the temporary colorizing rules */
306 color_filters_add_tmp(&color_filter_list);
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);
315 color_filters_reload(void)
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;
322 /* start the list with the temporary colorizing rules */
323 color_filters_add_tmp(&color_filter_list);
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);
332 color_filters_cleanup(void)
334 /* delete the previously deleted filters */
335 color_filter_list_delete(&color_filter_deleted_list);
339 color_filters_clone_cb(gpointer filter_arg, gpointer user_data)
341 color_filter_t * new_colorf = color_filter_clone((color_filter_t *)filter_arg);
342 color_filter_add_cb (new_colorf, user_data);
346 color_filters_clone(gpointer user_data)
348 g_slist_foreach(color_filter_list, color_filters_clone_cb, user_data);
353 color_filter_compile_cb(gpointer filter_arg, gpointer unused _U_)
355 color_filter_t *colorf = (color_filter_t *)filter_arg;
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);
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 */
371 color_filter_validate_cb(gpointer filter_arg, gpointer unused _U_)
373 color_filter_t *colorf = (color_filter_t *)filter_arg;
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);
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);
388 /* apply changes from the edit list */
390 color_filters_apply(GSList *tmp_cfl, GSList *edit_cfl)
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;
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) );
403 /* compile all filter */
404 g_slist_foreach(color_filter_valid_list, color_filter_validate_cb, NULL);
406 /* clone all list entries from tmp/edit to normal list */
407 color_filter_list = color_filter_list_clone(color_filter_valid_list);
409 /* compile all filter */
410 g_slist_foreach(color_filter_list, color_filter_compile_cb, NULL);
414 color_filters_used(void)
416 return color_filter_list != NULL && filters_enabled;
420 tmp_color_filters_used(void)
422 return tmp_colors_set;
426 color_filters_enable(gboolean enable)
428 packet_list_enable_color(enable);
432 /* prepare the epan_dissect_t for the filter */
434 prime_edt(gpointer data, gpointer user_data)
436 color_filter_t *colorf = (color_filter_t *)data;
437 epan_dissect_t *edt = (epan_dissect_t *)user_data;
439 if (colorf->c_colorfilter != NULL)
440 epan_dissect_prime_dfilter(edt, colorf->c_colorfilter);
443 /* Prime the epan_dissect_t with all the compiler
444 * color filters in 'color_filter_list'. */
446 color_filters_prime_edt(epan_dissect_t *edt)
448 if (color_filters_used())
449 g_slist_foreach(color_filter_list, prime_edt, edt);
452 /* * Return the color_t for later use */
453 const color_filter_t *
454 color_filters_colorize_packet(epan_dissect_t *edt)
457 color_filter_t *colorf;
459 /* If we have color filters, "search" for the matching one. */
460 if (color_filters_used()) {
461 curr = color_filter_list;
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)) {
470 curr = g_slist_next(curr);
477 /* read filters from the given file */
478 /* XXX - Would it make more sense to use GStrings here instead of reallocing
481 read_filters_file(FILE *f, gpointer user_data)
483 #define INIT_BUF_SIZE 128
485 gchar *filter_exp = NULL;
486 guint32 name_len = INIT_BUF_SIZE;
487 guint32 filter_exp_len = INIT_BUF_SIZE;
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;
494 name = (gchar *)g_malloc(name_len + 1);
495 filter_exp = (gchar *)g_malloc(filter_exp_len + 1);
499 if (skip_end_of_line) {
502 } while (c != EOF && c != '\n');
506 skip_end_of_line = FALSE;
509 while ((c = getc(f)) != EOF && g_ascii_isspace(c)) {
523 /* skip # comments and invalid lines */
525 skip_end_of_line = TRUE;
529 /* we get the @ delimiter.
531 * @name@filter expression@[background r,g,b][foreground r,g,b]
538 if (c == EOF || c == '@')
541 /* buffer isn't long enough; double its length.*/
543 name = (gchar *)g_realloc(name, name_len + 1);
552 skip_end_of_line = TRUE;
556 /* retrieve filter expression */
560 if (c == EOF || c == '@')
562 if (i >= filter_exp_len) {
563 /* buffer isn't long enough; double its length.*/
565 filter_exp = (gchar *)g_realloc(filter_exp, filter_exp_len + 1);
569 filter_exp[i] = '\0';
574 skip_end_of_line = TRUE;
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) {
582 /* we got a complete color filter */
584 color_t bg_color, fg_color;
585 color_filter_t *colorf;
586 dfilter_t *temp_dfilter;
589 if (!dfilter_compile(filter_exp, &temp_dfilter, &err_msg)) {
590 g_warning("Could not compile \"%s\" in colorfilters file.\n%s",
593 prefs.unknown_colorfilters = TRUE;
595 skip_end_of_line = TRUE;
599 if (!initialize_color(&fg_color, fg_r, fg_g, fg_b)) {
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;
608 if (!initialize_color(&bg_color, bg_r, bg_g, bg_b)) {
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;
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;
624 colorf->c_colorfilter = temp_dfilter;
625 *cfl = g_slist_append(*cfl, colorf);
628 /* just editing, don't need the compiled filter */
629 dfilter_free(temp_dfilter);
630 color_filter_add_cb (colorf, user_data);
634 skip_end_of_line = TRUE;
642 /* read filters from the user's filter file */
644 read_users_filters(GSList **cfl)
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,
664 ret = read_filters_file(f, cfl);
669 /* read filters from the filter file */
671 color_filters_read_globals(gpointer user_data)
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,
691 ret = read_filters_file(f, user_data);
696 /* read filters from some other filter file (import) */
698 color_filters_import(const gchar *path, const gpointer user_data)
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));
710 ret = read_filters_file(f, user_data);
715 struct write_filter_data
718 gboolean only_selected;
721 /* save a single filter */
723 write_filter(gpointer filter_arg, gpointer data_arg)
725 struct write_filter_data *data = (struct write_filter_data *)data_arg;
726 color_filter_t *colorf = (color_filter_t *)filter_arg;
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 ? "!" : "",
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);
744 /* save filters in a filter file */
746 write_filters_file(const GSList *cfl, FILE *f, gboolean only_selected)
748 struct write_filter_data data;
751 data.only_selected = only_selected;
753 fprintf(f,"# DO NOT EDIT THIS FILE! It was created by Wireshark\n");
754 g_slist_foreach((GSList *) cfl, write_filter, &data);
758 /* save filters in users filter file */
760 color_filters_write(GSList *cfl)
766 /* Create the directory that holds personal configuration files,
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));
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));
785 write_filters_file(cfl, f, FALSE);
790 /* save filters in some other filter file (export) */
792 color_filters_export(const gchar *path, const GSList *cfl, gboolean only_marked)
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));
802 write_filters_file(cfl, f, only_marked);
808 * Editor modelines - http://www.wireshark.org/tools/modelines.html
813 * indent-tabs-mode: nil
816 * vi: set shiftwidth=4 tabstop=8 expandtab:
817 * :indentSize=4:tabSize=8:noTabs=true: