Fix bug 1173, remove the space after the comma in CSV lines
[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 = g_slist_next(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         }
268     }
269
270     return NULL;
271 }
272
273 /* read filters from the given file */
274 /* XXX - Would it make more sense to use GStrings here instead of reallocing
275    our buffers? */
276 static gboolean
277 read_filters_file(FILE *f, gpointer user_data)
278 {
279 #define INIT_BUF_SIZE 128
280         gchar  *name = NULL;
281         gchar  *filter_exp = NULL;
282         guint32 name_len = INIT_BUF_SIZE;
283         guint32 filter_exp_len = INIT_BUF_SIZE;
284         guint32 i = 0;
285         gint32  c;
286         guint16 fg_r, fg_g, fg_b, bg_r, bg_g, bg_b;
287         gboolean skip_end_of_line = FALSE;
288
289         name = g_malloc(name_len + 1);
290         filter_exp = g_malloc(filter_exp_len + 1);
291
292         while (1) {
293
294                 if (skip_end_of_line) {
295                         do {
296                                 c = getc(f);
297                         } while (c != EOF && c != '\n');
298                         if (c == EOF)
299                                 break;
300                         skip_end_of_line = FALSE;
301                 }
302
303                 while ((c = getc(f)) != EOF && isspace(c)) {
304                         if (c == '\n') {
305                                 continue;
306                         }
307                 }
308
309                 if (c == EOF)
310                         break;
311
312                 /* skip # comments and invalid lines */
313                 if (c != '@') { 
314                         skip_end_of_line = TRUE;
315                         continue;
316                 }
317
318                 /* we get the @ delimiter.
319                  * Format is:
320                  * @name@filter expression@[background r,g,b][foreground r,g,b]
321                  */
322
323                 /* retrieve name */
324                 i = 0;
325                 while (1) {
326                         c = getc(f);
327                         if (c == EOF || c == '@')
328                                 break;
329                         if (i >= name_len) {
330                                 /* buffer isn't long enough; double its length.*/
331                                 name_len *= 2;
332                                 name = g_realloc(name, name_len + 1);
333                         }
334                         name[i++] = c;            
335                 }
336                 name[i] = '\0';
337
338                 if (c == EOF) {
339                         break;
340                 } else if (i == 0) {
341                         skip_end_of_line = TRUE;
342                         continue;
343                 }
344
345                 /* retrieve filter expression */
346                 i = 0;
347                 while (1) {
348                         c = getc(f);
349                         if (c == EOF || c == '@')
350                                 break;
351                         if (i >= filter_exp_len) {
352                                 /* buffer isn't long enough; double its length.*/
353                                 filter_exp_len *= 2;
354                                 filter_exp = g_realloc(filter_exp, filter_exp_len + 1);
355                         }
356                         filter_exp[i++] = c;
357                 }
358                 filter_exp[i] = '\0';
359
360                 if (c == EOF) {
361                         break;
362                 } else if (i == 0) {
363                         skip_end_of_line = TRUE;
364                         continue;
365                 }
366
367                 /* retrieve background and foreground colors */
368                 if (fscanf(f,"[%hu,%hu,%hu][%hu,%hu,%hu]",
369                         &bg_r, &bg_g, &bg_b, &fg_r, &fg_g, &fg_b) == 6) {
370
371                         /* we got a complete color filter */
372
373                         color_t bg_color, fg_color;
374                         color_filter_t *colorf;
375                         dfilter_t *temp_dfilter;
376
377                         if (!dfilter_compile(filter_exp, &temp_dfilter)) {
378                                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
379                                 "Could not compile color filter %s from saved filters.\n%s",
380                                               name, dfilter_error_msg);
381                                 skip_end_of_line = TRUE;
382                                 continue;
383                         }
384
385                         if (!initialize_color(&fg_color, fg_r, fg_g, fg_b)) {
386                                 /* oops */
387                                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
388                                     "Could not allocate foreground color "
389                                     "specified in input file for %s.", name);
390                                 dfilter_free(temp_dfilter);
391                                 skip_end_of_line = TRUE;
392                                 continue;
393                         }
394                         if (!initialize_color(&bg_color, bg_r, bg_g, bg_b)) {
395                                 /* oops */
396                                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
397                                     "Could not allocate background color "
398                                     "specified in input file for %s.", name);
399                                 dfilter_free(temp_dfilter);
400                                 skip_end_of_line = TRUE;
401                                 continue;
402                         }
403
404                         colorf = color_filter_new(name, filter_exp, &bg_color,
405                             &fg_color);
406                         if(user_data == &color_filter_list) {
407                                 GSList **cfl = (GSList **)user_data;
408
409                                 /* internal call */
410                                 colorf->c_colorfilter = temp_dfilter;
411                                 *cfl = g_slist_append(*cfl, colorf);
412                         } else {
413                                 /* external call */
414                                 /* just editing, don't need the compiled filter */
415                                 dfilter_free(temp_dfilter);
416                                 color_filter_add_cb (colorf, user_data);
417                         }
418                 }    /* if sscanf */
419
420                 skip_end_of_line = TRUE;
421         }
422
423         g_free(name);
424         g_free(filter_exp);
425         return TRUE;
426 }
427
428 /* read filters from the user's filter file */
429 static gboolean
430 read_users_filters(GSList **cfl)
431 {
432         gchar *path;
433         FILE *f;
434         gboolean ret;
435
436         /* decide what file to open (from dfilter code) */
437         path = get_persconffile_path("colorfilters", FALSE);
438         if ((f = eth_fopen(path, "r")) == NULL) {
439                 if (errno != ENOENT) {
440                         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
441                             "Could not open filter file\n\"%s\": %s.", path,
442                             strerror(errno));
443                 }
444                 g_free(path);
445                 return FALSE;
446         }
447         g_free(path);
448         path = NULL;
449
450         ret = read_filters_file(f, cfl);
451         fclose(f);
452         return ret;
453 }
454
455 /* read filters from the filter file */
456 gboolean
457 color_filters_read_globals(gpointer user_data)
458 {
459         gchar *path;
460         FILE *f;
461         gboolean ret;
462
463         /* decide what file to open (from dfilter code) */
464         path = get_datafile_path("colorfilters");
465         if ((f = eth_fopen(path, "r")) == NULL) {
466                 if (errno != ENOENT) {
467                         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
468                             "Could not open global filter file\n\"%s\": %s.", path,
469                             strerror(errno));
470                 }
471                 g_free(path);
472                 return FALSE;
473         }
474         g_free(path);
475         path = NULL;
476
477         ret = read_filters_file(f, user_data);
478         fclose(f);
479         return ret;
480 }
481
482 /* read filters from some other filter file (import) */
483 gboolean
484 color_filters_import(gchar *path, gpointer user_data)
485 {
486         FILE *f;
487         gboolean ret;
488
489         if ((f = eth_fopen(path, "r")) == NULL) {
490                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
491                     "Could not open\n%s\nfor reading: %s.",
492                     path, strerror(errno));
493                 return FALSE;
494         }
495
496         ret = read_filters_file(f, user_data);
497         fclose(f);
498         return ret;
499 }
500
501 struct write_filter_data
502 {
503   FILE * f;
504   gboolean only_selected;
505 };
506
507 /* save a single filter */
508 static void
509 write_filter(gpointer filter_arg, gpointer data_arg)
510 {
511         struct write_filter_data *data = data_arg;
512         color_filter_t *colorf = filter_arg;
513         FILE *f = data->f;
514
515         if (colorf->selected || !data->only_selected) {
516                 fprintf(f,"@%s@%s@[%d,%d,%d][%d,%d,%d]\n",
517                     colorf->filter_name,
518                     colorf->filter_text,
519                     colorf->bg_color.red,
520                     colorf->bg_color.green,
521                     colorf->bg_color.blue,
522                     colorf->fg_color.red,
523                     colorf->fg_color.green,
524                     colorf->fg_color.blue);
525         }
526 }
527
528 /* save filters in a filter file */
529 static gboolean
530 write_filters_file(GSList *cfl, FILE *f, gboolean only_selected)
531 {
532         struct write_filter_data data;
533
534         data.f = f;
535         data.only_selected = only_selected;
536   
537         fprintf(f,"# DO NOT EDIT THIS FILE!  It was created by Wireshark\n");
538         g_slist_foreach(cfl, write_filter, &data);
539         return TRUE;
540 }
541
542 /* save filters in users filter file */
543 gboolean
544 color_filters_write(GSList *cfl)
545 {
546         gchar *pf_dir_path;
547         const gchar *path;
548         FILE *f;
549
550         /* Create the directory that holds personal configuration files,
551            if necessary.  */
552         if (create_persconffile_dir(&pf_dir_path) == -1) {
553                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
554                     "Can't create directory\n\"%s\"\nfor color files: %s.",
555                     pf_dir_path, strerror(errno));
556                 g_free(pf_dir_path);
557                 return FALSE;
558         }
559
560         path = get_persconffile_path("colorfilters", TRUE);
561         if ((f = eth_fopen(path, "w+")) == NULL) {
562                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
563                     "Could not open\n%s\nfor writing: %s.",
564                     path, strerror(errno));
565                 return FALSE;
566         }
567         write_filters_file(cfl, f, FALSE);
568         fclose(f);
569         return TRUE;
570 }
571
572 /* save filters in some other filter file (export) */
573 gboolean
574 color_filters_export(gchar *path, GSList *cfl, gboolean only_marked)
575 {
576         FILE *f;
577
578         if ((f = eth_fopen(path, "w+")) == NULL) {
579                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
580                     "Could not open\n%s\nfor writing: %s.",
581                     path, strerror(errno));
582                 return FALSE;
583         }
584         write_filters_file(cfl, f, only_marked);
585         fclose(f);
586         return TRUE;
587 }
588