Add copyright notices and some comments about status and TODO
[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/option.h>
23
24 #define g_info(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format)
25 #define g_debug(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format)
26
27 typedef struct _LttvOption {
28 char *long_name;
29 char char_name;
30 char *description;
31 char *arg_description;
32 LttvOptionType t;
33 gpointer p;
34 LttvOptionHook hook;
35 gpointer hook_data;
36 } LttvOption;
37
38 GHashTable *options;
39
40
41 static void
42 list_options(gpointer key, gpointer value, gpointer user_data)
43 {
44 g_ptr_array_add((GPtrArray *)user_data, value);
45 }
46
47
48 static void
49 free_option(LttvOption *option)
50 {
51 g_free(option->long_name);
52 g_free(option->description);
53 g_free(option->arg_description);
54 g_free(option);
55 }
56
57
58 void lttv_option_init(int argc, char **argv)
59 {
60 g_info("Init option.c");
61 options = g_hash_table_new(g_str_hash, g_str_equal);
62 }
63
64
65 void lttv_option_destroy()
66 {
67 LttvOption option;
68
69 GPtrArray *list = g_ptr_array_new();
70
71 int i;
72
73 g_info("Destroy option.c");
74 g_hash_table_foreach(options, list_options, list);
75 g_hash_table_destroy(options);
76
77 for(i = 0 ; i < list->len ; i++) {
78 free_option((LttvOption *)list->pdata[i]);
79 }
80 g_ptr_array_free(list, TRUE);
81 }
82
83
84 void lttv_option_add(const char *long_name, const char char_name,
85 const char *description, const char *arg_description,
86 const LttvOptionType t, void *p,
87 const LttvOptionHook h, void *hook_data)
88 {
89 LttvOption *option;
90
91 g_info("Add option %s", long_name);
92 if(g_hash_table_lookup(options, long_name) != NULL) {
93 g_warning("duplicate option");
94 return;
95 }
96
97 option = g_new(LttvOption, 1);
98 option->long_name = g_strdup(long_name);
99 option->char_name = char_name;
100 option->description = g_strdup(description);
101 option->arg_description = g_strdup(arg_description);
102 option->t = t;
103 option->p = p;
104 option->hook = h;
105 option->hook_data = hook_data;
106 g_hash_table_insert(options, option->long_name, option);
107 }
108
109
110 void
111 lttv_option_remove(const char *long_name)
112 {
113 LttvOption *option = g_hash_table_lookup(options, long_name);
114
115 g_info("Remove option %s", long_name);
116 if(option == NULL) {
117 g_warning("trying to remove unknown option %s", long_name);
118 return;
119 }
120 g_hash_table_remove(options, long_name);
121 free_option(option);
122 }
123
124
125 static int poptToLTT[] = {
126 POPT_ARG_NONE, POPT_ARG_STRING, POPT_ARG_INT, POPT_ARG_LONG
127 };
128
129 static struct poptOption endOption = { NULL, '\0', 0, NULL, 0};
130
131
132 static void
133 build_popts(GPtrArray **plist, struct poptOption **ppopts, poptContext *pc,
134 int argc, char **argv)
135 {
136 LttvOption *option;
137
138 GPtrArray *list;
139
140 struct poptOption *popts;
141
142 poptContext c;
143
144 guint i;
145
146 list = g_ptr_array_new();
147
148 g_hash_table_foreach(options, list_options, list);
149
150 /* Build a popt options array from our list */
151
152 popts = g_new(struct poptOption, list->len + 1);
153
154 for(i = 0 ; i < list->len ; i++) {
155 option = (LttvOption *)list->pdata[i];
156 popts[i].longName = option->long_name;
157 popts[i].shortName = option->char_name;
158 popts[i].descrip = option->description;
159 popts[i].argDescrip = option->arg_description;
160 popts[i].argInfo = poptToLTT[option->t];
161 popts[i].arg = option->p;
162 popts[i].val = i + 1;
163 }
164
165 /* Terminate the array for popt and create the context */
166
167 popts[list->len] = endOption;
168 c = poptGetContext(argv[0], argc, (const char**)argv, popts, 0);
169
170 *plist = list;
171 *ppopts = popts;
172 *pc = c;
173 }
174
175
176 static void
177 destroy_popts(GPtrArray **plist, struct poptOption **ppopts, poptContext *pc)
178 {
179 g_ptr_array_free(*plist, TRUE); *plist = NULL;
180 g_free(*ppopts); *ppopts = NULL;
181 poptFreeContext(*pc);
182 }
183
184
185 void lttv_option_parse(int argc, char **argv)
186 {
187 GPtrArray *list;
188
189 LttvOption *option;
190
191 int i, rc, first_arg;
192
193 struct poptOption *popts;
194
195 poptContext c;
196
197 i = 0;
198
199 first_arg = 0;
200
201 build_popts(&list, &popts, &c, argc, argv);
202
203 /* Parse options while not end of options event */
204
205 while((rc = poptGetNextOpt(c)) != -1) {
206
207 /* The option was recognized and the rc value returned is the argument
208 position in the array. Call the associated hook if present. */
209
210 if(rc > 0) {
211 option = (LttvOption *)(list->pdata[rc - 1]);
212 g_info("Option %s encountered", option->long_name);
213 if(option->hook != NULL) {
214 g_info("Option %s hook called", option->long_name);
215 option->hook(option->hook_data);
216 }
217 i++;
218 }
219
220 else if(rc == POPT_ERROR_BADOPT && i != first_arg) {
221 g_info("Option %s not recognized, rescan options with new additions",
222 poptBadOption(c,0));
223
224 /* Perhaps this option is newly added, restart parsing */
225
226 destroy_popts(&list, &popts, &c);
227 build_popts(&list, &popts, &c, argc, argv);
228
229 /* Get back to the same argument */
230
231 first_arg = i;
232 for(i = 0; i < first_arg; i++) {
233 rc = poptGetNextOpt(c);
234 option = (LttvOption *)(list->pdata[rc - 1]);
235 g_info("Option %s rescanned, skipped", option->long_name);
236 }
237 }
238
239 else {
240
241 /* The option has some error and it is not because this is a newly
242 added option not recognized. */
243
244 g_error("option %s: %s", poptBadOption(c,0), poptStrerror(rc));
245 break;
246 }
247
248 }
249
250 destroy_popts(&list, &popts, &c);
251 }
252
253 static void show_help(LttvOption *option)
254 {
255 printf("--%s -%c argument: %s\n" , option->long_name,
256 option->char_name,
257 option->arg_description);
258 printf(" %s\n" , option->description);
259
260 }
261
262 void lttv_option_show_help(void)
263 {
264 LttvOption option;
265
266 GPtrArray *list = g_ptr_array_new();
267
268 int i;
269
270 g_hash_table_foreach(options, list_options, list);
271
272 printf("Built-in commands available:\n");
273 printf("\n");
274
275 for(i = 0 ; i < list->len ; i++) {
276 show_help((LttvOption *)list->pdata[i]);
277 }
278 g_ptr_array_free(list, TRUE);
279
280
281 }
282
This page took 0.056862 seconds and 4 git commands to generate.