2 * SPDX-License-Identifier: MIT
4 * Copyright (c) 2019-2021 Philippe Proulx <pproulx@efficios.com>
5 * Copyright (c) 2020-2021 Simon Marchi <simon.marchi@efficios.com>
17 #define ARGPAR_REALLOC(_ptr, _type, _nmemb) \
18 ((_type *) realloc(_ptr, (_nmemb) * sizeof(_type)))
20 #define ARGPAR_CALLOC(_type, _nmemb) \
21 ((_type *) calloc((_nmemb), sizeof(_type)))
23 #define ARGPAR_ZALLOC(_type) ARGPAR_CALLOC(_type, 1)
25 #define ARGPAR_ASSERT(_cond) assert(_cond)
27 #ifdef __MINGW_PRINTF_FORMAT
28 # define ARGPAR_PRINTF_FORMAT __MINGW_PRINTF_FORMAT
30 # define ARGPAR_PRINTF_FORMAT printf
36 * Such a structure contains the state of an iterator between calls to
41 * Data provided by the user to argpar_iter_create(); immutable
45 const char * const *argv
;
46 const struct argpar_opt_descr
*descrs
;
49 * Index of the argument to process in the next
50 * argpar_iter_next() call.
54 /* Counter of non-option arguments */
58 * Current character of the current short option group: if it's
59 * not `NULL`, the parser is in within a short option group,
60 * therefore it must resume there in the next
61 * argpar_iter_next() call.
63 const char *short_opt_ch
;
65 /* Temporary character buffer which only grows */
72 /* Base parsing item */
74 enum argpar_item_type type
;
77 /* Option parsing item */
78 struct argpar_item_opt
{
79 struct argpar_item base
;
81 /* Corresponding descriptor */
82 const struct argpar_opt_descr
*descr
;
84 /* Argument, or `NULL` if none; owned by this */
88 /* Non-option parsing item */
89 struct argpar_item_non_opt
{
90 struct argpar_item base
;
93 * Complete argument, pointing to one of the entries of the
94 * original arguments (`argv`).
99 * Index of this argument amongst all original arguments
102 unsigned int orig_index
;
104 /* Index of this argument amongst other non-option arguments */
105 unsigned int non_opt_index
;
108 static __attribute__((format(ARGPAR_PRINTF_FORMAT
, 1, 0)))
109 char *argpar_vasprintf(const char * const fmt
, va_list args
)
115 va_copy(args2
, args
);
116 len1
= vsnprintf(NULL
, 0, fmt
, args
);
122 str
= malloc(len1
+ 1);
127 len2
= vsnprintf(str
, len1
+ 1, fmt
, args2
);
128 ARGPAR_ASSERT(len1
== len2
);
136 static __attribute__((format(ARGPAR_PRINTF_FORMAT
, 1, 2)))
137 char *argpar_asprintf(const char * const fmt
, ...)
143 str
= argpar_vasprintf(fmt
, args
);
148 static __attribute__((format(ARGPAR_PRINTF_FORMAT
, 2, 3)))
149 bool try_append_string_printf(char ** const str
, const char *fmt
, ...)
151 char *new_str
= NULL
;
152 char *addendum
= NULL
;
163 addendum
= argpar_vasprintf(fmt
, args
);
171 new_str
= argpar_asprintf("%s%s", *str
? *str
: "", addendum
);
187 enum argpar_item_type
argpar_item_type(const struct argpar_item
* const item
)
194 const struct argpar_opt_descr
*argpar_item_opt_descr(
195 const struct argpar_item
* const item
)
198 ARGPAR_ASSERT(item
->type
== ARGPAR_ITEM_TYPE_OPT
);
199 return ((const struct argpar_item_opt
*) item
)->descr
;
203 const char *argpar_item_opt_arg(const struct argpar_item
* const item
)
206 ARGPAR_ASSERT(item
->type
== ARGPAR_ITEM_TYPE_OPT
);
207 return ((const struct argpar_item_opt
*) item
)->arg
;
211 const char *argpar_item_non_opt_arg(const struct argpar_item
* const item
)
214 ARGPAR_ASSERT(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
215 return ((const struct argpar_item_non_opt
*) item
)->arg
;
219 unsigned int argpar_item_non_opt_orig_index(
220 const struct argpar_item
* const item
)
223 ARGPAR_ASSERT(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
224 return ((const struct argpar_item_non_opt
*) item
)->orig_index
;
228 unsigned int argpar_item_non_opt_non_opt_index(
229 const struct argpar_item
* const item
)
232 ARGPAR_ASSERT(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
233 return ((const struct argpar_item_non_opt
*) item
)->non_opt_index
;
237 void argpar_item_destroy(const struct argpar_item
* const item
)
243 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
244 struct argpar_item_opt
* const opt_item
=
245 (struct argpar_item_opt
*) item
;
257 bool push_item(struct argpar_item_array
* const array
,
258 const struct argpar_item
* const item
)
262 ARGPAR_ASSERT(array
);
265 if (array
->n_items
== array
->n_alloc
) {
266 const unsigned int new_n_alloc
= array
->n_alloc
* 2;
267 const struct argpar_item
** const new_items
=
268 ARGPAR_REALLOC(array
->items
, const struct argpar_item
*,
275 array
->n_alloc
= new_n_alloc
;
276 array
->items
= new_items
;
279 array
->items
[array
->n_items
] = item
;
288 void destroy_item_array(struct argpar_item_array
* const array
)
293 for (i
= 0; i
< array
->n_items
; i
++) {
294 argpar_item_destroy(array
->items
[i
]);
303 struct argpar_item_array
*create_item_array(void)
305 struct argpar_item_array
*ret
;
306 const int initial_size
= 10;
308 ret
= ARGPAR_ZALLOC(struct argpar_item_array
);
313 ret
->items
= ARGPAR_CALLOC(const struct argpar_item
*, initial_size
);
318 ret
->n_alloc
= initial_size
;
322 destroy_item_array(ret
);
330 struct argpar_item_opt
*create_opt_item(
331 const struct argpar_opt_descr
* const descr
,
332 const char * const arg
)
334 struct argpar_item_opt
*opt_item
=
335 ARGPAR_ZALLOC(struct argpar_item_opt
);
341 opt_item
->base
.type
= ARGPAR_ITEM_TYPE_OPT
;
342 opt_item
->descr
= descr
;
345 opt_item
->arg
= strdup(arg
);
346 if (!opt_item
->arg
) {
354 argpar_item_destroy(&opt_item
->base
);
362 struct argpar_item_non_opt
*create_non_opt_item(const char * const arg
,
363 const unsigned int orig_index
,
364 const unsigned int non_opt_index
)
366 struct argpar_item_non_opt
* const non_opt_item
=
367 ARGPAR_ZALLOC(struct argpar_item_non_opt
);
373 non_opt_item
->base
.type
= ARGPAR_ITEM_TYPE_NON_OPT
;
374 non_opt_item
->arg
= arg
;
375 non_opt_item
->orig_index
= orig_index
;
376 non_opt_item
->non_opt_index
= non_opt_index
;
383 const struct argpar_opt_descr
*find_descr(
384 const struct argpar_opt_descr
* const descrs
,
385 const char short_name
, const char * const long_name
)
387 const struct argpar_opt_descr
*descr
;
389 for (descr
= descrs
; descr
->short_name
|| descr
->long_name
; descr
++) {
390 if (short_name
&& descr
->short_name
&&
391 short_name
== descr
->short_name
) {
395 if (long_name
&& descr
->long_name
&&
396 strcmp(long_name
, descr
->long_name
) == 0) {
402 return !descr
->short_name
&& !descr
->long_name
? NULL
: descr
;
405 enum parse_orig_arg_opt_ret
{
406 PARSE_ORIG_ARG_OPT_RET_OK
,
407 PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT
= -1,
408 PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG
= -2,
409 PARSE_ORIG_ARG_OPT_RET_ERROR_UNEXPECTED_OPT_ARG
= -4,
410 PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY
= -5,
414 enum parse_orig_arg_opt_ret
parse_short_opts(const char * const short_opts
,
415 const char * const next_orig_arg
,
416 const struct argpar_opt_descr
* const descrs
,
417 struct argpar_iter
* const iter
,
418 char ** const error
, struct argpar_item
** const item
)
420 enum parse_orig_arg_opt_ret ret
= PARSE_ORIG_ARG_OPT_RET_OK
;
421 bool used_next_orig_arg
= false;
422 const char *opt_arg
= NULL
;
423 const struct argpar_opt_descr
*descr
;
424 struct argpar_item_opt
*opt_item
;
426 ARGPAR_ASSERT(strlen(short_opts
) != 0);
428 if (!iter
->short_opt_ch
) {
429 iter
->short_opt_ch
= short_opts
;
432 /* Find corresponding option descriptor */
433 descr
= find_descr(descrs
, *iter
->short_opt_ch
, NULL
);
435 try_append_string_printf(error
, "Unknown option `-%c`",
436 *iter
->short_opt_ch
);
437 ret
= PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT
;
441 if (descr
->with_arg
) {
442 if (iter
->short_opt_ch
[1]) {
444 opt_arg
= &iter
->short_opt_ch
[1];
447 opt_arg
= next_orig_arg
;
448 used_next_orig_arg
= true;
452 * We accept `-o ''` (empty option argument), but not
453 * `-o` alone if an option argument is expected.
455 if (!opt_arg
|| (iter
->short_opt_ch
[1] &&
456 strlen(opt_arg
) == 0)) {
457 try_append_string_printf(error
,
458 "Missing required argument for option `-%c`",
459 *iter
->short_opt_ch
);
460 used_next_orig_arg
= false;
461 ret
= PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG
;
466 /* Create and append option argument */
467 opt_item
= create_opt_item(descr
, opt_arg
);
469 ret
= PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY
;
473 *item
= &opt_item
->base
;
474 iter
->short_opt_ch
++;
476 if (descr
->with_arg
|| !*iter
->short_opt_ch
) {
477 /* Option has an argument: no more options */
478 iter
->short_opt_ch
= NULL
;
480 if (used_next_orig_arg
) {
490 ARGPAR_ASSERT(ret
!= PARSE_ORIG_ARG_OPT_RET_OK
);
497 enum parse_orig_arg_opt_ret
parse_long_opt(const char * const long_opt_arg
,
498 const char * const next_orig_arg
,
499 const struct argpar_opt_descr
* const descrs
,
500 struct argpar_iter
* const iter
,
501 char ** const error
, struct argpar_item
** const item
)
503 enum parse_orig_arg_opt_ret ret
= PARSE_ORIG_ARG_OPT_RET_OK
;
504 const struct argpar_opt_descr
*descr
;
505 struct argpar_item_opt
*opt_item
;
506 bool used_next_orig_arg
= false;
508 /* Option's argument, if any */
509 const char *opt_arg
= NULL
;
511 /* Position of first `=`, if any */
515 const char *long_opt_name
= long_opt_arg
;
517 ARGPAR_ASSERT(strlen(long_opt_arg
) != 0);
519 /* Find the first `=` in original argument */
520 eq_pos
= strchr(long_opt_arg
, '=');
522 const size_t long_opt_name_size
= eq_pos
- long_opt_arg
;
524 /* Isolate the option name */
525 while (long_opt_name_size
> iter
->tmp_buf
.size
- 1) {
526 iter
->tmp_buf
.size
*= 2;
527 iter
->tmp_buf
.data
= ARGPAR_REALLOC(iter
->tmp_buf
.data
,
528 char, iter
->tmp_buf
.size
);
529 if (!iter
->tmp_buf
.data
) {
530 ret
= PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY
;
535 memcpy(iter
->tmp_buf
.data
, long_opt_arg
, long_opt_name_size
);
536 iter
->tmp_buf
.data
[long_opt_name_size
] = '\0';
537 long_opt_name
= iter
->tmp_buf
.data
;
540 /* Find corresponding option descriptor */
541 descr
= find_descr(descrs
, '\0', long_opt_name
);
543 try_append_string_printf(error
, "Unknown option `--%s`",
545 ret
= PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT
;
549 /* Find option's argument if any */
550 if (descr
->with_arg
) {
552 /* `--long-opt=arg` style */
553 opt_arg
= eq_pos
+ 1;
555 /* `--long-opt arg` style */
556 if (!next_orig_arg
) {
557 try_append_string_printf(error
,
558 "Missing required argument for option `--%s`",
560 ret
= PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG
;
564 opt_arg
= next_orig_arg
;
565 used_next_orig_arg
= true;
569 * Unexpected `--opt=arg` style for a long option which
570 * doesn't accept an argument.
572 try_append_string_printf(error
,
573 "Unexpected argument for option `--%s`", long_opt_name
);
574 ret
= PARSE_ORIG_ARG_OPT_RET_ERROR_UNEXPECTED_OPT_ARG
;
578 /* Create and append option argument */
579 opt_item
= create_opt_item(descr
, opt_arg
);
584 if (used_next_orig_arg
) {
590 *item
= &opt_item
->base
;
594 ARGPAR_ASSERT(ret
!= PARSE_ORIG_ARG_OPT_RET_OK
);
601 enum parse_orig_arg_opt_ret
parse_orig_arg_opt(const char * const orig_arg
,
602 const char * const next_orig_arg
,
603 const struct argpar_opt_descr
* const descrs
,
604 struct argpar_iter
* const iter
, char ** const error
,
605 struct argpar_item
** const item
)
607 enum parse_orig_arg_opt_ret ret
= PARSE_ORIG_ARG_OPT_RET_OK
;
609 ARGPAR_ASSERT(orig_arg
[0] == '-');
611 if (orig_arg
[1] == '-') {
613 ret
= parse_long_opt(&orig_arg
[2],
614 next_orig_arg
, descrs
, iter
, error
, item
);
617 ret
= parse_short_opts(&orig_arg
[1],
618 next_orig_arg
, descrs
, iter
, error
, item
);
625 bool try_prepend_while_parsing_arg_to_error(char ** const error
,
626 const unsigned int i
, const char * const arg
)
636 ARGPAR_ASSERT(*error
);
637 new_error
= argpar_asprintf("While parsing argument #%u (`%s`): %s",
653 struct argpar_iter
*argpar_iter_create(const unsigned int argc
,
654 const char * const * const argv
,
655 const struct argpar_opt_descr
* const descrs
)
657 struct argpar_iter
*iter
= ARGPAR_ZALLOC(struct argpar_iter
);
665 iter
->descrs
= descrs
;
666 iter
->tmp_buf
.size
= 128;
667 iter
->tmp_buf
.data
= ARGPAR_CALLOC(char, iter
->tmp_buf
.size
);
668 if (!iter
->tmp_buf
.data
) {
669 argpar_iter_destroy(iter
);
679 void argpar_iter_destroy(struct argpar_iter
* const iter
)
682 free(iter
->tmp_buf
.data
);
688 enum argpar_iter_next_status
argpar_iter_next(
689 struct argpar_iter
* const iter
,
690 const struct argpar_item
** const item
, char ** const error
)
692 enum argpar_iter_next_status status
;
693 enum parse_orig_arg_opt_ret parse_orig_arg_opt_ret
;
694 const char *orig_arg
;
695 const char *next_orig_arg
;
697 ARGPAR_ASSERT(iter
->i
<= iter
->argc
);
703 if (iter
->i
== iter
->argc
) {
704 status
= ARGPAR_ITER_NEXT_STATUS_END
;
708 orig_arg
= iter
->argv
[iter
->i
];
710 iter
->i
< (iter
->argc
- 1) ? iter
->argv
[iter
->i
+ 1] : NULL
;
712 if (strcmp(orig_arg
, "-") == 0 || strcmp(orig_arg
, "--") == 0 ||
713 orig_arg
[0] != '-') {
714 /* Non-option argument */
715 const struct argpar_item_non_opt
* const non_opt_item
=
716 create_non_opt_item(orig_arg
, iter
->i
,
717 iter
->non_opt_index
);
720 status
= ARGPAR_ITER_NEXT_STATUS_ERROR_MEMORY
;
724 iter
->non_opt_index
++;
726 *item
= &non_opt_item
->base
;
727 status
= ARGPAR_ITER_NEXT_STATUS_OK
;
731 /* Option argument */
732 parse_orig_arg_opt_ret
= parse_orig_arg_opt(orig_arg
,
733 next_orig_arg
, iter
->descrs
, iter
, error
,
734 (struct argpar_item
**) item
);
735 switch (parse_orig_arg_opt_ret
) {
736 case PARSE_ORIG_ARG_OPT_RET_OK
:
737 status
= ARGPAR_ITER_NEXT_STATUS_OK
;
739 case PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT
:
740 case PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG
:
741 case PARSE_ORIG_ARG_OPT_RET_ERROR_UNEXPECTED_OPT_ARG
:
742 try_prepend_while_parsing_arg_to_error(error
, iter
->i
,
745 switch (parse_orig_arg_opt_ret
) {
746 case PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT
:
747 status
= ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT
;
749 case PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG
:
750 status
= ARGPAR_ITER_NEXT_STATUS_ERROR_MISSING_OPT_ARG
;
752 case PARSE_ORIG_ARG_OPT_RET_ERROR_UNEXPECTED_OPT_ARG
:
753 status
= ARGPAR_ITER_NEXT_STATUS_ERROR_UNEXPECTED_OPT_ARG
;
760 case PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY
:
761 status
= ARGPAR_ITER_NEXT_STATUS_ERROR_MEMORY
;
772 unsigned int argpar_iter_ingested_orig_args(
773 const struct argpar_iter
* const iter
)
779 struct argpar_parse_ret
argpar_parse(const unsigned int argc
,
780 const char * const * const argv
,
781 const struct argpar_opt_descr
* const descrs
,
782 const bool fail_on_unknown_opt
)
784 struct argpar_parse_ret parse_ret
= { 0 };
785 const struct argpar_item
*item
= NULL
;
786 struct argpar_iter
*iter
= NULL
;
788 parse_ret
.items
= create_item_array();
789 if (!parse_ret
.items
) {
790 parse_ret
.error
= strdup("Failed to create items array.");
791 ARGPAR_ASSERT(parse_ret
.error
);
795 iter
= argpar_iter_create(argc
, argv
, descrs
);
797 parse_ret
.error
= strdup("Failed to create argpar iter.");
798 ARGPAR_ASSERT(parse_ret
.error
);
803 const enum argpar_iter_next_status status
=
804 argpar_iter_next(iter
, &item
, &parse_ret
.error
);
807 case ARGPAR_ITER_NEXT_STATUS_ERROR_MISSING_OPT_ARG
:
808 case ARGPAR_ITER_NEXT_STATUS_ERROR_UNEXPECTED_OPT_ARG
:
809 case ARGPAR_ITER_NEXT_STATUS_ERROR_MEMORY
:
811 case ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT
:
812 if (fail_on_unknown_opt
) {
813 parse_ret
.ingested_orig_args
=
814 argpar_iter_ingested_orig_args(iter
);
818 free(parse_ret
.error
);
819 parse_ret
.error
= NULL
;
821 case ARGPAR_ITER_NEXT_STATUS_END
:
824 ARGPAR_ASSERT(status
== ARGPAR_ITER_NEXT_STATUS_OK
);
828 if (!push_item(parse_ret
.items
, item
)) {
836 ARGPAR_ASSERT(!parse_ret
.error
);
837 parse_ret
.ingested_orig_args
= argpar_iter_ingested_orig_args(iter
);
841 ARGPAR_ASSERT(parse_ret
.error
);
843 /* That's how we indicate that an error occurred */
844 destroy_item_array(parse_ret
.items
);
845 parse_ret
.items
= NULL
;
848 argpar_iter_destroy(iter
);
849 argpar_item_destroy(item
);
854 void argpar_parse_ret_fini(struct argpar_parse_ret
* const ret
)
857 destroy_item_array(ret
->items
);