On Thu, Oct 24, 2019 at 10:43:14PM -0700, [email protected] wrote: > From: Aliasgar Ginwala <[email protected]> > > Signed-off-by: Aliasgar Ginwala <[email protected]>
To me, this looks more complicated than necessary, and thus harder to reason about regarding correctness. What about the following? It does not make me think as hard about correctness. However, I have not tested it. -8<--------------------------cut here-------------------------->8-- From: Aliasgar Ginwala <[email protected]> Date: Thu, 24 Oct 2019 22:43:14 -0700 Subject: [PATCH] command-line.c: Support parsing ctl options via env variable Signed-off-by: Aliasgar Ginwala <[email protected]> Signed-off-by: Ben Pfaff <[email protected]> --- lib/command-line.c | 34 ++++++++++++++++++++++++++++++++++ lib/command-line.h | 3 +++ 2 files changed, 37 insertions(+) diff --git a/lib/command-line.c b/lib/command-line.c index 9e000bd28d1a..fa02b49152e5 100644 --- a/lib/command-line.c +++ b/lib/command-line.c @@ -19,6 +19,7 @@ #include <getopt.h> #include <limits.h> #include <stdlib.h> +#include "svec.h" #include "openvswitch/dynamic-string.h" #include "ovs-thread.h" #include "util.h" @@ -77,6 +78,39 @@ find_option_by_value(const struct option *options, int value) return NULL; } +/* Parses options set using environment variable. The caller specifies the + * supported options in environment variable. On success, adds the parsed + * env variables in 'argv', the number of options in 'argc', and returns argv. + */ +char ** +ovs_cmdl_env_parse_all(int *argcp, char *argv[], const char *env_options) +{ + struct svec args = SVEC_EMPTY_INITIALIZER; + + /* argv[0] stays in place. */ + ovs_assert(*argcp > 0); + svec_add(&args, argv[0]); + + /* Anything from the environment variable goes next. */ + if (env_options) { + char *env_copy = xstrdup(env_options); + char *save_ptr = NULL; + for (char *option = strtok_r(env_copy, " ", &save_ptr); + option; option = strtok_r(NULL, " ", &save_ptr)) { + svec_add(&args, option); + } + free(env_copy); + } + + /* Remaining command-line options go at the end. */ + for (int i = 1; i < *argcp; i++) { + svec_add(&args, argv[i]); + } + + *argcp = args.n; + return args.names; +} + /* Parses the command-line options in 'argc' and 'argv'. The caller specifies * the supported options in 'options'. On success, stores the parsed options * in '*pop', the number of options in '*n_pop', and returns NULL. On failure, diff --git a/lib/command-line.h b/lib/command-line.h index 9d62dc2501c5..dc7ec36eae6c 100644 --- a/lib/command-line.h +++ b/lib/command-line.h @@ -54,6 +54,9 @@ char *ovs_cmdl_parse_all(int argc, char *argv[], const struct option *, struct ovs_cmdl_parsed_option **, size_t *) OVS_WARN_UNUSED_RESULT; +char **ovs_cmdl_env_parse_all(int *argcp, char *argv[], + const char *env_options); + void ovs_cmdl_print_options(const struct option *options); void ovs_cmdl_print_commands(const struct ovs_cmdl_command *commands); -- 2.21.0 _______________________________________________ dev mailing list [email protected] https://mail.openvswitch.org/mailman/listinfo/ovs-dev
