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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxx>
>> ---
>>  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@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [KVM ARM]     [KVM ia64]     [KVM ppc]     [Virtualization Tools]     [Spice Development]     [Libvirt]     [Libvirt Users]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite Questions]     [Linux Kernel]     [Linux SCSI]     [XFree86]
  Powered by Linux