Make infiniband plugin compile.
[obnox/wireshark/wip.git] / color_filters.c
index eba18678250844a1e06d5afa84d2f34dfcabde29..445c36d1cfa6b3ed4b5901e4629d86d7d72e8830 100644 (file)
 #include <epan/dfilter/dfilter.h>
 #include "simple_dialog.h"
 #include "ui_util.h"
+#include <epan/prefs.h>
 
-static gboolean read_filters(void);
-static gboolean read_global_filters(void);
+#define RED_COMPONENT(x)   (guint16) (((((x) >> 16) & 0xff) * 65535 / 255))
+#define GREEN_COMPONENT(x) (guint16) (((((x) >>  8) & 0xff) * 65535 / 255))
+#define BLUE_COMPONENT(x)  (guint16) ( (((x)        & 0xff) * 65535 / 255))
 
-/* Variables and routines defined in color.h */
+static gboolean read_users_filters(GSList **cfl);
 
-GSList *color_filter_list = NULL;
-GSList *removed_filter_list = NULL;
+/* the currently active filters */
+static GSList *color_filter_list = NULL;
+
+/* keep "old" deleted filters in this list until 
+ * the dissection no longer needs them (e.g. file is closed) */
+static GSList *color_filter_deleted_list = NULL;
+static GSList *color_filter_valid_list = NULL;
 
 /* Color Filters can en-/disabled. */
 gboolean filters_enabled = TRUE;
 
-/* Remove the specified filter from the list of existing color filters,
- * and add it to the list of removed color filters.
- * This way, unmarking and marking a packet which matches a now removed
- * color filter will still be colored correctly as the color filter is
- * still reachable. */
-void color_filter_remove(color_filter_t *colorf)
+/* Remember if there are temporary coloring filters set to
+ * add sensitivity to the "Reset Coloring 1-10" menu item
+ */
+gboolean tmp_colors_set = FALSE;
+
+/* Create a new filter */
+color_filter_t *
+color_filter_new(const gchar *name,    /* The name of the filter to create */
+                 const gchar *filter_string, /* The string representing the filter */
+                 color_t *bg_color,    /* The background color */
+                 color_t *fg_color,    /* The foreground color */
+                 gboolean disabled)     /* Is the filter disabled? */
+{
+       color_filter_t *colorf;
+
+       colorf = g_malloc(sizeof (color_filter_t));
+       colorf->filter_name = g_strdup(name);
+       colorf->filter_text = g_strdup(filter_string);
+       colorf->bg_color = *bg_color;
+       colorf->fg_color = *fg_color;
+       colorf->disabled = disabled;
+       colorf->c_colorfilter = NULL;
+       colorf->edit_dialog = NULL;
+       colorf->selected = FALSE;
+        return colorf;
+}
+
+/* Add ten empty (temporary) colorfilters for easy coloring */
+void
+color_filters_add_tmp(GSList **cfl)
+{
+       gchar  *name = NULL;
+       guint32 i;
+        gchar** bg_colors;
+        gchar** fg_colors;
+        unsigned long int cval;
+        color_t bg_color, fg_color;
+        color_filter_t *colorf;
+
+       g_assert(strlen(prefs.gui_colorized_fg)==69);
+       g_assert(strlen(prefs.gui_colorized_bg)==69);
+        fg_colors = g_strsplit(prefs.gui_colorized_fg, ",", -1);
+        bg_colors = g_strsplit(prefs.gui_colorized_bg, ",", -1);
+
+        for ( i=1 ; i<=10 ; i++ ) {
+                name = g_strdup_printf("%s%02d",TEMP_COLOR_PREFIX,i);
+
+               /* retrieve background and foreground colors */
+                cval = strtoul(fg_colors[i-1], NULL, 16);
+                initialize_color(&fg_color, RED_COMPONENT(cval),
+                                            GREEN_COMPONENT(cval),
+                                            BLUE_COMPONENT(cval) );
+                cval = strtoul(bg_colors[i-1], NULL, 16);
+                initialize_color(&bg_color, RED_COMPONENT(cval),
+                                            GREEN_COMPONENT(cval),
+                                            BLUE_COMPONENT(cval) );
+                colorf = color_filter_new(name, NULL, &bg_color, &fg_color, TRUE);
+                colorf->filter_text = g_strdup("frame");
+                colorf->c_colorfilter = NULL;
+                *cfl = g_slist_append(*cfl, colorf);
+
+                g_free(name);
+       }
+
+        g_strfreev(fg_colors);
+        g_strfreev(bg_colors);
+
+       return;
+}
+
+static gint
+color_filters_find_by_name_cb(gconstpointer arg1, gconstpointer arg2)
 {
-       /* Remove colorf from the list of color filters */
-       color_filter_list = g_slist_remove(color_filter_list, colorf);
-       /* Add colorf to the list of removed color filters */
-       removed_filter_list = g_slist_prepend(removed_filter_list, colorf);
+        color_filter_t *colorf = (color_filter_t *)arg1;
+        gchar          *name   = (gchar *)arg2;
+
+        return (strstr(colorf->filter_name, name)==NULL) ? -1 : 0 ;
+}
+
+
+/* Set the filter off a temporary colorfilters and enable it */
+void
+color_filters_set_tmp(guint8 filt_nr, gchar *filter, gboolean disabled)
+{
+       gchar  *name = NULL;
+       gchar  *tmpfilter = NULL;
+        GSList *cfl;
+        color_filter_t *colorf;
+        dfilter_t      *compiled_filter;
+       guint8 i;
+
+        /* Go through the tomporary filters and look for the same filter string.
+         * If found, clear it so that a filter can be "moved" up and down the list
+         */
+        for ( i=1 ; i<=10 ; i++ ) {
+                /* If we need to reset the temporary filter (filter==NULL), don't look 
+                 * for other rules with the same filter string
+                 */
+                if( i!=filt_nr && filter==NULL ) 
+                        continue;
+
+                name = g_strdup_printf("%s%02d",TEMP_COLOR_PREFIX,i);
+                cfl = g_slist_find_custom(color_filter_list, (gpointer *) name, color_filters_find_by_name_cb);
+                colorf = cfl->data;
+
+                /* Only change the filter rule if this is the rule to change or if
+                 * a matching filter string has been found
+                 */
+                if(colorf && ( (i==filt_nr) || (strstr(filter,colorf->filter_text)!=NULL) ) ) {
+                        /* set filter string to "frame" if we are resetting the rules
+                         * or if we found a matching filter string which need to be cleared
+                         */
+                        tmpfilter = ( (filter==NULL) || (i!=filt_nr) ) ? "frame" : filter;
+                        if (!dfilter_compile(tmpfilter, &compiled_filter)) {
+                                simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
+                                    "Could not compile color filter name: \"%s\""
+                                    " text: \"%s\".\n%s", name, filter, dfilter_error_msg);
+                        } else {
+                                if (colorf->filter_text != NULL)
+                                        g_free(colorf->filter_text);
+                                if (colorf->c_colorfilter != NULL)
+                                        dfilter_free(colorf->c_colorfilter);
+                                colorf->filter_text = g_strdup(tmpfilter);
+                                colorf->c_colorfilter = compiled_filter;
+                                colorf->disabled = ((i!=filt_nr) ? TRUE : disabled);
+                                /* Remember that there are now temporary coloring filters set */
+                                if( filter )
+                                        tmp_colors_set = TRUE;
+                        }
+                }
+                g_free(name);
+        }
+        return;
+}
+
+/* Reset the temporary colorfilters */
+void
+color_filters_reset_tmp()
+{
+       guint8 i;
+
+        for ( i=1 ; i<=10 ; i++ ) {
+            color_filters_set_tmp(i, NULL, TRUE);
+        }
+        /* Remember that there are now *no* temporary coloring filters set */
+        tmp_colors_set = FALSE;
+       return;
 }
 
 /* delete the specified filter */
-static void
-delete_color_filter(color_filter_t *colorf)
+void
+color_filter_delete(color_filter_t *colorf)
 {
        if (colorf->filter_name != NULL)
                g_free(colorf->filter_name);
@@ -81,66 +224,191 @@ delete_color_filter(color_filter_t *colorf)
        g_free(colorf);
 }
 
-/* delete the specified filter as an iterator */
+/* delete the specified filter (called from g_slist_foreach) */
 static void
-delete_color_filter_it(gpointer filter_arg, gpointer unused _U_)
+color_filter_delete_cb(gpointer filter_arg, gpointer unused _U_)
 {
        color_filter_t *colorf = filter_arg;
-       delete_color_filter(colorf);
+
+       color_filter_delete(colorf);
+}
+
+/* delete the specified list */
+void
+color_filter_list_delete(GSList **cfl)
+{
+        g_slist_foreach(*cfl, color_filter_delete_cb, NULL);
+        g_slist_free(*cfl);
+        *cfl = NULL;
+}
+
+/* clone a single list entries from normal to edit list */
+static color_filter_t *
+color_filter_clone(color_filter_t *colorf)
+{
+       color_filter_t *new_colorf;
+
+       new_colorf = g_malloc(sizeof (color_filter_t));
+       new_colorf->filter_name = g_strdup(colorf->filter_name);
+       new_colorf->filter_text = g_strdup(colorf->filter_text);
+       new_colorf->bg_color = colorf->bg_color;
+       new_colorf->fg_color = colorf->fg_color;
+       new_colorf->disabled = colorf->disabled;
+       new_colorf->c_colorfilter = NULL;
+       new_colorf->edit_dialog = NULL;
+       new_colorf->selected = FALSE;
+
+        return new_colorf;
 }
 
-/* delete all the filters */
 static void
-delete_all_color_filters (void)
+color_filter_list_clone_cb(gpointer filter_arg, gpointer cfl_arg)
 {
-        g_slist_foreach(color_filter_list, delete_color_filter_it, NULL);
-       g_slist_free(color_filter_list);
-       color_filter_list = NULL;
-        g_slist_foreach(removed_filter_list, delete_color_filter_it, NULL);
-       g_slist_free(removed_filter_list);
-       removed_filter_list = NULL;
+    gpointer *cfl = cfl_arg;
+    color_filter_t *new_colorf;
+
+    new_colorf = color_filter_clone(filter_arg);
+    *cfl = g_slist_append(*cfl, new_colorf);
+}
+
+/* clone the specified list */
+static GSList *
+color_filter_list_clone(GSList *cfl)
+{
+        GSList *new_list = NULL;
+
+        g_slist_foreach(cfl, color_filter_list_clone_cb, &new_list);
+
+        return new_list;
 }
 
 /* Initialize the filter structures (reading from file) for general running, including app startup */
 void
 color_filters_init(void)
 {
-       delete_all_color_filters();
-       if (!read_filters())
-               read_global_filters();
+       /* delete all currently existing filters */
+       color_filter_list_delete(&color_filter_list);
+
+        /* start the list with the temporary colorizing rules */
+        color_filters_add_tmp(&color_filter_list);
+
+       /* try to read the users filters */
+       if (!read_users_filters(&color_filter_list))
+               /* if that failed, try to read the global filters */
+               color_filters_read_globals(&color_filter_list);
 }
 
-/* Create a new filter */
-color_filter_t *
-color_filter_new(const gchar *name,    /* The name of the filter to create */
-                 const gchar *filter_string, /* The string representing the filter */
-                 color_t *bg_color,    /* The background color */
-                 color_t *fg_color)    /* The foreground color */
+void
+color_filters_reload(void)
 {
-       color_filter_t *colorf;
+        /* "move" old entries to the deleted list
+         * we must keep them until the dissection no longer needs them */
+        color_filter_deleted_list = g_slist_concat(color_filter_deleted_list, color_filter_list);
+        color_filter_list = NULL;
+
+        /* start the list with the temporary colorizing rules */
+        color_filters_add_tmp(&color_filter_list);
+
+       /* try to read the users filters */
+       if (!read_users_filters(&color_filter_list))
+               /* if that failed, try to read the global filters */
+               color_filters_read_globals(&color_filter_list);
+}
 
