Re: [libvirt] [PATCH 06/10] list: Add helper to convert strings separated by ', ' to array
On 2012年09月06日 00:24, Eric Blake wrote: On 09/04/2012 09:16 AM, Osier Yang wrote: tools/virsh.c: New helper function vshStringToArray. tools/virsh.h: Declare vshStringToArray. tools/virsh-domain.c: use the helper in cmdUndefine. --- tools/virsh-domain.c | 19 ++- tools/virsh.c| 44 tools/virsh.h|1 + 3 files changed, 47 insertions(+), 17 deletions(-) ACK. This is a nice refactor for later use. But down the road, do we want to borrow a leaf from qemu's book, and allow for users to input ',,' for a literal comma that does not separate options? If so, then we should merge in the double comma handling from vshParseSnapshotDiskspec (virsh-snapshot.c) into this function, and have that function also refactored to use this helper. Without something like that, users cannot input a literal comma when listing comma-separated arguments. That will be nice, but seems useless for virsh now, the only two commands which have option accepts multiple values are undefine and pool-list. For domain undefine, the volumes can't have ',' in the name (see the schema), for pool-list, pool types can't have ',' either. Regards, Osier -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH 06/10] list: Add helper to convert strings separated by ', ' to array
On 09/06/2012 08:38 AM, Osier Yang wrote: allow for users to input ',,' for a literal comma that does not separate options? If so, then we should merge in the double comma handling from vshParseSnapshotDiskspec (virsh-snapshot.c) into this function, and have that function also refactored to use this helper. Without something like that, users cannot input a literal comma when listing comma-separated arguments. That will be nice, but seems useless for virsh now, the only two commands which have option accepts multiple values are undefine and pool-list. For domain undefine, the volumes can't have ',' in the name (see the schema), for pool-list, pool types can't have ',' either. I just pointed out that 'virsh snapshot-create-as' is a third virsh command that accepts a list of comma-separated arguments, but where one of the arguments is a file name and therefore uses ',,' escaping. So I'm proposing that we _also_ merge vshParseSnapshotDiskspec into this common helper routine, and thus make double ',,' parsing common throughout virsh (even if most callers can't have ',' in a list element in the first place); but it can be a separate patch. -- Eric Blake ebl...@redhat.com+1-919-301-3266 Libvirt virtualization library http://libvirt.org signature.asc Description: OpenPGP digital signature -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH 06/10] list: Add helper to convert strings separated by ', ' to array
On 09/04/2012 09:16 AM, Osier Yang wrote: tools/virsh.c: New helper function vshStringToArray. tools/virsh.h: Declare vshStringToArray. tools/virsh-domain.c: use the helper in cmdUndefine. --- tools/virsh-domain.c | 19 ++- tools/virsh.c| 44 tools/virsh.h|1 + 3 files changed, 47 insertions(+), 17 deletions(-) ACK. This is a nice refactor for later use. But down the road, do we want to borrow a leaf from qemu's book, and allow for users to input ',,' for a literal comma that does not separate options? If so, then we should merge in the double comma handling from vshParseSnapshotDiskspec (virsh-snapshot.c) into this function, and have that function also refactored to use this helper. Without something like that, users cannot input a literal comma when listing comma-separated arguments. -- Eric Blake ebl...@redhat.com+1-919-301-3266 Libvirt virtualization library http://libvirt.org signature.asc Description: OpenPGP digital signature -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list
[libvirt] [PATCH 06/10] list: Add helper to convert strings separated by ', ' to array
tools/virsh.c: New helper function vshStringToArray. tools/virsh.h: Declare vshStringToArray. tools/virsh-domain.c: use the helper in cmdUndefine. --- tools/virsh-domain.c | 19 ++- tools/virsh.c| 44 tools/virsh.h|1 + 3 files changed, 47 insertions(+), 17 deletions(-) diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c index f0ec742..27d5dc1 100644 --- a/tools/virsh-domain.c +++ b/tools/virsh-domain.c @@ -2443,23 +2443,8 @@ cmdUndefine(vshControl *ctl, const vshCmd *cmd) /* tokenize the string from user and save it's parts into an array */ if (volumes) { -/* count the delimiters */ -volume_tok = volumes; -nvolume_tokens = 1; /* we need at least one member */ -while (*volume_tok) { -if (*(volume_tok++) == ',') -nvolume_tokens++; -} - -volume_tokens = vshCalloc(ctl, nvolume_tokens, sizeof(char *)); - -/* tokenize the input string */ -nvolume_tokens = 0; -volume_tok = volumes; -do { -volume_tokens[nvolume_tokens] = strsep(volume_tok, ,); -nvolume_tokens++; -} while (volume_tok); +if ((nvolume_tokens = vshStringToArray(volumes, volume_tokens)) 0) +goto cleanup; } if ((nvolumes = virXPathNodeSet(./devices/disk, ctxt, diff --git a/tools/virsh.c b/tools/virsh.c index 5cf3237..684f7ca 100644 --- a/tools/virsh.c +++ b/tools/virsh.c @@ -166,6 +166,50 @@ vshPrettyCapacity(unsigned long long val, const char **unit) } } +/* + * Convert the strings separated by ',' into array. The caller + * must free the returned array after use. + * + * Returns the length of the filled array on success, or -1 + * on error. + */ +int +vshStringToArray(char *str, + char ***array) +{ +char *str_tok = NULL; +unsigned int nstr_tokens = 0; +char **arr = NULL; + +/* tokenize the string from user and save it's parts into an array */ +if (str) { +nstr_tokens = 1; + +/* count the delimiters */ +str_tok = str; +while (*str_tok) { +if (*str_tok == ',') +nstr_tokens++; +str_tok++; +} + +if (VIR_ALLOC_N(arr, nstr_tokens) 0) { +virReportOOMError(); +return -1; +} + +/* tokenize the input string */ +nstr_tokens = 0; +str_tok = str; +do { +arr[nstr_tokens] = strsep(str_tok, ,); +nstr_tokens++; +} while (str_tok); +} + +*array = arr; +return nstr_tokens; +} virErrorPtr last_error; diff --git a/tools/virsh.h b/tools/virsh.h index 8923f94..2a9c6a2 100644 --- a/tools/virsh.h +++ b/tools/virsh.h @@ -328,6 +328,7 @@ int vshAskReedit(vshControl *ctl, const char *msg); int vshStreamSink(virStreamPtr st, const char *bytes, size_t nbytes, void *opaque); double vshPrettyCapacity(unsigned long long val, const char **unit); +int vshStringToArray(char *str, char ***array); /* Typedefs, function prototypes for job progress reporting. * There are used by some long lingering commands like -- 1.7.7.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list