2 * Routines to register UI information for stats
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
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.
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.
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.
31 #include <epan/stat_tap_ui.h>
33 /* structure to keep track of what stats have registered command-line
36 typedef struct _stat_cmd_arg {
39 void (*func)(const char *arg, void* userdata);
43 static wmem_tree_t *stat_cmd_arg_list=NULL;
45 /* structure to keep track of what stats have been specified on the
52 static GSList *stats_requested = NULL;
54 /* **********************************************************************
55 * Function called from stat to register the stat's command-line argument
56 * and initialization routine
57 * ********************************************************************** */
59 register_stat_tap_ui(stat_tap_ui *ui, void *userdata)
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;
68 if (stat_cmd_arg_list == NULL)
69 stat_cmd_arg_list = wmem_tree_new(wmem_epan_scope());
71 wmem_tree_insert_string(stat_cmd_arg_list, newsca->cmd, newsca, 0);
74 /* **********************************************************************
75 * Function called for a stat command-line argument
76 * ********************************************************************** */
78 process_stat_cmd_arg_func(const void *key, void *value, void *userdata)
80 char *optstr = (char*)userdata;
81 stat_cmd_arg *sca = (stat_cmd_arg*)value;
84 if (!strncmp((const char*)key, (const char*)optstr, strlen((const char*)key)))
86 tr=(stat_requested *)g_malloc(sizeof (stat_requested));
88 tr->arg=g_strdup(optstr);
89 stats_requested=g_slist_append(stats_requested, tr);
96 process_stat_cmd_arg(char *optstr)
98 return wmem_tree_foreach(stat_cmd_arg_list, process_stat_cmd_arg_func, optstr);
101 /* **********************************************************************
102 * Function to list all possible tap command-line arguments
103 * ********************************************************************** */
105 list_stat_cmd_args_func(const void *key, void *value _U_, void *userdata _U_)
107 fprintf(stderr," %s\n", (const char*)key);
112 list_stat_cmd_args(void)
114 wmem_tree_foreach(stat_cmd_arg_list, list_stat_cmd_args_func, NULL);
117 /* **********************************************************************
118 * Function to process stats requested with command-line arguments
119 * ********************************************************************** */
121 start_requested_stats(void)
125 while(stats_requested){
126 sr=(stat_requested *)stats_requested->data;
127 (*sr->sca->func)(sr->arg,sr->sca->userdata);
130 stats_requested=g_slist_remove(stats_requested, sr);
134 static wmem_tree_t *registered_stat_tables = NULL;
136 void register_stat_tap_table_ui(stat_tap_table_ui *ui)
138 if (registered_stat_tables == NULL)
139 registered_stat_tables = wmem_tree_new(wmem_epan_scope());
141 wmem_tree_insert_string(registered_stat_tables, ui->cli_string, ui, 0);
144 void new_stat_tap_iterate_tables(wmem_foreach_func func, gpointer user_data)
146 wmem_tree_foreach(registered_stat_tables, func, user_data);
149 void new_stat_tap_get_filter(stat_tap_table_ui* new_stat, const char *opt_arg, const char **filter, char** err)
151 guint len = (guint) strlen(new_stat->cli_string);
155 if (!strncmp(opt_arg, new_stat->cli_string, len))
157 if (opt_arg[len] == ',')
159 *filter = opt_arg + len+1;
163 if (new_stat->stat_filter_check_cb)
164 new_stat->stat_filter_check_cb(opt_arg, filter, err);
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)
170 stat_tap_table* new_table = g_new0(stat_tap_table, 1);
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);
179 gui_callback(new_table, gui_data);
184 void new_stat_tap_add_table(stat_tap_table_ui* new_stat, stat_tap_table* table)
186 if (new_stat->tables == NULL)
187 new_stat->tables = g_array_new(FALSE, TRUE, sizeof(stat_tap_table*));
189 g_array_insert_val(new_stat->tables, new_stat->tables->len, table);
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)
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;
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);
205 memcpy(stat_table->elements[table_index], fields, num_fields*sizeof(stat_tap_table_item_type));
209 stat_tap_table_item_type* new_stat_tap_get_field_data(const stat_tap_table *stat_table, guint table_index, guint field_index)
211 stat_tap_table_item_type* field_value;
212 g_assert(table_index < stat_table->num_elements);
214 field_value = stat_table->elements[table_index];
216 g_assert(field_index < stat_table->num_fields);
218 return &field_value[field_index];
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)
223 stat_tap_table_item_type* field_value;
224 g_assert(table_index < stat_table->num_elements);
226 field_value = stat_table->elements[table_index];
228 g_assert(field_index < stat_table->num_fields);
230 field_value[field_index] = *field_data;
233 void reset_stat_table(stat_tap_table_ui* new_stat, new_stat_tap_gui_reset_cb gui_callback, void *callback_data)
236 stat_tap_table *stat_table;
238 for (i = 0; i < new_stat->tables->len; i++)
240 stat_table = g_array_index(new_stat->tables, stat_tap_table*, i);
242 /* Give GUI the first crack at it before we clean up */
244 gui_callback(stat_table, callback_data);
246 if (new_stat->stat_tap_reset_table_cb)
247 new_stat->stat_tap_reset_table_cb(stat_table);
251 void free_stat_tables(stat_tap_table_ui* new_stat, new_stat_tap_gui_free_cb gui_callback, void *callback_data)
253 guint i = 0, element, field_index;
254 stat_tap_table *stat_table;
255 stat_tap_table_item_type* field_data;
257 for (i = 0; i < new_stat->tables->len; i++)
259 stat_table = g_array_index(new_stat->tables, stat_tap_table*, i);
261 /* Give GUI the first crack at it before we clean up */
263 gui_callback(stat_table, callback_data);
265 for (element = 0; element < stat_table->num_elements; element++)
267 for (field_index = 0; field_index < stat_table->num_fields; field_index++)
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);
275 g_free(stat_table->elements[element]);
277 g_free(stat_table->elements);
280 g_array_set_size(new_stat->tables, 0);
290 * indent-tabs-mode: nil
293 * ex: set shiftwidth=4 tabstop=8 expandtab:
294 * :indentSize=4:tabSize=8:noTabs=true: