(Temporarily) Allow DCE/RPC dissector table to have duplicates.
[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 GList *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 static gint
59 sort_by_name(gconstpointer a, gconstpointer b)
60 {
61     return strcmp(((const stat_cmd_arg *)a)->cmd, ((const stat_cmd_arg *)b)->cmd);
62 }
63
64 void
65 register_stat_tap_ui(stat_tap_ui *ui, void *userdata)
66 {
67     stat_cmd_arg *newsca;
68
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);
74 }
75
76 /* **********************************************************************
77  * Function called for a stat command-line argument
78  * ********************************************************************** */
79 gboolean
80 process_stat_cmd_arg(char *optstr)
81 {
82     GList *entry;
83     stat_cmd_arg *sca;
84     stat_requested *tr;
85
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));
90             tr->sca = sca;
91             tr->arg=g_strdup(optstr);
92             stats_requested=g_slist_append(stats_requested, tr);
93             return TRUE;
94         }
95     }
96     return FALSE;
97 }
98
99 /* **********************************************************************
100  * Function to list all possible tap command-line arguments
101  * ********************************************************************** */
102 void
103 list_stat_cmd_args(void)
104 {
105     GList *entry;
106     stat_cmd_arg *sca;
107
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);
111     }
112 }
113
114 /* **********************************************************************
115  * Function to process stats requested with command-line arguments
116  * ********************************************************************** */
117 void
118 start_requested_stats(void)
119 {
120     stat_requested *sr;
121
122     while(stats_requested){
123         sr=(stat_requested *)stats_requested->data;
124         (*sr->sca->func)(sr->arg,sr->sca->userdata);
125         g_free(sr->arg);
126         g_free(sr);
127         stats_requested=g_slist_remove(stats_requested, sr);
128     }
129 }
130
131 static GSList *registered_stat_tables = NULL;
132
133 static gint
134 insert_sorted_by_cli_string(gconstpointer aparam, gconstpointer bparam)
135 {
136     const new_stat_tap_ui *a = (new_stat_tap_ui *)aparam;
137     const new_stat_tap_ui *b = (new_stat_tap_ui *)bparam;
138
139     return g_ascii_strcasecmp(a->cli_string, b->cli_string);
140 }
141
142 void register_new_stat_tap_ui(new_stat_tap_ui *ui)
143 {
144     registered_stat_tables = g_slist_insert_sorted(registered_stat_tables, ui, insert_sorted_by_cli_string);
145 }
146
147 void new_stat_tap_iterate_tables(GFunc func, gpointer user_data)
148 {
149     g_slist_foreach(registered_stat_tables, func, user_data);
150 }
151
152 void new_stat_tap_get_filter(new_stat_tap_ui* new_stat, const char *opt_arg, const char **filter, char** err)
153 {
154     guint len = (guint) strlen(new_stat->cli_string);
155     *filter=NULL;
156     *err=NULL;
157
158     if (!strncmp(opt_arg, new_stat->cli_string, len))
159     {
160         if (opt_arg[len] == ',')
161         {
162            *filter = opt_arg + len+1;
163         }
164     }
165
166     if (new_stat->new_stat_filter_check_cb)
167         new_stat->new_stat_filter_check_cb(opt_arg, filter, err);
168 }
169
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)
172 {
173     new_stat_tap_table* new_table = g_new0(new_stat_tap_table, 1);
174
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);
180
181     if (gui_callback)
182         gui_callback(new_table, gui_data);
183
184     return new_table;
185 }
186
187 void new_stat_tap_add_table(new_stat_tap_ui* new_stat, new_stat_tap_table* table)
188 {
189     if (new_stat->tables == NULL)
190         new_stat->tables = g_array_new(FALSE, TRUE, sizeof(new_stat_tap_table*));
191
192     g_array_insert_val(new_stat->tables, new_stat->tables->len, table);
193 }
194
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)
196 {
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;
200         guint i;
201
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);
206         }
207     }
208     memcpy(stat_table->elements[table_index], fields, num_fields*sizeof(stat_tap_table_item_type));
209
210 }
211
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)
213 {
214     stat_tap_table_item_type* field_value;
215     g_assert(table_index < stat_table->num_elements);
216
217     field_value = stat_table->elements[table_index];
218
219     g_assert(field_index < stat_table->num_fields);
220
221     return &field_value[field_index];
222 }
223
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)
225 {
226     stat_tap_table_item_type* field_value;
227     g_assert(table_index < stat_table->num_elements);
228
229     field_value = stat_table->elements[table_index];
230
231     g_assert(field_index < stat_table->num_fields);
232
233     field_value[field_index] = *field_data;
234 }
235
236 void reset_stat_table(new_stat_tap_ui* new_stat, new_stat_tap_gui_reset_cb gui_callback, void *callback_data)
237 {
238     guint i = 0;
239     new_stat_tap_table *stat_table;
240
241     for (i = 0; i < new_stat->tables->len; i++)
242     {
243         stat_table = g_array_index(new_stat->tables, new_stat_tap_table*, i);
244
245         /* Give GUI the first crack at it before we clean up */
246         if (gui_callback)
247             gui_callback(stat_table, callback_data);
248
249         if (new_stat->stat_tap_reset_table_cb)
250             new_stat->stat_tap_reset_table_cb(stat_table);
251     }
252 }
253
254 void free_stat_tables(new_stat_tap_ui* new_stat, new_stat_tap_gui_free_cb gui_callback, void *callback_data)
255 {
256     guint i = 0, element, field_index;
257     new_stat_tap_table *stat_table;
258     stat_tap_table_item_type* field_data;
259
260     for (i = 0; i < new_stat->tables->len; i++)
261     {
262         stat_table = g_array_index(new_stat->tables, new_stat_tap_table*, i);
263
264         /* Give GUI the first crack at it before we clean up */
265         if (gui_callback)
266             gui_callback(stat_table, callback_data);
267
268         for (element = 0; element < stat_table->num_elements; element++)
269         {
270             for (field_index = 0; field_index < stat_table->num_fields; field_index++)
271             {
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);
277             }
278             g_free(stat_table->elements[element]);
279         }
280         g_free(stat_table->elements);
281         g_free(stat_table);
282     }
283     g_array_set_size(new_stat->tables, 0);
284 }
285
286
287 /*
288  * Editor modelines
289  *
290  * Local Variables:
291  * c-basic-offset: 4
292  * tab-width: 8
293  * indent-tabs-mode: nil
294  * End:
295  *
296  * ex: set shiftwidth=4 tabstop=8 expandtab:
297  * :indentSize=4:tabSize=8:noTabs=true:
298  */