From mboxrd@z Thu Jan 1 00:00:00 1970 From: Thomas Monjalon Subject: [PATCH 1/4] kvargs: support list value Date: Tue, 9 Oct 2018 04:18:55 +0200 Message-ID: <20181009021858.19216-2-thomas@monjalon.net> References: <20181009021858.19216-1-thomas@monjalon.net> Mime-Version: 1.0 Content-Transfer-Encoding: 8bit Cc: gaetan.rivet@6wind.com, ophirmu@mellanox.com, ferruh.yigit@intel.com, arybchenko@solarflare.com, olivier.matz@6wind.com, remy.horton@intel.com To: dev@dpdk.org Return-path: Received: from out3-smtp.messagingengine.com (out3-smtp.messagingengine.com [66.111.4.27]) by dpdk.org (Postfix) with ESMTP id 8C54537B4 for ; Tue, 9 Oct 2018 04:19:05 +0200 (CEST) In-Reply-To: <20181009021858.19216-1-thomas@monjalon.net> List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" If a value contains a comma, rte_kvargs_tokenize() will split here. In order to support list syntax [a,b] as value, an extra parsing of the square brackets is added. Signed-off-by: Thomas Monjalon --- lib/librte_kvargs/rte_kvargs.c | 10 ++++++++++ test/test/test_kvargs.c | 17 +++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/lib/librte_kvargs/rte_kvargs.c b/lib/librte_kvargs/rte_kvargs.c index a28f76945..b0774effb 100644 --- a/lib/librte_kvargs/rte_kvargs.c +++ b/lib/librte_kvargs/rte_kvargs.c @@ -44,6 +44,16 @@ rte_kvargs_tokenize(struct rte_kvargs *kvlist, const char *params) kvlist->pairs[i].value == NULL) return -1; + /* Detect list [a,b] to skip comma delimiter in list. */ + if (kvlist->pairs[i].value[0] == '[') { + /* Parse the end of the list. */ + str = strtok_r(NULL, "]", &ctx1); + if (str == NULL) + return -1; /* no closing bracket */ + /* Add back the bracket erased by strtok_t(). */ + *(str - 1) = ']'; + } + kvlist->count++; str = NULL; } diff --git a/test/test/test_kvargs.c b/test/test/test_kvargs.c index e6738624e..cb38b385c 100644 --- a/test/test/test_kvargs.c +++ b/test/test/test_kvargs.c @@ -137,6 +137,22 @@ static int test_valid_kvargs(void) } rte_kvargs_free(kvlist); + /* third test using list as value */ + args = "foo=[0,1],check=value2"; + valid_keys = valid_keys_list; + kvlist = rte_kvargs_parse(args, valid_keys); + if (kvlist == NULL) { + printf("rte_kvargs_parse() error"); + goto fail; + } + count = kvlist->count; + if (count != 2) { + printf("invalid count value %d\n", count); + rte_kvargs_free(kvlist); + goto fail; + } + rte_kvargs_free(kvlist); + return 0; fail: @@ -162,6 +178,7 @@ static int test_invalid_kvargs(void) "foo=1,,foo=2", /* empty key/value */ "foo=1,foo", /* no value */ "foo=1,=2", /* no key */ + "foo=[1,2", /* no closing bracket in value */ ",=", /* also test with a smiley */ NULL }; const char **args; -- 2.19.0