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>
38 #include <epan/packet.h>
40 #include "color_filters.h"
42 #include <epan/dfilter/dfilter.h>
43 #include "simple_dialog.h"
46 static gboolean read_filters(void);
47 static gboolean read_global_filters(void);
49 /* Variables and routines defined in color.h */
51 GSList *color_filter_list = NULL;
52 GSList *removed_filter_list = NULL;
54 /* Color Filters can en-/disabled. */
55 gboolean filters_enabled = TRUE;
57 /* Remove the specified filter from the list of existing color filters,
58 * and add it to the list of removed color filters.
59 * This way, unmarking and marking a packet which matches a now removed
60 * color filter will still be colored correctly as the color filter is
62 void color_filter_remove(color_filter_t *colorf)
64 /* Remove colorf from the list of color filters */
65 color_filter_list = g_slist_remove(color_filter_list, colorf);
66 /* Add colorf to the list of removed color filters */
67 removed_filter_list = g_slist_prepend(removed_filter_list, colorf);
70 /* delete the specified filter */
72 delete_color_filter(color_filter_t *colorf)
74 if (colorf->filter_name != NULL)
75 g_free(colorf->filter_name);
76 if (colorf->filter_text != NULL)
77 g_free(colorf->filter_text);
78 if (colorf->c_colorfilter != NULL)
79 dfilter_free(colorf->c_colorfilter);
83 /* delete the specified filter as an iterator */
85 delete_color_filter_it(gpointer filter_arg, gpointer unused _U_)
87 color_filter_t *colorf = filter_arg;
88 delete_color_filter(colorf);
91 /* delete all the filters */
93 delete_all_color_filters (void)
95 g_slist_foreach(color_filter_list, delete_color_filter_it, NULL);
96 g_slist_free(color_filter_list);
97 color_filter_list = NULL;
98 g_slist_foreach(removed_filter_list, delete_color_filter_it, NULL);
99 g_slist_free(removed_filter_list);
100 removed_filter_list = NULL;
103 /* Initialize the filter structures (reading from file) for general running, including app startup */
105 color_filters_init(void)
107 delete_all_color_filters();
109 read_global_filters();
112 /* Create a new filter */
114 color_filter_new(const gchar *name, /* The name of the filter to create */
115 const gchar *filter_string, /* The string representing the filter */
116 color_t *bg_color, /* The background color */
117 color_t *fg_color) /* The foreground color */
119 color_filter_t *colorf;
121 colorf = g_malloc(sizeof (color_filter_t));
122 colorf->filter_name = g_strdup(name);
123 colorf->filter_text = g_strdup(filter_string);
124 colorf->bg_color = *bg_color;
125 colorf->fg_color = *fg_color;
126 colorf->c_colorfilter = NULL;
127 colorf->edit_dialog = NULL;
128 colorf->marked = FALSE;
129 color_filter_list = g_slist_append(color_filter_list, colorf);
134 prime_edt(gpointer data, gpointer user_data)
136 color_filter_t *colorf = data;
137 epan_dissect_t *edt = user_data;
139 if (colorf->c_colorfilter != NULL)
140 epan_dissect_prime_dfilter(edt, colorf->c_colorfilter);
144 color_filters_used(void)
146 return color_filter_list != NULL && filters_enabled;
150 color_filters_enable(gboolean enable)
152 filters_enabled = enable;
157 color_filter_t *colorf;
159 } apply_color_filter_args;
162 * If no color filter has been applied, apply this one.
163 * (The "if no color filter has been applied" is to handle the case where
164 * more than one color filter matches the packet.)
167 apply_color_filter(gpointer filter_arg, gpointer argp)
169 color_filter_t *colorf = filter_arg;
170 apply_color_filter_args *args = argp;
172 if (colorf->c_colorfilter != NULL && args->colorf == NULL) {
173 if (dfilter_apply_edt(colorf->c_colorfilter, args->edt))
174 args->colorf = colorf;
180 color_filters_colorize_packet(gint row, epan_dissect_t *edt)
182 apply_color_filter_args args;
185 /* We don't yet have a color filter to apply. */
188 /* If we have color filters, "search" for the matching one. */
189 if (color_filters_used()) {
191 g_slist_foreach(color_filter_list, apply_color_filter, &args);
193 /* If the packet matches a color filter, apply the colors. */
194 if (args.colorf != NULL) {
195 packet_list_set_colors(row, &(args.colorf->fg_color), &(args.colorf->bg_color));
202 /* Prime the epan_dissect_t with all the compiler
203 * color filters in 'color_filter_list'. */
205 color_filters_prime_edt(epan_dissect_t *edt)
207 g_slist_foreach(color_filter_list, prime_edt, edt);
210 /* read filters from the given file */
212 /* XXX - Would it make more sense to use GStrings here instead of reallocing
215 read_filters_file(FILE *f, gpointer arg)
217 #define INIT_BUF_SIZE 128
219 gchar *filter_exp = NULL;
220 guint32 name_len = INIT_BUF_SIZE;
221 guint32 filter_exp_len = INIT_BUF_SIZE;
224 guint16 fg_r, fg_g, fg_b, bg_r, bg_g, bg_b;
225 gboolean skip_end_of_line = FALSE;
227 name = g_malloc(name_len + 1);
228 filter_exp = g_malloc(filter_exp_len + 1);
232 if (skip_end_of_line) {
235 } while (c != EOF && c != '\n');
238 skip_end_of_line = FALSE;
241 while ((c = getc(f)) != EOF && isspace(c)) {
250 /* skip # comments and invalid lines */
252 skip_end_of_line = TRUE;
256 /* we get the @ delimiter.
258 * @name@filter expression@[background r,g,b][foreground r,g,b]
265 if (c == EOF || c == '@')
268 /* buffer isn't long enough; double its length.*/
270 name = g_realloc(name, name_len + 1);
279 skip_end_of_line = TRUE;
283 /* retrieve filter expression */
287 if (c == EOF || c == '@')
289 if (i >= filter_exp_len) {
290 /* buffer isn't long enough; double its length.*/
292 filter_exp = g_realloc(filter_exp, filter_exp_len + 1);
296 filter_exp[i] = '\0';
301 skip_end_of_line = TRUE;
305 /* retrieve background and foreground colors */
306 if (fscanf(f,"[%hu,%hu,%hu][%hu,%hu,%hu]",
307 &bg_r, &bg_g, &bg_b, &fg_r, &fg_g, &fg_b) == 6) {
309 /* we got a complete color filter */
311 color_t bg_color, fg_color;
312 color_filter_t *colorf;
313 dfilter_t *temp_dfilter;
315 if (!dfilter_compile(filter_exp, &temp_dfilter)) {
316 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
317 "Could not compile color filter %s from saved filters.\n%s",
318 name, dfilter_error_msg);
319 skip_end_of_line = TRUE;
323 if (!initialize_color(&fg_color, fg_r, fg_g, fg_b)) {
325 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
326 "Could not allocate foreground color "
327 "specified in input file for %s.", name);
328 dfilter_free(temp_dfilter);
329 skip_end_of_line = TRUE;
332 if (!initialize_color(&bg_color, bg_r, bg_g, bg_b)) {
334 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
335 "Could not allocate background color "
336 "specified in input file for %s.", name);
337 dfilter_free(temp_dfilter);
338 skip_end_of_line = TRUE;
342 colorf = color_filter_new(name, filter_exp, &bg_color,
344 colorf->c_colorfilter = temp_dfilter;
347 color_filter_add_cb (colorf, arg);
350 skip_end_of_line = TRUE;
358 /* read filters from the user's filter file */
366 /* decide what file to open (from dfilter code) */
367 path = get_persconffile_path("colorfilters", FALSE);
368 if ((f = fopen(path, "r")) == NULL) {
369 if (errno != ENOENT) {
370 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
371 "Could not open filter file\n\"%s\": %s.", path,
380 ret = read_filters_file(f, NULL);
385 /* read filters from the filter file */
387 read_global_filters(void)
393 /* decide what file to open (from dfilter code) */
394 path = get_datafile_path("colorfilters");
395 if ((f = fopen(path, "r")) == NULL) {
396 if (errno != ENOENT) {
397 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
398 "Could not open global filter file\n\"%s\": %s.", path,
407 ret = read_filters_file(f, NULL);
412 /* read filters from some other filter file (import) */
414 color_filters_import(gchar *path, gpointer arg)
419 if ((f = fopen(path, "r")) == NULL) {
420 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
421 "Could not open\n%s\nfor reading: %s.",
422 path, strerror(errno));
426 ret = read_filters_file(f, arg);
431 struct write_filter_data
434 gboolean only_marked;
437 /* save a single filter */
439 write_filter(gpointer filter_arg, gpointer data_arg)
441 struct write_filter_data *data = data_arg;
442 color_filter_t *colorf = filter_arg;
445 if (colorf->marked || !data->only_marked) {
446 fprintf(f,"@%s@%s@[%d,%d,%d][%d,%d,%d]\n",
449 colorf->bg_color.red,
450 colorf->bg_color.green,
451 colorf->bg_color.blue,
452 colorf->fg_color.red,
453 colorf->fg_color.green,
454 colorf->fg_color.blue);
458 /* save filters in a filter file */
460 write_filters_file(FILE *f, gboolean only_marked)
462 struct write_filter_data data;
465 data.only_marked = only_marked;
467 fprintf(f,"# DO NOT EDIT THIS FILE! It was created by Ethereal\n");
468 g_slist_foreach(color_filter_list, write_filter, &data);
472 /* save filters in users filter file */
474 color_filters_write(void)
480 /* Create the directory that holds personal configuration files,
482 if (create_persconffile_dir(&pf_dir_path) == -1) {
483 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
484 "Can't create directory\n\"%s\"\nfor color files: %s.",
485 pf_dir_path, strerror(errno));
490 path = get_persconffile_path("colorfilters", TRUE);
491 if ((f = fopen(path, "w+")) == NULL) {
492 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
493 "Could not open\n%s\nfor writing: %s.",
494 path, strerror(errno));
497 write_filters_file(f, FALSE);
502 /* delete users filter file and reload global filters */
504 color_filters_revert(void)
508 path = get_persconffile_path("colorfilters", TRUE);
509 if (!deletefile(path)) {
516 /* Reload the (global) filters - Note: this does not update the dialog. */
517 color_filters_init();
522 /* save filters in some other filter file (export) */
524 color_filters_export(gchar *path, gboolean only_marked)
528 if ((f = fopen(path, "w+")) == NULL) {
529 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
530 "Could not open\n%s\nfor writing: %s.",
531 path, strerror(errno));
534 write_filters_file(f, only_marked);