1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2003-2004 Michel Dagenais
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;
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.
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,
22 #include <lttv/module.h>
23 #include <lttv/option.h>
25 typedef struct _LttvOption
{
29 char *arg_description
;
35 /* Keep the order of addition */
43 list_options(gpointer key
, gpointer value
, gpointer user_data
)
45 GPtrArray
*list
= (GPtrArray
*)user_data
;
46 LttvOption
*option
= (LttvOption
*)value
;
48 if(list
->len
< option
->val
)
49 g_ptr_array_set_size(list
, option
->val
);
50 list
->pdata
[option
->val
-1] = option
;
55 free_option(LttvOption
*option
)
57 g_free(option
->long_name
);
58 g_free(option
->description
);
59 g_free(option
->arg_description
);
64 void lttv_option_add(const char *long_name
, const char char_name
,
65 const char *description
, const char *arg_description
,
66 const LttvOptionType t
, void *p
,
67 const LttvOptionHook h
, void *hook_data
)
71 g_log(G_LOG_DOMAIN
, G_LOG_LEVEL_INFO
, "Add option %s", long_name
);
72 if(g_hash_table_lookup(options
, long_name
) != NULL
) {
73 g_warning("duplicate option");
77 option
= g_new(LttvOption
, 1);
78 option
->long_name
= g_strdup(long_name
);
79 option
->char_name
= char_name
;
80 option
->description
= g_strdup(description
);
81 option
->arg_description
= g_strdup(arg_description
);
85 option
->hook_data
= hook_data
;
86 option
->val
= g_hash_table_size(options
) + 1;
87 g_hash_table_insert(options
, option
->long_name
, option
);
92 lttv_option_remove(const char *long_name
)
94 LttvOption
*option
= g_hash_table_lookup(options
, long_name
);
96 g_log(G_LOG_DOMAIN
, G_LOG_LEVEL_INFO
, "Remove option %s", long_name
);
98 g_warning("trying to remove unknown option %s", long_name
);
101 g_hash_table_remove(options
, long_name
);
106 static int poptToLTT
[] = {
107 POPT_ARG_NONE
, POPT_ARG_STRING
, POPT_ARG_INT
, POPT_ARG_LONG
110 static struct poptOption endOption
= { NULL
, '\0', 0, NULL
, 0};
114 build_popts(GPtrArray
**plist
, struct poptOption
**ppopts
, poptContext
*pc
,
115 int argc
, char **argv
)
121 struct poptOption
*popts
;
127 list
= g_ptr_array_sized_new(g_hash_table_size(options
));
129 g_hash_table_foreach(options
, list_options
, list
);
131 /* Build a popt options array from our list */
133 popts
= g_new(struct poptOption
, list
->len
+ 1);
135 /* add the options in the reverse order, so last additions are parsed first */
136 for(i
= 0 ; i
< list
->len
; i
++) {
137 guint index
= list
->len
-1-i
;
138 option
= (LttvOption
*)list
->pdata
[i
];
139 popts
[index
].longName
= option
->long_name
;
140 popts
[index
].shortName
= option
->char_name
;
141 popts
[index
].descrip
= option
->description
;
142 popts
[index
].argDescrip
= option
->arg_description
;
143 popts
[index
].argInfo
= poptToLTT
[option
->t
];
144 popts
[index
].arg
= option
->p
;
145 popts
[index
].val
= option
->val
;
148 /* Terminate the array for popt and create the context */
150 popts
[list
->len
] = endOption
;
151 c
= poptGetContext(argv
[0], argc
, (const char**)argv
, popts
, 0);
160 destroy_popts(GPtrArray
**plist
, struct poptOption
**ppopts
, poptContext
*pc
)
162 g_ptr_array_free(*plist
, TRUE
); *plist
= NULL
;
163 g_free(*ppopts
); *ppopts
= NULL
;
164 poptFreeContext(*pc
);
168 void lttv_option_parse(int argc
, char **argv
)
174 int i
, rc
, first_arg
;
176 struct poptOption
*popts
;
186 build_popts(&list
, &popts
, &c
, argc
, argv
);
188 /* Parse options while not end of options event */
190 while((rc
= poptGetNextOpt(c
)) != -1) {
192 /* The option was recognized and the rc value returned is the argument
193 position in the array. Call the associated hook if present. */
196 option
= (LttvOption
*)(list
->pdata
[rc
- 1]);
197 g_log(G_LOG_DOMAIN
, G_LOG_LEVEL_INFO
, "Option %s encountered",
199 hash_size
= g_hash_table_size(options
);
200 if(option
->hook
!= NULL
) {
201 g_log(G_LOG_DOMAIN
, G_LOG_LEVEL_DEBUG
, "Option %s hook called",
203 option
->hook(option
->hook_data
);
207 /* If the size of the option hash changed, add new options
208 * right now. It resolves the conflict of multiple same short
211 if(hash_size
!= g_hash_table_size(options
)) {
212 destroy_popts(&list
, &popts
, &c
);
213 build_popts(&list
, &popts
, &c
, argc
, argv
);
215 /* Get back to the same argument */
218 for(i
= 0; i
< first_arg
; i
++) {
219 rc
= poptGetNextOpt(c
);
220 option
= (LttvOption
*)(list
->pdata
[rc
- 1]);
221 g_log(G_LOG_DOMAIN
, G_LOG_LEVEL_DEBUG
, "Option %s rescanned, skipped",
227 else if(rc
== POPT_ERROR_BADOPT
&& i
!= first_arg
) {
228 g_log(G_LOG_DOMAIN
, G_LOG_LEVEL_DEBUG
,
229 "Option %s not recognized, rescan options with new additions",
232 /* Perhaps this option is newly added, restart parsing */
234 destroy_popts(&list
, &popts
, &c
);
235 build_popts(&list
, &popts
, &c
, argc
, argv
);
237 /* Get back to the same argument */
240 for(i
= 0; i
< first_arg
; i
++) {
241 rc
= poptGetNextOpt(c
);
242 option
= (LttvOption
*)(list
->pdata
[rc
- 1]);
243 g_log(G_LOG_DOMAIN
, G_LOG_LEVEL_DEBUG
, "Option %s rescanned, skipped",
250 /* The option has some error and it is not because this is a newly
251 added option not recognized. */
253 g_error("option %s: %s", poptBadOption(c
,0), poptStrerror(rc
));
259 destroy_popts(&list
, &popts
, &c
);
263 static void show_help(LttvOption
*option
)
265 printf("--%s -%c argument: %s\n" , option
->long_name
,
267 option
->arg_description
);
268 printf(" %s\n" , option
->description
);
272 void lttv_option_show_help(void)
276 GPtrArray
*list
= g_ptr_array_new();
280 g_hash_table_foreach(options
, list_options
, list
);
282 printf("Built-in commands available:\n");
285 for(i
= 0 ; i
< list
->len
; i
++) {
286 show_help((LttvOption
*)list
->pdata
[i
]);
288 g_ptr_array_free(list
, TRUE
);
295 g_log(G_LOG_DOMAIN
, G_LOG_LEVEL_INFO
, "Init option.c");
296 options
= g_hash_table_new(g_str_hash
, g_str_equal
);
300 static void destroy()
304 GPtrArray
*list
= g_ptr_array_new();
308 g_log(G_LOG_DOMAIN
, G_LOG_LEVEL_INFO
, "Destroy option.c");
309 g_hash_table_foreach(options
, list_options
, list
);
310 g_hash_table_destroy(options
);
312 for(i
= 0 ; i
< list
->len
; i
++) {
313 free_option((LttvOption
*)list
->pdata
[i
]);
315 g_ptr_array_free(list
, TRUE
);
318 LTTV_MODULE("option", "Command line options processing", \
319 "Functions to add, remove and parse command line options", \
This page took 0.077492 seconds and 4 git commands to generate.