]> git.proxmox.com Git - mirror_iproute2.git/commitdiff
lib: Add parse_one_of(), parse_on_off()
authorPetr Machata <me@pmachata.org>
Thu, 12 Nov 2020 22:24:39 +0000 (23:24 +0100)
committerDavid Ahern <dsahern@gmail.com>
Sat, 14 Nov 2020 02:43:15 +0000 (19:43 -0700)
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>
include/utils.h
ip/ipmacsec.c
lib/utils.c

index 085b17b1f6e3ec9a790fcb5098f19ea4c7c0a867..d7653273af5f6caf393c8d3c85b4542862c1f7ee 100644 (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__ */
index 18289ecd6d9e0841b493e721c6acc192903cdb47..bf48e8b5d0b2ae26ec5d4a90c375bfcc24cfa260 100644 (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,
index 9815e328c9e0f94da872157b0a8c3cae040dad6b..eab0839a13c789d2294136a036d1f566c4f8e4ee 100644 (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);
+}