lib: Add parse_one_of(), parse_on_off()

Take from the macsec code parse_one_of() and adapt so that it passes the
primary result as the main return value, and error result through a
pointer. That is the simplest way to make the code reusable across data
types without introducing extra magic.

Also from macsec take the specialization of parse_one_of() for parsing
specifically the strings "off" and "on".

Convert the macsec code to the new helpers.

Signed-off-by: Petr Machata <me@pmachata.org>
Signed-off-by: David Ahern <dsahern@gmail.com>
This commit is contained in:
Petr Machata 2020-11-12 23:24:39 +01:00 committed by David Ahern
parent 1d9a81b8c9
commit 82604d2852
3 changed files with 43 additions and 41 deletions

View File

@ -325,4 +325,8 @@ char *sprint_time64(__s64 time, char *buf);
int do_batch(const char *name, bool force,
int (*cmd)(int argc, char *argv[], void *user), void *user);
int parse_one_of(const char *msg, const char *realval, const char * const *list,
size_t len, int *p_err);
bool parse_on_off(const char *msg, const char *realval, int *p_err);
#endif /* __UTILS_H__ */

View File

@ -23,8 +23,6 @@
#include "ll_map.h"
#include "libgenl.h"
static const char * const values_on_off[] = { "off", "on" };
static const char * const validate_str[] = {
[MACSEC_VALIDATE_DISABLED] = "disabled",
[MACSEC_VALIDATE_CHECK] = "check",
@ -108,25 +106,6 @@ static void ipmacsec_usage(void)
exit(-1);
}
static int one_of(const char *msg, const char *realval, const char * const *list,
size_t len, int *index)
{
int i;
for (i = 0; i < len; i++) {
if (matches(realval, list[i]) == 0) {
*index = i;
return 0;
}
}
fprintf(stderr, "Error: argument of \"%s\" must be one of ", msg);
for (i = 0; i < len; i++)
fprintf(stderr, "\"%s\", ", list[i]);
fprintf(stderr, "not \"%s\"\n", realval);
return -1;
}
static int get_an(__u8 *val, const char *arg)
{
int ret = get_u8(val, arg, 0);
@ -559,8 +538,7 @@ static int do_offload(enum cmd c, int argc, char **argv)
if (argc == 0)
ipmacsec_usage();
ret = one_of("offload", *argv, offload_str, ARRAY_SIZE(offload_str),
(int *)&offload);
offload = parse_one_of("offload", *argv, offload_str, ARRAY_SIZE(offload_str), &ret);
if (ret)
ipmacsec_usage();
@ -1334,8 +1312,7 @@ static int macsec_parse_opt(struct link_util *lu, int argc, char **argv,
NEXT_ARG();
int i;
ret = one_of("encrypt", *argv, values_on_off,
ARRAY_SIZE(values_on_off), &i);
i = parse_on_off("encrypt", *argv, &ret);
if (ret != 0)
return ret;
addattr8(n, MACSEC_BUFLEN, IFLA_MACSEC_ENCRYPT, i);
@ -1343,8 +1320,7 @@ static int macsec_parse_opt(struct link_util *lu, int argc, char **argv,
NEXT_ARG();
int i;
ret = one_of("send_sci", *argv, values_on_off,
ARRAY_SIZE(values_on_off), &i);
i = parse_on_off("send_sci", *argv, &ret);
if (ret != 0)
return ret;
send_sci = i;
@ -1354,8 +1330,7 @@ static int macsec_parse_opt(struct link_util *lu, int argc, char **argv,
NEXT_ARG();
int i;
ret = one_of("end_station", *argv, values_on_off,
ARRAY_SIZE(values_on_off), &i);
i = parse_on_off("end_station", *argv, &ret);
if (ret != 0)
return ret;
es = i;
@ -1364,8 +1339,7 @@ static int macsec_parse_opt(struct link_util *lu, int argc, char **argv,
NEXT_ARG();
int i;
ret = one_of("scb", *argv, values_on_off,
ARRAY_SIZE(values_on_off), &i);
i = parse_on_off("scb", *argv, &ret);
if (ret != 0)
return ret;
scb = i;
@ -1374,8 +1348,7 @@ static int macsec_parse_opt(struct link_util *lu, int argc, char **argv,
NEXT_ARG();
int i;
ret = one_of("protect", *argv, values_on_off,
ARRAY_SIZE(values_on_off), &i);
i = parse_on_off("protect", *argv, &ret);
if (ret != 0)
return ret;
addattr8(n, MACSEC_BUFLEN, IFLA_MACSEC_PROTECT, i);
@ -1383,8 +1356,7 @@ static int macsec_parse_opt(struct link_util *lu, int argc, char **argv,
NEXT_ARG();
int i;
ret = one_of("replay", *argv, values_on_off,
ARRAY_SIZE(values_on_off), &i);
i = parse_on_off("replay", *argv, &ret);
if (ret != 0)
return ret;
replay_protect = !!i;
@ -1395,9 +1367,8 @@ static int macsec_parse_opt(struct link_util *lu, int argc, char **argv,
invarg("expected replay window size", *argv);
} else if (strcmp(*argv, "validate") == 0) {
NEXT_ARG();
ret = one_of("validate", *argv,
validate_str, ARRAY_SIZE(validate_str),
(int *)&validate);
validate = parse_one_of("validate", *argv, validate_str,
ARRAY_SIZE(validate_str), &ret);
if (ret != 0)
return ret;
addattr8(n, MACSEC_BUFLEN,
@ -1411,9 +1382,8 @@ static int macsec_parse_opt(struct link_util *lu, int argc, char **argv,
invarg("expected an { 0..3 }", *argv);
} else if (strcmp(*argv, "offload") == 0) {
NEXT_ARG();
ret = one_of("offload", *argv,
offload_str, ARRAY_SIZE(offload_str),
(int *)&offload);
offload = parse_one_of("offload", *argv, offload_str,
ARRAY_SIZE(offload_str), &ret);
if (ret != 0)
return ret;
addattr8(n, MACSEC_BUFLEN,

View File

@ -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;
}
bool 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);
}