X-Git-Url: http://git.efficios.com/?p=argpar.git;a=blobdiff_plain;f=argpar%2Fargpar.c;h=47056b89f069bd3b100eaed3c874a1b55e62d95a;hp=79a22f565bf99add9bb16b1eda572cd4b525526b;hb=793f1620b6ef32be7f46a372d7d0502e1f1010a4;hpb=8b95d883334b188d05cb1967980eae628e295d10 diff --git a/argpar/argpar.c b/argpar/argpar.c index 79a22f5..47056b8 100644 --- a/argpar/argpar.c +++ b/argpar/argpar.c @@ -41,9 +41,11 @@ struct argpar_iter { * Data provided by the user to argpar_iter_create(); immutable * afterwards. */ - unsigned int argc; - const char * const *argv; - const struct argpar_opt_descr *descrs; + struct { + unsigned int argc; + const char * const *argv; + const struct argpar_opt_descr *descrs; + } user; /* * Index of the argument to process in the next @@ -190,6 +192,12 @@ end: return; } +/* + * Creates and returns an option parsing item for the descriptor `descr` + * and having the argument `arg` (copied; may be `NULL`). + * + * Returns `NULL` on memory error. + */ static struct argpar_item_opt *create_opt_item( const struct argpar_opt_descr * const descr, @@ -222,6 +230,13 @@ end: return opt_item; } +/* + * Creates and returns a non-option parsing item for the original + * argument `arg` having the original index `orig_index` and the + * non-option index `non_opt_index`. + * + * Returns `NULL` on memory error. + */ static struct argpar_item_non_opt *create_non_opt_item(const char * const arg, const unsigned int orig_index, @@ -338,6 +353,17 @@ void argpar_error_destroy(const struct argpar_error * const error) } } +/* + * Finds and returns the _first_ descriptor having the short option name + * `short_name` or the long option name `long_name` within the option + * descriptors `descrs`. + * + * `short_name` may be `'\0'` to not consider it. + * + * `long_name` may be `NULL` to not consider it. + * + * Returns `NULL` if no descriptor is found. + */ static const struct argpar_opt_descr *find_descr( const struct argpar_opt_descr * const descrs, @@ -361,6 +387,7 @@ end: return !descr->short_name && !descr->long_name ? NULL : descr; } +/* Return type of parse_short_opt_group() and parse_long_opt() */ enum parse_orig_arg_opt_ret { PARSE_ORIG_ARG_OPT_RET_OK, PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT = -1, @@ -369,6 +396,15 @@ enum parse_orig_arg_opt_ret { PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY = -5, }; +/* + * Parses the short option group argument `short_opt_group`, starting + * where needed depending on the state of `iter`. + * + * On success, sets `*item`. + * + * On error (except for `PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY`), sets + * `*error`. + */ static enum parse_orig_arg_opt_ret parse_short_opt_group( const char * const short_opt_group, @@ -461,6 +497,14 @@ end: return ret; } +/* + * Parses the long option argument `long_opt_arg`. + * + * On success, sets `*item`. + * + * On error (except for `PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY`), sets + * `*error`. + */ static enum parse_orig_arg_opt_ret parse_long_opt(const char * const long_opt_arg, const char * const next_orig_arg, @@ -574,6 +618,14 @@ end: return ret; } +/* + * Parses the original argument `orig_arg`. + * + * On success, sets `*item`. + * + * On error (except for `PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY`), sets + * `*error`. + */ static enum parse_orig_arg_opt_ret parse_orig_arg_opt(const char * const orig_arg, const char * const next_orig_arg, @@ -610,9 +662,9 @@ struct argpar_iter *argpar_iter_create(const unsigned int argc, goto end; } - iter->argc = argc; - iter->argv = argv; - iter->descrs = descrs; + iter->user.argc = argc; + iter->user.argv = argv; + iter->user.descrs = descrs; iter->tmp_buf.size = 128; iter->tmp_buf.data = ARGPAR_CALLOC(char, iter->tmp_buf.size); if (!iter->tmp_buf.data) { @@ -646,20 +698,21 @@ enum argpar_iter_next_status argpar_iter_next( const char *next_orig_arg; struct argpar_error ** const nc_error = (struct argpar_error **) error; - ARGPAR_ASSERT(iter->i <= iter->argc); + ARGPAR_ASSERT(iter->i <= iter->user.argc); if (error) { *nc_error = NULL; } - if (iter->i == iter->argc) { + if (iter->i == iter->user.argc) { status = ARGPAR_ITER_NEXT_STATUS_END; goto end; } - orig_arg = iter->argv[iter->i]; + orig_arg = iter->user.argv[iter->i]; next_orig_arg = - iter->i < (iter->argc - 1) ? iter->argv[iter->i + 1] : NULL; + iter->i < (iter->user.argc - 1) ? + iter->user.argv[iter->i + 1] : NULL; if (strcmp(orig_arg, "-") == 0 || strcmp(orig_arg, "--") == 0 || orig_arg[0] != '-') { @@ -682,7 +735,7 @@ enum argpar_iter_next_status argpar_iter_next( /* Option argument */ parse_orig_arg_opt_ret = parse_orig_arg_opt(orig_arg, - next_orig_arg, iter->descrs, iter, nc_error, + next_orig_arg, iter->user.descrs, iter, nc_error, (struct argpar_item **) item); switch (parse_orig_arg_opt_ret) { case PARSE_ORIG_ARG_OPT_RET_OK: