X-Git-Url: http://git.efficios.com/?p=argpar.git;a=blobdiff_plain;f=argpar%2Fargpar.c;h=1ee5539ec0b221cd1457237ba434ecb1974ea2f0;hp=4458d9a640efc3e51e85052f13bc0cf6d09a6969;hb=d4539a906f9b4114a3f0a40e33598cf8077767c2;hpb=11003cd5369dbb2422796b6ee2d8433895e4b05d diff --git a/argpar/argpar.c b/argpar/argpar.c index 4458d9a..1ee5539 100644 --- a/argpar/argpar.c +++ b/argpar/argpar.c @@ -63,6 +63,42 @@ struct argpar_iter { const char *short_opt_ch; }; +/* Base parsing item */ +struct argpar_item { + enum argpar_item_type type; +}; + +/* Option parsing item */ +struct argpar_item_opt { + struct argpar_item base; + + /* Corresponding descriptor */ + const struct argpar_opt_descr *descr; + + /* Argument, or `NULL` if none; owned by this */ + char *arg; +}; + +/* Non-option parsing item */ +struct argpar_item_non_opt { + struct argpar_item base; + + /* + * Complete argument, pointing to one of the entries of the + * original arguments (`argv`). + */ + const char *arg; + + /* + * Index of this argument amongst all original arguments + * (`argv`). + */ + unsigned int orig_index; + + /* Index of this argument amongst other non-option arguments */ + unsigned int non_opt_index; +}; + static __attribute__((format(ARGPAR_PRINTF_FORMAT, 1, 0))) char *argpar_vasprintf(const char * const fmt, va_list args) { @@ -136,6 +172,56 @@ end: return success; } +ARGPAR_HIDDEN +enum argpar_item_type argpar_item_type(const struct argpar_item * const item) +{ + ARGPAR_ASSERT(item); + return item->type; +} + +ARGPAR_HIDDEN +const struct argpar_opt_descr *argpar_item_opt_descr( + const struct argpar_item * const item) +{ + ARGPAR_ASSERT(item); + ARGPAR_ASSERT(item->type == ARGPAR_ITEM_TYPE_OPT); + return ((const struct argpar_item_opt *) item)->descr; +} + +ARGPAR_HIDDEN +const char *argpar_item_opt_arg(const struct argpar_item * const item) +{ + ARGPAR_ASSERT(item); + ARGPAR_ASSERT(item->type == ARGPAR_ITEM_TYPE_OPT); + return ((const struct argpar_item_opt *) item)->arg; +} + +ARGPAR_HIDDEN +const char *argpar_item_non_opt_arg(const struct argpar_item * const item) +{ + ARGPAR_ASSERT(item); + ARGPAR_ASSERT(item->type == ARGPAR_ITEM_TYPE_NON_OPT); + return ((const struct argpar_item_non_opt *) item)->arg; +} + +ARGPAR_HIDDEN +unsigned int argpar_item_non_opt_orig_index( + const struct argpar_item * const item) +{ + ARGPAR_ASSERT(item); + ARGPAR_ASSERT(item->type == ARGPAR_ITEM_TYPE_NON_OPT); + return ((const struct argpar_item_non_opt *) item)->orig_index; +} + +ARGPAR_HIDDEN +unsigned int argpar_item_non_opt_non_opt_index( + const struct argpar_item * const item) +{ + ARGPAR_ASSERT(item); + ARGPAR_ASSERT(item->type == ARGPAR_ITEM_TYPE_NON_OPT); + return ((const struct argpar_item_non_opt *) item)->non_opt_index; +} + ARGPAR_HIDDEN void argpar_item_destroy(const struct argpar_item * const item) { @@ -147,7 +233,7 @@ void argpar_item_destroy(const struct argpar_item * const item) struct argpar_item_opt * const opt_item = (struct argpar_item_opt *) item; - free((void *) opt_item->arg); + free(opt_item->arg); } free((void *) item);