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 struct argpar_item_opt
*create_opt_item(
197 const struct argpar_opt_descr
* const descr
,
198 const char * const arg
)
200 struct argpar_item_opt
*opt_item
=
201 ARGPAR_ZALLOC(struct argpar_item_opt
);
207 opt_item
->base
.type
= ARGPAR_ITEM_TYPE_OPT
;
208 opt_item
->descr
= descr
;
211 opt_item
->arg
= strdup(arg
);
212 if (!opt_item
->arg
) {
220 argpar_item_destroy(&opt_item
->base
);
228 struct argpar_item_non_opt
*create_non_opt_item(const char * const arg
,
229 const unsigned int orig_index
,
230 const unsigned int non_opt_index
)
232 struct argpar_item_non_opt
* const non_opt_item
=
233 ARGPAR_ZALLOC(struct argpar_item_non_opt
);
239 non_opt_item
->base
.type
= ARGPAR_ITEM_TYPE_NON_OPT
;
240 non_opt_item
->arg
= arg
;
241 non_opt_item
->orig_index
= orig_index
;
242 non_opt_item
->non_opt_index
= non_opt_index
;
249 * If `error` is not `NULL`, sets the error `error` to a new parsing
250 * error object, setting its `unknown_opt_name`, `opt_descr`, and
251 * `is_short` members from the parameters.
253 * `unknown_opt_name` is the unknown option name without any `-` or `--`
254 * prefix: `is_short` controls which type of unknown option it is.
256 * Returns 0 on success (including if `error` is `NULL`) or -1 on memory
260 int set_error(struct argpar_error
** const error
,
261 const char * const unknown_opt_name
,
262 const struct argpar_opt_descr
* const opt_descr
,
271 *error
= ARGPAR_ZALLOC(struct argpar_error
);
276 if (unknown_opt_name
) {
277 (*error
)->unknown_opt_name
= ARGPAR_CALLOC(char,
278 strlen(unknown_opt_name
) + 1 + is_short
? 1 : 2);
279 if (!(*error
)->unknown_opt_name
) {
284 strcpy((*error
)->unknown_opt_name
, "-");
286 strcpy((*error
)->unknown_opt_name
, "--");
289 strcat((*error
)->unknown_opt_name
, unknown_opt_name
);
292 (*error
)->opt_descr
= opt_descr
;
293 (*error
)->is_short
= is_short
;
297 argpar_error_destroy(*error
);
305 unsigned int argpar_error_orig_index(const struct argpar_error
* const error
)
307 ARGPAR_ASSERT(error
);
308 return error
->orig_index
;
312 const char *argpar_error_unknown_opt_name(
313 const struct argpar_error
* const error
)
315 ARGPAR_ASSERT(error
);
316 ARGPAR_ASSERT(error
->unknown_opt_name
);
317 return error
->unknown_opt_name
;
321 const struct argpar_opt_descr
*argpar_error_opt_descr(
322 const struct argpar_error
* const error
, bool * const is_short
)
324 ARGPAR_ASSERT(error
);
325 ARGPAR_ASSERT(error
->opt_descr
);
328 *is_short
= error
->is_short
;
331 return error
->opt_descr
;
335 void argpar_error_destroy(const struct argpar_error
* const error
)
338 free(error
->unknown_opt_name
);
339 free((void *) error
);
344 const struct argpar_opt_descr
*find_descr(
345 const struct argpar_opt_descr
* const descrs
,
346 const char short_name
, const char * const long_name
)
348 const struct argpar_opt_descr
*descr
;
350 for (descr
= descrs
; descr
->short_name
|| descr
->long_name
; descr
++) {
351 if (short_name
&& descr
->short_name
&&
352 short_name
== descr
->short_name
) {
356 if (long_name
&& descr
->long_name
&&
357 strcmp(long_name
, descr
->long_name
) == 0) {
363 return !descr
->short_name
&& !descr
->long_name
? NULL
: descr
;
366 enum parse_orig_arg_opt_ret
{
367 PARSE_ORIG_ARG_OPT_RET_OK
,
368 PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT
= -1,
369 PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG
= -2,
370 PARSE_ORIG_ARG_OPT_RET_ERROR_UNEXPECTED_OPT_ARG
= -4,
371 PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY
= -5,
375 enum parse_orig_arg_opt_ret
parse_short_opt_group(
376 const char * const short_opt_group
,
377 const char * const next_orig_arg
,
378 const struct argpar_opt_descr
* const descrs
,
379 struct argpar_iter
* const iter
,
380 struct argpar_error
** const error
,
381 struct argpar_item
** const item
)
383 enum parse_orig_arg_opt_ret ret
= PARSE_ORIG_ARG_OPT_RET_OK
;
384 bool used_next_orig_arg
= false;
385 const char *opt_arg
= NULL
;
386 const struct argpar_opt_descr
*descr
;
387 struct argpar_item_opt
*opt_item
;
389 ARGPAR_ASSERT(strlen(short_opt_group
) != 0);
391 if (!iter
->short_opt_group_ch
) {
392 iter
->short_opt_group_ch
= short_opt_group
;
395 /* Find corresponding option descriptor */
396 descr
= find_descr(descrs
, *iter
->short_opt_group_ch
, NULL
);
398 const char unknown_opt_name
[] =
399 {*iter
->short_opt_group_ch
, '\0'};
401 ret
= PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT
;
403 if (set_error(error
, unknown_opt_name
, NULL
, true)) {
404 ret
= PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY
;
410 if (descr
->with_arg
) {
411 if (iter
->short_opt_group_ch
[1]) {
413 opt_arg
= &iter
->short_opt_group_ch
[1];
416 opt_arg
= next_orig_arg
;
417 used_next_orig_arg
= true;
421 * We accept `-o ''` (empty option argument), but not
422 * `-o` alone if an option argument is expected.
424 if (!opt_arg
|| (iter
->short_opt_group_ch
[1] &&
425 strlen(opt_arg
) == 0)) {
426 ret
= PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG
;
428 if (set_error(error
, NULL
, descr
, true)) {
429 ret
= PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY
;
436 /* Create and append option argument */
437 opt_item
= create_opt_item(descr
, opt_arg
);
439 ret
= PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY
;
443 *item
= &opt_item
->base
;
444 iter
->short_opt_group_ch
++;
446 if (descr
->with_arg
|| !*iter
->short_opt_group_ch
) {
447 /* Option has an argument: no more options */
448 iter
->short_opt_group_ch
= NULL
;
450 if (used_next_orig_arg
) {
460 ARGPAR_ASSERT(ret
!= PARSE_ORIG_ARG_OPT_RET_OK
);
467 enum parse_orig_arg_opt_ret
parse_long_opt(const char * const long_opt_arg
,
468 const char * const next_orig_arg
,
469 const struct argpar_opt_descr
* const descrs
,
470 struct argpar_iter
* const iter
,
471 struct argpar_error
** const error
,
472 struct argpar_item
** const item
)
474 enum parse_orig_arg_opt_ret ret
= PARSE_ORIG_ARG_OPT_RET_OK
;
475 const struct argpar_opt_descr
*descr
;
476 struct argpar_item_opt
*opt_item
;
477 bool used_next_orig_arg
= false;
479 /* Option's argument, if any */
480 const char *opt_arg
= NULL
;
482 /* Position of first `=`, if any */
486 const char *long_opt_name
= long_opt_arg
;
488 ARGPAR_ASSERT(strlen(long_opt_arg
) != 0);
490 /* Find the first `=` in original argument */
491 eq_pos
= strchr(long_opt_arg
, '=');
493 const size_t long_opt_name_size
= eq_pos
- long_opt_arg
;
495 /* Isolate the option name */
496 while (long_opt_name_size
> iter
->tmp_buf
.size
- 1) {
497 iter
->tmp_buf
.size
*= 2;
498 iter
->tmp_buf
.data
= ARGPAR_REALLOC(iter
->tmp_buf
.data
,
499 char, iter
->tmp_buf
.size
);
500 if (!iter
->tmp_buf
.data
) {
501 ret
= PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY
;
506 memcpy(iter
->tmp_buf
.data
, long_opt_arg
, long_opt_name_size
);
507 iter
->tmp_buf
.data
[long_opt_name_size
] = '\0';
508 long_opt_name
= iter
->tmp_buf
.data
;
511 /* Find corresponding option descriptor */
512 descr
= find_descr(descrs
, '\0', long_opt_name
);
514 ret
= PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT
;
516 if (set_error(error
, long_opt_name
, NULL
, false)) {
517 ret
= PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY
;
523 /* Find option's argument if any */
524 if (descr
->with_arg
) {
526 /* `--long-opt=arg` style */
527 opt_arg
= eq_pos
+ 1;
529 /* `--long-opt arg` style */
530 if (!next_orig_arg
) {
531 ret
= PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG
;
533 if (set_error(error
, NULL
, descr
, false)) {
534 ret
= PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY
;
540 opt_arg
= next_orig_arg
;
541 used_next_orig_arg
= true;
545 * Unexpected `--opt=arg` style for a long option which
546 * doesn't accept an argument.
548 ret
= PARSE_ORIG_ARG_OPT_RET_ERROR_UNEXPECTED_OPT_ARG
;
550 if (set_error(error
, NULL
, descr
, false)) {
551 ret
= PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY
;
557 /* Create and append option argument */
558 opt_item
= create_opt_item(descr
, opt_arg
);
563 if (used_next_orig_arg
) {
569 *item
= &opt_item
->base
;
573 ARGPAR_ASSERT(ret
!= PARSE_ORIG_ARG_OPT_RET_OK
);
580 enum parse_orig_arg_opt_ret
parse_orig_arg_opt(const char * const orig_arg
,
581 const char * const next_orig_arg
,
582 const struct argpar_opt_descr
* const descrs
,
583 struct argpar_iter
* const iter
,
584 struct argpar_error
** const error
,
585 struct argpar_item
** const item
)
587 enum parse_orig_arg_opt_ret ret
= PARSE_ORIG_ARG_OPT_RET_OK
;
589 ARGPAR_ASSERT(orig_arg
[0] == '-');
591 if (orig_arg
[1] == '-') {
593 ret
= parse_long_opt(&orig_arg
[2],
594 next_orig_arg
, descrs
, iter
, error
, item
);
597 ret
= parse_short_opt_group(&orig_arg
[1],
598 next_orig_arg
, descrs
, iter
, error
, item
);
605 struct argpar_iter
*argpar_iter_create(const unsigned int argc
,
606 const char * const * const argv
,
607 const struct argpar_opt_descr
* const descrs
)
609 struct argpar_iter
*iter
= ARGPAR_ZALLOC(struct argpar_iter
);
615 iter
->user
.argc
= argc
;
616 iter
->user
.argv
= argv
;
617 iter
->user
.descrs
= descrs
;
618 iter
->tmp_buf
.size
= 128;
619 iter
->tmp_buf
.data
= ARGPAR_CALLOC(char, iter
->tmp_buf
.size
);
620 if (!iter
->tmp_buf
.data
) {
621 argpar_iter_destroy(iter
);
631 void argpar_iter_destroy(struct argpar_iter
* const iter
)
634 free(iter
->tmp_buf
.data
);
640 enum argpar_iter_next_status
argpar_iter_next(
641 struct argpar_iter
* const iter
,
642 const struct argpar_item
** const item
,
643 const struct argpar_error
** const error
)
645 enum argpar_iter_next_status status
;
646 enum parse_orig_arg_opt_ret parse_orig_arg_opt_ret
;
647 const char *orig_arg
;
648 const char *next_orig_arg
;
649 struct argpar_error
** const nc_error
= (struct argpar_error
**) error
;
651 ARGPAR_ASSERT(iter
->i
<= iter
->user
.argc
);
657 if (iter
->i
== iter
->user
.argc
) {
658 status
= ARGPAR_ITER_NEXT_STATUS_END
;
662 orig_arg
= iter
->user
.argv
[iter
->i
];
664 iter
->i
< (iter
->user
.argc
- 1) ?
665 iter
->user
.argv
[iter
->i
+ 1] : NULL
;
667 if (strcmp(orig_arg
, "-") == 0 || strcmp(orig_arg
, "--") == 0 ||
668 orig_arg
[0] != '-') {
669 /* Non-option argument */
670 const struct argpar_item_non_opt
* const non_opt_item
=
671 create_non_opt_item(orig_arg
, iter
->i
,
672 iter
->non_opt_index
);
675 status
= ARGPAR_ITER_NEXT_STATUS_ERROR_MEMORY
;
679 iter
->non_opt_index
++;
681 *item
= &non_opt_item
->base
;
682 status
= ARGPAR_ITER_NEXT_STATUS_OK
;
686 /* Option argument */
687 parse_orig_arg_opt_ret
= parse_orig_arg_opt(orig_arg
,
688 next_orig_arg
, iter
->user
.descrs
, iter
, nc_error
,
689 (struct argpar_item
**) item
);
690 switch (parse_orig_arg_opt_ret
) {
691 case PARSE_ORIG_ARG_OPT_RET_OK
:
692 status
= ARGPAR_ITER_NEXT_STATUS_OK
;
694 case PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT
:
695 case PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG
:
696 case PARSE_ORIG_ARG_OPT_RET_ERROR_UNEXPECTED_OPT_ARG
:
698 ARGPAR_ASSERT(*error
);
699 (*nc_error
)->orig_index
= iter
->i
;
702 switch (parse_orig_arg_opt_ret
) {
703 case PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT
:
704 status
= ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT
;
706 case PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG
:
707 status
= ARGPAR_ITER_NEXT_STATUS_ERROR_MISSING_OPT_ARG
;
709 case PARSE_ORIG_ARG_OPT_RET_ERROR_UNEXPECTED_OPT_ARG
:
710 status
= ARGPAR_ITER_NEXT_STATUS_ERROR_UNEXPECTED_OPT_ARG
;
717 case PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY
:
718 status
= ARGPAR_ITER_NEXT_STATUS_ERROR_MEMORY
;
729 unsigned int argpar_iter_ingested_orig_args(
730 const struct argpar_iter
* const iter
)