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