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"
45 static gboolean read_filters(void);
46 static gboolean read_global_filters(void);
48 /* Variables and routines defined in color.h */
50 GSList *filter_list = NULL;
51 GSList *removed_filter_list = NULL;
53 /* Remove the specified filter from the list of existing color filters,
54 * and add it to the list of removed color filters.
55 * This way, unmarking and marking a packet which matches a now removed
56 * color filter will still be colored correctly as the color filter is
58 void remove_color_filter(color_filter_t *colorf)
60 /* Remove colorf from the list of color filters */
61 filter_list = g_slist_remove(filter_list, colorf);
62 /* Add colorf to the list of removed color filters */
63 removed_filter_list = g_slist_prepend(removed_filter_list, colorf);
66 /* delete the specified filter */
68 delete_color_filter(color_filter_t *colorf)
70 if (colorf->filter_name != NULL)
71 g_free(colorf->filter_name);
72 if (colorf->filter_text != NULL)
73 g_free(colorf->filter_text);
74 if (colorf->c_colorfilter != NULL)
75 dfilter_free(colorf->c_colorfilter);
79 /* delete the specified filter as an iterator */
81 delete_color_filter_it(gpointer filter_arg, gpointer unused _U_)
83 color_filter_t *colorf = filter_arg;
84 delete_color_filter(colorf);
87 /* delete all the filters */
89 delete_all_color_filters (void)
91 g_slist_foreach(filter_list, delete_color_filter_it, NULL);
92 g_slist_free(filter_list);
94 g_slist_foreach(removed_filter_list, delete_color_filter_it, NULL);
95 g_slist_free(removed_filter_list);
96 removed_filter_list = NULL;
99 /* Initialize the filter structures (reading from file) for general running, including app startup */
103 delete_all_color_filters();
105 read_global_filters();
108 /* Create a new filter */
110 new_color_filter(gchar *name, /* The name of the filter to create */
111 gchar *filter_string, /* The string representing the filter */
112 color_t *bg_color, /* The background color */
113 color_t *fg_color) /* The foreground color */
115 color_filter_t *colorf;
117 colorf = g_malloc(sizeof (color_filter_t));
118 colorf->filter_name = g_strdup(name);
119 colorf->filter_text = g_strdup(filter_string);
120 colorf->bg_color = *bg_color;
121 colorf->fg_color = *fg_color;
122 colorf->c_colorfilter = NULL;
123 colorf->edit_dialog = NULL;
124 colorf->marked = FALSE;
125 filter_list = g_slist_append(filter_list, colorf);
130 prime_edt(gpointer data, gpointer user_data)
132 color_filter_t *colorf = data;
133 epan_dissect_t *edt = user_data;
135 if (colorf->c_colorfilter != NULL)
136 epan_dissect_prime_dfilter(edt, colorf->c_colorfilter);
139 /* Prime the epan_dissect_t with all the compiler
140 * color filters in 'filter_list'. */
142 filter_list_prime_edt(epan_dissect_t *edt)
144 g_slist_foreach(filter_list, prime_edt, edt);
147 /* read filters from the given file */
149 /* XXX - Would it make more sense to use GStrings here instead of reallocing
152 read_filters_file(FILE *f, gpointer arg)
154 #define INIT_BUF_SIZE 128
156 gchar *filter_exp = NULL;
157 guint32 name_len = INIT_BUF_SIZE;
158 guint32 filter_exp_len = INIT_BUF_SIZE;
161 guint16 fg_r, fg_g, fg_b, bg_r, bg_g, bg_b;
162 gboolean skip_end_of_line = FALSE;
164 name = g_malloc(name_len + 1);
165 filter_exp = g_malloc(filter_exp_len + 1);
169 if (skip_end_of_line) {
172 } while (c != EOF && c != '\n');
175 skip_end_of_line = FALSE;
178 while ((c = getc(f)) != EOF && isspace(c)) {
187 /* skip # comments and invalid lines */
189 skip_end_of_line = TRUE;
193 /* we get the @ delimiter.
195 * @name@filter expression@[background r,g,b][foreground r,g,b]
202 if (c == EOF || c == '@')
205 /* buffer isn't long enough; double its length.*/
207 name = g_realloc(name, name_len + 1);
216 skip_end_of_line = TRUE;
220 /* retrieve filter expression */
224 if (c == EOF || c == '@')
226 if (i >= filter_exp_len) {
227 /* buffer isn't long enough; double its length.*/
229 filter_exp = g_realloc(filter_exp, filter_exp_len + 1);
233 filter_exp[i] = '\0';
238 skip_end_of_line = TRUE;
242 /* retrieve background and foreground colors */
243 if (fscanf(f,"[%hu,%hu,%hu][%hu,%hu,%hu]",
244 &bg_r, &bg_g, &bg_b, &fg_r, &fg_g, &fg_b) == 6) {
246 /* we got a complete color filter */
248 color_t bg_color, fg_color;
249 color_filter_t *colorf;
250 dfilter_t *temp_dfilter;
252 if (!dfilter_compile(filter_exp, &temp_dfilter)) {
253 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
254 "Could not compile color filter %s from saved filters.\n%s",
255 name, dfilter_error_msg);
256 skip_end_of_line = TRUE;
260 if (!initialize_color(&fg_color, fg_r, fg_g, fg_b)) {
262 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
263 "Could not allocate foreground color "
264 "specified in input file for %s.", name);
265 dfilter_free(temp_dfilter);
266 skip_end_of_line = TRUE;
269 if (!initialize_color(&bg_color, bg_r, bg_g, bg_b)) {
271 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
272 "Could not allocate background color "
273 "specified in input file for %s.", name);
274 dfilter_free(temp_dfilter);
275 skip_end_of_line = TRUE;
279 colorf = new_color_filter(name, filter_exp, &bg_color,
281 colorf->c_colorfilter = temp_dfilter;
284 color_add_filter_cb (colorf, arg);
287 skip_end_of_line = TRUE;
295 /* read filters from the user's filter file */
303 /* decide what file to open (from dfilter code) */
304 path = get_persconffile_path("colorfilters", FALSE);
305 if ((f = fopen(path, "r")) == NULL) {
306 if (errno != ENOENT) {
307 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
308 "Could not open filter file\n\"%s\": %s.", path,
317 ret = read_filters_file(f, NULL);
322 /* read filters from the filter file */
324 read_global_filters(void)
330 /* decide what file to open (from dfilter code) */
331 path = get_datafile_path("colorfilters");
332 if ((f = fopen(path, "r")) == NULL) {
333 if (errno != ENOENT) {
334 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
335 "Could not open global filter file\n\"%s\": %s.", path,
344 ret = read_filters_file(f, NULL);
349 /* read filters from some other filter file (import) */
351 read_other_filters(gchar *path, gpointer arg)
356 if ((f = fopen(path, "r")) == NULL) {
357 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
358 "Could not open\n%s\nfor reading: %s.",
359 path, strerror(errno));
363 ret = read_filters_file(f, arg);
368 struct write_filter_data
371 gboolean only_marked;
374 /* save a single filter */
376 write_filter(gpointer filter_arg, gpointer data_arg)
378 struct write_filter_data *data = data_arg;
379 color_filter_t *colorf = filter_arg;
382 if (colorf->marked || !data->only_marked) {
383 fprintf(f,"@%s@%s@[%d,%d,%d][%d,%d,%d]\n",
386 colorf->bg_color.red,
387 colorf->bg_color.green,
388 colorf->bg_color.blue,
389 colorf->fg_color.red,
390 colorf->fg_color.green,
391 colorf->fg_color.blue);
395 /* save filters in a filter file */
397 write_filters_file(FILE *f, gboolean only_marked)
399 struct write_filter_data data;
402 data.only_marked = only_marked;
404 fprintf(f,"# DO NOT EDIT THIS FILE! It was created by Ethereal\n");
405 g_slist_foreach(filter_list, write_filter, &data);
409 /* save filters in users filter file */
417 /* Create the directory that holds personal configuration files,
419 if (create_persconffile_dir(&pf_dir_path) == -1) {
420 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
421 "Can't create directory\n\"%s\"\nfor color files: %s.",
422 pf_dir_path, strerror(errno));
427 path = get_persconffile_path("colorfilters", TRUE);
428 if ((f = fopen(path, "w+")) == NULL) {
429 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
430 "Could not open\n%s\nfor writing: %s.",
431 path, strerror(errno));
434 write_filters_file(f, FALSE);
439 /* delete users filter file and reload global filters */
446 /* Create the directory that holds personal configuration files,
448 if (create_persconffile_dir(&pf_dir_path) == -1) {
449 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
450 "Can't create directory\n\"%s\"\nfor color files: %s.",
451 pf_dir_path, strerror(errno));
456 path = get_persconffile_path("colorfilters", TRUE);
457 if (!deletefile(path)) {
464 /* Reload the (global) filters - Note: this does not update the dialog. */
470 /* save filters in some other filter file (export) */
472 write_other_filters(gchar *path, gboolean only_marked)
476 if ((f = fopen(path, "w+")) == NULL) {
477 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
478 "Could not open\n%s\nfor writing: %s.",
479 path, strerror(errno));
482 write_filters_file(f, only_marked);