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