7c80b7f65541215da8d9482c58057f6ed4fd425c
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/option.h>
24 typedef struct _LttvOption
{
28 char *arg_description
;
39 list_options(gpointer key
, gpointer value
, gpointer user_data
)
41 g_ptr_array_add((GPtrArray
*)user_data
, value
);
46 free_option(LttvOption
*option
)
48 g_free(option
->long_name
);
49 g_free(option
->description
);
50 g_free(option
->arg_description
);
55 void lttv_option_init(int argc
, char **argv
)
57 g_log(G_LOG_DOMAIN
, G_LOG_LEVEL_INFO
, "Init option.c");
58 options
= g_hash_table_new(g_str_hash
, g_str_equal
);
62 void lttv_option_destroy()
66 GPtrArray
*list
= g_ptr_array_new();
70 g_log(G_LOG_DOMAIN
, G_LOG_LEVEL_INFO
, "Destroy option.c");
71 g_hash_table_foreach(options
, list_options
, list
);
72 g_hash_table_destroy(options
);
74 for(i
= 0 ; i
< list
->len
; i
++) {
75 free_option((LttvOption
*)list
->pdata
[i
]);
77 g_ptr_array_free(list
, TRUE
);
81 void lttv_option_add(const char *long_name
, const char char_name
,
82 const char *description
, const char *arg_description
,
83 const LttvOptionType t
, void *p
,
84 const LttvOptionHook h
, void *hook_data
)
88 g_log(G_LOG_DOMAIN
, G_LOG_LEVEL_INFO
, "Add option %s", long_name
);
89 if(g_hash_table_lookup(options
, long_name
) != NULL
) {
90 g_warning("duplicate option");
94 option
= g_new(LttvOption
, 1);
95 option
->long_name
= g_strdup(long_name
);
96 option
->char_name
= char_name
;
97 option
->description
= g_strdup(description
);
98 option
->arg_description
= g_strdup(arg_description
);
102 option
->hook_data
= hook_data
;
103 g_hash_table_insert(options
, option
->long_name
, option
);
108 lttv_option_remove(const char *long_name
)
110 LttvOption
*option
= g_hash_table_lookup(options
, long_name
);
112 g_log(G_LOG_DOMAIN
, G_LOG_LEVEL_INFO
, "Remove option %s", long_name
);
114 g_warning("trying to remove unknown option %s", long_name
);
117 g_hash_table_remove(options
, long_name
);
122 static int poptToLTT
[] = {
123 POPT_ARG_NONE
, POPT_ARG_STRING
, POPT_ARG_INT
, POPT_ARG_LONG
126 static struct poptOption endOption
= { NULL
, '\0', 0, NULL
, 0};
130 build_popts(GPtrArray
**plist
, struct poptOption
**ppopts
, poptContext
*pc
,
131 int argc
, char **argv
)
137 struct poptOption
*popts
;
143 list
= g_ptr_array_new();
145 g_hash_table_foreach(options
, list_options
, list
);
147 /* Build a popt options array from our list */
149 popts
= g_new(struct poptOption
, list
->len
+ 1);
151 for(i
= 0 ; i
< list
->len
; i
++) {
152 option
= (LttvOption
*)list
->pdata
[i
];
153 popts
[i
].longName
= option
->long_name
;
154 popts
[i
].shortName
= option
->char_name
;
155 popts
[i
].descrip
= option
->description
;
156 popts
[i
].argDescrip
= option
->arg_description
;
157 popts
[i
].argInfo
= poptToLTT
[option
->t
];
158 popts
[i
].arg
= option
->p
;
159 popts
[i
].val
= i
+ 1;
162 /* Terminate the array for popt and create the context */
164 popts
[list
->len
] = endOption
;
165 c
= poptGetContext(argv
[0], argc
, (const char**)argv
, popts
, 0);
174 destroy_popts(GPtrArray
**plist
, struct poptOption
**ppopts
, poptContext
*pc
)
176 g_ptr_array_free(*plist
, TRUE
); *plist
= NULL
;
177 g_free(*ppopts
); *ppopts
= NULL
;
178 poptFreeContext(*pc
);
182 void lttv_option_parse(int argc
, char **argv
)
188 int i
, rc
, first_arg
;
190 struct poptOption
*popts
;
198 build_popts(&list
, &popts
, &c
, argc
, argv
);
200 /* Parse options while not end of options event */
202 while((rc
= poptGetNextOpt(c
)) != -1) {
204 /* The option was recognized and the rc value returned is the argument
205 position in the array. Call the associated hook if present. */
208 option
= (LttvOption
*)(list
->pdata
[rc
- 1]);
209 g_log(G_LOG_DOMAIN
, G_LOG_LEVEL_INFO
, "Option %s encountered",
211 if(option
->hook
!= NULL
) {
212 g_log(G_LOG_DOMAIN
, G_LOG_LEVEL_DEBUG
, "Option %s hook called",
214 option
->hook(option
->hook_data
);
219 else if(rc
== POPT_ERROR_BADOPT
&& i
!= first_arg
) {
220 g_log(G_LOG_DOMAIN
, G_LOG_LEVEL_DEBUG
,
221 "Option %s not recognized, rescan options with new additions",
224 /* Perhaps this option is newly added, restart parsing */
226 destroy_popts(&list
, &popts
, &c
);
227 build_popts(&list
, &popts
, &c
, argc
, argv
);
229 /* Get back to the same argument */
232 for(i
= 0; i
< first_arg
; i
++) {
233 rc
= poptGetNextOpt(c
);
234 option
= (LttvOption
*)(list
->pdata
[rc
- 1]);
235 g_log(G_LOG_DOMAIN
, G_LOG_LEVEL_DEBUG
, "Option %s rescanned, skipped",
242 /* The option has some error and it is not because this is a newly
243 added option not recognized. */
245 g_error("option %s: %s", poptBadOption(c
,0), poptStrerror(rc
));
251 destroy_popts(&list
, &popts
, &c
);
254 static void show_help(LttvOption
*option
)
256 printf("--%s -%c argument: %s\n" , option
->long_name
,
258 option
->arg_description
);
259 printf(" %s\n" , option
->description
);
263 void lttv_option_show_help(void)
267 GPtrArray
*list
= g_ptr_array_new();
271 g_hash_table_foreach(options
, list_options
, list
);
273 printf("Built-in commands available:\n");
276 for(i
= 0 ; i
< list
->len
; i
++) {
277 show_help((LttvOption
*)list
->pdata
[i
]);
279 g_ptr_array_free(list
, TRUE
);
This page took 0.033889 seconds and 3 git commands to generate.