2 * Copyright 2019 Philippe Proulx <pproulx@efficios.com>
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 #include "common/assert.h"
32 void destroy_item(struct bt_argpar_item
* const item
)
38 if (item
->type
== BT_ARGPAR_ITEM_TYPE_OPT
) {
39 struct bt_argpar_item_opt
* const opt_item
= (void *) item
;
41 g_free((void *) opt_item
->arg
);
51 struct bt_argpar_item_opt
*create_opt_item(
52 const struct bt_argpar_opt_descr
* const descr
,
53 const char * const arg
)
55 struct bt_argpar_item_opt
*opt_item
=
56 g_new0(struct bt_argpar_item_opt
, 1);
62 opt_item
->base
.type
= BT_ARGPAR_ITEM_TYPE_OPT
;
63 opt_item
->descr
= descr
;
66 opt_item
->arg
= g_strdup(arg
);
75 destroy_item(&opt_item
->base
);
83 struct bt_argpar_item_non_opt
*create_non_opt_item(const char * const arg
,
84 const unsigned int orig_index
,
85 const unsigned int non_opt_index
)
87 struct bt_argpar_item_non_opt
* const non_opt_item
=
88 g_new0(struct bt_argpar_item_non_opt
, 1);
94 non_opt_item
->base
.type
= BT_ARGPAR_ITEM_TYPE_NON_OPT
;
95 non_opt_item
->arg
= arg
;
96 non_opt_item
->orig_index
= orig_index
;
97 non_opt_item
->non_opt_index
= non_opt_index
;
104 const struct bt_argpar_opt_descr
*find_descr(
105 const struct bt_argpar_opt_descr
* const descrs
,
106 const char short_name
, const char * const long_name
)
108 const struct bt_argpar_opt_descr
*descr
;
110 for (descr
= descrs
; descr
->short_name
|| descr
->long_name
; descr
++) {
111 if (short_name
&& descr
->short_name
&&
112 short_name
== descr
->short_name
) {
116 if (long_name
&& descr
->long_name
&&
117 strcmp(long_name
, descr
->long_name
) == 0) {
123 return !descr
->short_name
&& !descr
->long_name
? NULL
: descr
;
126 enum parse_orig_arg_opt_ret
{
127 PARSE_ORIG_ARG_OPT_RET_OK
,
128 PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT
= -2,
129 PARSE_ORIG_ARG_OPT_RET_ERROR
= -1,
133 enum parse_orig_arg_opt_ret
parse_short_opts(const char * const short_opts
,
134 const char * const next_orig_arg
,
135 const struct bt_argpar_opt_descr
* const descrs
,
136 struct bt_argpar_parse_ret
* const parse_ret
,
137 bool * const used_next_orig_arg
)
139 enum parse_orig_arg_opt_ret ret
= PARSE_ORIG_ARG_OPT_RET_OK
;
140 const char *short_opt_ch
= short_opts
;
142 if (strlen(short_opts
) == 0) {
143 g_string_append(parse_ret
->error
, "Invalid argument");
147 while (*short_opt_ch
) {
148 const char *opt_arg
= NULL
;
149 const struct bt_argpar_opt_descr
*descr
;
150 struct bt_argpar_item_opt
*opt_item
;
152 /* Find corresponding option descriptor */
153 descr
= find_descr(descrs
, *short_opt_ch
, NULL
);
155 ret
= PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT
;
156 g_string_append_printf(parse_ret
->error
,
157 "Unknown option `-%c`", *short_opt_ch
);
161 if (descr
->with_arg
) {
162 if (short_opt_ch
[1]) {
164 opt_arg
= &short_opt_ch
[1];
167 opt_arg
= next_orig_arg
;
168 *used_next_orig_arg
= true;
172 * We accept `-o ''` (empty option's argument),
173 * but not `-o` alone if an option's argument is
176 if (!opt_arg
|| (short_opt_ch
[1] && strlen(opt_arg
) == 0)) {
177 g_string_append_printf(parse_ret
->error
,
178 "Missing required argument for option `-%c`",
180 *used_next_orig_arg
= false;
185 /* Create and append option argument */
186 opt_item
= create_opt_item(descr
, opt_arg
);
191 g_ptr_array_add(parse_ret
->items
, opt_item
);
193 if (descr
->with_arg
) {
194 /* Option has an argument: no more options */
198 /* Go to next short option */
205 if (ret
== PARSE_ORIG_ARG_OPT_RET_OK
) {
206 ret
= PARSE_ORIG_ARG_OPT_RET_ERROR
;
214 enum parse_orig_arg_opt_ret
parse_long_opt(const char * const long_opt_arg
,
215 const char * const next_orig_arg
,
216 const struct bt_argpar_opt_descr
* const descrs
,
217 struct bt_argpar_parse_ret
* const parse_ret
,
218 bool * const used_next_orig_arg
)
220 const size_t max_len
= 127;
221 enum parse_orig_arg_opt_ret ret
= PARSE_ORIG_ARG_OPT_RET_OK
;
222 const struct bt_argpar_opt_descr
*descr
;
223 struct bt_argpar_item_opt
*opt_item
;
225 /* Option's argument, if any */
226 const char *opt_arg
= NULL
;
228 /* Position of first `=`, if any */
231 /* Buffer holding option name when `long_opt_arg` contains `=` */
232 char buf
[max_len
+ 1];
235 const char *long_opt_name
= long_opt_arg
;
237 if (strlen(long_opt_arg
) == 0) {
238 g_string_append(parse_ret
->error
, "Invalid argument");
242 /* Find the first `=` in original argument */
243 eq_pos
= strchr(long_opt_arg
, '=');
245 const size_t long_opt_name_size
= eq_pos
- long_opt_arg
;
247 /* Isolate the option name */
248 if (long_opt_name_size
> max_len
) {
249 g_string_append_printf(parse_ret
->error
,
250 "Invalid argument `--%s`", long_opt_arg
);
254 memcpy(buf
, long_opt_arg
, long_opt_name_size
);
255 buf
[long_opt_name_size
] = '\0';
259 /* Find corresponding option descriptor */
260 descr
= find_descr(descrs
, '\0', long_opt_name
);
262 g_string_append_printf(parse_ret
->error
,
263 "Unknown option `--%s`", long_opt_name
);
264 ret
= PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT
;
268 /* Find option's argument if any */
269 if (descr
->with_arg
) {
271 /* `--long-opt=arg` style */
272 opt_arg
= eq_pos
+ 1;
274 /* `--long-opt arg` style */
275 if (!next_orig_arg
) {
276 g_string_append_printf(parse_ret
->error
,
277 "Missing required argument for option `--%s`",
282 opt_arg
= next_orig_arg
;
283 *used_next_orig_arg
= true;
287 /* Create and append option argument */
288 opt_item
= create_opt_item(descr
, opt_arg
);
293 g_ptr_array_add(parse_ret
->items
, opt_item
);
297 if (ret
== PARSE_ORIG_ARG_OPT_RET_OK
) {
298 ret
= PARSE_ORIG_ARG_OPT_RET_ERROR
;
306 enum parse_orig_arg_opt_ret
parse_orig_arg_opt(const char * const orig_arg
,
307 const char * const next_orig_arg
,
308 const struct bt_argpar_opt_descr
* const descrs
,
309 struct bt_argpar_parse_ret
* const parse_ret
,
310 bool * const used_next_orig_arg
)
312 enum parse_orig_arg_opt_ret ret
= PARSE_ORIG_ARG_OPT_RET_OK
;
314 BT_ASSERT(orig_arg
[0] == '-');
316 if (orig_arg
[1] == '-') {
318 ret
= parse_long_opt(&orig_arg
[2],
319 next_orig_arg
, descrs
, parse_ret
,
323 ret
= parse_short_opts(&orig_arg
[1],
324 next_orig_arg
, descrs
, parse_ret
,
332 void prepend_while_parsing_arg_to_error(GString
* const error
,
333 const unsigned int i
, const char * const arg
)
335 /* 🙁 There's no g_string_prepend_printf()! */
336 GString
* const tmp_str
= g_string_new(NULL
);
345 g_string_append_printf(tmp_str
, "While parsing argument #%u (`%s`): %s",
346 i
+ 1, arg
, error
->str
);
347 g_string_assign(error
, tmp_str
->str
);
348 g_string_free(tmp_str
, TRUE
);
355 struct bt_argpar_parse_ret
bt_argpar_parse(unsigned int argc
,
356 const char * const *argv
,
357 const struct bt_argpar_opt_descr
* const descrs
,
358 bool fail_on_unknown_opt
)
360 struct bt_argpar_parse_ret parse_ret
= { 0 };
362 unsigned int non_opt_index
= 0;
364 parse_ret
.error
= g_string_new(NULL
);
365 if (!parse_ret
.error
) {
369 parse_ret
.items
= g_ptr_array_new_with_free_func(
370 (GDestroyNotify
) destroy_item
);
371 if (!parse_ret
.items
) {
375 for (i
= 0; i
< argc
; i
++) {
376 enum parse_orig_arg_opt_ret parse_orig_arg_opt_ret
;
377 bool used_next_orig_arg
= false;
378 const char * const orig_arg
= argv
[i
];
379 const char * const next_orig_arg
=
380 i
< argc
- 1 ? argv
[i
+ 1] : NULL
;
382 if (orig_arg
[0] != '-') {
383 /* Non-option argument */
384 struct bt_argpar_item_non_opt
*non_opt_item
=
385 create_non_opt_item(orig_arg
, i
, non_opt_index
);
392 g_ptr_array_add(parse_ret
.items
, non_opt_item
);
396 /* Option argument */
397 parse_orig_arg_opt_ret
= parse_orig_arg_opt(orig_arg
,
398 next_orig_arg
, descrs
, &parse_ret
, &used_next_orig_arg
);
399 switch (parse_orig_arg_opt_ret
) {
400 case PARSE_ORIG_ARG_OPT_RET_OK
:
402 case PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT
:
403 BT_ASSERT(!used_next_orig_arg
);
405 if (fail_on_unknown_opt
) {
406 prepend_while_parsing_arg_to_error(
407 parse_ret
.error
, i
, orig_arg
);
412 * The current original argument is not
413 * considered ingested because it triggered an
416 parse_ret
.ingested_orig_args
= i
;
417 g_string_free(parse_ret
.error
, TRUE
);
418 parse_ret
.error
= NULL
;
420 case PARSE_ORIG_ARG_OPT_RET_ERROR
:
421 prepend_while_parsing_arg_to_error(
422 parse_ret
.error
, i
, orig_arg
);
428 if (used_next_orig_arg
) {
433 parse_ret
.ingested_orig_args
= argc
;
434 g_string_free(parse_ret
.error
, TRUE
);
435 parse_ret
.error
= NULL
;
439 if (parse_ret
.items
) {
440 /* That's how we indicate that an error occured */
441 g_ptr_array_free(parse_ret
.items
, TRUE
);
442 parse_ret
.items
= NULL
;
450 void bt_argpar_parse_ret_fini(struct bt_argpar_parse_ret
*ret
)
455 g_ptr_array_free(ret
->items
, TRUE
);
460 g_string_free(ret
->error
, TRUE
);