]> git.proxmox.com Git - mirror_iproute2.git/commitdiff
Unify batch processing across tools
authorPetr Machata <me@pmachata.org>
Thu, 12 Nov 2020 22:24:38 +0000 (23:24 +0100)
committerDavid Ahern <dsahern@gmail.com>
Sat, 14 Nov 2020 02:43:15 +0000 (19:43 -0700)
The code for handling batches is largely the same across iproute2 tools.
Extract a helper to handle the batch, and adjust the tools to dispatch to
this helper. Sandwitch the invocation between prologue / epilogue code
specific for each tool.

Signed-off-by: Petr Machata <me@pmachata.org>
Signed-off-by: David Ahern <dsahern@gmail.com>
bridge/bridge.c
devlink/devlink.c
include/utils.h
ip/ip.c
lib/utils.c
rdma/rdma.c
tc/tc.c

index 453d689732bd5d1715f95d2b919fc3365cb7e9d8..8f691cfdd4665866afb86be5da37f16f54adc870 100644 (file)
@@ -77,20 +77,14 @@ static int do_cmd(const char *argv0, int argc, char **argv)
        return -1;
 }
 
-static int batch(const char *name)
+static int br_batch_cmd(int argc, char *argv[], void *data)
 {
-       char *line = NULL;
-       size_t len = 0;
-       int ret = EXIT_SUCCESS;
+       return do_cmd(argv[0], argc, argv);
+}
 
-       if (name && strcmp(name, "-") != 0) {
-               if (freopen(name, "r", stdin) == NULL) {
-                       fprintf(stderr,
-                               "Cannot open file \"%s\" for reading: %s\n",
-                               name, strerror(errno));
-                       return EXIT_FAILURE;
-               }
-       }
+static int batch(const char *name)
+{
+       int ret;
 
        if (rtnl_open(&rth, 0) < 0) {
                fprintf(stderr, "Cannot open rtnetlink\n");
@@ -99,25 +93,7 @@ static int batch(const char *name)
 
        rtnl_set_strict_dump(&rth);
 
-       cmdlineno = 0;
-       while (getcmdline(&line, &len, stdin) != -1) {
-               char *largv[100];
-               int largc;
-
-               largc = makeargs(line, largv, 100);
-               if (largc == 0)
-                       continue;       /* blank line */
-
-               if (do_cmd(largv[0], largc, largv)) {
-                       fprintf(stderr, "Command failed %s:%d\n",
-                               name, cmdlineno);
-                       ret = EXIT_FAILURE;
-                       if (!force)
-                               break;
-               }
-       }
-       if (line)
-               free(line);
+       ret = do_batch(name, force, br_batch_cmd, NULL);
 
        rtnl_close(&rth);
        return ret;
index 1ff865bc5c22fead111d9c34f4bddb3151d2ed9f..5bb00b3a5b8cd83bb38ada63bcebfbbc83eddda3 100644 (file)
@@ -7912,43 +7912,16 @@ static void dl_free(struct dl *dl)
        free(dl);
 }
 
-static int dl_batch(struct dl *dl, const char *name, bool force)
+static int dl_batch_cmd(int argc, char *argv[], void *data)
 {
-       char *line = NULL;
-       size_t len = 0;
-       int ret = EXIT_SUCCESS;
-
-       if (name && strcmp(name, "-") != 0) {
-               if (freopen(name, "r", stdin) == NULL) {
-                       fprintf(stderr,
-                               "Cannot open file \"%s\" for reading: %s\n",
-                               name, strerror(errno));
-                       return EXIT_FAILURE;
-               }
-       }
-
-       cmdlineno = 0;
-       while (getcmdline(&line, &len, stdin) != -1) {
-               char *largv[100];
-               int largc;
-
-               largc = makeargs(line, largv, 100);
-               if (!largc)
-                       continue;       /* blank line */
-
-               if (dl_cmd(dl, largc, largv)) {
-                       fprintf(stderr, "Command failed %s:%d\n",
-                               name, cmdlineno);
-                       ret = EXIT_FAILURE;
-                       if (!force)
-                               break;
-               }
-       }
+       struct dl *dl = data;
 
-       if (line)
-               free(line);
+       return dl_cmd(dl, argc, argv);
+}
 
-       return ret;
+static int dl_batch(struct dl *dl, const char *name, bool force)
+{
+       return do_batch(name, force, dl_batch_cmd, dl);
 }
 
 int main(int argc, char **argv)
index 7041c4612e4632b40a5ce8d5b676ed65639b891f..085b17b1f6e3ec9a790fcb5098f19ea4c7c0a867 100644 (file)
@@ -322,4 +322,7 @@ int get_time64(__s64 *time, const char *str);
 char *sprint_time(__u32 time, char *buf);
 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);
