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
46 const char * const *argv
;
47 const struct argpar_opt_descr
*descrs
;
51 * Index of the argument to process in the next
52 * argpar_iter_next() call.
56 /* Counter of non-option arguments */
60 * Current character within the current short option group: if
61 * it's not `NULL`, the parser is within a short option group,
62 * therefore it must resume there in the next argpar_iter_next()
65 const char *short_opt_group_ch
;
67 /* Temporary character buffer which only grows */
74 /* Base parsing item */
76 enum argpar_item_type type
;
79 /* Option parsing item */
80 struct argpar_item_opt
{
81 struct argpar_item base
;
83 /* Corresponding descriptor */
84 const struct argpar_opt_descr
*descr
;
86 /* Argument, or `NULL` if none; owned by this */
90 /* Non-option parsing item */
91 struct argpar_item_non_opt
{
92 struct argpar_item base
;
95 * Complete argument, pointing to one of the entries of the
96 * original arguments (`argv`).
101 * Index of this argument amongst all original arguments
104 unsigned int orig_index
;
106 /* Index of this argument amongst other non-option arguments */
107 unsigned int non_opt_index
;
111 struct argpar_error
{
112 /* Original argument index */
113 unsigned int orig_index
;
115 /* Name of unknown option; owned by this */
116 char *unknown_opt_name
;
118 /* Option descriptor */
119 const struct argpar_opt_descr
*opt_descr
;
121 /* `true` if a short option caused the error */
126 enum argpar_item_type
argpar_item_type(const struct argpar_item
* const item
)
133 const struct argpar_opt_descr
*argpar_item_opt_descr(
134 const struct argpar_item
* const item
)
137 ARGPAR_ASSERT(item
->type
== ARGPAR_ITEM_TYPE_OPT
);
138 return ((const struct argpar_item_opt
*) item
)->descr
;
142 const char *argpar_item_opt_arg(const struct argpar_item
* const item
)
145 ARGPAR_ASSERT(item
->type
== ARGPAR_ITEM_TYPE_OPT
);
146 return ((const struct argpar_item_opt
*) item
)->arg
;
150 const char *argpar_item_non_opt_arg(const struct argpar_item
* const item
)
153 ARGPAR_ASSERT(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
154 return ((const struct argpar_item_non_opt
*) item
)->arg
;
158 unsigned int argpar_item_non_opt_orig_index(
159 const struct argpar_item
* const item
)
162 ARGPAR_ASSERT(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
163 return ((const struct argpar_item_non_opt
*) item
)->orig_index
;
167 unsigned int argpar_item_non_opt_non_opt_index(
168 const struct argpar_item
* const item
)
171 ARGPAR_ASSERT(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
172 return ((const struct argpar_item_non_opt
*) item
)->non_opt_index
;
176 void argpar_item_destroy(const struct argpar_item
* const item
)
182 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
183 struct argpar_item_opt
* const opt_item
=
184 (struct argpar_item_opt
*) item
;
196 * Creates and returns an option parsing item for the descriptor `descr`
197 * and having the argument `arg` (copied; may be `NULL`).
199 * Returns `NULL` on memory error.
202 struct argpar_item_opt
*create_opt_item(
203 const struct argpar_opt_descr
* const descr
,
204 const char * const arg
)
206 struct argpar_item_opt
*opt_item
=
207 ARGPAR_ZALLOC(struct argpar_item_opt
);
213 opt_item
->base
.type
= ARGPAR_ITEM_TYPE_OPT
;
214 opt_item
->descr
= descr
;
217 opt_item
->arg
= strdup(arg
);
218 if (!opt_item
->arg
) {
226 argpar_item_destroy(&opt_item
->base
);
234 * Creates and returns a non-option parsing item for the original
235 * argument `arg` having the original index `orig_index` and the
236 * non-option index `non_opt_index`.
238 * Returns `NULL` on memory error.
241 struct argpar_item_non_opt
*create_non_opt_item(const char * const arg
,
242 const unsigned int orig_index
,
243 const unsigned int non_opt_index
)
245 struct argpar_item_non_opt
* const non_opt_item
=
246 ARGPAR_ZALLOC(struct argpar_item_non_opt
);
252 non_opt_item
->base
.type
= ARGPAR_ITEM_TYPE_NON_OPT
;
253 non_opt_item
->arg
= arg
;
254 non_opt_item
->orig_index
= orig_index
;
255 non_opt_item
->non_opt_index
= non_opt_index
;
262 * If `error` is not `NULL`, sets the error `error` to a new parsing
263 * error object, setting its `unknown_opt_name`, `opt_descr`, and
264 * `is_short` members from the parameters.
266 * `unknown_opt_name` is the unknown option name without any `-` or `--`
267 * prefix: `is_short` controls which type of unknown option it is.
269 * Returns 0 on success (including if `error` is `NULL`) or -1 on memory
273 int set_error(struct argpar_error
** const error
,
274 const char * const unknown_opt_name
,
275 const struct argpar_opt_descr
* const opt_descr
,
284 *error
= ARGPAR_ZALLOC(struct argpar_error
);
289 if (unknown_opt_name
) {
290 (*error
)->unknown_opt_name
= ARGPAR_CALLOC(char,
291 strlen(unknown_opt_name
) + 1 + (is_short
? 1 : 2));
292 if (!(*error
)->unknown_opt_name
) {
297 strcpy((*error
)->unknown_opt_name
, "-");
299 strcpy((*error
)->unknown_opt_name
, "--");
302 strcat((*error
)->unknown_opt_name
, unknown_opt_name
);
305 (*error
)->opt_descr
= opt_descr
;
306 (*error
)->is_short
= is_short
;
310 argpar_error_destroy(*error
);
318 unsigned int argpar_error_orig_index(const struct argpar_error
* const error
)
320 ARGPAR_ASSERT(error
);
321 return error
->orig_index
;
325 const char *argpar_error_unknown_opt_name(
326 const struct argpar_error
* const error
)
328 ARGPAR_ASSERT(error
);
329 ARGPAR_ASSERT(error
->unknown_opt_name
);
330 return error
->unknown_opt_name
;
334 const struct argpar_opt_descr
*argpar_error_opt_descr(
335 const struct argpar_error
* const error
, bool * const is_short
)
337 ARGPAR_ASSERT(error
);
338 ARGPAR_ASSERT(error
->opt_descr
);
341 *is_short
= error
->is_short
;
344 return error
->opt_descr
;
348 void argpar_error_destroy(const struct argpar_error
* const error
)
351 free(error
->unknown_opt_name
);
352 free((void *) error
);
357 * Finds and returns the _first_ descriptor having the short option name
358 * `short_name` or the long option name `long_name` within the option
359 * descriptors `descrs`.
361 * `short_name` may be `'\0'` to not consider it.
363 * `long_name` may be `NULL` to not consider it.
365 * Returns `NULL` if no descriptor is found.
368 const struct argpar_opt_descr
*find_descr(
369 const struct argpar_opt_descr
* const descrs
,
370 const char short_name
, const char * const long_name
)
372 const struct argpar_opt_descr
*descr
;
374 for (descr
= descrs
; descr
->short_name
|| descr
->long_name
; descr
++) {
375 if (short_name
&& descr
->short_name
&&
376 short_name
== descr
->short_name
) {
380 if (long_name
&& descr
->long_name
&&
381 strcmp(long_name
, descr
->long_name
) == 0) {
387 return !descr
->short_name
&& !descr
->long_name
? NULL
: descr
;
390 /* Return type of parse_short_opt_group() and parse_long_opt() */
391 enum parse_orig_arg_opt_ret
{
392 PARSE_ORIG_ARG_OPT_RET_OK
,
393 PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT
= -1,
394 PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG
= -2,
395 PARSE_ORIG_ARG_OPT_RET_ERROR_UNEXPECTED_OPT_ARG
= -4,
396 PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY
= -5,
400 * Parses the short option group argument `short_opt_group`, starting
401 * where needed depending on the state of `iter`.
403 * On success, sets `*item`.
405 * On error (except for `PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY`), sets
409 enum parse_orig_arg_opt_ret
parse_short_opt_group(
410 const char * const short_opt_group
,
411 const char * const next_orig_arg
,
412 const struct argpar_opt_descr
* const descrs
,
413 struct argpar_iter
* const iter
,
414 struct argpar_error
** const error
,
415 struct argpar_item
** const item
)
417 enum parse_orig_arg_opt_ret ret
= PARSE_ORIG_ARG_OPT_RET_OK
;
418 bool used_next_orig_arg
= false;
419 const char *opt_arg
= NULL
;
420 const struct argpar_opt_descr
*descr
;
421 struct argpar_item_opt
*opt_item
;
423 ARGPAR_ASSERT(strlen(short_opt_group
) != 0);
425 if (!iter
->short_opt_group_ch
) {
426 iter
->short_opt_group_ch
= short_opt_group
;
429 /* Find corresponding option descriptor */
430 descr
= find_descr(descrs
, *iter
->short_opt_group_ch
, NULL
);
432 const char unknown_opt_name
[] =
433 {*iter
->short_opt_group_ch
, '\0'};
435 ret
= PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT
;
437 if (set_error(error
, unknown_opt_name
, NULL
, true)) {
438 ret
= PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY
;
444 if (descr
->with_arg
) {
445 if (iter
->short_opt_group_ch
[1]) {
447 opt_arg
= &iter
->short_opt_group_ch
[1];
450 opt_arg
= next_orig_arg
;
451 used_next_orig_arg
= true;
455 * We accept `-o ''` (empty option argument), but not
456 * `-o` alone if an option argument is expected.
458 if (!opt_arg
|| (iter
->short_opt_group_ch
[1] &&
459 strlen(opt_arg
) == 0)) {
460 ret
= PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG
;
462 if (set_error(error
, NULL
, descr
, true)) {
463 ret
= PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY
;
470 /* Create and append option argument */
471 opt_item
= create_opt_item(descr
, opt_arg
);
473 ret
= PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY
;
477 *item
= &opt_item
->base
;
478 iter
->short_opt_group_ch
++;
480 if (descr
->with_arg
|| !*iter
->short_opt_group_ch
) {
481 /* Option has an argument: no more options */
482 iter
->short_opt_group_ch
= NULL
;
484 if (used_next_orig_arg
) {
494 ARGPAR_ASSERT(ret
!= PARSE_ORIG_ARG_OPT_RET_OK
);
501 * Parses the long option argument `long_opt_arg`.
503 * On success, sets `*item`.
505 * On error (except for `PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY`), sets
509 enum parse_orig_arg_opt_ret
parse_long_opt(const char * const long_opt_arg
,
510 const char * const next_orig_arg
,
511 const struct argpar_opt_descr
* const descrs
,
512 struct argpar_iter
* const iter
,
513 struct argpar_error
** const error
,
514 struct argpar_item
** const item
)
516 enum parse_orig_arg_opt_ret ret
= PARSE_ORIG_ARG_OPT_RET_OK
;
517 const struct argpar_opt_descr
*descr
;
518 struct argpar_item_opt
*opt_item
;
519 bool used_next_orig_arg
= false;
521 /* Option's argument, if any */
522 const char *opt_arg
= NULL
;
524 /* Position of first `=`, if any */
528 const char *long_opt_name
= long_opt_arg
;
530 ARGPAR_ASSERT(strlen(long_opt_arg
) != 0);
532 /* Find the first `=` in original argument */
533 eq_pos
= strchr(long_opt_arg
, '=');
535 const size_t long_opt_name_size
= eq_pos
- long_opt_arg
;
537 /* Isolate the option name */
538 while (long_opt_name_size
> iter
->tmp_buf
.size
- 1) {
539 iter
->tmp_buf
.size
*= 2;
540 iter
->tmp_buf
.data
= ARGPAR_REALLOC(iter
->tmp_buf
.data
,
541 char, iter
->tmp_buf
.size
);
542 if (!iter
->tmp_buf
.data
) {
543 ret
= PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY
;
548 memcpy(iter
->tmp_buf
.data
, long_opt_arg
, long_opt_name_size
);
549 iter
->tmp_buf
.data
[long_opt_name_size
] = '\0';
550 long_opt_name
= iter
->tmp_buf
.data
;
553 /* Find corresponding option descriptor */
554 descr
= find_descr(descrs
, '\0', long_opt_name
);
556 ret
= PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT
;
558 if (set_error(error
, long_opt_name
, NULL
, false)) {
559 ret
= PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY
;
565 /* Find option's argument if any */
566 if (descr
->with_arg
) {
568 /* `--long-opt=arg` style */
569 opt_arg
= eq_pos
+ 1;
571 /* `--long-opt arg` style */
572 if (!next_orig_arg
) {
573 ret
= PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG
;
575 if (set_error(error
, NULL
, descr
, false)) {
576 ret
= PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY
;
582 opt_arg
= next_orig_arg
;
583 used_next_orig_arg
= true;
587 * Unexpected `--opt=arg` style for a long option which
588 * doesn't accept an argument.
590 ret
= PARSE_ORIG_ARG_OPT_RET_ERROR_UNEXPECTED_OPT_ARG
;
592 if (set_error(error
, NULL
, descr
, false)) {
593 ret
= PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY
;
599 /* Create and append option argument */
600 opt_item
= create_opt_item(descr
, opt_arg
);
605 if (used_next_orig_arg
) {
611 *item
= &opt_item
->base
;
615 ARGPAR_ASSERT(ret
!= PARSE_ORIG_ARG_OPT_RET_OK
);
622 * Parses the original argument `orig_arg`.
624 * On success, sets `*item`.
626 * On error (except for `PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY`), sets
630 enum parse_orig_arg_opt_ret
parse_orig_arg_opt(const char * const orig_arg
,
631 const char * const next_orig_arg
,
632 const struct argpar_opt_descr
* const descrs
,
633 struct argpar_iter
* const iter
,
634 struct argpar_error
** const error
,
635 struct argpar_item
** const item
)
637 enum parse_orig_arg_opt_ret ret
= PARSE_ORIG_ARG_OPT_RET_OK
;
639 ARGPAR_ASSERT(orig_arg
[0] == '-');
641 if (orig_arg
[1] == '-') {
643 ret
= parse_long_opt(&orig_arg
[2],
644 next_orig_arg
, descrs
, iter
, error
, item
);
647 ret
= parse_short_opt_group(&orig_arg
[1],
648 next_orig_arg
, descrs
, iter
, error
, item
);
655 struct argpar_iter
*argpar_iter_create(const unsigned int argc
,
656 const char * const * const argv
,
657 const struct argpar_opt_descr
* const descrs
)
659 struct argpar_iter
*iter
= ARGPAR_ZALLOC(struct argpar_iter
);
665 iter
->user
.argc
= argc
;
666 iter
->user
.argv
= argv
;
667 iter
->user
.descrs
= descrs
;
668 iter
->tmp_buf
.size
= 128;
669 iter
->tmp_buf
.data
= ARGPAR_CALLOC(char, iter
->tmp_buf
.size
);
670 if (!iter
->tmp_buf
.data
) {
671 argpar_iter_destroy(iter
);
681 void argpar_iter_destroy(struct argpar_iter
* const iter
)
684 free(iter
->tmp_buf
.data
);
690 enum argpar_iter_next_status
argpar_iter_next(
691 struct argpar_iter
* const iter
,
692 const struct argpar_item
** const item
,
693 const struct argpar_error
** const error
)
695 enum argpar_iter_next_status status
;
696 enum parse_orig_arg_opt_ret parse_orig_arg_opt_ret
;
697 const char *orig_arg
;
698 const char *next_orig_arg
;
699 struct argpar_error
** const nc_error
= (struct argpar_error
**) error
;
701 ARGPAR_ASSERT(iter
->i
<= iter
->user
.argc
);
707 if (iter
->i
== iter
->user
.argc
) {
708 status
= ARGPAR_ITER_NEXT_STATUS_END
;
712 orig_arg
= iter
->user
.argv
[iter
->i
];
714 iter
->i
< (iter
->user
.argc
- 1) ?
715 iter
->user
.argv
[iter
->i
+ 1] : NULL
;
717 if (strcmp(orig_arg
, "-") == 0 || strcmp(orig_arg
, "--") == 0 ||
718 orig_arg
[0] != '-') {
719 /* Non-option argument */
720 const struct argpar_item_non_opt
* const non_opt_item
=
721 create_non_opt_item(orig_arg
, iter
->i
,
722 iter
->non_opt_index
);
725 status
= ARGPAR_ITER_NEXT_STATUS_ERROR_MEMORY
;
729 iter
->non_opt_index
++;
731 *item
= &non_opt_item
->base
;
732 status
= ARGPAR_ITER_NEXT_STATUS_OK
;
736 /* Option argument */
737 parse_orig_arg_opt_ret
= parse_orig_arg_opt(orig_arg
,
738 next_orig_arg
, iter
->user
.descrs
, iter
, nc_error
,
739 (struct argpar_item
**) item
);
740 switch (parse_orig_arg_opt_ret
) {
741 case PARSE_ORIG_ARG_OPT_RET_OK
:
742 status
= ARGPAR_ITER_NEXT_STATUS_OK
;
744 case PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT
:
745 case PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG
:
746 case PARSE_ORIG_ARG_OPT_RET_ERROR_UNEXPECTED_OPT_ARG
:
748 ARGPAR_ASSERT(*error
);
749 (*nc_error
)->orig_index
= iter
->i
;
752 switch (parse_orig_arg_opt_ret
) {
753 case PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT
:
754 status
= ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT
;
756 case PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG
:
757 status
= ARGPAR_ITER_NEXT_STATUS_ERROR_MISSING_OPT_ARG
;
759 case PARSE_ORIG_ARG_OPT_RET_ERROR_UNEXPECTED_OPT_ARG
:
760 status
= ARGPAR_ITER_NEXT_STATUS_ERROR_UNEXPECTED_OPT_ARG
;
767 case PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY
:
768 status
= ARGPAR_ITER_NEXT_STATUS_ERROR_MEMORY
;
779 unsigned int argpar_iter_ingested_orig_args(
780 const struct argpar_iter
* const iter
)