struct argpar_iter: put user data under nested structure
[argpar.git] / argpar / argpar.c
... / ...
CommitLineData
1/*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright (c) 2019-2021 Philippe Proulx <pproulx@efficios.com>
5 * Copyright (c) 2020-2021 Simon Marchi <simon.marchi@efficios.com>
6 */
7
8#include <assert.h>
9#include <stdarg.h>
10#include <stdbool.h>
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14
15#include "argpar.h"
16
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)
24
25#define ARGPAR_ASSERT(_cond) assert(_cond)
26
27#ifdef __MINGW_PRINTF_FORMAT
28# define ARGPAR_PRINTF_FORMAT __MINGW_PRINTF_FORMAT
29#else
30# define ARGPAR_PRINTF_FORMAT printf
31#endif
32
33/*
34 * An argpar iterator.
35 *
36 * Such a structure contains the state of an iterator between calls to
37 * argpar_iter_next().
38 */
39struct argpar_iter {
40 /*
41 * Data provided by the user to argpar_iter_create(); immutable
42 * afterwards.
43 */
44 struct {
45 unsigned int argc;
46 const char * const *argv;
47 const struct argpar_opt_descr *descrs;
48 } user;
49
50 /*
51 * Index of the argument to process in the next
52 * argpar_iter_next() call.
53 */
54 unsigned int i;
55
56 /* Counter of non-option arguments */
57 int non_opt_index;
58
59 /*
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.
64 */
65 const char *short_opt_group_ch;
66
67 /* Temporary character buffer which only grows */
68 struct {
69 size_t size;
70 char *data;
71 } tmp_buf;
72};
73
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
110/* Parsing error */
111struct argpar_error {
112 /* Original argument index */
113 unsigned int orig_index;
114
115 /* Name of unknown option; owned by this */
116 char *unknown_opt_name;
117
118 /* Option descriptor */
119 const struct argpar_opt_descr *opt_descr;
120
121 /* `true` if a short option caused the error */
122 bool is_short;
123};
124
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
175ARGPAR_HIDDEN
176void argpar_item_destroy(const struct argpar_item * const item)
177{
178 if (!item) {
179 goto end;
180 }
181
182 if (item->type == ARGPAR_ITEM_TYPE_OPT) {
183 struct argpar_item_opt * const opt_item =
184 (struct argpar_item_opt *) item;
185
186 free(opt_item->arg);
187 }
188
189 free((void *) item);
190
191end:
192 return;
193}
194
195static
196struct argpar_item_opt *create_opt_item(
197 const struct argpar_opt_descr * const descr,
198 const char * const arg)
199{
200 struct argpar_item_opt *opt_item =
201 ARGPAR_ZALLOC(struct argpar_item_opt);
202
203 if (!opt_item) {
204 goto end;
205 }
206
207 opt_item->base.type = ARGPAR_ITEM_TYPE_OPT;
208 opt_item->descr = descr;
209
210 if (arg) {
211 opt_item->arg = strdup(arg);
212 if (!opt_item->arg) {
213 goto error;
214 }
215 }
216
217 goto end;
218
219error:
220 argpar_item_destroy(&opt_item->base);
221 opt_item = NULL;
222
223end:
224 return opt_item;
225}
226
227static
228struct argpar_item_non_opt *create_non_opt_item(const char * const arg,
229 const unsigned int orig_index,
230 const unsigned int non_opt_index)
231{
232 struct argpar_item_non_opt * const non_opt_item =
233 ARGPAR_ZALLOC(struct argpar_item_non_opt);
234
235 if (!non_opt_item) {
236 goto end;
237 }
238
239 non_opt_item->base.type = ARGPAR_ITEM_TYPE_NON_OPT;
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
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
343static
344const struct argpar_opt_descr *find_descr(
345 const struct argpar_opt_descr * const descrs,
346 const char short_name, const char * const long_name)
347{
348 const struct argpar_opt_descr *descr;
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,
368 PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT = -1,
369 PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG = -2,
370 PARSE_ORIG_ARG_OPT_RET_ERROR_UNEXPECTED_OPT_ARG = -4,
371 PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY = -5,
372};
373
374static
375enum parse_orig_arg_opt_ret parse_short_opt_group(
376 const char * const short_opt_group,
377 const char * const next_orig_arg,
378 const struct argpar_opt_descr * const descrs,
379 struct argpar_iter * const iter,
380 struct argpar_error ** const error,
381 struct argpar_item ** const item)
382{
383 enum parse_orig_arg_opt_ret ret = PARSE_ORIG_ARG_OPT_RET_OK;
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;
388
389 ARGPAR_ASSERT(strlen(short_opt_group) != 0);
390
391 if (!iter->short_opt_group_ch) {
392 iter->short_opt_group_ch = short_opt_group;
393 }
394
395 /* Find corresponding option descriptor */
396 descr = find_descr(descrs, *iter->short_opt_group_ch, NULL);
397 if (!descr) {
398 const char unknown_opt_name[] =
399 {*iter->short_opt_group_ch, '\0'};
400
401 ret = PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT;
402
403 if (set_error(error, unknown_opt_name, NULL, true)) {
404 ret = PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY;
405 }
406
407 goto error;
408 }
409
410 if (descr->with_arg) {
411 if (iter->short_opt_group_ch[1]) {
412 /* `-oarg` form */
413 opt_arg = &iter->short_opt_group_ch[1];
414 } else {
415 /* `-o arg` form */
416 opt_arg = next_orig_arg;
417 used_next_orig_arg = true;
418 }
419
420 /*
421 * We accept `-o ''` (empty option argument), but not
422 * `-o` alone if an option argument is expected.
423 */
424 if (!opt_arg || (iter->short_opt_group_ch[1] &&
425 strlen(opt_arg) == 0)) {
426 ret = PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG;
427
428 if (set_error(error, NULL, descr, true)) {
429 ret = PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY;
430 }
431
432 goto error;
433 }
434 }
435
436 /* Create and append option argument */
437 opt_item = create_opt_item(descr, opt_arg);
438 if (!opt_item) {
439 ret = PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY;
440 goto error;
441 }
442
443 *item = &opt_item->base;
444 iter->short_opt_group_ch++;
445
446 if (descr->with_arg || !*iter->short_opt_group_ch) {
447 /* Option has an argument: no more options */
448 iter->short_opt_group_ch = NULL;
449
450 if (used_next_orig_arg) {
451 iter->i += 2;
452 } else {
453 iter->i++;
454 }
455 }
456
457 goto end;
458
459error:
460 ARGPAR_ASSERT(ret != PARSE_ORIG_ARG_OPT_RET_OK);
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,
469 const struct argpar_opt_descr * const descrs,
470 struct argpar_iter * const iter,
471 struct argpar_error ** const error,
472 struct argpar_item ** const item)
473{
474 enum parse_orig_arg_opt_ret ret = PARSE_ORIG_ARG_OPT_RET_OK;
475 const struct argpar_opt_descr *descr;
476 struct argpar_item_opt *opt_item;
477 bool used_next_orig_arg = false;
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
485 /* Option name */
486 const char *long_opt_name = long_opt_arg;
487
488 ARGPAR_ASSERT(strlen(long_opt_arg) != 0);
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 */
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 }
504 }
505
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;
509 }
510
511 /* Find corresponding option descriptor */
512 descr = find_descr(descrs, '\0', long_opt_name);
513 if (!descr) {
514 ret = PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT;
515
516 if (set_error(error, long_opt_name, NULL, false)) {
517 ret = PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY;
518 }
519
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) {
531 ret = PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG;
532
533 if (set_error(error, NULL, descr, false)) {
534 ret = PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY;
535 }
536
537 goto error;
538 }
539
540 opt_arg = next_orig_arg;
541 used_next_orig_arg = true;
542 }
543 } else if (eq_pos) {
544 /*
545 * Unexpected `--opt=arg` style for a long option which
546 * doesn't accept an argument.
547 */
548 ret = PARSE_ORIG_ARG_OPT_RET_ERROR_UNEXPECTED_OPT_ARG;
549
550 if (set_error(error, NULL, descr, false)) {
551 ret = PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY;
552 }
553
554 goto error;
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
563 if (used_next_orig_arg) {
564 iter->i += 2;
565 } else {
566 iter->i++;
567 }
568
569 *item = &opt_item->base;
570 goto end;
571
572error:
573 ARGPAR_ASSERT(ret != PARSE_ORIG_ARG_OPT_RET_OK);
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,
582 const struct argpar_opt_descr * const descrs,
583 struct argpar_iter * const iter,
584 struct argpar_error ** const error,
585 struct argpar_item ** const item)
586{
587 enum parse_orig_arg_opt_ret ret = PARSE_ORIG_ARG_OPT_RET_OK;
588
589 ARGPAR_ASSERT(orig_arg[0] == '-');
590
591 if (orig_arg[1] == '-') {
592 /* Long option */
593 ret = parse_long_opt(&orig_arg[2],
594 next_orig_arg, descrs, iter, error, item);
595 } else {
596 /* Short option */
597 ret = parse_short_opt_group(&orig_arg[1],
598 next_orig_arg, descrs, iter, error, item);
599 }
600
601 return ret;
602}
603
604ARGPAR_HIDDEN
605struct argpar_iter *argpar_iter_create(const unsigned int argc,
606 const char * const * const argv,
607 const struct argpar_opt_descr * const descrs)
608{
609 struct argpar_iter *iter = ARGPAR_ZALLOC(struct argpar_iter);
610
611 if (!iter) {
612 goto end;
613 }
614
615 iter->user.argc = argc;
616 iter->user.argv = argv;
617 iter->user.descrs = descrs;
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 }
625
626end:
627 return iter;
628}
629
630ARGPAR_HIDDEN
631void argpar_iter_destroy(struct argpar_iter * const iter)
632{
633 if (iter) {
634 free(iter->tmp_buf.data);
635 free(iter);
636 }
637}
638
639ARGPAR_HIDDEN
640enum argpar_iter_next_status argpar_iter_next(
641 struct argpar_iter * const iter,
642 const struct argpar_item ** const item,
643 const struct argpar_error ** const error)
644{
645 enum argpar_iter_next_status status;
646 enum parse_orig_arg_opt_ret parse_orig_arg_opt_ret;
647 const char *orig_arg;
648 const char *next_orig_arg;
649 struct argpar_error ** const nc_error = (struct argpar_error **) error;
650
651 ARGPAR_ASSERT(iter->i <= iter->user.argc);
652
653 if (error) {
654 *nc_error = NULL;
655 }
656
657 if (iter->i == iter->user.argc) {
658 status = ARGPAR_ITER_NEXT_STATUS_END;
659 goto end;
660 }
661
662 orig_arg = iter->user.argv[iter->i];
663 next_orig_arg =
664 iter->i < (iter->user.argc - 1) ?
665 iter->user.argv[iter->i + 1] : NULL;
666
667 if (strcmp(orig_arg, "-") == 0 || strcmp(orig_arg, "--") == 0 ||
668 orig_arg[0] != '-') {
669 /* Non-option argument */
670 const struct argpar_item_non_opt * const non_opt_item =
671 create_non_opt_item(orig_arg, iter->i,
672 iter->non_opt_index);
673
674 if (!non_opt_item) {
675 status = ARGPAR_ITER_NEXT_STATUS_ERROR_MEMORY;
676 goto end;
677 }
678
679 iter->non_opt_index++;
680 iter->i++;
681 *item = &non_opt_item->base;
682 status = ARGPAR_ITER_NEXT_STATUS_OK;
683 goto end;
684 }
685
686 /* Option argument */
687 parse_orig_arg_opt_ret = parse_orig_arg_opt(orig_arg,
688 next_orig_arg, iter->user.descrs, iter, nc_error,
689 (struct argpar_item **) item);
690 switch (parse_orig_arg_opt_ret) {
691 case PARSE_ORIG_ARG_OPT_RET_OK:
692 status = ARGPAR_ITER_NEXT_STATUS_OK;
693 break;
694 case PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT:
695 case PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG:
696 case PARSE_ORIG_ARG_OPT_RET_ERROR_UNEXPECTED_OPT_ARG:
697 if (error) {
698 ARGPAR_ASSERT(*error);
699 (*nc_error)->orig_index = iter->i;
700 }
701
702 switch (parse_orig_arg_opt_ret) {
703 case PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT:
704 status = ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT;
705 break;
706 case PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG:
707 status = ARGPAR_ITER_NEXT_STATUS_ERROR_MISSING_OPT_ARG;
708 break;
709 case PARSE_ORIG_ARG_OPT_RET_ERROR_UNEXPECTED_OPT_ARG:
710 status = ARGPAR_ITER_NEXT_STATUS_ERROR_UNEXPECTED_OPT_ARG;
711 break;
712 default:
713 abort();
714 }
715
716 break;
717 case PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY:
718 status = ARGPAR_ITER_NEXT_STATUS_ERROR_MEMORY;
719 break;
720 default:
721 abort();
722 }
723
724end:
725 return status;
726}
727
728ARGPAR_HIDDEN
729unsigned int argpar_iter_ingested_orig_args(
730 const struct argpar_iter * const iter)
731{
732 return iter->i;
733}
This page took 0.023632 seconds and 4 git commands to generate.