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