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
37 * calls to argpar_iter_parse_next().
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_parse_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_parse_next() call.
63 const char *short_opt_ch
;
66 /* Base parsing item */
68 enum argpar_item_type type
;
71 /* Option parsing item */
72 struct argpar_item_opt
{
73 struct argpar_item base
;
75 /* Corresponding descriptor */
76 const struct argpar_opt_descr
*descr
;
78 /* Argument, or `NULL` if none; owned by this */
82 /* Non-option parsing item */
83 struct argpar_item_non_opt
{
84 struct argpar_item base
;
87 * Complete argument, pointing to one of the entries of the
88 * original arguments (`argv`).
93 * Index of this argument amongst all original arguments
96 unsigned int orig_index
;
98 /* Index of this argument amongst other non-option arguments */
99 unsigned int non_opt_index
;
102 static __attribute__((format(ARGPAR_PRINTF_FORMAT
, 1, 0)))
103 char *argpar_vasprintf(const char * const fmt
, va_list args
)
109 va_copy(args2
, args
);
110 len1
= vsnprintf(NULL
, 0, fmt
, args
);
116 str
= malloc(len1
+ 1);
121 len2
= vsnprintf(str
, len1
+ 1, fmt
, args2
);
122 ARGPAR_ASSERT(len1
== len2
);
130 static __attribute__((format(ARGPAR_PRINTF_FORMAT
, 1, 2)))
131 char *argpar_asprintf(const char * const fmt
, ...)
137 str
= argpar_vasprintf(fmt
, args
);
142 static __attribute__((format(ARGPAR_PRINTF_FORMAT
, 2, 3)))
143 bool append_string_printf(char ** const str
, const char *fmt
, ...)
145 char *new_str
= NULL
;
152 addendum
= argpar_vasprintf(fmt
, args
);
160 new_str
= argpar_asprintf("%s%s", *str
? *str
: "", addendum
);
176 enum argpar_item_type
argpar_item_type(const struct argpar_item
* const item
)
183 const struct argpar_opt_descr
*argpar_item_opt_descr(
184 const struct argpar_item
* const item
)
187 ARGPAR_ASSERT(item
->type
== ARGPAR_ITEM_TYPE_OPT
);
188 return ((const struct argpar_item_opt
*) item
)->descr
;
192 const char *argpar_item_opt_arg(const struct argpar_item
* const item
)
195 ARGPAR_ASSERT(item
->type
== ARGPAR_ITEM_TYPE_OPT
);
196 return ((const struct argpar_item_opt
*) item
)->arg
;
200 const char *argpar_item_non_opt_arg(const struct argpar_item
* const item
)
203 ARGPAR_ASSERT(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
204 return ((const struct argpar_item_non_opt
*) item
)->arg
;
208 unsigned int argpar_item_non_opt_orig_index(
209 const struct argpar_item
* const item
)
212 ARGPAR_ASSERT(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
213 return ((const struct argpar_item_non_opt
*) item
)->orig_index
;
217 unsigned int argpar_item_non_opt_non_opt_index(
218 const struct argpar_item
* const item
)
221 ARGPAR_ASSERT(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
222 return ((const struct argpar_item_non_opt
*) item
)->non_opt_index
;
226 void argpar_item_destroy(const struct argpar_item
* const item
)
232 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
233 struct argpar_item_opt
* const opt_item
=
234 (struct argpar_item_opt
*) item
;
246 bool push_item(struct argpar_item_array
* const array
,
247 const struct argpar_item
* const item
)
251 ARGPAR_ASSERT(array
);
254 if (array
->n_items
== array
->n_alloc
) {
255 const unsigned int new_n_alloc
= array
->n_alloc
* 2;
256 const struct argpar_item
** const new_items
=
257 ARGPAR_REALLOC(array
->items
, const struct argpar_item
*,
264 array
->n_alloc
= new_n_alloc
;
265 array
->items
= new_items
;
268 array
->items
[array
->n_items
] = item
;
277 void destroy_item_array(struct argpar_item_array
* const array
)
282 for (i
= 0; i
< array
->n_items
; i
++) {
283 argpar_item_destroy(array
->items
[i
]);
292 struct argpar_item_array
*create_item_array(void)
294 struct argpar_item_array
*ret
;
295 const int initial_size
= 10;
297 ret
= ARGPAR_ZALLOC(struct argpar_item_array
);
302 ret
->items
= ARGPAR_CALLOC(const struct argpar_item
*, initial_size
);
307 ret
->n_alloc
= initial_size
;
311 destroy_item_array(ret
);
319 struct argpar_item_opt
*create_opt_item(
320 const struct argpar_opt_descr
* const descr
,
321 const char * const arg
)
323 struct argpar_item_opt
*opt_item
=
324 ARGPAR_ZALLOC(struct argpar_item_opt
);
330 opt_item
->base
.type
= ARGPAR_ITEM_TYPE_OPT
;
331 opt_item
->descr
= descr
;
334 opt_item
->arg
= strdup(arg
);
335 if (!opt_item
->arg
) {
343 argpar_item_destroy(&opt_item
->base
);
351 struct argpar_item_non_opt
*create_non_opt_item(const char * const arg
,
352 const unsigned int orig_index
,
353 const unsigned int non_opt_index
)
355 struct argpar_item_non_opt
* const non_opt_item
=
356 ARGPAR_ZALLOC(struct argpar_item_non_opt
);
362 non_opt_item
->base
.type
= ARGPAR_ITEM_TYPE_NON_OPT
;
363 non_opt_item
->arg
= arg
;
364 non_opt_item
->orig_index
= orig_index
;
365 non_opt_item
->non_opt_index
= non_opt_index
;
372 const struct argpar_opt_descr
*find_descr(
373 const struct argpar_opt_descr
* const descrs
,
374 const char short_name
, const char * const long_name
)
376 const struct argpar_opt_descr
*descr
;
378 for (descr
= descrs
; descr
->short_name
|| descr
->long_name
; descr
++) {
379 if (short_name
&& descr
->short_name
&&
380 short_name
== descr
->short_name
) {
384 if (long_name
&& descr
->long_name
&&
385 strcmp(long_name
, descr
->long_name
) == 0) {
391 return !descr
->short_name
&& !descr
->long_name
? NULL
: descr
;
394 enum parse_orig_arg_opt_ret
{
395 PARSE_ORIG_ARG_OPT_RET_OK
,
396 PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT
= -2,
397 PARSE_ORIG_ARG_OPT_RET_ERROR
= -1,
401 enum parse_orig_arg_opt_ret
parse_short_opts(const char * const short_opts
,
402 const char * const next_orig_arg
,
403 const struct argpar_opt_descr
* const descrs
,
404 struct argpar_iter
* const iter
,
405 char ** const error
, struct argpar_item
** const item
)
407 enum parse_orig_arg_opt_ret ret
= PARSE_ORIG_ARG_OPT_RET_OK
;
408 bool used_next_orig_arg
= false;
409 const char *opt_arg
= NULL
;
410 const struct argpar_opt_descr
*descr
;
411 struct argpar_item_opt
*opt_item
;
413 if (strlen(short_opts
) == 0) {
414 append_string_printf(error
, "Invalid argument");
418 if (!iter
->short_opt_ch
) {
419 iter
->short_opt_ch
= short_opts
;
422 /* Find corresponding option descriptor */
423 descr
= find_descr(descrs
, *iter
->short_opt_ch
, NULL
);
425 ret
= PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT
;
426 append_string_printf(error
, "Unknown option `-%c`",
427 *iter
->short_opt_ch
);
431 if (descr
->with_arg
) {
432 if (iter
->short_opt_ch
[1]) {
434 opt_arg
= &iter
->short_opt_ch
[1];
437 opt_arg
= next_orig_arg
;
438 used_next_orig_arg
= true;
442 * We accept `-o ''` (empty option argument), but not
443 * `-o` alone if an option argument is expected.
445 if (!opt_arg
|| (iter
->short_opt_ch
[1] &&
446 strlen(opt_arg
) == 0)) {
447 append_string_printf(error
,
448 "Missing required argument for option `-%c`",
449 *iter
->short_opt_ch
);
450 used_next_orig_arg
= false;
455 /* Create and append option argument */
456 opt_item
= create_opt_item(descr
, opt_arg
);
461 *item
= &opt_item
->base
;
462 iter
->short_opt_ch
++;
464 if (descr
->with_arg
|| !*iter
->short_opt_ch
) {
465 /* Option has an argument: no more options */
466 iter
->short_opt_ch
= NULL
;
468 if (used_next_orig_arg
) {
478 if (ret
== PARSE_ORIG_ARG_OPT_RET_OK
) {
479 ret
= PARSE_ORIG_ARG_OPT_RET_ERROR
;
487 enum parse_orig_arg_opt_ret
parse_long_opt(const char * const long_opt_arg
,
488 const char * const next_orig_arg
,
489 const struct argpar_opt_descr
* const descrs
,
490 struct argpar_iter
* const iter
,
491 char ** const error
, struct argpar_item
** const item
)
493 const size_t max_len
= 127;
494 enum parse_orig_arg_opt_ret ret
= PARSE_ORIG_ARG_OPT_RET_OK
;
495 const struct argpar_opt_descr
*descr
;
496 struct argpar_item_opt
*opt_item
;
497 bool used_next_orig_arg
= false;
499 /* Option's argument, if any */
500 const char *opt_arg
= NULL
;
502 /* Position of first `=`, if any */
505 /* Buffer holding option name when `long_opt_arg` contains `=` */
506 char buf
[max_len
+ 1];
509 const char *long_opt_name
= long_opt_arg
;
511 if (strlen(long_opt_arg
) == 0) {
512 append_string_printf(error
, "Invalid argument");
516 /* Find the first `=` in original argument */
517 eq_pos
= strchr(long_opt_arg
, '=');
519 const size_t long_opt_name_size
= eq_pos
- long_opt_arg
;
521 /* Isolate the option name */
522 if (long_opt_name_size
> max_len
) {
523 append_string_printf(error
, "Invalid argument `--%s`",
528 memcpy(buf
, long_opt_arg
, long_opt_name_size
);
529 buf
[long_opt_name_size
] = '\0';
533 /* Find corresponding option descriptor */
534 descr
= find_descr(descrs
, '\0', long_opt_name
);
536 append_string_printf(error
, "Unknown option `--%s`",
538 ret
= PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT
;
542 /* Find option's argument if any */
543 if (descr
->with_arg
) {
545 /* `--long-opt=arg` style */
546 opt_arg
= eq_pos
+ 1;
548 /* `--long-opt arg` style */
549 if (!next_orig_arg
) {
550 append_string_printf(error
,
551 "Missing required argument for option `--%s`",
556 opt_arg
= next_orig_arg
;
557 used_next_orig_arg
= true;
561 * Unexpected `--opt=arg` style for a long option which
562 * doesn't accept an argument.
564 append_string_printf(error
,
565 "Unexpected argument for option `--%s`", long_opt_name
);
569 /* Create and append option argument */
570 opt_item
= create_opt_item(descr
, opt_arg
);
575 if (used_next_orig_arg
) {
581 *item
= &opt_item
->base
;
585 if (ret
== PARSE_ORIG_ARG_OPT_RET_OK
) {
586 ret
= PARSE_ORIG_ARG_OPT_RET_ERROR
;
594 enum parse_orig_arg_opt_ret
parse_orig_arg_opt(const char * const orig_arg
,
595 const char * const next_orig_arg
,
596 const struct argpar_opt_descr
* const descrs
,
597 struct argpar_iter
* const iter
, char ** const error
,
598 struct argpar_item
** const item
)
600 enum parse_orig_arg_opt_ret ret
= PARSE_ORIG_ARG_OPT_RET_OK
;
602 ARGPAR_ASSERT(orig_arg
[0] == '-');
604 if (orig_arg
[1] == '-') {
606 ret
= parse_long_opt(&orig_arg
[2],
607 next_orig_arg
, descrs
, iter
, error
, item
);
610 ret
= parse_short_opts(&orig_arg
[1],
611 next_orig_arg
, descrs
, iter
, error
, item
);
618 bool prepend_while_parsing_arg_to_error(char ** const error
,
619 const unsigned int i
, const char * const arg
)
624 ARGPAR_ASSERT(error
);
625 ARGPAR_ASSERT(*error
);
626 new_error
= argpar_asprintf("While parsing argument #%u (`%s`): %s",
642 struct argpar_iter
*argpar_iter_create(const unsigned int argc
,
643 const char * const * const argv
,
644 const struct argpar_opt_descr
* const descrs
)
646 struct argpar_iter
* const iter
= ARGPAR_ZALLOC(struct argpar_iter
);
654 iter
->descrs
= descrs
;
661 void argpar_iter_destroy(struct argpar_iter
* const iter
)
667 enum argpar_iter_parse_next_status
argpar_iter_parse_next(
668 struct argpar_iter
* const iter
,
669 const struct argpar_item
** const item
, char ** const error
)
671 enum argpar_iter_parse_next_status status
;
672 enum parse_orig_arg_opt_ret parse_orig_arg_opt_ret
;
673 const char *orig_arg
;
674 const char *next_orig_arg
;
676 ARGPAR_ASSERT(iter
->i
<= iter
->argc
);
679 if (iter
->i
== iter
->argc
) {
680 status
= ARGPAR_ITER_PARSE_NEXT_STATUS_END
;
684 orig_arg
= iter
->argv
[iter
->i
];
686 iter
->i
< (iter
->argc
- 1) ? iter
->argv
[iter
->i
+ 1] : NULL
;
688 if (orig_arg
[0] != '-') {
689 /* Non-option argument */
690 struct argpar_item_non_opt
* const non_opt_item
=
691 create_non_opt_item(orig_arg
, iter
->i
,
692 iter
->non_opt_index
);
695 status
= ARGPAR_ITER_PARSE_NEXT_STATUS_ERROR
;
699 iter
->non_opt_index
++;
701 *item
= &non_opt_item
->base
;
702 status
= ARGPAR_ITER_PARSE_NEXT_STATUS_OK
;
706 /* Option argument */
707 parse_orig_arg_opt_ret
= parse_orig_arg_opt(orig_arg
,
708 next_orig_arg
, iter
->descrs
, iter
, error
,
709 (struct argpar_item
**) item
);
710 switch (parse_orig_arg_opt_ret
) {
711 case PARSE_ORIG_ARG_OPT_RET_OK
:
712 status
= ARGPAR_ITER_PARSE_NEXT_STATUS_OK
;
714 case PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT
:
715 prepend_while_parsing_arg_to_error(error
, iter
->i
, orig_arg
);
716 status
= ARGPAR_ITER_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
;
718 case PARSE_ORIG_ARG_OPT_RET_ERROR
:
719 prepend_while_parsing_arg_to_error(error
, iter
->i
, orig_arg
);
720 status
= ARGPAR_ITER_PARSE_NEXT_STATUS_ERROR
;
731 unsigned int argpar_iter_get_ingested_orig_args(
732 const struct argpar_iter
* const iter
)
738 struct argpar_parse_ret
argpar_parse(const unsigned int argc
,
739 const char * const * const argv
,
740 const struct argpar_opt_descr
* const descrs
,
741 const bool fail_on_unknown_opt
)
743 struct argpar_parse_ret parse_ret
= { 0 };
744 const struct argpar_item
*item
= NULL
;
745 struct argpar_iter
*iter
= NULL
;
747 parse_ret
.items
= create_item_array();
748 if (!parse_ret
.items
) {
749 parse_ret
.error
= strdup("Failed to create items array.");
750 ARGPAR_ASSERT(parse_ret
.error
);
754 iter
= argpar_iter_create(argc
, argv
, descrs
);
756 parse_ret
.error
= strdup("Failed to create argpar iter.");
757 ARGPAR_ASSERT(parse_ret
.error
);
762 const enum argpar_iter_parse_next_status status
=
763 argpar_iter_parse_next(iter
, &item
, &parse_ret
.error
);
765 if (status
== ARGPAR_ITER_PARSE_NEXT_STATUS_ERROR
) {
767 } else if (status
== ARGPAR_ITER_PARSE_NEXT_STATUS_END
) {
769 } else if (status
== ARGPAR_ITER_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
770 if (fail_on_unknown_opt
) {
771 parse_ret
.ingested_orig_args
=
772 argpar_iter_get_ingested_orig_args(iter
);
776 free(parse_ret
.error
);
777 parse_ret
.error
= NULL
;
781 ARGPAR_ASSERT(status
== ARGPAR_ITER_PARSE_NEXT_STATUS_OK
);
783 if (!push_item(parse_ret
.items
, item
)) {
790 ARGPAR_ASSERT(!parse_ret
.error
);
791 parse_ret
.ingested_orig_args
= argpar_iter_get_ingested_orig_args(iter
);
795 ARGPAR_ASSERT(parse_ret
.error
);
797 /* That's how we indicate that an error occurred */
798 destroy_item_array(parse_ret
.items
);
799 parse_ret
.items
= NULL
;
802 argpar_iter_destroy(iter
);
803 argpar_item_destroy(item
);
808 void argpar_parse_ret_fini(struct argpar_parse_ret
* const ret
)
811 destroy_item_array(ret
->items
);