stat_tap_ui: remove leaks.
[metze/wireshark/wip.git] / epan / stat_tap_ui.c
1 /* stat_tap_ui.c
2  * Routines to register UI information for stats
3  *
4  * Wireshark - Network traffic analyzer
5  * By Gerald Combs <gerald@wireshark.org>
6  * Copyright 1998 Gerald Combs
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 #include "config.h"
24
25 #include <stdio.h>
26
27 #include <string.h>
28
29 #include <glib.h>
30
31 #include <epan/stat_tap_ui.h>
32
33 /* structure to keep track of what stats have registered command-line
34    arguments.
35  */
36 typedef struct _stat_cmd_arg {
37     stat_tap_ui *ui;
38     const char *cmd;
39     void (*func)(const char *arg, void* userdata);
40     void* userdata;
41 } stat_cmd_arg;
42
43 static wmem_tree_t *stat_cmd_arg_list=NULL;
44
45 /* structure to keep track of what stats have been specified on the
46    command line.
47  */
48 typedef struct {
49     stat_cmd_arg *sca;
50     char *arg;
51 } stat_requested;
52 static GSList *stats_requested = NULL;
53
54 /* **********************************************************************
55  * Function called from stat to register the stat's command-line argument
56  * and initialization routine
57  * ********************************************************************** */
58 void
59 register_stat_tap_ui(stat_tap_ui *ui, void *userdata)
60 {
61     stat_cmd_arg *newsca;
62
63     newsca = wmem_new(wmem_epan_scope(), stat_cmd_arg);
64     newsca->cmd= wmem_strdup(wmem_epan_scope(), ui->cli_string);
65     newsca->func=ui->tap_init_cb;
66     newsca->userdata=userdata;
67
68     if (stat_cmd_arg_list == NULL)
69         stat_cmd_arg_list = wmem_tree_new(wmem_epan_scope());
70
71     wmem_tree_insert_string(stat_cmd_arg_list, newsca->cmd, newsca, 0);
72 }
73
74 /* **********************************************************************
75  * Function called for a stat command-line argument
76  * ********************************************************************** */
77 static gboolean
78 process_stat_cmd_arg_func(const void *key, void *value, void *userdata)
79 {
80     char *optstr = (char*)userdata;
81     stat_cmd_arg *sca = (stat_cmd_arg*)value;
82     stat_requested *tr;
83
84     if (!strncmp((const char*)key, (const char*)optstr, strlen((const char*)key)))
85     {
86         tr=(stat_requested *)g_malloc(sizeof (stat_requested));
87         tr->sca = sca;
88         tr->arg=g_strdup(optstr);
89         stats_requested=g_slist_append(stats_requested, tr);
90         return TRUE;
91     }
92     return FALSE;
93 }
94
95 gboolean
96 process_stat_cmd_arg(char *optstr)
97 {
98     return wmem_tree_foreach(stat_cmd_arg_list, process_stat_cmd_arg_func, optstr);
99 }
100
101 /* **********************************************************************
102  * Function to list all possible tap command-line arguments
103  * ********************************************************************** */
104 static gboolean
105 list_stat_cmd_args_func(const void *key, void *value _U_, void *userdata _U_)
106 {
107     fprintf(stderr,"     %s\n", (const char*)key);
108     return FALSE;
109 }
110
111 void
112 list_stat_cmd_args(void)
113 {
114     wmem_tree_foreach(stat_cmd_arg_list, list_stat_cmd_args_func, NULL);
115 }
116
117 /* **********************************************************************
118  * Function to process stats requested with command-line arguments
119  * ********************************************************************** */
120 void
121 start_requested_stats(void)
122 {
123     stat_requested *sr;
124
125     while(stats_requested){
126         sr=(stat_requested *)stats_requested->data;
127         (*sr->sca->func)(sr->arg,sr->sca->userdata);
128         g_free(sr->arg);
129         g_free(sr);
130         stats_requested=g_slist_remove(stats_requested, sr);
131     }
132 }
133
134 static wmem_tree_t *registered_stat_tables = NULL;
135
136 void register_stat_tap_table_ui(stat_tap_table_ui *ui)
137 {
138     if (registered_stat_tables == NULL)
139         registered_stat_tables = wmem_tree_new(wmem_epan_scope());
140
141     wmem_tree_insert_string(registered_stat_tables, ui->cli_string, ui, 0);
142 }
143
144 void new_stat_tap_iterate_tables(wmem_foreach_func func, gpointer user_data)
145 {
146     wmem_tree_foreach(registered_stat_tables, func, user_data);
147 }
148
149 void new_stat_tap_get_filter(stat_tap_table_ui* new_stat, const char *opt_arg, const char **filter, char** err)
150 {
151     guint len = (guint) strlen(new_stat->cli_string);
152     *filter=NULL;
153     *err=NULL;
154
155     if (!strncmp(opt_arg, new_stat->cli_string, len))
156     {
157         if (opt_arg[len] == ',')
158         {
159            *filter = opt_arg + len+1;
160         }
161     }
162
163     if (new_stat->stat_filter_check_cb)
164         new_stat->stat_filter_check_cb(opt_arg, filter, err);
165 }
166
167 stat_tap_table* new_stat_tap_init_table(const char *name, int num_fields, int num_elements,
168                 const char *filter_string, new_stat_tap_gui_init_cb gui_callback, void* gui_data)
169 {
170     stat_tap_table* new_table = g_new0(stat_tap_table, 1);
171
172     new_table->title = name;
173     new_table->num_elements = num_elements;
174     new_table->num_fields = num_fields;
175     new_table->filter_string = filter_string;
176     new_table->elements = g_new0(stat_tap_table_item_type*, num_elements);
177
178     if (gui_callback)
179         gui_callback(new_table, gui_data);
180
181     return new_table;
182 }
183
184 void new_stat_tap_add_table(stat_tap_table_ui* new_stat, stat_tap_table* table)
185 {
186     if (new_stat->tables == NULL)
187         new_stat->tables = g_array_new(FALSE, TRUE, sizeof(stat_tap_table*));
188
189     g_array_insert_val(new_stat->tables, new_stat->tables->len, table);
190 }
191
192 void new_stat_tap_init_table_row(stat_tap_table *stat_table, guint table_index, guint num_fields, const stat_tap_table_item_type* fields)
193 {
194     /* we have discovered a new procedure. Extend the table accordingly */
195     if(table_index>=stat_table->num_elements){
196         guint old_num_elements=stat_table->num_elements;
197         guint i;
198
199         stat_table->num_elements=table_index+1;
200         stat_table->elements = (stat_tap_table_item_type**)g_realloc(stat_table->elements, sizeof(stat_tap_table_item_type*)*(stat_table->num_elements));
201         for(i=old_num_elements;i<stat_table->num_elements;i++){
202             stat_table->elements[i] = g_new0(stat_tap_table_item_type, stat_table->num_fields);
203         }
204     }
205     memcpy(stat_table->elements[table_index], fields, num_fields*sizeof(stat_tap_table_item_type));
206
207 }
208
209 stat_tap_table_item_type* new_stat_tap_get_field_data(const stat_tap_table *stat_table, guint table_index, guint field_index)
210 {
211     stat_tap_table_item_type* field_value;
212     g_assert(table_index < stat_table->num_elements);
213
214     field_value = stat_table->elements[table_index];
215
216     g_assert(field_index < stat_table->num_fields);
217
218     return &field_value[field_index];
219 }
220
221 void new_stat_tap_set_field_data(stat_tap_table *stat_table, guint table_index, guint field_index, stat_tap_table_item_type* field_data)
222 {
223     stat_tap_table_item_type* field_value;
224     g_assert(table_index < stat_table->num_elements);
225
226     field_value = stat_table->elements[table_index];
227
228     g_assert(field_index < stat_table->num_fields);
229
230     field_value[field_index] = *field_data;
231 }
232
233 void reset_stat_table(stat_tap_table_ui* new_stat, new_stat_tap_gui_reset_cb gui_callback, void *callback_data)
234 {
235     guint i = 0;
236     stat_tap_table *stat_table;
237
238     for (i = 0; i < new_stat->tables->len; i++)
239     {
240         stat_table = g_array_index(new_stat->tables, stat_tap_table*, i);
241
242         /* Give GUI the first crack at it before we clean up */
243         if (gui_callback)
244             gui_callback(stat_table, callback_data);
245
246         if (new_stat->stat_tap_reset_table_cb)
247             new_stat->stat_tap_reset_table_cb(stat_table);
248     }
249 }
250
251 void free_stat_tables(stat_tap_table_ui* new_stat, new_stat_tap_gui_free_cb gui_callback, void *callback_data)
252 {
253     guint i = 0, element, field_index;
254     stat_tap_table *stat_table;
255     stat_tap_table_item_type* field_data;
256
257     for (i = 0; i < new_stat->tables->len; i++)
258     {
259         stat_table = g_array_index(new_stat->tables, stat_tap_table*, i);
260
261         /* Give GUI the first crack at it before we clean up */
262         if (gui_callback)
263             gui_callback(stat_table, callback_data);
264
265         for (element = 0; element < stat_table->num_elements; element++)
266         {
267             for (field_index = 0; field_index < stat_table->num_fields; field_index++)
268             {
269                 field_data = new_stat_tap_get_field_data(stat_table, element, field_index);
270                 /* Give dissector a crack at it */
271                 /* XXX Should this be per-row instead? */
272                 if (new_stat->stat_tap_free_table_item_cb)
273                     new_stat->stat_tap_free_table_item_cb(stat_table, element, field_index, field_data);
274             }
275             g_free(stat_table->elements[element]);
276         }
277         g_free(stat_table->elements);
278         g_free(stat_table);
279     }
280     g_array_set_size(new_stat->tables, 0);
281 }
282
283
284 /*
285  * Editor modelines
286  *
287  * Local Variables:
288  * c-basic-offset: 4
289  * tab-width: 8
290  * indent-tabs-mode: nil
291  * End:
292  *
293  * ex: set shiftwidth=4 tabstop=8 expandtab:
294  * :indentSize=4:tabSize=8:noTabs=true:
295  */