Commit | Line | Data |
---|---|---|
903a5b8a | 1 | /* |
03e1579f | 2 | * SPDX-License-Identifier: MIT |
9cc0acc6 PP |
3 | * SPDX-FileCopyrightText: 2019-2024 Philippe Proulx <pproulx@efficios.com> |
4 | * SPDX-FileCopyrightText: 2020-2024 Simon Marchi <simon.marchi@efficios.com> | |
903a5b8a SM |
5 | */ |
6 | ||
7 | #include <stdbool.h> | |
7ac57709 | 8 | #include <stdio.h> |
903a5b8a SM |
9 | #include <stdlib.h> |
10 | #include <string.h> | |
903a5b8a SM |
11 | |
12 | #include "argpar.h" | |
13 | ||
a7e0140a SM |
14 | /* |
15 | * If argpar is used in some shared library, we don't want said library | |
16 | * to export its symbols, so mark them as "hidden". | |
17 | * | |
18 | * On Windows, symbols are local unless explicitly exported; see | |
19 | * <https://gcc.gnu.org/wiki/Visibility>. | |
20 | */ | |
21 | #if defined(_WIN32) || defined(__CYGWIN__) | |
45ad74d3 | 22 | # define ARGPAR_HIDDEN |
a7e0140a | 23 | #else |
45ad74d3 | 24 | # define ARGPAR_HIDDEN __attribute__((visibility("hidden"))) |
a7e0140a SM |
25 | #endif |
26 | ||
45ad74d3 | 27 | #define ARGPAR_REALLOC(_ptr, _type, _nmemb) ((_type *) realloc(_ptr, (_nmemb) * sizeof(_type))) |
fb12ac67 | 28 | |
45ad74d3 | 29 | #define ARGPAR_CALLOC(_type, _nmemb) ((_type *) calloc((_nmemb), sizeof(_type))) |
fb12ac67 PP |
30 | |
31 | #define ARGPAR_ZALLOC(_type) ARGPAR_CALLOC(_type, 1) | |
7ac57709 | 32 | |
143cec42 FD |
33 | #ifdef NDEBUG |
34 | /* | |
70e08ebf PP |
35 | * Force usage of the assertion condition to prevent unused variable |
36 | * warnings when `assert()` are disabled by the `NDEBUG` definition. | |
143cec42 | 37 | */ |
45ad74d3 | 38 | # define ARGPAR_ASSERT(_cond) ((void) sizeof((void) (_cond), 0)) |
143cec42 | 39 | #else |
45ad74d3 SM |
40 | # include <assert.h> |
41 | # define ARGPAR_ASSERT(_cond) assert(_cond) | |
143cec42 | 42 | #endif |
7ac57709 | 43 | |
fc07e526 SM |
44 | /* |
45 | * An argpar iterator. | |
46 | * | |
2af370d0 PP |
47 | * Such a structure contains the state of an iterator between calls to |
48 | * argpar_iter_next(). | |
fc07e526 | 49 | */ |
45ad74d3 SM |
50 | struct argpar_iter |
51 | { | |
52 | /* | |
53 | * Data provided by the user to argpar_iter_create(); immutable | |
54 | * afterwards. | |
55 | */ | |
56 | struct | |
57 | { | |
58 | unsigned int argc; | |
59 | const char * const *argv; | |
60 | const argpar_opt_descr_t *descrs; | |
61 | } user; | |
62 | ||
63 | /* | |
64 | * Index of the argument to process in the next | |
65 | * argpar_iter_next() call. | |
66 | */ | |
67 | unsigned int i; | |
68 | ||
69 | /* Counter of non-option arguments */ | |
70 | int non_opt_index; | |
71 | ||
72 | /* | |
73 | * Current character within the current short option group: if | |
74 | * it's not `NULL`, the parser is within a short option group, | |
75 | * therefore it must resume there in the next argpar_iter_next() | |
76 | * call. | |
77 | */ | |
78 | const char *short_opt_group_ch; | |
79 | ||
80 | /* Temporary character buffer which only grows */ | |
81 | struct | |
82 | { | |
83 | size_t size; | |
84 | char *data; | |
85 | } tmp_buf; | |
fc07e526 SM |
86 | }; |
87 | ||
d4539a90 | 88 | /* Base parsing item */ |
45ad74d3 SM |
89 | struct argpar_item |
90 | { | |
91 | argpar_item_type_t type; | |
d4539a90 PP |
92 | }; |
93 | ||
94 | /* Option parsing item */ | |
45ad74d3 SM |
95 | typedef struct argpar_item_opt |
96 | { | |
97 | argpar_item_t base; | |
d4539a90 | 98 | |
45ad74d3 SM |
99 | /* Corresponding descriptor */ |
100 | const argpar_opt_descr_t *descr; | |
d4539a90 | 101 | |
45ad74d3 SM |
102 | /* Argument, or `NULL` if none; owned by this */ |
103 | char *arg; | |
7554f0b6 | 104 | } argpar_item_opt_t; |
d4539a90 PP |
105 | |
106 | /* Non-option parsing item */ | |
45ad74d3 SM |
107 | typedef struct argpar_item_non_opt |
108 | { | |
109 | argpar_item_t base; | |
110 | ||
111 | /* | |
112 | * Complete argument, pointing to one of the entries of the | |
113 | * original arguments (`argv`). | |
114 | */ | |
115 | const char *arg; | |
116 | ||
117 | /* | |
118 | * Index of this argument amongst all original arguments | |
119 | * (`argv`). | |
120 | */ | |
121 | unsigned int orig_index; | |
122 | ||
123 | /* Index of this argument amongst other non-option arguments */ | |
124 | unsigned int non_opt_index; | |
7554f0b6 | 125 | } argpar_item_non_opt_t; |
d4539a90 | 126 | |
8b95d883 | 127 | /* Parsing error */ |
45ad74d3 SM |
128 | struct argpar_error |
129 | { | |
130 | /* Error type */ | |
131 | argpar_error_type_t type; | |
10aefab2 | 132 | |
45ad74d3 SM |
133 | /* Original argument index */ |
134 | unsigned int orig_index; | |
7ac57709 | 135 | |
45ad74d3 SM |
136 | /* Name of unknown option; owned by this */ |
137 | char *unknown_opt_name; | |
f46b5106 | 138 | |
45ad74d3 SM |
139 | /* Option descriptor */ |
140 | const argpar_opt_descr_t *opt_descr; | |
7ac57709 | 141 | |
45ad74d3 SM |
142 | /* `true` if a short option caused the error */ |
143 | bool is_short; | |
8b95d883 | 144 | }; |
7ac57709 | 145 | |
45ad74d3 | 146 | ARGPAR_HIDDEN argpar_item_type_t argpar_item_type(const argpar_item_t * const item) |
d4539a90 | 147 | { |
45ad74d3 SM |
148 | ARGPAR_ASSERT(item); |
149 | return item->type; | |
d4539a90 PP |
150 | } |
151 | ||
45ad74d3 | 152 | ARGPAR_HIDDEN const argpar_opt_descr_t *argpar_item_opt_descr(const argpar_item_t * const item) |
d4539a90 | 153 | { |
45ad74d3 SM |
154 | ARGPAR_ASSERT(item); |
155 | ARGPAR_ASSERT(item->type == ARGPAR_ITEM_TYPE_OPT); | |
156 | return ((const argpar_item_opt_t *) item)->descr; | |
d4539a90 PP |
157 | } |
158 | ||
45ad74d3 | 159 | ARGPAR_HIDDEN const char *argpar_item_opt_arg(const argpar_item_t * const item) |
d4539a90 | 160 | { |
45ad74d3 SM |
161 | ARGPAR_ASSERT(item); |
162 | ARGPAR_ASSERT(item->type == ARGPAR_ITEM_TYPE_OPT); | |
163 | return ((const argpar_item_opt_t *) item)->arg; | |
d4539a90 PP |
164 | } |
165 | ||
45ad74d3 | 166 | ARGPAR_HIDDEN const char *argpar_item_non_opt_arg(const argpar_item_t * const item) |
d4539a90 | 167 | { |
45ad74d3 SM |
168 | ARGPAR_ASSERT(item); |
169 | ARGPAR_ASSERT(item->type == ARGPAR_ITEM_TYPE_NON_OPT); | |
170 | return ((const argpar_item_non_opt_t *) item)->arg; | |
d4539a90 PP |
171 | } |
172 | ||
45ad74d3 | 173 | ARGPAR_HIDDEN unsigned int argpar_item_non_opt_orig_index(const argpar_item_t * const item) |
d4539a90 | 174 | { |
45ad74d3 SM |
175 | ARGPAR_ASSERT(item); |
176 | ARGPAR_ASSERT(item->type == ARGPAR_ITEM_TYPE_NON_OPT); | |
177 | return ((const argpar_item_non_opt_t *) item)->orig_index; | |
d4539a90 PP |
178 | } |
179 | ||
45ad74d3 | 180 | ARGPAR_HIDDEN unsigned int argpar_item_non_opt_non_opt_index(const argpar_item_t * const item) |
d4539a90 | 181 | { |
45ad74d3 SM |
182 | ARGPAR_ASSERT(item); |
183 | ARGPAR_ASSERT(item->type == ARGPAR_ITEM_TYPE_NON_OPT); | |
184 | return ((const argpar_item_non_opt_t *) item)->non_opt_index; | |
d4539a90 PP |
185 | } |
186 | ||
45ad74d3 | 187 | ARGPAR_HIDDEN void argpar_item_destroy(const argpar_item_t * const item) |
903a5b8a | 188 | { |
45ad74d3 SM |
189 | if (!item) { |
190 | goto end; | |
191 | } | |
903a5b8a | 192 | |
45ad74d3 SM |
193 | if (item->type == ARGPAR_ITEM_TYPE_OPT) { |
194 | argpar_item_opt_t * const opt_item = (argpar_item_opt_t *) item; | |
903a5b8a | 195 | |
45ad74d3 SM |
196 | free(opt_item->arg); |
197 | } | |
903a5b8a | 198 | |
45ad74d3 | 199 | free((void *) item); |
903a5b8a SM |
200 | |
201 | end: | |
45ad74d3 | 202 | return; |
903a5b8a SM |
203 | } |
204 | ||
793f1620 PP |
205 | /* |
206 | * Creates and returns an option parsing item for the descriptor `descr` | |
207 | * and having the argument `arg` (copied; may be `NULL`). | |
208 | * | |
209 | * Returns `NULL` on memory error. | |
210 | */ | |
45ad74d3 SM |
211 | static argpar_item_opt_t *create_opt_item(const argpar_opt_descr_t * const descr, |
212 | const char * const arg) | |
903a5b8a | 213 | { |
45ad74d3 | 214 | argpar_item_opt_t *opt_item = ARGPAR_ZALLOC(argpar_item_opt_t); |
903a5b8a | 215 | |
45ad74d3 SM |
216 | if (!opt_item) { |
217 | goto end; | |
218 | } | |
903a5b8a | 219 | |
45ad74d3 SM |
220 | opt_item->base.type = ARGPAR_ITEM_TYPE_OPT; |
221 | opt_item->descr = descr; | |
903a5b8a | 222 | |
45ad74d3 SM |
223 | if (arg) { |
224 | opt_item->arg = strdup(arg); | |
225 | if (!opt_item->arg) { | |
226 | goto error; | |
227 | } | |
228 | } | |
903a5b8a | 229 | |
45ad74d3 | 230 | goto end; |
903a5b8a SM |
231 | |
232 | error: | |
45ad74d3 SM |
233 | argpar_item_destroy(&opt_item->base); |
234 | opt_item = NULL; | |
903a5b8a SM |
235 | |
236 | end: | |
45ad74d3 | 237 | return opt_item; |
903a5b8a SM |
238 | } |
239 | ||
793f1620 PP |
240 | /* |
241 | * Creates and returns a non-option parsing item for the original | |
242 | * argument `arg` having the original index `orig_index` and the | |
243 | * non-option index `non_opt_index`. | |
244 | * | |
245 | * Returns `NULL` on memory error. | |
246 | */ | |
45ad74d3 SM |
247 | static argpar_item_non_opt_t *create_non_opt_item(const char * const arg, |
248 | const unsigned int orig_index, | |
249 | const unsigned int non_opt_index) | |
903a5b8a | 250 | { |
45ad74d3 | 251 | argpar_item_non_opt_t * const non_opt_item = ARGPAR_ZALLOC(argpar_item_non_opt_t); |
903a5b8a | 252 | |
45ad74d3 SM |
253 | if (!non_opt_item) { |
254 | goto end; | |
255 | } | |
903a5b8a | 256 | |
45ad74d3 SM |
257 | non_opt_item->base.type = ARGPAR_ITEM_TYPE_NON_OPT; |
258 | non_opt_item->arg = arg; | |
259 | non_opt_item->orig_index = orig_index; | |
260 | non_opt_item->non_opt_index = non_opt_index; | |
903a5b8a SM |
261 | |
262 | end: | |
45ad74d3 | 263 | return non_opt_item; |
903a5b8a SM |
264 | } |
265 | ||
8b95d883 PP |
266 | /* |
267 | * If `error` is not `NULL`, sets the error `error` to a new parsing | |
268 | * error object, setting its `unknown_opt_name`, `opt_descr`, and | |
269 | * `is_short` members from the parameters. | |
270 | * | |
271 | * `unknown_opt_name` is the unknown option name without any `-` or `--` | |
272 | * prefix: `is_short` controls which type of unknown option it is. | |
273 | * | |
274 | * Returns 0 on success (including if `error` is `NULL`) or -1 on memory | |
275 | * error. | |
276 | */ | |
45ad74d3 SM |
277 | static int set_error(argpar_error_t ** const error, argpar_error_type_t type, |
278 | const char * const unknown_opt_name, | |
279 | const argpar_opt_descr_t * const opt_descr, const bool is_short) | |
8b95d883 | 280 | { |
45ad74d3 | 281 | int ret = 0; |
8b95d883 | 282 | |
45ad74d3 SM |
283 | if (!error) { |
284 | goto end; | |
285 | } | |
8b95d883 | 286 | |
45ad74d3 SM |
287 | *error = ARGPAR_ZALLOC(argpar_error_t); |
288 | if (!*error) { | |
289 | goto error; | |
290 | } | |
8b95d883 | 291 | |
45ad74d3 | 292 | (*error)->type = type; |
10aefab2 | 293 | |
45ad74d3 SM |
294 | if (unknown_opt_name) { |
295 | (*error)->unknown_opt_name = | |
296 | ARGPAR_CALLOC(char, strlen(unknown_opt_name) + 1 + (is_short ? 1 : 2)); | |
297 | if (!(*error)->unknown_opt_name) { | |
298 | goto error; | |
299 | } | |
8b95d883 | 300 | |
45ad74d3 SM |
301 | if (is_short) { |
302 | strcpy((*error)->unknown_opt_name, "-"); | |
303 | } else { | |
304 | strcpy((*error)->unknown_opt_name, "--"); | |
305 | } | |
8b95d883 | 306 | |
45ad74d3 SM |
307 | strcat((*error)->unknown_opt_name, unknown_opt_name); |
308 | } | |
8b95d883 | 309 | |
45ad74d3 SM |
310 | (*error)->opt_descr = opt_descr; |
311 | (*error)->is_short = is_short; | |
312 | goto end; | |
8b95d883 PP |
313 | |
314 | error: | |
45ad74d3 SM |
315 | argpar_error_destroy(*error); |
316 | ret = -1; | |
8b95d883 PP |
317 | |
318 | end: | |
45ad74d3 | 319 | return ret; |
8b95d883 PP |
320 | } |
321 | ||
45ad74d3 | 322 | ARGPAR_HIDDEN argpar_error_type_t argpar_error_type(const argpar_error_t * const error) |
10aefab2 | 323 | { |
45ad74d3 SM |
324 | ARGPAR_ASSERT(error); |
325 | return error->type; | |
10aefab2 SM |
326 | } |
327 | ||
45ad74d3 | 328 | ARGPAR_HIDDEN unsigned int argpar_error_orig_index(const argpar_error_t * const error) |
8b95d883 | 329 | { |
45ad74d3 SM |
330 | ARGPAR_ASSERT(error); |
331 | return error->orig_index; | |
8b95d883 PP |
332 | } |
333 | ||
45ad74d3 | 334 | ARGPAR_HIDDEN const char *argpar_error_unknown_opt_name(const argpar_error_t * const error) |
8b95d883 | 335 | { |
45ad74d3 SM |
336 | ARGPAR_ASSERT(error); |
337 | ARGPAR_ASSERT(error->type == ARGPAR_ERROR_TYPE_UNKNOWN_OPT); | |
338 | ARGPAR_ASSERT(error->unknown_opt_name); | |
339 | return error->unknown_opt_name; | |
8b95d883 PP |
340 | } |
341 | ||
45ad74d3 SM |
342 | ARGPAR_HIDDEN const argpar_opt_descr_t *argpar_error_opt_descr(const argpar_error_t * const error, |
343 | bool * const is_short) | |
8b95d883 | 344 | { |
45ad74d3 SM |
345 | ARGPAR_ASSERT(error); |
346 | ARGPAR_ASSERT(error->type == ARGPAR_ERROR_TYPE_MISSING_OPT_ARG || | |
347 | error->type == ARGPAR_ERROR_TYPE_UNEXPECTED_OPT_ARG); | |
348 | ARGPAR_ASSERT(error->opt_descr); | |
8b95d883 | 349 | |
45ad74d3 SM |
350 | if (is_short) { |
351 | *is_short = error->is_short; | |
352 | } | |
8b95d883 | 353 | |
45ad74d3 | 354 | return error->opt_descr; |
8b95d883 PP |
355 | } |
356 | ||
45ad74d3 | 357 | ARGPAR_HIDDEN void argpar_error_destroy(const argpar_error_t * const error) |
8b95d883 | 358 | { |
45ad74d3 SM |
359 | if (error) { |
360 | free(error->unknown_opt_name); | |
361 | free((void *) error); | |
362 | } | |
8b95d883 PP |
363 | } |
364 | ||
793f1620 PP |
365 | /* |
366 | * Finds and returns the _first_ descriptor having the short option name | |
367 | * `short_name` or the long option name `long_name` within the option | |
368 | * descriptors `descrs`. | |
369 | * | |
370 | * `short_name` may be `'\0'` to not consider it. | |
371 | * | |
372 | * `long_name` may be `NULL` to not consider it. | |
373 | * | |
374 | * Returns `NULL` if no descriptor is found. | |
375 | */ | |
45ad74d3 SM |
376 | static const argpar_opt_descr_t *find_descr(const argpar_opt_descr_t * const descrs, |
377 | const char short_name, const char * const long_name) | |
903a5b8a | 378 | { |
45ad74d3 | 379 | const argpar_opt_descr_t *descr; |
903a5b8a | 380 | |
45ad74d3 SM |
381 | for (descr = descrs; descr->short_name || descr->long_name; descr++) { |
382 | if (short_name && descr->short_name && short_name == descr->short_name) { | |
383 | goto end; | |
384 | } | |
903a5b8a | 385 | |
45ad74d3 SM |
386 | if (long_name && descr->long_name && strcmp(long_name, descr->long_name) == 0) { |
387 | goto end; | |
388 | } | |
389 | } | |
903a5b8a SM |
390 | |
391 | end: | |
45ad74d3 | 392 | return !descr->short_name && !descr->long_name ? NULL : descr; |
903a5b8a SM |
393 | } |
394 | ||
793f1620 | 395 | /* Return type of parse_short_opt_group() and parse_long_opt() */ |
45ad74d3 SM |
396 | typedef enum parse_orig_arg_opt_ret |
397 | { | |
398 | PARSE_ORIG_ARG_OPT_RET_OK, | |
399 | PARSE_ORIG_ARG_OPT_RET_ERROR = -1, | |
400 | PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY = -2, | |
7554f0b6 | 401 | } parse_orig_arg_opt_ret_t; |
903a5b8a | 402 | |
793f1620 PP |
403 | /* |
404 | * Parses the short option group argument `short_opt_group`, starting | |
405 | * where needed depending on the state of `iter`. | |
406 | * | |
407 | * On success, sets `*item`. | |
408 | * | |
409 | * On error (except for `PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY`), sets | |
410 | * `*error`. | |
411 | */ | |
45ad74d3 SM |
412 | static parse_orig_arg_opt_ret_t |
413 | parse_short_opt_group(const char * const short_opt_group, const char * const next_orig_arg, | |
414 | const argpar_opt_descr_t * const descrs, argpar_iter_t * const iter, | |
415 | argpar_error_t ** const error, argpar_item_t ** const item) | |
903a5b8a | 416 | { |
45ad74d3 SM |
417 | parse_orig_arg_opt_ret_t ret = PARSE_ORIG_ARG_OPT_RET_OK; |
418 | bool used_next_orig_arg = false; | |
419 | const char *opt_arg = NULL; | |
420 | const argpar_opt_descr_t *descr; | |
421 | argpar_item_opt_t *opt_item; | |
422 | ||
423 | ARGPAR_ASSERT(strlen(short_opt_group) != 0); | |
424 | ||
425 | if (!iter->short_opt_group_ch) { | |
426 | iter->short_opt_group_ch = short_opt_group; | |
427 | } | |
428 | ||
429 | /* Find corresponding option descriptor */ | |
430 | descr = find_descr(descrs, *iter->short_opt_group_ch, NULL); | |
431 | if (!descr) { | |
432 | const char unknown_opt_name[] = {*iter->short_opt_group_ch, '\0'}; | |
433 | ||
434 | ret = PARSE_ORIG_ARG_OPT_RET_ERROR; | |
435 | ||
436 | if (set_error(error, ARGPAR_ERROR_TYPE_UNKNOWN_OPT, unknown_opt_name, NULL, true)) { | |
437 | ret = PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY; | |
438 | } | |
439 | ||
440 | goto error; | |
441 | } | |
442 | ||
443 | if (descr->with_arg) { | |
444 | if (iter->short_opt_group_ch[1]) { | |
445 | /* `-oarg` form */ | |
446 | opt_arg = &iter->short_opt_group_ch[1]; | |
447 | } else { | |
448 | /* `-o arg` form */ | |
449 | opt_arg = next_orig_arg; | |
450 | used_next_orig_arg = true; | |
451 | } | |
452 | ||
453 | /* | |
454 | * We accept `-o ''` (empty option argument), but not | |
455 | * `-o` alone if an option argument is expected. | |
456 | */ | |
457 | if (!opt_arg || (iter->short_opt_group_ch[1] && strlen(opt_arg) == 0)) { | |
458 | ret = PARSE_ORIG_ARG_OPT_RET_ERROR; | |
459 | ||
460 | if (set_error(error, ARGPAR_ERROR_TYPE_MISSING_OPT_ARG, NULL, descr, true)) { | |
461 | ret = PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY; | |
462 | } | |
463 | ||
464 | goto error; | |
465 | } | |
466 | } | |
467 | ||
468 | /* Create and append option argument */ | |
469 | opt_item = create_opt_item(descr, opt_arg); | |
470 | if (!opt_item) { | |
471 | ret = PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY; | |
472 | goto error; | |
473 | } | |
474 | ||
475 | *item = &opt_item->base; | |
476 | iter->short_opt_group_ch++; | |
477 | ||
478 | if (descr->with_arg || !*iter->short_opt_group_ch) { | |
479 | /* Option has an argument: no more options */ | |
480 | iter->short_opt_group_ch = NULL; | |
481 | ||
482 | if (used_next_orig_arg) { | |
483 | iter->i += 2; | |
484 | } else { | |
485 | iter->i++; | |
486 | } | |
487 | } | |
488 | ||
489 | goto end; | |
903a5b8a SM |
490 | |
491 | error: | |
45ad74d3 | 492 | ARGPAR_ASSERT(ret != PARSE_ORIG_ARG_OPT_RET_OK); |
903a5b8a SM |
493 | |
494 | end: | |
45ad74d3 | 495 | return ret; |
903a5b8a SM |
496 | } |
497 | ||
793f1620 PP |
498 | /* |
499 | * Parses the long option argument `long_opt_arg`. | |
500 | * | |
501 | * On success, sets `*item`. | |
502 | * | |
503 | * On error (except for `PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY`), sets | |
504 | * `*error`. | |
505 | */ | |
45ad74d3 SM |
506 | static parse_orig_arg_opt_ret_t |
507 | parse_long_opt(const char * const long_opt_arg, const char * const next_orig_arg, | |
508 | const argpar_opt_descr_t * const descrs, argpar_iter_t * const iter, | |
509 | argpar_error_t ** const error, argpar_item_t ** const item) | |
903a5b8a | 510 | { |
45ad74d3 SM |
511 | parse_orig_arg_opt_ret_t ret = PARSE_ORIG_ARG_OPT_RET_OK; |
512 | const argpar_opt_descr_t *descr; | |
513 | argpar_item_opt_t *opt_item; | |
514 | bool used_next_orig_arg = false; | |
515 | ||
516 | /* Option's argument, if any */ | |
517 | const char *opt_arg = NULL; | |
518 | ||
519 | /* Position of first `=`, if any */ | |
520 | const char *eq_pos; | |
521 | ||
522 | /* Option name */ | |
523 | const char *long_opt_name = long_opt_arg; | |
524 | ||
525 | ARGPAR_ASSERT(strlen(long_opt_arg) != 0); | |
526 | ||
527 | /* Find the first `=` in original argument */ | |
528 | eq_pos = strchr(long_opt_arg, '='); | |
529 | if (eq_pos) { | |
530 | const size_t long_opt_name_size = eq_pos - long_opt_arg; | |
531 | ||
532 | /* Isolate the option name */ | |
533 | while (long_opt_name_size > iter->tmp_buf.size - 1) { | |
2f9b7649 SM |
534 | const size_t new_size = iter->tmp_buf.size * 2; |
535 | char * const new_data = ARGPAR_REALLOC(iter->tmp_buf.data, char, new_size); | |
536 | ||
537 | if (!new_data) { | |
45ad74d3 SM |
538 | ret = PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY; |
539 | goto error; | |
540 | } | |
2f9b7649 SM |
541 | |
542 | iter->tmp_buf.size = new_size; | |
543 | iter->tmp_buf.data = new_data; | |
45ad74d3 SM |
544 | } |
545 | ||
546 | memcpy(iter->tmp_buf.data, long_opt_arg, long_opt_name_size); | |
547 | iter->tmp_buf.data[long_opt_name_size] = '\0'; | |
548 | long_opt_name = iter->tmp_buf.data; | |
549 | } | |
550 | ||
551 | /* Find corresponding option descriptor */ | |
552 | descr = find_descr(descrs, '\0', long_opt_name); | |
553 | if (!descr) { | |
554 | ret = PARSE_ORIG_ARG_OPT_RET_ERROR; | |
555 | ||
556 | if (set_error(error, ARGPAR_ERROR_TYPE_UNKNOWN_OPT, long_opt_name, NULL, false)) { | |
557 | ret = PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY; | |
558 | } | |
559 | ||
560 | goto error; | |
561 | } | |
562 | ||
563 | /* Find option's argument if any */ | |
564 | if (descr->with_arg) { | |
565 | if (eq_pos) { | |
566 | /* `--long-opt=arg` style */ | |
567 | opt_arg = eq_pos + 1; | |
568 | } else { | |
569 | /* `--long-opt arg` style */ | |
570 | if (!next_orig_arg) { | |
571 | ret = PARSE_ORIG_ARG_OPT_RET_ERROR; | |
572 | ||
573 | if (set_error(error, ARGPAR_ERROR_TYPE_MISSING_OPT_ARG, NULL, descr, false)) { | |
574 | ret = PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY; | |
575 | } | |
576 | ||
577 | goto error; | |
578 | } | |
579 | ||
580 | opt_arg = next_orig_arg; | |
581 | used_next_orig_arg = true; | |
582 | } | |
583 | } else if (eq_pos) { | |
584 | /* | |
585 | * Unexpected `--opt=arg` style for a long option which | |
586 | * doesn't accept an argument. | |
587 | */ | |
588 | ret = PARSE_ORIG_ARG_OPT_RET_ERROR; | |
589 | ||
590 | if (set_error(error, ARGPAR_ERROR_TYPE_UNEXPECTED_OPT_ARG, NULL, descr, false)) { | |
591 | ret = PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY; | |
592 | } | |
593 | ||
594 | goto error; | |
595 | } | |
596 | ||
597 | /* Create and append option argument */ | |
598 | opt_item = create_opt_item(descr, opt_arg); | |
599 | if (!opt_item) { | |
600 | goto error; | |
601 | } | |
602 | ||
603 | if (used_next_orig_arg) { | |
604 | iter->i += 2; | |
605 | } else { | |
606 | iter->i++; | |
607 | } | |
608 | ||
609 | *item = &opt_item->base; | |
610 | goto end; | |
903a5b8a SM |
611 | |
612 | error: | |
45ad74d3 | 613 | ARGPAR_ASSERT(ret != PARSE_ORIG_ARG_OPT_RET_OK); |
903a5b8a SM |
614 | |
615 | end: | |
45ad74d3 | 616 | return ret; |
903a5b8a SM |
617 | } |
618 | ||
793f1620 PP |
619 | /* |
620 | * Parses the original argument `orig_arg`. | |
621 | * | |
622 | * On success, sets `*item`. | |
623 | * | |
624 | * On error (except for `PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY`), sets | |
625 | * `*error`. | |
626 | */ | |
45ad74d3 SM |
627 | static parse_orig_arg_opt_ret_t |
628 | parse_orig_arg_opt(const char * const orig_arg, const char * const next_orig_arg, | |
629 | const argpar_opt_descr_t * const descrs, argpar_iter_t * const iter, | |
630 | argpar_error_t ** const error, argpar_item_t ** const item) | |
903a5b8a | 631 | { |
45ad74d3 | 632 | parse_orig_arg_opt_ret_t ret = PARSE_ORIG_ARG_OPT_RET_OK; |
903a5b8a | 633 | |
45ad74d3 | 634 | ARGPAR_ASSERT(orig_arg[0] == '-'); |
903a5b8a | 635 | |
45ad74d3 SM |
636 | if (orig_arg[1] == '-') { |
637 | /* Long option */ | |
638 | ret = parse_long_opt(&orig_arg[2], next_orig_arg, descrs, iter, error, item); | |
639 | } else { | |
640 | /* Short option */ | |
641 | ret = parse_short_opt_group(&orig_arg[1], next_orig_arg, descrs, iter, error, item); | |
642 | } | |
903a5b8a | 643 | |
45ad74d3 | 644 | return ret; |
903a5b8a SM |
645 | } |
646 | ||
45ad74d3 SM |
647 | ARGPAR_HIDDEN argpar_iter_t *argpar_iter_create(const unsigned int argc, |
648 | const char * const * const argv, | |
649 | const argpar_opt_descr_t * const descrs) | |
903a5b8a | 650 | { |
45ad74d3 SM |
651 | argpar_iter_t *iter = ARGPAR_ZALLOC(argpar_iter_t); |
652 | ||
653 | if (!iter) { | |
654 | goto end; | |
655 | } | |
656 | ||
657 | iter->user.argc = argc; | |
658 | iter->user.argv = argv; | |
659 | iter->user.descrs = descrs; | |
660 | iter->tmp_buf.size = 128; | |
661 | iter->tmp_buf.data = ARGPAR_CALLOC(char, iter->tmp_buf.size); | |
662 | if (!iter->tmp_buf.data) { | |
663 | argpar_iter_destroy(iter); | |
664 | iter = NULL; | |
665 | goto end; | |
666 | } | |
903a5b8a | 667 | |
fc07e526 | 668 | end: |
45ad74d3 | 669 | return iter; |
fc07e526 | 670 | } |
903a5b8a | 671 | |
45ad74d3 | 672 | ARGPAR_HIDDEN void argpar_iter_destroy(argpar_iter_t * const iter) |
fc07e526 | 673 | { |
45ad74d3 SM |
674 | if (iter) { |
675 | free(iter->tmp_buf.data); | |
676 | free(iter); | |
677 | } | |
fc07e526 | 678 | } |
903a5b8a | 679 | |
45ad74d3 SM |
680 | ARGPAR_HIDDEN argpar_iter_next_status_t argpar_iter_next(argpar_iter_t * const iter, |
681 | const argpar_item_t ** const item, | |
682 | const argpar_error_t ** const error) | |
fc07e526 | 683 | { |
45ad74d3 SM |
684 | argpar_iter_next_status_t status; |
685 | parse_orig_arg_opt_ret_t parse_orig_arg_opt_ret; | |
686 | const char *orig_arg; | |
687 | const char *next_orig_arg; | |
688 | argpar_error_t ** const nc_error = (argpar_error_t **) error; | |
689 | ||
690 | ARGPAR_ASSERT(iter->i <= iter->user.argc); | |
691 | ||
692 | if (error) { | |
693 | *nc_error = NULL; | |
694 | } | |
695 | ||
696 | if (iter->i == iter->user.argc) { | |
697 | status = ARGPAR_ITER_NEXT_STATUS_END; | |
698 | goto end; | |
699 | } | |
700 | ||
701 | orig_arg = iter->user.argv[iter->i]; | |
702 | next_orig_arg = iter->i < (iter->user.argc - 1) ? iter->user.argv[iter->i + 1] : NULL; | |
703 | ||
704 | if (strcmp(orig_arg, "-") == 0 || strcmp(orig_arg, "--") == 0 || orig_arg[0] != '-') { | |
705 | /* Non-option argument */ | |
706 | const argpar_item_non_opt_t * const non_opt_item = | |
707 | create_non_opt_item(orig_arg, iter->i, iter->non_opt_index); | |
708 | ||
709 | if (!non_opt_item) { | |
710 | status = ARGPAR_ITER_NEXT_STATUS_ERROR_MEMORY; | |
711 | goto end; | |
712 | } | |
713 | ||
714 | iter->non_opt_index++; | |
715 | iter->i++; | |
716 | *item = &non_opt_item->base; | |
717 | status = ARGPAR_ITER_NEXT_STATUS_OK; | |
718 | goto end; | |
719 | } | |
720 | ||
721 | /* Option argument */ | |
722 | parse_orig_arg_opt_ret = parse_orig_arg_opt(orig_arg, next_orig_arg, iter->user.descrs, iter, | |
723 | nc_error, (argpar_item_t **) item); | |
724 | switch (parse_orig_arg_opt_ret) { | |
725 | case PARSE_ORIG_ARG_OPT_RET_OK: | |
726 | status = ARGPAR_ITER_NEXT_STATUS_OK; | |
727 | break; | |
728 | case PARSE_ORIG_ARG_OPT_RET_ERROR: | |
729 | if (error) { | |
730 | ARGPAR_ASSERT(*error); | |
731 | (*nc_error)->orig_index = iter->i; | |
732 | } | |
733 | status = ARGPAR_ITER_NEXT_STATUS_ERROR; | |
734 | break; | |
735 | case PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY: | |
736 | status = ARGPAR_ITER_NEXT_STATUS_ERROR_MEMORY; | |
737 | break; | |
738 | default: | |
739 | abort(); | |
740 | } | |
fc07e526 SM |
741 | |
742 | end: | |
45ad74d3 | 743 | return status; |
fc07e526 SM |
744 | } |
745 | ||
45ad74d3 | 746 | ARGPAR_HIDDEN unsigned int argpar_iter_ingested_orig_args(const argpar_iter_t * const iter) |
fc07e526 | 747 | { |
45ad74d3 | 748 | return iter->i; |
fc07e526 | 749 | } |