Add casts to fix mac buildbots.
[metze/wireshark/wip.git] / epan / stat_cmd_args.c
1 /* stat_cmd_args.c
2  * Routines to register "-z" command-line argument handlers for stats
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24
25 #include "config.h"
26
27 #include <stdio.h>
28
29 #include <string.h>
30
31 #include <glib.h>
32
33 #include <epan/stat_cmd_args.h>
34
35 /* structure to keep track of what stats have registered command-line
36    arguments.
37  */
38 typedef struct _stat_cmd_arg {
39         const char *cmd;
40         void (*func)(const char *arg, void* userdata);
41     void* userdata;
42 } stat_cmd_arg;
43
44 static GSList *stat_cmd_arg_list=NULL;
45
46 /* structure to keep track of what stats have been specified on the
47    command line.
48  */
49 typedef struct {
50         stat_cmd_arg *sca;
51         char *arg;
52 } stat_requested;
53 static GSList *stats_requested = NULL;
54
55 /* **********************************************************************
56  * Function called from stat to register the stat's command-line argument
57  * and initialization routine
58  * ********************************************************************** */
59 static gint
60 sort_by_name(gconstpointer a, gconstpointer b)
61 {
62         return strcmp(((const stat_cmd_arg *)a)->cmd,
63             ((const stat_cmd_arg *)b)->cmd);
64 }
65 void
66 register_stat_cmd_arg(const char *cmd, void (*func)(const char*, void*),void* userdata)
67 {
68         stat_cmd_arg *newsca;
69
70         newsca=(stat_cmd_arg *)g_malloc(sizeof(stat_cmd_arg));
71         newsca->cmd=cmd;
72         newsca->func=func;
73         newsca->userdata=userdata;
74         stat_cmd_arg_list=g_slist_insert_sorted(stat_cmd_arg_list, newsca,
75             sort_by_name);
76 }
77
78 /* **********************************************************************
79  * Function called for a stat command-line argument
80  * ********************************************************************** */
81 gboolean
82 process_stat_cmd_arg(char *optstr)
83 {
84         GSList *entry;
85         stat_cmd_arg *sca;
86         stat_requested *tr;
87
88         for(entry=stat_cmd_arg_list;entry;entry=g_slist_next(entry)){
89                 sca=(stat_cmd_arg *)entry->data;
90                 if(!strncmp(sca->cmd,optstr,strlen(sca->cmd))){
91                         tr=(stat_requested *)g_malloc(sizeof (stat_requested));
92                         tr->sca = sca;
93                         tr->arg=g_strdup(optstr);
94                         stats_requested=g_slist_append(stats_requested, tr);
95                         return TRUE;
96                 }
97         }
98         return FALSE;
99 }
100
101 /* **********************************************************************
102  * Function to list all possible tap command-line arguments
103  * ********************************************************************** */
104 void
105 list_stat_cmd_args(void)
106 {
107         GSList *entry;
108         stat_cmd_arg *sca;
109
110         for(entry=stat_cmd_arg_list;entry;entry=g_slist_next(entry)){
111                 sca=(stat_cmd_arg *)entry->data;
112                 fprintf(stderr,"     %s\n",sca->cmd);
113         }
114 }
115
116 /* **********************************************************************
117  * Function to process stats requested with command-line arguments
118  * ********************************************************************** */
119 void
120 start_requested_stats(void)
121 {
122         stat_requested *sr;
123
124         while(stats_requested){
125                 sr=(stat_requested *)stats_requested->data;
126                 (*sr->sca->func)(sr->arg,sr->sca->userdata);
127                 g_free(sr->arg);
128                 g_free(sr);
129                 stats_requested=g_slist_remove(stats_requested, sr);
130         }
131 }