1.3.6 -> 1.5.0
[obnox/wireshark/wip.git] / color_filters.c
index 24dee0aab9b8da0ee65a48a725588d078b343c3a..7d624c9404ce82563e8c77904da45dab3ebabc83 100644 (file)
 /*
  * Updated 1 Dec 10 jjm
  */
+
 #ifdef HAVE_CONFIG_H
 # include "config.h"
 #endif
 
 #include <glib.h>
+
+#include <stdio.h>
 #include <ctype.h>
 #include <string.h>
 
 #include <epan/filesystem.h>
-#include "file_util.h"
+#include <wsutil/file_util.h>
 
 #include <epan/packet.h>
 #include "color.h"
@@ -54,7 +56,7 @@ static gboolean read_users_filters(GSList **cfl);
 /* the currently active filters */
 static GSList *color_filter_list = NULL;
 
-/* keep "old" deleted filters in this list until 
+/* 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;
@@ -62,6 +64,10 @@ static GSList *color_filter_valid_list = NULL;
 /* Color Filters can en-/disabled. */
 gboolean filters_enabled = TRUE;
 
+/* 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 *
@@ -73,7 +79,7 @@ color_filter_new(const gchar *name,    /* The name of the filter to create */
 {
        color_filter_t *colorf;
 
-       colorf = g_malloc(sizeof (color_filter_t));
+       colorf = (color_filter_t *)g_malloc(sizeof (color_filter_t));
        colorf->filter_name = g_strdup(name);
        colorf->filter_text = g_strdup(filter_string);
        colorf->bg_color = *bg_color;
@@ -86,11 +92,10 @@ color_filter_new(const gchar *name,    /* The name of the filter to create */
 }
 
 /* Add ten empty (temporary) colorfilters for easy coloring */
-void
+static void
 color_filters_add_tmp(GSList **cfl)
 {
        gchar  *name = NULL;
-        gchar  *stockstr = NULL;
        guint32 i;
         gchar** bg_colors;
         gchar** fg_colors;
@@ -132,8 +137,8 @@ color_filters_add_tmp(GSList **cfl)
 static gint
 color_filters_find_by_name_cb(gconstpointer arg1, gconstpointer arg2)
 {
-        color_filter_t *colorf = (color_filter_t *)arg1;
-        gchar          *name   = (gchar *)arg2;
+        const color_filter_t *colorf = (const color_filter_t *)arg1;
+        const gchar          *name   = (const gchar *)arg2;
 
         return (strstr(colorf->filter_name, name)==NULL) ? -1 : 0 ;
 }
@@ -141,33 +146,70 @@ color_filters_find_by_name_cb(gconstpointer arg1, gconstpointer arg2)
 
 /* Set the filter off a temporary colorfilters and enable it */
 void
-color_filters_set_tmp(guint8 filt_nr, gchar *filter)
+color_filters_set_tmp(guint8 filt_nr, gchar *filter, gboolean disabled)
 {
-       gchar  *name = NULL;
-        GSList *cfl;
+       gchar          *name = NULL;
+       const gchar    *tmpfilter = NULL;
+        GSList         *cfl;
         color_filter_t *colorf;
         dfilter_t      *compiled_filter;
+       guint8         i;
 
-        name = g_strdup_printf("%s%02d",TEMP_COLOR_PREFIX,filt_nr);
-        cfl = g_slist_find_custom(color_filter_list, (gpointer *) name, color_filters_find_by_name_cb);
-        colorf = cfl->data;
-
-        if(colorf) {
-                if (!dfilter_compile(filter, &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(filter);
-                        colorf->c_colorfilter = compiled_filter;
-                        colorf->disabled = FALSE;
+        /* 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 = (color_filter_t *)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);
         }
-        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;
 }
 
@@ -188,7 +230,7 @@ color_filter_delete(color_filter_t *colorf)
 static void
 color_filter_delete_cb(gpointer filter_arg, gpointer unused _U_)
 {
-       color_filter_t *colorf = filter_arg;
+       color_filter_t *colorf = (color_filter_t *)filter_arg;
 
        color_filter_delete(colorf);
 }
@@ -208,7 +250,7 @@ color_filter_clone(color_filter_t *colorf)
 {
        color_filter_t *new_colorf;
 
-       new_colorf = g_malloc(sizeof (color_filter_t));
+       new_colorf = (color_filter_t *)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;
@@ -224,10 +266,10 @@ color_filter_clone(color_filter_t *colorf)
 static void
 color_filter_list_clone_cb(gpointer filter_arg, gpointer cfl_arg)
 {
-    gpointer *cfl = cfl_arg;
+    GSList **cfl = (GSList **)cfl_arg;
     color_filter_t *new_colorf;
 
-    new_colorf = color_filter_clone(filter_arg);
+    new_colorf = color_filter_clone((color_filter_t *)filter_arg);
     *cfl = g_slist_append(*cfl, new_colorf);
 }
 
@@ -249,6 +291,23 @@ color_filters_init(void)
        /* 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);
+}
+
+void
+color_filters_reload(void)
+{
+        /* "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);
 
@@ -268,7 +327,7 @@ color_filters_cleanup(void)
 static void
 color_filters_clone_cb(gpointer filter_arg, gpointer user_data)
 {
-       color_filter_t * new_colorf = color_filter_clone(filter_arg);
+       color_filter_t * new_colorf = color_filter_clone((color_filter_t *)filter_arg);
         color_filter_add_cb (new_colorf, user_data);
 }
 
@@ -282,7 +341,7 @@ color_filters_clone(gpointer user_data)
 static void
 color_filter_compile_cb(gpointer filter_arg, gpointer unused _U_)
 {
-       color_filter_t *colorf = filter_arg;
+       color_filter_t *colorf = (color_filter_t *)filter_arg;
 
        g_assert(colorf->c_colorfilter == NULL);
        if (!dfilter_compile(colorf->filter_text, &colorf->c_colorfilter)) {
@@ -298,7 +357,7 @@ color_filter_compile_cb(gpointer filter_arg, gpointer unused _U_)
 static void
 color_filter_validate_cb(gpointer filter_arg, gpointer unused _U_)
 {
-       color_filter_t *colorf = filter_arg;
+       color_filter_t *colorf = (color_filter_t *)filter_arg;
 
        g_assert(colorf->c_colorfilter == NULL);
        if (!dfilter_compile(colorf->filter_text, &colorf->c_colorfilter)) {
@@ -323,7 +382,7 @@ color_filters_apply(GSList *tmp_cfl, GSList *edit_cfl)
         /* 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_valid_list = g_slist_concat(color_filter_valid_list,
                                                  color_filter_list_clone(edit_cfl) );
 
         /* compile all filter */
@@ -336,16 +395,26 @@ color_filters_apply(GSList *tmp_cfl, GSList *edit_cfl)
         g_slist_foreach(color_filter_list, color_filter_compile_cb, NULL);
 }
 
-gboolean 
+gboolean
 color_filters_used(void)
 {
         return color_filter_list != NULL && filters_enabled;
 }
 
+gboolean
+tmp_color_filters_used(void)
+{
+        return tmp_colors_set;
+}
+
 void
 color_filters_enable(gboolean enable)
 {
+#ifdef NEW_PACKET_LIST
+       new_packet_list_enable_color(enable);
+#else
         filters_enabled = enable;
+#endif
 }
 
 
@@ -353,8 +422,8 @@ color_filters_enable(gboolean enable)
 static void
 prime_edt(gpointer data, gpointer user_data)
 {
-       color_filter_t  *colorf = data;
-       epan_dissect_t   *edt = user_data;
+       color_filter_t  *colorf = (color_filter_t *)data;
+       epan_dissect_t   *edt = (epan_dissect_t *)user_data;
 
        if (colorf->c_colorfilter != NULL)
                epan_dissect_prime_dfilter(edt, colorf->c_colorfilter);
@@ -365,12 +434,19 @@ prime_edt(gpointer data, gpointer user_data)
 void
 color_filters_prime_edt(epan_dissect_t *edt)
 {
-       g_slist_foreach(color_filter_list, prime_edt, edt);
+       if (color_filters_used())
+               g_slist_foreach(color_filter_list, prime_edt, edt);
 }
 
-/* Colorize a single packet of the packet list */
-color_filter_t *
+/* Colorize a single packet of the packet list (old packet list)
+ *
+ * Return the color_t for later use (new packet list) */
+const color_filter_t *
+#ifdef NEW_PACKET_LIST
+color_filters_colorize_packet(epan_dissect_t *edt)
+#else
 color_filters_colorize_packet(gint row, epan_dissect_t *edt)
+#endif
 {
     GSList *curr;
     color_filter_t *colorf;
@@ -380,12 +456,15 @@ color_filters_colorize_packet(gint row, epan_dissect_t *edt)
         curr = color_filter_list;
 
         while(curr != NULL) {
-            colorf = curr->data;
+            colorf = (color_filter_t *)curr->data;
             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 */
+#ifndef NEW_PACKET_LIST
+                   /* We'll do this in the column cell function instead. */
                     packet_list_set_colors(row, &(colorf->fg_color), &(colorf->bg_color));
+#endif
                     return colorf;
             }
             curr = g_slist_next(curr);
@@ -412,8 +491,8 @@ read_filters_file(FILE *f, gpointer user_data)
        gboolean disabled = FALSE;
        gboolean skip_end_of_line = FALSE;
 
-       name = g_malloc(name_len + 1);
-       filter_exp = g_malloc(filter_exp_len + 1);
+       name = (gchar *)g_malloc(name_len + 1);
+       filter_exp = (gchar *)g_malloc(filter_exp_len + 1);
 
        while (1) {
 
@@ -442,7 +521,7 @@ read_filters_file(FILE *f, gpointer user_data)
                }
 
                /* skip # comments and invalid lines */
-               if (c != '@') { 
+               if (c != '@') {
                        skip_end_of_line = TRUE;
                        continue;
                }
@@ -461,9 +540,9 @@ read_filters_file(FILE *f, gpointer user_data)
                        if (i >= name_len) {
                                /* buffer isn't long enough; double its length.*/
                                name_len *= 2;
-                               name = g_realloc(name, name_len + 1);
+                               name = (gchar *)g_realloc(name, name_len + 1);
                        }
-                       name[i++] = c;            
+                       name[i++] = c;
                }
                name[i] = '\0';
 
@@ -483,7 +562,7 @@ read_filters_file(FILE *f, gpointer user_data)
                        if (i >= filter_exp_len) {
                                /* buffer isn't long enough; double its length.*/
                                filter_exp_len *= 2;
-                               filter_exp = g_realloc(filter_exp, filter_exp_len + 1);
+                               filter_exp = (gchar *)g_realloc(filter_exp, filter_exp_len + 1);
                        }
                        filter_exp[i++] = c;
                }
@@ -566,8 +645,8 @@ read_users_filters(GSList **cfl)
        gboolean ret;
 
        /* decide what file to open (from dfilter code) */
-       path = get_persconffile_path("colorfilters", FALSE);
-       if ((f = eth_fopen(path, "r")) == NULL) {
+       path = get_persconffile_path("colorfilters", TRUE, FALSE);
+       if ((f = ws_fopen(path, "r")) == NULL) {
                if (errno != ENOENT) {
                        simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
                            "Could not open filter file\n\"%s\": %s.", path,
@@ -594,7 +673,7 @@ color_filters_read_globals(gpointer user_data)
 
        /* decide what file to open (from dfilter code) */
        path = get_datafile_path("colorfilters");
-       if ((f = eth_fopen(path, "r")) == NULL) {
+       if ((f = ws_fopen(path, "r")) == NULL) {
                if (errno != ENOENT) {
                        simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
                            "Could not open global filter file\n\"%s\": %s.", path,
@@ -618,7 +697,7 @@ color_filters_import(gchar *path, gpointer user_data)
        FILE *f;
        gboolean ret;
 
-       if ((f = eth_fopen(path, "r")) == NULL) {
+       if ((f = ws_fopen(path, "r")) == NULL) {
                simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
                    "Could not open\n%s\nfor reading: %s.",
                    path, strerror(errno));
@@ -640,8 +719,8 @@ struct write_filter_data
 static void
 write_filter(gpointer filter_arg, gpointer data_arg)
 {
-       struct write_filter_data *data = data_arg;
-       color_filter_t *colorf = filter_arg;
+       struct write_filter_data *data = (struct write_filter_data *)data_arg;
+       color_filter_t *colorf = (color_filter_t *)filter_arg;
        FILE *f = data->f;
 
        if ( (colorf->selected || !data->only_selected) &&
@@ -667,7 +746,7 @@ write_filters_file(GSList *cfl, FILE *f, gboolean only_selected)
 
        data.f = f;
        data.only_selected = only_selected;
-  
+
        fprintf(f,"# DO NOT EDIT THIS FILE!  It was created by Wireshark\n");
         g_slist_foreach(cfl, write_filter, &data);
        return TRUE;
@@ -678,7 +757,7 @@ gboolean
 color_filters_write(GSList *cfl)
 {
        gchar *pf_dir_path;
-       const gchar *path;
+       gchar *path;
        FILE *f;
 
        /* Create the directory that holds personal configuration files,
@@ -691,13 +770,15 @@ color_filters_write(GSList *cfl)
                return FALSE;
        }
 
-       path = get_persconffile_path("colorfilters", TRUE);
-       if ((f = eth_fopen(path, "w+")) == NULL) {
+       path = get_persconffile_path("colorfilters", TRUE, TRUE);
+       if ((f = ws_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;
        }
+       g_free(path);
        write_filters_file(cfl, f, FALSE);
        fclose(f);
        return TRUE;
@@ -709,7 +790,7 @@ color_filters_export(gchar *path, GSList *cfl, gboolean only_marked)
 {
        FILE *f;
 
-       if ((f = eth_fopen(path, "w+")) == NULL) {
+       if ((f = ws_fopen(path, "w+")) == NULL) {
                simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
                    "Could not open\n%s\nfor writing: %s.",
                    path, strerror(errno));