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
32 #define argpar_realloc(_ptr, _type, _nmemb) ((_type *) realloc(_ptr, (_nmemb) * sizeof(_type)))
33 #define argpar_calloc(_type, _nmemb) ((_type *) calloc((_nmemb), sizeof(_type)))
34 #define argpar_zalloc(_type) argpar_calloc(_type, 1)
36 #define ARGPAR_ASSERT(_cond) assert(_cond)
39 char *argpar_vasprintf(const char *fmt
, va_list args
)
47 len1
= vsnprintf(NULL
, 0, fmt
, args
);
53 str
= malloc(len1
+ 1);
58 len2
= vsnprintf(str
, len1
+ 1, fmt
, args2
);
60 ARGPAR_ASSERT(len1
== len2
);
68 char *argpar_asprintf(const char *fmt
, ...)
74 str
= argpar_vasprintf(fmt
, args
);
81 bool argpar_string_append_printf(char **str
, const char *fmt
, ...)
91 addendum
= argpar_vasprintf(fmt
, args
);
99 new_str
= argpar_asprintf("%s%s", *str
? *str
: "", addendum
);
117 void destroy_item(struct argpar_item
* const item
)
123 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
124 struct argpar_item_opt
* const opt_item
= (void *) item
;
126 free((void *) opt_item
->arg
);
136 bool push_item(struct argpar_item_array
* const array
,
137 struct argpar_item
* const item
)
141 ARGPAR_ASSERT(array
);
144 if (array
->n_items
== array
->n_alloc
) {
145 unsigned int new_n_alloc
= array
->n_alloc
* 2;
146 struct argpar_item
**new_items
;
148 new_items
= argpar_realloc(array
->items
,
149 struct argpar_item
*, new_n_alloc
);
155 array
->n_alloc
= new_n_alloc
;
156 array
->items
= new_items
;
159 array
->items
[array
->n_items
] = item
;
169 void destroy_item_array(struct argpar_item_array
* const array
)
174 for (i
= 0; i
< array
->n_items
; i
++) {
175 destroy_item(array
->items
[i
]);
184 struct argpar_item_array
*new_item_array(void)
186 struct argpar_item_array
*ret
;
187 const int initial_size
= 10;
189 ret
= argpar_zalloc(struct argpar_item_array
);
194 ret
->items
= argpar_calloc(struct argpar_item
*, initial_size
);
199 ret
->n_alloc
= initial_size
;
204 destroy_item_array(ret
);
212 struct argpar_item_opt
*create_opt_item(
213 const struct argpar_opt_descr
* const descr
,
214 const char * const arg
)
216 struct argpar_item_opt
*opt_item
=
217 argpar_zalloc(struct argpar_item_opt
);
223 opt_item
->base
.type
= ARGPAR_ITEM_TYPE_OPT
;
224 opt_item
->descr
= descr
;
227 opt_item
->arg
= strdup(arg
);
228 if (!opt_item
->arg
) {
236 destroy_item(&opt_item
->base
);
244 struct argpar_item_non_opt
*create_non_opt_item(const char * const arg
,
245 const unsigned int orig_index
,
246 const unsigned int non_opt_index
)
248 struct argpar_item_non_opt
* const non_opt_item
=
249 argpar_zalloc(struct argpar_item_non_opt
);
255 non_opt_item
->base
.type
= ARGPAR_ITEM_TYPE_NON_OPT
;
256 non_opt_item
->arg
= arg
;
257 non_opt_item
->orig_index
= orig_index
;
258 non_opt_item
->non_opt_index
= non_opt_index
;
265 const struct argpar_opt_descr
*find_descr(
266 const struct argpar_opt_descr
* const descrs
,
267 const char short_name
, const char * const long_name
)
269 const struct argpar_opt_descr
*descr
;
271 for (descr
= descrs
; descr
->short_name
|| descr
->long_name
; descr
++) {
272 if (short_name
&& descr
->short_name
&&
273 short_name
== descr
->short_name
) {
277 if (long_name
&& descr
->long_name
&&
278 strcmp(long_name
, descr
->long_name
) == 0) {
284 return !descr
->short_name
&& !descr
->long_name
? NULL
: descr
;
287 enum parse_orig_arg_opt_ret
{
288 PARSE_ORIG_ARG_OPT_RET_OK
,
289 PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT
= -2,
290 PARSE_ORIG_ARG_OPT_RET_ERROR
= -1,
294 enum parse_orig_arg_opt_ret
parse_short_opts(const char * const short_opts
,
295 const char * const next_orig_arg
,
296 const struct argpar_opt_descr
* const descrs
,
297 struct argpar_parse_ret
* const parse_ret
,
298 bool * const used_next_orig_arg
)
300 enum parse_orig_arg_opt_ret ret
= PARSE_ORIG_ARG_OPT_RET_OK
;
301 const char *short_opt_ch
= short_opts
;
303 if (strlen(short_opts
) == 0) {
304 argpar_string_append_printf(&parse_ret
->error
, "Invalid argument");
308 while (*short_opt_ch
) {
309 const char *opt_arg
= NULL
;
310 const struct argpar_opt_descr
*descr
;
311 struct argpar_item_opt
*opt_item
;
313 /* Find corresponding option descriptor */
314 descr
= find_descr(descrs
, *short_opt_ch
, NULL
);
316 ret
= PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT
;
317 argpar_string_append_printf(&parse_ret
->error
,
318 "Unknown option `-%c`", *short_opt_ch
);
322 if (descr
->with_arg
) {
323 if (short_opt_ch
[1]) {
325 opt_arg
= &short_opt_ch
[1];
328 opt_arg
= next_orig_arg
;
329 *used_next_orig_arg
= true;
333 * We accept `-o ''` (empty option's argument),
334 * but not `-o` alone if an option's argument is
337 if (!opt_arg
|| (short_opt_ch
[1] && strlen(opt_arg
) == 0)) {
338 argpar_string_append_printf(&parse_ret
->error
,
339 "Missing required argument for option `-%c`",
341 *used_next_orig_arg
= false;
346 /* Create and append option argument */
347 opt_item
= create_opt_item(descr
, opt_arg
);
352 if (!push_item(parse_ret
->items
, &opt_item
->base
)) {
356 if (descr
->with_arg
) {
357 /* Option has an argument: no more options */
361 /* Go to next short option */
368 if (ret
== PARSE_ORIG_ARG_OPT_RET_OK
) {
369 ret
= PARSE_ORIG_ARG_OPT_RET_ERROR
;
377 enum parse_orig_arg_opt_ret
parse_long_opt(const char * const long_opt_arg
,
378 const char * const next_orig_arg
,
379 const struct argpar_opt_descr
* const descrs
,
380 struct argpar_parse_ret
* const parse_ret
,
381 bool * const used_next_orig_arg
)
383 const size_t max_len
= 127;
384 enum parse_orig_arg_opt_ret ret
= PARSE_ORIG_ARG_OPT_RET_OK
;
385 const struct argpar_opt_descr
*descr
;
386 struct argpar_item_opt
*opt_item
;
388 /* Option's argument, if any */
389 const char *opt_arg
= NULL
;
391 /* Position of first `=`, if any */
394 /* Buffer holding option name when `long_opt_arg` contains `=` */
395 char buf
[max_len
+ 1];
398 const char *long_opt_name
= long_opt_arg
;
400 if (strlen(long_opt_arg
) == 0) {
401 argpar_string_append_printf(&parse_ret
->error
,
406 /* Find the first `=` in original argument */
407 eq_pos
= strchr(long_opt_arg
, '=');
409 const size_t long_opt_name_size
= eq_pos
- long_opt_arg
;
411 /* Isolate the option name */
412 if (long_opt_name_size
> max_len
) {
413 argpar_string_append_printf(&parse_ret
->error
,
414 "Invalid argument `--%s`", long_opt_arg
);
418 memcpy(buf
, long_opt_arg
, long_opt_name_size
);
419 buf
[long_opt_name_size
] = '\0';
423 /* Find corresponding option descriptor */
424 descr
= find_descr(descrs
, '\0', long_opt_name
);
426 argpar_string_append_printf(&parse_ret
->error
,
427 "Unknown option `--%s`", long_opt_name
);
428 ret
= PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT
;
432 /* Find option's argument if any */
433 if (descr
->with_arg
) {
435 /* `--long-opt=arg` style */
436 opt_arg
= eq_pos
+ 1;
438 /* `--long-opt arg` style */
439 if (!next_orig_arg
) {
440 argpar_string_append_printf(&parse_ret
->error
,
441 "Missing required argument for option `--%s`",
446 opt_arg
= next_orig_arg
;
447 *used_next_orig_arg
= true;
451 /* Create and append option argument */
452 opt_item
= create_opt_item(descr
, opt_arg
);
457 if (!push_item(parse_ret
->items
, &opt_item
->base
)) {
464 if (ret
== PARSE_ORIG_ARG_OPT_RET_OK
) {
465 ret
= PARSE_ORIG_ARG_OPT_RET_ERROR
;
473 enum parse_orig_arg_opt_ret
parse_orig_arg_opt(const char * const orig_arg
,
474 const char * const next_orig_arg
,
475 const struct argpar_opt_descr
* const descrs
,
476 struct argpar_parse_ret
* const parse_ret
,
477 bool * const used_next_orig_arg
)
479 enum parse_orig_arg_opt_ret ret
= PARSE_ORIG_ARG_OPT_RET_OK
;
481 ARGPAR_ASSERT(orig_arg
[0] == '-');
483 if (orig_arg
[1] == '-') {
485 ret
= parse_long_opt(&orig_arg
[2],
486 next_orig_arg
, descrs
, parse_ret
,
490 ret
= parse_short_opts(&orig_arg
[1],
491 next_orig_arg
, descrs
, parse_ret
,
499 bool prepend_while_parsing_arg_to_error(char **error
,
500 const unsigned int i
, const char * const arg
)
505 ARGPAR_ASSERT(error
);
506 ARGPAR_ASSERT(*error
);
508 new_error
= argpar_asprintf("While parsing argument #%u (`%s`): %s",
524 struct argpar_parse_ret
argpar_parse(unsigned int argc
,
525 const char * const *argv
,
526 const struct argpar_opt_descr
* const descrs
,
527 bool fail_on_unknown_opt
)
529 struct argpar_parse_ret parse_ret
= { 0 };
531 unsigned int non_opt_index
= 0;
533 parse_ret
.items
= new_item_array();
534 if (!parse_ret
.items
) {
538 for (i
= 0; i
< argc
; i
++) {
539 enum parse_orig_arg_opt_ret parse_orig_arg_opt_ret
;
540 bool used_next_orig_arg
= false;
541 const char * const orig_arg
= argv
[i
];
542 const char * const next_orig_arg
=
543 i
< argc
- 1 ? argv
[i
+ 1] : NULL
;
545 if (orig_arg
[0] != '-') {
546 /* Non-option argument */
547 struct argpar_item_non_opt
*non_opt_item
=
548 create_non_opt_item(orig_arg
, i
, non_opt_index
);
556 if (!push_item(parse_ret
.items
, &non_opt_item
->base
)) {
563 /* Option argument */
564 parse_orig_arg_opt_ret
= parse_orig_arg_opt(orig_arg
,
565 next_orig_arg
, descrs
, &parse_ret
, &used_next_orig_arg
);
566 switch (parse_orig_arg_opt_ret
) {
567 case PARSE_ORIG_ARG_OPT_RET_OK
:
569 case PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT
:
570 ARGPAR_ASSERT(!used_next_orig_arg
);
572 if (fail_on_unknown_opt
) {
573 prepend_while_parsing_arg_to_error(
574 &parse_ret
.error
, i
, orig_arg
);
579 * The current original argument is not
580 * considered ingested because it triggered an
583 parse_ret
.ingested_orig_args
= i
;
584 free(parse_ret
.error
);
585 parse_ret
.error
= NULL
;
587 case PARSE_ORIG_ARG_OPT_RET_ERROR
:
588 prepend_while_parsing_arg_to_error(
589 &parse_ret
.error
, i
, orig_arg
);
595 if (used_next_orig_arg
) {
600 parse_ret
.ingested_orig_args
= argc
;
601 free(parse_ret
.error
);
602 parse_ret
.error
= NULL
;
606 /* That's how we indicate that an error occured */
607 destroy_item_array(parse_ret
.items
);
608 parse_ret
.items
= NULL
;
615 void argpar_parse_ret_fini(struct argpar_parse_ret
*ret
)
619 destroy_item_array(ret
->items
);