netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Petr Machata <me@pmachata.org>
To: netdev@vger.kernel.org, dsahern@gmail.com, stephen@networkplumber.org
Cc: Petr Machata <me@pmachata.org>
Subject: [PATCH iproute2-next 4/9] lib: Generalize parse_mapping()
Date: Wed, 23 Dec 2020 19:25:42 +0100	[thread overview]
Message-ID: <c4a47b7cb2afa735843bcee6027a8176513651e5.1608746691.git.me@pmachata.org> (raw)
In-Reply-To: <cover.1608746691.git.me@pmachata.org>

The function parse_mapping() assumes the key is a number, with a single
configurable exception, which is using "all" to mean "all possible keys".
If a caller wishes to use symbolic names instead of numbers, they cannot
reuse this function.

To facilitate reuse in these situations, convert parse_mapping() into a
helper, parse_mapping_gen(), which instead of an allow-all boolean takes a
generic key-parsing callback. Rewrite parse_mapping() in terms of this
newly-added helper and add a pair of key parsers, one for just numbers,
another for numbers and the keyword "all". Publish the latter as well.

Signed-off-by: Petr Machata <me@pmachata.org>
---
 include/utils.h |  5 +++++
 lib/utils.c     | 37 +++++++++++++++++++++++++++++++------
 2 files changed, 36 insertions(+), 6 deletions(-)

diff --git a/include/utils.h b/include/utils.h
index 1704392525a2..f1403f7306a0 100644
--- a/include/utils.h
+++ b/include/utils.h
@@ -331,6 +331,11 @@ 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);
 
+int parse_mapping_num_all(__u32 *keyp, const char *key);
+int parse_mapping_gen(int *argcp, char ***argvp,
+		      int (*key_cb)(__u32 *keyp, const char *key),
+		      int (*mapping_cb)(__u32 key, char *value, void *data),
+		      void *mapping_cb_data);
 int parse_mapping(int *argcp, char ***argvp, bool allow_all,
 		  int (*mapping_cb)(__u32 key, char *value, void *data),
 		  void *mapping_cb_data);
diff --git a/lib/utils.c b/lib/utils.c
index de875639c608..90e58fa309d5 100644
--- a/lib/utils.c
+++ b/lib/utils.c
@@ -1878,9 +1878,10 @@ bool parse_on_off(const char *msg, const char *realval, int *p_err)
 	return parse_one_of(msg, realval, values_on_off, ARRAY_SIZE(values_on_off), p_err);
 }
 
-int parse_mapping(int *argcp, char ***argvp, bool allow_all,
-		  int (*mapping_cb)(__u32 key, char *value, void *data),
-		  void *mapping_cb_data)
+int parse_mapping_gen(int *argcp, char ***argvp,
+		      int (*key_cb)(__u32 *keyp, const char *key),
+		      int (*mapping_cb)(__u32 key, char *value, void *data),
+		      void *mapping_cb_data)
 {
 	int argc = *argcp;
 	char **argv = *argvp;
@@ -1894,9 +1895,7 @@ int parse_mapping(int *argcp, char ***argvp, bool allow_all,
 			break;
 		*colon = '\0';
 
-		if (allow_all && matches(*argv, "all") == 0) {
-			key = (__u32) -1;
-		} else if (get_u32(&key, *argv, 0)) {
+		if (key_cb(&key, *argv)) {
 			ret = 1;
 			break;
 		}
@@ -1912,3 +1911,29 @@ int parse_mapping(int *argcp, char ***argvp, bool allow_all,
 	*argvp = argv;
 	return ret;
 }
+
+static int parse_mapping_num(__u32 *keyp, const char *key)
+{
+	return get_u32(keyp, key, 0);
+}
+
+int parse_mapping_num_all(__u32 *keyp, const char *key)
+{
+	if (matches(key, "all") == 0) {
+		*keyp = (__u32) -1;
+		return 0;
+	}
+	return parse_mapping_num(keyp, key);
+}
+
+int parse_mapping(int *argcp, char ***argvp, bool allow_all,
+		  int (*mapping_cb)(__u32 key, char *value, void *data),
+		  void *mapping_cb_data)
+{
+	if (allow_all)
+		return parse_mapping_gen(argcp, argvp, parse_mapping_num_all,
+					 mapping_cb, mapping_cb_data);
+	else
+		return parse_mapping_gen(argcp, argvp, parse_mapping_num,
+					 mapping_cb, mapping_cb_data);
+}
-- 
2.25.1


  parent reply	other threads:[~2020-12-23 18:27 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-23 18:25 [PATCH iproute2-next 0/9] dcb: Support APP, DCBX objects Petr Machata
2020-12-23 18:25 ` [PATCH iproute2-next 1/9] dcb: Set values with RTM_SETDCB type Petr Machata
2020-12-23 18:25 ` [PATCH iproute2-next 2/9] dcb: Plug a leaking DCB socket buffer Petr Machata
2020-12-23 18:25 ` [PATCH iproute2-next 3/9] lib: rt_names: Add rtnl_dsfield_get_name() Petr Machata
2020-12-23 18:25 ` Petr Machata [this message]
2020-12-23 18:25 ` [PATCH iproute2-next 5/9] dcb: Generalize dcb_set_attribute() Petr Machata
2020-12-23 18:25 ` [PATCH iproute2-next 6/9] dcb: Generalize dcb_get_attribute() Petr Machata
2020-12-23 18:25 ` [PATCH iproute2-next 7/9] dcb: Support -n to suppress translation to nice names Petr Machata
2020-12-31 17:11   ` David Ahern
2021-01-01 21:34     ` Petr Machata
2020-12-23 18:25 ` [PATCH iproute2-next 8/9] dcb: Add a subtool for the DCB APP object Petr Machata
2020-12-23 18:25 ` [PATCH iproute2-next 9/9] dcb: Add a subtool for the DCBX object Petr Machata

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=c4a47b7cb2afa735843bcee6027a8176513651e5.1608746691.git.me@pmachata.org \
    --to=me@pmachata.org \
    --cc=dsahern@gmail.com \
    --cc=netdev@vger.kernel.org \
    --cc=stephen@networkplumber.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).