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;
56 /* Color Filters can en-/disabled. */
57 gboolean filters_enabled = TRUE;
60 /* Create a new filter */
62 color_filter_new(const gchar *name, /* The name of the filter to create */
63 const gchar *filter_string, /* The string representing the filter */
64 color_t *bg_color, /* The background color */
65 color_t *fg_color) /* The foreground color */
67 color_filter_t *colorf;
69 colorf = g_malloc(sizeof (color_filter_t));
70 colorf->filter_name = g_strdup(name);
71 colorf->filter_text = g_strdup(filter_string);
72 colorf->bg_color = *bg_color;
73 colorf->fg_color = *fg_color;
74 colorf->c_colorfilter = NULL;
75 colorf->edit_dialog = NULL;
76 colorf->selected = FALSE;
80 /* delete the specified filter */
82 color_filter_delete(color_filter_t *colorf)
84 if (colorf->filter_name != NULL)
85 g_free(colorf->filter_name);
86 if (colorf->filter_text != NULL)
87 g_free(colorf->filter_text);
88 if (colorf->c_colorfilter != NULL)
89 dfilter_free(colorf->c_colorfilter);
93 /* delete the specified filter (called from g_slist_foreach) */
95 color_filter_delete_cb(gpointer filter_arg, gpointer unused _U_)
97 color_filter_t *colorf = filter_arg;
99 color_filter_delete(colorf);
102 /* delete the specified list */
104 color_filter_list_delete(GSList **cfl)
106 g_slist_foreach(*cfl, color_filter_delete_cb, NULL);
111 /* clone a single list entries from normal to edit list */
112 static color_filter_t *
113 color_filter_clone(color_filter_t *colorf)
115 color_filter_t *new_colorf;
117 new_colorf = g_malloc(sizeof (color_filter_t));
118 new_colorf->filter_name = g_strdup(colorf->filter_name);
119 new_colorf->filter_text = g_strdup(colorf->filter_text);
120 new_colorf->bg_color = colorf->bg_color;
121 new_colorf->fg_color = colorf->fg_color;
122 new_colorf->c_colorfilter = NULL;
123 new_colorf->edit_dialog = NULL;
124 new_colorf->selected = FALSE;
130 color_filter_list_clone_cb(gpointer filter_arg, gpointer cfl_arg)
132 gpointer *cfl = cfl_arg;
133 color_filter_t *new_colorf;
135 new_colorf = color_filter_clone(filter_arg);
136 *cfl = g_slist_append(*cfl, new_colorf);
139 /* clone the specified list */
141 color_filter_list_clone(GSList *cfl)
143 GSList *new_list = NULL;
145 g_slist_foreach(cfl, color_filter_list_clone_cb, &new_list);
150 /* Initialize the filter structures (reading from file) for general running, including app startup */
152 color_filters_init(void)
154 /* delete all currently existing filters */
155 color_filter_list_delete(&color_filter_list);
157 /* try to read the users filters */
158 if (!read_users_filters(&color_filter_list))
159 /* if that failed, try to read the global filters */
160 color_filters_read_globals(&color_filter_list);
164 color_filters_cleanup(void)
166 /* delete the previously deleted filters */
167 color_filter_list_delete(&color_filter_deleted_list);
171 color_filters_clone_cb(gpointer filter_arg, gpointer user_data)
173 color_filter_t * new_colorf = color_filter_clone(filter_arg);
175 color_filter_add_cb (new_colorf, user_data);
179 color_filters_clone(gpointer user_data)
181 g_slist_foreach(color_filter_list, color_filters_clone_cb, user_data);
186 color_filter_compile_cb(gpointer filter_arg, gpointer unused _U_)
188 color_filter_t *colorf = filter_arg;
190 g_assert(colorf->c_colorfilter == NULL);
191 if (!dfilter_compile(colorf->filter_text, &colorf->c_colorfilter)) {
192 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
193 "Could not compile color filter name: \"%s\" text: \"%s\".\n%s",
194 colorf->filter_name, colorf->filter_text, dfilter_error_msg);
195 /* this filter was compilable before, so this should never happen */
196 g_assert_not_reached();
200 /* apply changes from the edit list */
202 color_filters_apply(GSList *cfl)
204 /* "move" old entries to the deleted list
205 * we must keep them until the dissection no longer needs them */
206 color_filter_deleted_list = g_slist_concat(color_filter_deleted_list, color_filter_list);
207 color_filter_list = NULL;
209 /* clone all list entries from edit to normal list */
210 color_filter_list = color_filter_list_clone(cfl);
212 /* compile all filter */
213 g_slist_foreach(color_filter_list, color_filter_compile_cb, NULL);
217 color_filters_used(void)
219 return color_filter_list != NULL && filters_enabled;
223 color_filters_enable(gboolean enable)
225 filters_enabled = enable;
229 /* prepare the epan_dissect_t for the filter */
231 prime_edt(gpointer data, gpointer user_data)
233 color_filter_t *colorf = data;
234 epan_dissect_t *edt = user_data;
236 if (colorf->c_colorfilter != NULL)
237 epan_dissect_prime_dfilter(edt, colorf->c_colorfilter);
240 /* Prime the epan_dissect_t with all the compiler
241 * color filters in 'color_filter_list'. */
243 color_filters_prime_edt(epan_dissect_t *edt)
245 g_slist_foreach(color_filter_list, prime_edt, edt);
248 /* Colorize a single packet of the packet list */
250 color_filters_colorize_packet(gint row, epan_dissect_t *edt)
253 color_filter_t *colorf;
255 /* If we have color filters, "search" for the matching one. */
256 if (color_filters_used()) {
257 curr = color_filter_list;
259 while(curr != NULL) {
261 if ((colorf->c_colorfilter != NULL) &&
262 dfilter_apply_edt(colorf->c_colorfilter, edt)) {
263 /* this is the filter to use, apply it to the packet list */
264 packet_list_set_colors(row, &(colorf->fg_color), &(colorf->bg_color));
267 curr = g_slist_next(curr);
274 /* read filters from the given file */
275 /* XXX - Would it make more sense to use GStrings here instead of reallocing
278 read_filters_file(FILE *f, gpointer user_data)
280 #define INIT_BUF_SIZE 128
282 gchar *filter_exp = NULL;
283 guint32 name_len = INIT_BUF_SIZE;
284 guint32 filter_exp_len = INIT_BUF_SIZE;
287 guint16 fg_r, fg_g, fg_b, bg_r, bg_g, bg_b;
288 gboolean skip_end_of_line = FALSE;
290 name = g_malloc(name_len + 1);
291 filter_exp = g_malloc(filter_exp_len + 1);
295 if (skip_end_of_line) {
298 } while (c != EOF && c != '\n');
301 skip_end_of_line = FALSE;
304 while ((c = getc(f)) != EOF && isspace(c)) {
313 /* skip # comments and invalid lines */
315 skip_end_of_line = TRUE;
319 /* we get the @ delimiter.
321 * @name@filter expression@[background r,g,b][foreground r,g,b]
328 if (c == EOF || c == '@')
331 /* buffer isn't long enough; double its length.*/
333 name = g_realloc(name, name_len + 1);
342 skip_end_of_line = TRUE;
346 /* retrieve filter expression */
350 if (c == EOF || c == '@')
352 if (i >= filter_exp_len) {
353 /* buffer isn't long enough; double its length.*/
355 filter_exp = g_realloc(filter_exp, filter_exp_len + 1);
359 filter_exp[i] = '\0';
364 skip_end_of_line = TRUE;
368 /* retrieve background and foreground colors */
369 if (fscanf(f,"[%hu,%hu,%hu][%hu,%hu,%hu]",
370 &bg_r, &bg_g, &bg_b, &fg_r, &fg_g, &fg_b) == 6) {
372 /* we got a complete color filter */
374 color_t bg_color, fg_color;
375 color_filter_t *colorf;
376 dfilter_t *temp_dfilter;
378 if (!dfilter_compile(filter_exp, &temp_dfilter)) {
379 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
380 "Could not compile color filter %s from saved filters.\n%s",
381 name, dfilter_error_msg);
382 skip_end_of_line = TRUE;
386 if (!initialize_color(&fg_color, fg_r, fg_g, fg_b)) {
388 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
389 "Could not allocate foreground color "
390 "specified in input file for %s.", name);
391 dfilter_free(temp_dfilter);
392 skip_end_of_line = TRUE;
395 if (!initialize_color(&bg_color, bg_r, bg_g, bg_b)) {
397 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
398 "Could not allocate background color "
399 "specified in input file for %s.", name);
400 dfilter_free(temp_dfilter);
401 skip_end_of_line = TRUE;
405 colorf = color_filter_new(name, filter_exp, &bg_color,
407 if(user_data == &color_filter_list) {
408 GSList **cfl = (GSList **)user_data;
411 colorf->c_colorfilter = temp_dfilter;
412 *cfl = g_slist_append(*cfl, colorf);
415 /* just editing, don't need the compiled filter */
416 dfilter_free(temp_dfilter);
417 color_filter_add_cb (colorf, user_data);
421 skip_end_of_line = TRUE;
429 /* read filters from the user's filter file */
431 read_users_filters(GSList **cfl)
437 /* decide what file to open (from dfilter code) */
438 path = get_persconffile_path("colorfilters", FALSE);
439 if ((f = eth_fopen(path, "r")) == NULL) {
440 if (errno != ENOENT) {
441 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
442 "Could not open filter file\n\"%s\": %s.", path,
451 ret = read_filters_file(f, cfl);
456 /* read filters from the filter file */
458 color_filters_read_globals(gpointer user_data)
464 /* decide what file to open (from dfilter code) */
465 path = get_datafile_path("colorfilters");
466 if ((f = eth_fopen(path, "r")) == NULL) {
467 if (errno != ENOENT) {
468 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
469 "Could not open global filter file\n\"%s\": %s.", path,
478 ret = read_filters_file(f, user_data);
483 /* read filters from some other filter file (import) */
485 color_filters_import(gchar *path, gpointer user_data)
490 if ((f = eth_fopen(path, "r")) == NULL) {
491 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
492 "Could not open\n%s\nfor reading: %s.",
493 path, strerror(errno));
497 ret = read_filters_file(f, user_data);
502 struct write_filter_data
505 gboolean only_selected;
508 /* save a single filter */
510 write_filter(gpointer filter_arg, gpointer data_arg)
512 struct write_filter_data *data = data_arg;
513 color_filter_t *colorf = filter_arg;
516 if (colorf->selected || !data->only_selected) {
517 fprintf(f,"@%s@%s@[%d,%d,%d][%d,%d,%d]\n",
520 colorf->bg_color.red,
521 colorf->bg_color.green,
522 colorf->bg_color.blue,
523 colorf->fg_color.red,
524 colorf->fg_color.green,
525 colorf->fg_color.blue);
529 /* save filters in a filter file */
531 write_filters_file(GSList *cfl, FILE *f, gboolean only_selected)
533 struct write_filter_data data;
536 data.only_selected = only_selected;
538 fprintf(f,"# DO NOT EDIT THIS FILE! It was created by Wireshark\n");
539 g_slist_foreach(cfl, write_filter, &data);
543 /* save filters in users filter file */
545 color_filters_write(GSList *cfl)
551 /* Create the directory that holds personal configuration files,
553 if (create_persconffile_dir(&pf_dir_path) == -1) {
554 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
555 "Can't create directory\n\"%s\"\nfor color files: %s.",
556 pf_dir_path, strerror(errno));
561 path = get_persconffile_path("colorfilters", TRUE);
562 if ((f = eth_fopen(path, "w+")) == NULL) {
563 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
564 "Could not open\n%s\nfor writing: %s.",
565 path, strerror(errno));
568 write_filters_file(cfl, f, FALSE);
573 /* save filters in some other filter file (export) */
575 color_filters_export(gchar *path, GSList *cfl, gboolean only_marked)
579 if ((f = eth_fopen(path, "w+")) == NULL) {
580 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
581 "Could not open\n%s\nfor writing: %s.",
582 path, strerror(errno));
585 write_filters_file(cfl, f, only_marked);