8ee7e50752853429d3b81ab1a6903430f8ba6d4f
[obnox/wireshark/wip.git] / color_filters.c
1 /* color_filters.c
2  * Routines for color filters
3  *
4  * $Id$
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
8  * Copyright 1998 Gerald Combs
9  *
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.
14  *
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.
19  *
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.
23  */
24 /*
25  * Updated 1 Dec 10 jjm
26  */
27  
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <glib.h>
33 #include <ctype.h>
34 #include <string.h>
35
36 #include <epan/filesystem.h>
37
38 #include <epan/packet.h>
39 #include "color.h"
40 #include "color_filters.h"
41 #include "file.h"
42 #include <epan/dfilter/dfilter.h>
43 #include "simple_dialog.h"
44 #include "ui_util.h"
45
46 static gboolean read_filters(void);
47 static gboolean read_global_filters(void);
48
49 /* Variables and routines defined in color.h */
50
51 GSList *color_filter_list = NULL;
52 GSList *removed_filter_list = NULL;
53
54 /* Color Filters can en-/disabled. */
55 gboolean filters_enabled = TRUE;
56
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
61  * still reachable. */
62 void color_filter_remove(color_filter_t *colorf)
63 {
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);
68 }
69
70 /* delete the specified filter */
71 static void
72 delete_color_filter(color_filter_t *colorf)
73 {
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);
80         g_free(colorf);
81 }
82
83 /* delete the specified filter as an iterator */
84 static void
85 delete_color_filter_it(gpointer filter_arg, gpointer unused _U_)
86 {
87         color_filter_t *colorf = filter_arg;
88         delete_color_filter(colorf);
89 }
90
91 /* delete all the filters */
92 static void
93 delete_all_color_filters (void)
94 {
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;
101 }
102
103 /* Initialize the filter structures (reading from file) for general running, including app startup */
104 void
105 color_filters_init(void)
106 {
107         delete_all_color_filters();
108         if (!read_filters())
109                 read_global_filters();
110 }
111
112 /* Create a new filter */
113 color_filter_t *
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 */
118 {
119         color_filter_t *colorf;
120
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);
130         return colorf;
131 }
132
133 static void
134 prime_edt(gpointer data, gpointer user_data)
135 {
136         color_filter_t  *colorf = data;
137         epan_dissect_t   *edt = user_data;
138
139         if (colorf->c_colorfilter != NULL)
140                 epan_dissect_prime_dfilter(edt, colorf->c_colorfilter);
141 }
142
143 gboolean 
144 color_filters_used(void)
145 {
146     return color_filter_list != NULL && filters_enabled;
147 }
148
149 void
150 color_filters_enable(gboolean enable)
151 {
152     filters_enabled = enable;
153 }
154
155
156 typedef struct {
157   color_filter_t *colorf;
158   epan_dissect_t *edt;
159 } apply_color_filter_args;
160
161 /*
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.)
165  */
166 static void
167 apply_color_filter(gpointer filter_arg, gpointer argp)
168 {
169   color_filter_t *colorf = filter_arg;
170   apply_color_filter_args *args = argp;
171
172   if (colorf->c_colorfilter != NULL && args->colorf == NULL) {
173     if (dfilter_apply_edt(colorf->c_colorfilter, args->edt))
174       args->colorf = colorf;
175   }
176 }
177
178
179 color_filter_t *
180 color_filters_colorize_packet(gint row, epan_dissect_t *edt)
181 {
182   apply_color_filter_args args;
183
184
185   /* We don't yet have a color filter to apply. */
186   args.colorf = NULL;
187
188   /* If we have color filters, "search" for the matching one. */
189     if (color_filters_used()) {
190       args.edt = edt;
191       g_slist_foreach(color_filter_list, apply_color_filter, &args);
192
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));
196     }
197     }
198
199     return args.colorf;
200 }
201
202 /* Prime the epan_dissect_t with all the compiler
203  * color filters in 'color_filter_list'. */
204 void
205 color_filters_prime_edt(epan_dissect_t *edt)
206 {
207         g_slist_foreach(color_filter_list, prime_edt, edt);
208 }
209
210 /* read filters from the given file */
211
212 /* XXX - Would it make more sense to use GStrings here instead of reallocing
213    our buffers? */
214 static gboolean
215 read_filters_file(FILE *f, gpointer arg)
216 {
217 #define INIT_BUF_SIZE 128
218         gchar  *name = NULL;
219         gchar  *filter_exp = NULL;
220         guint32 name_len = INIT_BUF_SIZE;
221         guint32 filter_exp_len = INIT_BUF_SIZE;
222         guint32 i = 0;
223         gint32  c;
224         guint16 fg_r, fg_g, fg_b, bg_r, bg_g, bg_b;
225         gboolean skip_end_of_line = FALSE;
226
227         name = g_malloc(name_len + 1);
228         filter_exp = g_malloc(filter_exp_len + 1);
229
230         while (1) {
231
232                 if (skip_end_of_line) {
233                         do {
234                                 c = getc(f);
235                         } while (c != EOF && c != '\n');
236                         if (c == EOF)
237                                 break;
238                         skip_end_of_line = FALSE;
239                 }
240
241                 while ((c = getc(f)) != EOF && isspace(c)) {
242                         if (c == '\n') {
243                                 continue;
244                         }
245                 }
246
247                 if (c == EOF)
248                         break;
249
250                 /* skip # comments and invalid lines */
251                 if (c != '@') { 
252                         skip_end_of_line = TRUE;
253                         continue;
254                 }
255
256                 /* we get the @ delimiter.
257                  * Format is:
258                  * @name@filter expression@[background r,g,b][foreground r,g,b]
259                  */
260
261                 /* retrieve name */
262                 i = 0;
263                 while (1) {
264                         c = getc(f);
265                         if (c == EOF || c == '@')
266                                 break;
267                         if (i >= name_len) {
268                                 /* buffer isn't long enough; double its length.*/
269                                 name_len *= 2;
270                                 name = g_realloc(name, name_len + 1);
271                         }
272                         name[i++] = c;            
273                 }
274                 name[i] = '\0';
275
276                 if (c == EOF) {
277                         break;
278                 } else if (i == 0) {
279                         skip_end_of_line = TRUE;
280                         continue;
281                 }
282
283                 /* retrieve filter expression */
284                 i = 0;
285                 while (1) {
286                         c = getc(f);
287                         if (c == EOF || c == '@')
288                                 break;
289                         if (i >= filter_exp_len) {
290                                 /* buffer isn't long enough; double its length.*/
291                                 filter_exp_len *= 2;
292                                 filter_exp = g_realloc(filter_exp, filter_exp_len + 1);
293                         }
294                         filter_exp[i++] = c;
295                 }
296                 filter_exp[i] = '\0';
297
298                 if (c == EOF) {
299                         break;
300                 } else if (i == 0) {
301                         skip_end_of_line = TRUE;
302                         continue;
303                 }
304
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) {
308
309                         /* we got a complete color filter */
310
311                         color_t bg_color, fg_color;
312                         color_filter_t *colorf;
313                         dfilter_t *temp_dfilter;
314
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;
320                                 continue;
321                         }
322
323                         if (!initialize_color(&fg_color, fg_r, fg_g, fg_b)) {
324                                 /* oops */
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;
330                                 continue;
331                         }
332                         if (!initialize_color(&bg_color, bg_r, bg_g, bg_b)) {
333                                 /* oops */
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;
339                                 continue;
340                         }
341
342                         colorf = color_filter_new(name, filter_exp, &bg_color,
343                             &fg_color);
344                         colorf->c_colorfilter = temp_dfilter;
345
346                         if (arg != NULL)
347                                 color_filter_add_cb (colorf, arg);
348                 }    /* if sscanf */
349
350                 skip_end_of_line = TRUE;
351         }
352
353         g_free(name);
354         g_free(filter_exp);
355         return TRUE;
356 }
357
358 /* read filters from the user's filter file */
359 static gboolean
360 read_filters(void)
361 {
362         gchar *path;
363         FILE *f;
364         gboolean ret;
365
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,
372                             strerror(errno));
373                 }
374                 g_free(path);
375                 return FALSE;
376         }
377         g_free(path);
378         path = NULL;
379
380         ret = read_filters_file(f, NULL);
381         fclose(f);
382         return ret;
383 }
384
385 /* read filters from the filter file */
386 static gboolean
387 read_global_filters(void)
388 {
389         gchar *path;
390         FILE *f;
391         gboolean ret;
392
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,
399                             strerror(errno));
400                 }
401                 g_free(path);
402                 return FALSE;
403         }
404         g_free(path);
405         path = NULL;
406
407         ret = read_filters_file(f, NULL);
408         fclose(f);
409         return ret;
410 }
411
412 /* read filters from some other filter file (import) */
413 gboolean
414 color_filters_import(gchar *path, gpointer arg)
415 {
416         FILE *f;
417         gboolean ret;
418
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));
423                 return FALSE;
424         }
425
426         ret = read_filters_file(f, arg);
427         fclose(f);
428         return ret;
429 }
430
431 struct write_filter_data
432 {
433   FILE * f;
434   gboolean only_marked;
435 };
436
437 /* save a single filter */
438 static void
439 write_filter(gpointer filter_arg, gpointer data_arg)
440 {
441         struct write_filter_data *data = data_arg;
442         color_filter_t *colorf = filter_arg;
443         FILE *f = data->f;
444
445         if (colorf->marked || !data->only_marked) {
446                 fprintf(f,"@%s@%s@[%d,%d,%d][%d,%d,%d]\n",
447                     colorf->filter_name,
448                     colorf->filter_text,
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);
455         }
456 }
457
458 /* save filters in a filter file */
459 static gboolean
460 write_filters_file(FILE *f, gboolean only_marked)
461 {
462         struct write_filter_data data;
463
464         data.f = f;
465         data.only_marked = only_marked;
466   
467         fprintf(f,"# DO NOT EDIT THIS FILE!  It was created by Ethereal\n");
468         g_slist_foreach(color_filter_list, write_filter, &data);
469         return TRUE;
470 }
471
472 /* save filters in users filter file */
473 gboolean
474 color_filters_write(void)
475 {
476         gchar *pf_dir_path;
477         const gchar *path;
478         FILE *f;
479
480         /* Create the directory that holds personal configuration files,
481            if necessary.  */
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));
486                 g_free(pf_dir_path);
487                 return FALSE;
488         }
489
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));
495                 return FALSE;
496         }
497         write_filters_file(f, FALSE);
498         fclose(f);
499         return TRUE;
500 }
501
502 /* delete users filter file and reload global filters */
503 gboolean
504 color_filters_revert(void)
505 {
506         gchar *path;
507
508         path = get_persconffile_path("colorfilters", TRUE);
509         if (!deletefile(path)) {
510                 g_free(path);
511                 return FALSE;
512         }
513
514         g_free(path);
515
516         /* Reload the (global) filters - Note: this does not update the dialog. */
517         color_filters_init();
518         return TRUE;
519 }
520
521
522 /* save filters in some other filter file (export) */
523 gboolean
524 color_filters_export(gchar *path, gboolean only_marked)
525 {
526         FILE *f;
527
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));
532                 return FALSE;
533         }
534         write_filters_file(f, only_marked);
535         fclose(f);
536         return TRUE;
537 }