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
;
40 list_options(gpointer key
, gpointer value
, gpointer user_data
)
42 g_ptr_array_add((GPtrArray
*)user_data
, value
);
47 free_option(LttvOption
*option
)
49 g_free(option
->long_name
);
50 g_free(option
->description
);
51 g_free(option
->arg_description
);
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
)
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");
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
);
77 option
->hook_data
= hook_data
;
78 g_hash_table_insert(options
, option
->long_name
, option
);
83 lttv_option_remove(const char *long_name
)
85 LttvOption
*option
= g_hash_table_lookup(options
, long_name
);
87 g_log(G_LOG_DOMAIN
, G_LOG_LEVEL_INFO
, "Remove option %s", long_name
);
89 g_warning("trying to remove unknown option %s", long_name
);
92 g_hash_table_remove(options
, long_name
);
97 static int poptToLTT
[] = {
98 POPT_ARG_NONE
, POPT_ARG_STRING
, POPT_ARG_INT
, POPT_ARG_LONG
101 static struct poptOption endOption
= { NULL
, '\0', 0, NULL
, 0};
105 build_popts(GPtrArray
**plist
, struct poptOption
**ppopts
, poptContext
*pc
,
106 int argc
, char **argv
)
112 struct poptOption
*popts
;
118 list
= g_ptr_array_new();
120 g_hash_table_foreach(options
, list_options
, list
);
122 /* Build a popt options array from our list */
124 popts
= g_new(struct poptOption
, list
->len
+ 1);
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;
137 /* Terminate the array for popt and create the context */
139 popts
[list
->len
] = endOption
;
140 c
= poptGetContext(argv
[0], argc
, (const char**)argv
, popts
, 0);
149 destroy_popts(GPtrArray
**plist
, struct poptOption
**ppopts
, poptContext
*pc
)
151 g_ptr_array_free(*plist
, TRUE
); *plist
= NULL
;
152 g_free(*ppopts
); *ppopts
= NULL
;
153 poptFreeContext(*pc
);
157 void lttv_option_parse(int argc
, char **argv
)
163 int i
, rc
, first_arg
;
165 struct poptOption
*popts
;
173 build_popts(&list
, &popts
, &c
, argc
, argv
);
175 /* Parse options while not end of options event */
177 while((rc
= poptGetNextOpt(c
)) != -1) {
179 /* The option was recognized and the rc value returned is the argument
180 position in the array. Call the associated hook if present. */
183 option
= (LttvOption
*)(list
->pdata
[rc
- 1]);
184 g_log(G_LOG_DOMAIN
, G_LOG_LEVEL_INFO
, "Option %s encountered",
186 if(option
->hook
!= NULL
) {
187 g_log(G_LOG_DOMAIN
, G_LOG_LEVEL_DEBUG
, "Option %s hook called",
189 option
->hook(option
->hook_data
);
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",
199 /* Perhaps this option is newly added, restart parsing */
201 destroy_popts(&list
, &popts
, &c
);
202 build_popts(&list
, &popts
, &c
, argc
, argv
);
204 /* Get back to the same argument */
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",
217 /* The option has some error and it is not because this is a newly
218 added option not recognized. */
220 g_error("option %s: %s", poptBadOption(c
,0), poptStrerror(rc
));
226 destroy_popts(&list
, &popts
, &c
);
230 static void show_help(LttvOption
*option
)
232 printf("--%s -%c argument: %s\n" , option
->long_name
,
234 option
->arg_description
);
235 printf(" %s\n" , option
->description
);
239 void lttv_option_show_help(void)
243 GPtrArray
*list
= g_ptr_array_new();
247 g_hash_table_foreach(options
, list_options
, list
);
249 printf("Built-in commands available:\n");
252 for(i
= 0 ; i
< list
->len
; i
++) {
253 show_help((LttvOption
*)list
->pdata
[i
]);
255 g_ptr_array_free(list
, TRUE
);
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
);
267 static void destroy()
271 GPtrArray
*list
= g_ptr_array_new();
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
);
279 for(i
= 0 ; i
< list
->len
; i
++) {
280 free_option((LttvOption
*)list
->pdata
[i
]);
282 g_ptr_array_free(list
, TRUE
);
285 LTTV_MODULE("option", "Command line options processing", \
286 "Functions to add, remove and parse command line options", \
This page took 0.034289 seconds and 4 git commands to generate.