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)
132 color_filter_t *new_colorf;
134 new_colorf = color_filter_clone(filter_arg);
135 *cfl = g_slist_append(*cfl, new_colorf);
138 /* clone the specified list */
140 color_filter_list_clone(GSList *cfl)
142 GSList *new_list = NULL;
144 g_slist_foreach(cfl, color_filter_list_clone_cb, &new_list);
149 /* Initialize the filter structures (reading from file) for general running, including app startup */
151 color_filters_init(void)
153 /* delete all currently existing filters */
154 color_filter_list_delete(&color_filter_list);
156 /* try to read the users filters */
157 if (!read_users_filters(&color_filter_list))
158 /* if that failed, try to read the global filters */
159 color_filters_read_globals(&color_filter_list);
163 color_filters_cleanup(void)
165 /* delete the previously deleted filters */
166 color_filter_list_delete(&color_filter_deleted_list);
170 color_filters_clone_cb(gpointer filter_arg, gpointer user_data)
172 color_filter_t * new_colorf = color_filter_clone(filter_arg);
174 color_filter_add_cb (new_colorf, user_data);
178 color_filters_clone(gpointer user_data)
180 g_slist_foreach(color_filter_list, color_filters_clone_cb, user_data);
185 color_filter_compile_cb(gpointer filter_arg, gpointer *cfl)
187 color_filter_t *colorf = filter_arg;
189 g_assert(colorf->c_colorfilter == NULL);
190 if (!dfilter_compile(colorf->filter_text, &colorf->c_colorfilter)) {
191 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
192 "Could not compile color filter name: \"%s\" text: \"%s\".\n%s",
193 colorf->filter_name, colorf->filter_text, dfilter_error_msg);
194 /* this filter was compilable before, so this should never happen */
195 g_assert_not_reached();
199 /* apply changes from the edit list */
201 color_filters_apply(GSList *cfl)
203 /* "move" old entries to the deleted list
204 * we must keep them until the dissection no longer needs them */
205 color_filter_deleted_list = g_slist_concat(color_filter_deleted_list, color_filter_list);
206 color_filter_list = NULL;
208 /* clone all list entries from edit to normal list */
209 color_filter_list = color_filter_list_clone(cfl);
211 /* compile all filter */
212 g_slist_foreach(color_filter_list, color_filter_compile_cb, NULL);
216 color_filters_used(void)
218 return color_filter_list != NULL && filters_enabled;
222 color_filters_enable(gboolean enable)
224 filters_enabled = enable;
228 /* prepare the epan_dissect_t for the filter */
230 prime_edt(gpointer data, gpointer user_data)
232 color_filter_t *colorf = data;
233 epan_dissect_t *edt = user_data;
235 if (colorf->c_colorfilter != NULL)
236 epan_dissect_prime_dfilter(edt, colorf->c_colorfilter);
239 /* Prime the epan_dissect_t with all the compiler
240 * color filters in 'color_filter_list'. */
242 color_filters_prime_edt(epan_dissect_t *edt)
244 g_slist_foreach(color_filter_list, prime_edt, edt);
247 /* Colorize a single packet of the packet list */
249 color_filters_colorize_packet(gint row, epan_dissect_t *edt)
252 color_filter_t *colorf;
254 /* If we have color filters, "search" for the matching one. */
255 if (color_filters_used()) {
256 curr = color_filter_list;
258 while( (curr = g_slist_next(curr)) != NULL) {
260 if ((colorf->c_colorfilter != NULL) &&
261 dfilter_apply_edt(colorf->c_colorfilter, edt)) {
262 /* this is the filter to use, apply it to the packet list */
263 packet_list_set_colors(row, &(colorf->fg_color), &(colorf->bg_color));
272 /* read filters from the given file */
273 /* XXX - Would it make more sense to use GStrings here instead of reallocing
276 read_filters_file(FILE *f, gpointer user_data)
278 #define INIT_BUF_SIZE 128
280 gchar *filter_exp = NULL;
281 guint32 name_len = INIT_BUF_SIZE;
282 guint32 filter_exp_len = INIT_BUF_SIZE;
285 guint16 fg_r, fg_g, fg_b, bg_r, bg_g, bg_b;
286 gboolean skip_end_of_line = FALSE;
288 name = g_malloc(name_len + 1);
289 filter_exp = g_malloc(filter_exp_len + 1);
293 if (skip_end_of_line) {
296 } while (c != EOF && c != '\n');
299 skip_end_of_line = FALSE;
302 while ((c = getc(f)) != EOF && isspace(c)) {
311 /* skip # comments and invalid lines */
313 skip_end_of_line = TRUE;
317 /* we get the @ delimiter.
319 * @name@filter expression@[background r,g,b][foreground r,g,b]
326 if (c == EOF || c == '@')
329 /* buffer isn't long enough; double its length.*/
331 name = g_realloc(name, name_len + 1);
340 skip_end_of_line = TRUE;
344 /* retrieve filter expression */
348 if (c == EOF || c == '@')
350 if (i >= filter_exp_len) {
351 /* buffer isn't long enough; double its length.*/
353 filter_exp = g_realloc(filter_exp, filter_exp_len + 1);
357 filter_exp[i] = '\0';
362 skip_end_of_line = TRUE;
366 /* retrieve background and foreground colors */
367 if (fscanf(f,"[%hu,%hu,%hu][%hu,%hu,%hu]",
368 &bg_r, &bg_g, &bg_b, &fg_r, &fg_g, &fg_b) == 6) {
370 /* we got a complete color filter */
372 color_t bg_color, fg_color;
373 color_filter_t *colorf;
374 dfilter_t *temp_dfilter;
376 if (!dfilter_compile(filter_exp, &temp_dfilter)) {
377 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
378 "Could not compile color filter %s from saved filters.\n%s",
379 name, dfilter_error_msg);
380 skip_end_of_line = TRUE;
384 if (!initialize_color(&fg_color, fg_r, fg_g, fg_b)) {
386 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
387 "Could not allocate foreground color "
388 "specified in input file for %s.", name);
389 dfilter_free(temp_dfilter);
390 skip_end_of_line = TRUE;
393 if (!initialize_color(&bg_color, bg_r, bg_g, bg_b)) {
395 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
396 "Could not allocate background color "
397 "specified in input file for %s.", name);
398 dfilter_free(temp_dfilter);
399 skip_end_of_line = TRUE;
403 colorf = color_filter_new(name, filter_exp, &bg_color,
405 if(user_data == &color_filter_list) {
406 GSList **cfl = (GSList **)user_data;
409 colorf->c_colorfilter = temp_dfilter;
410 *cfl = g_slist_append(*cfl, colorf);
413 /* just editing, don't need the compiled filter */
414 dfilter_free(temp_dfilter);
415 color_filter_add_cb (colorf, user_data);
419 skip_end_of_line = TRUE;
427 /* read filters from the user's filter file */
429 read_users_filters(GSList **cfl)
435 /* decide what file to open (from dfilter code) */
436 path = get_persconffile_path("colorfilters", FALSE);
437 if ((f = eth_fopen(path, "r")) == NULL) {
438 if (errno != ENOENT) {
439 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
440 "Could not open filter file\n\"%s\": %s.", path,
449 ret = read_filters_file(f, cfl);
454 /* read filters from the filter file */
456 color_filters_read_globals(gpointer user_data)
462 /* decide what file to open (from dfilter code) */
463 path = get_datafile_path("colorfilters");
464 if ((f = eth_fopen(path, "r")) == NULL) {
465 if (errno != ENOENT) {
466 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
467 "Could not open global filter file\n\"%s\": %s.", path,
476 ret = read_filters_file(f, user_data);
481 /* read filters from some other filter file (import) */
483 color_filters_import(gchar *path, gpointer user_data)
488 if ((f = eth_fopen(path, "r")) == NULL) {
489 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
490 "Could not open\n%s\nfor reading: %s.",
491 path, strerror(errno));
495 ret = read_filters_file(f, user_data);
500 struct write_filter_data
503 gboolean only_selected;
506 /* save a single filter */
508 write_filter(gpointer filter_arg, gpointer data_arg)
510 struct write_filter_data *data = data_arg;
511 color_filter_t *colorf = filter_arg;
514 if (colorf->selected || !data->only_selected) {
515 fprintf(f,"@%s@%s@[%d,%d,%d][%d,%d,%d]\n",
518 colorf->bg_color.red,
519 colorf->bg_color.green,
520 colorf->bg_color.blue,
521 colorf->fg_color.red,
522 colorf->fg_color.green,
523 colorf->fg_color.blue);
527 /* save filters in a filter file */
529 write_filters_file(GSList *cfl, FILE *f, gboolean only_selected)
531 struct write_filter_data data;
534 data.only_selected = only_selected;
536 fprintf(f,"# DO NOT EDIT THIS FILE! It was created by Wireshark\n");
537 g_slist_foreach(cfl, write_filter, &data);
541 /* save filters in users filter file */
543 color_filters_write(GSList *cfl)
549 /* Create the directory that holds personal configuration files,
551 if (create_persconffile_dir(&pf_dir_path) == -1) {
552 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
553 "Can't create directory\n\"%s\"\nfor color files: %s.",
554 pf_dir_path, strerror(errno));
559 path = get_persconffile_path("colorfilters", TRUE);
560 if ((f = eth_fopen(path, "w+")) == NULL) {
561 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
562 "Could not open\n%s\nfor writing: %s.",
563 path, strerror(errno));
566 write_filters_file(cfl, f, FALSE);
571 /* save filters in some other filter file (export) */
573 color_filters_export(gchar *path, GSList *cfl, gboolean only_marked)
577 if ((f = eth_fopen(path, "w+")) == NULL) {
578 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
579 "Could not open\n%s\nfor writing: %s.",
580 path, strerror(errno));
583 write_filters_file(cfl, f, only_marked);