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
36 #include <epan/filesystem.h>
37 #include "file_util.h"
39 #include <epan/packet.h>
41 #include "color_filters.h"
43 #include <epan/dfilter/dfilter.h>
44 #include "simple_dialog.h"
47 static gboolean read_filters(void);
48 static gboolean read_global_filters(void);
50 /* Variables and routines defined in color.h */
52 GSList *color_filter_list = NULL;
53 GSList *removed_filter_list = NULL;
55 /* Color Filters can en-/disabled. */
56 gboolean filters_enabled = TRUE;
58 /* Remove the specified filter from the list of existing color filters,
59 * and add it to the list of removed color filters.
60 * This way, unmarking and marking a packet which matches a now removed
61 * color filter will still be colored correctly as the color filter is
63 void color_filter_remove(color_filter_t *colorf)
65 /* Remove colorf from the list of color filters */
66 color_filter_list = g_slist_remove(color_filter_list, colorf);
67 /* Add colorf to the list of removed color filters */
68 removed_filter_list = g_slist_prepend(removed_filter_list, colorf);
71 /* delete the specified filter */
73 delete_color_filter(color_filter_t *colorf)
75 if (colorf->filter_name != NULL)
76 g_free(colorf->filter_name);
77 if (colorf->filter_text != NULL)
78 g_free(colorf->filter_text);
79 if (colorf->c_colorfilter != NULL)
80 dfilter_free(colorf->c_colorfilter);
84 /* delete the specified filter as an iterator */
86 delete_color_filter_it(gpointer filter_arg, gpointer unused _U_)
88 color_filter_t *colorf = filter_arg;
89 delete_color_filter(colorf);
92 /* delete all the filters */
94 delete_all_color_filters (void)
96 g_slist_foreach(color_filter_list, delete_color_filter_it, NULL);
97 g_slist_free(color_filter_list);
98 color_filter_list = NULL;
99 g_slist_foreach(removed_filter_list, delete_color_filter_it, NULL);
100 g_slist_free(removed_filter_list);
101 removed_filter_list = NULL;
104 /* Initialize the filter structures (reading from file) for general running, including app startup */
106 color_filters_init(void)
108 delete_all_color_filters();
110 read_global_filters();
113 /* Create a new filter */
115 color_filter_new(const gchar *name, /* The name of the filter to create */
116 const gchar *filter_string, /* The string representing the filter */
117 color_t *bg_color, /* The background color */
118 color_t *fg_color) /* The foreground color */
120 color_filter_t *colorf;
122 colorf = g_malloc(sizeof (color_filter_t));
123 colorf->filter_name = g_strdup(name);
124 colorf->filter_text = g_strdup(filter_string);
125 colorf->bg_color = *bg_color;
126 colorf->fg_color = *fg_color;
127 colorf->c_colorfilter = NULL;
128 colorf->edit_dialog = NULL;
129 colorf->marked = FALSE;
130 color_filter_list = g_slist_append(color_filter_list, colorf);
135 color_filters_used(void)
137 return color_filter_list != NULL && filters_enabled;
141 color_filters_enable(gboolean enable)
143 filters_enabled = enable;
147 /* prepare the epan_dissect_t for the filter */
149 prime_edt(gpointer data, gpointer user_data)
151 color_filter_t *colorf = data;
152 epan_dissect_t *edt = user_data;
154 if (colorf->c_colorfilter != NULL)
155 epan_dissect_prime_dfilter(edt, colorf->c_colorfilter);
158 /* Prime the epan_dissect_t with all the compiler
159 * color filters in 'color_filter_list'. */
161 color_filters_prime_edt(epan_dissect_t *edt)
163 g_slist_foreach(color_filter_list, prime_edt, edt);
166 /* Colorize a single packet of the packet list */
168 color_filters_colorize_packet(gint row, epan_dissect_t *edt)
171 color_filter_t *colorf;
173 /* If we have color filters, "search" for the matching one. */
174 if (color_filters_used()) {
175 curr = color_filter_list;
177 while( (curr = g_slist_next(curr)) != NULL) {
179 if ((colorf->c_colorfilter != NULL) &&
180 dfilter_apply_edt(colorf->c_colorfilter, edt)) {
181 /* this is the filter to use, apply it to the packet list */
182 packet_list_set_colors(row, &(colorf->fg_color), &(colorf->bg_color));
191 /* read filters from the given file */
193 /* XXX - Would it make more sense to use GStrings here instead of reallocing
196 read_filters_file(FILE *f, gpointer arg)
198 #define INIT_BUF_SIZE 128
200 gchar *filter_exp = NULL;
201 guint32 name_len = INIT_BUF_SIZE;
202 guint32 filter_exp_len = INIT_BUF_SIZE;
205 guint16 fg_r, fg_g, fg_b, bg_r, bg_g, bg_b;
206 gboolean skip_end_of_line = FALSE;
208 name = g_malloc(name_len + 1);
209 filter_exp = g_malloc(filter_exp_len + 1);
213 if (skip_end_of_line) {
216 } while (c != EOF && c != '\n');
219 skip_end_of_line = FALSE;
222 while ((c = getc(f)) != EOF && isspace(c)) {
231 /* skip # comments and invalid lines */
233 skip_end_of_line = TRUE;
237 /* we get the @ delimiter.
239 * @name@filter expression@[background r,g,b][foreground r,g,b]
246 if (c == EOF || c == '@')
249 /* buffer isn't long enough; double its length.*/
251 name = g_realloc(name, name_len + 1);
260 skip_end_of_line = TRUE;
264 /* retrieve filter expression */
268 if (c == EOF || c == '@')
270 if (i >= filter_exp_len) {
271 /* buffer isn't long enough; double its length.*/
273 filter_exp = g_realloc(filter_exp, filter_exp_len + 1);
277 filter_exp[i] = '\0';
282 skip_end_of_line = TRUE;
286 /* retrieve background and foreground colors */
287 if (fscanf(f,"[%hu,%hu,%hu][%hu,%hu,%hu]",
288 &bg_r, &bg_g, &bg_b, &fg_r, &fg_g, &fg_b) == 6) {
290 /* we got a complete color filter */
292 color_t bg_color, fg_color;
293 color_filter_t *colorf;
294 dfilter_t *temp_dfilter;
296 if (!dfilter_compile(filter_exp, &temp_dfilter)) {
297 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
298 "Could not compile color filter %s from saved filters.\n%s",
299 name, dfilter_error_msg);
300 skip_end_of_line = TRUE;
304 if (!initialize_color(&fg_color, fg_r, fg_g, fg_b)) {
306 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
307 "Could not allocate foreground color "
308 "specified in input file for %s.", name);
309 dfilter_free(temp_dfilter);
310 skip_end_of_line = TRUE;
313 if (!initialize_color(&bg_color, bg_r, bg_g, bg_b)) {
315 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
316 "Could not allocate background color "
317 "specified in input file for %s.", name);
318 dfilter_free(temp_dfilter);
319 skip_end_of_line = TRUE;
323 colorf = color_filter_new(name, filter_exp, &bg_color,
325 colorf->c_colorfilter = temp_dfilter;
328 color_filter_add_cb (colorf, arg);
331 skip_end_of_line = TRUE;
339 /* read filters from the user's filter file */
347 /* decide what file to open (from dfilter code) */
348 path = get_persconffile_path("colorfilters", FALSE);
349 if ((f = eth_fopen(path, "r")) == NULL) {
350 if (errno != ENOENT) {
351 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
352 "Could not open filter file\n\"%s\": %s.", path,
361 ret = read_filters_file(f, NULL);
366 /* read filters from the filter file */
368 read_global_filters(void)
374 /* decide what file to open (from dfilter code) */
375 path = get_datafile_path("colorfilters");
376 if ((f = eth_fopen(path, "r")) == NULL) {
377 if (errno != ENOENT) {
378 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
379 "Could not open global filter file\n\"%s\": %s.", path,
388 ret = read_filters_file(f, NULL);
393 /* read filters from some other filter file (import) */
395 color_filters_import(gchar *path, gpointer arg)
400 if ((f = eth_fopen(path, "r")) == NULL) {
401 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
402 "Could not open\n%s\nfor reading: %s.",
403 path, strerror(errno));
407 ret = read_filters_file(f, arg);
412 struct write_filter_data
415 gboolean only_marked;
418 /* save a single filter */
420 write_filter(gpointer filter_arg, gpointer data_arg)
422 struct write_filter_data *data = data_arg;
423 color_filter_t *colorf = filter_arg;
426 if (colorf->marked || !data->only_marked) {
427 fprintf(f,"@%s@%s@[%d,%d,%d][%d,%d,%d]\n",
430 colorf->bg_color.red,
431 colorf->bg_color.green,
432 colorf->bg_color.blue,
433 colorf->fg_color.red,
434 colorf->fg_color.green,
435 colorf->fg_color.blue);
439 /* save filters in a filter file */
441 write_filters_file(FILE *f, gboolean only_marked)
443 struct write_filter_data data;
446 data.only_marked = only_marked;
448 fprintf(f,"# DO NOT EDIT THIS FILE! It was created by Wireshark\n");
449 g_slist_foreach(color_filter_list, write_filter, &data);
453 /* save filters in users filter file */
455 color_filters_write(void)
461 /* Create the directory that holds personal configuration files,
463 if (create_persconffile_dir(&pf_dir_path) == -1) {
464 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
465 "Can't create directory\n\"%s\"\nfor color files: %s.",
466 pf_dir_path, strerror(errno));
471 path = get_persconffile_path("colorfilters", TRUE);
472 if ((f = eth_fopen(path, "w+")) == NULL) {
473 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
474 "Could not open\n%s\nfor writing: %s.",
475 path, strerror(errno));
478 write_filters_file(f, FALSE);
483 /* save filters in some other filter file (export) */
485 color_filters_export(gchar *path, gboolean only_marked)
489 if ((f = eth_fopen(path, "w+")) == NULL) {
490 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
491 "Could not open\n%s\nfor writing: %s.",
492 path, strerror(errno));
495 write_filters_file(f, only_marked);
500 /* delete users filter file and reload global filters */
502 color_filters_revert(void)
506 path = get_persconffile_path("colorfilters", TRUE);
507 if (!deletefile(path)) {
514 /* Reload the (global) filters - Note: this does not update the dialog. */
515 color_filters_init();