2 * Routines for color filters
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@ethereal.com>
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 prime_edt(gpointer data, gpointer user_data)
137 color_filter_t *colorf = data;
138 epan_dissect_t *edt = user_data;
140 if (colorf->c_colorfilter != NULL)
141 epan_dissect_prime_dfilter(edt, colorf->c_colorfilter);
145 color_filters_used(void)
147 return color_filter_list != NULL && filters_enabled;
151 color_filters_enable(gboolean enable)
153 filters_enabled = enable;
158 color_filter_t *colorf;
160 } apply_color_filter_args;
163 * If no color filter has been applied, apply this one.
164 * (The "if no color filter has been applied" is to handle the case where
165 * more than one color filter matches the packet.)
168 apply_color_filter(gpointer filter_arg, gpointer argp)
170 color_filter_t *colorf = filter_arg;
171 apply_color_filter_args *args = argp;
173 if (colorf->c_colorfilter != NULL && args->colorf == NULL) {
174 if (dfilter_apply_edt(colorf->c_colorfilter, args->edt))
175 args->colorf = colorf;
181 color_filters_colorize_packet(gint row, epan_dissect_t *edt)
183 apply_color_filter_args args;
186 /* We don't yet have a color filter to apply. */
189 /* If we have color filters, "search" for the matching one. */
190 if (color_filters_used()) {
192 g_slist_foreach(color_filter_list, apply_color_filter, &args);
194 /* If the packet matches a color filter, apply the colors. */
195 if (args.colorf != NULL) {
196 packet_list_set_colors(row, &(args.colorf->fg_color), &(args.colorf->bg_color));
203 /* Prime the epan_dissect_t with all the compiler
204 * color filters in 'color_filter_list'. */
206 color_filters_prime_edt(epan_dissect_t *edt)
208 g_slist_foreach(color_filter_list, prime_edt, edt);
211 /* read filters from the given file */
213 /* XXX - Would it make more sense to use GStrings here instead of reallocing
216 read_filters_file(FILE *f, gpointer arg)
218 #define INIT_BUF_SIZE 128
220 gchar *filter_exp = NULL;
221 guint32 name_len = INIT_BUF_SIZE;
222 guint32 filter_exp_len = INIT_BUF_SIZE;
225 guint16 fg_r, fg_g, fg_b, bg_r, bg_g, bg_b;
226 gboolean skip_end_of_line = FALSE;
228 name = g_malloc(name_len + 1);
229 filter_exp = g_malloc(filter_exp_len + 1);
233 if (skip_end_of_line) {
236 } while (c != EOF && c != '\n');
239 skip_end_of_line = FALSE;
242 while ((c = getc(f)) != EOF && isspace(c)) {
251 /* skip # comments and invalid lines */
253 skip_end_of_line = TRUE;
257 /* we get the @ delimiter.
259 * @name@filter expression@[background r,g,b][foreground r,g,b]
266 if (c == EOF || c == '@')
269 /* buffer isn't long enough; double its length.*/
271 name = g_realloc(name, name_len + 1);
280 skip_end_of_line = TRUE;
284 /* retrieve filter expression */
288 if (c == EOF || c == '@')
290 if (i >= filter_exp_len) {
291 /* buffer isn't long enough; double its length.*/
293 filter_exp = g_realloc(filter_exp, filter_exp_len + 1);
297 filter_exp[i] = '\0';
302 skip_end_of_line = TRUE;
306 /* retrieve background and foreground colors */
307 if (fscanf(f,"[%hu,%hu,%hu][%hu,%hu,%hu]",
308 &bg_r, &bg_g, &bg_b, &fg_r, &fg_g, &fg_b) == 6) {
310 /* we got a complete color filter */
312 color_t bg_color, fg_color;
313 color_filter_t *colorf;
314 dfilter_t *temp_dfilter;
316 if (!dfilter_compile(filter_exp, &temp_dfilter)) {
317 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
318 "Could not compile color filter %s from saved filters.\n%s",
319 name, dfilter_error_msg);
320 skip_end_of_line = TRUE;
324 if (!initialize_color(&fg_color, fg_r, fg_g, fg_b)) {
326 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
327 "Could not allocate foreground color "
328 "specified in input file for %s.", name);
329 dfilter_free(temp_dfilter);
330 skip_end_of_line = TRUE;
333 if (!initialize_color(&bg_color, bg_r, bg_g, bg_b)) {
335 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
336 "Could not allocate background color "
337 "specified in input file for %s.", name);
338 dfilter_free(temp_dfilter);
339 skip_end_of_line = TRUE;
343 colorf = color_filter_new(name, filter_exp, &bg_color,
345 colorf->c_colorfilter = temp_dfilter;
348 color_filter_add_cb (colorf, arg);
351 skip_end_of_line = TRUE;
359 /* read filters from the user's filter file */
367 /* decide what file to open (from dfilter code) */
368 path = get_persconffile_path("colorfilters", FALSE);
369 if ((f = eth_fopen(path, "r")) == NULL) {
370 if (errno != ENOENT) {
371 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
372 "Could not open filter file\n\"%s\": %s.", path,
381 ret = read_filters_file(f, NULL);
386 /* read filters from the filter file */
388 read_global_filters(void)
394 /* decide what file to open (from dfilter code) */
395 path = get_datafile_path("colorfilters");
396 if ((f = eth_fopen(path, "r")) == NULL) {
397 if (errno != ENOENT) {
398 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
399 "Could not open global filter file\n\"%s\": %s.", path,
408 ret = read_filters_file(f, NULL);
413 /* read filters from some other filter file (import) */
415 color_filters_import(gchar *path, gpointer arg)
420 if ((f = eth_fopen(path, "r")) == NULL) {
421 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
422 "Could not open\n%s\nfor reading: %s.",
423 path, strerror(errno));
427 ret = read_filters_file(f, arg);
432 struct write_filter_data
435 gboolean only_marked;
438 /* save a single filter */
440 write_filter(gpointer filter_arg, gpointer data_arg)
442 struct write_filter_data *data = data_arg;
443 color_filter_t *colorf = filter_arg;
446 if (colorf->marked || !data->only_marked) {
447 fprintf(f,"@%s@%s@[%d,%d,%d][%d,%d,%d]\n",
450 colorf->bg_color.red,
451 colorf->bg_color.green,
452 colorf->bg_color.blue,
453 colorf->fg_color.red,
454 colorf->fg_color.green,
455 colorf->fg_color.blue);
459 /* save filters in a filter file */
461 write_filters_file(FILE *f, gboolean only_marked)
463 struct write_filter_data data;
466 data.only_marked = only_marked;
468 fprintf(f,"# DO NOT EDIT THIS FILE! It was created by Ethereal\n");
469 g_slist_foreach(color_filter_list, write_filter, &data);
473 /* save filters in users filter file */
475 color_filters_write(void)
481 /* Create the directory that holds personal configuration files,
483 if (create_persconffile_dir(&pf_dir_path) == -1) {
484 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
485 "Can't create directory\n\"%s\"\nfor color files: %s.",
486 pf_dir_path, strerror(errno));
491 path = get_persconffile_path("colorfilters", TRUE);
492 if ((f = eth_fopen(path, "w+")) == NULL) {
493 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
494 "Could not open\n%s\nfor writing: %s.",
495 path, strerror(errno));
498 write_filters_file(f, FALSE);
503 /* delete users filter file and reload global filters */
505 color_filters_revert(void)
509 path = get_persconffile_path("colorfilters", TRUE);
510 if (!deletefile(path)) {
517 /* Reload the (global) filters - Note: this does not update the dialog. */
518 color_filters_init();
523 /* save filters in some other filter file (export) */
525 color_filters_export(gchar *path, gboolean only_marked)
529 if ((f = eth_fopen(path, "w+")) == NULL) {
530 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
531 "Could not open\n%s\nfor writing: %s.",
532 path, strerror(errno));
535 write_filters_file(f, only_marked);