Parse `-` and `--` as non-option arguments
[argpar.git] / argpar / argpar.c
CommitLineData
903a5b8a 1/*
03e1579f 2 * SPDX-License-Identifier: MIT
903a5b8a 3 *
fc07e526
SM
4 * Copyright (c) 2019-2021 Philippe Proulx <pproulx@efficios.com>
5 * Copyright (c) 2020-2021 Simon Marchi <simon.marchi@efficios.com>
903a5b8a
SM
6 */
7
7ac57709
SM
8#include <assert.h>
9#include <stdarg.h>
903a5b8a 10#include <stdbool.h>
7ac57709 11#include <stdio.h>
903a5b8a
SM
12#include <stdlib.h>
13#include <string.h>
903a5b8a
SM
14
15#include "argpar.h"
16
fb12ac67
PP
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)
7ac57709
SM
24
25#define ARGPAR_ASSERT(_cond) assert(_cond)
26
1ae22b5e
SM
27#ifdef __MINGW_PRINTF_FORMAT
28# define ARGPAR_PRINTF_FORMAT __MINGW_PRINTF_FORMAT
29#else
30# define ARGPAR_PRINTF_FORMAT printf
31#endif
32
fc07e526
SM
33/*
34 * An argpar iterator.
35 *
2af370d0
PP
36 * Such a structure contains the state of an iterator between calls to
37 * argpar_iter_next().
fc07e526
SM
38 */
39struct 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
2af370d0 50 * argpar_iter_next() call.
fc07e526
SM
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
2af370d0 61 * argpar_iter_next() call.
fc07e526
SM
62 */
63 const char *short_opt_ch;
64};
65
d4539a90
PP
66/* Base parsing item */
67struct argpar_item {
68 enum argpar_item_type type;
69};
70
71/* Option parsing item */
72struct 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 */
83struct 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
1ae22b5e 102static __attribute__((format(ARGPAR_PRINTF_FORMAT, 1, 0)))
fb12ac67 103char *argpar_vasprintf(const char * const fmt, va_list args)
7ac57709
SM
104{
105 int len1, len2;
106 char *str;
107 va_list args2;
108
109 va_copy(args2, args);
7ac57709
SM
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);
7ac57709
SM
122 ARGPAR_ASSERT(len1 == len2);
123
124end:
92ecd98e 125 va_end(args2);
7ac57709
SM
126 return str;
127}
128
129
1ae22b5e 130static __attribute__((format(ARGPAR_PRINTF_FORMAT, 1, 2)))
fb12ac67 131char *argpar_asprintf(const char * const fmt, ...)
7ac57709
SM
132{
133 va_list args;
134 char *str;
f46b5106 135
7ac57709
SM
136 va_start(args, fmt);
137 str = argpar_vasprintf(fmt, args);
138 va_end(args);
7ac57709
SM
139 return str;
140}
141
1ae22b5e 142static __attribute__((format(ARGPAR_PRINTF_FORMAT, 2, 3)))
37bb2b1f 143bool try_append_string_printf(char ** const str, const char *fmt, ...)
7ac57709
SM
144{
145 char *new_str = NULL;
37bb2b1f 146 char *addendum = NULL;
7ac57709
SM
147 bool success;
148 va_list args;
149
37bb2b1f
PP
150 if (!str) {
151 success = true;
152 goto end;
153 }
154
7ac57709 155 ARGPAR_ASSERT(str);
7ac57709
SM
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 }
f46b5106 170
7ac57709
SM
171 free(*str);
172 *str = new_str;
7ac57709
SM
173 success = true;
174
175end:
176 free(addendum);
7ac57709
SM
177 return success;
178}
179
d4539a90
PP
180ARGPAR_HIDDEN
181enum argpar_item_type argpar_item_type(const struct argpar_item * const item)
182{
183 ARGPAR_ASSERT(item);
184 return item->type;
185}
186
187ARGPAR_HIDDEN
188const 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
196ARGPAR_HIDDEN
197const 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
204ARGPAR_HIDDEN
205const 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
212ARGPAR_HIDDEN
213unsigned 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
221ARGPAR_HIDDEN
222unsigned 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
fc07e526
SM
230ARGPAR_HIDDEN
231void argpar_item_destroy(const struct argpar_item * const item)
903a5b8a
SM
232{
233 if (!item) {
234 goto end;
235 }
236
1c9a6bde 237 if (item->type == ARGPAR_ITEM_TYPE_OPT) {
fc07e526
SM
238 struct argpar_item_opt * const opt_item =
239 (struct argpar_item_opt *) item;
903a5b8a 240
d4539a90 241 free(opt_item->arg);
903a5b8a
SM
242 }
243
fc07e526 244 free((void *) item);
903a5b8a
SM
245
246end:
247 return;
248}
249
7ac57709 250static
1c9a6bde 251bool push_item(struct argpar_item_array * const array,
fb12ac67 252 const struct argpar_item * const item)
7ac57709
SM
253{
254 bool success;
255
256 ARGPAR_ASSERT(array);
257 ARGPAR_ASSERT(item);
258
259 if (array->n_items == array->n_alloc) {
fb12ac67
PP
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);
7ac57709
SM
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++;
7ac57709
SM
275 success = true;
276
277end:
278 return success;
279}
280
281static
1c9a6bde 282void destroy_item_array(struct argpar_item_array * const array)
7ac57709
SM
283{
284 if (array) {
285 unsigned int i;
286
287 for (i = 0; i < array->n_items; i++) {
fc07e526 288 argpar_item_destroy(array->items[i]);
7ac57709
SM
289 }
290
291 free(array->items);
292 free(array);
293 }
294}
295
296static
fb12ac67 297struct argpar_item_array *create_item_array(void)
7ac57709 298{
1c9a6bde 299 struct argpar_item_array *ret;
7ac57709
SM
300 const int initial_size = 10;
301
fb12ac67 302 ret = ARGPAR_ZALLOC(struct argpar_item_array);
7ac57709
SM
303 if (!ret) {
304 goto end;
305 }
306
fb12ac67 307 ret->items = ARGPAR_CALLOC(const struct argpar_item *, initial_size);
7ac57709
SM
308 if (!ret->items) {
309 goto error;
310 }
311
312 ret->n_alloc = initial_size;
7ac57709
SM
313 goto end;
314
315error:
316 destroy_item_array(ret);
317 ret = NULL;
318
319end:
320 return ret;
321}
322
903a5b8a 323static
1c9a6bde
SM
324struct argpar_item_opt *create_opt_item(
325 const struct argpar_opt_descr * const descr,
903a5b8a
SM
326 const char * const arg)
327{
1c9a6bde 328 struct argpar_item_opt *opt_item =
fb12ac67 329 ARGPAR_ZALLOC(struct argpar_item_opt);
903a5b8a
SM
330
331 if (!opt_item) {
332 goto end;
333 }
334
1c9a6bde 335 opt_item->base.type = ARGPAR_ITEM_TYPE_OPT;
903a5b8a
SM
336 opt_item->descr = descr;
337
338 if (arg) {
7ac57709 339 opt_item->arg = strdup(arg);
903a5b8a
SM
340 if (!opt_item->arg) {
341 goto error;
342 }
343 }
344
345 goto end;
346
347error:
fc07e526 348 argpar_item_destroy(&opt_item->base);
903a5b8a
SM
349 opt_item = NULL;
350
351end:
352 return opt_item;
353}
354
355static
1c9a6bde 356struct argpar_item_non_opt *create_non_opt_item(const char * const arg,
903a5b8a
SM
357 const unsigned int orig_index,
358 const unsigned int non_opt_index)
359{
1c9a6bde 360 struct argpar_item_non_opt * const non_opt_item =
fb12ac67 361 ARGPAR_ZALLOC(struct argpar_item_non_opt);
903a5b8a
SM
362
363 if (!non_opt_item) {
364 goto end;
365 }
366
1c9a6bde 367 non_opt_item->base.type = ARGPAR_ITEM_TYPE_NON_OPT;
903a5b8a
SM
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
372end:
373 return non_opt_item;
374}
375
376static
1c9a6bde
SM
377const struct argpar_opt_descr *find_descr(
378 const struct argpar_opt_descr * const descrs,
903a5b8a
SM
379 const char short_name, const char * const long_name)
380{
1c9a6bde 381 const struct argpar_opt_descr *descr;
903a5b8a
SM
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
395end:
396 return !descr->short_name && !descr->long_name ? NULL : descr;
397}
398
399enum parse_orig_arg_opt_ret {
400 PARSE_ORIG_ARG_OPT_RET_OK,
871eba32
PP
401 PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT = -1,
402 PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG = -2,
d4d05805 403 PARSE_ORIG_ARG_OPT_RET_ERROR_INVALID_ARG = -3,
871eba32
PP
404 PARSE_ORIG_ARG_OPT_RET_ERROR_UNEXPECTED_OPT_ARG = -4,
405 PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY = -5,
903a5b8a
SM
406};
407
408static
409enum parse_orig_arg_opt_ret parse_short_opts(const char * const short_opts,
410 const char * const next_orig_arg,
1c9a6bde 411 const struct argpar_opt_descr * const descrs,
fc07e526
SM
412 struct argpar_iter * const iter,
413 char ** const error, struct argpar_item ** const item)
903a5b8a
SM
414{
415 enum parse_orig_arg_opt_ret ret = PARSE_ORIG_ARG_OPT_RET_OK;
fc07e526
SM
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;
903a5b8a 420
dd757a65 421 ARGPAR_ASSERT(strlen(short_opts) != 0);
903a5b8a 422
fc07e526
SM
423 if (!iter->short_opt_ch) {
424 iter->short_opt_ch = short_opts;
425 }
903a5b8a 426
fc07e526
SM
427 /* Find corresponding option descriptor */
428 descr = find_descr(descrs, *iter->short_opt_ch, NULL);
429 if (!descr) {
37bb2b1f 430 try_append_string_printf(error, "Unknown option `-%c`",
fb12ac67 431 *iter->short_opt_ch);
871eba32 432 ret = PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT;
fc07e526
SM
433 goto error;
434 }
903a5b8a 435
fc07e526
SM
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;
903a5b8a
SM
444 }
445
fc07e526
SM
446 /*
447 * We accept `-o ''` (empty option argument), but not
448 * `-o` alone if an option argument is expected.
449 */
fb12ac67
PP
450 if (!opt_arg || (iter->short_opt_ch[1] &&
451 strlen(opt_arg) == 0)) {
37bb2b1f 452 try_append_string_printf(error,
fc07e526
SM
453 "Missing required argument for option `-%c`",
454 *iter->short_opt_ch);
455 used_next_orig_arg = false;
871eba32 456 ret = PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG;
903a5b8a
SM
457 goto error;
458 }
fc07e526 459 }
903a5b8a 460
fc07e526
SM
461 /* Create and append option argument */
462 opt_item = create_opt_item(descr, opt_arg);
463 if (!opt_item) {
871eba32 464 ret = PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY;
fc07e526
SM
465 goto error;
466 }
903a5b8a 467
fc07e526
SM
468 *item = &opt_item->base;
469 iter->short_opt_ch++;
903a5b8a 470
fc07e526
SM
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 }
903a5b8a
SM
480 }
481
482 goto end;
483
484error:
871eba32 485 ARGPAR_ASSERT(ret != PARSE_ORIG_ARG_OPT_RET_OK);
903a5b8a
SM
486
487end:
488 return ret;
489}
490
491static
492enum parse_orig_arg_opt_ret parse_long_opt(const char * const long_opt_arg,
493 const char * const next_orig_arg,
1c9a6bde 494 const struct argpar_opt_descr * const descrs,
fc07e526
SM
495 struct argpar_iter * const iter,
496 char ** const error, struct argpar_item ** const item)
903a5b8a
SM
497{
498 const size_t max_len = 127;
499 enum parse_orig_arg_opt_ret ret = PARSE_ORIG_ARG_OPT_RET_OK;
1c9a6bde
SM
500 const struct argpar_opt_descr *descr;
501 struct argpar_item_opt *opt_item;
fc07e526 502 bool used_next_orig_arg = false;
903a5b8a
SM
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
dd757a65 516 ARGPAR_ASSERT(strlen(long_opt_arg) != 0);
903a5b8a
SM
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) {
37bb2b1f
PP
525 try_append_string_printf(error,
526 "Invalid argument `--%s`", long_opt_arg);
d4d05805 527 ret = PARSE_ORIG_ARG_OPT_RET_ERROR_INVALID_ARG;
903a5b8a
SM
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) {
37bb2b1f 539 try_append_string_printf(error, "Unknown option `--%s`",
fb12ac67 540 long_opt_name);
903a5b8a
SM
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) {
37bb2b1f 553 try_append_string_printf(error,
903a5b8a
SM
554 "Missing required argument for option `--%s`",
555 long_opt_name);
871eba32 556 ret = PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG;
903a5b8a
SM
557 goto error;
558 }
559
560 opt_arg = next_orig_arg;
fc07e526 561 used_next_orig_arg = true;
903a5b8a 562 }
430fe886
SM
563 } else if (eq_pos) {
564 /*
565 * Unexpected `--opt=arg` style for a long option which
566 * doesn't accept an argument.
567 */
37bb2b1f 568 try_append_string_printf(error,
fc07e526 569 "Unexpected argument for option `--%s`", long_opt_name);
871eba32 570 ret = PARSE_ORIG_ARG_OPT_RET_ERROR_UNEXPECTED_OPT_ARG;
430fe886 571 goto error;
903a5b8a
SM
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
fc07e526
SM
580 if (used_next_orig_arg) {
581 iter->i += 2;
582 } else {
583 iter->i++;
7ac57709
SM
584 }
585
fc07e526 586 *item = &opt_item->base;
903a5b8a
SM
587 goto end;
588
589error:
871eba32 590 ARGPAR_ASSERT(ret != PARSE_ORIG_ARG_OPT_RET_OK);
903a5b8a
SM
591
592end:
593 return ret;
594}
595
596static
597enum parse_orig_arg_opt_ret parse_orig_arg_opt(const char * const orig_arg,
598 const char * const next_orig_arg,
1c9a6bde 599 const struct argpar_opt_descr * const descrs,
fb12ac67 600 struct argpar_iter * const iter, char ** const error,
fc07e526 601 struct argpar_item ** const item)
903a5b8a
SM
602{
603 enum parse_orig_arg_opt_ret ret = PARSE_ORIG_ARG_OPT_RET_OK;
604
7ac57709 605 ARGPAR_ASSERT(orig_arg[0] == '-');
903a5b8a
SM
606
607 if (orig_arg[1] == '-') {
608 /* Long option */
609 ret = parse_long_opt(&orig_arg[2],
fc07e526 610 next_orig_arg, descrs, iter, error, item);
903a5b8a
SM
611 } else {
612 /* Short option */
613 ret = parse_short_opts(&orig_arg[1],
fc07e526 614 next_orig_arg, descrs, iter, error, item);
903a5b8a
SM
615 }
616
617 return ret;
618}
619
620static
37bb2b1f 621bool try_prepend_while_parsing_arg_to_error(char ** const error,
903a5b8a
SM
622 const unsigned int i, const char * const arg)
623{
7ac57709
SM
624 char *new_error;
625 bool success;
903a5b8a 626
37bb2b1f
PP
627 if (!error) {
628 success = true;
629 goto end;
630 }
631
7ac57709 632 ARGPAR_ASSERT(*error);
7ac57709
SM
633 new_error = argpar_asprintf("While parsing argument #%u (`%s`): %s",
634 i + 1, arg, *error);
635 if (!new_error) {
636 success = false;
903a5b8a
SM
637 goto end;
638 }
639
7ac57709
SM
640 free(*error);
641 *error = new_error;
642 success = true;
903a5b8a
SM
643
644end:
7ac57709 645 return success;
903a5b8a
SM
646}
647
7ac57709 648ARGPAR_HIDDEN
fc07e526
SM
649struct argpar_iter *argpar_iter_create(const unsigned int argc,
650 const char * const * const argv,
651 const struct argpar_opt_descr * const descrs)
903a5b8a 652{
fb12ac67 653 struct argpar_iter * const iter = ARGPAR_ZALLOC(struct argpar_iter);
903a5b8a 654
fc07e526
SM
655 if (!iter) {
656 goto end;
903a5b8a
SM
657 }
658
fc07e526
SM
659 iter->argc = argc;
660 iter->argv = argv;
661 iter->descrs = descrs;
903a5b8a 662
fc07e526
SM
663end:
664 return iter;
665}
903a5b8a 666
fc07e526
SM
667ARGPAR_HIDDEN
668void argpar_iter_destroy(struct argpar_iter * const iter)
669{
670 free(iter);
671}
903a5b8a 672
fc07e526 673ARGPAR_HIDDEN
2af370d0 674enum argpar_iter_next_status argpar_iter_next(
fc07e526 675 struct argpar_iter * const iter,
fb12ac67 676 const struct argpar_item ** const item, char ** const error)
fc07e526 677{
2af370d0 678 enum argpar_iter_next_status status;
fc07e526
SM
679 enum parse_orig_arg_opt_ret parse_orig_arg_opt_ret;
680 const char *orig_arg;
681 const char *next_orig_arg;
7ac57709 682
fc07e526 683 ARGPAR_ASSERT(iter->i <= iter->argc);
37bb2b1f
PP
684
685 if (error) {
686 *error = NULL;
687 }
fc07e526
SM
688
689 if (iter->i == iter->argc) {
2af370d0 690 status = ARGPAR_ITER_NEXT_STATUS_END;
fc07e526
SM
691 goto end;
692 }
7ac57709 693
fc07e526
SM
694 orig_arg = iter->argv[iter->i];
695 next_orig_arg =
696 iter->i < (iter->argc - 1) ? iter->argv[iter->i + 1] : NULL;
697
dd757a65
PP
698 if (strcmp(orig_arg, "-") == 0 || strcmp(orig_arg, "--") == 0 ||
699 orig_arg[0] != '-') {
fc07e526 700 /* Non-option argument */
dd757a65 701 const struct argpar_item_non_opt * const non_opt_item =
fc07e526
SM
702 create_non_opt_item(orig_arg, iter->i,
703 iter->non_opt_index);
704
705 if (!non_opt_item) {
2af370d0 706 status = ARGPAR_ITER_NEXT_STATUS_ERROR_MEMORY;
fc07e526 707 goto end;
903a5b8a
SM
708 }
709
fc07e526
SM
710 iter->non_opt_index++;
711 iter->i++;
712 *item = &non_opt_item->base;
2af370d0 713 status = ARGPAR_ITER_NEXT_STATUS_OK;
fc07e526
SM
714 goto end;
715 }
903a5b8a 716
fc07e526
SM
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:
2af370d0 723 status = ARGPAR_ITER_NEXT_STATUS_OK;
fc07e526
SM
724 break;
725 case PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT:
871eba32 726 case PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG:
d4d05805 727 case PARSE_ORIG_ARG_OPT_RET_ERROR_INVALID_ARG:
871eba32 728 case PARSE_ORIG_ARG_OPT_RET_ERROR_UNEXPECTED_OPT_ARG:
37bb2b1f
PP
729 try_prepend_while_parsing_arg_to_error(error, iter->i,
730 orig_arg);
d4d05805
PP
731
732 switch (parse_orig_arg_opt_ret) {
733 case PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT:
2af370d0 734 status = ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT;
d4d05805
PP
735 break;
736 case PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG:
2af370d0 737 status = ARGPAR_ITER_NEXT_STATUS_ERROR_MISSING_OPT_ARG;
d4d05805
PP
738 break;
739 case PARSE_ORIG_ARG_OPT_RET_ERROR_INVALID_ARG:
2af370d0 740 status = ARGPAR_ITER_NEXT_STATUS_ERROR_INVALID_ARG;
d4d05805
PP
741 break;
742 case PARSE_ORIG_ARG_OPT_RET_ERROR_UNEXPECTED_OPT_ARG:
2af370d0 743 status = ARGPAR_ITER_NEXT_STATUS_ERROR_UNEXPECTED_OPT_ARG;
d4d05805
PP
744 break;
745 default:
746 abort();
747 }
748
749 break;
750 case PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY:
2af370d0 751 status = ARGPAR_ITER_NEXT_STATUS_ERROR_MEMORY;
fc07e526
SM
752 break;
753 default:
754 abort();
755 }
756
757end:
758 return status;
759}
760
761ARGPAR_HIDDEN
f3ab5ca1 762unsigned int argpar_iter_ingested_orig_args(
fc07e526
SM
763 const struct argpar_iter * const iter)
764{
765 return iter->i;
766}
767
768ARGPAR_HIDDEN
769struct 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
fb12ac67 778 parse_ret.items = create_item_array();
fc07e526
SM
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) {
2af370d0
PP
793 const enum argpar_iter_next_status status =
794 argpar_iter_next(iter, &item, &parse_ret.error);
fc07e526 795
d4d05805 796 switch (status) {
2af370d0
PP
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:
fc07e526 801 goto error;
2af370d0 802 case ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT:
903a5b8a 803 if (fail_on_unknown_opt) {
fc07e526 804 parse_ret.ingested_orig_args =
f3ab5ca1 805 argpar_iter_ingested_orig_args(iter);
903a5b8a
SM
806 goto error;
807 }
808
7ac57709 809 free(parse_ret.error);
903a5b8a 810 parse_ret.error = NULL;
d4d05805 811 goto success;
2af370d0 812 case ARGPAR_ITER_NEXT_STATUS_END:
d4d05805
PP
813 goto success;
814 default:
2af370d0 815 ARGPAR_ASSERT(status == ARGPAR_ITER_NEXT_STATUS_OK);
fc07e526 816 break;
903a5b8a
SM
817 }
818
fb12ac67 819 if (!push_item(parse_ret.items, item)) {
fc07e526 820 goto error;
903a5b8a 821 }
fc07e526
SM
822
823 item = NULL;
903a5b8a
SM
824 }
825
d4d05805 826success:
fc07e526 827 ARGPAR_ASSERT(!parse_ret.error);
f3ab5ca1 828 parse_ret.ingested_orig_args = argpar_iter_ingested_orig_args(iter);
903a5b8a
SM
829 goto end;
830
831error:
fc07e526
SM
832 ARGPAR_ASSERT(parse_ret.error);
833
834 /* That's how we indicate that an error occurred */
7ac57709
SM
835 destroy_item_array(parse_ret.items);
836 parse_ret.items = NULL;
903a5b8a
SM
837
838end:
fc07e526
SM
839 argpar_iter_destroy(iter);
840 argpar_item_destroy(item);
903a5b8a
SM
841 return parse_ret;
842}
843
7ac57709 844ARGPAR_HIDDEN
fb12ac67 845void argpar_parse_ret_fini(struct argpar_parse_ret * const ret)
903a5b8a 846{
7ac57709 847 ARGPAR_ASSERT(ret);
7ac57709
SM
848 destroy_item_array(ret->items);
849 ret->items = NULL;
7ac57709
SM
850 free(ret->error);
851 ret->error = NULL;
903a5b8a 852}
This page took 0.100942 seconds and 4 git commands to generate.