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 try_append_string_printf(char ** const str
, const char *fmt
, ...)
145 char *new_str
= NULL
;
146 char *addendum
= NULL
;
157 addendum
= argpar_vasprintf(fmt
, args
);
165 new_str
= argpar_asprintf("%s%s", *str
? *str
: "", addendum
);
181 enum argpar_item_type
argpar_item_type(const struct argpar_item
* const item
)
188 const struct argpar_opt_descr
*argpar_item_opt_descr(
189 const struct argpar_item
* const item
)
192 ARGPAR_ASSERT(item
->type
== ARGPAR_ITEM_TYPE_OPT
);
193 return ((const struct argpar_item_opt
*) item
)->descr
;
197 const char *argpar_item_opt_arg(const struct argpar_item
* const item
)
200 ARGPAR_ASSERT(item
->type
== ARGPAR_ITEM_TYPE_OPT
);
201 return ((const struct argpar_item_opt
*) item
)->arg
;
205 const char *argpar_item_non_opt_arg(const struct argpar_item
* const item
)
208 ARGPAR_ASSERT(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
209 return ((const struct argpar_item_non_opt
*) item
)->arg
;
213 unsigned int argpar_item_non_opt_orig_index(
214 const struct argpar_item
* const item
)
217 ARGPAR_ASSERT(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
218 return ((const struct argpar_item_non_opt
*) item
)->orig_index
;
222 unsigned int argpar_item_non_opt_non_opt_index(
223 const struct argpar_item
* const item
)
226 ARGPAR_ASSERT(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
227 return ((const struct argpar_item_non_opt
*) item
)->non_opt_index
;
231 void argpar_item_destroy(const struct argpar_item
* const item
)
237 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
238 struct argpar_item_opt
* const opt_item
=
239 (struct argpar_item_opt
*) item
;
251 bool push_item(struct argpar_item_array
* const array
,
252 const struct argpar_item
* const item
)
256 ARGPAR_ASSERT(array
);
259 if (array
->n_items
== array
->n_alloc
) {
260 const unsigned int new_n_alloc
= array
->n_alloc
* 2;
261 const struct argpar_item
** const new_items
=
262 ARGPAR_REALLOC(array
->items
, const struct argpar_item
*,
269 array
->n_alloc
= new_n_alloc
;
270 array
->items
= new_items
;
273 array
->items
[array
->n_items
] = item
;
282 void destroy_item_array(struct argpar_item_array
* const array
)
287 for (i
= 0; i
< array
->n_items
; i
++) {
288 argpar_item_destroy(array
->items
[i
]);
297 struct argpar_item_array
*create_item_array(void)
299 struct argpar_item_array
*ret
;
300 const int initial_size
= 10;
302 ret
= ARGPAR_ZALLOC(struct argpar_item_array
);
307 ret
->items
= ARGPAR_CALLOC(const struct argpar_item
*, initial_size
);
312 ret
->n_alloc
= initial_size
;
316 destroy_item_array(ret
);
324 struct argpar_item_opt
*create_opt_item(
325 const struct argpar_opt_descr
* const descr
,
326 const char * const arg
)
328 struct argpar_item_opt
*opt_item
=
329 ARGPAR_ZALLOC(struct argpar_item_opt
);
335 opt_item
->base
.type
= ARGPAR_ITEM_TYPE_OPT
;
336 opt_item
->descr
= descr
;
339 opt_item
->arg
= strdup(arg
);
340 if (!opt_item
->arg
) {
348 argpar_item_destroy(&opt_item
->base
);
356 struct argpar_item_non_opt
*create_non_opt_item(const char * const arg
,
357 const unsigned int orig_index
,
358 const unsigned int non_opt_index
)
360 struct argpar_item_non_opt
* const non_opt_item
=
361 ARGPAR_ZALLOC(struct argpar_item_non_opt
);
367 non_opt_item
->base
.type
= ARGPAR_ITEM_TYPE_NON_OPT
;
368 non_opt_item
->arg
= arg
;
369 non_opt_item
->orig_index
= orig_index
;
370 non_opt_item
->non_opt_index
= non_opt_index
;
377 const struct argpar_opt_descr
*find_descr(
378 const struct argpar_opt_descr
* const descrs
,
379 const char short_name
, const char * const long_name
)
381 const struct argpar_opt_descr
*descr
;
383 for (descr
= descrs
; descr
->short_name
|| descr
->long_name
; descr
++) {
384 if (short_name
&& descr
->short_name
&&
385 short_name
== descr
->short_name
) {
389 if (long_name
&& descr
->long_name
&&
390 strcmp(long_name
, descr
->long_name
) == 0) {
396 return !descr
->short_name
&& !descr
->long_name
? NULL
: descr
;
399 enum parse_orig_arg_opt_ret
{
400 PARSE_ORIG_ARG_OPT_RET_OK
,
401 PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT
= -2,
402 PARSE_ORIG_ARG_OPT_RET_ERROR
= -1,
406 enum parse_orig_arg_opt_ret
parse_short_opts(const char * const short_opts
,
407 const char * const next_orig_arg
,
408 const struct argpar_opt_descr
* const descrs
,
409 struct argpar_iter
* const iter
,
410 char ** const error
, struct argpar_item
** const item
)
412 enum parse_orig_arg_opt_ret ret
= PARSE_ORIG_ARG_OPT_RET_OK
;
413 bool used_next_orig_arg
= false;
414 const char *opt_arg
= NULL
;
415 const struct argpar_opt_descr
*descr
;
416 struct argpar_item_opt
*opt_item
;
418 if (strlen(short_opts
) == 0) {
419 try_append_string_printf(error
, "Invalid argument");
423 if (!iter
->short_opt_ch
) {
424 iter
->short_opt_ch
= short_opts
;
427 /* Find corresponding option descriptor */
428 descr
= find_descr(descrs
, *iter
->short_opt_ch
, NULL
);
430 ret
= PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT
;
431 try_append_string_printf(error
, "Unknown option `-%c`",
432 *iter
->short_opt_ch
);
436 if (descr
->with_arg
) {
437 if (iter
->short_opt_ch
[1]) {
439 opt_arg
= &iter
->short_opt_ch
[1];
442 opt_arg
= next_orig_arg
;
443 used_next_orig_arg
= true;
447 * We accept `-o ''` (empty option argument), but not
448 * `-o` alone if an option argument is expected.
450 if (!opt_arg
|| (iter
->short_opt_ch
[1] &&
451 strlen(opt_arg
) == 0)) {
452 try_append_string_printf(error
,
453 "Missing required argument for option `-%c`",
454 *iter
->short_opt_ch
);
455 used_next_orig_arg
= false;
460 /* Create and append option argument */
461 opt_item
= create_opt_item(descr
, opt_arg
);
466 *item
= &opt_item
->base
;
467 iter
->short_opt_ch
++;
469 if (descr
->with_arg
|| !*iter
->short_opt_ch
) {
470 /* Option has an argument: no more options */
471 iter
->short_opt_ch
= NULL
;
473 if (used_next_orig_arg
) {
483 if (ret
== PARSE_ORIG_ARG_OPT_RET_OK
) {
484 ret
= PARSE_ORIG_ARG_OPT_RET_ERROR
;
492 enum parse_orig_arg_opt_ret
parse_long_opt(const char * const long_opt_arg
,
493 const char * const next_orig_arg
,
494 const struct argpar_opt_descr
* const descrs
,
495 struct argpar_iter
* const iter
,
496 char ** const error
, struct argpar_item
** const item
)
498 const size_t max_len
= 127;
499 enum parse_orig_arg_opt_ret ret
= PARSE_ORIG_ARG_OPT_RET_OK
;
500 const struct argpar_opt_descr
*descr
;
501 struct argpar_item_opt
*opt_item
;
502 bool used_next_orig_arg
= false;
504 /* Option's argument, if any */
505 const char *opt_arg
= NULL
;
507 /* Position of first `=`, if any */
510 /* Buffer holding option name when `long_opt_arg` contains `=` */
511 char buf
[max_len
+ 1];
514 const char *long_opt_name
= long_opt_arg
;
516 if (strlen(long_opt_arg
) == 0) {
517 try_append_string_printf(error
, "Invalid argument");
521 /* Find the first `=` in original argument */
522 eq_pos
= strchr(long_opt_arg
, '=');
524 const size_t long_opt_name_size
= eq_pos
- long_opt_arg
;
526 /* Isolate the option name */
527 if (long_opt_name_size
> max_len
) {
528 try_append_string_printf(error
,
529 "Invalid argument `--%s`", long_opt_arg
);
533 memcpy(buf
, long_opt_arg
, long_opt_name_size
);
534 buf
[long_opt_name_size
] = '\0';
538 /* Find corresponding option descriptor */
539 descr
= find_descr(descrs
, '\0', long_opt_name
);
541 try_append_string_printf(error
, "Unknown option `--%s`",
543 ret
= PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT
;
547 /* Find option's argument if any */
548 if (descr
->with_arg
) {
550 /* `--long-opt=arg` style */
551 opt_arg
= eq_pos
+ 1;
553 /* `--long-opt arg` style */
554 if (!next_orig_arg
) {
555 try_append_string_printf(error
,
556 "Missing required argument for option `--%s`",
561 opt_arg
= next_orig_arg
;
562 used_next_orig_arg
= true;
566 * Unexpected `--opt=arg` style for a long option which
567 * doesn't accept an argument.
569 try_append_string_printf(error
,
570 "Unexpected argument for option `--%s`", long_opt_name
);
574 /* Create and append option argument */
575 opt_item
= create_opt_item(descr
, opt_arg
);
580 if (used_next_orig_arg
) {
586 *item
= &opt_item
->base
;
590 if (ret
== PARSE_ORIG_ARG_OPT_RET_OK
) {
591 ret
= PARSE_ORIG_ARG_OPT_RET_ERROR
;
599 enum parse_orig_arg_opt_ret
parse_orig_arg_opt(const char * const orig_arg
,
600 const char * const next_orig_arg
,
601 const struct argpar_opt_descr
* const descrs
,
602 struct argpar_iter
* const iter
, char ** const error
,
603 struct argpar_item
** const item
)
605 enum parse_orig_arg_opt_ret ret
= PARSE_ORIG_ARG_OPT_RET_OK
;
607 ARGPAR_ASSERT(orig_arg
[0] == '-');
609 if (orig_arg
[1] == '-') {
611 ret
= parse_long_opt(&orig_arg
[2],
612 next_orig_arg
, descrs
, iter
, error
, item
);
615 ret
= parse_short_opts(&orig_arg
[1],
616 next_orig_arg
, descrs
, iter
, error
, item
);
623 bool try_prepend_while_parsing_arg_to_error(char ** const error
,
624 const unsigned int i
, const char * const arg
)
634 ARGPAR_ASSERT(*error
);
635 new_error
= argpar_asprintf("While parsing argument #%u (`%s`): %s",
651 struct argpar_iter
*argpar_iter_create(const unsigned int argc
,
652 const char * const * const argv
,
653 const struct argpar_opt_descr
* const descrs
)
655 struct argpar_iter
* const iter
= ARGPAR_ZALLOC(struct argpar_iter
);
663 iter
->descrs
= descrs
;
670 void argpar_iter_destroy(struct argpar_iter
* const iter
)
676 enum argpar_iter_parse_next_status
argpar_iter_parse_next(
677 struct argpar_iter
* const iter
,
678 const struct argpar_item
** const item
, char ** const error
)
680 enum argpar_iter_parse_next_status status
;
681 enum parse_orig_arg_opt_ret parse_orig_arg_opt_ret
;
682 const char *orig_arg
;
683 const char *next_orig_arg
;
685 ARGPAR_ASSERT(iter
->i
<= iter
->argc
);
691 if (iter
->i
== iter
->argc
) {
692 status
= ARGPAR_ITER_PARSE_NEXT_STATUS_END
;
696 orig_arg
= iter
->argv
[iter
->i
];
698 iter
->i
< (iter
->argc
- 1) ? iter
->argv
[iter
->i
+ 1] : NULL
;
700 if (orig_arg
[0] != '-') {
701 /* Non-option argument */
702 struct argpar_item_non_opt
* const non_opt_item
=
703 create_non_opt_item(orig_arg
, iter
->i
,
704 iter
->non_opt_index
);
707 status
= ARGPAR_ITER_PARSE_NEXT_STATUS_ERROR
;
711 iter
->non_opt_index
++;
713 *item
= &non_opt_item
->base
;
714 status
= ARGPAR_ITER_PARSE_NEXT_STATUS_OK
;
718 /* Option argument */
719 parse_orig_arg_opt_ret
= parse_orig_arg_opt(orig_arg
,
720 next_orig_arg
, iter
->descrs
, iter
, error
,
721 (struct argpar_item
**) item
);
722 switch (parse_orig_arg_opt_ret
) {
723 case PARSE_ORIG_ARG_OPT_RET_OK
:
724 status
= ARGPAR_ITER_PARSE_NEXT_STATUS_OK
;
726 case PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT
:
727 try_prepend_while_parsing_arg_to_error(error
, iter
->i
,
729 status
= ARGPAR_ITER_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
;
731 case PARSE_ORIG_ARG_OPT_RET_ERROR
:
732 try_prepend_while_parsing_arg_to_error(error
, iter
->i
,
734 status
= ARGPAR_ITER_PARSE_NEXT_STATUS_ERROR
;
745 unsigned int argpar_iter_get_ingested_orig_args(
746 const struct argpar_iter
* const iter
)
752 struct argpar_parse_ret
argpar_parse(const unsigned int argc
,
753 const char * const * const argv
,
754 const struct argpar_opt_descr
* const descrs
,
755 const bool fail_on_unknown_opt
)
757 struct argpar_parse_ret parse_ret
= { 0 };
758 const struct argpar_item
*item
= NULL
;
759 struct argpar_iter
*iter
= NULL
;
761 parse_ret
.items
= create_item_array();
762 if (!parse_ret
.items
) {
763 parse_ret
.error
= strdup("Failed to create items array.");
764 ARGPAR_ASSERT(parse_ret
.error
);
768 iter
= argpar_iter_create(argc
, argv
, descrs
);
770 parse_ret
.error
= strdup("Failed to create argpar iter.");
771 ARGPAR_ASSERT(parse_ret
.error
);
776 const enum argpar_iter_parse_next_status status
=
777 argpar_iter_parse_next(iter
, &item
, &parse_ret
.error
);
779 if (status
== ARGPAR_ITER_PARSE_NEXT_STATUS_ERROR
) {
781 } else if (status
== ARGPAR_ITER_PARSE_NEXT_STATUS_END
) {
783 } else if (status
== ARGPAR_ITER_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
784 if (fail_on_unknown_opt
) {
785 parse_ret
.ingested_orig_args
=
786 argpar_iter_get_ingested_orig_args(iter
);
790 free(parse_ret
.error
);
791 parse_ret
.error
= NULL
;
795 ARGPAR_ASSERT(status
== ARGPAR_ITER_PARSE_NEXT_STATUS_OK
);
797 if (!push_item(parse_ret
.items
, item
)) {
804 ARGPAR_ASSERT(!parse_ret
.error
);
805 parse_ret
.ingested_orig_args
= argpar_iter_get_ingested_orig_args(iter
);
809 ARGPAR_ASSERT(parse_ret
.error
);
811 /* That's how we indicate that an error occurred */
812 destroy_item_array(parse_ret
.items
);
813 parse_ret
.items
= NULL
;
816 argpar_iter_destroy(iter
);
817 argpar_item_destroy(item
);
822 void argpar_parse_ret_fini(struct argpar_parse_ret
* const ret
)
825 destroy_item_array(ret
->items
);