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 | ||
256 | static | |
1c9a6bde SM |
257 | struct argpar_item_opt *create_opt_item( |
258 | const struct argpar_opt_descr * const descr, | |
903a5b8a SM |
259 | const char * const arg) |
260 | { | |
1c9a6bde | 261 | struct argpar_item_opt *opt_item = |
fb12ac67 | 262 | ARGPAR_ZALLOC(struct argpar_item_opt); |
903a5b8a SM |
263 | |
264 | if (!opt_item) { | |
265 | goto end; | |
266 | } | |
267 | ||
1c9a6bde | 268 | opt_item->base.type = ARGPAR_ITEM_TYPE_OPT; |
903a5b8a SM |
269 | opt_item->descr = descr; |
270 | ||
271 | if (arg) { | |
7ac57709 | 272 | opt_item->arg = strdup(arg); |
903a5b8a SM |
273 | if (!opt_item->arg) { |
274 | goto error; | |
275 | } | |
276 | } | |
277 | ||
278 | goto end; | |
279 | ||
280 | error: | |
fc07e526 | 281 | argpar_item_destroy(&opt_item->base); |
903a5b8a SM |
282 | opt_item = NULL; |
283 | ||
284 | end: | |
285 | return opt_item; | |
286 | } | |
287 | ||
288 | static | |
1c9a6bde | 289 | struct argpar_item_non_opt *create_non_opt_item(const char * const arg, |
903a5b8a SM |
290 | const unsigned int orig_index, |
291 | const unsigned int non_opt_index) | |
292 | { | |
1c9a6bde | 293 | struct argpar_item_non_opt * const non_opt_item = |
fb12ac67 | 294 | ARGPAR_ZALLOC(struct argpar_item_non_opt); |
903a5b8a SM |
295 | |
296 | if (!non_opt_item) { | |
297 | goto end; | |
298 | } | |
299 | ||
1c9a6bde | 300 | non_opt_item->base.type = ARGPAR_ITEM_TYPE_NON_OPT; |
903a5b8a SM |
301 | non_opt_item->arg = arg; |
302 | non_opt_item->orig_index = orig_index; | |
303 | non_opt_item->non_opt_index = non_opt_index; | |
304 | ||
305 | end: | |
306 | return non_opt_item; | |
307 | } | |
308 | ||
309 | static | |
1c9a6bde SM |
310 | const struct argpar_opt_descr *find_descr( |
311 | const struct argpar_opt_descr * const descrs, | |
903a5b8a SM |
312 | const char short_name, const char * const long_name) |
313 | { | |
1c9a6bde | 314 | const struct argpar_opt_descr *descr; |
903a5b8a SM |
315 | |
316 | for (descr = descrs; descr->short_name || descr->long_name; descr++) { | |
317 | if (short_name && descr->short_name && | |
318 | short_name == descr->short_name) { | |
319 | goto end; | |
320 | } | |
321 | ||
322 | if (long_name && descr->long_name && | |
323 | strcmp(long_name, descr->long_name) == 0) { | |
324 | goto end; | |
325 | } | |
326 | } | |
327 | ||
328 | end: | |
329 | return !descr->short_name && !descr->long_name ? NULL : descr; | |
330 | } | |
331 | ||
332 | enum parse_orig_arg_opt_ret { | |
333 | PARSE_ORIG_ARG_OPT_RET_OK, | |
871eba32 PP |
334 | PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT = -1, |
335 | PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG = -2, | |
871eba32 PP |
336 | PARSE_ORIG_ARG_OPT_RET_ERROR_UNEXPECTED_OPT_ARG = -4, |
337 | PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY = -5, | |
903a5b8a SM |
338 | }; |
339 | ||
340 | static | |
341 | enum parse_orig_arg_opt_ret parse_short_opts(const char * const short_opts, | |
342 | const char * const next_orig_arg, | |
1c9a6bde | 343 | const struct argpar_opt_descr * const descrs, |
fc07e526 SM |
344 | struct argpar_iter * const iter, |
345 | char ** const error, struct argpar_item ** const item) | |
903a5b8a SM |
346 | { |
347 | enum parse_orig_arg_opt_ret ret = PARSE_ORIG_ARG_OPT_RET_OK; | |
fc07e526 SM |
348 | bool used_next_orig_arg = false; |
349 | const char *opt_arg = NULL; | |
350 | const struct argpar_opt_descr *descr; | |
351 | struct argpar_item_opt *opt_item; | |
903a5b8a | 352 | |
dd757a65 | 353 | ARGPAR_ASSERT(strlen(short_opts) != 0); |
903a5b8a | 354 | |
fc07e526 SM |
355 | if (!iter->short_opt_ch) { |
356 | iter->short_opt_ch = short_opts; | |
357 | } | |
903a5b8a | 358 | |
fc07e526 SM |
359 | /* Find corresponding option descriptor */ |
360 | descr = find_descr(descrs, *iter->short_opt_ch, NULL); | |
361 | if (!descr) { | |
37bb2b1f | 362 | try_append_string_printf(error, "Unknown option `-%c`", |
fb12ac67 | 363 | *iter->short_opt_ch); |
871eba32 | 364 | ret = PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT; |
fc07e526 SM |
365 | goto error; |
366 | } | |
903a5b8a | 367 | |
fc07e526 SM |
368 | if (descr->with_arg) { |
369 | if (iter->short_opt_ch[1]) { | |
370 | /* `-oarg` form */ | |
371 | opt_arg = &iter->short_opt_ch[1]; | |
372 | } else { | |
373 | /* `-o arg` form */ | |
374 | opt_arg = next_orig_arg; | |
375 | used_next_orig_arg = true; | |
903a5b8a SM |
376 | } |
377 | ||
fc07e526 SM |
378 | /* |
379 | * We accept `-o ''` (empty option argument), but not | |
380 | * `-o` alone if an option argument is expected. | |
381 | */ | |
fb12ac67 PP |
382 | if (!opt_arg || (iter->short_opt_ch[1] && |
383 | strlen(opt_arg) == 0)) { | |
37bb2b1f | 384 | try_append_string_printf(error, |
fc07e526 SM |
385 | "Missing required argument for option `-%c`", |
386 | *iter->short_opt_ch); | |
387 | used_next_orig_arg = false; | |
871eba32 | 388 | ret = PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG; |
903a5b8a SM |
389 | goto error; |
390 | } | |
fc07e526 | 391 | } |
903a5b8a | 392 | |
fc07e526 SM |
393 | /* Create and append option argument */ |
394 | opt_item = create_opt_item(descr, opt_arg); | |
395 | if (!opt_item) { | |
871eba32 | 396 | ret = PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY; |
fc07e526 SM |
397 | goto error; |
398 | } | |
903a5b8a | 399 | |
fc07e526 SM |
400 | *item = &opt_item->base; |
401 | iter->short_opt_ch++; | |
903a5b8a | 402 | |
fc07e526 SM |
403 | if (descr->with_arg || !*iter->short_opt_ch) { |
404 | /* Option has an argument: no more options */ | |
405 | iter->short_opt_ch = NULL; | |
406 | ||
407 | if (used_next_orig_arg) { | |
408 | iter->i += 2; | |
409 | } else { | |
410 | iter->i++; | |
411 | } | |
903a5b8a SM |
412 | } |
413 | ||
414 | goto end; | |
415 | ||
416 | error: | |
871eba32 | 417 | ARGPAR_ASSERT(ret != PARSE_ORIG_ARG_OPT_RET_OK); |
903a5b8a SM |
418 | |
419 | end: | |
420 | return ret; | |
421 | } | |
422 | ||
423 | static | |
424 | enum parse_orig_arg_opt_ret parse_long_opt(const char * const long_opt_arg, | |
425 | const char * const next_orig_arg, | |
1c9a6bde | 426 | const struct argpar_opt_descr * const descrs, |
fc07e526 SM |
427 | struct argpar_iter * const iter, |
428 | char ** const error, struct argpar_item ** const item) | |
903a5b8a | 429 | { |
903a5b8a | 430 | enum parse_orig_arg_opt_ret ret = PARSE_ORIG_ARG_OPT_RET_OK; |
1c9a6bde SM |
431 | const struct argpar_opt_descr *descr; |
432 | struct argpar_item_opt *opt_item; | |
fc07e526 | 433 | bool used_next_orig_arg = false; |
903a5b8a SM |
434 | |
435 | /* Option's argument, if any */ | |
436 | const char *opt_arg = NULL; | |
437 | ||
438 | /* Position of first `=`, if any */ | |
439 | const char *eq_pos; | |
440 | ||
903a5b8a SM |
441 | /* Option name */ |
442 | const char *long_opt_name = long_opt_arg; | |
443 | ||
dd757a65 | 444 | ARGPAR_ASSERT(strlen(long_opt_arg) != 0); |
903a5b8a SM |
445 | |
446 | /* Find the first `=` in original argument */ | |
447 | eq_pos = strchr(long_opt_arg, '='); | |
448 | if (eq_pos) { | |
449 | const size_t long_opt_name_size = eq_pos - long_opt_arg; | |
450 | ||
451 | /* Isolate the option name */ | |
d1f7bbdb PP |
452 | while (long_opt_name_size > iter->tmp_buf.size - 1) { |
453 | iter->tmp_buf.size *= 2; | |
454 | iter->tmp_buf.data = ARGPAR_REALLOC(iter->tmp_buf.data, | |
455 | char, iter->tmp_buf.size); | |
456 | if (!iter->tmp_buf.data) { | |
457 | ret = PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY; | |
458 | goto error; | |
459 | } | |
903a5b8a SM |
460 | } |
461 | ||
d1f7bbdb PP |
462 | memcpy(iter->tmp_buf.data, long_opt_arg, long_opt_name_size); |
463 | iter->tmp_buf.data[long_opt_name_size] = '\0'; | |
464 | long_opt_name = iter->tmp_buf.data; | |
903a5b8a SM |
465 | } |
466 | ||
467 | /* Find corresponding option descriptor */ | |
468 | descr = find_descr(descrs, '\0', long_opt_name); | |
469 | if (!descr) { | |
37bb2b1f | 470 | try_append_string_printf(error, "Unknown option `--%s`", |
fb12ac67 | 471 | long_opt_name); |
903a5b8a SM |
472 | ret = PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT; |
473 | goto error; | |
474 | } | |
475 | ||
476 | /* Find option's argument if any */ | |
477 | if (descr->with_arg) { | |
478 | if (eq_pos) { | |
479 | /* `--long-opt=arg` style */ | |
480 | opt_arg = eq_pos + 1; | |
481 | } else { | |
482 | /* `--long-opt arg` style */ | |
483 | if (!next_orig_arg) { | |
37bb2b1f | 484 | try_append_string_printf(error, |
903a5b8a SM |
485 | "Missing required argument for option `--%s`", |
486 | long_opt_name); | |
871eba32 | 487 | ret = PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG; |
903a5b8a SM |
488 | goto error; |
489 | } | |
490 | ||
491 | opt_arg = next_orig_arg; | |
fc07e526 | 492 | used_next_orig_arg = true; |
903a5b8a | 493 | } |
430fe886 SM |
494 | } else if (eq_pos) { |
495 | /* | |
496 | * Unexpected `--opt=arg` style for a long option which | |
497 | * doesn't accept an argument. | |
498 | */ | |
37bb2b1f | 499 | try_append_string_printf(error, |
fc07e526 | 500 | "Unexpected argument for option `--%s`", long_opt_name); |
871eba32 | 501 | ret = PARSE_ORIG_ARG_OPT_RET_ERROR_UNEXPECTED_OPT_ARG; |
430fe886 | 502 | goto error; |
903a5b8a SM |
503 | } |
504 | ||
505 | /* Create and append option argument */ | |
506 | opt_item = create_opt_item(descr, opt_arg); | |
507 | if (!opt_item) { | |
508 | goto error; | |
509 | } | |
510 | ||
fc07e526 SM |
511 | if (used_next_orig_arg) { |
512 | iter->i += 2; | |
513 | } else { | |
514 | iter->i++; | |
7ac57709 SM |
515 | } |
516 | ||
fc07e526 | 517 | *item = &opt_item->base; |
903a5b8a SM |
518 | goto end; |
519 | ||
520 | error: | |
871eba32 | 521 | ARGPAR_ASSERT(ret != PARSE_ORIG_ARG_OPT_RET_OK); |
903a5b8a SM |
522 | |
523 | end: | |
524 | return ret; | |
525 | } | |
526 | ||
527 | static | |
528 | enum parse_orig_arg_opt_ret parse_orig_arg_opt(const char * const orig_arg, | |
529 | const char * const next_orig_arg, | |
1c9a6bde | 530 | const struct argpar_opt_descr * const descrs, |
fb12ac67 | 531 | struct argpar_iter * const iter, char ** const error, |
fc07e526 | 532 | struct argpar_item ** const item) |
903a5b8a SM |
533 | { |
534 | enum parse_orig_arg_opt_ret ret = PARSE_ORIG_ARG_OPT_RET_OK; | |
535 | ||
7ac57709 | 536 | ARGPAR_ASSERT(orig_arg[0] == '-'); |
903a5b8a SM |
537 | |
538 | if (orig_arg[1] == '-') { | |
539 | /* Long option */ | |
540 | ret = parse_long_opt(&orig_arg[2], | |
fc07e526 | 541 | next_orig_arg, descrs, iter, error, item); |
903a5b8a SM |
542 | } else { |
543 | /* Short option */ | |
544 | ret = parse_short_opts(&orig_arg[1], | |
fc07e526 | 545 | next_orig_arg, descrs, iter, error, item); |
903a5b8a SM |
546 | } |
547 | ||
548 | return ret; | |
549 | } | |
550 | ||
551 | static | |
37bb2b1f | 552 | bool try_prepend_while_parsing_arg_to_error(char ** const error, |
903a5b8a SM |
553 | const unsigned int i, const char * const arg) |
554 | { | |
7ac57709 SM |
555 | char *new_error; |
556 | bool success; | |
903a5b8a | 557 | |
37bb2b1f PP |
558 | if (!error) { |
559 | success = true; | |
560 | goto end; | |
561 | } | |
562 | ||
7ac57709 | 563 | ARGPAR_ASSERT(*error); |
7ac57709 SM |
564 | new_error = argpar_asprintf("While parsing argument #%u (`%s`): %s", |
565 | i + 1, arg, *error); | |
566 | if (!new_error) { | |
567 | success = false; | |
903a5b8a SM |
568 | goto end; |
569 | } | |
570 | ||
7ac57709 SM |
571 | free(*error); |
572 | *error = new_error; | |
573 | success = true; | |
903a5b8a SM |
574 | |
575 | end: | |
7ac57709 | 576 | return success; |
903a5b8a SM |
577 | } |
578 | ||
7ac57709 | 579 | ARGPAR_HIDDEN |
fc07e526 SM |
580 | struct argpar_iter *argpar_iter_create(const unsigned int argc, |
581 | const char * const * const argv, | |
582 | const struct argpar_opt_descr * const descrs) | |
903a5b8a | 583 | { |
d1f7bbdb | 584 | struct argpar_iter *iter = ARGPAR_ZALLOC(struct argpar_iter); |
903a5b8a | 585 | |
fc07e526 SM |
586 | if (!iter) { |
587 | goto end; | |
903a5b8a SM |
588 | } |
589 | ||
fc07e526 SM |
590 | iter->argc = argc; |
591 | iter->argv = argv; | |
592 | iter->descrs = descrs; | |
d1f7bbdb PP |
593 | iter->tmp_buf.size = 128; |
594 | iter->tmp_buf.data = ARGPAR_CALLOC(char, iter->tmp_buf.size); | |
595 | if (!iter->tmp_buf.data) { | |
596 | argpar_iter_destroy(iter); | |
597 | iter = NULL; | |
598 | goto end; | |
599 | } | |
903a5b8a | 600 | |
fc07e526 SM |
601 | end: |
602 | return iter; | |
603 | } | |
903a5b8a | 604 | |
fc07e526 SM |
605 | ARGPAR_HIDDEN |
606 | void argpar_iter_destroy(struct argpar_iter * const iter) | |
607 | { | |
d1f7bbdb PP |
608 | if (iter) { |
609 | free(iter->tmp_buf.data); | |
610 | free(iter); | |
611 | } | |
fc07e526 | 612 | } |
903a5b8a | 613 | |
fc07e526 | 614 | ARGPAR_HIDDEN |
2af370d0 | 615 | enum argpar_iter_next_status argpar_iter_next( |
fc07e526 | 616 | struct argpar_iter * const iter, |
fb12ac67 | 617 | const struct argpar_item ** const item, char ** const error) |
fc07e526 | 618 | { |
2af370d0 | 619 | enum argpar_iter_next_status status; |
fc07e526 SM |
620 | enum parse_orig_arg_opt_ret parse_orig_arg_opt_ret; |
621 | const char *orig_arg; | |
622 | const char *next_orig_arg; | |
7ac57709 | 623 | |
fc07e526 | 624 | ARGPAR_ASSERT(iter->i <= iter->argc); |
37bb2b1f PP |
625 | |
626 | if (error) { | |
627 | *error = NULL; | |
628 | } | |
fc07e526 SM |
629 | |
630 | if (iter->i == iter->argc) { | |
2af370d0 | 631 | status = ARGPAR_ITER_NEXT_STATUS_END; |
fc07e526 SM |
632 | goto end; |
633 | } | |
7ac57709 | 634 | |
fc07e526 SM |
635 | orig_arg = iter->argv[iter->i]; |
636 | next_orig_arg = | |
637 | iter->i < (iter->argc - 1) ? iter->argv[iter->i + 1] : NULL; | |
638 | ||
dd757a65 PP |
639 | if (strcmp(orig_arg, "-") == 0 || strcmp(orig_arg, "--") == 0 || |
640 | orig_arg[0] != '-') { | |
fc07e526 | 641 | /* Non-option argument */ |
dd757a65 | 642 | const struct argpar_item_non_opt * const non_opt_item = |
fc07e526 SM |
643 | create_non_opt_item(orig_arg, iter->i, |
644 | iter->non_opt_index); | |
645 | ||
646 | if (!non_opt_item) { | |
2af370d0 | 647 | status = ARGPAR_ITER_NEXT_STATUS_ERROR_MEMORY; |
fc07e526 | 648 | goto end; |
903a5b8a SM |
649 | } |
650 | ||
fc07e526 SM |
651 | iter->non_opt_index++; |
652 | iter->i++; | |
653 | *item = &non_opt_item->base; | |
2af370d0 | 654 | status = ARGPAR_ITER_NEXT_STATUS_OK; |
fc07e526 SM |
655 | goto end; |
656 | } | |
903a5b8a | 657 | |
fc07e526 SM |
658 | /* Option argument */ |
659 | parse_orig_arg_opt_ret = parse_orig_arg_opt(orig_arg, | |
660 | next_orig_arg, iter->descrs, iter, error, | |
661 | (struct argpar_item **) item); | |
662 | switch (parse_orig_arg_opt_ret) { | |
663 | case PARSE_ORIG_ARG_OPT_RET_OK: | |
2af370d0 | 664 | status = ARGPAR_ITER_NEXT_STATUS_OK; |
fc07e526 SM |
665 | break; |
666 | case PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT: | |
871eba32 | 667 | case PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG: |
871eba32 | 668 | case PARSE_ORIG_ARG_OPT_RET_ERROR_UNEXPECTED_OPT_ARG: |
37bb2b1f PP |
669 | try_prepend_while_parsing_arg_to_error(error, iter->i, |
670 | orig_arg); | |
d4d05805 PP |
671 | |
672 | switch (parse_orig_arg_opt_ret) { | |
673 | case PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT: | |
2af370d0 | 674 | status = ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT; |
d4d05805 PP |
675 | break; |
676 | case PARSE_ORIG_ARG_OPT_RET_ERROR_MISSING_OPT_ARG: | |
2af370d0 | 677 | status = ARGPAR_ITER_NEXT_STATUS_ERROR_MISSING_OPT_ARG; |
d4d05805 | 678 | break; |
d4d05805 | 679 | case PARSE_ORIG_ARG_OPT_RET_ERROR_UNEXPECTED_OPT_ARG: |
2af370d0 | 680 | status = ARGPAR_ITER_NEXT_STATUS_ERROR_UNEXPECTED_OPT_ARG; |
d4d05805 PP |
681 | break; |
682 | default: | |
683 | abort(); | |
684 | } | |
685 | ||
686 | break; | |
687 | case PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY: | |
2af370d0 | 688 | status = ARGPAR_ITER_NEXT_STATUS_ERROR_MEMORY; |
fc07e526 SM |
689 | break; |
690 | default: | |
691 | abort(); | |
692 | } | |
693 | ||
694 | end: | |
695 | return status; | |
696 | } | |
697 | ||
698 | ARGPAR_HIDDEN | |
f3ab5ca1 | 699 | unsigned int argpar_iter_ingested_orig_args( |
fc07e526 SM |
700 | const struct argpar_iter * const iter) |
701 | { | |
702 | return iter->i; | |
703 | } |