]> git.ipfire.org Git - thirdparty/iproute2.git/commitdiff
lib: Generalize parse_mapping()
authorPetr Machata <me@pmachata.org>
Sat, 2 Jan 2021 00:03:36 +0000 (01:03 +0100)
committerDavid Ahern <dsahern@kernel.org>
Mon, 18 Jan 2021 04:09:29 +0000 (04:09 +0000)
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>
Signed-off-by: David Ahern <dsahern@kernel.org>
include/utils.h
lib/utils.c

index 1704392525a2bec55b28314d2f54c2a0670d1a66..f1403f7306a026c8d5959553352780964555b4e1 100644 (file)
@@ -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);
index de875639c60884ba9d0783c3c241635e09918bb6..90e58fa309d593eb6e11584582e359a2ac2113cc 100644 (file)
@@ -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);
+}