diff options
author | Petr Machata <petrm@nvidia.com> | 2023-11-22 16:23:29 +0100 |
---|---|---|
committer | David Ahern <dsahern@kernel.org> | 2023-11-22 19:31:59 +0000 |
commit | 256e0ca4b84f585ed0a3fcdc14216498d2e02b45 (patch) | |
tree | 3c793268ef519274f3169ddb82f4f6fbbc51ddee | |
parent | 60254925ccab10cfd24dc43c91cb95e50f65c7cf (diff) | |
download | iproute2-256e0ca4b84f585ed0a3fcdc14216498d2e02b45.tar.gz |
lib: utils: Generalize parse_one_of()
The following patch will change the way parse_one_of() and parse_on_off()
parse the strings they are given. To prepare for this change, extract from
parse_one_of() the functional core, which express in terms of a
configurable matcher, a pointer to a function that does the string
comparison. Then rewrite parse_one_of() and parse_on_off() as wrappers that
just pass matches() as the matcher, thereby maintaining the same behavior
as they currently have.
Signed-off-by: Petr Machata <petrm@nvidia.com>
Signed-off-by: David Ahern <dsahern@kernel.org>
-rw-r--r-- | lib/utils.c | 17 |
1 files changed, 13 insertions, 4 deletions
diff --git a/lib/utils.c b/lib/utils.c index 1fc42a9a8..5c91aaa96 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -1729,13 +1729,15 @@ 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) +static int +__parse_one_of(const char *msg, const char *realval, + const char * const *list, size_t len, int *p_err, + int (*matcher)(const char *, const char *)) { int i; for (i = 0; i < len; i++) { - if (list[i] && matches(realval, list[i]) == 0) { + if (list[i] && matcher(realval, list[i]) == 0) { *p_err = 0; return i; } @@ -1750,11 +1752,18 @@ int parse_one_of(const char *msg, const char *realval, const char * const *list, return 0; } +int parse_one_of(const char *msg, const char *realval, const char * const *list, + size_t len, int *p_err) +{ + return __parse_one_of(msg, realval, list, len, p_err, matches); +} + 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); + return __parse_one_of(msg, realval, values_on_off, + ARRAY_SIZE(values_on_off), p_err, matches); } int parse_mapping_gen(int *argcp, char ***argvp, |