+
 #endif /* __UTILS_H__ */
diff --git a/ip/ip.c b/ip/ip.c
index ac44502353702b522ae8394b7cb67deab0192c3e..5e31957f2420abacc279e0da0a08a32577620ffe 100644 (file)
--- a/ip/ip.c
+++ b/ip/ip.c
@@ -121,56 +121,30 @@ static int do_cmd(const char *argv0, int argc, char **argv)
        return EXIT_FAILURE;
 }
 
-static int batch(const char *name)
+static int ip_batch_cmd(int argc, char *argv[], void *data)
 {
-       char *line = NULL;
-       size_t len = 0;
-       int ret = EXIT_SUCCESS;
-       int orig_family = preferred_family;
+       const int *orig_family = data;
 
-       batch_mode = 1;
+       preferred_family = *orig_family;
+       return do_cmd(argv[0], argc, argv);
+}
 
-       if (name && strcmp(name, "-") != 0) {
-               if (freopen(name, "r", stdin) == NULL) {
-                       fprintf(stderr,
-                               "Cannot open file \"%s\" for reading: %s\n",
-                               name, strerror(errno));
-                       return EXIT_FAILURE;
-               }
-       }
+static int batch(const char *name)
+{
+       int orig_family = preferred_family;
+       int ret;
 
        if (rtnl_open(&rth, 0) < 0) {
                fprintf(stderr, "Cannot open rtnetlink\n");
                return EXIT_FAILURE;
        }
 
-       cmdlineno = 0;
-       while (getcmdline(&line, &len, stdin) != -1) {
-               char *largv[100];
-               int largc;
-
-               preferred_family = orig_family;
-
-               largc = makeargs(line, largv, 100);
-               if (largc == 0)
-                       continue;       /* blank line */
-
-               if (do_cmd(largv[0], largc, largv)) {
-                       fprintf(stderr, "Command failed %s:%d\n",
-                               name, cmdlineno);
-                       ret = EXIT_FAILURE;
-                       if (!force)
-                               break;
-               }
-       }
-       if (line)
-               free(line);
+       ret = do_batch(name, force, ip_batch_cmd, &orig_family);
 
        rtnl_close(&rth);
        return ret;
 }
 
-
 int main(int argc, char **argv)
 {
        char *basename;
index c98021d6ecad23e0f274770f8a53aaaadaa241ae..9815e328c9e0f94da872157b0a8c3cae040dad6b 100644 (file)
@@ -1695,3 +1695,43 @@ char *sprint_time64(__s64 time, char *buf)
        print_time64(buf, SPRINT_BSIZE-1, time);
        return buf;
 }
