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
35 #include <epan/filesystem.h>
37 #include <epan/packet.h>
39 #include "color_filters.h"
41 #include <epan/dfilter/dfilter.h>
42 #include "simple_dialog.h"
44 static gboolean read_filters(void);
45 static gboolean read_global_filters(void);
47 /* Variables and routines defined in color.h */
49 GSList *filter_list = NULL;
50 GSList *removed_filter_list = NULL;
52 /* Remove the specified filter from the list of existing color filters,
53 * and add it to the list of removed color filters.
54 * This way, unmarking and marking a packet which matches a now removed
55 * color filter will still be colored correctly as the color filter is
57 void remove_color_filter(color_filter_t *colorf)
59 /* Remove colorf from the list of color filters */
60 filter_list = g_slist_remove(filter_list, colorf);
61 /* Add colorf to the list of removed color filters */
62 removed_filter_list = g_slist_prepend(removed_filter_list, colorf);
65 /* delete the specified filter */
67 delete_color_filter(color_filter_t *colorf)
69 if (colorf->filter_name != NULL)
70 g_free(colorf->filter_name);
71 if (colorf->filter_text != NULL)
72 g_free(colorf->filter_text);
73 if (colorf->c_colorfilter != NULL)
74 dfilter_free(colorf->c_colorfilter);
78 /* delete the specified filter as an iterator*/
80 delete_color_filter_it(gpointer filter_arg, gpointer unused _U_)
82 color_filter_t *colorf = filter_arg;
83 delete_color_filter(colorf);
86 /* delete all the filters */
88 delete_all_color_filters (void)
90 g_slist_foreach(filter_list, delete_color_filter_it, NULL);
91 g_slist_free(filter_list);
93 g_slist_foreach(removed_filter_list, delete_color_filter_it, NULL);
94 g_slist_free(removed_filter_list);
95 removed_filter_list = NULL;
98 /* Initialize the filter structures (reading from file) for general running, including app startup */
102 delete_all_color_filters();
104 read_global_filters();
107 /* Create a new filter */
109 new_color_filter(gchar *name, /* The name of the filter to create */
110 gchar *filter_string, /* The string representing the filter */
111 color_t *bg_color, /* The background color */
112 color_t *fg_color) /* The foreground color */
114 color_filter_t *colorf;
116 colorf = g_malloc(sizeof (color_filter_t));
117 colorf->filter_name = g_strdup(name);
118 colorf->filter_text = g_strdup(filter_string);
119 colorf->bg_color = *bg_color;
120 colorf->fg_color = *fg_color;
121 colorf->c_colorfilter = NULL;
122 colorf->edit_dialog = NULL;
123 colorf->marked = FALSE;
124 filter_list = g_slist_append(filter_list, colorf);
129 prime_edt(gpointer data, gpointer user_data)
131 color_filter_t *colorf = data;
132 epan_dissect_t *edt = user_data;
134 if (colorf->c_colorfilter != NULL)
135 epan_dissect_prime_dfilter(edt, colorf->c_colorfilter);
138 /* Prime the epan_dissect_t with all the compiler
139 * color filters in 'filter_list'. */
141 filter_list_prime_edt(epan_dissect_t *edt)
143 g_slist_foreach(filter_list, prime_edt, edt);
146 /* read filters from the given file */
148 /* XXX - Would it make more sense to use GStrings here instead of reallocing
151 read_filters_file(FILE *f, gpointer arg)
153 #define INIT_BUF_SIZE 128
155 gchar *filter_exp = NULL;
156 guint32 name_len = INIT_BUF_SIZE;
157 guint32 filter_exp_len = INIT_BUF_SIZE;
160 guint16 fg_r, fg_g, fg_b, bg_r, bg_g, bg_b;
161 gboolean skip_end_of_line = FALSE;
163 name = g_malloc(name_len + 1);
164 filter_exp = g_malloc(filter_exp_len + 1);
168 if (skip_end_of_line) {
171 } while (c != EOF && c != '\n');
174 skip_end_of_line = FALSE;
177 while ((c = getc(f)) != EOF && isspace(c)) {
186 /* skip # comments and invalid lines */
188 skip_end_of_line = TRUE;
192 /* we get the @ delimiter.
194 * @name@filter expression@[background r,g,b][foreground r,g,b]
201 if (c == EOF || c == '@')
204 /* buffer isn't long enough; double its length.*/
206 name = g_realloc(name, name_len + 1);
215 skip_end_of_line = TRUE;
219 /* retrieve filter expression */
223 if (c == EOF || c == '@')
225 if (i >= filter_exp_len) {
226 /* buffer isn't long enough; double its length.*/
228 filter_exp = g_realloc(filter_exp, filter_exp_len + 1);
232 filter_exp[i] = '\0';
237 skip_end_of_line = TRUE;
241 /* retrieve background and foreground colors */
242 if (fscanf(f,"[%hu,%hu,%hu][%hu,%hu,%hu]",
243 &bg_r, &bg_g, &bg_b, &fg_r, &fg_g, &fg_b) == 6) {
245 /* we got a complete color filter */
247 color_t bg_color, fg_color;
248 color_filter_t *colorf;
249 dfilter_t *temp_dfilter;
251 if (!dfilter_compile(filter_exp, &temp_dfilter)) {
252 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
253 "Could not compile color filter %s from saved filters.\n%s",
254 name, dfilter_error_msg);
255 skip_end_of_line = TRUE;
259 if (!initialize_color(&fg_color, fg_r, fg_g, fg_b)) {
261 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
262 "Could not allocate foreground color "
263 "specified in input file for %s.", name);
264 dfilter_free(temp_dfilter);
265 skip_end_of_line = TRUE;
268 if (!initialize_color(&bg_color, bg_r, bg_g, bg_b)) {
270 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
271 "Could not allocate background color "
272 "specified in input file for %s.", name);
273 dfilter_free(temp_dfilter);
274 skip_end_of_line = TRUE;
278 colorf = new_color_filter(name, filter_exp, &bg_color,
280 colorf->c_colorfilter = temp_dfilter;
283 color_add_filter_cb (colorf, arg);
286 skip_end_of_line = TRUE;
294 /* read filters from the user's filter file */
302 /* decide what file to open (from dfilter code) */
303 path = get_persconffile_path("colorfilters", FALSE);
304 if ((f = fopen(path, "r")) == NULL) {
305 if (errno != ENOENT) {
306 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
307 "Could not open filter file\n\"%s\": %s.", path,
316 ret = read_filters_file(f, NULL);
321 /* read filters from the filter file */
323 read_global_filters(void)
329 /* decide what file to open (from dfilter code) */
330 path = get_datafile_path("colorfilters");
331 if ((f = fopen(path, "r")) == NULL) {
332 if (errno != ENOENT) {
333 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
334 "Could not open global filter file\n\"%s\": %s.", path,
343 ret = read_filters_file(f, NULL);
348 /* save filters in some other filter file */
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;
375 write_filter(gpointer filter_arg, gpointer data_arg)
377 struct write_filter_data *data = data_arg;
378 color_filter_t *colorf = filter_arg;
381 if (colorf->marked || !data->only_marked) {
382 fprintf(f,"@%s@%s@[%d,%d,%d][%d,%d,%d]\n",
385 colorf->bg_color.red,
386 colorf->bg_color.green,
387 colorf->bg_color.blue,
388 colorf->fg_color.red,
389 colorf->fg_color.green,
390 colorf->fg_color.blue);
394 /* save filters in a filter file */
396 write_filters_file(FILE *f, gboolean only_marked)
398 struct write_filter_data data;
401 data.only_marked = only_marked;
403 fprintf(f,"# DO NOT EDIT THIS FILE! It was created by Ethereal\n");
404 g_slist_foreach(filter_list, write_filter, &data);
408 /* 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*/
447 /* Create the directory that holds personal configuration files,
449 if (create_persconffile_dir(&pf_dir_path) == -1) {
450 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
451 "Can't create directory\n\"%s\"\nfor color files: %s.",
452 pf_dir_path, strerror(errno));
457 path = get_persconffile_path("colorfilters", TRUE);
458 if (!deletefile(path)) {
459 g_free((gchar *)path);
463 g_free((gchar *)path);
465 /* Reload the (global) filters - Note: this does not update the dialog. */
471 /* save filters in some other filter file */
474 write_other_filters(gchar *path, gboolean only_marked)
478 if ((f = fopen(path, "w+")) == NULL) {
479 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
480 "Could not open\n%s\nfor writing: %s.",
481 path, strerror(errno));
484 write_filters_file(f, only_marked);