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 GSList *filter_list = NULL;
48 GSList *removed_filter_list = NULL;
50 /* Remove the specified filter from the list of existing color filters,
51 * and add it to the list of removed color filters.
52 * This way, unmarking and marking a packet which matches a now removed
53 * color filter will still be colored correctly as the color filter is
55 void remove_color_filter(color_filter_t *colorf)
57 /* Remove colorf from the list of color filters */
58 filter_list = g_slist_remove(filter_list, colorf);
59 /* Add colorf to the list of removed color filters */
60 removed_filter_list = g_slist_prepend(removed_filter_list, colorf);
63 /* delete the specified filter */
65 delete_color_filter(color_filter_t *colorf)
67 if (colorf->filter_name != NULL)
68 g_free(colorf->filter_name);
69 if (colorf->filter_text != NULL)
70 g_free(colorf->filter_text);
71 if (colorf->c_colorfilter != NULL)
72 dfilter_free(colorf->c_colorfilter);
76 /* delete the specified filter as an iterator*/
78 delete_color_filter_it(gpointer filter_arg, gpointer unused _U_)
80 color_filter_t *colorf = filter_arg;
81 delete_color_filter(colorf);
84 /* delete all the filters */
86 delete_all_color_filters (void)
88 g_slist_foreach(filter_list, delete_color_filter_it, NULL);
89 g_slist_free(filter_list);
91 g_slist_foreach(removed_filter_list, delete_color_filter_it, NULL);
92 g_slist_free(removed_filter_list);
93 removed_filter_list = NULL;
96 /* Initialize the filter structures (reading from file) for general running, including app startup */
100 delete_all_color_filters();
102 read_global_filters();
105 /* Create a new filter */
107 new_color_filter(gchar *name, /* The name of the filter to create */
108 gchar *filter_string, /* The string representing the filter */
109 color_t *bg_color, /* The background color */
110 color_t *fg_color) /* The foreground color */
112 color_filter_t *colorf;
114 colorf = g_malloc(sizeof (color_filter_t));
115 colorf->filter_name = g_strdup(name);
116 colorf->filter_text = g_strdup(filter_string);
117 colorf->bg_color = *bg_color;
118 colorf->fg_color = *fg_color;
119 colorf->c_colorfilter = NULL;
120 colorf->edit_dialog = NULL;
121 colorf->marked = FALSE;
122 filter_list = g_slist_append(filter_list, colorf);
127 prime_edt(gpointer data, gpointer user_data)
129 color_filter_t *colorf = data;
130 epan_dissect_t *edt = user_data;
132 if (colorf->c_colorfilter != NULL)
133 epan_dissect_prime_dfilter(edt, colorf->c_colorfilter);
136 /* Prime the epan_dissect_t with all the compiler
137 * color filters in 'filter_list'. */
139 filter_list_prime_edt(epan_dissect_t *edt)
141 g_slist_foreach(filter_list, prime_edt, edt);
144 /* read filters from the given file */
146 read_filters_file(FILE *f, gpointer arg)
148 #define INIT_BUF_SIZE 128
150 gchar *filter_exp = NULL;
151 guint32 name_len = INIT_BUF_SIZE;
152 guint32 filter_exp_len = INIT_BUF_SIZE;
155 guint16 fg_r, fg_g, fg_b, bg_r, bg_g, bg_b;
156 gboolean skip_end_of_line = FALSE;
158 name = g_malloc(name_len + 1);
159 filter_exp = g_malloc(filter_exp_len + 1);
163 if (skip_end_of_line) {
166 } while (c != EOF && c != '\n');
169 skip_end_of_line = FALSE;
172 while ((c = getc(f)) != EOF && isspace(c)) {
181 /* skip # comments and invalid lines */
183 skip_end_of_line = TRUE;
187 /* we get the @ delimiter.
189 * @name@filter expression@[background r,g,b][foreground r,g,b]
196 if (c == EOF || c == '@')
199 /* buffer isn't long enough; double its length.*/
201 name = g_realloc(name, name_len + 1);
210 skip_end_of_line = TRUE;
214 /* retrieve filter expression */
218 if (c == EOF || c == '@')
220 if (i >= filter_exp_len) {
221 /* buffer isn't long enough; double its length.*/
223 filter_exp = g_realloc(filter_exp, filter_exp_len + 1);
227 filter_exp[i] = '\0';
232 skip_end_of_line = TRUE;
236 /* retrieve background and foreground colors */
237 if (fscanf(f,"[%hu,%hu,%hu][%hu,%hu,%hu]",
238 &bg_r, &bg_g, &bg_b, &fg_r, &fg_g, &fg_b) == 6) {
240 /* we got a complete color filter */
242 color_t bg_color, fg_color;
243 color_filter_t *colorf;
244 dfilter_t *temp_dfilter;
246 if (!dfilter_compile(filter_exp, &temp_dfilter)) {
247 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
248 "Could not compile color filter %s from saved filters.\n%s",
249 name, dfilter_error_msg);
250 skip_end_of_line = TRUE;
254 if (!create_color(&fg_color, fg_r, fg_g, fg_b)) {
256 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
257 "Could not allocate foreground color "
258 "specified in input file for %s.", name);
259 dfilter_free(temp_dfilter);
260 skip_end_of_line = TRUE;
263 if (!create_color(&bg_color, bg_r, bg_g, bg_b)) {
265 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
266 "Could not allocate background color "
267 "specified in input file for %s.", name);
268 dfilter_free(temp_dfilter);
269 skip_end_of_line = TRUE;
273 colorf = new_color_filter(name, filter_exp, &bg_color,
275 colorf->c_colorfilter = temp_dfilter;
278 color_add_filter_cb (colorf, arg);
281 skip_end_of_line = TRUE;
289 /* read filters from the user's filter file */
297 /* decide what file to open (from dfilter code) */
298 path = get_persconffile_path("colorfilters", FALSE);
299 if ((f = fopen(path, "r")) == NULL) {
300 if (errno != ENOENT) {
301 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
302 "Could not open filter file\n\"%s\": %s.", path,
311 ret = read_filters_file(f, NULL);
316 /* read filters from the filter file */
318 read_global_filters(void)
324 /* decide what file to open (from dfilter code) */
325 path = get_datafile_path("colorfilters");
326 if ((f = fopen(path, "r")) == NULL) {
327 if (errno != ENOENT) {
328 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
329 "Could not open global filter file\n\"%s\": %s.", path,
338 ret = read_filters_file(f, NULL);
343 /* save filters in some other filter file */
346 read_other_filters(gchar *path, gpointer arg)
351 if ((f = fopen(path, "r")) == NULL) {
352 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
353 "Could not open\n%s\nfor reading: %s.",
354 path, strerror(errno));
358 ret = read_filters_file(f, arg);
363 struct write_filter_data
366 gboolean only_marked;
370 write_filter(gpointer filter_arg, gpointer data_arg)
372 struct write_filter_data *data = data_arg;
373 color_filter_t *colorf = filter_arg;
376 if (colorf->marked || !data->only_marked) {
377 fprintf(f,"@%s@%s@[%d,%d,%d][%d,%d,%d]\n",
380 colorf->bg_color.red,
381 colorf->bg_color.green,
382 colorf->bg_color.blue,
383 colorf->fg_color.red,
384 colorf->fg_color.green,
385 colorf->fg_color.blue);
389 /* save filters in a filter file */
391 write_filters_file(FILE *f, gboolean only_marked)
393 struct write_filter_data data;
396 data.only_marked = only_marked;
398 fprintf(f,"# DO NOT EDIT THIS FILE! It was created by Ethereal\n");
399 g_slist_foreach(filter_list, write_filter, &data);
403 /* save filters in users filter file */
412 /* Create the directory that holds personal configuration files,
414 if (create_persconffile_dir(&pf_dir_path) == -1) {
415 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
416 "Can't create directory\n\"%s\"\nfor color files: %s.",
417 pf_dir_path, strerror(errno));
422 path = get_persconffile_path("colorfilters", TRUE);
423 if ((f = fopen(path, "w+")) == NULL) {
424 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
425 "Could not open\n%s\nfor writing: %s.",
426 path, strerror(errno));
429 write_filters_file(f, FALSE);
434 /* delete users filter file and reload global filters*/
442 /* Create the directory that holds personal configuration files,
444 if (create_persconffile_dir(&pf_dir_path) == -1) {
445 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
446 "Can't create directory\n\"%s\"\nfor color files: %s.",
447 pf_dir_path, strerror(errno));
452 path = get_persconffile_path("colorfilters", TRUE);
453 if (!deletefile(path)) {
454 g_free((gchar *)path);
458 g_free((gchar *)path);
460 /* Reload the (global) filters - Note: this does not update the dialog. */
466 /* save filters in some other filter file */
469 write_other_filters(gchar *path, gboolean only_marked)
473 if ((f = fopen(path, "w+")) == NULL) {
474 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
475 "Could not open\n%s\nfor writing: %s.",
476 path, strerror(errno));
479 write_filters_file(f, only_marked);