-       colorf = g_malloc(sizeof (color_filter_t));
-       colorf->filter_name = g_strdup(name);
-       colorf->filter_text = g_strdup(filter_string);
-       colorf->bg_color = *bg_color;
-       colorf->fg_color = *fg_color;
-       colorf->c_colorfilter = NULL;
-       colorf->edit_dialog = NULL;
-       colorf->marked = FALSE;
-       color_filter_list = g_slist_append(color_filter_list, colorf);
-        return colorf;
+void
+color_filters_cleanup(void)
+{
+        /* delete the previously deleted filters */
+        color_filter_list_delete(&color_filter_deleted_list);
+}
+
+static void
+color_filters_clone_cb(gpointer filter_arg, gpointer user_data)
+{
+       color_filter_t * new_colorf = color_filter_clone(filter_arg);
+        color_filter_add_cb (new_colorf, user_data);
+}
+
+void
+color_filters_clone(gpointer user_data)
+{
+    g_slist_foreach(color_filter_list, color_filters_clone_cb, user_data);
+}
+
+
+static void
+color_filter_compile_cb(gpointer filter_arg, gpointer unused _U_)
+{
+       color_filter_t *colorf = filter_arg;
+
+       g_assert(colorf->c_colorfilter == NULL);
+       if (!dfilter_compile(colorf->filter_text, &colorf->c_colorfilter)) {
+               simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
+               "Could not compile color filter name: \"%s\" text: \"%s\".\n%s",
+                             colorf->filter_name, colorf->filter_text, dfilter_error_msg);
+               /* this filter was compilable before, so this should never happen */
+               /* except if the OK button of the parent window has been clicked */
+               /* so don't use g_assert_not_reached() but check the filters again */
+       }
+}
+
+static void
+color_filter_validate_cb(gpointer filter_arg, gpointer unused _U_)
+{
+       color_filter_t *colorf = filter_arg;
+
+       g_assert(colorf->c_colorfilter == NULL);
+       if (!dfilter_compile(colorf->filter_text, &colorf->c_colorfilter)) {
+               simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
+               "Removing color filter name: \"%s\" text: \"%s\".\n%s",
+                             colorf->filter_name, colorf->filter_text, dfilter_error_msg);
+               /* Delete the color filter from the list of color filters. */
+               color_filter_valid_list = g_slist_remove(color_filter_valid_list, colorf);
+               color_filter_delete(colorf);
+       }
+}
+
+/* apply changes from the edit list */
+void
+color_filters_apply(GSList *tmp_cfl, GSList *edit_cfl)
+{
+        /* "move" old entries to the deleted list
+         * we must keep them until the dissection no longer needs them */
+        color_filter_deleted_list = g_slist_concat(color_filter_deleted_list, color_filter_list);
+        color_filter_list = NULL;
+
+        /* clone all list entries from tmp/edit to normal list */
+        color_filter_valid_list = NULL;
+        color_filter_valid_list = color_filter_list_clone(tmp_cfl);
+        color_filter_valid_list = g_slist_concat(color_filter_valid_list, 
+                                                 color_filter_list_clone(edit_cfl) );
+
+        /* compile all filter */
+        g_slist_foreach(color_filter_valid_list, color_filter_validate_cb, NULL);
+
+        /* clone all list entries from tmp/edit to normal list */
+        color_filter_list = color_filter_list_clone(color_filter_valid_list);
+
+        /* compile all filter */
+        g_slist_foreach(color_filter_list, color_filter_compile_cb, NULL);
 }
 
 gboolean 
 color_filters_used(void)
 {
-    return color_filter_list != NULL && filters_enabled;
+        return color_filter_list != NULL && filters_enabled;
+}
+
+gboolean 
+tmp_color_filters_used(void)
+{
+        return tmp_colors_set;
 }
 
 void
 color_filters_enable(gboolean enable)
 {
-    filters_enabled = enable;
+        filters_enabled = enable;
 }
 
 
