From fdfe8ac080e38fd064ad501d2c23940c1cc91369 Mon Sep 17 00:00:00 2001 From: Ariadne Conill Date: Tue, 11 Aug 2020 22:51:53 -0600 Subject: [PATCH] multicall: migrate execution options to common code --- Makefile | 1 + cmd/ifquery.c | 24 ++--------- cmd/ifupdown.c | 11 ++--- cmd/multicall-exec-options.c | 77 +++++++++++++++++++++++++++++++++++ cmd/multicall-match-options.c | 57 ++++++++++++++++++++++++++ cmd/multicall.h | 3 ++ 6 files changed, 144 insertions(+), 29 deletions(-) create mode 100644 cmd/multicall-exec-options.c create mode 100644 cmd/multicall-match-options.c diff --git a/Makefile b/Makefile index a0615be..6762eec 100644 --- a/Makefile +++ b/Makefile @@ -42,6 +42,7 @@ LIBIFUPDOWN_LIB = libifupdown.a MULTICALL_SRC = \ cmd/multicall.c \ cmd/multicall-options.c \ + cmd/multicall-exec-options.c \ cmd/multicall-match-options.c MULTICALL_OBJ = ${MULTICALL_SRC:.c=.o} MULTICALL = ifupdown diff --git a/cmd/ifquery.c b/cmd/ifquery.c index b1df261..89c84d4 100644 --- a/cmd/ifquery.c +++ b/cmd/ifquery.c @@ -21,11 +21,6 @@ #include "libifupdown/libifupdown.h" #include "cmd/multicall.h" -static struct lif_execute_opts exec_opts = { - .interfaces_file = INTERFACES_FILE, - .executor_path = EXECUTOR_PATH -}; - void print_interface(struct lif_interface *iface) { @@ -250,18 +245,14 @@ ifquery_main(int argc, char *argv[]) struct lif_dict state = {}; struct lif_dict collection = {}; struct option long_options[] = { - {"interfaces", required_argument, 0, 'i'}, {"list", no_argument, 0, 'L'}, {"pretty-print", no_argument, 0, 'P'}, - {"state-file", required_argument, 0, 'S'}, {"state", no_argument, 0, 's'}, {"dot", no_argument, 0, 'D'}, {"property", required_argument, 0, 'p'}, - {"executor-path", required_argument, 0, 'E'}, {NULL, 0, 0, 0} }; bool listing = false, listing_stat = false; - char *state_file = STATE_FILE; for (;;) { @@ -270,18 +261,12 @@ ifquery_main(int argc, char *argv[]) break; switch (c) { - case 'i': - exec_opts.interfaces_file = optarg; - break; case 'L': listing = true; break; case 'P': match_opts.pretty_print = true; break; - case 'S': - state_file = optarg; - break; case 's': listing_stat = true; break; @@ -291,15 +276,12 @@ ifquery_main(int argc, char *argv[]) case 'p': match_opts.property = optarg; break; - case 'E': - exec_opts.executor_path = optarg; - break; } } - if (!lif_state_read_path(&state, state_file)) + if (!lif_state_read_path(&state, exec_opts.state_file)) { - fprintf(stderr, "%s: could not parse %s\n", argv0, state_file); + fprintf(stderr, "%s: could not parse %s\n", argv0, exec_opts.state_file); return EXIT_FAILURE; } @@ -360,5 +342,5 @@ struct if_applet ifquery_applet = { .desc = "query interface configuration", .main = ifquery_main, .usage = ifquery_usage, - .groups = { &global_option_group, &match_option_group, NULL }, + .groups = { &global_option_group, &match_option_group, &exec_option_group }, }; diff --git a/cmd/ifupdown.c b/cmd/ifupdown.c index 75d9c61..2c4143a 100644 --- a/cmd/ifupdown.c +++ b/cmd/ifupdown.c @@ -25,11 +25,6 @@ #include "cmd/multicall.h" static bool up; -static struct lif_execute_opts exec_opts = { - .executor_path = EXECUTOR_PATH, - .interfaces_file = INTERFACES_FILE, - .state_file = STATE_FILE, -}; void ifupdown_usage(int status) @@ -218,9 +213,6 @@ ifupdown_main(int argc, char *argv[]) case 'V': lif_common_version(); break; - case 'i': - exec_opts.interfaces_file = optarg; - break; case 'a': match_opts.is_auto = true; break; @@ -230,6 +222,9 @@ ifupdown_main(int argc, char *argv[]) case 'X': match_opts.exclude_pattern = optarg; break; + case 'i': + exec_opts.interfaces_file = optarg; + break; case 'S': exec_opts.state_file = optarg; break; diff --git a/cmd/multicall-exec-options.c b/cmd/multicall-exec-options.c new file mode 100644 index 0000000..f043167 --- /dev/null +++ b/cmd/multicall-exec-options.c @@ -0,0 +1,77 @@ +/* + * cmd/multicall-exec-options.c + * Purpose: multi-call binary frontend -- option handling + * + * Copyright (c) 2020 Ariadne Conill + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * This software is provided 'as is' and without any warranty, express or + * implied. In no event shall the authors be liable for any damages arising + * from the use of this software. + */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include "cmd/multicall.h" + +struct lif_execute_opts exec_opts = { + .interfaces_file = INTERFACES_FILE, + .executor_path = EXECUTOR_PATH, + .state_file = STATE_FILE +}; + +static void handle_exec(int short_opt, const struct if_option *opt, const char *opt_arg, const struct if_applet *applet) +{ + (void) opt; + (void) applet; + + switch (short_opt) + { + case 'i': + exec_opts.interfaces_file = opt_arg; + break; + case 'S': + exec_opts.state_file = opt_arg; + break; + case 'n': + exec_opts.mock = true; + exec_opts.verbose = true; + break; + case 'v': + exec_opts.verbose = true; + break; + case 'E': + exec_opts.executor_path = opt_arg; + break; + case 'f': + break; + case 'L': + exec_opts.no_lock = true; + break; + default: + break; + } +} + +static struct if_option exec_options[] = { + {'f', "force", NULL, "force (de)configuration", true, handle_exec}, + {'i', "interfaces", "interfaces FILE", "use FILE for interface definitions", true, handle_exec}, + {'n', "no-act", NULL, "do not actually run any commands", false, handle_exec}, + {'v', "verbose", NULL, "show what commands are being run", false, handle_exec}, + {'E', "executor-path", "executor-path PATH", "use PATH for executor directory", true, handle_exec}, + {'L', "no-lock", NULL, "do not use a lockfile to serialize state changes", false, handle_exec}, + {'S', "state-file", "state-file FILE", "use FILE for state", true, handle_exec}, +}; + +struct if_option_group exec_option_group = { + .desc = "Execution", + .group_size = ARRAY_SIZE(exec_options), + .group = exec_options +}; diff --git a/cmd/multicall-match-options.c b/cmd/multicall-match-options.c new file mode 100644 index 0000000..d89056a --- /dev/null +++ b/cmd/multicall-match-options.c @@ -0,0 +1,57 @@ +/* + * cmd/multicall-match-options.c + * Purpose: multi-call binary frontend -- option handling + * + * Copyright (c) 2020 Ariadne Conill + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * This software is provided 'as is' and without any warranty, express or + * implied. In no event shall the authors be liable for any damages arising + * from the use of this software. + */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include "cmd/multicall.h" + +struct match_options match_opts = {}; + +static void handle_match(int short_opt, const struct if_option *opt, const char *opt_arg, const struct if_applet *applet) +{ + (void) opt; + (void) applet; + + switch (short_opt) + { + case 'a': + match_opts.is_auto = true; + break; + case 'I': + match_opts.include_pattern = opt_arg; + break; + case 'X': + match_opts.exclude_pattern = opt_arg; + break; + default: + break; + } +} + +static struct if_option match_options[] = { + {'a', "auto", NULL, "only match against interfaces hinted as 'auto'", false, handle_match}, + {'I', "include", "include PATTERN", "only match against interfaces matching PATTERN", true, handle_match}, + {'X', "exclude", "exclude PATTERN", "never match against interfaces matching PATTERN", true, handle_match}, +}; + +struct if_option_group match_option_group = { + .desc = "Matching interfaces", + .group_size = ARRAY_SIZE(match_options), + .group = match_options +}; diff --git a/cmd/multicall.h b/cmd/multicall.h index aa5752e..20d2c20 100644 --- a/cmd/multicall.h +++ b/cmd/multicall.h @@ -67,4 +67,7 @@ extern const struct if_option *lookup_option(const struct if_applet *applet, int extern struct if_option_group match_option_group; +extern struct lif_execute_opts exec_opts; +extern struct if_option_group exec_option_group; + #endif