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