@@ -174,14 +442,16 @@ color_filters_colorize_packet(gint row, epan_dissect_t *edt)
     if (color_filters_used()) {
         curr = color_filter_list;
 
-        while( (curr = g_slist_next(curr)) != NULL) {
+        while(curr != NULL) {
             colorf = curr->data;
-            if ((colorf->c_colorfilter != NULL) &&
+            if ( (!colorf->disabled) &&
+                 (colorf->c_colorfilter != NULL) &&
                  dfilter_apply_edt(colorf->c_colorfilter, edt)) {
                     /* this is the filter to use, apply it to the packet list */
                     packet_list_set_colors(row, &(colorf->fg_color), &(colorf->bg_color));
                     return colorf;
             }
+            curr = g_slist_next(curr);
         }
     }
 
@@ -189,11 +459,10 @@ color_filters_colorize_packet(gint row, epan_dissect_t *edt)
 }
 
 /* read filters from the given file */
-
 /* XXX - Would it make more sense to use GStrings here instead of reallocing
    our buffers? */
 static gboolean
-read_filters_file(FILE *f, gpointer arg)
+read_filters_file(FILE *f, gpointer user_data)
 {
 #define INIT_BUF_SIZE 128
        gchar  *name = NULL;
@@ -203,6 +472,7 @@ read_filters_file(FILE *f, gpointer arg)
        guint32 i = 0;
        gint32  c;
        guint16 fg_r, fg_g, fg_b, bg_r, bg_g, bg_b;
+       gboolean disabled = FALSE;
        gboolean skip_end_of_line = FALSE;
 
        name = g_malloc(name_len + 1);
@@ -216,6 +486,7 @@ read_filters_file(FILE *f, gpointer arg)
                        } while (c != EOF && c != '\n');
                        if (c == EOF)
                                break;
+                       disabled = FALSE;
                        skip_end_of_line = FALSE;
                }
 
@@ -228,6 +499,11 @@ read_filters_file(FILE *f, gpointer arg)
                if (c == EOF)
                        break;
 
+               if (c == '!') {
+                       disabled = TRUE;
+                       continue;
+               }
+
                /* skip # comments and invalid lines */
                if (c != '@') { 
                        skip_end_of_line = TRUE;
@@ -321,11 +597,19 @@ read_filters_file(FILE *f, gpointer arg)
                        }
 
                        colorf = color_filter_new(name, filter_exp, &bg_color,
-                           &fg_color);
-                       colorf->c_colorfilter = temp_dfilter;
-
-                       if (arg != NULL)
-                               color_filter_add_cb (colorf, arg);
+                           &fg_color, disabled);
+                        if(user_data == &color_filter_list) {
+                                GSList **cfl = (GSList **)user_data;
+
+                                /* internal call */
+                               colorf->c_colorfilter = temp_dfilter;
+                                *cfl = g_slist_append(*cfl, colorf);
+                        } else {
+                                /* external call */
+                               /* just editing, don't need the compiled filter */
+                               dfilter_free(temp_dfilter);
+                               color_filter_add_cb (colorf, user_data);
+                        }
                }    /* if sscanf */
 
                skip_end_of_line = TRUE;
@@ -338,14 +622,14 @@ read_filters_file(FILE *f, gpointer arg)
 
 /* read filters from the user's filter file */
 static gboolean
