diff mbox

[kvm-unit-tests,v4,05/13] lib/util: add args_parse_keyval

Message ID 1462983171-4208-6-git-send-email-rkrcmar@redhat.com (mailing list archive)
State New, archived
Headers show

Commit Message

Radim Krčmář May 11, 2016, 4:12 p.m. UTC
The function parses command line arguments in "key=val" format, and
treats "key" as "key=1".

Signed-off-by: Radim Kr?má? <rkrcmar@redhat.com>
---
 v4: new

 lib/util.c | 17 +++++++++++++++++
 lib/util.h | 10 ++++++++++
 2 files changed, 27 insertions(+)

Comments

Andrew Jones May 11, 2016, 4:58 p.m. UTC | #1
On Wed, May 11, 2016 at 06:12:47PM +0200, Radim Kr?má? wrote:
> The function parses command line arguments in "key=val" format, and
> treats "key" as "key=1".
> 
> Signed-off-by: Radim Kr?má? <rkrcmar@redhat.com>
> ---
>  v4: new
> 
>  lib/util.c | 17 +++++++++++++++++
>  lib/util.h | 10 ++++++++++
>  2 files changed, 27 insertions(+)
> 
> diff --git a/lib/util.c b/lib/util.c
> index 69b18100c972..8b33d474f4c0 100644
> --- a/lib/util.c
> +++ b/lib/util.c
> @@ -16,3 +16,20 @@ int parse_keyval(char *s, long *val)
>  	*val = atol(p+1);
>  	return p - s;
>  }
> +
> +long args_parse_keyval(int argc, char **argv, char *key)
> +{

I like this better than parse_keyval, except for...

> +	int i;
> +	size_t keylen = strlen(key);
> +
> +	for (i = 1; i < argc; i++) {
> +		if (keylen > 0 && strncmp(argv[i], key, keylen - 1))
> +			continue;
> +		if (argv[i][keylen] == '\0')
> +			return 1;
> +		if (argv[i][keylen] == '=')
> +			return atol(argv[i] + keylen + 1);
> +	}
> +
> +	return 0;

...this. Here we have ambiguous results. Either key was there
and had a value of 0, or wasn't there, and we still get zero.
How about merging the two, and then fixing-up the arm and powerpc
uses of the old one. I'd drop the "args_" from the name then
too.

Thanks,
drew

> +}
> diff --git a/lib/util.h b/lib/util.h
> index 4c4b44132277..d475b058526b 100644
> --- a/lib/util.h
> +++ b/lib/util.h
> @@ -20,4 +20,14 @@
>   */
>  extern int parse_keyval(char *s, long *val);
>  
> +/*
> + * argc and argv are standard C arguments to main().
> + * args_parse_keyval looks for an element of argv that matches the key.
> + * Returns val interpreted as a long if the element is in key=val format.
> + * Returns 1 if the element is key.
> + * Returns 0 otherwise.
> + *
> + */
> +long args_parse_keyval(int argc, char **argv, char *key);
> +
>  #endif
> -- 
> 2.8.2
> 
> --
> To unsubscribe from this list: send the line "unsubscribe kvm" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Radim Krčmář May 11, 2016, 5:19 p.m. UTC | #2
2016-05-11 18:58+0200, Andrew Jones:
> On Wed, May 11, 2016 at 06:12:47PM +0200, Radim Kr?má? wrote:
>> The function parses command line arguments in "key=val" format, and
>> treats "key" as "key=1".
>> 
>> Signed-off-by: Radim Kr?má? <rkrcmar@redhat.com>
>> ---
>>  v4: new
>> 
>>  lib/util.c | 17 +++++++++++++++++
>>  lib/util.h | 10 ++++++++++
>>  2 files changed, 27 insertions(+)
>> 
>> diff --git a/lib/util.c b/lib/util.c
>> index 69b18100c972..8b33d474f4c0 100644
>> --- a/lib/util.c
>> +++ b/lib/util.c
>> @@ -16,3 +16,20 @@ int parse_keyval(char *s, long *val)
>>  	*val = atol(p+1);
>>  	return p - s;
>>  }
>> +
>> +long args_parse_keyval(int argc, char **argv, char *key)
>> +{
> 
> I like this better than parse_keyval, except for...
> 
>> +	int i;
>> +	size_t keylen = strlen(key);
>> +
>> +	for (i = 1; i < argc; i++) {
>> +		if (keylen > 0 && strncmp(argv[i], key, keylen - 1))
>> +			continue;
>> +		if (argv[i][keylen] == '\0')
>> +			return 1;
>> +		if (argv[i][keylen] == '=')
>> +			return atol(argv[i] + keylen + 1);
>> +	}
>> +
>> +	return 0;
> 
> ...this. Here we have ambiguous results. Either key was there
> and had a value of 0, or wasn't there, and we still get zero.

Yes, I implied a boolean key.

> How about merging the two, and then fixing-up the arm and powerpc
> uses of the old one. I'd drop the "args_" from the name then
> too.

Seems reasonable.  I'll make a new series:
 [1/3] add strncmp
 [2/3] add a slightly modified args_parse_keyval,
       int __parse_keyval(int argc, char **argv, char *key, long *val)
       that will return -1/0/1 on !key/key/key=val
 [3/3] replace parse_keyval with renamed __parse_keyval

([2/3] and [3/3] may be squished) The series will depend on your "make
argv[0] the program name" and v5 of mine will depend on it.
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Andrew Jones May 11, 2016, 5:23 p.m. UTC | #3
On Wed, May 11, 2016 at 06:12:47PM +0200, Radim Kr?má? wrote:
> The function parses command line arguments in "key=val" format, and
> treats "key" as "key=1".
> 
> Signed-off-by: Radim Kr?má? <rkrcmar@redhat.com>
> ---
>  v4: new
> 
>  lib/util.c | 17 +++++++++++++++++
>  lib/util.h | 10 ++++++++++
>  2 files changed, 27 insertions(+)
> 
> diff --git a/lib/util.c b/lib/util.c
> index 69b18100c972..8b33d474f4c0 100644
> --- a/lib/util.c
> +++ b/lib/util.c
> @@ -16,3 +16,20 @@ int parse_keyval(char *s, long *val)
>  	*val = atol(p+1);
>  	return p - s;
>  }
> +
> +long args_parse_keyval(int argc, char **argv, char *key)
> +{
> +	int i;
> +	size_t keylen = strlen(key);
> +
> +	for (i = 1; i < argc; i++) {

Forgot to mention that I like this 'i = 1' here because it's
motivation to merge a series[*] I have in flight :-)

[*] http://www.spinics.net/lists/kvm/msg131243.html

> +		if (keylen > 0 && strncmp(argv[i], key, keylen - 1))
> +			continue;
> +		if (argv[i][keylen] == '\0')
> +			return 1;
> +		if (argv[i][keylen] == '=')
> +			return atol(argv[i] + keylen + 1);
> +	}
> +
> +	return 0;
> +}
> diff --git a/lib/util.h b/lib/util.h
> index 4c4b44132277..d475b058526b 100644
> --- a/lib/util.h
> +++ b/lib/util.h
> @@ -20,4 +20,14 @@
>   */
>  extern int parse_keyval(char *s, long *val);
>  
> +/*
> + * argc and argv are standard C arguments to main().
> + * args_parse_keyval looks for an element of argv that matches the key.
> + * Returns val interpreted as a long if the element is in key=val format.
> + * Returns 1 if the element is key.
> + * Returns 0 otherwise.
> + *
> + */
> +long args_parse_keyval(int argc, char **argv, char *key);
> +
>  #endif
> -- 
> 2.8.2
> 
> --
> To unsubscribe from this list: send the line "unsubscribe kvm" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/lib/util.c b/lib/util.c
index 69b18100c972..8b33d474f4c0 100644
--- a/lib/util.c
+++ b/lib/util.c
@@ -16,3 +16,20 @@  int parse_keyval(char *s, long *val)
 	*val = atol(p+1);
 	return p - s;
 }
+
+long args_parse_keyval(int argc, char **argv, char *key)
+{
+	int i;
+	size_t keylen = strlen(key);
+
+	for (i = 1; i < argc; i++) {
+		if (keylen > 0 && strncmp(argv[i], key, keylen - 1))
+			continue;
+		if (argv[i][keylen] == '\0')
+			return 1;
+		if (argv[i][keylen] == '=')
+			return atol(argv[i] + keylen + 1);
+	}
+
+	return 0;
+}
diff --git a/lib/util.h b/lib/util.h
index 4c4b44132277..d475b058526b 100644
--- a/lib/util.h
+++ b/lib/util.h
@@ -20,4 +20,14 @@ 
  */
 extern int parse_keyval(char *s, long *val);
 
+/*
+ * argc and argv are standard C arguments to main().
+ * args_parse_keyval looks for an element of argv that matches the key.
+ * Returns val interpreted as a long if the element is in key=val format.
+ * Returns 1 if the element is key.
+ * Returns 0 otherwise.
+ *
+ */
+long args_parse_keyval(int argc, char **argv, char *key);
+
 #endif