58cbc2b9d917203be676d58e7259bb8518cf0529
[argpar.git] / argpar / argpar.c
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright (c) 2019-2021 Philippe Proulx <pproulx@efficios.com>
5 * Copyright (c) 2020-2021 Simon Marchi <simon.marchi@efficios.com>
6 */
7
8 #include <assert.h>
9 #include <stdarg.h>
10 #include <stdbool.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14
15 #include "argpar.h"
16
17 #define ARGPAR_REALLOC(_ptr, _type, _nmemb) \
18 ((_type *) realloc(_ptr, (_nmemb) * sizeof(_type)))
19
20 #define ARGPAR_CALLOC(_type, _nmemb) \
21 ((_type *) calloc((_nmemb), sizeof(_type)))
22
23 #define ARGPAR_ZALLOC(_type) ARGPAR_CALLOC(_type, 1)
24
25 #define ARGPAR_ASSERT(_cond) assert(_cond)
26
27 #ifdef __MINGW_PRINTF_FORMAT
28 # define ARGPAR_PRINTF_FORMAT __MINGW_PRINTF_FORMAT
29 #else
30 # define ARGPAR_PRINTF_FORMAT printf
31 #endif
32
33 /*
34 * An argpar iterator.
35 *
36 * Such a structure contains the state of an iterator between calls to
37 * argpar_iter_next().
38 */
39 struct argpar_iter {
40 /*
41 * Data provided by the user to argpar_iter_create(); immutable
42 * afterwards.
43 */
44 unsigned int argc;
45 const char * const *argv;
46 const struct argpar_opt_descr *descrs;
47
48 /*
49 * Index of the argument to process in the next
50 * argpar_iter_next() call.
51 */
52 unsigned int i;
53
54 /* Counter of non-option arguments */
55 int non_opt_index;
56
57 /*
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.
62 */
63 const char *short_opt_ch;
64 };
65
66 /* Base parsing item */
67 struct argpar_item {
68 enum argpar_item_type type;
69 };
70
71 /* Option parsing item */
72 struct argpar_item_opt {
73 struct argpar_item base;
74
75 /* Corresponding descriptor */
76 const struct argpar_opt_descr *descr;
77
78 /* Argument, or `NULL` if none; owned by this */
79 char *arg;
80 };
81
82 /* Non-option parsing item */
83 struct argpar_item_non_opt {
84 struct argpar_item base;
85
86 /*
87 * Complete argument, pointing to one of the entries of the
88 * original arguments (`argv`).
89 */
90 const char *arg;
91
92 /*
93 * Index of this argument amongst all original arguments
94 * (`argv`).
95 */
96 unsigned int orig_index;
97
98 /* Index of this argument amongst other non-option arguments */
99 unsigned int non_opt_index;
100 };
101
102 static __attribute__((format(ARGPAR_PRINTF_FORMAT, 1, 0)))
103 char *argpar_vasprintf(const char * const fmt, va_list args)
104 {
105 int len1, len2;
106 char *str;
107 va_list args2;
108
109 va_copy(args2, args);
110 len1 = vsnprintf(NULL, 0, fmt, args);
111 if (len1 < 0) {
112 str = NULL;
113 goto end;
114 }
115
116 str = malloc(len1 + 1);
117 if (!str) {
118 goto end;
119 }
120
121 len2 = vsnprintf(str, len1 + 1, fmt, args2);
122 ARGPAR_ASSERT(len1 == len2);
123
124 end:
125 va_end(args2);
126 return str;
127 }
128
129
130 static __attribute__((format(ARGPAR_PRINTF_FORMAT, 1, 2)))
131 char *argpar_asprintf(const char * const fmt, ...)
132 {
133 va_list args;
134 char *str;
135
136 va_start(args, fmt);
137 str = argpar_vasprintf(fmt, args);
138 va_end(args);
139 return str;
140 }
141
142 static __attribute__((format(ARGPAR_PRINTF_FORMAT, 2, 3)))
143 bool try_append_string_printf(char ** const str, const char *fmt, ...)
144 {
145 char *new_str = NULL;
146 char *addendum = NULL;
147 bool success;
148 va_list args;
149
150 if (!str) {
151 success = true;
152 goto end;
153 }
154
155 ARGPAR_ASSERT(str);
156 va_start(args, fmt);
157 addendum = argpar_vasprintf(fmt, args);
158 va_end(args);
159
160 if (!addendum) {
161 success = false;
162 goto end;
163 }
164
165 new_str = argpar_asprintf("%s%s", *str ? *str : "", addendum);
166 if (!new_str) {
167 success = false;
168 goto end;
169 }
170
171 free(*str);
172 *str = new_str;
173 success = true;
174
175 end:
176 free(addendum);
177 return success;
178 }
179
180 ARGPAR_HIDDEN
181 enum argpar_item_type argpar_item_type(const struct argpar_item * const item)
182 {
183 ARGPAR_ASSERT(item);
184 return item->type;
185 }
186
187 ARGPAR_HIDDEN
188 const struct argpar_opt_descr *argpar_item_opt_descr(
189 const struct argpar_item * const item)
190 {
191 ARGPAR_ASSERT(item);
192 ARGPAR_ASSERT(item->type == ARGPAR_ITEM_TYPE_OPT);
193 return ((const struct argpar_item_opt *) item)->descr;
194 }
195
196 ARGPAR_HIDDEN
197 const char *argpar_item_opt_arg(const struct argpar_item * const item)
198 {
199 ARGPAR_ASSERT(item);
200 ARGPAR_ASSERT(item->type == ARGPAR_ITEM_TYPE_OPT);
201 return ((const struct argpar_item_opt *) item)->arg;
202 }
203
204 ARGPAR_HIDDEN
205 const char *argpar_item_non_opt_arg(const struct argpar_item * const item)
206 {
207 ARGPAR_ASSERT(item);
208 ARGPAR_ASSERT(item->type == ARGPAR_ITEM_TYPE_NON_OPT);
209 return ((const struct argpar_item_non_opt *) item)->arg;
210 }
211
212 ARGPAR_HIDDEN
213 unsigned int argpar_item_non_opt_orig_index(
214 const struct argpar_item * const item)
215 {
216 ARGPAR_ASSERT(item);
217 ARGPAR_ASSERT(item->type == ARGPAR_ITEM_TYPE_NON_OPT);
218 return ((const struct argpar_item_non_opt *) item)->orig_index;
219 }
220
221 ARGPAR_HIDDEN
222 unsigned int argpar_item_non_opt_non_opt_index(
223 const struct argpar_item * const item)
224 {
225 ARGPAR_ASSERT(item);
226 ARGPAR_ASSERT(item->type == ARGPAR_ITEM_TYPE_NON_OPT);
227 return ((const struct argpar_item_non_opt *) item)->non_opt_index;
228 }
229
230 ARGPAR_HIDDEN
231 void argpar_item_destroy(const struct argpar_item * const item)
232 {
233 if (!item) {
234 goto end;
235 }
236
237 if (item->type == ARGPAR_ITEM_TYPE_OPT) {
238 struct argpar_item_opt * const opt_item =
239 (struct argpar_item_opt *) item;
240
241 free(opt_item->arg);
242 }
243
244 free((void *) item);
245
246 end:
247 return;
248 }
249
250 static
251 bool push_item(struct argpar_item_array * const array,
252 const struct argpar_item * const item)
253 {
254 bool success;
255
256 ARGPAR_ASSERT(array);
257 ARGPAR_ASSERT(item);
258
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 *,
263 new_n_alloc);
264 if (!new_items) {
265 success = false;
266 goto end;
267 }
268
269 array->n_alloc = new_n_alloc;
270 array->items = new_items;
271 }
272
273 array->items[array->n_items] = item;
274 array->n_items++;
275 success = true;
276
277 end:
278 return success;
279 }
280
281 static
282 void destroy_item_array(struct argpar_item_array * const array)
283 {
284 if (array) {
285 unsigned int i;
286
287 for (i = 0; i < array->n_items; i++) {
288 argpar_item_destroy(array->items[i]);
289 }
290
291 free(array->items);
292 free(array);
293 }
294 }
295
296 static
297 struct argpar_item_array *create_item_array(void)
298 {
299 struct argpar_item_array *ret;
300 const int initial_size = 10;
301
302 ret = ARGPAR_ZALLOC(struct argpar_item_array);
303 if (!ret) {
304 goto end;
305 }
306
307 ret->items = ARGPAR_CALLOC(const struct argpar_item *, initial_size);
308 if (!ret->items) {
309 goto error;
310 }
311
312 ret->n_alloc = initial_size;
313 goto end;
314
315 error:
316 destroy_item_array(ret);
317 ret = NULL;
318
319 end:
320 return ret;
321 }
322
323 static
324 struct argpar_item_opt *create_opt_item(
325 const struct argpar_opt_descr * const descr,
326 const char * const arg)
327 {
328 struct argpar_item_opt *opt_item =
329 ARGPAR_ZALLOC(struct argpar_item_opt);
330
331 if (!opt_item) {
332 goto end;
333 }
334
335 opt_item->base.type = ARGPAR_ITEM_TYPE_OPT;
336 opt_item->descr = descr;
337
338 if (arg) {
339 opt_item->arg = strdup(arg);
340 if (!opt_item->arg) {
341 goto error;
342 }
343 }
344
345 goto end;
346
347 error:
348 argpar_item_destroy(&opt_item->base);
349 opt_item = NULL;
350
351 end:
352 return opt_item;
353 }
354
355 static
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)
359 {
360 struct argpar_item_non_opt * const non_opt_item =
361 ARGPAR_ZALLOC(struct argpar_item_non_opt);
362
363 if (!non_opt_item) {
364 goto end;
365 }
366
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;
371
372 end:
373 return non_opt_item;
374 }
375
376 static
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)
380 {
381 const struct argpar_opt_descr *descr;
382
383 for (descr = descrs; descr->short_name || descr->long_name; descr++) {
384 if (short_name && descr->short_name &&
385 short_name == descr->short_name) {
386 goto end;
387 }
388
389 if (long_name && descr->long_name &&
390 strcmp(long_name, descr->long_name) == 0) {
391 goto end;
392 }
393 }
394
395 end:
396 return !descr->short_name && !descr->long_name ? NULL : descr;
397 }
398
399 enum parse_orig_arg_opt_ret {
400 PARSE_ORIG_ARG_OPT_RET_OK,
401 PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT = -1,
402 PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG = -2,
403 PARSE_ORIG_ARG_OPT_RET_ERROR_INVALID_ARG = -3,
404 PARSE_ORIG_ARG_OPT_RET_ERROR_UNEXPECTED_OPT_ARG = -4,
405 PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY = -5,
406 };
407
408 static
409 enum parse_orig_arg_opt_ret parse_short_opts(const char * const short_opts,
410 const char * const next_orig_arg,
411 const struct argpar_opt_descr * const descrs,
412 struct argpar_iter * const iter,
413 char ** const error, struct argpar_item ** const item)
414 {
415 enum parse_orig_arg_opt_ret ret = PARSE_ORIG_ARG_OPT_RET_OK;
416 bool used_next_orig_arg = false;
417 const char *opt_arg = NULL;
418 const struct argpar_opt_descr *descr;
419 struct argpar_item_opt *opt_item;
420
421 ARGPAR_ASSERT(strlen(short_opts) != 0);
422
423 if (!iter->short_opt_ch) {
424 iter->short_opt_ch = short_opts;
425 }
426
427 /* Find corresponding option descriptor */
428 descr = find_descr(descrs, *iter->short_opt_ch, NULL);
429 if (!descr) {
430 try_append_string_printf(error, "Unknown option `-%c`",
431 *iter->short_opt_ch);
432 ret = PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT;
433 goto error;
434 }
435
436 if (descr->with_arg) {
437 if (iter->short_opt_ch[1]) {
438 /* `-oarg` form */
439 opt_arg = &iter->short_opt_ch[1];
440 } else {
441 /* `-o arg` form */
442 opt_arg = next_orig_arg;
443 used_next_orig_arg = true;
444 }
445
446 /*
447 * We accept `-o ''` (empty option argument), but not
448 * `-o` alone if an option argument is expected.
449 */
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;
456 ret = PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG;
457 goto error;
458 }
459 }
460
461 /* Create and append option argument */
462 opt_item = create_opt_item(descr, opt_arg);
463 if (!opt_item) {
464 ret = PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY;
465 goto error;
466 }
467
468 *item = &opt_item->base;
469 iter->short_opt_ch++;
470
471 if (descr->with_arg || !*iter->short_opt_ch) {
472 /* Option has an argument: no more options */
473 iter->short_opt_ch = NULL;
474
475 if (used_next_orig_arg) {
476 iter->i += 2;
477 } else {
478 iter->i++;
479 }
480 }
481
482 goto end;
483
484 error:
485 ARGPAR_ASSERT(ret != PARSE_ORIG_ARG_OPT_RET_OK);
486
487 end:
488 return ret;
489 }
490
491 static
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)
497 {
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;
503
504 /* Option's argument, if any */
505 const char *opt_arg = NULL;
506
507 /* Position of first `=`, if any */
508 const char *eq_pos;
509
510 /* Buffer holding option name when `long_opt_arg` contains `=` */
511 char buf[max_len + 1];
512
513 /* Option name */
514 const char *long_opt_name = long_opt_arg;
515
516 ARGPAR_ASSERT(strlen(long_opt_arg) != 0);
517
518 /* Find the first `=` in original argument */
519 eq_pos = strchr(long_opt_arg, '=');
520 if (eq_pos) {
521 const size_t long_opt_name_size = eq_pos - long_opt_arg;
522
523 /* Isolate the option name */
524 if (long_opt_name_size > max_len) {
525 try_append_string_printf(error,
526 "Invalid argument `--%s`", long_opt_arg);
527 ret = PARSE_ORIG_ARG_OPT_RET_ERROR_INVALID_ARG;
528 goto error;
529 }
530
531 memcpy(buf, long_opt_arg, long_opt_name_size);
532 buf[long_opt_name_size] = '\0';
533 long_opt_name = buf;
534 }
535
536 /* Find corresponding option descriptor */
537 descr = find_descr(descrs, '\0', long_opt_name);
538 if (!descr) {
539 try_append_string_printf(error, "Unknown option `--%s`",
540 long_opt_name);
541 ret = PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT;
542 goto error;
543 }
544
545 /* Find option's argument if any */
546 if (descr->with_arg) {
547 if (eq_pos) {
548 /* `--long-opt=arg` style */
549 opt_arg = eq_pos + 1;
550 } else {
551 /* `--long-opt arg` style */
552 if (!next_orig_arg) {
553 try_append_string_printf(error,
554 "Missing required argument for option `--%s`",
555 long_opt_name);
556 ret = PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG;
557 goto error;
558 }
559
560 opt_arg = next_orig_arg;
561 used_next_orig_arg = true;
562 }
563 } else if (eq_pos) {
564 /*
565 * Unexpected `--opt=arg` style for a long option which
566 * doesn't accept an argument.
567 */
568 try_append_string_printf(error,
569 "Unexpected argument for option `--%s`", long_opt_name);
570 ret = PARSE_ORIG_ARG_OPT_RET_ERROR_UNEXPECTED_OPT_ARG;
571 goto error;
572 }
573
574 /* Create and append option argument */
575 opt_item = create_opt_item(descr, opt_arg);
576 if (!opt_item) {
577 goto error;
578 }
579
580 if (used_next_orig_arg) {
581 iter->i += 2;
582 } else {
583 iter->i++;
584 }
585
586 *item = &opt_item->base;
587 goto end;
588
589 error:
590 ARGPAR_ASSERT(ret != PARSE_ORIG_ARG_OPT_RET_OK);
591
592 end:
593 return ret;
594 }
595
596 static
597 enum parse_orig_arg_opt_ret parse_orig_arg_opt(const char * const orig_arg,
598 const char * const next_orig_arg,
599 const struct argpar_opt_descr * const descrs,
600 struct argpar_iter * const iter, char ** const error,
601 struct argpar_item ** const item)
602 {
603 enum parse_orig_arg_opt_ret ret = PARSE_ORIG_ARG_OPT_RET_OK;
604
605 ARGPAR_ASSERT(orig_arg[0] == '-');
606
607 if (orig_arg[1] == '-') {
608 /* Long option */
609 ret = parse_long_opt(&orig_arg[2],
610 next_orig_arg, descrs, iter, error, item);
611 } else {
612 /* Short option */
613 ret = parse_short_opts(&orig_arg[1],
614 next_orig_arg, descrs, iter, error, item);
615 }
616
617 return ret;
618 }
619
620 static
621 bool try_prepend_while_parsing_arg_to_error(char ** const error,
622 const unsigned int i, const char * const arg)
623 {
624 char *new_error;
625 bool success;
626
627 if (!error) {
628 success = true;
629 goto end;
630 }
631
632 ARGPAR_ASSERT(*error);
633 new_error = argpar_asprintf("While parsing argument #%u (`%s`): %s",
634 i + 1, arg, *error);
635 if (!new_error) {
636 success = false;
637 goto end;
638 }
639
640 free(*error);
641 *error = new_error;
642 success = true;
643
644 end:
645 return success;
646 }
647
648 ARGPAR_HIDDEN
649 struct argpar_iter *argpar_iter_create(const unsigned int argc,
650 const char * const * const argv,
651 const struct argpar_opt_descr * const descrs)
652 {
653 struct argpar_iter * const iter = ARGPAR_ZALLOC(struct argpar_iter);
654
655 if (!iter) {
656 goto end;
657 }
658
659 iter->argc = argc;
660 iter->argv = argv;
661 iter->descrs = descrs;
662
663 end:
664 return iter;
665 }
666
667 ARGPAR_HIDDEN
668 void argpar_iter_destroy(struct argpar_iter * const iter)
669 {
670 free(iter);
671 }
672
673 ARGPAR_HIDDEN
674 enum argpar_iter_next_status argpar_iter_next(
675 struct argpar_iter * const iter,
676 const struct argpar_item ** const item, char ** const error)
677 {
678 enum argpar_iter_next_status status;
679 enum parse_orig_arg_opt_ret parse_orig_arg_opt_ret;
680 const char *orig_arg;
681 const char *next_orig_arg;
682
683 ARGPAR_ASSERT(iter->i <= iter->argc);
684
685 if (error) {
686 *error = NULL;
687 }
688
689 if (iter->i == iter->argc) {
690 status = ARGPAR_ITER_NEXT_STATUS_END;
691 goto end;
692 }
693
694 orig_arg = iter->argv[iter->i];
695 next_orig_arg =
696 iter->i < (iter->argc - 1) ? iter->argv[iter->i + 1] : NULL;
697
698 if (strcmp(orig_arg, "-") == 0 || strcmp(orig_arg, "--") == 0 ||
699 orig_arg[0] != '-') {
700 /* Non-option argument */
701 const struct argpar_item_non_opt * const non_opt_item =
702 create_non_opt_item(orig_arg, iter->i,
703 iter->non_opt_index);
704
705 if (!non_opt_item) {
706 status = ARGPAR_ITER_NEXT_STATUS_ERROR_MEMORY;
707 goto end;
708 }
709
710 iter->non_opt_index++;
711 iter->i++;
712 *item = &non_opt_item->base;
713 status = ARGPAR_ITER_NEXT_STATUS_OK;
714 goto end;
715 }
716
717 /* Option argument */
718 parse_orig_arg_opt_ret = parse_orig_arg_opt(orig_arg,
719 next_orig_arg, iter->descrs, iter, error,
720 (struct argpar_item **) item);
721 switch (parse_orig_arg_opt_ret) {
722 case PARSE_ORIG_ARG_OPT_RET_OK:
723 status = ARGPAR_ITER_NEXT_STATUS_OK;
724 break;
725 case PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT:
726 case PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG:
727 case PARSE_ORIG_ARG_OPT_RET_ERROR_INVALID_ARG:
728 case PARSE_ORIG_ARG_OPT_RET_ERROR_UNEXPECTED_OPT_ARG:
729 try_prepend_while_parsing_arg_to_error(error, iter->i,
730 orig_arg);
731
732 switch (parse_orig_arg_opt_ret) {
733 case PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT:
734 status = ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT;
735 break;
736 case PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG:
737 status = ARGPAR_ITER_NEXT_STATUS_ERROR_MISSING_OPT_ARG;
738 break;
739 case PARSE_ORIG_ARG_OPT_RET_ERROR_INVALID_ARG:
740 status = ARGPAR_ITER_NEXT_STATUS_ERROR_INVALID_ARG;
741 break;
742 case PARSE_ORIG_ARG_OPT_RET_ERROR_UNEXPECTED_OPT_ARG:
743 status = ARGPAR_ITER_NEXT_STATUS_ERROR_UNEXPECTED_OPT_ARG;
744 break;
745 default:
746 abort();
747 }
748
749 break;
750 case PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY:
751 status = ARGPAR_ITER_NEXT_STATUS_ERROR_MEMORY;
752 break;
753 default:
754 abort();
755 }
756
757 end:
758 return status;
759 }
760
761 ARGPAR_HIDDEN
762 unsigned int argpar_iter_ingested_orig_args(
763 const struct argpar_iter * const iter)
764 {
765 return iter->i;
766 }
767
768 ARGPAR_HIDDEN
769 struct argpar_parse_ret argpar_parse(const unsigned int argc,
770 const char * const * const argv,
771 const struct argpar_opt_descr * const descrs,
772 const bool fail_on_unknown_opt)
773 {
774 struct argpar_parse_ret parse_ret = { 0 };
775 const struct argpar_item *item = NULL;
776 struct argpar_iter *iter = NULL;
777
778 parse_ret.items = create_item_array();
779 if (!parse_ret.items) {
780 parse_ret.error = strdup("Failed to create items array.");
781 ARGPAR_ASSERT(parse_ret.error);
782 goto error;
783 }
784
785 iter = argpar_iter_create(argc, argv, descrs);
786 if (!iter) {
787 parse_ret.error = strdup("Failed to create argpar iter.");
788 ARGPAR_ASSERT(parse_ret.error);
789 goto error;
790 }
791
792 while (true) {
793 const enum argpar_iter_next_status status =
794 argpar_iter_next(iter, &item, &parse_ret.error);
795
796 switch (status) {
797 case ARGPAR_ITER_NEXT_STATUS_ERROR_MISSING_OPT_ARG:
798 case ARGPAR_ITER_NEXT_STATUS_ERROR_INVALID_ARG:
799 case ARGPAR_ITER_NEXT_STATUS_ERROR_UNEXPECTED_OPT_ARG:
800 case ARGPAR_ITER_NEXT_STATUS_ERROR_MEMORY:
801 goto error;
802 case ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT:
803 if (fail_on_unknown_opt) {
804 parse_ret.ingested_orig_args =
805 argpar_iter_ingested_orig_args(iter);
806 goto error;
807 }
808
809 free(parse_ret.error);
810 parse_ret.error = NULL;
811 goto success;
812 case ARGPAR_ITER_NEXT_STATUS_END:
813 goto success;
814 default:
815 ARGPAR_ASSERT(status == ARGPAR_ITER_NEXT_STATUS_OK);
816 break;
817 }
818
819 if (!push_item(parse_ret.items, item)) {
820 goto error;
821 }
822
823 item = NULL;
824 }
825
826 success:
827 ARGPAR_ASSERT(!parse_ret.error);
828 parse_ret.ingested_orig_args = argpar_iter_ingested_orig_args(iter);
829 goto end;
830
831 error:
832 ARGPAR_ASSERT(parse_ret.error);
833
834 /* That's how we indicate that an error occurred */
835 destroy_item_array(parse_ret.items);
836 parse_ret.items = NULL;
837
838 end:
839 argpar_iter_destroy(iter);
840 argpar_item_destroy(item);
841 return parse_ret;
842 }
843
844 ARGPAR_HIDDEN
845 void argpar_parse_ret_fini(struct argpar_parse_ret * const ret)
846 {
847 ARGPAR_ASSERT(ret);
848 destroy_item_array(ret->items);
849 ret->items = NULL;
850 free(ret->error);
851 ret->error = NULL;
852 }
This page took 0.075692 seconds and 4 git commands to generate.