-read_filters(void)
+read_users_filters(GSList **cfl)
 {
        gchar *path;
        FILE *f;
        gboolean ret;
 
        /* decide what file to open (from dfilter code) */
-       path = get_persconffile_path("colorfilters", FALSE);
+       path = get_persconffile_path("colorfilters", TRUE, FALSE);
        if ((f = eth_fopen(path, "r")) == NULL) {
                if (errno != ENOENT) {
                        simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
@@ -358,14 +642,14 @@ read_filters(void)
        g_free(path);
        path = NULL;
 
-       ret = read_filters_file(f, NULL);
+       ret = read_filters_file(f, cfl);
        fclose(f);
        return ret;
 }
 
 /* read filters from the filter file */
-static gboolean
-read_global_filters(void)
+gboolean
+color_filters_read_globals(gpointer user_data)
 {
        gchar *path;
        FILE *f;
@@ -385,14 +669,14 @@ read_global_filters(void)
        g_free(path);
        path = NULL;
 
-       ret = read_filters_file(f, NULL);
+       ret = read_filters_file(f, user_data);
        fclose(f);
        return ret;
 }
 
 /* read filters from some other filter file (import) */
 gboolean
-color_filters_import(gchar *path, gpointer arg)
+color_filters_import(gchar *path, gpointer user_data)
 {
        FILE *f;
        gboolean ret;
@@ -404,7 +688,7 @@ color_filters_import(gchar *path, gpointer arg)
                return FALSE;
        }
 
-       ret = read_filters_file(f, arg);
+       ret = read_filters_file(f, user_data);
        fclose(f);
        return ret;
 }
@@ -412,7 +696,7 @@ color_filters_import(gchar *path, gpointer arg)
 struct write_filter_data
 {
   FILE * f;
-  gboolean only_marked;
+  gboolean only_selected;
 };
 
 /* save a single filter */
@@ -423,8 +707,10 @@ write_filter(gpointer filter_arg, gpointer data_arg)
        color_filter_t *colorf = filter_arg;
        FILE *f = data->f;
 
-       if (colorf->marked || !data->only_marked) {
-               fprintf(f,"@%s@%s@[%d,%d,%d][%d,%d,%d]\n",
+       if ( (colorf->selected || !data->only_selected) &&
+             (strstr(colorf->filter_name,TEMP_COLOR_PREFIX)==NULL) ) {
+               fprintf(f,"%s@%s@%s@[%d,%d,%d][%d,%d,%d]\n",
+                   colorf->disabled ? "!" : "",
                    colorf->filter_name,
                    colorf->filter_text,
                    colorf->bg_color.red,
@@ -438,24 +724,24 @@ write_filter(gpointer filter_arg, gpointer data_arg)
 
 /* save filters in a filter file */
 static gboolean
-write_filters_file(FILE *f, gboolean only_marked)
+write_filters_file(GSList *cfl, FILE *f, gboolean only_selected)
 {
        struct write_filter_data data;
 
        data.f = f;
-       data.only_marked = only_marked;
+       data.only_selected = only_selected;
   
        fprintf(f,"# DO NOT EDIT THIS FILE!  It was created by Wireshark\n");
-        g_slist_foreach(color_filter_list, write_filter, &data);
+        g_slist_foreach(cfl, write_filter, &data);
        return TRUE;
 }
 
 /* save filters in users filter file */
 gboolean
-color_filters_write(void)
+color_filters_write(GSList *cfl)
 {
        gchar *pf_dir_path;
-       const gchar *path;
+       gchar *path;
        FILE *f;
 
        /* Create the directory that holds personal configuration files,
@@ -468,21 +754,23 @@ color_filters_write(void)
                return FALSE;
        }
 
-       path = get_persconffile_path("colorfilters", TRUE);
+       path = get_persconffile_path("colorfilters", TRUE, TRUE);
        if ((f = eth_fopen(path, "w+")) == NULL) {
                simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
                    "Could not open\n%s\nfor writing: %s.",
                    path, strerror(errno));
+               g_free(path);
                return FALSE;
        }
-       write_filters_file(f, FALSE);
+       g_free(path);
+       write_filters_file(cfl, f, FALSE);
        fclose(f);
        return TRUE;
 }
 
 /* save filters in some other filter file (export) */
 gboolean
-color_filters_export(gchar *path, gboolean only_marked)
+color_filters_export(gchar *path, GSList *cfl, gboolean only_marked)
 {
        FILE *f;
 
@@ -492,26 +780,8 @@ color_filters_export(gchar *path, gboolean only_marked)
                    path, strerror(errno));
                return FALSE;
        }
-       write_filters_file(f, only_marked);
+       write_filters_file(cfl, f, only_marked);
        fclose(f);
        return TRUE;
 }
 
-/* delete users filter file and reload global filters */
-gboolean
-color_filters_revert(void)
-{
-       gchar *path;
-
-       path = get_persconffile_path("colorfilters", TRUE);
-       if (!deletefile(path)) {
-               g_free(path);
-               return FALSE;
-       }
-
-       g_free(path);
-
-       /* Reload the (global) filters - Note: this does not update the dialog. */
-       color_filters_init();
-        return TRUE;
-}