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
28 /* With MSVC and a libethereal.dll this file needs to import some variables
29 in a special way. Therefore _NEED_VAR_IMPORT_ is defined. */
30 #define _NEED_VAR_IMPORT_
40 #include <epan/filesystem.h>
42 #include <epan/packet.h>
44 #include "color_filters.h"
46 #include <epan/dfilter/dfilter.h>
47 #include "simple_dialog.h"
49 static gboolean read_filters(void);
50 static gboolean read_global_filters(void);
52 /* Variables and routines defined in color.h */
54 GSList *filter_list = NULL;
55 GSList *removed_filter_list = NULL;
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 remove_color_filter(color_filter_t *colorf)
64 /* Remove colorf from the list of color filters */
65 filter_list = g_slist_remove(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(filter_list, delete_color_filter_it, NULL);
96 g_slist_free(filter_list);
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 */
107 delete_all_color_filters();
109 read_global_filters();
112 /* Create a new filter */
114 new_color_filter(gchar *name, /* The name of the filter to create */
115 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 filter_list = g_slist_append(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);
143 /* Prime the epan_dissect_t with all the compiler
144 * color filters in 'filter_list'. */
146 filter_list_prime_edt(epan_dissect_t *edt)
148 g_slist_foreach(filter_list, prime_edt, edt);
151 /* read filters from the given file */
153 /* XXX - Would it make more sense to use GStrings here instead of reallocing
156 read_filters_file(FILE *f, gpointer arg)
158 #define INIT_BUF_SIZE 128
160 gchar *filter_exp = NULL;
161 guint32 name_len = INIT_BUF_SIZE;
162 guint32 filter_exp_len = INIT_BUF_SIZE;
165 guint16 fg_r, fg_g, fg_b, bg_r, bg_g, bg_b;
166 gboolean skip_end_of_line = FALSE;
168 name = g_malloc(name_len + 1);
169 filter_exp = g_malloc(filter_exp_len + 1);
173 if (skip_end_of_line) {
176 } while (c != EOF && c != '\n');
179 skip_end_of_line = FALSE;
182 while ((c = getc(f)) != EOF && isspace(c)) {
191 /* skip # comments and invalid lines */
193 skip_end_of_line = TRUE;
197 /* we get the @ delimiter.
199 * @name@filter expression@[background r,g,b][foreground r,g,b]
206 if (c == EOF || c == '@')
209 /* buffer isn't long enough; double its length.*/
211 name = g_realloc(name, name_len + 1);
220 skip_end_of_line = TRUE;
224 /* retrieve filter expression */
228 if (c == EOF || c == '@')
230 if (i >= filter_exp_len) {
231 /* buffer isn't long enough; double its length.*/
233 filter_exp = g_realloc(filter_exp, filter_exp_len + 1);
237 filter_exp[i] = '\0';
242 skip_end_of_line = TRUE;
246 /* retrieve background and foreground colors */
247 if (fscanf(f,"[%hu,%hu,%hu][%hu,%hu,%hu]",
248 &bg_r, &bg_g, &bg_b, &fg_r, &fg_g, &fg_b) == 6) {
250 /* we got a complete color filter */
252 color_t bg_color, fg_color;
253 color_filter_t *colorf;
254 dfilter_t *temp_dfilter;
256 if (!dfilter_compile(filter_exp, &temp_dfilter)) {
257 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
258 "Could not compile color filter %s from saved filters.\n%s",
259 name, dfilter_error_msg);
260 skip_end_of_line = TRUE;
264 if (!initialize_color(&fg_color, fg_r, fg_g, fg_b)) {
266 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
267 "Could not allocate foreground color "
268 "specified in input file for %s.", name);
269 dfilter_free(temp_dfilter);
270 skip_end_of_line = TRUE;
273 if (!initialize_color(&bg_color, bg_r, bg_g, bg_b)) {
275 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
276 "Could not allocate background color "
277 "specified in input file for %s.", name);
278 dfilter_free(temp_dfilter);
279 skip_end_of_line = TRUE;
283 colorf = new_color_filter(name, filter_exp, &bg_color,
285 colorf->c_colorfilter = temp_dfilter;
288 color_add_filter_cb (colorf, arg);
291 skip_end_of_line = TRUE;
299 /* read filters from the user's filter file */
307 /* decide what file to open (from dfilter code) */
308 path = get_persconffile_path("colorfilters", FALSE);
309 if ((f = fopen(path, "r")) == NULL) {
310 if (errno != ENOENT) {
311 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
312 "Could not open filter file\n\"%s\": %s.", path,
321 ret = read_filters_file(f, NULL);
326 /* read filters from the filter file */
328 read_global_filters(void)
334 /* decide what file to open (from dfilter code) */
335 path = get_datafile_path("colorfilters");
336 if ((f = fopen(path, "r")) == NULL) {
337 if (errno != ENOENT) {
338 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
339 "Could not open global filter file\n\"%s\": %s.", path,
348 ret = read_filters_file(f, NULL);
353 /* read filters from some other filter file (import) */
355 read_other_filters(gchar *path, gpointer arg)
360 if ((f = fopen(path, "r")) == NULL) {
361 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
362 "Could not open\n%s\nfor reading: %s.",
363 path, strerror(errno));
367 ret = read_filters_file(f, arg);
372 struct write_filter_data
375 gboolean only_marked;
378 /* save a single filter */
380 write_filter(gpointer filter_arg, gpointer data_arg)
382 struct write_filter_data *data = data_arg;
383 color_filter_t *colorf = filter_arg;
386 if (colorf->marked || !data->only_marked) {
387 fprintf(f,"@%s@%s@[%d,%d,%d][%d,%d,%d]\n",
390 colorf->bg_color.red,
391 colorf->bg_color.green,
392 colorf->bg_color.blue,
393 colorf->fg_color.red,
394 colorf->fg_color.green,
395 colorf->fg_color.blue);
399 /* save filters in a filter file */
401 write_filters_file(FILE *f, gboolean only_marked)
403 struct write_filter_data data;
406 data.only_marked = only_marked;
408 fprintf(f,"# DO NOT EDIT THIS FILE! It was created by Ethereal\n");
409 g_slist_foreach(filter_list, write_filter, &data);
413 /* save filters in users filter file */
421 /* Create the directory that holds personal configuration files,
423 if (create_persconffile_dir(&pf_dir_path) == -1) {
424 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
425 "Can't create directory\n\"%s\"\nfor color files: %s.",
426 pf_dir_path, strerror(errno));
431 path = get_persconffile_path("colorfilters", TRUE);
432 if ((f = fopen(path, "w+")) == NULL) {
433 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
434 "Could not open\n%s\nfor writing: %s.",
435 path, strerror(errno));
438 write_filters_file(f, FALSE);
443 /* delete users filter file and reload global filters */
450 /* Create the directory that holds personal configuration files,
452 if (create_persconffile_dir(&pf_dir_path) == -1) {
453 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
454 "Can't create directory\n\"%s\"\nfor color files: %s.",
455 pf_dir_path, strerror(errno));
460 path = get_persconffile_path("colorfilters", TRUE);
461 if (!deletefile(path)) {
462 g_free((gchar *)path);
466 g_free((gchar *)path);
468 /* Reload the (global) filters - Note: this does not update the dialog. */
474 /* save filters in some other filter file (export) */
476 write_other_filters(gchar *path, gboolean only_marked)
480 if ((f = fopen(path, "w+")) == NULL) {
481 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
482 "Could not open\n%s\nfor writing: %s.",
483 path, strerror(errno));
486 write_filters_file(f, only_marked);