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 GList *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 sort_by_name(gconstpointer a, gconstpointer b)
61 return strcmp(((const stat_cmd_arg *)a)->cmd, ((const stat_cmd_arg *)b)->cmd);
65 register_stat_tap_ui(stat_tap_ui *ui, void *userdata)
69 newsca=(stat_cmd_arg *)g_malloc(sizeof(stat_cmd_arg));
70 newsca->cmd=ui->cli_string;
71 newsca->func=ui->tap_init_cb;
72 newsca->userdata=userdata;
73 stat_cmd_arg_list=g_list_insert_sorted(stat_cmd_arg_list, newsca, sort_by_name);
76 /* **********************************************************************
77 * Function called for a stat command-line argument
78 * ********************************************************************** */
80 process_stat_cmd_arg(char *optstr)
86 for(entry=g_list_last(stat_cmd_arg_list);entry;entry=g_list_previous(entry)){
87 sca=(stat_cmd_arg *)entry->data;
88 if(!strncmp(sca->cmd,optstr,strlen(sca->cmd))){
89 tr=(stat_requested *)g_malloc(sizeof (stat_requested));
91 tr->arg=g_strdup(optstr);
92 stats_requested=g_slist_append(stats_requested, tr);
99 /* **********************************************************************
100 * Function to list all possible tap command-line arguments
101 * ********************************************************************** */
103 list_stat_cmd_args(void)
108 for(entry=stat_cmd_arg_list;entry;entry=g_list_next(entry)){
109 sca=(stat_cmd_arg *)entry->data;
110 fprintf(stderr," %s\n",sca->cmd);
114 /* **********************************************************************
115 * Function to process stats requested with command-line arguments
116 * ********************************************************************** */
118 start_requested_stats(void)
122 while(stats_requested){
123 sr=(stat_requested *)stats_requested->data;
124 (*sr->sca->func)(sr->arg,sr->sca->userdata);
127 stats_requested=g_slist_remove(stats_requested, sr);
131 static GSList *registered_stat_tables = NULL;
134 insert_sorted_by_cli_string(gconstpointer aparam, gconstpointer bparam)
136 const new_stat_tap_ui *a = (const new_stat_tap_ui *)aparam;
137 const new_stat_tap_ui *b = (const new_stat_tap_ui *)bparam;
139 return g_ascii_strcasecmp(a->cli_string, b->cli_string);
142 void register_new_stat_tap_ui(new_stat_tap_ui *ui)
144 registered_stat_tables = g_slist_insert_sorted(registered_stat_tables, ui, insert_sorted_by_cli_string);
147 void new_stat_tap_iterate_tables(GFunc func, gpointer user_data)
149 g_slist_foreach(registered_stat_tables, func, user_data);
152 void new_stat_tap_get_filter(new_stat_tap_ui* new_stat, const char *opt_arg, const char **filter, char** err)
154 guint len = (guint) strlen(new_stat->cli_string);
158 if (!strncmp(opt_arg, new_stat->cli_string, len))
160 if (opt_arg[len] == ',')
162 *filter = opt_arg + len+1;
166 if (new_stat->new_stat_filter_check_cb)
167 new_stat->new_stat_filter_check_cb(opt_arg, filter, err);
170 new_stat_tap_table* new_stat_tap_init_table(const char *name, int num_fields, int num_elements,
171 const char *filter_string, new_stat_tap_gui_init_cb gui_callback, void* gui_data)
173 new_stat_tap_table* new_table = g_new0(new_stat_tap_table, 1);
175 new_table->title = name;
176 new_table->num_elements = num_elements;
177 new_table->num_fields = num_fields;
178 new_table->filter_string = filter_string;
179 new_table->elements = g_new0(stat_tap_table_item_type*, num_elements);
182 gui_callback(new_table, gui_data);
187 void new_stat_tap_add_table(new_stat_tap_ui* new_stat, new_stat_tap_table* table)
189 if (new_stat->tables == NULL)
190 new_stat->tables = g_array_new(FALSE, TRUE, sizeof(new_stat_tap_table*));
192 g_array_insert_val(new_stat->tables, new_stat->tables->len, table);
195 void new_stat_tap_init_table_row(new_stat_tap_table *stat_table, guint table_index, guint num_fields, const stat_tap_table_item_type* fields)
197 /* we have discovered a new procedure. Extend the table accordingly */
198 if(table_index>=stat_table->num_elements){
199 guint old_num_elements=stat_table->num_elements;
202 stat_table->num_elements=table_index+1;
203 stat_table->elements = (stat_tap_table_item_type**)g_realloc(stat_table->elements, sizeof(stat_tap_table_item_type*)*(stat_table->num_elements));
204 for(i=old_num_elements;i<stat_table->num_elements;i++){
205 stat_table->elements[i] = g_new0(stat_tap_table_item_type, stat_table->num_fields);
208 memcpy(stat_table->elements[table_index], fields, num_fields*sizeof(stat_tap_table_item_type));
212 stat_tap_table_item_type* new_stat_tap_get_field_data(const new_stat_tap_table *stat_table, guint table_index, guint field_index)
214 stat_tap_table_item_type* field_value;
215 g_assert(table_index < stat_table->num_elements);
217 field_value = stat_table->elements[table_index];
219 g_assert(field_index < stat_table->num_fields);
221 return &field_value[field_index];
224 void new_stat_tap_set_field_data(new_stat_tap_table *stat_table, guint table_index, guint field_index, stat_tap_table_item_type* field_data)
226 stat_tap_table_item_type* field_value;
227 g_assert(table_index < stat_table->num_elements);
229 field_value = stat_table->elements[table_index];
231 g_assert(field_index < stat_table->num_fields);
233 field_value[field_index] = *field_data;
236 void reset_stat_table(new_stat_tap_ui* new_stat, new_stat_tap_gui_reset_cb gui_callback, void *callback_data)
239 new_stat_tap_table *stat_table;
241 for (i = 0; i < new_stat->tables->len; i++)
243 stat_table = g_array_index(new_stat->tables, new_stat_tap_table*, i);
245 /* Give GUI the first crack at it before we clean up */
247 gui_callback(stat_table, callback_data);
249 if (new_stat->stat_tap_reset_table_cb)
250 new_stat->stat_tap_reset_table_cb(stat_table);
254 void free_stat_tables(new_stat_tap_ui* new_stat, new_stat_tap_gui_free_cb gui_callback, void *callback_data)
256 guint i = 0, element, field_index;
257 new_stat_tap_table *stat_table;
258 stat_tap_table_item_type* field_data;
260 for (i = 0; i < new_stat->tables->len; i++)
262 stat_table = g_array_index(new_stat->tables, new_stat_tap_table*, i);
264 /* Give GUI the first crack at it before we clean up */
266 gui_callback(stat_table, callback_data);
268 for (element = 0; element < stat_table->num_elements; element++)
270 for (field_index = 0; field_index < stat_table->num_fields; field_index++)
272 field_data = new_stat_tap_get_field_data(stat_table, element, field_index);
273 /* Give dissector a crack at it */
274 /* XXX Should this be per-row instead? */
275 if (new_stat->stat_tap_free_table_item_cb)
276 new_stat->stat_tap_free_table_item_cb(stat_table, element, field_index, field_data);
278 g_free(stat_table->elements[element]);
280 g_free(stat_table->elements);
283 g_array_set_size(new_stat->tables, 0);
293 * indent-tabs-mode: nil
296 * ex: set shiftwidth=4 tabstop=8 expandtab:
297 * :indentSize=4:tabSize=8:noTabs=true: