White space changes.
[obnox/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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <stdio.h>
30
31 #include <string.h>
32
33 #include <glib.h>
34
35 #include <epan/stat_cmd_args.h>
36
37 /* structure to keep track of what stats have registered command-line
38    arguments.
39  */
40 typedef struct _stat_cmd_arg {
41         const char *cmd;
42         void (*func)(const char *arg, void* userdata);
43     void* userdata;
44 } stat_cmd_arg;
45
46 static GSList *stat_cmd_arg_list=NULL;
47
48 /* structure to keep track of what stats have been specified on the
49    command line.
50  */
51 typedef struct {
52         stat_cmd_arg *sca;
53         char *arg;
54 } stat_requested;
55 static GSList *stats_requested = NULL;
56
57 /* **********************************************************************
58  * Function called from stat to register the stat's command-line argument
59  * and initialization routine
60  * ********************************************************************** */
61 static gint
62 sort_by_name(gconstpointer a, gconstpointer b)
63 {
64         return strcmp(((const stat_cmd_arg *)a)->cmd,
65             ((const stat_cmd_arg *)b)->cmd);
66 }
67 void
68 register_stat_cmd_arg(const char *cmd, void (*func)(const char*, void*),void* userdata)
69 {
70         stat_cmd_arg *newsca;
71
72         newsca=g_malloc(sizeof(stat_cmd_arg));
73         newsca->cmd=cmd;
74         newsca->func=func;
75         newsca->userdata=userdata;
76         stat_cmd_arg_list=g_slist_insert_sorted(stat_cmd_arg_list, newsca,
77             sort_by_name);
78 }
79
80 /* **********************************************************************
81  * Function called for a stat command-line argument
82  * ********************************************************************** */
83 gboolean
84 process_stat_cmd_arg(char *optstr)
85 {
86         GSList *entry;
87         stat_cmd_arg *sca;
88         stat_requested *tr;
89
90         for(entry=stat_cmd_arg_list;entry;entry=g_slist_next(entry)){
91                 sca=entry->data;
92                 if(!strncmp(sca->cmd,optstr,strlen(sca->cmd))){
93                         tr=g_malloc(sizeof (stat_requested));
94                         tr->sca = sca;
95                         tr->arg=g_strdup(optstr);
96                         stats_requested=g_slist_append(stats_requested, tr);
97                         return TRUE;
98                 }
99         }
100         return FALSE;
101 }
102
103 /* **********************************************************************
104  * Function to list all possible tap command-line arguments
105  * ********************************************************************** */
106 void
107 list_stat_cmd_args(void)
108 {
109         GSList *entry;
110         stat_cmd_arg *sca;
111
112         for(entry=stat_cmd_arg_list;entry;entry=g_slist_next(entry)){
113                 sca=entry->data;
114                 fprintf(stderr,"     %s\n",sca->cmd);
115         }
116 }
117
118 /* **********************************************************************
119  * Function to process stats requested with command-line arguments
120  * ********************************************************************** */
121 void
122 start_requested_stats(void)
123 {
124         stat_requested *sr;
125
126         while(stats_requested){
127                 sr=stats_requested->data;
128                 (*sr->sca->func)(sr->arg,sr->sca->userdata);
129                 g_free(sr->arg);
130                 g_free(sr);
131                 stats_requested=g_slist_remove(stats_requested, sr);
132         }
133 }