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(GSList **cfl);
49 /* the currently active filters */
50 static GSList *color_filter_list = NULL;
52 /* keep "old" deleted filters in this list until
53 * the dissection no longer needs them (e.g. file is closed) */
54 static GSList *color_filter_deleted_list = NULL;
55 static GSList *color_filter_valid_list = NULL;
57 /* Color Filters can en-/disabled. */
58 gboolean filters_enabled = TRUE;
61 /* Create a new filter */
63 color_filter_new(const gchar *name, /* The name of the filter to create */
64 const gchar *filter_string, /* The string representing the filter */
65 color_t *bg_color, /* The background color */
66 color_t *fg_color, /* The foreground color */
67 gboolean disabled) /* Is the filter disabled? */
69 color_filter_t *colorf;
71 colorf = g_malloc(sizeof (color_filter_t));
72 colorf->filter_name = g_strdup(name);
73 colorf->filter_text = g_strdup(filter_string);
74 colorf->bg_color = *bg_color;
75 colorf->fg_color = *fg_color;
76 colorf->disabled = disabled;
77 colorf->c_colorfilter = NULL;
78 colorf->edit_dialog = NULL;
79 colorf->selected = FALSE;
83 /* delete the specified filter */
85 color_filter_delete(color_filter_t *colorf)
87 if (colorf->filter_name != NULL)
88 g_free(colorf->filter_name);
89 if (colorf->filter_text != NULL)
90 g_free(colorf->filter_text);
91 if (colorf->c_colorfilter != NULL)
92 dfilter_free(colorf->c_colorfilter);
96 /* delete the specified filter (called from g_slist_foreach) */
98 color_filter_delete_cb(gpointer filter_arg, gpointer unused _U_)
100 color_filter_t *colorf = filter_arg;
102 color_filter_delete(colorf);
105 /* delete the specified list */
107 color_filter_list_delete(GSList **cfl)
109 g_slist_foreach(*cfl, color_filter_delete_cb, NULL);
114 /* clone a single list entries from normal to edit list */
115 static color_filter_t *
116 color_filter_clone(color_filter_t *colorf)
118 color_filter_t *new_colorf;
120 new_colorf = g_malloc(sizeof (color_filter_t));
121 new_colorf->filter_name = g_strdup(colorf->filter_name);
122 new_colorf->filter_text = g_strdup(colorf->filter_text);
123 new_colorf->bg_color = colorf->bg_color;
124 new_colorf->fg_color = colorf->fg_color;
125 new_colorf->disabled = colorf->disabled;
126 new_colorf->c_colorfilter = NULL;
127 new_colorf->edit_dialog = NULL;
128 new_colorf->selected = FALSE;
134 color_filter_list_clone_cb(gpointer filter_arg, gpointer cfl_arg)
136 gpointer *cfl = cfl_arg;
137 color_filter_t *new_colorf;
139 new_colorf = color_filter_clone(filter_arg);
140 *cfl = g_slist_append(*cfl, new_colorf);
143 /* clone the specified list */
145 color_filter_list_clone(GSList *cfl)
147 GSList *new_list = NULL;
149 g_slist_foreach(cfl, color_filter_list_clone_cb, &new_list);
154 /* Initialize the filter structures (reading from file) for general running, including app startup */
156 color_filters_init(void)
158 /* delete all currently existing filters */
159 color_filter_list_delete(&color_filter_list);
161 /* try to read the users filters */
162 if (!read_users_filters(&color_filter_list))
163 /* if that failed, try to read the global filters */
164 color_filters_read_globals(&color_filter_list);
168 color_filters_cleanup(void)
170 /* delete the previously deleted filters */
171 color_filter_list_delete(&color_filter_deleted_list);
175 color_filters_clone_cb(gpointer filter_arg, gpointer user_data)
177 color_filter_t * new_colorf = color_filter_clone(filter_arg);
179 color_filter_add_cb (new_colorf, user_data);
183 color_filters_clone(gpointer user_data)
185 g_slist_foreach(color_filter_list, color_filters_clone_cb, user_data);
190 color_filter_compile_cb(gpointer filter_arg, gpointer unused _U_)
192 color_filter_t *colorf = filter_arg;
194 g_assert(colorf->c_colorfilter == NULL);
195 if (!dfilter_compile(colorf->filter_text, &colorf->c_colorfilter)) {
196 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
197 "Could not compile color filter name: \"%s\" text: \"%s\".\n%s",
198 colorf->filter_name, colorf->filter_text, dfilter_error_msg);
199 /* this filter was compilable before, so this should never happen */
200 /* except if the OK button of the parent window has been clicked */
201 /* so don't use g_assert_not_reached() but check the filters again */
206 color_filter_validate_cb(gpointer filter_arg, gpointer unused _U_)
208 color_filter_t *colorf = filter_arg;
210 g_assert(colorf->c_colorfilter == NULL);
211 if (!dfilter_compile(colorf->filter_text, &colorf->c_colorfilter)) {
212 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
213 "Removing color filter name: \"%s\" text: \"%s\".\n%s",
214 colorf->filter_name, colorf->filter_text, dfilter_error_msg);
215 /* Delete the color filter from the list of color filters. */
216 color_filter_valid_list = g_slist_remove(color_filter_valid_list, colorf);
217 color_filter_delete(colorf);
221 /* apply changes from the edit list */
223 color_filters_apply(GSList *cfl)
225 /* "move" old entries to the deleted list
226 * we must keep them until the dissection no longer needs them */
227 color_filter_deleted_list = g_slist_concat(color_filter_deleted_list, color_filter_list);
228 color_filter_list = NULL;
230 /* clone all list entries from edit to normal list */
231 color_filter_valid_list = NULL;
232 color_filter_valid_list = color_filter_list_clone(cfl);
234 /* compile all filter */
235 g_slist_foreach(color_filter_valid_list, color_filter_validate_cb, NULL);
237 /* clone all list entries from edit to normal list */
238 color_filter_list = color_filter_list_clone(color_filter_valid_list);
240 /* compile all filter */
241 g_slist_foreach(color_filter_list, color_filter_compile_cb, NULL);
245 color_filters_used(void)
247 return color_filter_list != NULL && filters_enabled;
251 color_filters_enable(gboolean enable)
253 filters_enabled = enable;
257 /* prepare the epan_dissect_t for the filter */
259 prime_edt(gpointer data, gpointer user_data)
261 color_filter_t *colorf = data;
262 epan_dissect_t *edt = user_data;
264 if (colorf->c_colorfilter != NULL)
265 epan_dissect_prime_dfilter(edt, colorf->c_colorfilter);
268 /* Prime the epan_dissect_t with all the compiler
269 * color filters in 'color_filter_list'. */
271 color_filters_prime_edt(epan_dissect_t *edt)
273 g_slist_foreach(color_filter_list, prime_edt, edt);
276 /* Colorize a single packet of the packet list */
278 color_filters_colorize_packet(gint row, epan_dissect_t *edt)
281 color_filter_t *colorf;
283 /* If we have color filters, "search" for the matching one. */
284 if (color_filters_used()) {
285 curr = color_filter_list;
287 while(curr != NULL) {
289 if ((colorf->c_colorfilter != NULL) &&
290 (!colorf->disabled) &&
291 dfilter_apply_edt(colorf->c_colorfilter, edt)) {
292 /* this is the filter to use, apply it to the packet list */
293 packet_list_set_colors(row, &(colorf->fg_color), &(colorf->bg_color));
296 curr = g_slist_next(curr);
303 /* read filters from the given file */
304 /* XXX - Would it make more sense to use GStrings here instead of reallocing
307 read_filters_file(FILE *f, gpointer user_data)
309 #define INIT_BUF_SIZE 128
311 gchar *filter_exp = NULL;
312 guint32 name_len = INIT_BUF_SIZE;
313 guint32 filter_exp_len = INIT_BUF_SIZE;
316 guint16 fg_r, fg_g, fg_b, bg_r, bg_g, bg_b;
317 gboolean disabled = FALSE;
318 gboolean skip_end_of_line = FALSE;
320 name = g_malloc(name_len + 1);
321 filter_exp = g_malloc(filter_exp_len + 1);
325 if (skip_end_of_line) {
328 } while (c != EOF && c != '\n');
332 skip_end_of_line = FALSE;
335 while ((c = getc(f)) != EOF && isspace(c)) {
349 /* skip # comments and invalid lines */
351 skip_end_of_line = TRUE;
355 /* we get the @ delimiter.
357 * @name@filter expression@[background r,g,b][foreground r,g,b]
364 if (c == EOF || c == '@')
367 /* buffer isn't long enough; double its length.*/
369 name = g_realloc(name, name_len + 1);
378 skip_end_of_line = TRUE;
382 /* retrieve filter expression */
386 if (c == EOF || c == '@')
388 if (i >= filter_exp_len) {
389 /* buffer isn't long enough; double its length.*/
391 filter_exp = g_realloc(filter_exp, filter_exp_len + 1);
395 filter_exp[i] = '\0';
400 skip_end_of_line = TRUE;
404 /* retrieve background and foreground colors */
405 if (fscanf(f,"[%hu,%hu,%hu][%hu,%hu,%hu]",
406 &bg_r, &bg_g, &bg_b, &fg_r, &fg_g, &fg_b) == 6) {
408 /* we got a complete color filter */
410 color_t bg_color, fg_color;
411 color_filter_t *colorf;
412 dfilter_t *temp_dfilter;
414 if (!dfilter_compile(filter_exp, &temp_dfilter)) {
415 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
416 "Could not compile color filter %s from saved filters.\n%s",
417 name, dfilter_error_msg);
418 skip_end_of_line = TRUE;
422 if (!initialize_color(&fg_color, fg_r, fg_g, fg_b)) {
424 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
425 "Could not allocate foreground color "
426 "specified in input file for %s.", name);
427 dfilter_free(temp_dfilter);
428 skip_end_of_line = TRUE;
431 if (!initialize_color(&bg_color, bg_r, bg_g, bg_b)) {
433 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
434 "Could not allocate background color "
435 "specified in input file for %s.", name);
436 dfilter_free(temp_dfilter);
437 skip_end_of_line = TRUE;
441 colorf = color_filter_new(name, filter_exp, &bg_color,
442 &fg_color, disabled);
443 if(user_data == &color_filter_list) {
444 GSList **cfl = (GSList **)user_data;
447 colorf->c_colorfilter = temp_dfilter;
448 *cfl = g_slist_append(*cfl, colorf);
451 /* just editing, don't need the compiled filter */
452 dfilter_free(temp_dfilter);
453 color_filter_add_cb (colorf, user_data);
457 skip_end_of_line = TRUE;
465 /* read filters from the user's filter file */
467 read_users_filters(GSList **cfl)
473 /* decide what file to open (from dfilter code) */
474 path = get_persconffile_path("colorfilters", FALSE);
475 if ((f = eth_fopen(path, "r")) == NULL) {
476 if (errno != ENOENT) {
477 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
478 "Could not open filter file\n\"%s\": %s.", path,
487 ret = read_filters_file(f, cfl);
492 /* read filters from the filter file */
494 color_filters_read_globals(gpointer user_data)
500 /* decide what file to open (from dfilter code) */
501 path = get_datafile_path("colorfilters");
502 if ((f = eth_fopen(path, "r")) == NULL) {
503 if (errno != ENOENT) {
504 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
505 "Could not open global filter file\n\"%s\": %s.", path,
514 ret = read_filters_file(f, user_data);
519 /* read filters from some other filter file (import) */
521 color_filters_import(gchar *path, gpointer user_data)
526 if ((f = eth_fopen(path, "r")) == NULL) {
527 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
528 "Could not open\n%s\nfor reading: %s.",
529 path, strerror(errno));
533 ret = read_filters_file(f, user_data);
538 struct write_filter_data
541 gboolean only_selected;
544 /* save a single filter */
546 write_filter(gpointer filter_arg, gpointer data_arg)
548 struct write_filter_data *data = data_arg;
549 color_filter_t *colorf = filter_arg;
552 if (colorf->selected || !data->only_selected) {
553 fprintf(f,"%s@%s@%s@[%d,%d,%d][%d,%d,%d]\n",
554 colorf->disabled ? "!" : "",
557 colorf->bg_color.red,
558 colorf->bg_color.green,
559 colorf->bg_color.blue,
560 colorf->fg_color.red,
561 colorf->fg_color.green,
562 colorf->fg_color.blue);
566 /* save filters in a filter file */
568 write_filters_file(GSList *cfl, FILE *f, gboolean only_selected)
570 struct write_filter_data data;
573 data.only_selected = only_selected;
575 fprintf(f,"# DO NOT EDIT THIS FILE! It was created by Wireshark\n");
576 g_slist_foreach(cfl, write_filter, &data);
580 /* save filters in users filter file */
582 color_filters_write(GSList *cfl)
588 /* Create the directory that holds personal configuration files,
590 if (create_persconffile_dir(&pf_dir_path) == -1) {
591 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
592 "Can't create directory\n\"%s\"\nfor color files: %s.",
593 pf_dir_path, strerror(errno));
598 path = get_persconffile_path("colorfilters", TRUE);
599 if ((f = eth_fopen(path, "w+")) == NULL) {
600 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
601 "Could not open\n%s\nfor writing: %s.",
602 path, strerror(errno));
605 write_filters_file(cfl, f, FALSE);
610 /* save filters in some other filter file (export) */
612 color_filters_export(gchar *path, GSList *cfl, gboolean only_marked)
616 if ((f = eth_fopen(path, "w+")) == NULL) {
617 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
618 "Could not open\n%s\nfor writing: %s.",
619 path, strerror(errno));
622 write_filters_file(cfl, f, only_marked);