+
+int do_batch(const char *name, bool force,
+            int (*cmd)(int argc, char *argv[], void *data), void *data)
+{
+       char *line = NULL;
+       size_t len = 0;
+       int ret = EXIT_SUCCESS;
+
+       if (name && strcmp(name, "-") != 0) {
+               if (freopen(name, "r", stdin) == NULL) {
+                       fprintf(stderr,
+                               "Cannot open file \"%s\" for reading: %s\n",
+                               name, strerror(errno));
+                       return EXIT_FAILURE;
+               }
+       }
+
+       cmdlineno = 0;
+       while (getcmdline(&line, &len, stdin) != -1) {
+               char *largv[100];
+               int largc;
+
+               largc = makeargs(line, largv, 100);
+               if (!largc)
+                       continue;       /* blank line */
+
+               if (cmd(largc, largv, data)) {
+                       fprintf(stderr, "Command failed %s:%d\n",
+                               name, cmdlineno);
+                       ret = EXIT_FAILURE;
+                       if (!force)
+                               break;
+               }
+       }
+
+       if (line)
+               free(line);
+
+       return ret;
+}
index 9ea2d17ffe9e362bcecffb8299d07fd000f50400..8dc2d3e344be288f00e8f28b78a35826eac3ed3e 100644 (file)
@@ -41,40 +41,16 @@ static int rd_cmd(struct rd *rd, int argc, char **argv)
        return rd_exec_cmd(rd, cmds, "object");
 }
 
-static int rd_batch(struct rd *rd, const char *name, bool force)
+static int rd_batch_cmd(int argc, char *argv[], void *data)
 {
-       char *line = NULL;
-       size_t len = 0;
-       int ret = 0;
-
-       if (name && strcmp(name, "-") != 0) {
-               if (!freopen(name, "r", stdin)) {
-                       pr_err("Cannot open file \"%s\" for reading: %s\n",
-                              name, strerror(errno));
-                       return errno;
-               }
-       }
+       struct rd *rd = data;
 
-       cmdlineno = 0;
-       while (getcmdline(&line, &len, stdin) != -1) {
-               char *largv[512];
-               int largc;
-
-               largc = makeargs(line, largv, ARRAY_SIZE(largv));
-               if (!largc)
-                       continue;       /* blank line */
-
-               ret = rd_cmd(rd, largc, largv);
-               if (ret) {
-                       pr_err("Command failed %s:%d\n", name, cmdlineno);
-                       if (!force)
-                               break;
-               }
-       }
-
-       free(line);
+       return rd_cmd(rd, argc, argv);
+}
 
-       return ret;
+static int rd_batch(struct rd *rd, const char *name, bool force)
+{
+       return do_batch(name, force, rd_batch_cmd, rd);
 }
 
 static int rd_init(struct rd *rd, char *filename)
diff --git a/tc/tc.c b/tc/tc.c
index bdd5d4faf8867ec1f420b215f8743dbda99c2c00..af9b21dae1fcd0bc2d8c28c889b51b74581d27ce 100644 (file)
--- a/tc/tc.c
+++ b/tc/tc.c
@@ -233,22 +233,16 @@ static int do_cmd(int argc, char **argv)
        return -1;
 }
 
+static int tc_batch_cmd(int argc, char *argv[], void *data)
+{
+       return do_cmd(argc, argv);
+}
+
 static int batch(const char *name)
 {
-       char *line = NULL;
-       size_t len = 0;
-       int ret = 0;
+       int ret;
 
        batch_mode = 1;
-       if (name && strcmp(name, "-") != 0) {
-               if (freopen(name, "r", stdin) == NULL) {
-                       fprintf(stderr,
-                               "Cannot open file \"%s\" for reading: %s\n",
-                               name, strerror(errno));
-                       return -1;
-               }
-       }
-
        tc_core_init();
 
        if (rtnl_open(&rth, 0) < 0) {
@@ -256,26 +250,8 @@ static int batch(const char *name)
                return -1;
        }
 
-       cmdlineno = 0;
-       while (getcmdline(&line, &len, stdin) != -1) {
-               char *largv[100];
-               int largc;
-
-               largc = makeargs(line, largv, 100);
-               if (largc == 0)
-                       continue;       /* blank line */
-
-               if (do_cmd(largc, largv)) {
-                       fprintf(stderr, "Command failed %s:%d\n",
-                               name, cmdlineno);
-                       ret = 1;
-                       if (!force)
-                               break;
-               }
-               fflush(stdout);
-       }
+       ret = do_batch(name, force, tc_batch_cmd, NULL);
 
-       free(line);
        rtnl_close(&rth);
        return ret;
 }