79a22f565bf99add9bb16b1eda572cd4b525526b
[argpar.git] / argpar / argpar.c
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 */
39 struct 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
50 * argpar_iter_next() call.
51 */
52 unsigned int i;
53
54 /* Counter of non-option arguments */
55 int non_opt_index;
56
57 /*
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.
62 */
63 const char *short_opt_group_ch;
64
65 /* Temporary character buffer which only grows */
66 struct {
67 size_t size;
68 char *data;
69 } tmp_buf;
70 };
71
72 /* Base parsing item */
73 struct argpar_item {
74 enum argpar_item_type type;
75 };
76
77 /* Option parsing item */
78 struct 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 */
89 struct 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
108 /* Parsing error */
109 struct argpar_error {
110 /* Original argument index */
111 unsigned int orig_index;
112
113 /* Name of unknown option; owned by this */
114 char *unknown_opt_name;
115
116 /* Option descriptor */
117 const struct argpar_opt_descr *opt_descr;
118
119 /* `true` if a short option caused the error */
120 bool is_short;
121 };
122
123 ARGPAR_HIDDEN
124 enum argpar_item_type argpar_item_type(const struct argpar_item * const item)
125 {
126 ARGPAR_ASSERT(item);
127 return item->type;
128 }
129
130 ARGPAR_HIDDEN
131 const 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
139 ARGPAR_HIDDEN
140 const 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
147 ARGPAR_HIDDEN
148 const 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
155 ARGPAR_HIDDEN
156 unsigned 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
164 ARGPAR_HIDDEN
165 unsigned 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
173 ARGPAR_HIDDEN
174 void argpar_item_destroy(const struct argpar_item * const item)
175 {
176 if (!item) {
177 goto end;
178 }
179
180 if (item->type == ARGPAR_ITEM_TYPE_OPT) {
181 struct argpar_item_opt * const opt_item =
182 (struct argpar_item_opt *) item;
183
184 free(opt_item->arg);
185 }
186
187 free((void *) item);
188
189 end:
190 return;
191 }
192
193 static
194 struct argpar_item_opt *create_opt_item(
195 const struct argpar_opt_descr * const descr,
196 const char * const arg)
197 {
198 struct argpar_item_opt *opt_item =
199 ARGPAR_ZALLOC(struct argpar_item_opt);
200
201 if (!opt_item) {
202 goto end;
203 }
204
205 opt_item->base.type = ARGPAR_ITEM_TYPE_OPT;
206 opt_item->descr = descr;
207
208 if (arg) {
209 opt_item->arg = strdup(arg);
210 if (!opt_item->arg) {
211 goto error;
212 }
213 }
214
215 goto end;
216
217 error:
218 argpar_item_destroy(&opt_item->base);
219 opt_item = NULL;
220
221 end:
222 return opt_item;
223 }
224
225 static
226 struct argpar_item_non_opt *create_non_opt_item(const char * const arg,
227 const unsigned int orig_index,
228 const unsigned int non_opt_index)
229 {
230 struct argpar_item_non_opt * const non_opt_item =
231 ARGPAR_ZALLOC(struct argpar_item_non_opt);
232
233 if (!non_opt_item) {
234 goto end;
235 }
236
237 non_opt_item->base.type = ARGPAR_ITEM_TYPE_NON_OPT;
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
242 end:
243 return non_opt_item;
244 }
245
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 */
257 static
258 int 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
294 error:
295 argpar_error_destroy(*error);
296 ret = -1;
297
298 end:
299 return ret;
300 }
301
302 ARGPAR_HIDDEN
303 unsigned int argpar_error_orig_index(const struct argpar_error * const error)
304 {
305 ARGPAR_ASSERT(error);
306 return error->orig_index;
307 }
308
309 ARGPAR_HIDDEN
310 const 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
318 ARGPAR_HIDDEN
319 const 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
332 ARGPAR_HIDDEN
333 void 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
341 static
342 const struct argpar_opt_descr *find_descr(
343 const struct argpar_opt_descr * const descrs,
344 const char short_name, const char * const long_name)
345 {
346 const struct argpar_opt_descr *descr;
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
360 end:
361 return !descr->short_name && !descr->long_name ? NULL : descr;
362 }
363
364 enum parse_orig_arg_opt_ret {
365 PARSE_ORIG_ARG_OPT_RET_OK,
366 PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT = -1,
367 PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG = -2,
368 PARSE_ORIG_ARG_OPT_RET_ERROR_UNEXPECTED_OPT_ARG = -4,
369 PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY = -5,
370 };
371
372 static
373 enum parse_orig_arg_opt_ret parse_short_opt_group(
374 const char * const short_opt_group,
375 const char * const next_orig_arg,
376 const struct argpar_opt_descr * const descrs,
377 struct argpar_iter * const iter,
378 struct argpar_error ** const error,
379 struct argpar_item ** const item)
380 {
381 enum parse_orig_arg_opt_ret ret = PARSE_ORIG_ARG_OPT_RET_OK;
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;
386
387 ARGPAR_ASSERT(strlen(short_opt_group) != 0);
388
389 if (!iter->short_opt_group_ch) {
390 iter->short_opt_group_ch = short_opt_group;
391 }
392
393 /* Find corresponding option descriptor */
394 descr = find_descr(descrs, *iter->short_opt_group_ch, NULL);
395 if (!descr) {
396 const char unknown_opt_name[] =
397 {*iter->short_opt_group_ch, '\0'};
398
399 ret = PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT;
400
401 if (set_error(error, unknown_opt_name, NULL, true)) {
402 ret = PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY;
403 }
404
405 goto error;
406 }
407
408 if (descr->with_arg) {
409 if (iter->short_opt_group_ch[1]) {
410 /* `-oarg` form */
411 opt_arg = &iter->short_opt_group_ch[1];
412 } else {
413 /* `-o arg` form */
414 opt_arg = next_orig_arg;
415 used_next_orig_arg = true;
416 }
417
418 /*
419 * We accept `-o ''` (empty option argument), but not
420 * `-o` alone if an option argument is expected.
421 */
422 if (!opt_arg || (iter->short_opt_group_ch[1] &&
423 strlen(opt_arg) == 0)) {
424 ret = PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG;
425
426 if (set_error(error, NULL, descr, true)) {
427 ret = PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY;
428 }
429
430 goto error;
431 }
432 }
433
434 /* Create and append option argument */
435 opt_item = create_opt_item(descr, opt_arg);
436 if (!opt_item) {
437 ret = PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY;
438 goto error;
439 }
440
441 *item = &opt_item->base;
442 iter->short_opt_group_ch++;
443
444 if (descr->with_arg || !*iter->short_opt_group_ch) {
445 /* Option has an argument: no more options */
446 iter->short_opt_group_ch = NULL;
447
448 if (used_next_orig_arg) {
449 iter->i += 2;
450 } else {
451 iter->i++;
452 }
453 }
454
455 goto end;
456
457 error:
458 ARGPAR_ASSERT(ret != PARSE_ORIG_ARG_OPT_RET_OK);
459
460 end:
461 return ret;
462 }
463
464 static
465 enum parse_orig_arg_opt_ret parse_long_opt(const char * const long_opt_arg,
466 const char * const next_orig_arg,
467 const struct argpar_opt_descr * const descrs,
468 struct argpar_iter * const iter,
469 struct argpar_error ** const error,
470 struct argpar_item ** const item)
471 {
472 enum parse_orig_arg_opt_ret ret = PARSE_ORIG_ARG_OPT_RET_OK;
473 const struct argpar_opt_descr *descr;
474 struct argpar_item_opt *opt_item;
475 bool used_next_orig_arg = false;
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
483 /* Option name */
484 const char *long_opt_name = long_opt_arg;
485
486 ARGPAR_ASSERT(strlen(long_opt_arg) != 0);
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 */
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 }
502 }
503
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;
507 }
508
509 /* Find corresponding option descriptor */
510 descr = find_descr(descrs, '\0', long_opt_name);
511 if (!descr) {
512 ret = PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT;
513
514 if (set_error(error, long_opt_name, NULL, false)) {
515 ret = PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY;
516 }
517
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) {
529 ret = PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG;
530
531 if (set_error(error, NULL, descr, false)) {
532 ret = PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY;
533 }
534
535 goto error;
536 }
537
538 opt_arg = next_orig_arg;
539 used_next_orig_arg = true;
540 }
541 } else if (eq_pos) {
542 /*
543 * Unexpected `--opt=arg` style for a long option which
544 * doesn't accept an argument.
545 */
546 ret = PARSE_ORIG_ARG_OPT_RET_ERROR_UNEXPECTED_OPT_ARG;
547
548 if (set_error(error, NULL, descr, false)) {
549 ret = PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY;
550 }
551
552 goto error;
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
561 if (used_next_orig_arg) {
562 iter->i += 2;
563 } else {
564 iter->i++;
565 }
566
567 *item = &opt_item->base;
568 goto end;
569
570 error:
571 ARGPAR_ASSERT(ret != PARSE_ORIG_ARG_OPT_RET_OK);
572
573 end:
574 return ret;
575 }
576
577 static
578 enum parse_orig_arg_opt_ret parse_orig_arg_opt(const char * const orig_arg,
579 const char * const next_orig_arg,
580 const struct argpar_opt_descr * const descrs,
581 struct argpar_iter * const iter,
582 struct argpar_error ** const error,
583 struct argpar_item ** const item)
584 {
585 enum parse_orig_arg_opt_ret ret = PARSE_ORIG_ARG_OPT_RET_OK;
586
587 ARGPAR_ASSERT(orig_arg[0] == '-');
588
589 if (orig_arg[1] == '-') {
590 /* Long option */
591 ret = parse_long_opt(&orig_arg[2],
592 next_orig_arg, descrs, iter, error, item);
593 } else {
594 /* Short option */
595 ret = parse_short_opt_group(&orig_arg[1],
596 next_orig_arg, descrs, iter, error, item);
597 }
598
599 return ret;
600 }
601
602 ARGPAR_HIDDEN
603 struct argpar_iter *argpar_iter_create(const unsigned int argc,
604 const char * const * const argv,
605 const struct argpar_opt_descr * const descrs)
606 {
607 struct argpar_iter *iter = ARGPAR_ZALLOC(struct argpar_iter);
608
609 if (!iter) {
610 goto end;
611 }
612
613 iter->argc = argc;
614 iter->argv = argv;
615 iter->descrs = descrs;
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 }
623
624 end:
625 return iter;
626 }
627
628 ARGPAR_HIDDEN
629 void argpar_iter_destroy(struct argpar_iter * const iter)
630 {
631 if (iter) {
632 free(iter->tmp_buf.data);
633 free(iter);
634 }
635 }
636
637 ARGPAR_HIDDEN
638 enum argpar_iter_next_status argpar_iter_next(
639 struct argpar_iter * const iter,
640 const struct argpar_item ** const item,
641 const struct argpar_error ** const error)
642 {
643 enum argpar_iter_next_status status;
644 enum parse_orig_arg_opt_ret parse_orig_arg_opt_ret;
645 const char *orig_arg;
646 const char *next_orig_arg;
647 struct argpar_error ** const nc_error = (struct argpar_error **) error;
648
649 ARGPAR_ASSERT(iter->i <= iter->argc);
650
651 if (error) {
652 *nc_error = NULL;
653 }
654
655 if (iter->i == iter->argc) {
656 status = ARGPAR_ITER_NEXT_STATUS_END;
657 goto end;
658 }
659
660 orig_arg = iter->argv[iter->i];
661 next_orig_arg =
662 iter->i < (iter->argc - 1) ? iter->argv[iter->i + 1] : NULL;
663
664 if (strcmp(orig_arg, "-") == 0 || strcmp(orig_arg, "--") == 0 ||
665 orig_arg[0] != '-') {
666 /* Non-option argument */
667 const struct argpar_item_non_opt * const non_opt_item =
668 create_non_opt_item(orig_arg, iter->i,
669 iter->non_opt_index);
670
671 if (!non_opt_item) {
672 status = ARGPAR_ITER_NEXT_STATUS_ERROR_MEMORY;
673 goto end;
674 }
675
676 iter->non_opt_index++;
677 iter->i++;
678 *item = &non_opt_item->base;
679 status = ARGPAR_ITER_NEXT_STATUS_OK;
680 goto end;
681 }
682
683 /* Option argument */
684 parse_orig_arg_opt_ret = parse_orig_arg_opt(orig_arg,
685 next_orig_arg, iter->descrs, iter, nc_error,
686 (struct argpar_item **) item);
687 switch (parse_orig_arg_opt_ret) {
688 case PARSE_ORIG_ARG_OPT_RET_OK:
689 status = ARGPAR_ITER_NEXT_STATUS_OK;
690 break;
691 case PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT:
692 case PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG:
693 case PARSE_ORIG_ARG_OPT_RET_ERROR_UNEXPECTED_OPT_ARG:
694 if (error) {
695 ARGPAR_ASSERT(*error);
696 (*nc_error)->orig_index = iter->i;
697 }
698
699 switch (parse_orig_arg_opt_ret) {
700 case PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT:
701 status = ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT;
702 break;
703 case PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG:
704 status = ARGPAR_ITER_NEXT_STATUS_ERROR_MISSING_OPT_ARG;
705 break;
706 case PARSE_ORIG_ARG_OPT_RET_ERROR_UNEXPECTED_OPT_ARG:
707 status = ARGPAR_ITER_NEXT_STATUS_ERROR_UNEXPECTED_OPT_ARG;
708 break;
709 default:
710 abort();
711 }
712
713 break;
714 case PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY:
715 status = ARGPAR_ITER_NEXT_STATUS_ERROR_MEMORY;
716 break;
717 default:
718 abort();
719 }
720
721 end:
722 return status;
723 }
724
725 ARGPAR_HIDDEN
726 unsigned int argpar_iter_ingested_orig_args(
727 const struct argpar_iter * const iter)
728 {
729 return iter->i;
730 }
This page took 0.04191 seconds and 3 git commands to generate.