From patchwork Wed Oct 16 01:52:34 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: aginwala aginwala X-Patchwork-Id: 1177492 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=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="IXAIfkZ5"; dkim-atps=neutral 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 46tFg317YZz9sP7 for ; Wed, 16 Oct 2019 12:52:46 +1100 (AEDT) Received: from mail.linux-foundation.org (localhost [127.0.0.1]) by mail.linuxfoundation.org (Postfix) with ESMTP id E7F10C5D; Wed, 16 Oct 2019 01:52:41 +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 EE6DEBA9 for ; Wed, 16 Oct 2019 01:52:40 +0000 (UTC) X-Greylist: whitelisted by SQLgrey-1.7.6 Received: from mail-pl1-f193.google.com (mail-pl1-f193.google.com [209.85.214.193]) by smtp1.linuxfoundation.org (Postfix) with ESMTPS id C6C6B14D for ; Wed, 16 Oct 2019 01:52:39 +0000 (UTC) Received: by mail-pl1-f193.google.com with SMTP id d22so10462262pll.7 for ; Tue, 15 Oct 2019 18:52:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=5Q0HVaRHw8UjFwVjHhrMXUIw0kM4ZAdUGJ4qTyq8Sdo=; b=IXAIfkZ5alcI0CaR2rZsKeNmOixggsqDyPUCEIePy6PXdB2fSUFrk3JJ2N9pNXsJxB nOUwwwkhqHM8XVh+WX2xDc/pD43RjrvL5yVejL5vxDQlDxrDZotlpIZ75ZFyP8MlIVZl k6WXhnPYKx+qNuC53Mdu4IoyW7FCcB1WBDBKhTRzPFiQ4Ajj0UwVAwydl1yWSWxJg1mK t4yrjHU/O88hAl0nXutP/rsGVL80FtIAT1k3G3mcSypt5wnjOT/7ECG9HQD8ADzaidxD 5BLQxXogsrJvw4kFS4YL9/z0yfQ82/fcPs6ClIoiq6NYkYQK4PWdCHfmO7sNoVMQp4Qt GqOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=5Q0HVaRHw8UjFwVjHhrMXUIw0kM4ZAdUGJ4qTyq8Sdo=; b=XK+RkDm3jvNfA34aSXREiERMhM5BXu/fXGIHDv4I9+TH9RZrST83fVieAYqo1qZgU1 mVNEaioUt0teeXOzuw20Q9IxWcj3GUdHkaQHr9FHs4wVhXPk5hSUFIpO5xU7+/DQ10Zj +5Kg0jmDusThv5jkn7pRZ4FxBuomF8sAss8xDh7NjS2L4xzr/2VRAwAMa1x3xi5H9Q2D DD29mYhnwDOxCXcoWjesB9xVr0DuA6RCTYMWIvpI3CBly3CLRLnoDmSTX/bXI3+g2riH 6i25QMSegidpK7TVytw8PPF9qg5xEwwx8PBW1r3OOpqycBzEf9E+DOrQBfbPWWCCeR2j 5wEw== X-Gm-Message-State: APjAAAV8gasoAlULTVuWwDytk4osHTj1G/mxrMSe/7gmLCGcV+hKtxB6 GVb685/QA7AMHib+2dwqC61Q6TIl X-Google-Smtp-Source: APXvYqw+NCxRwNE0lLjVYIQSD8D9OYgYfPck/vX/sduZkOTjFM9yot0CmnMGpwkQ6HKIuexhPcBc3w== X-Received: by 2002:a17:902:fe90:: with SMTP id x16mr1587603plm.48.1571190759028; Tue, 15 Oct 2019 18:52:39 -0700 (PDT) Received: from LM-SJC-11015761.corp.ebay.com ([216.113.160.77]) by smtp.gmail.com with ESMTPSA id o15sm526482pjs.14.2019.10.15.18.52.38 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 15 Oct 2019 18:52:38 -0700 (PDT) From: amginwal@gmail.com To: dev@openvswitch.org Date: Tue, 15 Oct 2019 18:52:34 -0700 Message-Id: <20191016015234.10388-1-amginwal@gmail.com> X-Mailer: git-send-email 2.20.1 (Apple Git-117) MIME-Version: 1.0 X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on smtp1.linux-foundation.org Cc: Aliasgar Ginwala Subject: [ovs-dev] [PATCH v1] command-line.c: Support parsing ctl options via env variable 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: , Sender: ovs-dev-bounces@openvswitch.org Errors-To: ovs-dev-bounces@openvswitch.org From: Aliasgar Ginwala Signed-off-by: Aliasgar Ginwala --- lib/command-line.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++ lib/command-line.h | 3 +++ 2 files changed, 53 insertions(+) diff --git a/lib/command-line.c b/lib/command-line.c index 9e000bd28..64a84efa8 100644 --- a/lib/command-line.c +++ b/lib/command-line.c @@ -77,6 +77,56 @@ 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_[], + char *env_options) +{ + char *str1, *token, *saveptr1; + char **argv = NULL; + int i, j, total_args, argc; + int env_argc = 0; + + argc = *argcp; + if (!env_options) { + /* Copy args for parsing as is from argv_ */ + argv = xcalloc(argc + 1, sizeof( *argv_) + 1 ); + for (i = 0; i < argc; i++) { + argv[i] = xstrdup(argv_[i]); + } + return argv; + } + /* Count number of options passed via environment variable */ + for (j = 1, str1 = env_options; ; j++, str1 = NULL) { + token = strtok_r(str1, " ", &saveptr1); + if (token == NULL) { + break; + } + env_argc++; + } + total_args = argc + env_argc + 1; + argv = xcalloc(total_args, sizeof( *argv_) + env_argc + 1); + + /* Construct argv with command line + environment options. */ + for (i = 0, j = 0; i < argc; i++, j++) { + if (j == 1) { + for (j = 1, str1 = env_options; ; j++, str1 = NULL) { + token = strtok_r(str1, " ", &saveptr1); + if (token == NULL) { + break; + } + argv[j] = token; + } + } + argv[j] = xstrdup(argv_[i]); + } + *argcp = j; + return argv; +} + /* 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 9d62dc250..4b8f76da7 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_[], + char *env_options); + void ovs_cmdl_print_options(const struct option *options); void ovs_cmdl_print_commands(const struct ovs_cmdl_command *commands);