[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <0090c2dd421a51856d33ba62fdcc740624753464.1700061513.git.petrm@nvidia.com>
Date: Wed, 15 Nov 2023 16:31:58 +0100
From: Petr Machata <petrm@...dia.com>
To: David Ahern <dsahern@...il.com>, Stephen Hemminger
<stephen@...workplumber.org>, <netdev@...r.kernel.org>
CC: Patrisious Haddad <phaddad@...dia.com>, Petr Machata <petrm@...dia.com>
Subject: [PATCH iproute2-next 2/3] lib: utils: Generalize code of parse_one_of(), parse_on_off()
The following patch will split these two functions into their deprecated
and vanilla versions. Their behavior should be identical, except the
deprecated version will keep using matches() under the hood, wherease the
vanilla will use strcmp().
To prepare for this change, extract from each function a core, which
express in terms of a configurable matcher. Then rewrite parse_one_of() and
parse_on_off() as wrappers that pass matches() as the matcher to keep the
current behavior.
Signed-off-by: Petr Machata <petrm@...dia.com>
---
lib/utils.c | 25 ++++++++++++++++++++-----
1 file changed, 20 insertions(+), 5 deletions(-)
diff --git a/lib/utils.c b/lib/utils.c
index 1fc42a9a..7aa3409f 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,24 @@ int parse_one_of(const char *msg, const char *realval, const char * const *list,
return 0;
}
-bool parse_on_off(const char *msg, const char *realval, int *p_err)
+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);
+}
+
+static bool __parse_on_off(const char *msg, const char *realval, int *p_err,
+ int (*matcher)(const char *, const char *))
{
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, matcher);
+}
+
+bool parse_on_off(const char *msg, const char *realval, int *p_err)
+{
+ return __parse_on_off(msg, realval, p_err, matches);
}
int parse_mapping_gen(int *argcp, char ***argvp,
--
2.41.0
Powered by blists - more mailing lists