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