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>
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");
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;
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)
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__ */
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;
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;
+}
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)
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) {
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;
}