3 #include <lttv/option.h>
5 typedef struct _LttvOption
{
8 const char *description
;
9 const char *arg_description
;
20 list_options(gpointer key
, gpointer value
, gpointer user_data
)
22 g_ptr_array_add((GPtrArray
*)user_data
, value
);
27 free_option(LttvOption
*option
)
29 g_free(option
->long_name
);
30 g_free(option
->description
);
31 g_free(option
->arg_description
);
36 void lttv_option_init(int argc
, char **argv
)
38 options
= g_hash_table_new(g_str_hash
, g_str_equal
);
42 void lttv_option_destroy()
46 GPtrArray list
= g_ptr_array_new();
50 g_hash_table_foreach(options
, list_options
, list
);
51 g_hash_table_destroy(options
);
53 for(i
= 0 ; i
< list
->len
; i
++) {
54 free_option((LttvOption
*)list
->pdata
[i
]);
56 g_ptr_array_free(list
, TRUE
);
60 void lttv_option_add(const char *long_name
, const char char_name
,
61 const char *description
, const char *arg_description
,
62 const LttvOptionType t
, void *p
,
63 const LttvOptionHook h
, void *hook_data
)
67 if(g_hash_table_lookup(options
, long_name
) != NULL
) {
68 g_warning("duplicate option");
72 option
= g_new(LttvOption
, 1);
73 option
->long_name
= g_strdup(long_name
);
74 option
->char_name
= char_name
;
75 option
->description
= g_strdup(description
);
76 option
->arg_description
= g_strdup(arg_description
);
80 option
->hook_data
= hook_data
;
81 g_hash_table_insert(options
, option
->long_name
, option
);
86 lttv_option_remove(const char *long_name
)
88 LttvOption
*option
= g_hash_table_lookup(options
, long_name
);
91 g_warning("trying to remove unknown option %s", long_name
);
94 g_hash_table_remove(options
, long_name
);
99 static int poptToLTT
[] = {
100 POPT_ARG_NONE
, POPT_ARG_STRING
, POPT_ARG_INT
, POPT_ARG_LONG
103 static struct poptOption endOption
= { NULL
, '\0', 0, NULL
, 0};
107 build_popts(GPtrArray
**plist
, struct poptOption
**ppopts
, poptContext
*pc
,
108 int argv
, char **argv
)
114 struct poptOption
*popts
;
120 list
= g_ptr_array_new();
122 g_hash_table_foreach(options
, list_options
, list
);
124 /* Build a popt options array from our list */
126 popts
= g_new(struct poptOption
, list
->len
+ 1);
128 for(i
= 0 ; i
< list
->len
; i
++) {
129 option
= (LttvOption
*)list
->pdata
[i
];
130 popts
[i
].longName
= option
->long_name
;
131 popts
[i
].shortName
= option
->char_name
;
132 popts
[i
].descrip
= option
->description
;
133 popts
[i
].argDescrip
= option
->arg_description
;
134 popts
[i
].argInfo
= poptToLTT
[option
->t
];
135 popts
[i
].arg
= option
->p
;
136 popts
[i
].val
= i
+ 1;
139 /* Terminate the array for popt and create the context */
141 popts
[list
->len
] = endOption
;
142 c
= poptGetContext(argv
[0], argc
, (const char**)argv
, popts
, 0);
151 destroy_popts(GPtrArray
**plist
, struct poptOption
**ppopts
, poptContext
*pc
)
153 g_ptr_array_free(*plist
, TRUE
); *plist
= NULL
;
154 g_free(*ppopts
); *ppopts
= NULL
;
159 void lttv_option_parse(int argc
, char **argv
)
165 int i
, rc
, first_arg
;
167 struct poptOption
*popts
;
175 build_popts(&list
, &popts
, &c
, argc
, argv
);
177 /* Parse options while not end of options event */
179 while((rc
= poptGetNextOpt(c
)) != -1) {
181 /* The option was recognized and the rc value returned is the argument
182 position in the array. Call the associated hook if present. */
185 option
= (LttvOption
*)(list
->pdata
[rc
- 1]);
186 if(option
->hook
!= NULL
) option
->hook(option
->hook_data
);
190 else if(rc
== POPT_ERROR_BADOPT
&& i
!= first_arg
) {
192 /* Perhaps this option is newly added, restart parsing */
194 destroy_popts(&list
, &popts
, &c
);
195 build_popts(&list
, &popts
, &c
, argc
, argv
);
197 /* Get back to the same argument */
200 for(i
= 0; i
< first_arg
; i
++) poptGetNextOpt(c
);
205 /* The option has some error and it is not because this is a newly
206 added option not recognized. */
208 g_error("option %s: %s", poptBadOption(c
,0), poptStrerror(rc
));
214 destroy_popts(&list
, &popts
, &c
);
This page took 0.033882 seconds and 5 git commands to generate.