[1/4] kvargs: support list value

Message ID 20181009021858.19216-2-thomas@monjalon.net (mailing list archive)
State Superseded, archived
Delegated to: Ferruh Yigit
Headers
Series support more ethdev iterator filters |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/Intel-compilation fail Compilation issues

Commit Message

Thomas Monjalon Oct. 9, 2018, 2:18 a.m. UTC
  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 <thomas@monjalon.net>
---
 lib/librte_kvargs/rte_kvargs.c | 10 ++++++++++
 test/test/test_kvargs.c        | 17 +++++++++++++++++
 2 files changed, 27 insertions(+)
  

Comments

Gaëtan Rivet Oct. 9, 2018, 2:14 p.m. UTC | #1
Hi Thomas,

On Tue, Oct 09, 2018 at 04:18:55AM +0200, Thomas Monjalon wrote:
> 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.
> 

Nice, I was actually planning to do this.

I think it could be useful to also support () and {}, as well as
recursive lists, but it is best to have a first version to support
representor and go from this.

> Signed-off-by: Thomas Monjalon <thomas@monjalon.net>
> ---
>  lib/librte_kvargs/rte_kvargs.c | 10 ++++++++++
>  test/test/test_kvargs.c        | 17 +++++++++++++++++
>  2 files changed, 27 insertions(+)
>
  
Thomas Monjalon Oct. 9, 2018, 2:31 p.m. UTC | #2
09/10/2018 16:14, Gaëtan Rivet:
> Hi Thomas,
> 
> On Tue, Oct 09, 2018 at 04:18:55AM +0200, Thomas Monjalon wrote:
> > 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.
> > 
> 
> Nice, I was actually planning to do this.
> 
> I think it could be useful to also support () and {}, as well as
> recursive lists, but it is best to have a first version to support
> representor and go from this.

Yes, we have no usage of () and {} so far.
  
Stephen Hemminger Oct. 9, 2018, 3:11 p.m. UTC | #3
On Tue, 09 Oct 2018 16:31:24 +0200
Thomas Monjalon <thomas@monjalon.net> wrote:

> 09/10/2018 16:14, Gaëtan Rivet:
> > Hi Thomas,
> > 
> > On Tue, Oct 09, 2018 at 04:18:55AM +0200, Thomas Monjalon wrote:  
> > > 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.
> > >   
> > 
> > Nice, I was actually planning to do this.
> > 
> > I think it could be useful to also support () and {}, as well as
> > recursive lists, but it is best to have a first version to support
> > representor and go from this.  
> 
> Yes, we have no usage of () and {} so far.

This is getting complex enough that doing a real parser maybe necessary.
Why not lex/yacc?

Or better yet go to real syntax like JSON.
  
Thomas Monjalon Oct. 9, 2018, 5:11 p.m. UTC | #4
09/10/2018 17:11, Stephen Hemminger:
> On Tue, 09 Oct 2018 16:31:24 +0200
> Thomas Monjalon <thomas@monjalon.net> wrote:
> 
> > 09/10/2018 16:14, Gaëtan Rivet:
> > > Hi Thomas,
> > > 
> > > On Tue, Oct 09, 2018 at 04:18:55AM +0200, Thomas Monjalon wrote:  
> > > > 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.
> > > >   
> > > 
> > > Nice, I was actually planning to do this.
> > > 
> > > I think it could be useful to also support () and {}, as well as
> > > recursive lists, but it is best to have a first version to support
> > > representor and go from this.  
> > 
> > Yes, we have no usage of () and {} so far.
> 
> This is getting complex enough that doing a real parser maybe necessary.
> Why not lex/yacc?

I don't know how much it fits with our needs for devargs.

> Or better yet go to real syntax like JSON.

JSON is not suitable for one-line string as devargs.
  
Remy Horton Oct. 10, 2018, 1:12 p.m. UTC | #5
On 09/10/2018 16:11, Stephen Hemminger wrote:
> On Tue, 09 Oct 2018 16:31:24 +0200
[..]
> This is getting complex enough that doing a real parser maybe necessary.

I thought the same thing back in April with the port representor 
patchsets. Quickly run out of things to use as delimiters otherwise.


> Why not lex/yacc?

Think Yacc/Bison are vastly overcomplex and a bit of a pain to 
integrate. The sort of config grammar needed here will most likely be 
LL(1), so fairly easy to do by hand anyway.


> Or better yet go to real syntax like JSON.
>
  

Patch

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;