Memory leak in lemon.c
[obnox/wireshark/wip.git] / tap-stats_tree.c
1 /* tap-stats_tree.c
2  * tshark's tap implememntation of stats_tree
3  * 2005, Luis E. G. Ontanon
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  * 
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  * 
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  * 
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include <string.h>
31 #include <stdio.h>
32 #include <glib.h>
33 #include <epan/stats_tree_priv.h>
34 #include <epan/stat_cmd_args.h>
35 #include <epan/report_err.h>
36
37 /* actually unused */
38 struct _st_node_pres {
39         void *dummy;
40 };
41
42 struct _tree_pres {
43         void** dummy;
44 };
45
46 struct _tree_cfg_pres {
47         gchar *init_string;     
48 };
49
50 static void
51 draw_stats_tree(void *psp)
52 {
53         stats_tree *st = (stats_tree *)psp;
54         GString *s;
55         gchar *fmt;
56         stat_node *child;
57         
58         s = g_string_new("\n===================================================================\n");
59         fmt = g_strdup_printf(" %%s%%-%us%%12s\t%%12s\t%%12s\n",stats_tree_branch_max_namelen(&st->root,0));
60         g_string_append_printf(s,fmt,"",st->cfg->name,"value","rate","percent");
61         g_free(fmt);
62         g_string_append_printf(s,"-------------------------------------------------------------------\n");
63         
64         for (child = st->root.children; child; child = child->next ) {
65                 stats_tree_branch_to_str(child,s,0);
66         }
67         
68         s = g_string_append(s,"\n===================================================================\n");
69         
70         printf("%s",s->str);
71         
72 }
73
74 static void
75 init_stats_tree(const char *optarg, void *userdata _U_)
76 {
77         char *abbr = stats_tree_get_abbr(optarg);
78         GString *error_string;
79         stats_tree_cfg *cfg = NULL;
80         stats_tree *st = NULL;
81         
82         if (abbr) {
83                 cfg = stats_tree_get_cfg_by_abbr(abbr);
84
85                 if (cfg != NULL) {
86                         if (strncmp (optarg, cfg->pr->init_string, strlen(cfg->pr->init_string)) == 0){
87                                 st = stats_tree_new(cfg,NULL,optarg+strlen(cfg->pr->init_string));
88                         } else {
89                                 report_failure("Wrong stats_tree (%s) found when looking at ->init_string",abbr);
90                                 return;
91                         }
92                 } else {
93                         report_failure("no such stats_tree (%s) found in stats_tree registry",abbr);
94                         return;
95                 }
96                 
97                 g_free(abbr);
98                 
99         } else {
100                 report_failure("could not obtain stats_tree abbr (%s) from arg '%s'",abbr,optarg);              
101                 return;
102         }
103         
104         error_string = register_tap_listener(st->cfg->tapname,
105                                              st,
106                                              st->filter,
107                                              st->cfg->flags,
108                                              stats_tree_reset,
109                                              stats_tree_packet,
110                                              draw_stats_tree);
111         
112         if (error_string) {
113                 report_failure("stats_tree for: %s failed to attach to the tap: %s",cfg->name,error_string->str);
114                 return;
115         }
116
117         if (cfg->init) cfg->init(st);
118
119 }
120
121 void
122 register_stats_tree_tap (gpointer k _U_, gpointer v, gpointer p _U_)
123 {
124         stats_tree_cfg *cfg = (stats_tree_cfg *)v;
125         
126         cfg->pr = (tree_cfg_pres *)g_malloc(sizeof(tree_cfg_pres));
127         cfg->pr->init_string = g_strdup_printf("%s,tree", cfg->abbr);
128
129         register_stat_cmd_arg(cfg->pr->init_string, init_stats_tree, NULL);
130         
131 }
132
133 static void
134 free_tree_presentation(stats_tree *st)
135 {
136         g_free(st->pr);
137 }
138
139
140 void
141 register_tap_listener_stats_tree_stat(void)
142 {
143         stats_tree_presentation(register_stats_tree_tap, NULL, NULL, NULL, NULL,
144                                 NULL, free_tree_presentation, NULL, NULL, NULL);
145 }