4 #include <lttv/option.h>
6 #define g_info(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format)
7 #define g_debug(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format)
9 typedef struct _LttvOption
{
13 char *arg_description
;
24 list_options(gpointer key
, gpointer value
, gpointer user_data
)
26 g_ptr_array_add((GPtrArray
*)user_data
, value
);
31 free_option(LttvOption
*option
)
33 g_free(option
->long_name
);
34 g_free(option
->description
);
35 g_free(option
->arg_description
);
40 void lttv_option_init(int argc
, char **argv
)
42 g_info("Init option.c");
43 options
= g_hash_table_new(g_str_hash
, g_str_equal
);
47 void lttv_option_destroy()
51 GPtrArray
*list
= g_ptr_array_new();
55 g_info("Destroy option.c");
56 g_hash_table_foreach(options
, list_options
, list
);
57 g_hash_table_destroy(options
);
59 for(i
= 0 ; i
< list
->len
; i
++) {
60 free_option((LttvOption
*)list
->pdata
[i
]);
62 g_ptr_array_free(list
, TRUE
);
66 void lttv_option_add(const char *long_name
, const char char_name
,
67 const char *description
, const char *arg_description
,
68 const LttvOptionType t
, void *p
,
69 const LttvOptionHook h
, void *hook_data
)
73 g_info("Add option %s", long_name
);
74 if(g_hash_table_lookup(options
, long_name
) != NULL
) {
75 g_warning("duplicate option");
79 option
= g_new(LttvOption
, 1);
80 option
->long_name
= g_strdup(long_name
);
81 option
->char_name
= char_name
;
82 option
->description
= g_strdup(description
);
83 option
->arg_description
= g_strdup(arg_description
);
87 option
->hook_data
= hook_data
;
88 g_hash_table_insert(options
, option
->long_name
, option
);
93 lttv_option_remove(const char *long_name
)
95 LttvOption
*option
= g_hash_table_lookup(options
, long_name
);
97 g_info("Remove option %s", long_name
);
99 g_warning("trying to remove unknown option %s", long_name
);
102 g_hash_table_remove(options
, long_name
);
107 static int poptToLTT
[] = {
108 POPT_ARG_NONE
, POPT_ARG_STRING
, POPT_ARG_INT
, POPT_ARG_LONG
111 static struct poptOption endOption
= { NULL
, '\0', 0, NULL
, 0};
115 build_popts(GPtrArray
**plist
, struct poptOption
**ppopts
, poptContext
*pc
,
116 int argc
, char **argv
)
122 struct poptOption
*popts
;
128 list
= g_ptr_array_new();
130 g_hash_table_foreach(options
, list_options
, list
);
132 /* Build a popt options array from our list */
134 popts
= g_new(struct poptOption
, list
->len
+ 1);
136 for(i
= 0 ; i
< list
->len
; i
++) {
137 option
= (LttvOption
*)list
->pdata
[i
];
138 popts
[i
].longName
= option
->long_name
;
139 popts
[i
].shortName
= option
->char_name
;
140 popts
[i
].descrip
= option
->description
;
141 popts
[i
].argDescrip
= option
->arg_description
;
142 popts
[i
].argInfo
= poptToLTT
[option
->t
];
143 popts
[i
].arg
= option
->p
;
144 popts
[i
].val
= i
+ 1;
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);
159 destroy_popts(GPtrArray
**plist
, struct poptOption
**ppopts
, poptContext
*pc
)
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
;
183 build_popts(&list
, &popts
, &c
, argc
, argv
);
185 /* Parse options while not end of options event */
187 while((rc
= poptGetNextOpt(c
)) != -1) {
189 /* The option was recognized and the rc value returned is the argument
190 position in the array. Call the associated hook if present. */
193 option
= (LttvOption
*)(list
->pdata
[rc
- 1]);
194 g_info("Option %s encountered", option
->long_name
);
195 if(option
->hook
!= NULL
) {
196 g_info("Option %s hook called", option
->long_name
);
197 option
->hook(option
->hook_data
);
202 else if(rc
== POPT_ERROR_BADOPT
&& i
!= first_arg
) {
203 g_info("Option %s not recognized, rescan options with new additions",
206 /* Perhaps this option is newly added, restart parsing */
208 destroy_popts(&list
, &popts
, &c
);
209 build_popts(&list
, &popts
, &c
, argc
, argv
);
211 /* Get back to the same argument */
214 for(i
= 0; i
< first_arg
; i
++) {
215 rc
= poptGetNextOpt(c
);
216 option
= (LttvOption
*)(list
->pdata
[rc
- 1]);
217 g_info("Option %s rescanned, skipped", option
->long_name
);
223 /* The option has some error and it is not because this is a newly
224 added option not recognized. */
226 g_error("option %s: %s", poptBadOption(c
,0), poptStrerror(rc
));
232 destroy_popts(&list
, &popts
, &c
);
235 static void show_help(LttvOption
*option
)
237 printf("--%s -%c argument: %s\n" , option
->long_name
,
239 option
->arg_description
);
240 printf(" %s\n" , option
->description
);
244 void lttv_option_show_help(void)
248 GPtrArray
*list
= g_ptr_array_new();
252 g_hash_table_foreach(options
, list_options
, list
);
254 printf("Built-in commands available:\n");
257 for(i
= 0 ; i
< list
->len
; i
++) {
258 show_help((LttvOption
*)list
->pdata
[i
]);
260 g_ptr_array_free(list
, TRUE
);
This page took 0.047963 seconds and 5 git commands to generate.