enum argpar_iter_next_status: assign negative integral values to errors
[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 /*
d7a82d7f
PP
58 * Current character within the current short option group: if
59 * it's not `NULL`, the parser is within a short option group,
60 * therefore it must resume there in the next argpar_iter_next()
61 * call.
fc07e526 62 */
d7a82d7f 63 const char *short_opt_group_ch;
d1f7bbdb
PP
64
65 /* Temporary character buffer which only grows */
66 struct {
67 size_t size;
68 char *data;
69 } tmp_buf;
fc07e526
SM
70};
71
d4539a90
PP
72/* Base parsing item */
73struct argpar_item {
74 enum argpar_item_type type;
75};
76
77/* Option parsing item */
78struct argpar_item_opt {
79 struct argpar_item base;
80
81 /* Corresponding descriptor */
82 const struct argpar_opt_descr *descr;
83
84 /* Argument, or `NULL` if none; owned by this */
85 char *arg;
86};
87
88/* Non-option parsing item */
89struct argpar_item_non_opt {
90 struct argpar_item base;
91
92 /*
93 * Complete argument, pointing to one of the entries of the
94 * original arguments (`argv`).
95 */
96 const char *arg;
97
98 /*
99 * Index of this argument amongst all original arguments
100 * (`argv`).
101 */
102 unsigned int orig_index;
103
104 /* Index of this argument amongst other non-option arguments */
105 unsigned int non_opt_index;
106};
107
8b95d883
PP
108/* Parsing error */
109struct argpar_error {
110 /* Original argument index */
111 unsigned int orig_index;
7ac57709 112
8b95d883
PP
113 /* Name of unknown option; owned by this */
114 char *unknown_opt_name;
f46b5106 115
8b95d883
PP
116 /* Option descriptor */
117 const struct argpar_opt_descr *opt_descr;
7ac57709 118
8b95d883
PP
119 /* `true` if a short option caused the error */
120 bool is_short;
121};
7ac57709 122
d4539a90
PP
123ARGPAR_HIDDEN
124enum argpar_item_type argpar_item_type(const struct argpar_item * const item)
125{
126 ARGPAR_ASSERT(item);
127 return item->type;
128}
129
130ARGPAR_HIDDEN
131const struct argpar_opt_descr *argpar_item_opt_descr(
132 const struct argpar_item * const item)
133{
134 ARGPAR_ASSERT(item);
135 ARGPAR_ASSERT(item->type == ARGPAR_ITEM_TYPE_OPT);
136 return ((const struct argpar_item_opt *) item)->descr;
137}
138
139ARGPAR_HIDDEN
140const char *argpar_item_opt_arg(const struct argpar_item * const item)
141{
142 ARGPAR_ASSERT(item);
143 ARGPAR_ASSERT(item->type == ARGPAR_ITEM_TYPE_OPT);
144 return ((const struct argpar_item_opt *) item)->arg;
145}
146
147ARGPAR_HIDDEN
148const char *argpar_item_non_opt_arg(const struct argpar_item * const item)
149{
150 ARGPAR_ASSERT(item);
151 ARGPAR_ASSERT(item->type == ARGPAR_ITEM_TYPE_NON_OPT);
152 return ((const struct argpar_item_non_opt *) item)->arg;
153}
154
155ARGPAR_HIDDEN
156unsigned int argpar_item_non_opt_orig_index(
157 const struct argpar_item * const item)
158{
159 ARGPAR_ASSERT(item);
160 ARGPAR_ASSERT(item->type == ARGPAR_ITEM_TYPE_NON_OPT);
161 return ((const struct argpar_item_non_opt *) item)->orig_index;
162}
163
164ARGPAR_HIDDEN
165unsigned int argpar_item_non_opt_non_opt_index(
166 const struct argpar_item * const item)
167{
168 ARGPAR_ASSERT(item);
169 ARGPAR_ASSERT(item->type == ARGPAR_ITEM_TYPE_NON_OPT);
170 return ((const struct argpar_item_non_opt *) item)->non_opt_index;
171}
172
fc07e526
SM
173ARGPAR_HIDDEN
174void argpar_item_destroy(const struct argpar_item * const item)
903a5b8a
SM
175{
176 if (!item) {
177 goto end;
178 }
179
1c9a6bde 180 if (item->type == ARGPAR_ITEM_TYPE_OPT) {
fc07e526
SM
181 struct argpar_item_opt * const opt_item =
182 (struct argpar_item_opt *) item;
903a5b8a 183
d4539a90 184 free(opt_item->arg);
903a5b8a
SM
185 }
186
fc07e526 187 free((void *) item);
903a5b8a
SM
188
189end:
190 return;
191}
192
193static
1c9a6bde
SM
194struct argpar_item_opt *create_opt_item(
195 const struct argpar_opt_descr * const descr,
903a5b8a
SM
196 const char * const arg)
197{
1c9a6bde 198 struct argpar_item_opt *opt_item =
fb12ac67 199 ARGPAR_ZALLOC(struct argpar_item_opt);
903a5b8a
SM
200
201 if (!opt_item) {
202 goto end;
203 }
204
1c9a6bde 205 opt_item->base.type = ARGPAR_ITEM_TYPE_OPT;
903a5b8a
SM
206 opt_item->descr = descr;
207
208 if (arg) {
7ac57709 209 opt_item->arg = strdup(arg);
903a5b8a
SM
210 if (!opt_item->arg) {
211 goto error;
212 }
213 }
214
215 goto end;
216
217error:
fc07e526 218 argpar_item_destroy(&opt_item->base);
903a5b8a
SM
219 opt_item = NULL;
220
221end:
222 return opt_item;
223}
224
225static
1c9a6bde 226struct argpar_item_non_opt *create_non_opt_item(const char * const arg,
903a5b8a
SM
227 const unsigned int orig_index,
228 const unsigned int non_opt_index)
229{
1c9a6bde 230 struct argpar_item_non_opt * const non_opt_item =
fb12ac67 231 ARGPAR_ZALLOC(struct argpar_item_non_opt);
903a5b8a
SM
232
233 if (!non_opt_item) {
234 goto end;
235 }
236
1c9a6bde 237 non_opt_item->base.type = ARGPAR_ITEM_TYPE_NON_OPT;
903a5b8a
SM
238 non_opt_item->arg = arg;
239 non_opt_item->orig_index = orig_index;
240 non_opt_item->non_opt_index = non_opt_index;
241
242end:
243 return non_opt_item;
244}
245
8b95d883
PP
246/*
247 * If `error` is not `NULL`, sets the error `error` to a new parsing
248 * error object, setting its `unknown_opt_name`, `opt_descr`, and
249 * `is_short` members from the parameters.
250 *
251 * `unknown_opt_name` is the unknown option name without any `-` or `--`
252 * prefix: `is_short` controls which type of unknown option it is.
253 *
254 * Returns 0 on success (including if `error` is `NULL`) or -1 on memory
255 * error.
256 */
257static
258int set_error(struct argpar_error ** const error,
259 const char * const unknown_opt_name,
260 const struct argpar_opt_descr * const opt_descr,
261 const bool is_short)
262{
263 int ret = 0;
264
265 if (!error) {
266 goto end;
267 }
268
269 *error = ARGPAR_ZALLOC(struct argpar_error);
270 if (!*error) {
271 goto error;
272 }
273
274 if (unknown_opt_name) {
275 (*error)->unknown_opt_name = ARGPAR_CALLOC(char,
276 strlen(unknown_opt_name) + 1 + is_short ? 1 : 2);
277 if (!(*error)->unknown_opt_name) {
278 goto error;
279 }
280
281 if (is_short) {
282 strcpy((*error)->unknown_opt_name, "-");
283 } else {
284 strcpy((*error)->unknown_opt_name, "--");
285 }
286
287 strcat((*error)->unknown_opt_name, unknown_opt_name);
288 }
289
290 (*error)->opt_descr = opt_descr;
291 (*error)->is_short = is_short;
292 goto end;
293
294error:
295 argpar_error_destroy(*error);
296 ret = -1;
297
298end:
299 return ret;
300}
301
302ARGPAR_HIDDEN
303unsigned int argpar_error_orig_index(const struct argpar_error * const error)
304{
305 ARGPAR_ASSERT(error);
306 return error->orig_index;
307}
308
309ARGPAR_HIDDEN
310const char *argpar_error_unknown_opt_name(
311 const struct argpar_error * const error)
312{
313 ARGPAR_ASSERT(error);
314 ARGPAR_ASSERT(error->unknown_opt_name);
315 return error->unknown_opt_name;
316}
317
318ARGPAR_HIDDEN
319const struct argpar_opt_descr *argpar_error_opt_descr(
320 const struct argpar_error * const error, bool * const is_short)
321{
322 ARGPAR_ASSERT(error);
323 ARGPAR_ASSERT(error->opt_descr);
324
325 if (is_short) {
326 *is_short = error->is_short;
327 }
328
329 return error->opt_descr;
330}
331
332ARGPAR_HIDDEN
333void argpar_error_destroy(const struct argpar_error * const error)
334{
335 if (error) {
336 free(error->unknown_opt_name);
337 free((void *) error);
338 }
339}
340
903a5b8a 341static
1c9a6bde
SM
342const struct argpar_opt_descr *find_descr(
343 const struct argpar_opt_descr * const descrs,
903a5b8a
SM
344 const char short_name, const char * const long_name)
345{
1c9a6bde 346 const struct argpar_opt_descr *descr;
903a5b8a
SM
347
348 for (descr = descrs; descr->short_name || descr->long_name; descr++) {
349 if (short_name && descr->short_name &&
350 short_name == descr->short_name) {
351 goto end;
352 }
353
354 if (long_name && descr->long_name &&
355 strcmp(long_name, descr->long_name) == 0) {
356 goto end;
357 }
358 }
359
360end:
361 return !descr->short_name && !descr->long_name ? NULL : descr;
362}
363
364enum parse_orig_arg_opt_ret {
365 PARSE_ORIG_ARG_OPT_RET_OK,
871eba32
PP
366 PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT = -1,
367 PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG = -2,
871eba32
PP
368 PARSE_ORIG_ARG_OPT_RET_ERROR_UNEXPECTED_OPT_ARG = -4,
369 PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY = -5,
903a5b8a
SM
370};
371
372static
d7a82d7f
PP
373enum parse_orig_arg_opt_ret parse_short_opt_group(
374 const char * const short_opt_group,
903a5b8a 375 const char * const next_orig_arg,
1c9a6bde 376 const struct argpar_opt_descr * const descrs,
fc07e526 377 struct argpar_iter * const iter,
8b95d883
PP
378 struct argpar_error ** const error,
379 struct argpar_item ** const item)
903a5b8a
SM
380{
381 enum parse_orig_arg_opt_ret ret = PARSE_ORIG_ARG_OPT_RET_OK;
fc07e526
SM
382 bool used_next_orig_arg = false;
383 const char *opt_arg = NULL;
384 const struct argpar_opt_descr *descr;
385 struct argpar_item_opt *opt_item;
903a5b8a 386
d7a82d7f 387 ARGPAR_ASSERT(strlen(short_opt_group) != 0);
903a5b8a 388
d7a82d7f
PP
389 if (!iter->short_opt_group_ch) {
390 iter->short_opt_group_ch = short_opt_group;
fc07e526 391 }
903a5b8a 392
fc07e526 393 /* Find corresponding option descriptor */
d7a82d7f 394 descr = find_descr(descrs, *iter->short_opt_group_ch, NULL);
fc07e526 395 if (!descr) {
8b95d883
PP
396 const char unknown_opt_name[] =
397 {*iter->short_opt_group_ch, '\0'};
398
871eba32 399 ret = PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT;
8b95d883
PP
400
401 if (set_error(error, unknown_opt_name, NULL, true)) {
402 ret = PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY;
403 }
404
fc07e526
SM
405 goto error;
406 }
903a5b8a 407
fc07e526 408 if (descr->with_arg) {
d7a82d7f 409 if (iter->short_opt_group_ch[1]) {
fc07e526 410 /* `-oarg` form */
d7a82d7f 411 opt_arg = &iter->short_opt_group_ch[1];
fc07e526
SM
412 } else {
413 /* `-o arg` form */
414 opt_arg = next_orig_arg;
415 used_next_orig_arg = true;
903a5b8a
SM
416 }
417
fc07e526
SM
418 /*
419 * We accept `-o ''` (empty option argument), but not
420 * `-o` alone if an option argument is expected.
421 */
d7a82d7f 422 if (!opt_arg || (iter->short_opt_group_ch[1] &&
fb12ac67 423 strlen(opt_arg) == 0)) {
871eba32 424 ret = PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG;
8b95d883
PP
425
426 if (set_error(error, NULL, descr, true)) {
427 ret = PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY;
428 }
429
903a5b8a
SM
430 goto error;
431 }
fc07e526 432 }
903a5b8a 433
fc07e526
SM
434 /* Create and append option argument */
435 opt_item = create_opt_item(descr, opt_arg);
436 if (!opt_item) {
871eba32 437 ret = PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY;
fc07e526
SM
438 goto error;
439 }
903a5b8a 440
fc07e526 441 *item = &opt_item->base;
d7a82d7f 442 iter->short_opt_group_ch++;
903a5b8a 443
d7a82d7f 444 if (descr->with_arg || !*iter->short_opt_group_ch) {
fc07e526 445 /* Option has an argument: no more options */
d7a82d7f 446 iter->short_opt_group_ch = NULL;
fc07e526
SM
447
448 if (used_next_orig_arg) {
449 iter->i += 2;
450 } else {
451 iter->i++;
452 }
903a5b8a
SM
453 }
454
455 goto end;
456
457error:
871eba32 458 ARGPAR_ASSERT(ret != PARSE_ORIG_ARG_OPT_RET_OK);
903a5b8a
SM
459
460end:
461 return ret;
462}
463
464static
465enum parse_orig_arg_opt_ret parse_long_opt(const char * const long_opt_arg,
466 const char * const next_orig_arg,
1c9a6bde 467 const struct argpar_opt_descr * const descrs,
fc07e526 468 struct argpar_iter * const iter,
8b95d883
PP
469 struct argpar_error ** const error,
470 struct argpar_item ** const item)
903a5b8a 471{
903a5b8a 472 enum parse_orig_arg_opt_ret ret = PARSE_ORIG_ARG_OPT_RET_OK;
1c9a6bde
SM
473 const struct argpar_opt_descr *descr;
474 struct argpar_item_opt *opt_item;
fc07e526 475 bool used_next_orig_arg = false;
903a5b8a
SM
476
477 /* Option's argument, if any */
478 const char *opt_arg = NULL;
479
480 /* Position of first `=`, if any */
481 const char *eq_pos;
482
903a5b8a
SM
483 /* Option name */
484 const char *long_opt_name = long_opt_arg;
485
dd757a65 486 ARGPAR_ASSERT(strlen(long_opt_arg) != 0);
903a5b8a
SM
487
488 /* Find the first `=` in original argument */
489 eq_pos = strchr(long_opt_arg, '=');
490 if (eq_pos) {
491 const size_t long_opt_name_size = eq_pos - long_opt_arg;
492
493 /* Isolate the option name */
d1f7bbdb
PP
494 while (long_opt_name_size > iter->tmp_buf.size - 1) {
495 iter->tmp_buf.size *= 2;
496 iter->tmp_buf.data = ARGPAR_REALLOC(iter->tmp_buf.data,
497 char, iter->tmp_buf.size);
498 if (!iter->tmp_buf.data) {
499 ret = PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY;
500 goto error;
501 }
903a5b8a
SM
502 }
503
d1f7bbdb
PP
504 memcpy(iter->tmp_buf.data, long_opt_arg, long_opt_name_size);
505 iter->tmp_buf.data[long_opt_name_size] = '\0';
506 long_opt_name = iter->tmp_buf.data;
903a5b8a
SM
507 }
508
509 /* Find corresponding option descriptor */
510 descr = find_descr(descrs, '\0', long_opt_name);
511 if (!descr) {
903a5b8a 512 ret = PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT;
8b95d883
PP
513
514 if (set_error(error, long_opt_name, NULL, false)) {
515 ret = PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY;
516 }
517
903a5b8a
SM
518 goto error;
519 }
520
521 /* Find option's argument if any */
522 if (descr->with_arg) {
523 if (eq_pos) {
524 /* `--long-opt=arg` style */
525 opt_arg = eq_pos + 1;
526 } else {
527 /* `--long-opt arg` style */
528 if (!next_orig_arg) {
871eba32 529 ret = PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG;
8b95d883
PP
530
531 if (set_error(error, NULL, descr, false)) {
532 ret = PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY;
533 }
534
903a5b8a
SM
535 goto error;
536 }
537
538 opt_arg = next_orig_arg;
fc07e526 539 used_next_orig_arg = true;
903a5b8a 540 }
430fe886
SM
541 } else if (eq_pos) {
542 /*
543 * Unexpected `--opt=arg` style for a long option which
544 * doesn't accept an argument.
545 */
871eba32 546 ret = PARSE_ORIG_ARG_OPT_RET_ERROR_UNEXPECTED_OPT_ARG;
8b95d883
PP
547
548 if (set_error(error, NULL, descr, false)) {
549 ret = PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY;
550 }
551
430fe886 552 goto error;
903a5b8a
SM
553 }
554
555 /* Create and append option argument */
556 opt_item = create_opt_item(descr, opt_arg);
557 if (!opt_item) {
558 goto error;
559 }
560
fc07e526
SM
561 if (used_next_orig_arg) {
562 iter->i += 2;
563 } else {
564 iter->i++;
7ac57709
SM
565 }
566
fc07e526 567 *item = &opt_item->base;
903a5b8a
SM
568 goto end;
569
570error:
871eba32 571 ARGPAR_ASSERT(ret != PARSE_ORIG_ARG_OPT_RET_OK);
903a5b8a
SM
572
573end:
574 return ret;
575}
576
577static
578enum parse_orig_arg_opt_ret parse_orig_arg_opt(const char * const orig_arg,
579 const char * const next_orig_arg,
1c9a6bde 580 const struct argpar_opt_descr * const descrs,
8b95d883
PP
581 struct argpar_iter * const iter,
582 struct argpar_error ** const error,
fc07e526 583 struct argpar_item ** const item)
903a5b8a
SM
584{
585 enum parse_orig_arg_opt_ret ret = PARSE_ORIG_ARG_OPT_RET_OK;
586
7ac57709 587 ARGPAR_ASSERT(orig_arg[0] == '-');
903a5b8a
SM
588
589 if (orig_arg[1] == '-') {
590 /* Long option */
591 ret = parse_long_opt(&orig_arg[2],
fc07e526 592 next_orig_arg, descrs, iter, error, item);
903a5b8a
SM
593 } else {
594 /* Short option */
d7a82d7f 595 ret = parse_short_opt_group(&orig_arg[1],
fc07e526 596 next_orig_arg, descrs, iter, error, item);
903a5b8a
SM
597 }
598
599 return ret;
600}
601
7ac57709 602ARGPAR_HIDDEN
fc07e526
SM
603struct argpar_iter *argpar_iter_create(const unsigned int argc,
604 const char * const * const argv,
605 const struct argpar_opt_descr * const descrs)
903a5b8a 606{
d1f7bbdb 607 struct argpar_iter *iter = ARGPAR_ZALLOC(struct argpar_iter);
903a5b8a 608
fc07e526
SM
609 if (!iter) {
610 goto end;
903a5b8a
SM
611 }
612
fc07e526
SM
613 iter->argc = argc;
614 iter->argv = argv;
615 iter->descrs = descrs;
d1f7bbdb
PP
616 iter->tmp_buf.size = 128;
617 iter->tmp_buf.data = ARGPAR_CALLOC(char, iter->tmp_buf.size);
618 if (!iter->tmp_buf.data) {
619 argpar_iter_destroy(iter);
620 iter = NULL;
621 goto end;
622 }
903a5b8a 623
fc07e526
SM
624end:
625 return iter;
626}
903a5b8a 627
fc07e526
SM
628ARGPAR_HIDDEN
629void argpar_iter_destroy(struct argpar_iter * const iter)
630{
d1f7bbdb
PP
631 if (iter) {
632 free(iter->tmp_buf.data);
633 free(iter);
634 }
fc07e526 635}
903a5b8a 636
fc07e526 637ARGPAR_HIDDEN
2af370d0 638enum argpar_iter_next_status argpar_iter_next(
fc07e526 639 struct argpar_iter * const iter,
8b95d883
PP
640 const struct argpar_item ** const item,
641 const struct argpar_error ** const error)
fc07e526 642{
2af370d0 643 enum argpar_iter_next_status status;
fc07e526
SM
644 enum parse_orig_arg_opt_ret parse_orig_arg_opt_ret;
645 const char *orig_arg;
646 const char *next_orig_arg;
8b95d883 647 struct argpar_error ** const nc_error = (struct argpar_error **) error;
7ac57709 648
fc07e526 649 ARGPAR_ASSERT(iter->i <= iter->argc);
37bb2b1f
PP
650
651 if (error) {
8b95d883 652 *nc_error = NULL;
37bb2b1f 653 }
fc07e526
SM
654
655 if (iter->i == iter->argc) {
2af370d0 656 status = ARGPAR_ITER_NEXT_STATUS_END;
fc07e526
SM
657 goto end;
658 }
7ac57709 659
fc07e526
SM
660 orig_arg = iter->argv[iter->i];
661 next_orig_arg =
662 iter->i < (iter->argc - 1) ? iter->argv[iter->i + 1] : NULL;
663
dd757a65
PP
664 if (strcmp(orig_arg, "-") == 0 || strcmp(orig_arg, "--") == 0 ||
665 orig_arg[0] != '-') {
fc07e526 666 /* Non-option argument */
dd757a65 667 const struct argpar_item_non_opt * const non_opt_item =
fc07e526
SM
668 create_non_opt_item(orig_arg, iter->i,
669 iter->non_opt_index);
670
671 if (!non_opt_item) {
2af370d0 672 status = ARGPAR_ITER_NEXT_STATUS_ERROR_MEMORY;
fc07e526 673 goto end;
903a5b8a
SM
674 }
675
fc07e526
SM
676 iter->non_opt_index++;
677 iter->i++;
678 *item = &non_opt_item->base;
2af370d0 679 status = ARGPAR_ITER_NEXT_STATUS_OK;
fc07e526
SM
680 goto end;
681 }
903a5b8a 682
fc07e526
SM
683 /* Option argument */
684 parse_orig_arg_opt_ret = parse_orig_arg_opt(orig_arg,
8b95d883 685 next_orig_arg, iter->descrs, iter, nc_error,
fc07e526
SM
686 (struct argpar_item **) item);
687 switch (parse_orig_arg_opt_ret) {
688 case PARSE_ORIG_ARG_OPT_RET_OK:
2af370d0 689 status = ARGPAR_ITER_NEXT_STATUS_OK;
fc07e526
SM
690 break;
691 case PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT:
871eba32 692 case PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG:
871eba32 693 case PARSE_ORIG_ARG_OPT_RET_ERROR_UNEXPECTED_OPT_ARG:
8b95d883
PP
694 if (error) {
695 ARGPAR_ASSERT(*error);
696 (*nc_error)->orig_index = iter->i;
697 }
d4d05805
PP
698
699 switch (parse_orig_arg_opt_ret) {
700 case PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT:
2af370d0 701 status = ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT;
d4d05805
PP
702 break;
703 case PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG:
2af370d0 704 status = ARGPAR_ITER_NEXT_STATUS_ERROR_MISSING_OPT_ARG;
d4d05805 705 break;
d4d05805 706 case PARSE_ORIG_ARG_OPT_RET_ERROR_UNEXPECTED_OPT_ARG:
2af370d0 707 status = ARGPAR_ITER_NEXT_STATUS_ERROR_UNEXPECTED_OPT_ARG;
d4d05805
PP
708 break;
709 default:
710 abort();
711 }
712
713 break;
714 case PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY:
2af370d0 715 status = ARGPAR_ITER_NEXT_STATUS_ERROR_MEMORY;
fc07e526
SM
716 break;
717 default:
718 abort();
719 }
720
721end:
722 return status;
723}
724
725ARGPAR_HIDDEN
f3ab5ca1 726unsigned int argpar_iter_ingested_orig_args(
fc07e526
SM
727 const struct argpar_iter * const iter)
728{
729 return iter->i;
730}
This page took 0.053581 seconds and 4 git commands to generate.