Add reference counts to state and stats saved attributes. This way, the
[lttv.git] / ltt / branches / poly / lttv / main / option.c
1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2003-2004 Michel Dagenais
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License Version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
16 * MA 02111-1307, USA.
17 */
18
19
20 #include <popt.h>
21 #include <glib.h>
22 #include <lttv/module.h>
23 #include <lttv/option.h>
24
25 typedef struct _LttvOption {
26 char *long_name;
27 char char_name;
28 char *description;
29 char *arg_description;
30 LttvOptionType t;
31 gpointer p;
32 LttvOptionHook hook;
33 gpointer hook_data;
34 } LttvOption;
35
36 GHashTable *options;
37
38
39 static void
40 list_options(gpointer key, gpointer value, gpointer user_data)
41 {
42 g_ptr_array_add((GPtrArray *)user_data, value);
43 }
44
45
46 static void
47 free_option(LttvOption *option)
48 {
49 g_free(option->long_name);
50 g_free(option->description);
51 g_free(option->arg_description);
52 g_free(option);
53 }
54
55
56 void lttv_option_add(const char *long_name, const char char_name,
57 const char *description, const char *arg_description,
58 const LttvOptionType t, void *p,
59 const LttvOptionHook h, void *hook_data)
60 {
61 LttvOption *option;
62
63 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "Add option %s", long_name);
64 if(g_hash_table_lookup(options, long_name) != NULL) {
65 g_warning("duplicate option");
66 return;
67 }
68
69 option = g_new(LttvOption, 1);
70 option->long_name = g_strdup(long_name);
71 option->char_name = char_name;
72 option->description = g_strdup(description);
73 option->arg_description = g_strdup(arg_description);
74 option->t = t;
75 option->p = p;
76 option->hook = h;
77 option->hook_data = hook_data;
78 g_hash_table_insert(options, option->long_name, option);
79 }
80
81
82 void
83 lttv_option_remove(const char *long_name)
84 {
85 LttvOption *option = g_hash_table_lookup(options, long_name);
86
87 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "Remove option %s", long_name);
88 if(option == NULL) {
89 g_warning("trying to remove unknown option %s", long_name);
90 return;
91 }
92 g_hash_table_remove(options, long_name);
93 free_option(option);
94 }
95
96
97 static int poptToLTT[] = {
98 POPT_ARG_NONE, POPT_ARG_STRING, POPT_ARG_INT, POPT_ARG_LONG
99 };
100
101 static struct poptOption endOption = { NULL, '\0', 0, NULL, 0};
102
103
104 static void
105 build_popts(GPtrArray **plist, struct poptOption **ppopts, poptContext *pc,
106 int argc, char **argv)
107 {
108 LttvOption *option;
109
110 GPtrArray *list;
111
112 struct poptOption *popts;
113
114 poptContext c;
115
116 guint i;
117
118 list = g_ptr_array_new();
119
120 g_hash_table_foreach(options, list_options, list);
121
122 /* Build a popt options array from our list */
123
124 popts = g_new(struct poptOption, list->len + 1);
125
126 for(i = 0 ; i < list->len ; i++) {
127 option = (LttvOption *)list->pdata[i];
128 popts[i].longName = option->long_name;
129 popts[i].shortName = option->char_name;
130 popts[i].descrip = option->description;
131 popts[i].argDescrip = option->arg_description;
132 popts[i].argInfo = poptToLTT[option->t];
133 popts[i].arg = option->p;
134 popts[i].val = i + 1;
135 }
136
137 /* Terminate the array for popt and create the context */
138
139 popts[list->len] = endOption;
140 c = poptGetContext(argv[0], argc, (const char**)argv, popts, 0);
141
142 *plist = list;
143 *ppopts = popts;
144 *pc = c;
145 }
146
147
148 static void
149 destroy_popts(GPtrArray **plist, struct poptOption **ppopts, poptContext *pc)
150 {
151 g_ptr_array_free(*plist, TRUE); *plist = NULL;
152 g_free(*ppopts); *ppopts = NULL;
153 poptFreeContext(*pc);
154 }
155
156
157 void lttv_option_parse(int argc, char **argv)
158 {
159 GPtrArray *list;
160
161 LttvOption *option;
162
163 int i, rc, first_arg;
164
165 struct poptOption *popts;
166
167 poptContext c;
168
169 i = 0;
170
171 first_arg = 0;
172
173 build_popts(&list, &popts, &c, argc, argv);
174
175 /* Parse options while not end of options event */
176
177 while((rc = poptGetNextOpt(c)) != -1) {
178
179 /* The option was recognized and the rc value returned is the argument
180 position in the array. Call the associated hook if present. */
181
182 if(rc > 0) {
183 option = (LttvOption *)(list->pdata[rc - 1]);
184 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "Option %s encountered",
185 option->long_name);
186 if(option->hook != NULL) {
187 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Option %s hook called",
188 option->long_name);
189 option->hook(option->hook_data);
190 }
191 i++;
192 }
193
194 else if(rc == POPT_ERROR_BADOPT && i != first_arg) {
195 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
196 "Option %s not recognized, rescan options with new additions",
197 poptBadOption(c,0));
198
199 /* Perhaps this option is newly added, restart parsing */
200
201 destroy_popts(&list, &popts, &c);
202 build_popts(&list, &popts, &c, argc, argv);
203
204 /* Get back to the same argument */
205
206 first_arg = i;
207 for(i = 0; i < first_arg; i++) {
208 rc = poptGetNextOpt(c);
209 option = (LttvOption *)(list->pdata[rc - 1]);
210 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Option %s rescanned, skipped",
211 option->long_name);
212 }
213 }
214
215 else {
216
217 /* The option has some error and it is not because this is a newly
218 added option not recognized. */
219
220 g_error("option %s: %s", poptBadOption(c,0), poptStrerror(rc));
221 break;
222 }
223
224 }
225
226 destroy_popts(&list, &popts, &c);
227 }
228
229 /* CHECK */
230 static void show_help(LttvOption *option)
231 {
232 printf("--%s -%c argument: %s\n" , option->long_name,
233 option->char_name,
234 option->arg_description);
235 printf(" %s\n" , option->description);
236
237 }
238
239 void lttv_option_show_help(void)
240 {
241 LttvOption option;
242
243 GPtrArray *list = g_ptr_array_new();
244
245 int i;
246
247 g_hash_table_foreach(options, list_options, list);
248
249 printf("Built-in commands available:\n");
250 printf("\n");
251
252 for(i = 0 ; i < list->len ; i++) {
253 show_help((LttvOption *)list->pdata[i]);
254 }
255 g_ptr_array_free(list, TRUE);
256
257
258 }
259
260 static void init()
261 {
262 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "Init option.c");
263 options = g_hash_table_new(g_str_hash, g_str_equal);
264 }
265
266
267 static void destroy()
268 {
269 LttvOption option;
270
271 GPtrArray *list = g_ptr_array_new();
272
273 int i;
274
275 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "Destroy option.c");
276 g_hash_table_foreach(options, list_options, list);
277 g_hash_table_destroy(options);
278
279 for(i = 0 ; i < list->len ; i++) {
280 free_option((LttvOption *)list->pdata[i]);
281 }
282 g_ptr_array_free(list, TRUE);
283 }
284
285 LTTV_MODULE("option", "Command line options processing", \
286 "Functions to add, remove and parse command line options", \
287 init, destroy)
This page took 0.036315 seconds and 4 git commands to generate.