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