X-Git-Url: http://git.tuebingen.mpg.de/?a=blobdiff_plain;f=lopsub.c;h=a9e401f259f63422e3db094080cc66ecb808c694;hb=HEAD;hp=44abc93348c7b6e4120caeeea620c0f6504d46bb;hpb=cd0ae8bc3fd6cd643ef67f068a00d7f91927b0ea;p=lopsub.git diff --git a/lopsub.c b/lopsub.c index 44abc93..63da983 100644 --- a/lopsub.c +++ b/lopsub.c @@ -1,7 +1,7 @@ /* * Copyright (C) 2016 Andre Noll * - * Licensed under the LGPL v3, see http://www.gnu.org/licenses/lgpl-3.0.html + * Licensed under the LGPL v3, see https://www.gnu.org/licenses/lgpl-3.0.html */ #include #include @@ -155,7 +155,7 @@ static int xrealloc(void *p, size_t size) return 0; } -/* Print a formated message to a dynamically allocated string. */ +/* Print a formatted message to a dynamically allocated string. */ __attribute__ ((format (printf, 2, 0))) static int xvasprintf(char **result, const char *fmt, va_list ap) { @@ -632,17 +632,23 @@ static int lls_parse_arg(struct lls_arg *la, const struct lls_option *opts, bool multiple; int idx, ret; - if (!la->arg) - goto success; - if (opt->arg_info == LLS_NO_ARGUMENT) { + if (la->arg && opt->arg_info == LLS_NO_ARGUMENT) { xasprintf(errctx, "arg: %s, option: %s", la->arg, opt->name); return -E_LLS_ARG_GIVEN; } multiple = opt->flags & LLS_MULTIPLE; + if (opt->arg_type == LLS_STRING && !opt->values && !multiple) { + if (lor->value) { /* discard previous value */ + free(lor->value[0].string_val); + free(lor->value); + lor->value = NULL; + } + } + if (!la->arg) + goto success; idx = multiple? lor->given : 0; - if (lor->given == 0 || multiple) { - ret = xrealloc(&lor->value, - (lor->given + 1) * sizeof(*lor->value)); + if (!lor->value || multiple) { + ret = xrealloc(&lor->value, (idx + 1) * sizeof(*lor->value)); if (ret < 0) { xasprintf(errctx, "option value array for --%s", opt->name); @@ -651,8 +657,6 @@ static int lls_parse_arg(struct lls_arg *la, const struct lls_option *opts, } switch (opt->arg_type) { case LLS_STRING: - if (!opt->values && lor->given > 0 && !multiple) - free(lor->value[idx].string_val); if (opt->values) { ret = check_enum_arg(la->arg, opt, errctx); if (ret < 0) @@ -724,9 +728,11 @@ int lls_check_arg_count(const struct lls_parse_result *lpr, { if (errctx) *errctx = NULL; + assert(min_argc <= max_argc); if (lpr->num_inputs < min_argc) { - xasprintf(errctx, "at least %u non-option args required, " - "%u given", min_argc, lpr->num_inputs); + xasprintf(errctx, "%s %u non-option args required, %u given", + min_argc < max_argc? "at least" : "exactly", + min_argc, lpr->num_inputs); return -E_LLS_BAD_ARG_COUNT; } if (lpr->num_inputs > max_argc) { @@ -734,8 +740,11 @@ int lls_check_arg_count(const struct lls_parse_result *lpr, xasprintf(errctx, "no non-option args allowed, " "%u given", lpr->num_inputs); else - xasprintf(errctx, "at most %u non-option args allowed, " - "%u given", max_argc, lpr->num_inputs); + xasprintf(errctx, "%s %u non-option args %s, %u given", + min_argc < max_argc? "at most" : "exactly", + max_argc, + min_argc < max_argc? "allowed" : "required", + lpr->num_inputs); return -E_LLS_BAD_ARG_COUNT; } return 1;