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