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_users_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 color_filter_delete(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 (called from g_slist_foreach) */
86 color_filter_delete_cb(gpointer filter_arg, gpointer unused _U_)
88 color_filter_t *colorf = filter_arg;
89 color_filter_delete(colorf);
92 /* delete all the filters */
94 color_filters_delete_all(void)
96 /* delete the color_filter_list */
97 g_slist_foreach(color_filter_list, color_filter_delete_cb, NULL);
98 g_slist_free(color_filter_list);
99 color_filter_list = NULL;
101 /* delete the removed_filter_list */
102 g_slist_foreach(removed_filter_list, color_filter_delete_cb, NULL);
103 g_slist_free(removed_filter_list);
104 removed_filter_list = NULL;
107 /* Initialize the filter structures (reading from file) for general running, including app startup */
109 color_filters_init(void)
111 /* delete all existing filters */
112 color_filters_delete_all();
114 /* try to read the users filters */
115 if (!read_users_filters())
116 /* if that failed, try to read the global filters */
117 read_global_filters();
120 /* Create a new filter */
122 color_filter_new(const gchar *name, /* The name of the filter to create */
123 const gchar *filter_string, /* The string representing the filter */
124 color_t *bg_color, /* The background color */
125 color_t *fg_color) /* The foreground color */
127 color_filter_t *colorf;
129 colorf = g_malloc(sizeof (color_filter_t));
130 colorf->filter_name = g_strdup(name);
131 colorf->filter_text = g_strdup(filter_string);
132 colorf->bg_color = *bg_color;
133 colorf->fg_color = *fg_color;
134 colorf->c_colorfilter = NULL;
135 colorf->edit_dialog = NULL;
136 colorf->selected = FALSE;
137 color_filter_list = g_slist_append(color_filter_list, colorf);
142 color_filters_used(void)
144 return color_filter_list != NULL && filters_enabled;
148 color_filters_enable(gboolean enable)
150 filters_enabled = enable;
154 /* prepare the epan_dissect_t for the filter */
156 prime_edt(gpointer data, gpointer user_data)
158 color_filter_t *colorf = data;
159 epan_dissect_t *edt = user_data;
161 if (colorf->c_colorfilter != NULL)
162 epan_dissect_prime_dfilter(edt, colorf->c_colorfilter);
165 /* Prime the epan_dissect_t with all the compiler
166 * color filters in 'color_filter_list'. */
168 color_filters_prime_edt(epan_dissect_t *edt)
170 g_slist_foreach(color_filter_list, prime_edt, edt);
173 /* Colorize a single packet of the packet list */
175 color_filters_colorize_packet(gint row, epan_dissect_t *edt)
178 color_filter_t *colorf;
180 /* If we have color filters, "search" for the matching one. */
181 if (color_filters_used()) {
182 curr = color_filter_list;
184 while( (curr = g_slist_next(curr)) != NULL) {
186 if ((colorf->c_colorfilter != NULL) &&
187 dfilter_apply_edt(colorf->c_colorfilter, edt)) {
188 /* this is the filter to use, apply it to the packet list */
189 packet_list_set_colors(row, &(colorf->fg_color), &(colorf->bg_color));
198 /* read filters from the given file */
199 /* XXX - Would it make more sense to use GStrings here instead of reallocing
202 read_filters_file(FILE *f, gpointer arg)
204 #define INIT_BUF_SIZE 128
206 gchar *filter_exp = NULL;
207 guint32 name_len = INIT_BUF_SIZE;
208 guint32 filter_exp_len = INIT_BUF_SIZE;
211 guint16 fg_r, fg_g, fg_b, bg_r, bg_g, bg_b;
212 gboolean skip_end_of_line = FALSE;
214 name = g_malloc(name_len + 1);
215 filter_exp = g_malloc(filter_exp_len + 1);
219 if (skip_end_of_line) {
222 } while (c != EOF && c != '\n');
225 skip_end_of_line = FALSE;
228 while ((c = getc(f)) != EOF && isspace(c)) {
237 /* skip # comments and invalid lines */
239 skip_end_of_line = TRUE;
243 /* we get the @ delimiter.
245 * @name@filter expression@[background r,g,b][foreground r,g,b]
252 if (c == EOF || c == '@')
255 /* buffer isn't long enough; double its length.*/
257 name = g_realloc(name, name_len + 1);
266 skip_end_of_line = TRUE;
270 /* retrieve filter expression */
274 if (c == EOF || c == '@')
276 if (i >= filter_exp_len) {
277 /* buffer isn't long enough; double its length.*/
279 filter_exp = g_realloc(filter_exp, filter_exp_len + 1);
283 filter_exp[i] = '\0';
288 skip_end_of_line = TRUE;
292 /* retrieve background and foreground colors */
293 if (fscanf(f,"[%hu,%hu,%hu][%hu,%hu,%hu]",
294 &bg_r, &bg_g, &bg_b, &fg_r, &fg_g, &fg_b) == 6) {
296 /* we got a complete color filter */
298 color_t bg_color, fg_color;
299 color_filter_t *colorf;
300 dfilter_t *temp_dfilter;
302 if (!dfilter_compile(filter_exp, &temp_dfilter)) {
303 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
304 "Could not compile color filter %s from saved filters.\n%s",
305 name, dfilter_error_msg);
306 skip_end_of_line = TRUE;
310 if (!initialize_color(&fg_color, fg_r, fg_g, fg_b)) {
312 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
313 "Could not allocate foreground color "
314 "specified in input file for %s.", name);
315 dfilter_free(temp_dfilter);
316 skip_end_of_line = TRUE;
319 if (!initialize_color(&bg_color, bg_r, bg_g, bg_b)) {
321 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
322 "Could not allocate background color "
323 "specified in input file for %s.", name);
324 dfilter_free(temp_dfilter);
325 skip_end_of_line = TRUE;
329 colorf = color_filter_new(name, filter_exp, &bg_color,
331 colorf->c_colorfilter = temp_dfilter;
334 color_filter_add_cb (colorf, arg);
337 skip_end_of_line = TRUE;
345 /* read filters from the user's filter file */
347 read_users_filters(void)
353 /* decide what file to open (from dfilter code) */
354 path = get_persconffile_path("colorfilters", FALSE);
355 if ((f = eth_fopen(path, "r")) == NULL) {
356 if (errno != ENOENT) {
357 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
358 "Could not open filter file\n\"%s\": %s.", path,
367 ret = read_filters_file(f, NULL);
372 /* read filters from the filter file */
374 read_global_filters(void)
380 /* decide what file to open (from dfilter code) */
381 path = get_datafile_path("colorfilters");
382 if ((f = eth_fopen(path, "r")) == NULL) {
383 if (errno != ENOENT) {
384 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
385 "Could not open global filter file\n\"%s\": %s.", path,
394 ret = read_filters_file(f, NULL);
399 /* read filters from some other filter file (import) */
401 color_filters_import(gchar *path, gpointer arg)
406 if ((f = eth_fopen(path, "r")) == NULL) {
407 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
408 "Could not open\n%s\nfor reading: %s.",
409 path, strerror(errno));
413 ret = read_filters_file(f, arg);
418 struct write_filter_data
421 gboolean only_selected;
424 /* save a single filter */
426 write_filter(gpointer filter_arg, gpointer data_arg)
428 struct write_filter_data *data = data_arg;
429 color_filter_t *colorf = filter_arg;
432 if (colorf->selected || !data->only_selected) {
433 fprintf(f,"@%s@%s@[%d,%d,%d][%d,%d,%d]\n",
436 colorf->bg_color.red,
437 colorf->bg_color.green,
438 colorf->bg_color.blue,
439 colorf->fg_color.red,
440 colorf->fg_color.green,
441 colorf->fg_color.blue);
445 /* save filters in a filter file */
447 write_filters_file(FILE *f, gboolean only_selected)
449 struct write_filter_data data;
452 data.only_selected = only_selected;
454 fprintf(f,"# DO NOT EDIT THIS FILE! It was created by Wireshark\n");
455 g_slist_foreach(color_filter_list, write_filter, &data);
459 /* save filters in users filter file */
461 color_filters_write(void)
467 /* Create the directory that holds personal configuration files,
469 if (create_persconffile_dir(&pf_dir_path) == -1) {
470 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
471 "Can't create directory\n\"%s\"\nfor color files: %s.",
472 pf_dir_path, strerror(errno));
477 path = get_persconffile_path("colorfilters", TRUE);
478 if ((f = eth_fopen(path, "w+")) == NULL) {
479 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
480 "Could not open\n%s\nfor writing: %s.",
481 path, strerror(errno));
484 write_filters_file(f, FALSE);
489 /* save filters in some other filter file (export) */
491 color_filters_export(gchar *path, gboolean only_marked)
495 if ((f = eth_fopen(path, "w+")) == NULL) {
496 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
497 "Could not open\n%s\nfor writing: %s.",
498 path, strerror(errno));
501 write_filters_file(f, only_marked);
506 /* delete users filter file and reload global filters */
508 color_filters_revert(void)
512 /* try to delete the "old" user color filter file */
513 path = get_persconffile_path("colorfilters", TRUE);
514 if (!deletefile(path)) {
521 /* Reload the (global) filters - Note: this does not update the dialog. */
522 color_filters_init();