Commit | Line | Data |
---|---|---|
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 | */ |
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 | |
2af370d0 | 50 | * argpar_iter_next() call. |
fc07e526 SM |
51 | */ |
52 | unsigned int i; | |
53 | ||
54 | /* Counter of non-option arguments */ | |
55 | int non_opt_index; | |
56 | ||
57 | /* | |
58 | * Current character of the current short option group: if it's | |
59 | * not `NULL`, the parser is in within a short option group, | |
60 | * therefore it must resume there in the next | |
2af370d0 | 61 | * argpar_iter_next() call. |
fc07e526 SM |
62 | */ |
63 | const char *short_opt_ch; | |
64 | }; | |
65 | ||
d4539a90 PP |
66 | /* Base parsing item */ |
67 | struct argpar_item { | |
68 | enum argpar_item_type type; | |
69 | }; | |
70 | ||
71 | /* Option parsing item */ | |
72 | struct argpar_item_opt { | |
73 | struct argpar_item base; | |
74 | ||
75 | /* Corresponding descriptor */ | |
76 | const struct argpar_opt_descr *descr; | |
77 | ||
78 | /* Argument, or `NULL` if none; owned by this */ | |
79 | char *arg; | |
80 | }; | |
81 | ||
82 | /* Non-option parsing item */ | |
83 | struct argpar_item_non_opt { | |
84 | struct argpar_item base; | |
85 | ||
86 | /* | |
87 | * Complete argument, pointing to one of the entries of the | |
88 | * original arguments (`argv`). | |
89 | */ | |
90 | const char *arg; | |
91 | ||
92 | /* | |
93 | * Index of this argument amongst all original arguments | |
94 | * (`argv`). | |
95 | */ | |
96 | unsigned int orig_index; | |
97 | ||
98 | /* Index of this argument amongst other non-option arguments */ | |
99 | unsigned int non_opt_index; | |
100 | }; | |
101 | ||
1ae22b5e | 102 | static __attribute__((format(ARGPAR_PRINTF_FORMAT, 1, 0))) |
fb12ac67 | 103 | char *argpar_vasprintf(const char * const fmt, va_list args) |
7ac57709 SM |
104 | { |
105 | int len1, len2; | |
106 | char *str; | |
107 | va_list args2; | |
108 | ||
109 | va_copy(args2, args); | |
7ac57709 SM |
110 | len1 = vsnprintf(NULL, 0, fmt, args); |
111 | if (len1 < 0) { | |
112 | str = NULL; | |
113 | goto end; | |
114 | } | |
115 | ||
116 | str = malloc(len1 + 1); | |
117 | if (!str) { | |
118 | goto end; | |
119 | } | |
120 | ||
121 | len2 = vsnprintf(str, len1 + 1, fmt, args2); | |
7ac57709 SM |
122 | ARGPAR_ASSERT(len1 == len2); |
123 | ||
124 | end: | |
92ecd98e | 125 | va_end(args2); |
7ac57709 SM |
126 | return str; |
127 | } | |
128 | ||
129 | ||
1ae22b5e | 130 | static __attribute__((format(ARGPAR_PRINTF_FORMAT, 1, 2))) |
fb12ac67 | 131 | char *argpar_asprintf(const char * const fmt, ...) |
7ac57709 SM |
132 | { |
133 | va_list args; | |
134 | char *str; | |
f46b5106 | 135 | |
7ac57709 SM |
136 | va_start(args, fmt); |
137 | str = argpar_vasprintf(fmt, args); | |
138 | va_end(args); | |
7ac57709 SM |
139 | return str; |
140 | } | |
141 | ||
1ae22b5e | 142 | static __attribute__((format(ARGPAR_PRINTF_FORMAT, 2, 3))) |
37bb2b1f | 143 | bool try_append_string_printf(char ** const str, const char *fmt, ...) |
7ac57709 SM |
144 | { |
145 | char *new_str = NULL; | |
37bb2b1f | 146 | char *addendum = NULL; |
7ac57709 SM |
147 | bool success; |
148 | va_list args; | |
149 | ||
37bb2b1f PP |
150 | if (!str) { |
151 | success = true; | |
152 | goto end; | |
153 | } | |
154 | ||
7ac57709 | 155 | ARGPAR_ASSERT(str); |
7ac57709 SM |
156 | va_start(args, fmt); |
157 | addendum = argpar_vasprintf(fmt, args); | |
158 | va_end(args); | |
159 | ||
160 | if (!addendum) { | |
161 | success = false; | |
162 | goto end; | |
163 | } | |
164 | ||
165 | new_str = argpar_asprintf("%s%s", *str ? *str : "", addendum); | |
166 | if (!new_str) { | |
167 | success = false; | |
168 | goto end; | |
169 | } | |
f46b5106 | 170 | |
7ac57709 SM |
171 | free(*str); |
172 | *str = new_str; | |
7ac57709 SM |
173 | success = true; |
174 | ||
175 | end: | |
176 | free(addendum); | |
7ac57709 SM |
177 | return success; |
178 | } | |
179 | ||
d4539a90 PP |
180 | ARGPAR_HIDDEN |
181 | enum argpar_item_type argpar_item_type(const struct argpar_item * const item) | |
182 | { | |
183 | ARGPAR_ASSERT(item); | |
184 | return item->type; | |
185 | } | |
186 | ||
187 | ARGPAR_HIDDEN | |
188 | const struct argpar_opt_descr *argpar_item_opt_descr( | |
189 | const struct argpar_item * const item) | |
190 | { | |
191 | ARGPAR_ASSERT(item); | |
192 | ARGPAR_ASSERT(item->type == ARGPAR_ITEM_TYPE_OPT); | |
193 | return ((const struct argpar_item_opt *) item)->descr; | |
194 | } | |
195 | ||
196 | ARGPAR_HIDDEN | |
197 | const char *argpar_item_opt_arg(const struct argpar_item * const item) | |
198 | { | |
199 | ARGPAR_ASSERT(item); | |
200 | ARGPAR_ASSERT(item->type == ARGPAR_ITEM_TYPE_OPT); | |
201 | return ((const struct argpar_item_opt *) item)->arg; | |
202 | } | |
203 | ||
204 | ARGPAR_HIDDEN | |
205 | const char *argpar_item_non_opt_arg(const struct argpar_item * const item) | |
206 | { | |
207 | ARGPAR_ASSERT(item); | |
208 | ARGPAR_ASSERT(item->type == ARGPAR_ITEM_TYPE_NON_OPT); | |
209 | return ((const struct argpar_item_non_opt *) item)->arg; | |
210 | } | |
211 | ||
212 | ARGPAR_HIDDEN | |
213 | unsigned int argpar_item_non_opt_orig_index( | |
214 | const struct argpar_item * const item) | |
215 | { | |
216 | ARGPAR_ASSERT(item); | |
217 | ARGPAR_ASSERT(item->type == ARGPAR_ITEM_TYPE_NON_OPT); | |
218 | return ((const struct argpar_item_non_opt *) item)->orig_index; | |
219 | } | |
220 | ||
221 | ARGPAR_HIDDEN | |
222 | unsigned int argpar_item_non_opt_non_opt_index( | |
223 | const struct argpar_item * const item) | |
224 | { | |
225 | ARGPAR_ASSERT(item); | |
226 | ARGPAR_ASSERT(item->type == ARGPAR_ITEM_TYPE_NON_OPT); | |
227 | return ((const struct argpar_item_non_opt *) item)->non_opt_index; | |
228 | } | |
229 | ||
fc07e526 SM |
230 | ARGPAR_HIDDEN |
231 | void argpar_item_destroy(const struct argpar_item * const item) | |
903a5b8a SM |
232 | { |
233 | if (!item) { | |
234 | goto end; | |
235 | } | |
236 | ||
1c9a6bde | 237 | if (item->type == ARGPAR_ITEM_TYPE_OPT) { |
fc07e526 SM |
238 | struct argpar_item_opt * const opt_item = |
239 | (struct argpar_item_opt *) item; | |
903a5b8a | 240 | |
d4539a90 | 241 | free(opt_item->arg); |
903a5b8a SM |
242 | } |
243 | ||
fc07e526 | 244 | free((void *) item); |
903a5b8a SM |
245 | |
246 | end: | |
247 | return; | |
248 | } | |
249 | ||
7ac57709 | 250 | static |
1c9a6bde | 251 | bool push_item(struct argpar_item_array * const array, |
fb12ac67 | 252 | const struct argpar_item * const item) |
7ac57709 SM |
253 | { |
254 | bool success; | |
255 | ||
256 | ARGPAR_ASSERT(array); | |
257 | ARGPAR_ASSERT(item); | |
258 | ||
259 | if (array->n_items == array->n_alloc) { | |
fb12ac67 PP |
260 | const unsigned int new_n_alloc = array->n_alloc * 2; |
261 | const struct argpar_item ** const new_items = | |
262 | ARGPAR_REALLOC(array->items, const struct argpar_item *, | |
263 | new_n_alloc); | |
7ac57709 SM |
264 | if (!new_items) { |
265 | success = false; | |
266 | goto end; | |
267 | } | |
268 | ||
269 | array->n_alloc = new_n_alloc; | |
270 | array->items = new_items; | |
271 | } | |
272 | ||
273 | array->items[array->n_items] = item; | |
274 | array->n_items++; | |
7ac57709 SM |
275 | success = true; |
276 | ||
277 | end: | |
278 | return success; | |
279 | } | |
280 | ||
281 | static | |
1c9a6bde | 282 | void destroy_item_array(struct argpar_item_array * const array) |
7ac57709 SM |
283 | { |
284 | if (array) { | |
285 | unsigned int i; | |
286 | ||
287 | for (i = 0; i < array->n_items; i++) { | |
fc07e526 | 288 | argpar_item_destroy(array->items[i]); |
7ac57709 SM |
289 | } |
290 | ||
291 | free(array->items); | |
292 | free(array); | |
293 | } | |
294 | } | |
295 | ||
296 | static | |
fb12ac67 | 297 | struct argpar_item_array *create_item_array(void) |
7ac57709 | 298 | { |
1c9a6bde | 299 | struct argpar_item_array *ret; |
7ac57709 SM |
300 | const int initial_size = 10; |
301 | ||
fb12ac67 | 302 | ret = ARGPAR_ZALLOC(struct argpar_item_array); |
7ac57709 SM |
303 | if (!ret) { |
304 | goto end; | |
305 | } | |
306 | ||
fb12ac67 | 307 | ret->items = ARGPAR_CALLOC(const struct argpar_item *, initial_size); |
7ac57709 SM |
308 | if (!ret->items) { |
309 | goto error; | |
310 | } | |
311 | ||
312 | ret->n_alloc = initial_size; | |
7ac57709 SM |
313 | goto end; |
314 | ||
315 | error: | |
316 | destroy_item_array(ret); | |
317 | ret = NULL; | |
318 | ||
319 | end: | |
320 | return ret; | |
321 | } | |
322 | ||
903a5b8a | 323 | static |
1c9a6bde SM |
324 | struct argpar_item_opt *create_opt_item( |
325 | const struct argpar_opt_descr * const descr, | |
903a5b8a SM |
326 | const char * const arg) |
327 | { | |
1c9a6bde | 328 | struct argpar_item_opt *opt_item = |
fb12ac67 | 329 | ARGPAR_ZALLOC(struct argpar_item_opt); |
903a5b8a SM |
330 | |
331 | if (!opt_item) { | |
332 | goto end; | |
333 | } | |
334 | ||
1c9a6bde | 335 | opt_item->base.type = ARGPAR_ITEM_TYPE_OPT; |
903a5b8a SM |
336 | opt_item->descr = descr; |
337 | ||
338 | if (arg) { | |
7ac57709 | 339 | opt_item->arg = strdup(arg); |
903a5b8a SM |
340 | if (!opt_item->arg) { |
341 | goto error; | |
342 | } | |
343 | } | |
344 | ||
345 | goto end; | |
346 | ||
347 | error: | |
fc07e526 | 348 | argpar_item_destroy(&opt_item->base); |
903a5b8a SM |
349 | opt_item = NULL; |
350 | ||
351 | end: | |
352 | return opt_item; | |
353 | } | |
354 | ||
355 | static | |
1c9a6bde | 356 | struct argpar_item_non_opt *create_non_opt_item(const char * const arg, |
903a5b8a SM |
357 | const unsigned int orig_index, |
358 | const unsigned int non_opt_index) | |
359 | { | |
1c9a6bde | 360 | struct argpar_item_non_opt * const non_opt_item = |
fb12ac67 | 361 | ARGPAR_ZALLOC(struct argpar_item_non_opt); |
903a5b8a SM |
362 | |
363 | if (!non_opt_item) { | |
364 | goto end; | |
365 | } | |
366 | ||
1c9a6bde | 367 | non_opt_item->base.type = ARGPAR_ITEM_TYPE_NON_OPT; |
903a5b8a SM |
368 | non_opt_item->arg = arg; |
369 | non_opt_item->orig_index = orig_index; | |
370 | non_opt_item->non_opt_index = non_opt_index; | |
371 | ||
372 | end: | |
373 | return non_opt_item; | |
374 | } | |
375 | ||
376 | static | |
1c9a6bde SM |
377 | const struct argpar_opt_descr *find_descr( |
378 | const struct argpar_opt_descr * const descrs, | |
903a5b8a SM |
379 | const char short_name, const char * const long_name) |
380 | { | |
1c9a6bde | 381 | const struct argpar_opt_descr *descr; |
903a5b8a SM |
382 | |
383 | for (descr = descrs; descr->short_name || descr->long_name; descr++) { | |
384 | if (short_name && descr->short_name && | |
385 | short_name == descr->short_name) { | |
386 | goto end; | |
387 | } | |
388 | ||
389 | if (long_name && descr->long_name && | |
390 | strcmp(long_name, descr->long_name) == 0) { | |
391 | goto end; | |
392 | } | |
393 | } | |
394 | ||
395 | end: | |
396 | return !descr->short_name && !descr->long_name ? NULL : descr; | |
397 | } | |
398 | ||
399 | enum parse_orig_arg_opt_ret { | |
400 | PARSE_ORIG_ARG_OPT_RET_OK, | |
871eba32 PP |
401 | PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT = -1, |
402 | PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG = -2, | |
d4d05805 | 403 | PARSE_ORIG_ARG_OPT_RET_ERROR_INVALID_ARG = -3, |
871eba32 PP |
404 | PARSE_ORIG_ARG_OPT_RET_ERROR_UNEXPECTED_OPT_ARG = -4, |
405 | PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY = -5, | |
903a5b8a SM |
406 | }; |
407 | ||
408 | static | |
409 | enum parse_orig_arg_opt_ret parse_short_opts(const char * const short_opts, | |
410 | const char * const next_orig_arg, | |
1c9a6bde | 411 | const struct argpar_opt_descr * const descrs, |
fc07e526 SM |
412 | struct argpar_iter * const iter, |
413 | char ** const error, struct argpar_item ** const item) | |
903a5b8a SM |
414 | { |
415 | enum parse_orig_arg_opt_ret ret = PARSE_ORIG_ARG_OPT_RET_OK; | |
fc07e526 SM |
416 | bool used_next_orig_arg = false; |
417 | const char *opt_arg = NULL; | |
418 | const struct argpar_opt_descr *descr; | |
419 | struct argpar_item_opt *opt_item; | |
903a5b8a SM |
420 | |
421 | if (strlen(short_opts) == 0) { | |
37bb2b1f | 422 | try_append_string_printf(error, "Invalid argument"); |
d4d05805 | 423 | ret = PARSE_ORIG_ARG_OPT_RET_ERROR_INVALID_ARG; |
903a5b8a SM |
424 | goto error; |
425 | } | |
426 | ||
fc07e526 SM |
427 | if (!iter->short_opt_ch) { |
428 | iter->short_opt_ch = short_opts; | |
429 | } | |
903a5b8a | 430 | |
fc07e526 SM |
431 | /* Find corresponding option descriptor */ |
432 | descr = find_descr(descrs, *iter->short_opt_ch, NULL); | |
433 | if (!descr) { | |
37bb2b1f | 434 | try_append_string_printf(error, "Unknown option `-%c`", |
fb12ac67 | 435 | *iter->short_opt_ch); |
871eba32 | 436 | ret = PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT; |
fc07e526 SM |
437 | goto error; |
438 | } | |
903a5b8a | 439 | |
fc07e526 SM |
440 | if (descr->with_arg) { |
441 | if (iter->short_opt_ch[1]) { | |
442 | /* `-oarg` form */ | |
443 | opt_arg = &iter->short_opt_ch[1]; | |
444 | } else { | |
445 | /* `-o arg` form */ | |
446 | opt_arg = next_orig_arg; | |
447 | used_next_orig_arg = true; | |
903a5b8a SM |
448 | } |
449 | ||
fc07e526 SM |
450 | /* |
451 | * We accept `-o ''` (empty option argument), but not | |
452 | * `-o` alone if an option argument is expected. | |
453 | */ | |
fb12ac67 PP |
454 | if (!opt_arg || (iter->short_opt_ch[1] && |
455 | strlen(opt_arg) == 0)) { | |
37bb2b1f | 456 | try_append_string_printf(error, |
fc07e526 SM |
457 | "Missing required argument for option `-%c`", |
458 | *iter->short_opt_ch); | |
459 | used_next_orig_arg = false; | |
871eba32 | 460 | ret = PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG; |
903a5b8a SM |
461 | goto error; |
462 | } | |
fc07e526 | 463 | } |
903a5b8a | 464 | |
fc07e526 SM |
465 | /* Create and append option argument */ |
466 | opt_item = create_opt_item(descr, opt_arg); | |
467 | if (!opt_item) { | |
871eba32 | 468 | ret = PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY; |
fc07e526 SM |
469 | goto error; |
470 | } | |
903a5b8a | 471 | |
fc07e526 SM |
472 | *item = &opt_item->base; |
473 | iter->short_opt_ch++; | |
903a5b8a | 474 | |
fc07e526 SM |
475 | if (descr->with_arg || !*iter->short_opt_ch) { |
476 | /* Option has an argument: no more options */ | |
477 | iter->short_opt_ch = NULL; | |
478 | ||
479 | if (used_next_orig_arg) { | |
480 | iter->i += 2; | |
481 | } else { | |
482 | iter->i++; | |
483 | } | |
903a5b8a SM |
484 | } |
485 | ||
486 | goto end; | |
487 | ||
488 | error: | |
871eba32 | 489 | ARGPAR_ASSERT(ret != PARSE_ORIG_ARG_OPT_RET_OK); |
903a5b8a SM |
490 | |
491 | end: | |
492 | return ret; | |
493 | } | |
494 | ||
495 | static | |
496 | enum parse_orig_arg_opt_ret parse_long_opt(const char * const long_opt_arg, | |
497 | const char * const next_orig_arg, | |
1c9a6bde | 498 | const struct argpar_opt_descr * const descrs, |
fc07e526 SM |
499 | struct argpar_iter * const iter, |
500 | char ** const error, struct argpar_item ** const item) | |
903a5b8a SM |
501 | { |
502 | const size_t max_len = 127; | |
503 | enum parse_orig_arg_opt_ret ret = PARSE_ORIG_ARG_OPT_RET_OK; | |
1c9a6bde SM |
504 | const struct argpar_opt_descr *descr; |
505 | struct argpar_item_opt *opt_item; | |
fc07e526 | 506 | bool used_next_orig_arg = false; |
903a5b8a SM |
507 | |
508 | /* Option's argument, if any */ | |
509 | const char *opt_arg = NULL; | |
510 | ||
511 | /* Position of first `=`, if any */ | |
512 | const char *eq_pos; | |
513 | ||
514 | /* Buffer holding option name when `long_opt_arg` contains `=` */ | |
515 | char buf[max_len + 1]; | |
516 | ||
517 | /* Option name */ | |
518 | const char *long_opt_name = long_opt_arg; | |
519 | ||
520 | if (strlen(long_opt_arg) == 0) { | |
37bb2b1f | 521 | try_append_string_printf(error, "Invalid argument"); |
d4d05805 | 522 | ret = PARSE_ORIG_ARG_OPT_RET_ERROR_INVALID_ARG; |
903a5b8a SM |
523 | goto error; |
524 | } | |
525 | ||
526 | /* Find the first `=` in original argument */ | |
527 | eq_pos = strchr(long_opt_arg, '='); | |
528 | if (eq_pos) { | |
529 | const size_t long_opt_name_size = eq_pos - long_opt_arg; | |
530 | ||
531 | /* Isolate the option name */ | |
532 | if (long_opt_name_size > max_len) { | |
37bb2b1f PP |
533 | try_append_string_printf(error, |
534 | "Invalid argument `--%s`", long_opt_arg); | |
d4d05805 | 535 | ret = PARSE_ORIG_ARG_OPT_RET_ERROR_INVALID_ARG; |
903a5b8a SM |
536 | goto error; |
537 | } | |
538 | ||
539 | memcpy(buf, long_opt_arg, long_opt_name_size); | |
540 | buf[long_opt_name_size] = '\0'; | |
541 | long_opt_name = buf; | |
542 | } | |
543 | ||
544 | /* Find corresponding option descriptor */ | |
545 | descr = find_descr(descrs, '\0', long_opt_name); | |
546 | if (!descr) { | |
37bb2b1f | 547 | try_append_string_printf(error, "Unknown option `--%s`", |
fb12ac67 | 548 | long_opt_name); |
903a5b8a SM |
549 | ret = PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT; |
550 | goto error; | |
551 | } | |
552 | ||
553 | /* Find option's argument if any */ | |
554 | if (descr->with_arg) { | |
555 | if (eq_pos) { | |
556 | /* `--long-opt=arg` style */ | |
557 | opt_arg = eq_pos + 1; | |
558 | } else { | |
559 | /* `--long-opt arg` style */ | |
560 | if (!next_orig_arg) { | |
37bb2b1f | 561 | try_append_string_printf(error, |
903a5b8a SM |
562 | "Missing required argument for option `--%s`", |
563 | long_opt_name); | |
871eba32 | 564 | ret = PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG; |
903a5b8a SM |
565 | goto error; |
566 | } | |
567 | ||
568 | opt_arg = next_orig_arg; | |
fc07e526 | 569 | used_next_orig_arg = true; |
903a5b8a | 570 | } |
430fe886 SM |
571 | } else if (eq_pos) { |
572 | /* | |
573 | * Unexpected `--opt=arg` style for a long option which | |
574 | * doesn't accept an argument. | |
575 | */ | |
37bb2b1f | 576 | try_append_string_printf(error, |
fc07e526 | 577 | "Unexpected argument for option `--%s`", long_opt_name); |
871eba32 | 578 | ret = PARSE_ORIG_ARG_OPT_RET_ERROR_UNEXPECTED_OPT_ARG; |
430fe886 | 579 | goto error; |
903a5b8a SM |
580 | } |
581 | ||
582 | /* Create and append option argument */ | |
583 | opt_item = create_opt_item(descr, opt_arg); | |
584 | if (!opt_item) { | |
585 | goto error; | |
586 | } | |
587 | ||
fc07e526 SM |
588 | if (used_next_orig_arg) { |
589 | iter->i += 2; | |
590 | } else { | |
591 | iter->i++; | |
7ac57709 SM |
592 | } |
593 | ||
fc07e526 | 594 | *item = &opt_item->base; |
903a5b8a SM |
595 | goto end; |
596 | ||
597 | error: | |
871eba32 | 598 | ARGPAR_ASSERT(ret != PARSE_ORIG_ARG_OPT_RET_OK); |
903a5b8a SM |
599 | |
600 | end: | |
601 | return ret; | |
602 | } | |
603 | ||
604 | static | |
605 | enum parse_orig_arg_opt_ret parse_orig_arg_opt(const char * const orig_arg, | |
606 | const char * const next_orig_arg, | |
1c9a6bde | 607 | const struct argpar_opt_descr * const descrs, |
fb12ac67 | 608 | struct argpar_iter * const iter, char ** const error, |
fc07e526 | 609 | struct argpar_item ** const item) |
903a5b8a SM |
610 | { |
611 | enum parse_orig_arg_opt_ret ret = PARSE_ORIG_ARG_OPT_RET_OK; | |
612 | ||
7ac57709 | 613 | ARGPAR_ASSERT(orig_arg[0] == '-'); |
903a5b8a SM |
614 | |
615 | if (orig_arg[1] == '-') { | |
616 | /* Long option */ | |
617 | ret = parse_long_opt(&orig_arg[2], | |
fc07e526 | 618 | next_orig_arg, descrs, iter, error, item); |
903a5b8a SM |
619 | } else { |
620 | /* Short option */ | |
621 | ret = parse_short_opts(&orig_arg[1], | |
fc07e526 | 622 | next_orig_arg, descrs, iter, error, item); |
903a5b8a SM |
623 | } |
624 | ||
625 | return ret; | |
626 | } | |
627 | ||
628 | static | |
37bb2b1f | 629 | bool try_prepend_while_parsing_arg_to_error(char ** const error, |
903a5b8a SM |
630 | const unsigned int i, const char * const arg) |
631 | { | |
7ac57709 SM |
632 | char *new_error; |
633 | bool success; | |
903a5b8a | 634 | |
37bb2b1f PP |
635 | if (!error) { |
636 | success = true; | |
637 | goto end; | |
638 | } | |
639 | ||
7ac57709 | 640 | ARGPAR_ASSERT(*error); |
7ac57709 SM |
641 | new_error = argpar_asprintf("While parsing argument #%u (`%s`): %s", |
642 | i + 1, arg, *error); | |
643 | if (!new_error) { | |
644 | success = false; | |
903a5b8a SM |
645 | goto end; |
646 | } | |
647 | ||
7ac57709 SM |
648 | free(*error); |
649 | *error = new_error; | |
650 | success = true; | |
903a5b8a SM |
651 | |
652 | end: | |
7ac57709 | 653 | return success; |
903a5b8a SM |
654 | } |
655 | ||
7ac57709 | 656 | ARGPAR_HIDDEN |
fc07e526 SM |
657 | struct argpar_iter *argpar_iter_create(const unsigned int argc, |
658 | const char * const * const argv, | |
659 | const struct argpar_opt_descr * const descrs) | |
903a5b8a | 660 | { |
fb12ac67 | 661 | struct argpar_iter * const iter = ARGPAR_ZALLOC(struct argpar_iter); |
903a5b8a | 662 | |
fc07e526 SM |
663 | if (!iter) { |
664 | goto end; | |
903a5b8a SM |
665 | } |
666 | ||
fc07e526 SM |
667 | iter->argc = argc; |
668 | iter->argv = argv; | |
669 | iter->descrs = descrs; | |
903a5b8a | 670 | |
fc07e526 SM |
671 | end: |
672 | return iter; | |
673 | } | |
903a5b8a | 674 | |
fc07e526 SM |
675 | ARGPAR_HIDDEN |
676 | void argpar_iter_destroy(struct argpar_iter * const iter) | |
677 | { | |
678 | free(iter); | |
679 | } | |
903a5b8a | 680 | |
fc07e526 | 681 | ARGPAR_HIDDEN |
2af370d0 | 682 | enum argpar_iter_next_status argpar_iter_next( |
fc07e526 | 683 | struct argpar_iter * const iter, |
fb12ac67 | 684 | const struct argpar_item ** const item, char ** const error) |
fc07e526 | 685 | { |
2af370d0 | 686 | enum argpar_iter_next_status status; |
fc07e526 SM |
687 | enum parse_orig_arg_opt_ret parse_orig_arg_opt_ret; |
688 | const char *orig_arg; | |
689 | const char *next_orig_arg; | |
7ac57709 | 690 | |
fc07e526 | 691 | ARGPAR_ASSERT(iter->i <= iter->argc); |
37bb2b1f PP |
692 | |
693 | if (error) { | |
694 | *error = NULL; | |
695 | } | |
fc07e526 SM |
696 | |
697 | if (iter->i == iter->argc) { | |
2af370d0 | 698 | status = ARGPAR_ITER_NEXT_STATUS_END; |
fc07e526 SM |
699 | goto end; |
700 | } | |
7ac57709 | 701 | |
fc07e526 SM |
702 | orig_arg = iter->argv[iter->i]; |
703 | next_orig_arg = | |
704 | iter->i < (iter->argc - 1) ? iter->argv[iter->i + 1] : NULL; | |
705 | ||
706 | if (orig_arg[0] != '-') { | |
707 | /* Non-option argument */ | |
708 | struct argpar_item_non_opt * const non_opt_item = | |
709 | create_non_opt_item(orig_arg, iter->i, | |
710 | iter->non_opt_index); | |
711 | ||
712 | if (!non_opt_item) { | |
2af370d0 | 713 | status = ARGPAR_ITER_NEXT_STATUS_ERROR_MEMORY; |
fc07e526 | 714 | goto end; |
903a5b8a SM |
715 | } |
716 | ||
fc07e526 SM |
717 | iter->non_opt_index++; |
718 | iter->i++; | |
719 | *item = &non_opt_item->base; | |
2af370d0 | 720 | status = ARGPAR_ITER_NEXT_STATUS_OK; |
fc07e526 SM |
721 | goto end; |
722 | } | |
903a5b8a | 723 | |
fc07e526 SM |
724 | /* Option argument */ |
725 | parse_orig_arg_opt_ret = parse_orig_arg_opt(orig_arg, | |
726 | next_orig_arg, iter->descrs, iter, error, | |
727 | (struct argpar_item **) item); | |
728 | switch (parse_orig_arg_opt_ret) { | |
729 | case PARSE_ORIG_ARG_OPT_RET_OK: | |
2af370d0 | 730 | status = ARGPAR_ITER_NEXT_STATUS_OK; |
fc07e526 SM |
731 | break; |
732 | case PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT: | |
871eba32 | 733 | case PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG: |
d4d05805 | 734 | case PARSE_ORIG_ARG_OPT_RET_ERROR_INVALID_ARG: |
871eba32 | 735 | case PARSE_ORIG_ARG_OPT_RET_ERROR_UNEXPECTED_OPT_ARG: |
37bb2b1f PP |
736 | try_prepend_while_parsing_arg_to_error(error, iter->i, |
737 | orig_arg); | |
d4d05805 PP |
738 | |
739 | switch (parse_orig_arg_opt_ret) { | |
740 | case PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT: | |
2af370d0 | 741 | status = ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT; |
d4d05805 PP |
742 | break; |
743 | case PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG: | |
2af370d0 | 744 | status = ARGPAR_ITER_NEXT_STATUS_ERROR_MISSING_OPT_ARG; |
d4d05805 PP |
745 | break; |
746 | case PARSE_ORIG_ARG_OPT_RET_ERROR_INVALID_ARG: | |
2af370d0 | 747 | status = ARGPAR_ITER_NEXT_STATUS_ERROR_INVALID_ARG; |
d4d05805 PP |
748 | break; |
749 | case PARSE_ORIG_ARG_OPT_RET_ERROR_UNEXPECTED_OPT_ARG: | |
2af370d0 | 750 | status = ARGPAR_ITER_NEXT_STATUS_ERROR_UNEXPECTED_OPT_ARG; |
d4d05805 PP |
751 | break; |
752 | default: | |
753 | abort(); | |
754 | } | |
755 | ||
756 | break; | |
757 | case PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY: | |
2af370d0 | 758 | status = ARGPAR_ITER_NEXT_STATUS_ERROR_MEMORY; |
fc07e526 SM |
759 | break; |
760 | default: | |
761 | abort(); | |
762 | } | |
763 | ||
764 | end: | |
765 | return status; | |
766 | } | |
767 | ||
768 | ARGPAR_HIDDEN | |
769 | unsigned int argpar_iter_get_ingested_orig_args( | |
770 | const struct argpar_iter * const iter) | |
771 | { | |
772 | return iter->i; | |
773 | } | |
774 | ||
775 | ARGPAR_HIDDEN | |
776 | struct argpar_parse_ret argpar_parse(const unsigned int argc, | |
777 | const char * const * const argv, | |
778 | const struct argpar_opt_descr * const descrs, | |
779 | const bool fail_on_unknown_opt) | |
780 | { | |
781 | struct argpar_parse_ret parse_ret = { 0 }; | |
782 | const struct argpar_item *item = NULL; | |
783 | struct argpar_iter *iter = NULL; | |
784 | ||
fb12ac67 | 785 | parse_ret.items = create_item_array(); |
fc07e526 SM |
786 | if (!parse_ret.items) { |
787 | parse_ret.error = strdup("Failed to create items array."); | |
788 | ARGPAR_ASSERT(parse_ret.error); | |
789 | goto error; | |
790 | } | |
791 | ||
792 | iter = argpar_iter_create(argc, argv, descrs); | |
793 | if (!iter) { | |
794 | parse_ret.error = strdup("Failed to create argpar iter."); | |
795 | ARGPAR_ASSERT(parse_ret.error); | |
796 | goto error; | |
797 | } | |
798 | ||
799 | while (true) { | |
2af370d0 PP |
800 | const enum argpar_iter_next_status status = |
801 | argpar_iter_next(iter, &item, &parse_ret.error); | |
fc07e526 | 802 | |
d4d05805 | 803 | switch (status) { |
2af370d0 PP |
804 | case ARGPAR_ITER_NEXT_STATUS_ERROR_MISSING_OPT_ARG: |
805 | case ARGPAR_ITER_NEXT_STATUS_ERROR_INVALID_ARG: | |
806 | case ARGPAR_ITER_NEXT_STATUS_ERROR_UNEXPECTED_OPT_ARG: | |
807 | case ARGPAR_ITER_NEXT_STATUS_ERROR_MEMORY: | |
fc07e526 | 808 | goto error; |
2af370d0 | 809 | case ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT: |
903a5b8a | 810 | if (fail_on_unknown_opt) { |
fc07e526 SM |
811 | parse_ret.ingested_orig_args = |
812 | argpar_iter_get_ingested_orig_args(iter); | |
903a5b8a SM |
813 | goto error; |
814 | } | |
815 | ||
7ac57709 | 816 | free(parse_ret.error); |
903a5b8a | 817 | parse_ret.error = NULL; |
d4d05805 | 818 | goto success; |
2af370d0 | 819 | case ARGPAR_ITER_NEXT_STATUS_END: |
d4d05805 PP |
820 | goto success; |
821 | default: | |
2af370d0 | 822 | ARGPAR_ASSERT(status == ARGPAR_ITER_NEXT_STATUS_OK); |
fc07e526 | 823 | break; |
903a5b8a SM |
824 | } |
825 | ||
fb12ac67 | 826 | if (!push_item(parse_ret.items, item)) { |
fc07e526 | 827 | goto error; |
903a5b8a | 828 | } |
fc07e526 SM |
829 | |
830 | item = NULL; | |
903a5b8a SM |
831 | } |
832 | ||
d4d05805 | 833 | success: |
fc07e526 | 834 | ARGPAR_ASSERT(!parse_ret.error); |
fb12ac67 | 835 | parse_ret.ingested_orig_args = argpar_iter_get_ingested_orig_args(iter); |
903a5b8a SM |
836 | goto end; |
837 | ||
838 | error: | |
fc07e526 SM |
839 | ARGPAR_ASSERT(parse_ret.error); |
840 | ||
841 | /* That's how we indicate that an error occurred */ | |
7ac57709 SM |
842 | destroy_item_array(parse_ret.items); |
843 | parse_ret.items = NULL; | |
903a5b8a SM |
844 | |
845 | end: | |
fc07e526 SM |
846 | argpar_iter_destroy(iter); |
847 | argpar_item_destroy(item); | |
903a5b8a SM |
848 | return parse_ret; |
849 | } | |
850 | ||
7ac57709 | 851 | ARGPAR_HIDDEN |
fb12ac67 | 852 | void argpar_parse_ret_fini(struct argpar_parse_ret * const ret) |
903a5b8a | 853 | { |
7ac57709 | 854 | ARGPAR_ASSERT(ret); |
7ac57709 SM |
855 | destroy_item_array(ret->items); |
856 | ret->items = NULL; | |
7ac57709 SM |
857 | free(ret->error); |
858 | ret->error = NULL; | |
903a5b8a | 859 | } |