[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <a814a899-f811-d634-2f0c-8e3240bfcfa4@gmail.com>
Date: Sat, 31 Oct 2020 09:37:45 -0600
From: David Ahern <dsahern@...il.com>
To: Petr Machata <me@...chata.org>, netdev@...r.kernel.org,
stephen@...workplumber.org
Cc: john.fastabend@...il.com, jiri@...dia.com, idosch@...dia.com,
Jakub Kicinski <kuba@...nel.org>,
Roman Mashak <mrv@...atatu.com>
Subject: Re: [PATCH iproute2-next v2 02/11] lib: Add parse_one_of(),
parse_on_off()
On 10/30/20 6:29 AM, Petr Machata wrote:
> diff --git a/lib/utils.c b/lib/utils.c
> index 9815e328c9e0..930877ae0f0d 100644
> --- a/lib/utils.c
> +++ b/lib/utils.c
> @@ -1735,3 +1735,31 @@ int do_batch(const char *name, bool force,
>
> return ret;
> }
> +
> +int parse_one_of(const char *msg, const char *realval, const char * const *list,
> + size_t len, int *p_err)
> +{
> + int i;
> +
> + for (i = 0; i < len; i++) {
> + if (list[i] && matches(realval, list[i]) == 0) {
> + *p_err = 0;
> + return i;
> + }
> + }
> +
> + fprintf(stderr, "Error: argument of \"%s\" must be one of ", msg);
> + for (i = 0; i < len; i++)
> + if (list[i])
> + fprintf(stderr, "\"%s\", ", list[i]);
> + fprintf(stderr, "not \"%s\"\n", realval);
> + *p_err = -EINVAL;
> + return 0;
> +}
> +
> +int parse_on_off(const char *msg, const char *realval, int *p_err)
> +{
> + static const char * const values_on_off[] = { "off", "on" };
> +
> + return parse_one_of(msg, realval, values_on_off, ARRAY_SIZE(values_on_off), p_err);
> +}
>
This has weird semantics to me. You have a buried array of strings and
returning the index of the one that matches. Let's use a 'bool' return
for parse_on_off that makes it clear that the string is 'off' = false or
'on' = true.
Powered by blists - more mailing lists