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,
25 #include <lttv/module.h>
26 #include <lttv/option.h>
28 typedef struct _LttvOption
{
32 char *arg_description
;
38 /* Keep the order of addition */
45 static void list_options(gpointer key
, gpointer value
, gpointer user_data
)
47 GPtrArray
*list
= (GPtrArray
*)user_data
;
48 LttvOption
*option
= (LttvOption
*)value
;
50 if(list
->len
< option
->val
)
51 g_ptr_array_set_size(list
, option
->val
);
52 list
->pdata
[option
->val
-1] = option
;
56 static void free_option(LttvOption
*option
)
58 g_free(option
->long_name
);
59 g_free(option
->description
);
60 g_free(option
->arg_description
);
65 void lttv_option_add(const char *long_name
, const char char_name
,
66 const char *description
, const char *arg_description
,
67 const LttvOptionType t
, void *p
,
68 const LttvOptionHook h
, void *hook_data
)
72 g_log(G_LOG_DOMAIN
, G_LOG_LEVEL_INFO
, "Add option %s", long_name
);
73 if(g_hash_table_lookup(options
, long_name
) != NULL
) {
74 g_warning("duplicate option");
78 option
= g_new(LttvOption
, 1);
79 option
->long_name
= g_strdup(long_name
);
80 option
->char_name
= char_name
;
81 option
->description
= g_strdup(description
);
82 option
->arg_description
= g_strdup(arg_description
);
86 option
->hook_data
= hook_data
;
87 option
->val
= g_hash_table_size(options
) + 1;
88 g_hash_table_insert(options
, option
->long_name
, option
);
92 void 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, NULL
, NULL
};
113 static void build_popts(GPtrArray
**plist
, struct poptOption
**ppopts
,
114 poptContext
*pc
, int argc
, char **argv
)
120 struct poptOption
*popts
;
126 list
= g_ptr_array_sized_new(g_hash_table_size(options
));
128 g_hash_table_foreach(options
, list_options
, list
);
130 /* Build a popt options array from our list */
132 popts
= g_new(struct poptOption
, list
->len
+ 1);
134 /* add the options in the reverse order, so last additions are parsed first */
135 for(i
= 0 ; i
< list
->len
; i
++) {
136 guint reverse_i
= list
->len
-1-i
;
137 option
= (LttvOption
*)list
->pdata
[i
];
138 popts
[reverse_i
].longName
= option
->long_name
;
139 popts
[reverse_i
].shortName
= option
->char_name
;
140 popts
[reverse_i
].descrip
= option
->description
;
141 popts
[reverse_i
].argDescrip
= option
->arg_description
;
142 popts
[reverse_i
].argInfo
= poptToLTT
[option
->t
];
143 popts
[reverse_i
].arg
= option
->p
;
144 popts
[reverse_i
].val
= option
->val
;
147 /* Terminate the array for popt and create the context */
149 popts
[list
->len
] = endOption
;
150 c
= poptGetContext(argv
[0], argc
, (const char**)argv
, popts
, 0);
158 static void destroy_popts(GPtrArray
**plist
, struct poptOption
**ppopts
,
161 g_ptr_array_free(*plist
, TRUE
); *plist
= NULL
;
162 g_free(*ppopts
); *ppopts
= NULL
;
163 poptFreeContext(*pc
);
167 void lttv_option_parse(int argc
, char **argv
)
173 int i
, rc
, first_arg
;
175 struct poptOption
*popts
;
185 build_popts(&list
, &popts
, &c
, argc
, argv
);
187 /* Parse options while not end of options event */
189 while((rc
= poptGetNextOpt(c
)) != -1) {
191 /* The option was recognized and the rc value returned is the argument
192 position in the array. Call the associated hook if present. */
195 option
= (LttvOption
*)(list
->pdata
[rc
- 1]);
196 g_log(G_LOG_DOMAIN
, G_LOG_LEVEL_INFO
, "Option %s encountered",
198 hash_size
= g_hash_table_size(options
);
199 if(option
->hook
!= NULL
) {
200 g_log(G_LOG_DOMAIN
, G_LOG_LEVEL_DEBUG
, "Option %s hook called",
202 option
->hook(option
->hook_data
);
206 /* If the size of the option hash changed, add new options
207 * right now. It resolves the conflict of multiple same short
210 if(hash_size
!= g_hash_table_size(options
)) {
211 destroy_popts(&list
, &popts
, &c
);
212 build_popts(&list
, &popts
, &c
, argc
, argv
);
214 /* Get back to the same argument */
217 for(i
= 0; i
< first_arg
; i
++) {
218 rc
= poptGetNextOpt(c
);
219 option
= (LttvOption
*)(list
->pdata
[rc
- 1]);
220 g_log(G_LOG_DOMAIN
, G_LOG_LEVEL_DEBUG
, "Option %s rescanned, skipped",
226 else if(rc
== POPT_ERROR_BADOPT
&& i
!= first_arg
) {
227 g_log(G_LOG_DOMAIN
, G_LOG_LEVEL_DEBUG
,
228 "Option %s not recognized, rescan options with new additions",
231 /* Perhaps this option is newly added, restart parsing */
233 destroy_popts(&list
, &popts
, &c
);
234 build_popts(&list
, &popts
, &c
, argc
, argv
);
236 /* Get back to the same argument */
239 for(i
= 0; i
< first_arg
; i
++) {
240 rc
= poptGetNextOpt(c
);
241 option
= (LttvOption
*)(list
->pdata
[rc
- 1]);
242 g_log(G_LOG_DOMAIN
, G_LOG_LEVEL_DEBUG
, "Option %s rescanned, skipped",
249 /* The option has some error and it is not because this is a newly
250 added option not recognized. */
252 g_error("option %s: %s", poptBadOption(c
,0), poptStrerror(rc
));
258 destroy_popts(&list
, &popts
, &c
);
262 static void show_help(LttvOption
*option
)
264 printf("--%s -%c argument: %s\n" , option
->long_name
,
265 option
->char_name
, option
->arg_description
);
266 printf(" %s\n" , option
->description
);
270 void lttv_option_show_help(void)
272 GPtrArray
*list
= g_ptr_array_new();
276 g_hash_table_foreach(options
, list_options
, list
);
278 printf("Built-in commands available:\n");
281 for(i
= 0 ; i
< list
->len
; i
++) {
282 show_help((LttvOption
*)list
->pdata
[i
]);
284 g_ptr_array_free(list
, TRUE
);
289 g_log(G_LOG_DOMAIN
, G_LOG_LEVEL_INFO
, "Init option.c");
290 options
= g_hash_table_new(g_str_hash
, g_str_equal
);
294 static void destroy()
296 GPtrArray
*list
= g_ptr_array_new();
300 g_log(G_LOG_DOMAIN
, G_LOG_LEVEL_INFO
, "Destroy option.c");
301 g_hash_table_foreach(options
, list_options
, list
);
302 g_hash_table_destroy(options
);
304 for(i
= 0 ; i
< list
->len
; i
++) {
305 free_option((LttvOption
*)list
->pdata
[i
]);
307 g_ptr_array_free(list
, TRUE
);
310 LTTV_MODULE("option", "Command line options processing", \
311 "Functions to add, remove and parse command line options", \
This page took 0.118607 seconds and 4 git commands to generate.