From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (xvm-189-124.dc0.ghst.net [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 35BA4A09FF; Wed, 6 Jan 2021 17:18:12 +0100 (CET) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id ACD74140E06; Wed, 6 Jan 2021 17:18:07 +0100 (CET) Received: from mellanox.co.il (mail-il-dmz.mellanox.com [193.47.165.129]) by mails.dpdk.org (Postfix) with ESMTP id 4F14F140D2C for ; Wed, 6 Jan 2021 17:18:05 +0100 (CET) Received: from Internal Mail-Server by MTLPINE1 (envelope-from xuemingl@nvidia.com) with SMTP; 6 Jan 2021 18:18:01 +0200 Received: from nvidia.com (pegasus05.mtr.labs.mlnx [10.210.16.100]) by labmailer.mlnx (8.13.8/8.13.8) with ESMTP id 106GI0Mn017451; Wed, 6 Jan 2021 18:18:01 +0200 From: Xueming Li To: Thomas Monjalon , Ferruh Yigit , Andrew Rybchenko , Olivier Matz , Viacheslav Ovsiienko Cc: dev@dpdk.org, xuemingl@nvidia.com, Asaf Penso Date: Wed, 6 Jan 2021 16:17:34 +0000 Message-Id: <1609949855-23817-9-git-send-email-xuemingl@nvidia.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1609949855-23817-1-git-send-email-xuemingl@nvidia.com> References: <1609949855-23817-1-git-send-email-xuemingl@nvidia.com> In-Reply-To: <1608303356-13089-2-git-send-email-xuemingl@nvidia.com> References: <1608303356-13089-2-git-send-email-xuemingl@nvidia.com> Subject: [dpdk-dev] [PATCH v2 8/9] kvargs: update parser for new representor syntax X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" This patch updates kvargs parser to allow comma in list value: k1=a[1,2]b[3-5] Signed-off-by: Xueming Li --- lib/librte_kvargs/rte_kvargs.c | 83 +++++++++++++++++++++++----------- 1 file changed, 57 insertions(+), 26 deletions(-) diff --git a/lib/librte_kvargs/rte_kvargs.c b/lib/librte_kvargs/rte_kvargs.c index 285081c86c..4053989157 100644 --- a/lib/librte_kvargs/rte_kvargs.c +++ b/lib/librte_kvargs/rte_kvargs.c @@ -5,6 +5,7 @@ #include #include +#include #include @@ -13,15 +14,19 @@ /* * Receive a string with a list of arguments following the pattern * key=value,key=value,... and insert them into the list. - * strtok() is used so the params string will be copied to be modified. + * Params string will be copied to be modified. + * list "[]" and list element splitter ",", "-" is treated as value. + * Supported examples: + * k1=v1,k2=v2 + * v1 + * k1=x[0-1]y[1,3-5,9]z */ static int rte_kvargs_tokenize(struct rte_kvargs *kvlist, const char *params) { unsigned i; - char *str; - char *ctx1 = NULL; - char *ctx2 = NULL; + char *str, *start; + bool in_list = false, end_k = false, end_v = false; /* Copy the const char *params to a modifiable string * to pass to rte_strsplit @@ -32,36 +37,62 @@ rte_kvargs_tokenize(struct rte_kvargs *kvlist, const char *params) /* browse each key/value pair and add it in kvlist */ str = kvlist->str; - while ((str = strtok_r(str, RTE_KVARGS_PAIRS_DELIM, &ctx1)) != NULL) { + start = str; /* start of current key or value */ + while (1) { + switch (*str) { + case '=': /* End of key. */ + end_k = true; + break; + case ',': + /* End of value, skip comma in middle of range */ + if (!in_list) + end_v = true; + break; + case '[': /* Start of list. */ + in_list = true; + break; + case ']': /* End of list. */ + if (in_list) + in_list = false; + break; + case 0: /* End of string */ + end_v = true; + break; + default: + break; + } + + if (!end_k && !end_v) { + /* Continue if not end of key or value. */ + str++; + continue; + } i = kvlist->count; if (i >= RTE_KVARGS_MAX) return -1; - kvlist->pairs[i].key = strtok_r(str, RTE_KVARGS_KV_DELIM, &ctx2); - kvlist->pairs[i].value = strtok_r(NULL, RTE_KVARGS_KV_DELIM, &ctx2); - if (kvlist->pairs[i].key == NULL || - kvlist->pairs[i].value == NULL) - return -1; - - /* Detect list [a,b] to skip comma delimiter in list. */ - str = kvlist->pairs[i].value; - if (str[0] == '[') { - /* Find the end of the list. */ - while (str[strlen(str) - 1] != ']') { - /* Restore the comma erased by strtok_r(). */ - if (ctx1 == NULL || ctx1[0] == '\0') - return -1; /* no closing bracket */ - str[strlen(str)] = ','; - /* Parse until next comma. */ - str = strtok_r(NULL, RTE_KVARGS_PAIRS_DELIM, &ctx1); - if (str == NULL) - return -1; /* no closing bracket */ + if (start == str) /* Empty key or value. */ + start = NULL; + + if (end_k) { + /* Key parsed. */ + kvlist->pairs[i].key = start; + end_k = false; + } else if (end_v) { + /* Allow single key or single value. */ + if (kvlist->pairs[i].key || start) { + kvlist->pairs[i].value = start; + kvlist->count++; } + end_v = false; } - kvlist->count++; - str = NULL; + if (*str == '\0') /* End of string. */ + break; + *str = '\0'; + str++; + start = str; } return 0; -- 2.25.1