From patchwork Fri Aug 3 17:54:36 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ben Pfaff X-Patchwork-Id: 953348 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=openvswitch.org (client-ip=140.211.169.12; helo=mail.linuxfoundation.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=ovn.org Received: from mail.linuxfoundation.org (mail.linuxfoundation.org [140.211.169.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 41hvph403gz9s0R for ; Sat, 4 Aug 2018 03:55:40 +1000 (AEST) Received: from mail.linux-foundation.org (localhost [127.0.0.1]) by mail.linuxfoundation.org (Postfix) with ESMTP id 77C95D65; Fri, 3 Aug 2018 17:54:49 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@mail.linuxfoundation.org Received: from smtp1.linuxfoundation.org (smtp1.linux-foundation.org [172.17.192.35]) by mail.linuxfoundation.org (Postfix) with ESMTPS id CB434D5D for ; Fri, 3 Aug 2018 17:54:48 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from relay6-d.mail.gandi.net (relay6-d.mail.gandi.net [217.70.183.198]) by smtp1.linuxfoundation.org (Postfix) with ESMTPS id BBC49F1 for ; Fri, 3 Aug 2018 17:54:47 +0000 (UTC) X-Originating-IP: 173.228.112.177 Received: from sigabrt.gateway.sonic.net (173-228-112-177.dsl.dynamic.fusionbroadband.com [173.228.112.177]) (Authenticated sender: blp@ovn.org) by relay6-d.mail.gandi.net (Postfix) with ESMTPSA id 2B5A6C0008; Fri, 3 Aug 2018 17:54:44 +0000 (UTC) From: Ben Pfaff To: dev@openvswitch.org Date: Fri, 3 Aug 2018 10:54:36 -0700 Message-Id: <20180803175437.30472-3-blp@ovn.org> X-Mailer: git-send-email 2.16.1 In-Reply-To: <20180803175437.30472-1-blp@ovn.org> References: <20180803175437.30472-1-blp@ovn.org> X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on smtp1.linux-foundation.org Cc: Ben Pfaff Subject: [ovs-dev] [PATCH v2 2/3] ovn-nbctl: Separate command-line options parsing and interpretation. X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: ovs-dev-bounces@openvswitch.org Errors-To: ovs-dev-bounces@openvswitch.org This will allow selected options to be interpreted locally and others to be passed to the daemon, when the daemon is in use. Signed-off-by: Ben Pfaff --- lib/command-line.c | 108 ++++++++++++++++++++++++++++++++++++++++++++++ lib/command-line.h | 10 +++++ ovn/utilities/ovn-nbctl.c | 39 +++++++++-------- 3 files changed, 138 insertions(+), 19 deletions(-) diff --git a/lib/command-line.c b/lib/command-line.c index 81283314d1f3..235e59b25716 100644 --- a/lib/command-line.c +++ b/lib/command-line.c @@ -52,6 +52,114 @@ ovs_cmdl_long_options_to_short_options(const struct option options[]) return xstrdup(short_options); } +static char * OVS_WARN_UNUSED_RESULT +build_short_options(const struct option *long_options) +{ + char *tmp, *short_options; + + tmp = ovs_cmdl_long_options_to_short_options(long_options); + short_options = xasprintf("+:%s", tmp); + free(tmp); + + return short_options; +} + +static const struct option * +find_option_by_value(const struct option *options, int value) +{ + const struct option *o; + + for (o = options; o->name; o++) { + if (o->val == value) { + return o; + } + } + return NULL; +} + +/* 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, + * returns an error message and zeros the output arguments. */ +char * OVS_WARN_UNUSED_RESULT +ovs_cmdl_parse_all(int argc, char *argv[], + const struct option *options, + struct ovs_cmdl_parsed_option **pop, size_t *n_pop) +{ + /* Count number of options so we can have better assertions later. */ + size_t n_options OVS_UNUSED = 0; + while (options[n_options].name) { + n_options++; + } + + char *short_options = build_short_options(options); + + struct ovs_cmdl_parsed_option *po = NULL; + size_t allocated_po = 0; + size_t n_po = 0; + + char *error; + + optind = 0; + opterr = 0; + for (;;) { + int idx = -1; + int c = getopt_long(argc, argv, short_options, options, &idx); + switch (c) { + case -1: + *pop = po; + *n_pop = n_po; + free(short_options); + return NULL; + + case 0: + /* getopt_long() processed the option directly by setting a flag + * variable. This is probably undesirable for use with this + * function. */ + OVS_NOT_REACHED(); + + case '?': + if (optopt && find_option_by_value(options, optopt)) { + error = xasprintf("option '%s' doesn't allow an argument", + argv[optind - 1]); + } else if (optopt) { + error = xasprintf("unrecognized option '%c'", optopt); + } else { + error = xasprintf("unrecognized option '%s'", + argv[optind - 1]); + } + goto error; + + case ':': + error = xasprintf("option '%s' requires an argument", + argv[optind - 1]); + goto error; + + default: + if (allocated_po >= n_po) { + po = x2nrealloc(po, &allocated_po, sizeof *po); + } + if (idx == -1) { + po[n_po].o = find_option_by_value(options, c); + } else { + ovs_assert(idx >= 0 && idx < n_options); + po[n_po].o = &options[idx]; + } + po[n_po].arg = optarg; + n_po++; + break; + } + } + OVS_NOT_REACHED(); + +error: + free(po); + *pop = NULL; + *n_pop = 0; + free(short_options); + return error; +} + /* Given the 'struct ovs_cmdl_command' array, prints the usage of all commands. */ void ovs_cmdl_print_commands(const struct ovs_cmdl_command commands[]) diff --git a/lib/command-line.h b/lib/command-line.h index 00ace949bad6..9d62dc2501c5 100644 --- a/lib/command-line.h +++ b/lib/command-line.h @@ -45,8 +45,18 @@ struct ovs_cmdl_command { }; char *ovs_cmdl_long_options_to_short_options(const struct option *options); + +struct ovs_cmdl_parsed_option { + const struct option *o; + char *arg; +}; +char *ovs_cmdl_parse_all(int argc, char *argv[], const struct option *, + struct ovs_cmdl_parsed_option **, size_t *) + OVS_WARN_UNUSED_RESULT; + void ovs_cmdl_print_options(const struct option *options); void ovs_cmdl_print_commands(const struct ovs_cmdl_command *commands); + void ovs_cmdl_run_command(struct ovs_cmdl_context *, const struct ovs_cmdl_command[]); void ovs_cmdl_run_command_read_only(struct ovs_cmdl_context *, diff --git a/ovn/utilities/ovn-nbctl.c b/ovn/utilities/ovn-nbctl.c index c625546bb8c0..0659ced378ef 100644 --- a/ovn/utilities/ovn-nbctl.c +++ b/ovn/utilities/ovn-nbctl.c @@ -368,24 +368,23 @@ parse_options(int argc, char *argv[], struct shash *local_options) {NULL, 0, NULL, 0}, }; const int n_global_long_options = ARRAY_SIZE(global_long_options) - 1; - char *short_options; struct option *options; size_t i; - short_options = build_short_options(global_long_options, true); options = append_command_options(global_long_options, OPT_LOCAL); - for (;;) { - int idx; - int c; - - c = getopt_long(argc, argv, short_options, options, &idx); - if (c == -1) { - break; - } + struct ovs_cmdl_parsed_option *parsed_options; + size_t n_po; + char *error = ovs_cmdl_parse_all(argc, argv, options, + &parsed_options, &n_po); + if (error) { + ctl_fatal("%s", error); + } + for (const struct ovs_cmdl_parsed_option *po = parsed_options; + po < &parsed_options[n_po]; po++) { bool handled; - char *error = handle_main_loop_option(c, optarg, &handled); + error = handle_main_loop_option(po->o->val, po->arg, &handled); if (error) { ctl_fatal("%s", error); } @@ -393,9 +392,10 @@ parse_options(int argc, char *argv[], struct shash *local_options) continue; } - switch (c) { + optarg = po->arg; + switch (po->o->val) { case OPT_DB: - db = optarg; + db = po->arg; break; case OPT_NO_SYSLOG: @@ -403,13 +403,13 @@ parse_options(int argc, char *argv[], struct shash *local_options) break; case OPT_LOCAL: - if (shash_find(local_options, options[idx].name)) { + if (shash_find(local_options, po->o->name)) { ctl_fatal("'%s' option specified multiple times", - options[idx].name); + po->o->name); } shash_add_nocopy(local_options, - xasprintf("--%s", options[idx].name), - nullable_xstrdup(optarg)); + xasprintf("--%s", po->o->name), + nullable_xstrdup(po->arg)); break; case 'h': @@ -435,7 +435,7 @@ parse_options(int argc, char *argv[], struct shash *local_options) STREAM_SSL_OPTION_HANDLERS case OPT_BOOTSTRAP_CA_CERT: - stream_ssl_set_ca_cert_file(optarg, true); + stream_ssl_set_ca_cert_file(po->arg, true); break; case '?': @@ -448,7 +448,7 @@ parse_options(int argc, char *argv[], struct shash *local_options) break; } } - free(short_options); + free(parsed_options); if (!db) { db = default_nb_db(); @@ -4984,6 +4984,7 @@ server_parse_options(int argc, char *argv[], struct shash *local_options, int *n_options_p) { static const struct option global_long_options[] = { + VLOG_LONG_OPTIONS, MAIN_LOOP_LONG_OPTIONS, TABLE_LONG_OPTIONS, {NULL, 0, NULL, 0},