From Xiao Xiangquan:
[obnox/wireshark/wip.git] / tap-stats_tree.c
index a09bf637b114edcbffbf0aa748ea1de7f9d204ec..e9568b306c42c3e039ce10ed1f510cc46ca8e117 100644 (file)
@@ -1,11 +1,11 @@
 /* tap-stats_tree.c
- * tethereal's tap implememntation of stats_tree
+ * tshark's tap implememntation of stats_tree
  * 2005, Luis E. G. Ontanon
  *
- * $Id$
+ * $Id$
  *
- * Ethereal - Network traffic analyzer
- * By Gerald Combs <gerald@ethereal.com>
+ * Wireshark - Network traffic analyzer
+ * By Gerald Combs <gerald@wireshark.org>
  * Copyright 1998 Gerald Combs
  * 
  * This program is free software; you can redistribute it and/or
 #include <stdio.h>
 #include <glib.h>
 #include <epan/stats_tree_priv.h>
+#include <epan/stat_cmd_args.h>
+#include <epan/report_err.h>
 
 /* actually unused */
 struct _st_node_pres {
-       voiddummy;
+       void *dummy;
 };
 
-
 struct _tree_pres {
-       guint8* init_string;    
+       void** dummy;
+};
+
+struct _tree_cfg_pres {
+       gchar *init_string;     
 };
 
-static void draw_stats_tree(void *psp) {
-       stats_tree *st = psp;
-       GString* s;
-       stat_node* child;
+static void
+draw_stats_tree(void *psp)
+{
+       stats_tree *st = (stats_tree *)psp;
+       GString *s;
+       gchar *fmt;
+       stat_node *child;
        
        s = g_string_new("\n===================================================================\n");
-       
-       g_string_sprintfa(s,"Statistics for %s\n===================================================================\n",
-                                        st->name);
+       fmt = g_strdup_printf(" %%s%%-%us%%12s\t%%12s\t%%12s\n",stats_tree_branch_max_namelen(&st->root,0));
+       g_string_append_printf(s,fmt,"",st->cfg->name,"value","rate","percent");
+       g_free(fmt);
+       g_string_append_printf(s,"-------------------------------------------------------------------\n");
        
        for (child = st->root.children; child; child = child->next ) {
-               stat_branch_to_str(child,s,1);
+               stats_tree_branch_to_str(child,s,0);
        }
        
        s = g_string_append(s,"\n===================================================================\n");
@@ -62,60 +71,75 @@ static void draw_stats_tree(void *psp) {
        
 }
 
-static void  init_stats_tree(char *optarg) {
-       guint8* abbr = get_st_abbr(optarg);
+static void
+init_stats_tree(const char *optarg, void *userdata _U_)
+{
+       char *abbr = stats_tree_get_abbr(optarg);
        GString *error_string;
+       stats_tree_cfg *cfg = NULL;
        stats_tree *st = NULL;
-
+       
        if (abbr) {
-               st = get_stats_tree_by_abbr(abbr);
-               
-               if (st != NULL) {
-                       if (strncmp (optarg, st->pr->init_string, strlen(st->pr->init_string)) == 0){
-                               st->filter=((guint8*)optarg)+strlen(st->pr->init_string);
+               cfg = stats_tree_get_cfg_by_abbr(abbr);
+
+               if (cfg != NULL) {
+                       if (strncmp (optarg, cfg->pr->init_string, strlen(cfg->pr->init_string)) == 0){
+                               st = stats_tree_new(cfg,NULL,optarg+strlen(cfg->pr->init_string));
                        } else {
-                               st->filter=NULL;
+                               report_failure("Wrong stats_tree (%s) found when looking at ->init_string",abbr);
+                               return;
                        }
                } else {
-                       g_error("no such stats_tree (%s) found in stats_tree registry",abbr);
+                       report_failure("no such stats_tree (%s) found in stats_tree registry",abbr);
+                       return;
                }
                
                g_free(abbr);
                
        } else {
-               g_error("could not obtain stats_tree abbr (%s) from optarg '%s'",abbr,optarg);          
+               report_failure("could not obtain stats_tree abbr (%s) from arg '%s'",abbr,optarg);              
+               return;
        }
        
-       error_string = register_tap_listener( st->tapname,
-                                                                                 st,
-                                                                                 st->filter,
-                                                                                 reset_stats_tree,
-                                                                                 stats_tree_packet,
-                                                                                 draw_stats_tree);
+       error_string = register_tap_listener(st->cfg->tapname,
+                                            st,
+                                            st->filter,
+                                            st->cfg->flags,
+                                            stats_tree_reset,
+                                            stats_tree_packet,
+                                            draw_stats_tree);
        
        if (error_string) {
-               g_error("stats_tree for: %s failed to attach to the tap: %s",st->name,error_string->str);
+               report_failure("stats_tree for: %s failed to attach to the tap: %s",cfg->name,error_string->str);
+               return;
        }
 
-       if (st->init) st->init(st);
+       if (cfg->init) cfg->init(st);
 
 }
 
-void register_stats_tree_tap (gpointer k _U_, gpointer v, gpointer p _U_) {
-       stats_tree* st = v;
+void
+register_stats_tree_tap (gpointer k _U_, gpointer v, gpointer p _U_)
+{
+       stats_tree_cfg *cfg = (stats_tree_cfg *)v;
        
-       st->pr = g_malloc(sizeof(tree_pres));
-       st->pr->init_string = g_strdup_printf("%s,tree",st->abbr);
+       cfg->pr = (tree_cfg_pres *)g_malloc(sizeof(tree_cfg_pres));
+       cfg->pr->init_string = g_strdup_printf("%s,tree", cfg->abbr);
 
-       register_ethereal_tap(st->pr->init_string, init_stats_tree);
+       register_stat_cmd_arg(cfg->pr->init_string, init_stats_tree, NULL);
        
 }
 
+static void
+free_tree_presentation(stats_tree *st)
+{
+       g_free(st->pr);
+}
+
 
 void
 register_tap_listener_stats_tree_stat(void)
 {
-       stats_tree_presentation(register_stats_tree_tap,
-                                                       NULL, NULL, NULL, NULL, NULL,
-                                                       NULL, NULL, NULL, NULL);
+       stats_tree_presentation(register_stats_tree_tap, NULL, NULL, NULL, NULL,
+                               NULL, free_tree_presentation, NULL, NULL, NULL);
 }