Add Windows version info resource.
[obnox/wireshark/wip.git] / color_filters.c
1 /* color_filters.c
2  * Routines for color filters
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
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 #include "file_util.h"
38
39 #include <epan/packet.h>
40 #include "color.h"
41 #include "color_filters.h"
42 #include "file.h"
43 #include <epan/dfilter/dfilter.h>
44 #include "simple_dialog.h"
45 #include "ui_util.h"
46
47 static gboolean read_users_filters(GSList **cfl);
48
49 /* the currently active filters */
50 static GSList *color_filter_list = NULL;
51
52 /* keep "old" deleted filters in this list until 
53  * the dissection no longer needs them (e.g. file is closed) */
54 static GSList *color_filter_deleted_list = NULL;
55
56 /* Color Filters can en-/disabled. */
57 gboolean filters_enabled = TRUE;
58
59
60 /* Create a new filter */
61 color_filter_t *
62 color_filter_new(const gchar *name,    /* The name of the filter to create */
63                  const gchar *filter_string, /* The string representing the filter */
64                  color_t *bg_color,    /* The background color */
65                  color_t *fg_color)    /* The foreground color */
66 {
67         color_filter_t *colorf;
68
69         colorf = g_malloc(sizeof (color_filter_t));
70         colorf->filter_name = g_strdup(name);
71         colorf->filter_text = g_strdup(filter_string);
72         colorf->bg_color = *bg_color;
73         colorf->fg_color = *fg_color;
74         colorf->c_colorfilter = NULL;
75         colorf->edit_dialog = NULL;
76         colorf->selected = FALSE;
77         return colorf;
78 }
79
80 /* delete the specified filter */
81 void
82 color_filter_delete(color_filter_t *colorf)
83 {
84         if (colorf->filter_name != NULL)
85                 g_free(colorf->filter_name);
86         if (colorf->filter_text != NULL)
87                 g_free(colorf->filter_text);
88         if (colorf->c_colorfilter != NULL)
89                 dfilter_free(colorf->c_colorfilter);
90         g_free(colorf);
91 }
92
93 /* delete the specified filter (called from g_slist_foreach) */
94 static void
95 color_filter_delete_cb(gpointer filter_arg, gpointer unused _U_)
96 {
97         color_filter_t *colorf = filter_arg;
98
99         color_filter_delete(colorf);
100 }
101
102 /* delete the specified list */
103 void
104 color_filter_list_delete(GSList **cfl)
105 {
106         g_slist_foreach(*cfl, color_filter_delete_cb, NULL);
107         g_slist_free(*cfl);
108         *cfl = NULL;
109 }
110
111 /* clone a single list entries from normal to edit list */
112 static color_filter_t *
113 color_filter_clone(color_filter_t *colorf)
114 {
115         color_filter_t *new_colorf;
116
117         new_colorf = g_malloc(sizeof (color_filter_t));
118         new_colorf->filter_name = g_strdup(colorf->filter_name);
119         new_colorf->filter_text = g_strdup(colorf->filter_text);
120         new_colorf->bg_color = colorf->bg_color;
121         new_colorf->fg_color = colorf->fg_color;
122         new_colorf->c_colorfilter = NULL;
123         new_colorf->edit_dialog = NULL;
124         new_colorf->selected = FALSE;
125
126         return new_colorf;
127 }
128
129 static void
130 color_filter_list_clone_cb(gpointer filter_arg, gpointer cfl_arg)
131 {
132     gpointer *cfl = cfl_arg;
133     color_filter_t *new_colorf;
134
135     new_colorf = color_filter_clone(filter_arg);
136     *cfl = g_slist_append(*cfl, new_colorf);
137 }
138
139 /* clone the specified list */
140 static GSList *
141 color_filter_list_clone(GSList *cfl)
142 {
143         GSList *new_list = NULL;
144
145         g_slist_foreach(cfl, color_filter_list_clone_cb, &new_list);
146
147         return new_list;
148 }
149
150 /* Initialize the filter structures (reading from file) for general running, including app startup */
151 void
152 color_filters_init(void)
153 {
154         /* delete all currently existing filters */
155         color_filter_list_delete(&color_filter_list);
156
157         /* try to read the users filters */
158         if (!read_users_filters(&color_filter_list))
159                 /* if that failed, try to read the global filters */
160                 color_filters_read_globals(&color_filter_list);
161 }
162
163 void
164 color_filters_cleanup(void)
165 {
166         /* delete the previously deleted filters */
167         color_filter_list_delete(&color_filter_deleted_list);
168 }
169
170 static void
171 color_filters_clone_cb(gpointer filter_arg, gpointer user_data)
172 {
173         color_filter_t * new_colorf = color_filter_clone(filter_arg);
174
175         color_filter_add_cb (new_colorf, user_data);
176 }
177
178 void
179 color_filters_clone(gpointer user_data)
180 {
181     g_slist_foreach(color_filter_list, color_filters_clone_cb, user_data);
182 }
183
184
185 static void
186 color_filter_compile_cb(gpointer filter_arg, gpointer unused _U_)
187 {
188         color_filter_t *colorf = filter_arg;
189
190         g_assert(colorf->c_colorfilter == NULL);
191         if (!dfilter_compile(colorf->filter_text, &colorf->c_colorfilter)) {
192                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
193                 "Could not compile color filter name: \"%s\" text: \"%s\".\n%s",
194                               colorf->filter_name, colorf->filter_text, dfilter_error_msg);
195                 /* this filter was compilable before, so this should never happen */
196                 g_assert_not_reached();
197         }
198 }
199
200 /* apply changes from the edit list */
201 void
202 color_filters_apply(GSList *cfl)
203 {
204         /* "move" old entries to the deleted list
205          * we must keep them until the dissection no longer needs them */
206         color_filter_deleted_list = g_slist_concat(color_filter_deleted_list, color_filter_list);
207         color_filter_list = NULL;
208
209         /* clone all list entries from edit to normal list */
210         color_filter_list = color_filter_list_clone(cfl);
211
212         /* compile all filter */
213         g_slist_foreach(color_filter_list, color_filter_compile_cb, NULL);
214 }
215
216 gboolean 
217 color_filters_used(void)
218 {
219         return color_filter_list != NULL && filters_enabled;
220 }
221
222 void
223 color_filters_enable(gboolean enable)
224 {
225         filters_enabled = enable;
226 }
227
228
229 /* prepare the epan_dissect_t for the filter */
230 static void
231 prime_edt(gpointer data, gpointer user_data)
232 {
233         color_filter_t  *colorf = data;
234         epan_dissect_t   *edt = user_data;
235
236         if (colorf->c_colorfilter != NULL)
237                 epan_dissect_prime_dfilter(edt, colorf->c_colorfilter);
238 }
239
240 /* Prime the epan_dissect_t with all the compiler
241  * color filters in 'color_filter_list'. */
242 void
243 color_filters_prime_edt(epan_dissect_t *edt)
244 {
245         g_slist_foreach(color_filter_list, prime_edt, edt);
246 }
247
248 /* Colorize a single packet of the packet list */
249 color_filter_t *
250 color_filters_colorize_packet(gint row, epan_dissect_t *edt)
251 {
252     GSList *curr;
253     color_filter_t *colorf;
254
255     /* If we have color filters, "search" for the matching one. */
256     if (color_filters_used()) {
257         curr = color_filter_list;
258
259         while(curr != NULL) {
260             colorf = curr->data;
261             if ((colorf->c_colorfilter != NULL) &&
262                  dfilter_apply_edt(colorf->c_colorfilter, edt)) {
263                     /* this is the filter to use, apply it to the packet list */
264                     packet_list_set_colors(row, &(colorf->fg_color), &(colorf->bg_color));
265                     return colorf;
266             }
267             curr = g_slist_next(curr);
268         }
269     }
270
271     return NULL;
272 }
273
274 /* read filters from the given file */
275 /* XXX - Would it make more sense to use GStrings here instead of reallocing
276    our buffers? */
277 static gboolean
278 read_filters_file(FILE *f, gpointer user_data)
279 {
280 #define INIT_BUF_SIZE 128
281         gchar  *name = NULL;
282         gchar  *filter_exp = NULL;
283         guint32 name_len = INIT_BUF_SIZE;
284         guint32 filter_exp_len = INIT_BUF_SIZE;
285         guint32 i = 0;
286         gint32  c;
287         guint16 fg_r, fg_g, fg_b, bg_r, bg_g, bg_b;
288         gboolean skip_end_of_line = FALSE;
289
290         name = g_malloc(name_len + 1);
291         filter_exp = g_malloc(filter_exp_len + 1);
292
293         while (1) {
294
295                 if (skip_end_of_line) {
296                         do {
297                                 c = getc(f);
298                         } while (c != EOF && c != '\n');
299                         if (c == EOF)
300                                 break;
301                         skip_end_of_line = FALSE;
302                 }
303
304                 while ((c = getc(f)) != EOF && isspace(c)) {
305                         if (c == '\n') {
306                                 continue;
307                         }
308                 }
309
310                 if (c == EOF)
311                         break;
312
313                 /* skip # comments and invalid lines */
314                 if (c != '@') { 
315                         skip_end_of_line = TRUE;
316                         continue;
317                 }
318
319                 /* we get the @ delimiter.
320                  * Format is:
321                  * @name@filter expression@[background r,g,b][foreground r,g,b]
322                  */
323
324                 /* retrieve name */
325                 i = 0;
326                 while (1) {
327                         c = getc(f);
328                         if (c == EOF || c == '@')
329                                 break;
330                         if (i >= name_len) {
331                                 /* buffer isn't long enough; double its length.*/
332                                 name_len *= 2;
333                                 name = g_realloc(name, name_len + 1);
334                         }
335                         name[i++] = c;            
336                 }
337                 name[i] = '\0';
338
339                 if (c == EOF) {
340                         break;
341                 } else if (i == 0) {
342                         skip_end_of_line = TRUE;
343                         continue;
344                 }
345
346                 /* retrieve filter expression */
347                 i = 0;
348                 while (1) {
349                         c = getc(f);
350                         if (c == EOF || c == '@')
351                                 break;
352                         if (i >= filter_exp_len) {
353                                 /* buffer isn't long enough; double its length.*/
354                                 filter_exp_len *= 2;
355                                 filter_exp = g_realloc(filter_exp, filter_exp_len + 1);
356                         }
357                         filter_exp[i++] = c;
358                 }
359                 filter_exp[i] = '\0';
360
361                 if (c == EOF) {
362                         break;
363                 } else if (i == 0) {
364                         skip_end_of_line = TRUE;
365                         continue;
366                 }
367
368                 /* retrieve background and foreground colors */
369                 if (fscanf(f,"[%hu,%hu,%hu][%hu,%hu,%hu]",
370                         &bg_r, &bg_g, &bg_b, &fg_r, &fg_g, &fg_b) == 6) {
371
372                         /* we got a complete color filter */
373
374                         color_t bg_color, fg_color;
375                         color_filter_t *colorf;
376                         dfilter_t *temp_dfilter;
377
378                         if (!dfilter_compile(filter_exp, &temp_dfilter)) {
379                                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
380                                 "Could not compile color filter %s from saved filters.\n%s",
381                                               name, dfilter_error_msg);
382                                 skip_end_of_line = TRUE;
383                                 continue;
384                         }
385
386                         if (!initialize_color(&fg_color, fg_r, fg_g, fg_b)) {
387                                 /* oops */
388                                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
389                                     "Could not allocate foreground color "
390                                     "specified in input file for %s.", name);
391                                 dfilter_free(temp_dfilter);
392                                 skip_end_of_line = TRUE;
393                                 continue;
394                         }
395                         if (!initialize_color(&bg_color, bg_r, bg_g, bg_b)) {
396                                 /* oops */
397                                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
398                                     "Could not allocate background color "
399                                     "specified in input file for %s.", name);
400                                 dfilter_free(temp_dfilter);
401                                 skip_end_of_line = TRUE;
402                                 continue;
403                         }
404
405                         colorf = color_filter_new(name, filter_exp, &bg_color,
406                             &fg_color);
407                         if(user_data == &color_filter_list) {
408                                 GSList **cfl = (GSList **)user_data;
409
410                                 /* internal call */
411                                 colorf->c_colorfilter = temp_dfilter;
412                                 *cfl = g_slist_append(*cfl, colorf);
413                         } else {
414                                 /* external call */
415                                 /* just editing, don't need the compiled filter */
416                                 dfilter_free(temp_dfilter);
417                                 color_filter_add_cb (colorf, user_data);
418                         }
419                 }    /* if sscanf */
420
421                 skip_end_of_line = TRUE;
422         }
423
424         g_free(name);
425         g_free(filter_exp);
426         return TRUE;
427 }
428
429 /* read filters from the user's filter file */
430 static gboolean
431 read_users_filters(GSList **cfl)
432 {
433         gchar *path;
434         FILE *f;
435         gboolean ret;
436
437         /* decide what file to open (from dfilter code) */
438         path = get_persconffile_path("colorfilters", FALSE);
439         if ((f = eth_fopen(path, "r")) == NULL) {
440                 if (errno != ENOENT) {
441                         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
442                             "Could not open filter file\n\"%s\": %s.", path,
443                             strerror(errno));
444                 }
445                 g_free(path);
446                 return FALSE;
447         }
448         g_free(path);
449         path = NULL;
450
451         ret = read_filters_file(f, cfl);
452         fclose(f);
453         return ret;
454 }
455
456 /* read filters from the filter file */
457 gboolean
458 color_filters_read_globals(gpointer user_data)
459 {
460         gchar *path;
461         FILE *f;
462         gboolean ret;
463
464         /* decide what file to open (from dfilter code) */
465         path = get_datafile_path("colorfilters");
466         if ((f = eth_fopen(path, "r")) == NULL) {
467                 if (errno != ENOENT) {
468                         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
469                             "Could not open global filter file\n\"%s\": %s.", path,
470                             strerror(errno));
471                 }
472                 g_free(path);
473                 return FALSE;
474         }
475         g_free(path);
476         path = NULL;
477
478         ret = read_filters_file(f, user_data);
479         fclose(f);
480         return ret;
481 }
482
483 /* read filters from some other filter file (import) */
484 gboolean
485 color_filters_import(gchar *path, gpointer user_data)
486 {
487         FILE *f;
488         gboolean ret;
489
490         if ((f = eth_fopen(path, "r")) == NULL) {
491                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
492                     "Could not open\n%s\nfor reading: %s.",
493                     path, strerror(errno));
494                 return FALSE;
495         }
496
497         ret = read_filters_file(f, user_data);
498         fclose(f);
499         return ret;
500 }
501
502 struct write_filter_data
503 {
504   FILE * f;
505   gboolean only_selected;
506 };
507
508 /* save a single filter */
509 static void
510 write_filter(gpointer filter_arg, gpointer data_arg)
511 {
512         struct write_filter_data *data = data_arg;
513         color_filter_t *colorf = filter_arg;
514         FILE *f = data->f;
515
516         if (colorf->selected || !data->only_selected) {
517                 fprintf(f,"@%s@%s@[%d,%d,%d][%d,%d,%d]\n",
518                     colorf->filter_name,
519                     colorf->filter_text,
520                     colorf->bg_color.red,
521                     colorf->bg_color.green,
522                     colorf->bg_color.blue,
523                     colorf->fg_color.red,
524                     colorf->fg_color.green,
525                     colorf->fg_color.blue);
526         }
527 }
528
529 /* save filters in a filter file */
530 static gboolean
531 write_filters_file(GSList *cfl, FILE *f, gboolean only_selected)
532 {
533         struct write_filter_data data;
534
535         data.f = f;
536         data.only_selected = only_selected;
537   
538         fprintf(f,"# DO NOT EDIT THIS FILE!  It was created by Wireshark\n");
539         g_slist_foreach(cfl, write_filter, &data);
540         return TRUE;
541 }
542
543 /* save filters in users filter file */
544 gboolean
545 color_filters_write(GSList *cfl)
546 {
547         gchar *pf_dir_path;
548         const gchar *path;
549         FILE *f;
550
551         /* Create the directory that holds personal configuration files,
552            if necessary.  */
553         if (create_persconffile_dir(&pf_dir_path) == -1) {
554                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
555                     "Can't create directory\n\"%s\"\nfor color files: %s.",
556                     pf_dir_path, strerror(errno));
557                 g_free(pf_dir_path);
558                 return FALSE;
559         }
560
561         path = get_persconffile_path("colorfilters", TRUE);
562         if ((f = eth_fopen(path, "w+")) == NULL) {
563                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
564                     "Could not open\n%s\nfor writing: %s.",
565                     path, strerror(errno));
566                 return FALSE;
567         }
568         write_filters_file(cfl, f, FALSE);
569         fclose(f);
570         return TRUE;
571 }
572
573 /* save filters in some other filter file (export) */
574 gboolean
575 color_filters_export(gchar *path, GSList *cfl, gboolean only_marked)
576 {
577         FILE *f;
578
579         if ((f = eth_fopen(path, "w+")) == NULL) {
580                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
581                     "Could not open\n%s\nfor writing: %s.",
582                     path, strerror(errno));
583                 return FALSE;
584         }
585         write_filters_file(cfl, f, only_marked);
586         fclose(f);
587         return TRUE;
588 }
589