[libvirt] [PATCH v2 00/15] Split and enhancement of virsh completer

v2 of: https://www.redhat.com/archives/libvir-list/2019-July/msg01225.html diff to v1: - Rebase and adapt to new checkpoint completer Michal Prívozník (15): tools: s/Nodedev/NodeDevice/ tools: Expose virshCommaStringListComplete() tools: Separate domain related completers into a file tools: Separate storage pool related completers into a file tools: Separate storage volume related completers into a file tools: Separate interface related completers into a file tools: Separate network related completers into a file tools: Separate nodedev related completers into a file tools: Separate nwfilter related completers into a file tools: Separate secret related completers into a file tools: Separate snapshot related completers into a file tools: Separate host related completers into a file tools: Separate checkpoint related completers into a file virsh-completer: Drop needless #include virsh: Introduce virshPoolTypeCompleter tools/Makefile.am | 11 + tools/virsh-completer-checkpoint.c | 78 +++ tools/virsh-completer-checkpoint.h | 27 + tools/virsh-completer-domain.c | 314 +++++++++ tools/virsh-completer-domain.h | 55 ++ tools/virsh-completer-host.c | 148 ++++ tools/virsh-completer-host.h | 31 + tools/virsh-completer-interface.c | 67 ++ tools/virsh-completer-interface.h | 27 + tools/virsh-completer-network.c | 145 ++++ tools/virsh-completer-network.h | 35 + tools/virsh-completer-nodedev.c | 117 ++++ tools/virsh-completer-nodedev.h | 35 + tools/virsh-completer-nwfilter.c | 105 +++ tools/virsh-completer-nwfilter.h | 31 + tools/virsh-completer-pool.c | 120 ++++ tools/virsh-completer-pool.h | 35 + tools/virsh-completer-secret.c | 91 +++ tools/virsh-completer-secret.h | 31 + tools/virsh-completer-snapshot.c | 73 ++ tools/virsh-completer-snapshot.h | 27 + tools/virsh-completer-volume.c | 73 ++ tools/virsh-completer-volume.h | 28 + tools/virsh-completer.c | 1028 +--------------------------- tools/virsh-completer.h | 114 +-- tools/virsh-nodedev.c | 16 +- tools/virsh-nodedev.h | 6 +- tools/virsh-pool.c | 1 + 28 files changed, 1730 insertions(+), 1139 deletions(-) create mode 100644 tools/virsh-completer-checkpoint.c create mode 100644 tools/virsh-completer-checkpoint.h create mode 100644 tools/virsh-completer-domain.c create mode 100644 tools/virsh-completer-domain.h create mode 100644 tools/virsh-completer-host.c create mode 100644 tools/virsh-completer-host.h create mode 100644 tools/virsh-completer-interface.c create mode 100644 tools/virsh-completer-interface.h create mode 100644 tools/virsh-completer-network.c create mode 100644 tools/virsh-completer-network.h create mode 100644 tools/virsh-completer-nodedev.c create mode 100644 tools/virsh-completer-nodedev.h create mode 100644 tools/virsh-completer-nwfilter.c create mode 100644 tools/virsh-completer-nwfilter.h create mode 100644 tools/virsh-completer-pool.c create mode 100644 tools/virsh-completer-pool.h create mode 100644 tools/virsh-completer-secret.c create mode 100644 tools/virsh-completer-secret.h create mode 100644 tools/virsh-completer-snapshot.c create mode 100644 tools/virsh-completer-snapshot.h create mode 100644 tools/virsh-completer-volume.c create mode 100644 tools/virsh-completer-volume.h -- 2.21.0

The proper name is [vir|virsh]NodeDevice* and not Nodedev. Fortunately, there are only handful of offenders. Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- tools/virsh-completer.c | 14 +++++++------- tools/virsh-completer.h | 12 ++++++------ tools/virsh-nodedev.c | 16 ++++++++-------- tools/virsh-nodedev.h | 6 +++--- 4 files changed, 24 insertions(+), 24 deletions(-) diff --git a/tools/virsh-completer.c b/tools/virsh-completer.c index 4fa6b19225..957db16003 100644 --- a/tools/virsh-completer.c +++ b/tools/virsh-completer.c @@ -1004,9 +1004,9 @@ virshDomainInterfaceStateCompleter(vshControl *ctl, char ** -virshNodedevEventNameCompleter(vshControl *ctl ATTRIBUTE_UNUSED, - const vshCmd *cmd ATTRIBUTE_UNUSED, - unsigned int flags) +virshNodeDeviceEventNameCompleter(vshControl *ctl ATTRIBUTE_UNUSED, + const vshCmd *cmd ATTRIBUTE_UNUSED, + unsigned int flags) { size_t i = 0; char **ret = NULL; @@ -1018,7 +1018,7 @@ virshNodedevEventNameCompleter(vshControl *ctl ATTRIBUTE_UNUSED, return NULL; for (i = 0; i < VIR_NODE_DEVICE_EVENT_ID_LAST; i++) { - if (VIR_STRDUP(tmp[i], virshNodedevEventCallbacks[i].name) < 0) + if (VIR_STRDUP(tmp[i], virshNodeDeviceEventCallbacks[i].name) < 0) return NULL; } @@ -1028,9 +1028,9 @@ virshNodedevEventNameCompleter(vshControl *ctl ATTRIBUTE_UNUSED, char ** -virshNodedevCapabilityNameCompleter(vshControl *ctl, - const vshCmd *cmd, - unsigned int flags) +virshNodeDeviceCapabilityNameCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags) { VIR_AUTOSTRINGLIST tmp = NULL; const char *cap_str = NULL; diff --git a/tools/virsh-completer.h b/tools/virsh-completer.h index 2e54dddc05..fb41c2c89f 100644 --- a/tools/virsh-completer.h +++ b/tools/virsh-completer.h @@ -106,13 +106,13 @@ char ** virshDomainInterfaceStateCompleter(vshControl *ctl, const vshCmd *cmd, unsigned int flags); -char ** virshNodedevEventNameCompleter(vshControl *ctl, - const vshCmd *cmd, - unsigned int flags); +char ** virshNodeDeviceEventNameCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags); -char ** virshNodedevCapabilityNameCompleter(vshControl *ctl, - const vshCmd *cmd, - unsigned int flags); +char ** virshNodeDeviceCapabilityNameCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags); char ** virshDomainDeviceAliasCompleter(vshControl *ctl, const vshCmd *cmd, diff --git a/tools/virsh-nodedev.c b/tools/virsh-nodedev.c index e8372a4daf..b21e2e2fcc 100644 --- a/tools/virsh-nodedev.c +++ b/tools/virsh-nodedev.c @@ -375,7 +375,7 @@ static const vshCmdOptDef opts_node_list_devices[] = { }, {.name = "cap", .type = VSH_OT_STRING, - .completer = virshNodedevCapabilityNameCompleter, + .completer = virshNodeDeviceCapabilityNameCompleter, .help = N_("capability names, separated by comma") }, {.name = NULL} @@ -775,7 +775,7 @@ struct virshNodeDeviceEventData { bool loop; bool timestamp; int count; - virshNodedevEventCallback *cb; + virshNodeDeviceEventCallback *cb; }; typedef struct virshNodeDeviceEventData virshNodeDeviceEventData; @@ -841,12 +841,12 @@ vshEventGenericPrint(virConnectPtr conn ATTRIBUTE_UNUSED, vshEventDone(data->ctl); } -virshNodedevEventCallback virshNodedevEventCallbacks[] = { +virshNodeDeviceEventCallback virshNodeDeviceEventCallbacks[] = { { "lifecycle", VIR_NODE_DEVICE_EVENT_CALLBACK(vshEventLifecyclePrint), }, { "update", vshEventGenericPrint, } }; -verify(VIR_NODE_DEVICE_EVENT_ID_LAST == ARRAY_CARDINALITY(virshNodedevEventCallbacks)); +verify(VIR_NODE_DEVICE_EVENT_ID_LAST == ARRAY_CARDINALITY(virshNodeDeviceEventCallbacks)); static const vshCmdInfo info_node_device_event[] = { @@ -867,7 +867,7 @@ static const vshCmdOptDef opts_node_device_event[] = { }, {.name = "event", .type = VSH_OT_STRING, - .completer = virshNodedevEventNameCompleter, + .completer = virshNodeDeviceEventNameCompleter, .help = N_("which event type to wait for") }, {.name = "loop", @@ -906,7 +906,7 @@ cmdNodeDeviceEvent(vshControl *ctl, const vshCmd *cmd) size_t i; for (i = 0; i < VIR_NODE_DEVICE_EVENT_ID_LAST; i++) - vshPrint(ctl, "%s\n", virshNodedevEventCallbacks[i].name); + vshPrint(ctl, "%s\n", virshNodeDeviceEventCallbacks[i].name); return true; } @@ -918,7 +918,7 @@ cmdNodeDeviceEvent(vshControl *ctl, const vshCmd *cmd) } for (event = 0; event < VIR_NODE_DEVICE_EVENT_ID_LAST; event++) - if (STREQ(eventName, virshNodedevEventCallbacks[event].name)) + if (STREQ(eventName, virshNodeDeviceEventCallbacks[event].name)) break; if (event == VIR_NODE_DEVICE_EVENT_ID_LAST) { vshError(ctl, _("unknown event type %s"), eventName); @@ -929,7 +929,7 @@ cmdNodeDeviceEvent(vshControl *ctl, const vshCmd *cmd) data.loop = vshCommandOptBool(cmd, "loop"); data.timestamp = vshCommandOptBool(cmd, "timestamp"); data.count = 0; - data.cb = &virshNodedevEventCallbacks[event]; + data.cb = &virshNodeDeviceEventCallbacks[event]; if (vshCommandOptTimeoutToMs(ctl, cmd, &timeout) < 0) return false; if (vshCommandOptStringReq(ctl, cmd, "device", &device_value) < 0) diff --git a/tools/virsh-nodedev.h b/tools/virsh-nodedev.h index 9691b0db4d..95ff3bf526 100644 --- a/tools/virsh-nodedev.h +++ b/tools/virsh-nodedev.h @@ -22,12 +22,12 @@ #include "virsh.h" -struct virshNodedevEventCallback { +struct virshNodeDeviceEventCallback { const char *name; virConnectNodeDeviceEventGenericCallback cb; }; -typedef struct virshNodedevEventCallback virshNodedevEventCallback; +typedef struct virshNodeDeviceEventCallback virshNodeDeviceEventCallback; -extern virshNodedevEventCallback virshNodedevEventCallbacks[]; +extern virshNodeDeviceEventCallback virshNodeDeviceEventCallbacks[]; extern const vshCmdDef nodedevCmds[]; -- 2.21.0

In next commits the virsh-completer.c is going to be split into smaller files. Expose virshCommaStringListComplete() so that it can still be used from those new files. Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- tools/virsh-completer.c | 2 +- tools/virsh-completer.h | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/tools/virsh-completer.c b/tools/virsh-completer.c index 957db16003..476c3a95c4 100644 --- a/tools/virsh-completer.c +++ b/tools/virsh-completer.c @@ -96,7 +96,7 @@ * Returns: string list of completions on success, * NULL otherwise. */ -static char ** +char ** virshCommaStringListComplete(const char *input, const char **options) { diff --git a/tools/virsh-completer.h b/tools/virsh-completer.h index fb41c2c89f..f7e638d67f 100644 --- a/tools/virsh-completer.h +++ b/tools/virsh-completer.h @@ -38,6 +38,9 @@ char ** virshDomainDiskTargetCompleter(vshControl *ctl, const vshCmd *cmd, unsigned int flags); +char ** virshCommaStringListComplete(const char *input, + const char **options); + char ** virshStoragePoolNameCompleter(vshControl *ctl, const vshCmd *cmd, unsigned int flags); -- 2.21.0

Mixing all completers in one file does not support maintainability. Separate those completers which relate to domains (e.g. they complete various domain aspects) into virsh-completer-domain.c. Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- tools/Makefile.am | 1 + tools/virsh-completer-domain.c | 314 +++++++++++++++++++++++++++++++++ tools/virsh-completer-domain.h | 55 ++++++ tools/virsh-completer.c | 285 ------------------------------ tools/virsh-completer.h | 32 +--- 5 files changed, 371 insertions(+), 316 deletions(-) create mode 100644 tools/virsh-completer-domain.c create mode 100644 tools/virsh-completer-domain.h diff --git a/tools/Makefile.am b/tools/Makefile.am index 2807b9f6fd..4a3f72b357 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am @@ -218,6 +218,7 @@ virsh_SOURCES = \ virsh.c virsh.h \ virsh-checkpoint.c virsh-checkpoint.h \ virsh-completer.c virsh-completer.h \ + virsh-completer-domain.c virsh-completer-domain.h \ virsh-console.c virsh-console.h \ virsh-domain.c virsh-domain.h \ virsh-domain-monitor.c virsh-domain-monitor.h \ diff --git a/tools/virsh-completer-domain.c b/tools/virsh-completer-domain.c new file mode 100644 index 0000000000..9c10e38aba --- /dev/null +++ b/tools/virsh-completer-domain.c @@ -0,0 +1,314 @@ +/* + * virsh-completer-domain.c: virsh completer callbacks related to domains + * + * Copyright (C) 2019 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * <http://www.gnu.org/licenses/>. + */ + +#include <config.h> + +#include "virsh-completer-domain.h" +#include "viralloc.h" +#include "virmacaddr.h" +#include "virsh-domain.h" +#include "virsh-util.h" +#include "virsh.h" +#include "virstring.h" +#include "virxml.h" + +char ** +virshDomainNameCompleter(vshControl *ctl, + const vshCmd *cmd ATTRIBUTE_UNUSED, + unsigned int flags) +{ + virshControlPtr priv = ctl->privData; + virDomainPtr *domains = NULL; + int ndomains = 0; + size_t i = 0; + char **ret = NULL; + VIR_AUTOSTRINGLIST tmp = NULL; + + virCheckFlags(VIR_CONNECT_LIST_DOMAINS_ACTIVE | + VIR_CONNECT_LIST_DOMAINS_INACTIVE | + VIR_CONNECT_LIST_DOMAINS_OTHER | + VIR_CONNECT_LIST_DOMAINS_PAUSED | + VIR_CONNECT_LIST_DOMAINS_PERSISTENT | + VIR_CONNECT_LIST_DOMAINS_RUNNING | + VIR_CONNECT_LIST_DOMAINS_SHUTOFF, + NULL); + + if (!priv->conn || virConnectIsAlive(priv->conn) <= 0) + return NULL; + + if ((ndomains = virConnectListAllDomains(priv->conn, &domains, flags)) < 0) + return NULL; + + if (VIR_ALLOC_N(tmp, ndomains + 1) < 0) + goto cleanup; + + for (i = 0; i < ndomains; i++) { + const char *name = virDomainGetName(domains[i]); + + if (VIR_STRDUP(tmp[i], name) < 0) + goto cleanup; + } + + VIR_STEAL_PTR(ret, tmp); + + cleanup: + for (i = 0; i < ndomains; i++) + virshDomainFree(domains[i]); + VIR_FREE(domains); + return ret; +} + + +char ** +virshDomainInterfaceCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags) +{ + virshControlPtr priv = ctl->privData; + VIR_AUTOPTR(xmlDoc) xmldoc = NULL; + VIR_AUTOPTR(xmlXPathContext) ctxt = NULL; + int ninterfaces; + VIR_AUTOFREE(xmlNodePtr *) interfaces = NULL; + size_t i; + unsigned int domainXMLFlags = 0; + char **ret = NULL; + VIR_AUTOSTRINGLIST tmp = NULL; + + virCheckFlags(VIRSH_DOMAIN_INTERFACE_COMPLETER_MAC, NULL); + + if (!priv->conn || virConnectIsAlive(priv->conn) <= 0) + return NULL; + + if (vshCommandOptBool(cmd, "config")) + domainXMLFlags = VIR_DOMAIN_XML_INACTIVE; + + if (virshDomainGetXML(ctl, cmd, domainXMLFlags, &xmldoc, &ctxt) < 0) + return NULL; + + ninterfaces = virXPathNodeSet("./devices/interface", ctxt, &interfaces); + if (ninterfaces < 0) + return NULL; + + if (VIR_ALLOC_N(tmp, ninterfaces + 1) < 0) + return NULL; + + for (i = 0; i < ninterfaces; i++) { + ctxt->node = interfaces[i]; + + if (!(flags & VIRSH_DOMAIN_INTERFACE_COMPLETER_MAC) && + (tmp[i] = virXPathString("string(./target/@dev)", ctxt))) + continue; + + /* In case we are dealing with inactive domain XML there's no + * <target dev=''/>. Offer MAC addresses then. */ + if (!(tmp[i] = virXPathString("string(./mac/@address)", ctxt))) + return NULL; + } + + VIR_STEAL_PTR(ret, tmp); + return ret; +} + + +char ** +virshDomainDiskTargetCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags) +{ + virshControlPtr priv = ctl->privData; + VIR_AUTOPTR(xmlDoc) xmldoc = NULL; + VIR_AUTOPTR(xmlXPathContext) ctxt = NULL; + VIR_AUTOFREE(xmlNodePtr *) disks = NULL; + int ndisks; + size_t i; + VIR_AUTOSTRINGLIST tmp = NULL; + char **ret = NULL; + + virCheckFlags(0, NULL); + + if (!priv->conn || virConnectIsAlive(priv->conn) <= 0) + return NULL; + + if (virshDomainGetXML(ctl, cmd, 0, &xmldoc, &ctxt) < 0) + return NULL; + + ndisks = virXPathNodeSet("./devices/disk", ctxt, &disks); + if (ndisks < 0) + return NULL; + + if (VIR_ALLOC_N(tmp, ndisks + 1) < 0) + return NULL; + + for (i = 0; i < ndisks; i++) { + ctxt->node = disks[i]; + if (!(tmp[i] = virXPathString("string(./target/@dev)", ctxt))) + return NULL; + } + + VIR_STEAL_PTR(ret, tmp); + return ret; +} + + +char ** +virshDomainEventNameCompleter(vshControl *ctl ATTRIBUTE_UNUSED, + const vshCmd *cmd ATTRIBUTE_UNUSED, + unsigned int flags) +{ + size_t i = 0; + char **ret = NULL; + VIR_AUTOSTRINGLIST tmp = NULL; + + virCheckFlags(0, NULL); + + if (VIR_ALLOC_N(tmp, VIR_DOMAIN_EVENT_ID_LAST + 1) < 0) + return NULL; + + for (i = 0; i < VIR_DOMAIN_EVENT_ID_LAST; i++) { + if (VIR_STRDUP(tmp[i], virshDomainEventCallbacks[i].name) < 0) + return NULL; + } + + VIR_STEAL_PTR(ret, tmp); + return ret; +} + + +char ** +virshDomainInterfaceStateCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags) +{ + virshControlPtr priv = ctl->privData; + const char *iface = NULL; + char **ret = NULL; + VIR_AUTOPTR(xmlDoc) xml = NULL; + VIR_AUTOPTR(xmlXPathContext) ctxt = NULL; + virMacAddr macaddr; + char macstr[VIR_MAC_STRING_BUFLEN] = ""; + int ninterfaces; + VIR_AUTOFREE(xmlNodePtr *) interfaces = NULL; + VIR_AUTOFREE(char *) xpath = NULL; + VIR_AUTOFREE(char *) state = NULL; + VIR_AUTOSTRINGLIST tmp = NULL; + + virCheckFlags(0, NULL); + + if (!priv->conn || virConnectIsAlive(priv->conn) <= 0) + return NULL; + + if (virshDomainGetXML(ctl, cmd, flags, &xml, &ctxt) < 0) + return NULL; + + if (vshCommandOptStringReq(ctl, cmd, "interface", &iface) < 0) + return NULL; + + /* normalize the mac addr */ + if (virMacAddrParse(iface, &macaddr) == 0) + virMacAddrFormat(&macaddr, macstr); + + if (virAsprintf(&xpath, "/domain/devices/interface[(mac/@address = '%s') or " + " (target/@dev = '%s')]", + macstr, iface) < 0) + return NULL; + + if ((ninterfaces = virXPathNodeSet(xpath, ctxt, &interfaces)) < 0) + return NULL; + + if (ninterfaces != 1) + return NULL; + + ctxt->node = interfaces[0]; + + if (VIR_ALLOC_N(tmp, 2) < 0) + return NULL; + + if ((state = virXPathString("string(./link/@state)", ctxt)) && + STREQ(state, "down")) { + if (VIR_STRDUP(tmp[0], "up") < 0) + return NULL; + } else { + if (VIR_STRDUP(tmp[0], "down") < 0) + return NULL; + } + + VIR_STEAL_PTR(ret, tmp); + return ret; +} + + +char ** +virshDomainDeviceAliasCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags) +{ + virshControlPtr priv = ctl->privData; + VIR_AUTOPTR(xmlDoc) xmldoc = NULL; + VIR_AUTOPTR(xmlXPathContext) ctxt = NULL; + int naliases; + VIR_AUTOFREE(xmlNodePtr *) aliases = NULL; + size_t i; + unsigned int domainXMLFlags = 0; + char **ret = NULL; + VIR_AUTOSTRINGLIST tmp = NULL; + + virCheckFlags(0, NULL); + + if (!priv->conn || virConnectIsAlive(priv->conn) <= 0) + return NULL; + + if (vshCommandOptBool(cmd, "config")) + domainXMLFlags = VIR_DOMAIN_XML_INACTIVE; + + if (virshDomainGetXML(ctl, cmd, domainXMLFlags, &xmldoc, &ctxt) < 0) + return NULL; + + naliases = virXPathNodeSet("./devices//alias/@name", ctxt, &aliases); + if (naliases < 0) + return NULL; + + if (VIR_ALLOC_N(tmp, naliases + 1) < 0) + return NULL; + + for (i = 0; i < naliases; i++) { + if (!(tmp[i] = virXMLNodeContentString(aliases[i]))) + return NULL; + } + + VIR_STEAL_PTR(ret, tmp); + return ret; +} + + +char ** +virshDomainShutdownModeCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags) +{ + const char *modes[] = {"acpi", "agent", "initctl", "signal", "paravirt", NULL}; + const char *mode = NULL; + + virCheckFlags(0, NULL); + + if (vshCommandOptStringQuiet(ctl, cmd, "mode", &mode) < 0) + return NULL; + + return virshCommaStringListComplete(mode, modes); +} diff --git a/tools/virsh-completer-domain.h b/tools/virsh-completer-domain.h new file mode 100644 index 0000000000..083ab327cc --- /dev/null +++ b/tools/virsh-completer-domain.h @@ -0,0 +1,55 @@ +/* + * virsh-completer-domain.h: virsh completer callbacks related to domains + * + * Copyright (C) 2019 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * <http://www.gnu.org/licenses/>. + */ + +#pragma once + +#include "vsh.h" + +char ** virshDomainNameCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags); + +enum { + VIRSH_DOMAIN_INTERFACE_COMPLETER_MAC = 1 << 0, /* Return just MACs */ +}; + +char ** virshDomainInterfaceCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags); + +char ** virshDomainDiskTargetCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags); + +char ** virshDomainEventNameCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags); + +char ** virshDomainInterfaceStateCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags); + +char ** virshDomainDeviceAliasCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags); + +char ** virshDomainShutdownModeCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags); diff --git a/tools/virsh-completer.c b/tools/virsh-completer.c index 476c3a95c4..787bac08f6 100644 --- a/tools/virsh-completer.c +++ b/tools/virsh-completer.c @@ -143,144 +143,6 @@ virshCommaStringListComplete(const char *input, } -char ** -virshDomainNameCompleter(vshControl *ctl, - const vshCmd *cmd ATTRIBUTE_UNUSED, - unsigned int flags) -{ - virshControlPtr priv = ctl->privData; - virDomainPtr *domains = NULL; - int ndomains = 0; - size_t i = 0; - char **ret = NULL; - VIR_AUTOSTRINGLIST tmp = NULL; - - virCheckFlags(VIR_CONNECT_LIST_DOMAINS_ACTIVE | - VIR_CONNECT_LIST_DOMAINS_INACTIVE | - VIR_CONNECT_LIST_DOMAINS_OTHER | - VIR_CONNECT_LIST_DOMAINS_PAUSED | - VIR_CONNECT_LIST_DOMAINS_PERSISTENT | - VIR_CONNECT_LIST_DOMAINS_RUNNING | - VIR_CONNECT_LIST_DOMAINS_SHUTOFF, - NULL); - - if (!priv->conn || virConnectIsAlive(priv->conn) <= 0) - return NULL; - - if ((ndomains = virConnectListAllDomains(priv->conn, &domains, flags)) < 0) - return NULL; - - if (VIR_ALLOC_N(tmp, ndomains + 1) < 0) - goto cleanup; - - for (i = 0; i < ndomains; i++) { - const char *name = virDomainGetName(domains[i]); - - if (VIR_STRDUP(tmp[i], name) < 0) - goto cleanup; - } - - VIR_STEAL_PTR(ret, tmp); - - cleanup: - for (i = 0; i < ndomains; i++) - virshDomainFree(domains[i]); - VIR_FREE(domains); - return ret; -} - - -char ** -virshDomainInterfaceCompleter(vshControl *ctl, - const vshCmd *cmd, - unsigned int flags) -{ - virshControlPtr priv = ctl->privData; - VIR_AUTOPTR(xmlDoc) xmldoc = NULL; - VIR_AUTOPTR(xmlXPathContext) ctxt = NULL; - int ninterfaces; - VIR_AUTOFREE(xmlNodePtr *) interfaces = NULL; - size_t i; - unsigned int domainXMLFlags = 0; - char **ret = NULL; - VIR_AUTOSTRINGLIST tmp = NULL; - - virCheckFlags(VIRSH_DOMAIN_INTERFACE_COMPLETER_MAC, NULL); - - if (!priv->conn || virConnectIsAlive(priv->conn) <= 0) - return NULL; - - if (vshCommandOptBool(cmd, "config")) - domainXMLFlags = VIR_DOMAIN_XML_INACTIVE; - - if (virshDomainGetXML(ctl, cmd, domainXMLFlags, &xmldoc, &ctxt) < 0) - return NULL; - - ninterfaces = virXPathNodeSet("./devices/interface", ctxt, &interfaces); - if (ninterfaces < 0) - return NULL; - - if (VIR_ALLOC_N(tmp, ninterfaces + 1) < 0) - return NULL; - - for (i = 0; i < ninterfaces; i++) { - ctxt->node = interfaces[i]; - - if (!(flags & VIRSH_DOMAIN_INTERFACE_COMPLETER_MAC) && - (tmp[i] = virXPathString("string(./target/@dev)", ctxt))) - continue; - - /* In case we are dealing with inactive domain XML there's no - * <target dev=''/>. Offer MAC addresses then. */ - if (!(tmp[i] = virXPathString("string(./mac/@address)", ctxt))) - return NULL; - } - - VIR_STEAL_PTR(ret, tmp); - return ret; -} - - -char ** -virshDomainDiskTargetCompleter(vshControl *ctl, - const vshCmd *cmd, - unsigned int flags) -{ - virshControlPtr priv = ctl->privData; - VIR_AUTOPTR(xmlDoc) xmldoc = NULL; - VIR_AUTOPTR(xmlXPathContext) ctxt = NULL; - VIR_AUTOFREE(xmlNodePtr *) disks = NULL; - int ndisks; - size_t i; - VIR_AUTOSTRINGLIST tmp = NULL; - char **ret = NULL; - - virCheckFlags(0, NULL); - - if (!priv->conn || virConnectIsAlive(priv->conn) <= 0) - return NULL; - - if (virshDomainGetXML(ctl, cmd, 0, &xmldoc, &ctxt) < 0) - return NULL; - - ndisks = virXPathNodeSet("./devices/disk", ctxt, &disks); - if (ndisks < 0) - return NULL; - - if (VIR_ALLOC_N(tmp, ndisks + 1) < 0) - return NULL; - - for (i = 0; i < ndisks; i++) { - ctxt->node = disks[i]; - if (!(tmp[i] = virXPathString("string(./target/@dev)", ctxt))) - return NULL; - } - - VIR_STEAL_PTR(ret, tmp); - return ret; -} - - char ** virshStoragePoolNameCompleter(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED, @@ -892,30 +754,6 @@ virshSecretEventNameCompleter(vshControl *ctl ATTRIBUTE_UNUSED, } -char ** -virshDomainEventNameCompleter(vshControl *ctl ATTRIBUTE_UNUSED, - const vshCmd *cmd ATTRIBUTE_UNUSED, - unsigned int flags) -{ - size_t i = 0; - char **ret = NULL; - VIR_AUTOSTRINGLIST tmp = NULL; - - virCheckFlags(0, NULL); - - if (VIR_ALLOC_N(tmp, VIR_DOMAIN_EVENT_ID_LAST + 1) < 0) - return NULL; - - for (i = 0; i < VIR_DOMAIN_EVENT_ID_LAST; i++) { - if (VIR_STRDUP(tmp[i], virshDomainEventCallbacks[i].name) < 0) - return NULL; - } - - VIR_STEAL_PTR(ret, tmp); - return ret; -} - - char ** virshPoolEventNameCompleter(vshControl *ctl ATTRIBUTE_UNUSED, const vshCmd *cmd ATTRIBUTE_UNUSED, @@ -940,69 +778,6 @@ virshPoolEventNameCompleter(vshControl *ctl ATTRIBUTE_UNUSED, } -char ** -virshDomainInterfaceStateCompleter(vshControl *ctl, - const vshCmd *cmd, - unsigned int flags) -{ - virshControlPtr priv = ctl->privData; - const char *iface = NULL; - char **ret = NULL; - VIR_AUTOPTR(xmlDoc) xml = NULL; - VIR_AUTOPTR(xmlXPathContext) ctxt = NULL; - virMacAddr macaddr; - char macstr[VIR_MAC_STRING_BUFLEN] = ""; - int ninterfaces; - VIR_AUTOFREE(xmlNodePtr *) interfaces = NULL; - VIR_AUTOFREE(char *) xpath = NULL; - VIR_AUTOFREE(char *) state = NULL; - VIR_AUTOSTRINGLIST tmp = NULL; - - virCheckFlags(0, NULL); - - if (!priv->conn || virConnectIsAlive(priv->conn) <= 0) - return NULL; - - if (virshDomainGetXML(ctl, cmd, flags, &xml, &ctxt) < 0) - return NULL; - - if (vshCommandOptStringReq(ctl, cmd, "interface", &iface) < 0) - return NULL; - - /* normalize the mac addr */ - if (virMacAddrParse(iface, &macaddr) == 0) - virMacAddrFormat(&macaddr, macstr); - - if (virAsprintf(&xpath, "/domain/devices/interface[(mac/@address = '%s') or " - " (target/@dev = '%s')]", - macstr, iface) < 0) - return NULL; - - if ((ninterfaces = virXPathNodeSet(xpath, ctxt, &interfaces)) < 0) - return NULL; - - if (ninterfaces != 1) - return NULL; - - ctxt->node = interfaces[0]; - - if (VIR_ALLOC_N(tmp, 2) < 0) - return NULL; - - if ((state = virXPathString("string(./link/@state)", ctxt)) && - STREQ(state, "down")) { - if (VIR_STRDUP(tmp[0], "up") < 0) - return NULL; - } else { - if (VIR_STRDUP(tmp[0], "down") < 0) - return NULL; - } - - VIR_STEAL_PTR(ret, tmp); - return ret; -} - - char ** virshNodeDeviceEventNameCompleter(vshControl *ctl ATTRIBUTE_UNUSED, const vshCmd *cmd ATTRIBUTE_UNUSED, @@ -1094,63 +869,3 @@ virshCellnoCompleter(vshControl *ctl, VIR_STEAL_PTR(ret, tmp); return ret; } - - -char ** -virshDomainDeviceAliasCompleter(vshControl *ctl, - const vshCmd *cmd, - unsigned int flags) -{ - virshControlPtr priv = ctl->privData; - VIR_AUTOPTR(xmlDoc) xmldoc = NULL; - VIR_AUTOPTR(xmlXPathContext) ctxt = NULL; - int naliases; - VIR_AUTOFREE(xmlNodePtr *) aliases = NULL; - size_t i; - unsigned int domainXMLFlags = 0; - char **ret = NULL; - VIR_AUTOSTRINGLIST tmp = NULL; - - virCheckFlags(0, NULL); - - if (!priv->conn || virConnectIsAlive(priv->conn) <= 0) - return NULL; - - if (vshCommandOptBool(cmd, "config")) - domainXMLFlags = VIR_DOMAIN_XML_INACTIVE; - - if (virshDomainGetXML(ctl, cmd, domainXMLFlags, &xmldoc, &ctxt) < 0) - return NULL; - - naliases = virXPathNodeSet("./devices//alias/@name", ctxt, &aliases); - if (naliases < 0) - return NULL; - - if (VIR_ALLOC_N(tmp, naliases + 1) < 0) - return NULL; - - for (i = 0; i < naliases; i++) { - if (!(tmp[i] = virXMLNodeContentString(aliases[i]))) - return NULL; - } - - VIR_STEAL_PTR(ret, tmp); - return ret; -} - - -char ** -virshDomainShutdownModeCompleter(vshControl *ctl, - const vshCmd *cmd, - unsigned int flags) -{ - const char *modes[] = {"acpi", "agent", "initctl", "signal", "paravirt", NULL}; - const char *mode = NULL; - - virCheckFlags(0, NULL); - - if (vshCommandOptStringQuiet(ctl, cmd, "mode", &mode) < 0) - return NULL; - - return virshCommaStringListComplete(mode, modes); -} diff --git a/tools/virsh-completer.h b/tools/virsh-completer.h index f7e638d67f..5a58bd797e 100644 --- a/tools/virsh-completer.h +++ b/tools/virsh-completer.h @@ -22,21 +22,7 @@ #include "vsh.h" -char ** virshDomainNameCompleter(vshControl *ctl, - const vshCmd *cmd, - unsigned int flags); - -enum { - VIRSH_DOMAIN_INTERFACE_COMPLETER_MAC = 1 << 0, /* Return just MACs */ -}; - -char ** virshDomainInterfaceCompleter(vshControl *ctl, - const vshCmd *cmd, - unsigned int flags); - -char ** virshDomainDiskTargetCompleter(vshControl *ctl, - const vshCmd *cmd, - unsigned int flags); +#include "virsh-completer-domain.h" char ** virshCommaStringListComplete(const char *input, const char **options); @@ -97,18 +83,10 @@ char ** virshSecretEventNameCompleter(vshControl *ctl, const vshCmd *cmd, unsigned int flags); -char ** virshDomainEventNameCompleter(vshControl *ctl, - const vshCmd *cmd, - unsigned int flags); - char ** virshPoolEventNameCompleter(vshControl *ctl, const vshCmd *cmd, unsigned int flags); -char ** virshDomainInterfaceStateCompleter(vshControl *ctl, - const vshCmd *cmd, - unsigned int flags); - char ** virshNodeDeviceEventNameCompleter(vshControl *ctl, const vshCmd *cmd, unsigned int flags); @@ -117,14 +95,6 @@ char ** virshNodeDeviceCapabilityNameCompleter(vshControl *ctl, const vshCmd *cmd, unsigned int flags); -char ** virshDomainDeviceAliasCompleter(vshControl *ctl, - const vshCmd *cmd, - unsigned int flags); - char ** virshCellnoCompleter(vshControl *ctl, const vshCmd *cmd, unsigned int flags); - -char ** virshDomainShutdownModeCompleter(vshControl *ctl, - const vshCmd *cmd, - unsigned int flags); -- 2.21.0

Mixing all completers in one file does not support maintainability. Separate those completers which relate to storage pools (e.g. they complete various storage pool aspects) into virsh-completer-pool.c. Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- tools/Makefile.am | 1 + tools/virsh-completer-pool.c | 93 ++++++++++++++++++++++++++++++++++++ tools/virsh-completer-pool.h | 31 ++++++++++++ tools/virsh-completer.c | 67 -------------------------- tools/virsh-completer.h | 9 +--- 5 files changed, 126 insertions(+), 75 deletions(-) create mode 100644 tools/virsh-completer-pool.c create mode 100644 tools/virsh-completer-pool.h diff --git a/tools/Makefile.am b/tools/Makefile.am index 4a3f72b357..a5ce6a45d4 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am @@ -219,6 +219,7 @@ virsh_SOURCES = \ virsh-checkpoint.c virsh-checkpoint.h \ virsh-completer.c virsh-completer.h \ virsh-completer-domain.c virsh-completer-domain.h \ + virsh-completer-pool.c virsh-completer-pool.h \ virsh-console.c virsh-console.h \ virsh-domain.c virsh-domain.h \ virsh-domain-monitor.c virsh-domain-monitor.h \ diff --git a/tools/virsh-completer-pool.c b/tools/virsh-completer-pool.c new file mode 100644 index 0000000000..fc01550908 --- /dev/null +++ b/tools/virsh-completer-pool.c @@ -0,0 +1,93 @@ +/* + * virsh-completer-pool.c: virsh completer callbacks related to pools + * + * Copyright (C) 2019 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * <http://www.gnu.org/licenses/>. + */ + +#include <config.h> + +#include "virsh-completer-pool.h" +#include "viralloc.h" +#include "virsh-pool.h" +#include "virsh.h" +#include "virstring.h" + +char ** +virshStoragePoolNameCompleter(vshControl *ctl, + const vshCmd *cmd ATTRIBUTE_UNUSED, + unsigned int flags) +{ + virshControlPtr priv = ctl->privData; + virStoragePoolPtr *pools = NULL; + int npools = 0; + size_t i = 0; + char **ret = NULL; + VIR_AUTOSTRINGLIST tmp = NULL; + + virCheckFlags(VIR_CONNECT_LIST_STORAGE_POOLS_INACTIVE | + VIR_CONNECT_LIST_STORAGE_POOLS_ACTIVE | + VIR_CONNECT_LIST_STORAGE_POOLS_PERSISTENT, + NULL); + + if (!priv->conn || virConnectIsAlive(priv->conn) <= 0) + return NULL; + + if ((npools = virConnectListAllStoragePools(priv->conn, &pools, flags)) < 0) + return NULL; + + if (VIR_ALLOC_N(tmp, npools + 1) < 0) + goto cleanup; + + for (i = 0; i < npools; i++) { + const char *name = virStoragePoolGetName(pools[i]); + + if (VIR_STRDUP(tmp[i], name) < 0) + goto cleanup; + } + + VIR_STEAL_PTR(ret, tmp); + + cleanup: + for (i = 0; i < npools; i++) + virStoragePoolFree(pools[i]); + VIR_FREE(pools); + return ret; +} + + +char ** +virshPoolEventNameCompleter(vshControl *ctl ATTRIBUTE_UNUSED, + const vshCmd *cmd ATTRIBUTE_UNUSED, + unsigned int flags) +{ + size_t i = 0; + char **ret = NULL; + VIR_AUTOSTRINGLIST tmp = NULL; + + virCheckFlags(0, NULL); + + if (VIR_ALLOC_N(tmp, VIR_STORAGE_POOL_EVENT_ID_LAST + 1) < 0) + return NULL; + + for (i = 0; i < VIR_STORAGE_POOL_EVENT_ID_LAST; i++) { + if (VIR_STRDUP(tmp[i], virshPoolEventCallbacks[i].name) < 0) + return NULL; + } + + VIR_STEAL_PTR(ret, tmp); + return ret; +} diff --git a/tools/virsh-completer-pool.h b/tools/virsh-completer-pool.h new file mode 100644 index 0000000000..778ab25df2 --- /dev/null +++ b/tools/virsh-completer-pool.h @@ -0,0 +1,31 @@ +/* + * virsh-completer-pool.h: virsh completer callbacks related to pools + * + * Copyright (C) 2019 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * <http://www.gnu.org/licenses/>. + */ + +#pragma once + +#include "vsh.h" + +char ** virshStoragePoolNameCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags); + +char ** virshPoolEventNameCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags); diff --git a/tools/virsh-completer.c b/tools/virsh-completer.c index 787bac08f6..3b22688562 100644 --- a/tools/virsh-completer.c +++ b/tools/virsh-completer.c @@ -143,49 +143,6 @@ virshCommaStringListComplete(const char *input, } -char ** -virshStoragePoolNameCompleter(vshControl *ctl, - const vshCmd *cmd ATTRIBUTE_UNUSED, - unsigned int flags) -{ - virshControlPtr priv = ctl->privData; - virStoragePoolPtr *pools = NULL; - int npools = 0; - size_t i = 0; - char **ret = NULL; - VIR_AUTOSTRINGLIST tmp = NULL; - - virCheckFlags(VIR_CONNECT_LIST_STORAGE_POOLS_INACTIVE | - VIR_CONNECT_LIST_STORAGE_POOLS_ACTIVE | - VIR_CONNECT_LIST_STORAGE_POOLS_PERSISTENT, - NULL); - - if (!priv->conn || virConnectIsAlive(priv->conn) <= 0) - return NULL; - - if ((npools = virConnectListAllStoragePools(priv->conn, &pools, flags)) < 0) - return NULL; - - if (VIR_ALLOC_N(tmp, npools + 1) < 0) - goto cleanup; - - for (i = 0; i < npools; i++) { - const char *name = virStoragePoolGetName(pools[i]); - - if (VIR_STRDUP(tmp[i], name) < 0) - goto cleanup; - } - - VIR_STEAL_PTR(ret, tmp); - - cleanup: - for (i = 0; i < npools; i++) - virStoragePoolFree(pools[i]); - VIR_FREE(pools); - return ret; -} - - char ** virshStorageVolNameCompleter(vshControl *ctl, const vshCmd *cmd, @@ -754,30 +711,6 @@ virshSecretEventNameCompleter(vshControl *ctl ATTRIBUTE_UNUSED, } -char ** -virshPoolEventNameCompleter(vshControl *ctl ATTRIBUTE_UNUSED, - const vshCmd *cmd ATTRIBUTE_UNUSED, - unsigned int flags) -{ - size_t i = 0; - char **ret = NULL; - VIR_AUTOSTRINGLIST tmp = NULL; - - virCheckFlags(0, NULL); - - if (VIR_ALLOC_N(tmp, VIR_STORAGE_POOL_EVENT_ID_LAST + 1) < 0) - return NULL; - - for (i = 0; i < VIR_STORAGE_POOL_EVENT_ID_LAST; i++) { - if (VIR_STRDUP(tmp[i], virshPoolEventCallbacks[i].name) < 0) - return NULL; - } - - VIR_STEAL_PTR(ret, tmp); - return ret; -} - - char ** virshNodeDeviceEventNameCompleter(vshControl *ctl ATTRIBUTE_UNUSED, const vshCmd *cmd ATTRIBUTE_UNUSED, diff --git a/tools/virsh-completer.h b/tools/virsh-completer.h index 5a58bd797e..dcfc405331 100644 --- a/tools/virsh-completer.h +++ b/tools/virsh-completer.h @@ -23,14 +23,11 @@ #include "vsh.h" #include "virsh-completer-domain.h" +#include "virsh-completer-pool.h" char ** virshCommaStringListComplete(const char *input, const char **options); -char ** virshStoragePoolNameCompleter(vshControl *ctl, - const vshCmd *cmd, - unsigned int flags); - char ** virshStorageVolNameCompleter(vshControl *ctl, const vshCmd *cmd, unsigned int flags); @@ -83,10 +80,6 @@ char ** virshSecretEventNameCompleter(vshControl *ctl, const vshCmd *cmd, unsigned int flags); -char ** virshPoolEventNameCompleter(vshControl *ctl, - const vshCmd *cmd, - unsigned int flags); - char ** virshNodeDeviceEventNameCompleter(vshControl *ctl, const vshCmd *cmd, unsigned int flags); -- 2.21.0

Mixing all completers in one file does not support maintainability. Separate those completers which relate to storage volumes (e.g. they complete various storage volume aspects) into virsh-completer-volume.c Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- tools/Makefile.am | 1 + tools/virsh-completer-volume.c | 73 ++++++++++++++++++++++++++++++++++ tools/virsh-completer-volume.h | 28 +++++++++++++ tools/virsh-completer.c | 47 ---------------------- tools/virsh-completer.h | 5 +-- 5 files changed, 103 insertions(+), 51 deletions(-) create mode 100644 tools/virsh-completer-volume.c create mode 100644 tools/virsh-completer-volume.h diff --git a/tools/Makefile.am b/tools/Makefile.am index a5ce6a45d4..06c05303ed 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am @@ -220,6 +220,7 @@ virsh_SOURCES = \ virsh-completer.c virsh-completer.h \ virsh-completer-domain.c virsh-completer-domain.h \ virsh-completer-pool.c virsh-completer-pool.h \ + virsh-completer-volume.c virsh-completer-volume.h \ virsh-console.c virsh-console.h \ virsh-domain.c virsh-domain.h \ virsh-domain-monitor.c virsh-domain-monitor.h \ diff --git a/tools/virsh-completer-volume.c b/tools/virsh-completer-volume.c new file mode 100644 index 0000000000..2df0077d7a --- /dev/null +++ b/tools/virsh-completer-volume.c @@ -0,0 +1,73 @@ +/* + * virsh-completer-volume.c: virsh completer callbacks related to volumes + * + * Copyright (C) 2019 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * <http://www.gnu.org/licenses/>. + */ + +#include <config.h> + +#include "virsh-completer-volume.h" +#include "viralloc.h" +#include "virsh-pool.h" +#include "virsh.h" +#include "virstring.h" + +char ** +virshStorageVolNameCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags) +{ + virshControlPtr priv = ctl->privData; + virStoragePoolPtr pool = NULL; + virStorageVolPtr *vols = NULL; + int rc; + int nvols = 0; + size_t i = 0; + char **ret = NULL; + VIR_AUTOSTRINGLIST tmp = NULL; + + virCheckFlags(0, NULL); + + if (!priv->conn || virConnectIsAlive(priv->conn) <= 0) + return NULL; + + if (!(pool = virshCommandOptPool(ctl, cmd, "pool", NULL))) + return NULL; + + if ((rc = virStoragePoolListAllVolumes(pool, &vols, flags)) < 0) + goto cleanup; + nvols = rc; + + if (VIR_ALLOC_N(tmp, nvols + 1) < 0) + goto cleanup; + + for (i = 0; i < nvols; i++) { + const char *name = virStorageVolGetName(vols[i]); + + if (VIR_STRDUP(tmp[i], name) < 0) + goto cleanup; + } + + VIR_STEAL_PTR(ret, tmp); + + cleanup: + virStoragePoolFree(pool); + for (i = 0; i < nvols; i++) + virStorageVolFree(vols[i]); + VIR_FREE(vols); + return ret; +} diff --git a/tools/virsh-completer-volume.h b/tools/virsh-completer-volume.h new file mode 100644 index 0000000000..6591e13fdf --- /dev/null +++ b/tools/virsh-completer-volume.h @@ -0,0 +1,28 @@ +/* + * virsh-completer-volume.h: virsh completer callbacks related to volumes + * + * Copyright (C) 2019 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * <http://www.gnu.org/licenses/>. + */ + +#pragma once + +#include "vsh.h" + + +char ** virshStorageVolNameCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags); diff --git a/tools/virsh-completer.c b/tools/virsh-completer.c index 3b22688562..e6f04296a3 100644 --- a/tools/virsh-completer.c +++ b/tools/virsh-completer.c @@ -143,53 +143,6 @@ virshCommaStringListComplete(const char *input, } -char ** -virshStorageVolNameCompleter(vshControl *ctl, - const vshCmd *cmd, - unsigned int flags) -{ - virshControlPtr priv = ctl->privData; - virStoragePoolPtr pool = NULL; - virStorageVolPtr *vols = NULL; - int rc; - int nvols = 0; - size_t i = 0; - char **ret = NULL; - VIR_AUTOSTRINGLIST tmp = NULL; - - virCheckFlags(0, NULL); - - if (!priv->conn || virConnectIsAlive(priv->conn) <= 0) - return NULL; - - if (!(pool = virshCommandOptPool(ctl, cmd, "pool", NULL))) - return NULL; - - if ((rc = virStoragePoolListAllVolumes(pool, &vols, flags)) < 0) - goto cleanup; - nvols = rc; - - if (VIR_ALLOC_N(tmp, nvols + 1) < 0) - goto cleanup; - - for (i = 0; i < nvols; i++) { - const char *name = virStorageVolGetName(vols[i]); - - if (VIR_STRDUP(tmp[i], name) < 0) - goto cleanup; - } - - VIR_STEAL_PTR(ret, tmp); - - cleanup: - virStoragePoolFree(pool); - for (i = 0; i < nvols; i++) - virStorageVolFree(vols[i]); - VIR_FREE(vols); - return ret; -} - - char ** virshInterfaceNameCompleter(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED, diff --git a/tools/virsh-completer.h b/tools/virsh-completer.h index dcfc405331..e03724824a 100644 --- a/tools/virsh-completer.h +++ b/tools/virsh-completer.h @@ -24,14 +24,11 @@ #include "virsh-completer-domain.h" #include "virsh-completer-pool.h" +#include "virsh-completer-volume.h" char ** virshCommaStringListComplete(const char *input, const char **options); -char ** virshStorageVolNameCompleter(vshControl *ctl, - const vshCmd *cmd, - unsigned int flags); - char ** virshInterfaceNameCompleter(vshControl *ctl, const vshCmd *cmd, unsigned int flags); -- 2.21.0

Mixing all completers in one file does not support maintainability. Separate those completers which relate to interfaces (e.g. they complete various interface aspects) into virsh-completer-interface.c Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- tools/Makefile.am | 1 + tools/virsh-completer-interface.c | 67 +++++++++++++++++++++++++++++++ tools/virsh-completer-interface.h | 27 +++++++++++++ tools/virsh-completer.c | 42 ------------------- tools/virsh-completer.h | 5 +-- 5 files changed, 96 insertions(+), 46 deletions(-) create mode 100644 tools/virsh-completer-interface.c create mode 100644 tools/virsh-completer-interface.h diff --git a/tools/Makefile.am b/tools/Makefile.am index 06c05303ed..7e3c9c35c9 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am @@ -219,6 +219,7 @@ virsh_SOURCES = \ virsh-checkpoint.c virsh-checkpoint.h \ virsh-completer.c virsh-completer.h \ virsh-completer-domain.c virsh-completer-domain.h \ + virsh-completer-interface.c virsh-completer-interface.h \ virsh-completer-pool.c virsh-completer-pool.h \ virsh-completer-volume.c virsh-completer-volume.h \ virsh-console.c virsh-console.h \ diff --git a/tools/virsh-completer-interface.c b/tools/virsh-completer-interface.c new file mode 100644 index 0000000000..5dcdab8e92 --- /dev/null +++ b/tools/virsh-completer-interface.c @@ -0,0 +1,67 @@ +/* + * virsh-completer-interface.c: virsh completer callbacks related to interfaces + * + * Copyright (C) 2019 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * <http://www.gnu.org/licenses/>. + */ + +#include <config.h> + +#include "virsh-completer-interface.h" +#include "viralloc.h" +#include "virsh.h" +#include "virstring.h" + +char ** +virshInterfaceNameCompleter(vshControl *ctl, + const vshCmd *cmd ATTRIBUTE_UNUSED, + unsigned int flags) +{ + virshControlPtr priv = ctl->privData; + virInterfacePtr *ifaces = NULL; + int nifaces = 0; + size_t i = 0; + char **ret = NULL; + VIR_AUTOSTRINGLIST tmp = NULL; + + virCheckFlags(VIR_CONNECT_LIST_INTERFACES_ACTIVE | + VIR_CONNECT_LIST_INTERFACES_INACTIVE, + NULL); + + if (!priv->conn || virConnectIsAlive(priv->conn) <= 0) + return NULL; + + if ((nifaces = virConnectListAllInterfaces(priv->conn, &ifaces, flags)) < 0) + return NULL; + + if (VIR_ALLOC_N(tmp, nifaces + 1) < 0) + goto cleanup; + + for (i = 0; i < nifaces; i++) { + const char *name = virInterfaceGetName(ifaces[i]); + + if (VIR_STRDUP(tmp[i], name) < 0) + goto cleanup; + } + + VIR_STEAL_PTR(ret, tmp); + + cleanup: + for (i = 0; i < nifaces; i++) + virInterfaceFree(ifaces[i]); + VIR_FREE(ifaces); + return ret; +} diff --git a/tools/virsh-completer-interface.h b/tools/virsh-completer-interface.h new file mode 100644 index 0000000000..893dee5a6b --- /dev/null +++ b/tools/virsh-completer-interface.h @@ -0,0 +1,27 @@ +/* + * virsh-completer-interface.h: virsh completer callbacks related to interfaces + * + * Copyright (C) 2019 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * <http://www.gnu.org/licenses/>. + */ + +#pragma once + +#include "vsh.h" + +char ** virshInterfaceNameCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags); diff --git a/tools/virsh-completer.c b/tools/virsh-completer.c index e6f04296a3..563ee180b6 100644 --- a/tools/virsh-completer.c +++ b/tools/virsh-completer.c @@ -143,48 +143,6 @@ virshCommaStringListComplete(const char *input, } -char ** -virshInterfaceNameCompleter(vshControl *ctl, - const vshCmd *cmd ATTRIBUTE_UNUSED, - unsigned int flags) -{ - virshControlPtr priv = ctl->privData; - virInterfacePtr *ifaces = NULL; - int nifaces = 0; - size_t i = 0; - char **ret = NULL; - VIR_AUTOSTRINGLIST tmp = NULL; - - virCheckFlags(VIR_CONNECT_LIST_INTERFACES_ACTIVE | - VIR_CONNECT_LIST_INTERFACES_INACTIVE, - NULL); - - if (!priv->conn || virConnectIsAlive(priv->conn) <= 0) - return NULL; - - if ((nifaces = virConnectListAllInterfaces(priv->conn, &ifaces, flags)) < 0) - return NULL; - - if (VIR_ALLOC_N(tmp, nifaces + 1) < 0) - goto cleanup; - - for (i = 0; i < nifaces; i++) { - const char *name = virInterfaceGetName(ifaces[i]); - - if (VIR_STRDUP(tmp[i], name) < 0) - goto cleanup; - } - - VIR_STEAL_PTR(ret, tmp); - - cleanup: - for (i = 0; i < nifaces; i++) - virInterfaceFree(ifaces[i]); - VIR_FREE(ifaces); - return ret; -} - - char ** virshNetworkNameCompleter(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED, diff --git a/tools/virsh-completer.h b/tools/virsh-completer.h index e03724824a..f50b91c6ed 100644 --- a/tools/virsh-completer.h +++ b/tools/virsh-completer.h @@ -23,16 +23,13 @@ #include "vsh.h" #include "virsh-completer-domain.h" +#include "virsh-completer-interface.h" #include "virsh-completer-pool.h" #include "virsh-completer-volume.h" char ** virshCommaStringListComplete(const char *input, const char **options); -char ** virshInterfaceNameCompleter(vshControl *ctl, - const vshCmd *cmd, - unsigned int flags); - char ** virshNetworkNameCompleter(vshControl *ctl, const vshCmd *cmd, unsigned int flags); -- 2.21.0

Mixing all completers in one file does not support maintainability. Separate those completers which relate to networks (e.g. they complete various network aspects) into virsh-completer-network.c Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- tools/Makefile.am | 1 + tools/virsh-completer-network.c | 145 ++++++++++++++++++++++++++++++++ tools/virsh-completer-network.h | 35 ++++++++ tools/virsh-completer.c | 119 -------------------------- tools/virsh-completer.h | 13 +-- 5 files changed, 182 insertions(+), 131 deletions(-) create mode 100644 tools/virsh-completer-network.c create mode 100644 tools/virsh-completer-network.h diff --git a/tools/Makefile.am b/tools/Makefile.am index 7e3c9c35c9..fb42277269 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am @@ -220,6 +220,7 @@ virsh_SOURCES = \ virsh-completer.c virsh-completer.h \ virsh-completer-domain.c virsh-completer-domain.h \ virsh-completer-interface.c virsh-completer-interface.h \ + virsh-completer-network.c virsh-completer-network.h \ virsh-completer-pool.c virsh-completer-pool.h \ virsh-completer-volume.c virsh-completer-volume.h \ virsh-console.c virsh-console.h \ diff --git a/tools/virsh-completer-network.c b/tools/virsh-completer-network.c new file mode 100644 index 0000000000..6f92780feb --- /dev/null +++ b/tools/virsh-completer-network.c @@ -0,0 +1,145 @@ +/* + * virsh-completer-network.c: virsh completer callbacks related to networks + * + * Copyright (C) 2019 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * <http://www.gnu.org/licenses/>. + */ + +#include <config.h> + +#include "virsh-completer-network.h" +#include "viralloc.h" +#include "virsh-network.h" +#include "virsh.h" +#include "virstring.h" + +char ** +virshNetworkNameCompleter(vshControl *ctl, + const vshCmd *cmd ATTRIBUTE_UNUSED, + unsigned int flags) +{ + virshControlPtr priv = ctl->privData; + virNetworkPtr *nets = NULL; + int nnets = 0; + size_t i = 0; + char **ret = NULL; + VIR_AUTOSTRINGLIST tmp = NULL; + + virCheckFlags(VIR_CONNECT_LIST_NETWORKS_INACTIVE | + VIR_CONNECT_LIST_NETWORKS_ACTIVE | + VIR_CONNECT_LIST_NETWORKS_PERSISTENT, + NULL); + + if (!priv->conn || virConnectIsAlive(priv->conn) <= 0) + return NULL; + + if ((nnets = virConnectListAllNetworks(priv->conn, &nets, flags)) < 0) + return NULL; + + if (VIR_ALLOC_N(tmp, nnets + 1) < 0) + goto cleanup; + + for (i = 0; i < nnets; i++) { + const char *name = virNetworkGetName(nets[i]); + + if (VIR_STRDUP(tmp[i], name) < 0) + goto cleanup; + } + + VIR_STEAL_PTR(ret, tmp); + + cleanup: + for (i = 0; i < nnets; i++) + virNetworkFree(nets[i]); + VIR_FREE(nets); + return ret; +} + + +char ** +virshNetworkEventNameCompleter(vshControl *ctl ATTRIBUTE_UNUSED, + const vshCmd *cmd ATTRIBUTE_UNUSED, + unsigned int flags) +{ + size_t i = 0; + char **ret = NULL; + VIR_AUTOSTRINGLIST tmp = NULL; + + virCheckFlags(0, NULL); + + if (VIR_ALLOC_N(tmp, VIR_NETWORK_EVENT_ID_LAST + 1) < 0) + goto cleanup; + + for (i = 0; i < VIR_NETWORK_EVENT_ID_LAST; i++) { + if (VIR_STRDUP(tmp[i], virshNetworkEventCallbacks[i].name) < 0) + goto cleanup; + } + + VIR_STEAL_PTR(ret, tmp); + + cleanup: + return ret; +} + + +char ** +virshNetworkPortUUIDCompleter(vshControl *ctl, + const vshCmd *cmd ATTRIBUTE_UNUSED, + unsigned int flags) +{ + virshControlPtr priv = ctl->privData; + virNetworkPtr net = NULL; + virNetworkPortPtr *ports = NULL; + int nports = 0; + size_t i = 0; + char **ret = NULL; + + virCheckFlags(0, NULL); + + if (!priv->conn || virConnectIsAlive(priv->conn) <= 0) + return NULL; + + if (!(net = virshCommandOptNetwork(ctl, cmd, NULL))) + return false; + + if ((nports = virNetworkListAllPorts(net, &ports, flags)) < 0) + return NULL; + + if (VIR_ALLOC_N(ret, nports + 1) < 0) + goto error; + + for (i = 0; i < nports; i++) { + char uuid[VIR_UUID_STRING_BUFLEN]; + + if (virNetworkPortGetUUIDString(ports[i], uuid) < 0 || + VIR_STRDUP(ret[i], uuid) < 0) + goto error; + + virNetworkPortFree(ports[i]); + } + VIR_FREE(ports); + + return ret; + + error: + for (; i < nports; i++) + virNetworkPortFree(ports[i]); + VIR_FREE(ports); + for (i = 0; i < nports; i++) + VIR_FREE(ret[i]); + VIR_FREE(ret); + return NULL; +} diff --git a/tools/virsh-completer-network.h b/tools/virsh-completer-network.h new file mode 100644 index 0000000000..e317e483c1 --- /dev/null +++ b/tools/virsh-completer-network.h @@ -0,0 +1,35 @@ +/* + * virsh-completer-network.h: virsh completer callbacks related to networks + * + * Copyright (C) 2019 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * <http://www.gnu.org/licenses/>. + */ + +#pragma once + +#include "vsh.h" + +char ** virshNetworkNameCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags); + +char ** virshNetworkEventNameCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags); + +char ** virshNetworkPortUUIDCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags); diff --git a/tools/virsh-completer.c b/tools/virsh-completer.c index 563ee180b6..cc2e92708c 100644 --- a/tools/virsh-completer.c +++ b/tools/virsh-completer.c @@ -143,125 +143,6 @@ virshCommaStringListComplete(const char *input, } -char ** -virshNetworkNameCompleter(vshControl *ctl, - const vshCmd *cmd ATTRIBUTE_UNUSED, - unsigned int flags) -{ - virshControlPtr priv = ctl->privData; - virNetworkPtr *nets = NULL; - int nnets = 0; - size_t i = 0; - char **ret = NULL; - VIR_AUTOSTRINGLIST tmp = NULL; - - virCheckFlags(VIR_CONNECT_LIST_NETWORKS_INACTIVE | - VIR_CONNECT_LIST_NETWORKS_ACTIVE | - VIR_CONNECT_LIST_NETWORKS_PERSISTENT, - NULL); - - if (!priv->conn || virConnectIsAlive(priv->conn) <= 0) - return NULL; - - if ((nnets = virConnectListAllNetworks(priv->conn, &nets, flags)) < 0) - return NULL; - - if (VIR_ALLOC_N(tmp, nnets + 1) < 0) - goto cleanup; - - for (i = 0; i < nnets; i++) { - const char *name = virNetworkGetName(nets[i]); - - if (VIR_STRDUP(tmp[i], name) < 0) - goto cleanup; - } - - VIR_STEAL_PTR(ret, tmp); - - cleanup: - for (i = 0; i < nnets; i++) - virNetworkFree(nets[i]); - VIR_FREE(nets); - return ret; -} - - -char ** -virshNetworkEventNameCompleter(vshControl *ctl ATTRIBUTE_UNUSED, - const vshCmd *cmd ATTRIBUTE_UNUSED, - unsigned int flags) -{ - size_t i = 0; - char **ret = NULL; - VIR_AUTOSTRINGLIST tmp = NULL; - - virCheckFlags(0, NULL); - - if (VIR_ALLOC_N(tmp, VIR_NETWORK_EVENT_ID_LAST + 1) < 0) - goto cleanup; - - for (i = 0; i < VIR_NETWORK_EVENT_ID_LAST; i++) { - if (VIR_STRDUP(tmp[i], virshNetworkEventCallbacks[i].name) < 0) - goto cleanup; - } - - VIR_STEAL_PTR(ret, tmp); - - cleanup: - return ret; -} - - -char ** -virshNetworkPortUUIDCompleter(vshControl *ctl, - const vshCmd *cmd ATTRIBUTE_UNUSED, - unsigned int flags) -{ - virshControlPtr priv = ctl->privData; - virNetworkPtr net = NULL; - virNetworkPortPtr *ports = NULL; - int nports = 0; - size_t i = 0; - char **ret = NULL; - - virCheckFlags(0, NULL); - - if (!priv->conn || virConnectIsAlive(priv->conn) <= 0) - return NULL; - - if (!(net = virshCommandOptNetwork(ctl, cmd, NULL))) - return false; - - if ((nports = virNetworkListAllPorts(net, &ports, flags)) < 0) - return NULL; - - if (VIR_ALLOC_N(ret, nports + 1) < 0) - goto error; - - for (i = 0; i < nports; i++) { - char uuid[VIR_UUID_STRING_BUFLEN]; - - if (virNetworkPortGetUUIDString(ports[i], uuid) < 0 || - VIR_STRDUP(ret[i], uuid) < 0) - goto error; - - virNetworkPortFree(ports[i]); - } - VIR_FREE(ports); - - return ret; - - error: - for (; i < nports; i++) - virNetworkPortFree(ports[i]); - VIR_FREE(ports); - for (i = 0; i < nports; i++) - VIR_FREE(ret[i]); - VIR_FREE(ret); - return NULL; -} - - char ** virshNodeDeviceNameCompleter(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED, diff --git a/tools/virsh-completer.h b/tools/virsh-completer.h index f50b91c6ed..6fa5032458 100644 --- a/tools/virsh-completer.h +++ b/tools/virsh-completer.h @@ -24,24 +24,13 @@ #include "virsh-completer-domain.h" #include "virsh-completer-interface.h" +#include "virsh-completer-network.h" #include "virsh-completer-pool.h" #include "virsh-completer-volume.h" char ** virshCommaStringListComplete(const char *input, const char **options); -char ** virshNetworkNameCompleter(vshControl *ctl, - const vshCmd *cmd, - unsigned int flags); - -char ** virshNetworkEventNameCompleter(vshControl *ctl, - const vshCmd *cmd, - unsigned int flags); - -char ** virshNetworkPortUUIDCompleter(vshControl *ctl, - const vshCmd *cmd, - unsigned int flags); - char ** virshNodeDeviceNameCompleter(vshControl *ctl, const vshCmd *cmd, unsigned int flags); -- 2.21.0

Mixing all completers in one file does not support maintainability. Separate those completers which relate to nodedev (e.g. they complete various nodedev aspects) into virsh-completer-nodedev.c Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- tools/Makefile.am | 1 + tools/virsh-completer-nodedev.c | 117 ++++++++++++++++++++++++++++++++ tools/virsh-completer-nodedev.h | 35 ++++++++++ tools/virsh-completer.c | 90 ------------------------ tools/virsh-completer.h | 13 +--- 5 files changed, 154 insertions(+), 102 deletions(-) create mode 100644 tools/virsh-completer-nodedev.c create mode 100644 tools/virsh-completer-nodedev.h diff --git a/tools/Makefile.am b/tools/Makefile.am index fb42277269..e31e0f636d 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am @@ -221,6 +221,7 @@ virsh_SOURCES = \ virsh-completer-domain.c virsh-completer-domain.h \ virsh-completer-interface.c virsh-completer-interface.h \ virsh-completer-network.c virsh-completer-network.h \ + virsh-completer-nodedev.c virsh-completer-nodedev.h \ virsh-completer-pool.c virsh-completer-pool.h \ virsh-completer-volume.c virsh-completer-volume.h \ virsh-console.c virsh-console.h \ diff --git a/tools/virsh-completer-nodedev.c b/tools/virsh-completer-nodedev.c new file mode 100644 index 0000000000..36f38acd57 --- /dev/null +++ b/tools/virsh-completer-nodedev.c @@ -0,0 +1,117 @@ +/* + * virsh-completer-nodedev.c: virsh completer callbacks related to nodedev + * + * Copyright (C) 2019 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * <http://www.gnu.org/licenses/>. + */ + +#include <config.h> + +#include "virsh-completer-nodedev.h" +#include "conf/node_device_conf.h" +#include "viralloc.h" +#include "virsh-nodedev.h" +#include "virsh.h" +#include "virstring.h" + +char ** +virshNodeDeviceNameCompleter(vshControl *ctl, + const vshCmd *cmd ATTRIBUTE_UNUSED, + unsigned int flags) +{ + virshControlPtr priv = ctl->privData; + virNodeDevicePtr *devs = NULL; + int ndevs = 0; + size_t i = 0; + char **ret = NULL; + VIR_AUTOSTRINGLIST tmp = NULL; + + virCheckFlags(0, NULL); + + if (!priv->conn || virConnectIsAlive(priv->conn) <= 0) + return NULL; + + if ((ndevs = virConnectListAllNodeDevices(priv->conn, &devs, flags)) < 0) + return NULL; + + if (VIR_ALLOC_N(tmp, ndevs + 1) < 0) + goto cleanup; + + for (i = 0; i < ndevs; i++) { + const char *name = virNodeDeviceGetName(devs[i]); + + if (VIR_STRDUP(tmp[i], name) < 0) + goto cleanup; + } + + VIR_STEAL_PTR(ret, tmp); + + cleanup: + for (i = 0; i < ndevs; i++) + virNodeDeviceFree(devs[i]); + VIR_FREE(devs); + return ret; +} + + +char ** +virshNodeDeviceEventNameCompleter(vshControl *ctl ATTRIBUTE_UNUSED, + const vshCmd *cmd ATTRIBUTE_UNUSED, + unsigned int flags) +{ + size_t i = 0; + char **ret = NULL; + VIR_AUTOSTRINGLIST tmp = NULL; + + virCheckFlags(0, NULL); + + if (VIR_ALLOC_N(tmp, VIR_NODE_DEVICE_EVENT_ID_LAST + 1) < 0) + return NULL; + + for (i = 0; i < VIR_NODE_DEVICE_EVENT_ID_LAST; i++) { + if (VIR_STRDUP(tmp[i], virshNodeDeviceEventCallbacks[i].name) < 0) + return NULL; + } + + VIR_STEAL_PTR(ret, tmp); + return ret; +} + + +char ** +virshNodeDeviceCapabilityNameCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags) +{ + VIR_AUTOSTRINGLIST tmp = NULL; + const char *cap_str = NULL; + size_t i = 0; + + virCheckFlags(0, NULL); + + if (vshCommandOptStringQuiet(ctl, cmd, "cap", &cap_str) < 0) + return NULL; + + if (VIR_ALLOC_N(tmp, VIR_NODE_DEV_CAP_LAST + 1) < 0) + return NULL; + + for (i = 0; i < VIR_NODE_DEV_CAP_LAST; i++) { + if (VIR_STRDUP(tmp[i], virNodeDevCapTypeToString(i)) < 0) + return NULL; + } + + return virshCommaStringListComplete(cap_str, (const char **)tmp); +} diff --git a/tools/virsh-completer-nodedev.h b/tools/virsh-completer-nodedev.h new file mode 100644 index 0000000000..45c0b73c8c --- /dev/null +++ b/tools/virsh-completer-nodedev.h @@ -0,0 +1,35 @@ +/* + * virsh-completer-nodedev.h: virsh completer callbacks related to nodedev + * + * Copyright (C) 2019 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * <http://www.gnu.org/licenses/>. + */ + +#pragma once + +#include "vsh.h" + +char ** virshNodeDeviceNameCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags); + +char ** virshNodeDeviceEventNameCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags); + +char ** virshNodeDeviceCapabilityNameCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags); diff --git a/tools/virsh-completer.c b/tools/virsh-completer.c index cc2e92708c..c9374de97a 100644 --- a/tools/virsh-completer.c +++ b/tools/virsh-completer.c @@ -143,46 +143,6 @@ virshCommaStringListComplete(const char *input, } -char ** -virshNodeDeviceNameCompleter(vshControl *ctl, - const vshCmd *cmd ATTRIBUTE_UNUSED, - unsigned int flags) -{ - virshControlPtr priv = ctl->privData; - virNodeDevicePtr *devs = NULL; - int ndevs = 0; - size_t i = 0; - char **ret = NULL; - VIR_AUTOSTRINGLIST tmp = NULL; - - virCheckFlags(0, NULL); - - if (!priv->conn || virConnectIsAlive(priv->conn) <= 0) - return NULL; - - if ((ndevs = virConnectListAllNodeDevices(priv->conn, &devs, flags)) < 0) - return NULL; - - if (VIR_ALLOC_N(tmp, ndevs + 1) < 0) - goto cleanup; - - for (i = 0; i < ndevs; i++) { - const char *name = virNodeDeviceGetName(devs[i]); - - if (VIR_STRDUP(tmp[i], name) < 0) - goto cleanup; - } - - VIR_STEAL_PTR(ret, tmp); - - cleanup: - for (i = 0; i < ndevs; i++) - virNodeDeviceFree(devs[i]); - VIR_FREE(devs); - return ret; -} - - char ** virshNWFilterNameCompleter(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED, @@ -503,56 +463,6 @@ virshSecretEventNameCompleter(vshControl *ctl ATTRIBUTE_UNUSED, } -char ** -virshNodeDeviceEventNameCompleter(vshControl *ctl ATTRIBUTE_UNUSED, - const vshCmd *cmd ATTRIBUTE_UNUSED, - unsigned int flags) -{ - size_t i = 0; - char **ret = NULL; - VIR_AUTOSTRINGLIST tmp = NULL; - - virCheckFlags(0, NULL); - - if (VIR_ALLOC_N(tmp, VIR_NODE_DEVICE_EVENT_ID_LAST + 1) < 0) - return NULL; - - for (i = 0; i < VIR_NODE_DEVICE_EVENT_ID_LAST; i++) { - if (VIR_STRDUP(tmp[i], virshNodeDeviceEventCallbacks[i].name) < 0) - return NULL; - } - - VIR_STEAL_PTR(ret, tmp); - return ret; -} - - -char ** -virshNodeDeviceCapabilityNameCompleter(vshControl *ctl, - const vshCmd *cmd, - unsigned int flags) -{ - VIR_AUTOSTRINGLIST tmp = NULL; - const char *cap_str = NULL; - size_t i = 0; - - virCheckFlags(0, NULL); - - if (vshCommandOptStringQuiet(ctl, cmd, "cap", &cap_str) < 0) - return NULL; - - if (VIR_ALLOC_N(tmp, VIR_NODE_DEV_CAP_LAST + 1) < 0) - return NULL; - - for (i = 0; i < VIR_NODE_DEV_CAP_LAST; i++) { - if (VIR_STRDUP(tmp[i], virNodeDevCapTypeToString(i)) < 0) - return NULL; - } - - return virshCommaStringListComplete(cap_str, (const char **)tmp); -} - - char ** virshCellnoCompleter(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED, diff --git a/tools/virsh-completer.h b/tools/virsh-completer.h index 6fa5032458..fac7a24dbb 100644 --- a/tools/virsh-completer.h +++ b/tools/virsh-completer.h @@ -25,16 +25,13 @@ #include "virsh-completer-domain.h" #include "virsh-completer-interface.h" #include "virsh-completer-network.h" +#include "virsh-completer-nodedev.h" #include "virsh-completer-pool.h" #include "virsh-completer-volume.h" char ** virshCommaStringListComplete(const char *input, const char **options); -char ** virshNodeDeviceNameCompleter(vshControl *ctl, - const vshCmd *cmd, - unsigned int flags); - char ** virshNWFilterNameCompleter(vshControl *ctl, const vshCmd *cmd, unsigned int flags); @@ -63,14 +60,6 @@ char ** virshSecretEventNameCompleter(vshControl *ctl, const vshCmd *cmd, unsigned int flags); -char ** virshNodeDeviceEventNameCompleter(vshControl *ctl, - const vshCmd *cmd, - unsigned int flags); - -char ** virshNodeDeviceCapabilityNameCompleter(vshControl *ctl, - const vshCmd *cmd, - unsigned int flags); - char ** virshCellnoCompleter(vshControl *ctl, const vshCmd *cmd, unsigned int flags); -- 2.21.0

Mixing all completers in one file does not support maintainability. Separate those completers which relate to nwfilter (e.g. they complete various nwfilter aspects) into virsh-completer-nwfilter.c Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- tools/Makefile.am | 1 + tools/virsh-completer-nwfilter.c | 105 +++++++++++++++++++++++++++++++ tools/virsh-completer-nwfilter.h | 31 +++++++++ tools/virsh-completer.c | 80 ----------------------- tools/virsh-completer.h | 9 +-- 5 files changed, 138 insertions(+), 88 deletions(-) create mode 100644 tools/virsh-completer-nwfilter.c create mode 100644 tools/virsh-completer-nwfilter.h diff --git a/tools/Makefile.am b/tools/Makefile.am index e31e0f636d..6e6d9a05cc 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am @@ -222,6 +222,7 @@ virsh_SOURCES = \ virsh-completer-interface.c virsh-completer-interface.h \ virsh-completer-network.c virsh-completer-network.h \ virsh-completer-nodedev.c virsh-completer-nodedev.h \ + virsh-completer-nwfilter.c virsh-completer-nwfilter.h \ virsh-completer-pool.c virsh-completer-pool.h \ virsh-completer-volume.c virsh-completer-volume.h \ virsh-console.c virsh-console.h \ diff --git a/tools/virsh-completer-nwfilter.c b/tools/virsh-completer-nwfilter.c new file mode 100644 index 0000000000..0942526f22 --- /dev/null +++ b/tools/virsh-completer-nwfilter.c @@ -0,0 +1,105 @@ +/* + * virsh-completer-nwfilter.c: virsh completer callbacks related to nwfilters + * + * Copyright (C) 2019 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * <http://www.gnu.org/licenses/>. + */ + +#include <config.h> + +#include "virsh-completer-nwfilter.h" +#include "viralloc.h" +#include "virsh.h" +#include "virstring.h" + +char ** +virshNWFilterNameCompleter(vshControl *ctl, + const vshCmd *cmd ATTRIBUTE_UNUSED, + unsigned int flags) +{ + virshControlPtr priv = ctl->privData; + virNWFilterPtr *nwfilters = NULL; + int nnwfilters = 0; + size_t i = 0; + char **ret = NULL; + VIR_AUTOSTRINGLIST tmp = NULL; + + virCheckFlags(0, NULL); + + if (!priv->conn || virConnectIsAlive(priv->conn) <= 0) + return NULL; + + if ((nnwfilters = virConnectListAllNWFilters(priv->conn, &nwfilters, flags)) < 0) + return NULL; + + if (VIR_ALLOC_N(tmp, nnwfilters + 1) < 0) + goto cleanup; + + for (i = 0; i < nnwfilters; i++) { + const char *name = virNWFilterGetName(nwfilters[i]); + + if (VIR_STRDUP(tmp[i], name) < 0) + goto cleanup; + } + + VIR_STEAL_PTR(ret, tmp); + + cleanup: + for (i = 0; i < nnwfilters; i++) + virNWFilterFree(nwfilters[i]); + VIR_FREE(nwfilters); + return ret; +} + + +char ** +virshNWFilterBindingNameCompleter(vshControl *ctl, + const vshCmd *cmd ATTRIBUTE_UNUSED, + unsigned int flags) +{ + virshControlPtr priv = ctl->privData; + virNWFilterBindingPtr *bindings = NULL; + int nbindings = 0; + size_t i = 0; + char **ret = NULL; + VIR_AUTOSTRINGLIST tmp = NULL; + + virCheckFlags(0, NULL); + + if (!priv->conn || virConnectIsAlive(priv->conn) <= 0) + return NULL; + + if ((nbindings = virConnectListAllNWFilterBindings(priv->conn, &bindings, flags)) < 0) + return NULL; + + if (VIR_ALLOC_N(tmp, nbindings + 1) < 0) + goto cleanup; + + for (i = 0; i < nbindings; i++) { + const char *name = virNWFilterBindingGetPortDev(bindings[i]); + + if (VIR_STRDUP(tmp[i], name) < 0) + goto cleanup; + } + + VIR_STEAL_PTR(ret, tmp); + + cleanup: + for (i = 0; i < nbindings; i++) + virNWFilterBindingFree(bindings[i]); + VIR_FREE(bindings); + return ret; +} diff --git a/tools/virsh-completer-nwfilter.h b/tools/virsh-completer-nwfilter.h new file mode 100644 index 0000000000..86e7df7da4 --- /dev/null +++ b/tools/virsh-completer-nwfilter.h @@ -0,0 +1,31 @@ +/* + * virsh-completer-nwfilter.h: virsh completer callbacks related to nwfilters + * + * Copyright (C) 2019 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * <http://www.gnu.org/licenses/>. + */ + +#pragma once + +#include "vsh.h" + +char ** virshNWFilterNameCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags); + +char ** virshNWFilterBindingNameCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags); diff --git a/tools/virsh-completer.c b/tools/virsh-completer.c index c9374de97a..c985b86fa9 100644 --- a/tools/virsh-completer.c +++ b/tools/virsh-completer.c @@ -143,86 +143,6 @@ virshCommaStringListComplete(const char *input, } -char ** -virshNWFilterNameCompleter(vshControl *ctl, - const vshCmd *cmd ATTRIBUTE_UNUSED, - unsigned int flags) -{ - virshControlPtr priv = ctl->privData; - virNWFilterPtr *nwfilters = NULL; - int nnwfilters = 0; - size_t i = 0; - char **ret = NULL; - VIR_AUTOSTRINGLIST tmp = NULL; - - virCheckFlags(0, NULL); - - if (!priv->conn || virConnectIsAlive(priv->conn) <= 0) - return NULL; - - if ((nnwfilters = virConnectListAllNWFilters(priv->conn, &nwfilters, flags)) < 0) - return NULL; - - if (VIR_ALLOC_N(tmp, nnwfilters + 1) < 0) - goto cleanup; - - for (i = 0; i < nnwfilters; i++) { - const char *name = virNWFilterGetName(nwfilters[i]); - - if (VIR_STRDUP(tmp[i], name) < 0) - goto cleanup; - } - - VIR_STEAL_PTR(ret, tmp); - - cleanup: - for (i = 0; i < nnwfilters; i++) - virNWFilterFree(nwfilters[i]); - VIR_FREE(nwfilters); - return ret; -} - - -char ** -virshNWFilterBindingNameCompleter(vshControl *ctl, - const vshCmd *cmd ATTRIBUTE_UNUSED, - unsigned int flags) -{ - virshControlPtr priv = ctl->privData; - virNWFilterBindingPtr *bindings = NULL; - int nbindings = 0; - size_t i = 0; - char **ret = NULL; - VIR_AUTOSTRINGLIST tmp = NULL; - - virCheckFlags(0, NULL); - - if (!priv->conn || virConnectIsAlive(priv->conn) <= 0) - return NULL; - - if ((nbindings = virConnectListAllNWFilterBindings(priv->conn, &bindings, flags)) < 0) - return NULL; - - if (VIR_ALLOC_N(tmp, nbindings + 1) < 0) - goto cleanup; - - for (i = 0; i < nbindings; i++) { - const char *name = virNWFilterBindingGetPortDev(bindings[i]); - - if (VIR_STRDUP(tmp[i], name) < 0) - goto cleanup; - } - - VIR_STEAL_PTR(ret, tmp); - - cleanup: - for (i = 0; i < nbindings; i++) - virNWFilterBindingFree(bindings[i]); - VIR_FREE(bindings); - return ret; -} - - char ** virshSecretUUIDCompleter(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED, diff --git a/tools/virsh-completer.h b/tools/virsh-completer.h index fac7a24dbb..5cc5794f58 100644 --- a/tools/virsh-completer.h +++ b/tools/virsh-completer.h @@ -26,20 +26,13 @@ #include "virsh-completer-interface.h" #include "virsh-completer-network.h" #include "virsh-completer-nodedev.h" +#include "virsh-completer-nwfilter.h" #include "virsh-completer-pool.h" #include "virsh-completer-volume.h" char ** virshCommaStringListComplete(const char *input, const char **options); -char ** virshNWFilterNameCompleter(vshControl *ctl, - const vshCmd *cmd, - unsigned int flags); - -char ** virshNWFilterBindingNameCompleter(vshControl *ctl, - const vshCmd *cmd, - unsigned int flags); - char ** virshSecretUUIDCompleter(vshControl *ctl, const vshCmd *cmd, unsigned int flags); -- 2.21.0

Mixing all completers in one file does not support maintainability. Separate those completers which relate to secret (e.g. they complete various secret aspects) into virsh-completer-secret.c Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- tools/Makefile.am | 1 + tools/virsh-completer-secret.c | 91 ++++++++++++++++++++++++++++++++++ tools/virsh-completer-secret.h | 31 ++++++++++++ tools/virsh-completer.c | 65 ------------------------ tools/virsh-completer.h | 9 +--- 5 files changed, 124 insertions(+), 73 deletions(-) create mode 100644 tools/virsh-completer-secret.c create mode 100644 tools/virsh-completer-secret.h diff --git a/tools/Makefile.am b/tools/Makefile.am index 6e6d9a05cc..f229db39d8 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am @@ -224,6 +224,7 @@ virsh_SOURCES = \ virsh-completer-nodedev.c virsh-completer-nodedev.h \ virsh-completer-nwfilter.c virsh-completer-nwfilter.h \ virsh-completer-pool.c virsh-completer-pool.h \ + virsh-completer-secret.c virsh-completer-secret.h \ virsh-completer-volume.c virsh-completer-volume.h \ virsh-console.c virsh-console.h \ virsh-domain.c virsh-domain.h \ diff --git a/tools/virsh-completer-secret.c b/tools/virsh-completer-secret.c new file mode 100644 index 0000000000..a6924b6b8c --- /dev/null +++ b/tools/virsh-completer-secret.c @@ -0,0 +1,91 @@ +/* + * virsh-completer-secret.c: virsh completer callbacks related to secret + * + * Copyright (C) 2019 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * <http://www.gnu.org/licenses/>. + */ + +#include <config.h> + +#include "virsh-completer-secret.h" +#include "viralloc.h" +#include "virsh-secret.h" +#include "virsh.h" +#include "virstring.h" + +char ** +virshSecretUUIDCompleter(vshControl *ctl, + const vshCmd *cmd ATTRIBUTE_UNUSED, + unsigned int flags) +{ + virshControlPtr priv = ctl->privData; + virSecretPtr *secrets = NULL; + int nsecrets = 0; + size_t i = 0; + char **ret = NULL; + VIR_AUTOSTRINGLIST tmp = NULL; + + virCheckFlags(0, NULL); + + if (!priv->conn || virConnectIsAlive(priv->conn) <= 0) + return NULL; + + if ((nsecrets = virConnectListAllSecrets(priv->conn, &secrets, flags)) < 0) + return NULL; + + if (VIR_ALLOC_N(tmp, nsecrets + 1) < 0) + goto cleanup; + + for (i = 0; i < nsecrets; i++) { + char uuid[VIR_UUID_STRING_BUFLEN]; + + if (virSecretGetUUIDString(secrets[i], uuid) < 0 || + VIR_STRDUP(tmp[i], uuid) < 0) + goto cleanup; + } + + VIR_STEAL_PTR(ret, tmp); + + cleanup: + for (i = 0; i < nsecrets; i++) + virSecretFree(secrets[i]); + VIR_FREE(secrets); + return ret; +} + + +char ** +virshSecretEventNameCompleter(vshControl *ctl ATTRIBUTE_UNUSED, + const vshCmd *cmd ATTRIBUTE_UNUSED, + unsigned int flags) +{ + size_t i; + char **ret = NULL; + VIR_AUTOSTRINGLIST tmp = NULL; + + virCheckFlags(0, NULL); + + if (VIR_ALLOC_N(tmp, VIR_SECRET_EVENT_ID_LAST + 1) < 0) + return NULL; + + for (i = 0; i < VIR_SECRET_EVENT_ID_LAST; i++) { + if (VIR_STRDUP(tmp[i], virshSecretEventCallbacks[i].name) < 0) + return NULL; + } + + VIR_STEAL_PTR(ret, tmp); + return ret; +} diff --git a/tools/virsh-completer-secret.h b/tools/virsh-completer-secret.h new file mode 100644 index 0000000000..3ed6ba5198 --- /dev/null +++ b/tools/virsh-completer-secret.h @@ -0,0 +1,31 @@ +/* + * virsh-completer-secret.h: virsh completer callbacks related to secret + * + * Copyright (C) 2019 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * <http://www.gnu.org/licenses/>. + */ + +#pragma once + +#include "vsh.h" + +char ** virshSecretUUIDCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags); + +char ** virshSecretEventNameCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags); diff --git a/tools/virsh-completer.c b/tools/virsh-completer.c index c985b86fa9..3489f8741a 100644 --- a/tools/virsh-completer.c +++ b/tools/virsh-completer.c @@ -143,47 +143,6 @@ virshCommaStringListComplete(const char *input, } -char ** -virshSecretUUIDCompleter(vshControl *ctl, - const vshCmd *cmd ATTRIBUTE_UNUSED, - unsigned int flags) -{ - virshControlPtr priv = ctl->privData; - virSecretPtr *secrets = NULL; - int nsecrets = 0; - size_t i = 0; - char **ret = NULL; - VIR_AUTOSTRINGLIST tmp = NULL; - - virCheckFlags(0, NULL); - - if (!priv->conn || virConnectIsAlive(priv->conn) <= 0) - return NULL; - - if ((nsecrets = virConnectListAllSecrets(priv->conn, &secrets, flags)) < 0) - return NULL; - - if (VIR_ALLOC_N(tmp, nsecrets + 1) < 0) - goto cleanup; - - for (i = 0; i < nsecrets; i++) { - char uuid[VIR_UUID_STRING_BUFLEN]; - - if (virSecretGetUUIDString(secrets[i], uuid) < 0 || - VIR_STRDUP(tmp[i], uuid) < 0) - goto cleanup; - } - - VIR_STEAL_PTR(ret, tmp); - - cleanup: - for (i = 0; i < nsecrets; i++) - virSecretFree(secrets[i]); - VIR_FREE(secrets); - return ret; -} - - char ** virshCheckpointNameCompleter(vshControl *ctl, const vshCmd *cmd, @@ -359,30 +318,6 @@ virshAllocpagesPagesizeCompleter(vshControl *ctl, } -char ** -virshSecretEventNameCompleter(vshControl *ctl ATTRIBUTE_UNUSED, - const vshCmd *cmd ATTRIBUTE_UNUSED, - unsigned int flags) -{ - size_t i; - char **ret = NULL; - VIR_AUTOSTRINGLIST tmp = NULL; - - virCheckFlags(0, NULL); - - if (VIR_ALLOC_N(tmp, VIR_SECRET_EVENT_ID_LAST + 1) < 0) - return NULL; - - for (i = 0; i < VIR_SECRET_EVENT_ID_LAST; i++) { - if (VIR_STRDUP(tmp[i], virshSecretEventCallbacks[i].name) < 0) - return NULL; - } - - VIR_STEAL_PTR(ret, tmp); - return ret; -} - - char ** virshCellnoCompleter(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED, diff --git a/tools/virsh-completer.h b/tools/virsh-completer.h index 5cc5794f58..e27d58e536 100644 --- a/tools/virsh-completer.h +++ b/tools/virsh-completer.h @@ -28,15 +28,12 @@ #include "virsh-completer-nodedev.h" #include "virsh-completer-nwfilter.h" #include "virsh-completer-pool.h" +#include "virsh-completer-secret.h" #include "virsh-completer-volume.h" char ** virshCommaStringListComplete(const char *input, const char **options); -char ** virshSecretUUIDCompleter(vshControl *ctl, - const vshCmd *cmd, - unsigned int flags); - char ** virshCheckpointNameCompleter(vshControl *ctl, const vshCmd *cmd, unsigned int flags); @@ -49,10 +46,6 @@ char ** virshAllocpagesPagesizeCompleter(vshControl *ctl, const vshCmd *cmd, unsigned int flags); -char ** virshSecretEventNameCompleter(vshControl *ctl, - const vshCmd *cmd, - unsigned int flags); - char ** virshCellnoCompleter(vshControl *ctl, const vshCmd *cmd, unsigned int flags); -- 2.21.0

Mixing all completers in one file does not support maintainability. Separate those completers which relate to snapshot (e.g. they complete various snapshot aspects) into virsh-completer-snapshot.c Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- tools/Makefile.am | 1 + tools/virsh-completer-snapshot.c | 73 ++++++++++++++++++++++++++++++++ tools/virsh-completer-snapshot.h | 27 ++++++++++++ tools/virsh-completer.c | 45 -------------------- tools/virsh-completer.h | 5 +-- 5 files changed, 102 insertions(+), 49 deletions(-) create mode 100644 tools/virsh-completer-snapshot.c create mode 100644 tools/virsh-completer-snapshot.h diff --git a/tools/Makefile.am b/tools/Makefile.am index f229db39d8..3a89ec70a2 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am @@ -225,6 +225,7 @@ virsh_SOURCES = \ virsh-completer-nwfilter.c virsh-completer-nwfilter.h \ virsh-completer-pool.c virsh-completer-pool.h \ virsh-completer-secret.c virsh-completer-secret.h \ + virsh-completer-snapshot.c virsh-completer-snapshot.h \ virsh-completer-volume.c virsh-completer-volume.h \ virsh-console.c virsh-console.h \ virsh-domain.c virsh-domain.h \ diff --git a/tools/virsh-completer-snapshot.c b/tools/virsh-completer-snapshot.c new file mode 100644 index 0000000000..cad56e2fba --- /dev/null +++ b/tools/virsh-completer-snapshot.c @@ -0,0 +1,73 @@ +/* + * virsh-completer-snapshot.c: virsh completer callbacks related to snapshots + * + * Copyright (C) 2019 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * <http://www.gnu.org/licenses/>. + */ + +#include <config.h> + +#include "virsh-completer-snapshot.h" +#include "viralloc.h" +#include "virsh-util.h" +#include "virsh.h" +#include "virstring.h" + +char ** +virshSnapshotNameCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags) +{ + virshControlPtr priv = ctl->privData; + virDomainPtr dom = NULL; + virDomainSnapshotPtr *snapshots = NULL; + int rc; + int nsnapshots = 0; + size_t i = 0; + char **ret = NULL; + VIR_AUTOSTRINGLIST tmp = NULL; + + virCheckFlags(0, NULL); + + if (!priv->conn || virConnectIsAlive(priv->conn) <= 0) + return NULL; + + if (!(dom = virshCommandOptDomain(ctl, cmd, NULL))) + return NULL; + + if ((rc = virDomainListAllSnapshots(dom, &snapshots, flags)) < 0) + goto cleanup; + nsnapshots = rc; + + if (VIR_ALLOC_N(tmp, nsnapshots + 1) < 0) + goto cleanup; + + for (i = 0; i < nsnapshots; i++) { + const char *name = virDomainSnapshotGetName(snapshots[i]); + + if (VIR_STRDUP(tmp[i], name) < 0) + goto cleanup; + } + + VIR_STEAL_PTR(ret, tmp); + + cleanup: + virshDomainFree(dom); + for (i = 0; i < nsnapshots; i++) + virshDomainSnapshotFree(snapshots[i]); + VIR_FREE(snapshots); + return ret; +} diff --git a/tools/virsh-completer-snapshot.h b/tools/virsh-completer-snapshot.h new file mode 100644 index 0000000000..1af32e28ca --- /dev/null +++ b/tools/virsh-completer-snapshot.h @@ -0,0 +1,27 @@ +/* + * virsh-completer-snapshot.h: virsh completer callbacks related to snapshots + * + * Copyright (C) 2019 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * <http://www.gnu.org/licenses/>. + */ + +#pragma once + +#include "vsh.h" + +char ** virshSnapshotNameCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags); diff --git a/tools/virsh-completer.c b/tools/virsh-completer.c index 3489f8741a..72223803dd 100644 --- a/tools/virsh-completer.c +++ b/tools/virsh-completer.c @@ -194,51 +194,6 @@ virshCheckpointNameCompleter(vshControl *ctl, return NULL; } -char ** -virshSnapshotNameCompleter(vshControl *ctl, - const vshCmd *cmd, - unsigned int flags) -{ - virshControlPtr priv = ctl->privData; - virDomainPtr dom = NULL; - virDomainSnapshotPtr *snapshots = NULL; - int rc; - int nsnapshots = 0; - size_t i = 0; - char **ret = NULL; - VIR_AUTOSTRINGLIST tmp = NULL; - - virCheckFlags(0, NULL); - - if (!priv->conn || virConnectIsAlive(priv->conn) <= 0) - return NULL; - - if (!(dom = virshCommandOptDomain(ctl, cmd, NULL))) - return NULL; - - if ((rc = virDomainListAllSnapshots(dom, &snapshots, flags)) < 0) - goto cleanup; - nsnapshots = rc; - - if (VIR_ALLOC_N(tmp, nsnapshots + 1) < 0) - goto cleanup; - - for (i = 0; i < nsnapshots; i++) { - const char *name = virDomainSnapshotGetName(snapshots[i]); - - if (VIR_STRDUP(tmp[i], name) < 0) - goto cleanup; - } - - VIR_STEAL_PTR(ret, tmp); - - cleanup: - virshDomainFree(dom); - for (i = 0; i < nsnapshots; i++) - virshDomainSnapshotFree(snapshots[i]); - VIR_FREE(snapshots); - return ret; -} static char * virshPagesizeNodeToString(xmlNodePtr node) diff --git a/tools/virsh-completer.h b/tools/virsh-completer.h index e27d58e536..4ebd61f3be 100644 --- a/tools/virsh-completer.h +++ b/tools/virsh-completer.h @@ -29,6 +29,7 @@ #include "virsh-completer-nwfilter.h" #include "virsh-completer-pool.h" #include "virsh-completer-secret.h" +#include "virsh-completer-snapshot.h" #include "virsh-completer-volume.h" char ** virshCommaStringListComplete(const char *input, @@ -38,10 +39,6 @@ char ** virshCheckpointNameCompleter(vshControl *ctl, const vshCmd *cmd, unsigned int flags); -char ** virshSnapshotNameCompleter(vshControl *ctl, - const vshCmd *cmd, - unsigned int flags); - char ** virshAllocpagesPagesizeCompleter(vshControl *ctl, const vshCmd *cmd, unsigned int flags); -- 2.21.0

Mixing all completers in one file does not support maintainability. Separate those completers which relate to host (e.g. they complete various host aspects) into virsh-completer-host.c Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- tools/Makefile.am | 1 + tools/virsh-completer-host.c | 148 +++++++++++++++++++++++++++++++++++ tools/virsh-completer-host.h | 31 ++++++++ tools/virsh-completer.c | 121 ---------------------------- tools/virsh-completer.h | 9 +-- 5 files changed, 181 insertions(+), 129 deletions(-) create mode 100644 tools/virsh-completer-host.c create mode 100644 tools/virsh-completer-host.h diff --git a/tools/Makefile.am b/tools/Makefile.am index 3a89ec70a2..43083f4afa 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am @@ -219,6 +219,7 @@ virsh_SOURCES = \ virsh-checkpoint.c virsh-checkpoint.h \ virsh-completer.c virsh-completer.h \ virsh-completer-domain.c virsh-completer-domain.h \ + virsh-completer-host.c virsh-completer-host.h \ virsh-completer-interface.c virsh-completer-interface.h \ virsh-completer-network.c virsh-completer-network.h \ virsh-completer-nodedev.c virsh-completer-nodedev.h \ diff --git a/tools/virsh-completer-host.c b/tools/virsh-completer-host.c new file mode 100644 index 0000000000..7e31ca2bf3 --- /dev/null +++ b/tools/virsh-completer-host.c @@ -0,0 +1,148 @@ +/* + * virsh-completer-host.c: virsh completer callbacks related to host + * + * Copyright (C) 2019 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * <http://www.gnu.org/licenses/>. + */ + +#include <config.h> + +#include "virsh-completer-host.h" +#include "viralloc.h" +#include "virsh.h" +#include "virstring.h" +#include "virxml.h" +#include "virutil.h" + +static char * +virshPagesizeNodeToString(xmlNodePtr node) +{ + VIR_AUTOFREE(char *) pagesize = NULL; + VIR_AUTOFREE(char *) unit = NULL; + unsigned long long byteval = 0; + const char *suffix = NULL; + double size = 0; + char *ret; + + pagesize = virXMLPropString(node, "size"); + unit = virXMLPropString(node, "unit"); + if (virStrToLong_ull(pagesize, NULL, 10, &byteval) < 0) + return NULL; + if (virScaleInteger(&byteval, unit, 1024, UINT_MAX) < 0) + return NULL; + size = vshPrettyCapacity(byteval, &suffix); + if (virAsprintf(&ret, "%.0f%s", size, suffix) < 0) + return NULL; + return ret; +} + +char ** +virshAllocpagesPagesizeCompleter(vshControl *ctl, + const vshCmd *cmd ATTRIBUTE_UNUSED, + unsigned int flags) +{ + VIR_AUTOPTR(xmlXPathContext) ctxt = NULL; + virshControlPtr priv = ctl->privData; + unsigned int npages = 0; + VIR_AUTOFREE(xmlNodePtr *) pages = NULL; + VIR_AUTOPTR(xmlDoc) doc = NULL; + size_t i = 0; + const char *cellnum = NULL; + bool cellno = vshCommandOptBool(cmd, "cellno"); + VIR_AUTOFREE(char *) path = NULL; + VIR_AUTOFREE(char *) cap_xml = NULL; + char **ret = NULL; + VIR_AUTOSTRINGLIST tmp = NULL; + + virCheckFlags(0, NULL); + + if (!priv->conn || virConnectIsAlive(priv->conn) <= 0) + return NULL; + + if (!(cap_xml = virConnectGetCapabilities(priv->conn))) + return NULL; + + if (!(doc = virXMLParseStringCtxt(cap_xml, _("capabilities"), &ctxt))) + return NULL; + + if (cellno && vshCommandOptStringQuiet(ctl, cmd, "cellno", &cellnum) > 0) { + if (virAsprintf(&path, + "/capabilities/host/topology/cells/cell[@id=\"%s\"]/pages", + cellnum) < 0) + return NULL; + } else { + if (virAsprintf(&path, "/capabilities/host/cpu/pages") < 0) + return NULL; + } + + npages = virXPathNodeSet(path, ctxt, &pages); + if (npages <= 0) + return NULL; + + if (VIR_ALLOC_N(tmp, npages + 1) < 0) + return NULL; + + for (i = 0; i < npages; i++) { + if (!(tmp[i] = virshPagesizeNodeToString(pages[i]))) + return NULL; + } + + VIR_STEAL_PTR(ret, tmp); + return ret; +} + + +char ** +virshCellnoCompleter(vshControl *ctl, + const vshCmd *cmd ATTRIBUTE_UNUSED, + unsigned int flags) +{ + VIR_AUTOPTR(xmlXPathContext) ctxt = NULL; + virshControlPtr priv = ctl->privData; + unsigned int ncells = 0; + VIR_AUTOFREE(xmlNodePtr *) cells = NULL; + VIR_AUTOPTR(xmlDoc) doc = NULL; + size_t i = 0; + VIR_AUTOFREE(char *) cap_xml = NULL; + char **ret = NULL; + VIR_AUTOSTRINGLIST tmp = NULL; + + virCheckFlags(0, NULL); + + if (!priv->conn || virConnectIsAlive(priv->conn) <= 0) + return NULL; + + if (!(cap_xml = virConnectGetCapabilities(priv->conn))) + return NULL; + + if (!(doc = virXMLParseStringCtxt(cap_xml, _("capabilities"), &ctxt))) + return NULL; + + ncells = virXPathNodeSet("/capabilities/host/topology/cells/cell", ctxt, &cells); + if (ncells <= 0) + return NULL; + + if (VIR_ALLOC_N(tmp, ncells + 1)) + return NULL; + + for (i = 0; i < ncells; i++) { + if (!(tmp[i] = virXMLPropString(cells[i], "id"))) + return NULL; + } + + VIR_STEAL_PTR(ret, tmp); + return ret; +} diff --git a/tools/virsh-completer-host.h b/tools/virsh-completer-host.h new file mode 100644 index 0000000000..921beb7a2d --- /dev/null +++ b/tools/virsh-completer-host.h @@ -0,0 +1,31 @@ +/* + * virsh-completer-host.h: virsh completer callbacks related to host + * + * Copyright (C) 2019 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * <http://www.gnu.org/licenses/>. + */ + +#pragma once + +#include "vsh.h" + +char ** virshAllocpagesPagesizeCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags); + +char ** virshCellnoCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags); diff --git a/tools/virsh-completer.c b/tools/virsh-completer.c index 72223803dd..ce42f62fc1 100644 --- a/tools/virsh-completer.c +++ b/tools/virsh-completer.c @@ -193,124 +193,3 @@ virshCheckpointNameCompleter(vshControl *ctl, virshDomainFree(dom); return NULL; } - - -static char * -virshPagesizeNodeToString(xmlNodePtr node) -{ - VIR_AUTOFREE(char *) pagesize = NULL; - VIR_AUTOFREE(char *) unit = NULL; - unsigned long long byteval = 0; - const char *suffix = NULL; - double size = 0; - char *ret; - - pagesize = virXMLPropString(node, "size"); - unit = virXMLPropString(node, "unit"); - if (virStrToLong_ull(pagesize, NULL, 10, &byteval) < 0) - return NULL; - if (virScaleInteger(&byteval, unit, 1024, UINT_MAX) < 0) - return NULL; - size = vshPrettyCapacity(byteval, &suffix); - if (virAsprintf(&ret, "%.0f%s", size, suffix) < 0) - return NULL; - return ret; -} - -char ** -virshAllocpagesPagesizeCompleter(vshControl *ctl, - const vshCmd *cmd ATTRIBUTE_UNUSED, - unsigned int flags) -{ - VIR_AUTOPTR(xmlXPathContext) ctxt = NULL; - virshControlPtr priv = ctl->privData; - unsigned int npages = 0; - VIR_AUTOFREE(xmlNodePtr *) pages = NULL; - VIR_AUTOPTR(xmlDoc) doc = NULL; - size_t i = 0; - const char *cellnum = NULL; - bool cellno = vshCommandOptBool(cmd, "cellno"); - VIR_AUTOFREE(char *) path = NULL; - VIR_AUTOFREE(char *) cap_xml = NULL; - char **ret = NULL; - VIR_AUTOSTRINGLIST tmp = NULL; - - virCheckFlags(0, NULL); - - if (!priv->conn || virConnectIsAlive(priv->conn) <= 0) - return NULL; - - if (!(cap_xml = virConnectGetCapabilities(priv->conn))) - return NULL; - - if (!(doc = virXMLParseStringCtxt(cap_xml, _("capabilities"), &ctxt))) - return NULL; - - if (cellno && vshCommandOptStringQuiet(ctl, cmd, "cellno", &cellnum) > 0) { - if (virAsprintf(&path, - "/capabilities/host/topology/cells/cell[@id=\"%s\"]/pages", - cellnum) < 0) - return NULL; - } else { - if (virAsprintf(&path, "/capabilities/host/cpu/pages") < 0) - return NULL; - } - - npages = virXPathNodeSet(path, ctxt, &pages); - if (npages <= 0) - return NULL; - - if (VIR_ALLOC_N(tmp, npages + 1) < 0) - return NULL; - - for (i = 0; i < npages; i++) { - if (!(tmp[i] = virshPagesizeNodeToString(pages[i]))) - return NULL; - } - - VIR_STEAL_PTR(ret, tmp); - return ret; -} - - -char ** -virshCellnoCompleter(vshControl *ctl, - const vshCmd *cmd ATTRIBUTE_UNUSED, - unsigned int flags) -{ - VIR_AUTOPTR(xmlXPathContext) ctxt = NULL; - virshControlPtr priv = ctl->privData; - unsigned int ncells = 0; - VIR_AUTOFREE(xmlNodePtr *) cells = NULL; - VIR_AUTOPTR(xmlDoc) doc = NULL; - size_t i = 0; - VIR_AUTOFREE(char *) cap_xml = NULL; - char **ret = NULL; - VIR_AUTOSTRINGLIST tmp = NULL; - - virCheckFlags(0, NULL); - - if (!priv->conn || virConnectIsAlive(priv->conn) <= 0) - return NULL; - - if (!(cap_xml = virConnectGetCapabilities(priv->conn))) - return NULL; - - if (!(doc = virXMLParseStringCtxt(cap_xml, _("capabilities"), &ctxt))) - return NULL; - - ncells = virXPathNodeSet("/capabilities/host/topology/cells/cell", ctxt, &cells); - if (ncells <= 0) - return NULL; - - if (VIR_ALLOC_N(tmp, ncells + 1)) - return NULL; - - for (i = 0; i < ncells; i++) { - if (!(tmp[i] = virXMLPropString(cells[i], "id"))) - return NULL; - } - - VIR_STEAL_PTR(ret, tmp); - return ret; -} diff --git a/tools/virsh-completer.h b/tools/virsh-completer.h index 4ebd61f3be..bfe304411b 100644 --- a/tools/virsh-completer.h +++ b/tools/virsh-completer.h @@ -23,6 +23,7 @@ #include "vsh.h" #include "virsh-completer-domain.h" +#include "virsh-completer-host.h" #include "virsh-completer-interface.h" #include "virsh-completer-network.h" #include "virsh-completer-nodedev.h" @@ -38,11 +39,3 @@ char ** virshCommaStringListComplete(const char *input, char ** virshCheckpointNameCompleter(vshControl *ctl, const vshCmd *cmd, unsigned int flags); - -char ** virshAllocpagesPagesizeCompleter(vshControl *ctl, - const vshCmd *cmd, - unsigned int flags); - -char ** virshCellnoCompleter(vshControl *ctl, - const vshCmd *cmd, - unsigned int flags); -- 2.21.0

Mixing all completers in one file does not support maintainability. Separate those completers which relate to host (e.g. they complete various checkpoint aspects) into virsh-completer-checkpoint.c Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- tools/Makefile.am | 1 + tools/virsh-completer-checkpoint.c | 78 ++++++++++++++++++++++++++++++ tools/virsh-completer-checkpoint.h | 27 +++++++++++ tools/virsh-completer.c | 52 -------------------- 4 files changed, 106 insertions(+), 52 deletions(-) create mode 100644 tools/virsh-completer-checkpoint.c create mode 100644 tools/virsh-completer-checkpoint.h diff --git a/tools/Makefile.am b/tools/Makefile.am index 43083f4afa..274f941d74 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am @@ -219,6 +219,7 @@ virsh_SOURCES = \ virsh-checkpoint.c virsh-checkpoint.h \ virsh-completer.c virsh-completer.h \ virsh-completer-domain.c virsh-completer-domain.h \ + virsh-completer-checkpoint.c virsh-completer-checkpoint.h \ virsh-completer-host.c virsh-completer-host.h \ virsh-completer-interface.c virsh-completer-interface.h \ virsh-completer-network.c virsh-completer-network.h \ diff --git a/tools/virsh-completer-checkpoint.c b/tools/virsh-completer-checkpoint.c new file mode 100644 index 0000000000..ce9d32844d --- /dev/null +++ b/tools/virsh-completer-checkpoint.c @@ -0,0 +1,78 @@ +/* + * virsh-completer-checkpoint.c: virsh completer callbacks related to checkpoints + * + * Copyright (C) 2019 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * <http://www.gnu.org/licenses/>. + */ + +#include <config.h> + +#include "virsh-completer-checkpoint.h" +#include "viralloc.h" +#include "virsh-util.h" +#include "virsh.h" +#include "virstring.h" + +char ** +virshCheckpointNameCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags) +{ + virshControlPtr priv = ctl->privData; + virDomainPtr dom = NULL; + virDomainCheckpointPtr *checkpoints = NULL; + int ncheckpoints = 0; + size_t i = 0; + char **ret = NULL; + + virCheckFlags(0, NULL); + + if (!priv->conn || virConnectIsAlive(priv->conn) <= 0) + return NULL; + + if (!(dom = virshCommandOptDomain(ctl, cmd, NULL))) + return NULL; + + if ((ncheckpoints = virDomainListAllCheckpoints(dom, &checkpoints, + flags)) < 0) + goto error; + + if (VIR_ALLOC_N(ret, ncheckpoints + 1) < 0) + goto error; + + for (i = 0; i < ncheckpoints; i++) { + const char *name = virDomainCheckpointGetName(checkpoints[i]); + + if (VIR_STRDUP(ret[i], name) < 0) + goto error; + + virshDomainCheckpointFree(checkpoints[i]); + } + VIR_FREE(checkpoints); + virshDomainFree(dom); + + return ret; + + error: + for (; i < ncheckpoints; i++) + virshDomainCheckpointFree(checkpoints[i]); + VIR_FREE(checkpoints); + for (i = 0; i < ncheckpoints; i++) + VIR_FREE(ret[i]); + VIR_FREE(ret); + virshDomainFree(dom); + return NULL; +} diff --git a/tools/virsh-completer-checkpoint.h b/tools/virsh-completer-checkpoint.h new file mode 100644 index 0000000000..c536a3ffda --- /dev/null +++ b/tools/virsh-completer-checkpoint.h @@ -0,0 +1,27 @@ +/* + * virsh-completer-checkpoint.h: virsh completer callbacks related to checkpoints + * + * Copyright (C) 2019 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * <http://www.gnu.org/licenses/>. + */ + +#pragma once + +#include "vsh.h" + +char ** virshCheckpointNameCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags); diff --git a/tools/virsh-completer.c b/tools/virsh-completer.c index ce42f62fc1..0f1709b4ba 100644 --- a/tools/virsh-completer.c +++ b/tools/virsh-completer.c @@ -141,55 +141,3 @@ virshCommaStringListComplete(const char *input, VIR_RETURN_PTR(ret); } - - -char ** -virshCheckpointNameCompleter(vshControl *ctl, - const vshCmd *cmd, - unsigned int flags) -{ - virshControlPtr priv = ctl->privData; - virDomainPtr dom = NULL; - virDomainCheckpointPtr *checkpoints = NULL; - int ncheckpoints = 0; - size_t i = 0; - char **ret = NULL; - - virCheckFlags(0, NULL); - - if (!priv->conn || virConnectIsAlive(priv->conn) <= 0) - return NULL; - - if (!(dom = virshCommandOptDomain(ctl, cmd, NULL))) - return NULL; - - if ((ncheckpoints = virDomainListAllCheckpoints(dom, &checkpoints, - flags)) < 0) - goto error; - - if (VIR_ALLOC_N(ret, ncheckpoints + 1) < 0) - goto error; - - for (i = 0; i < ncheckpoints; i++) { - const char *name = virDomainCheckpointGetName(checkpoints[i]); - - if (VIR_STRDUP(ret[i], name) < 0) - goto error; - - virshDomainCheckpointFree(checkpoints[i]); - } - VIR_FREE(checkpoints); - virshDomainFree(dom); - - return ret; - - error: - for (; i < ncheckpoints; i++) - virshDomainCheckpointFree(checkpoints[i]); - VIR_FREE(checkpoints); - for (i = 0; i < ncheckpoints; i++) - VIR_FREE(ret[i]); - VIR_FREE(ret); - virshDomainFree(dom); - return NULL; -} -- 2.21.0

Now that there is no code in virsh-completer.c it doesn't make much sense to keep those #include-s around. Delete them. Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- tools/virsh-completer.c | 13 ------------- tools/virsh-completer.h | 2 -- 2 files changed, 15 deletions(-) diff --git a/tools/virsh-completer.c b/tools/virsh-completer.c index 0f1709b4ba..1fa66b4081 100644 --- a/tools/virsh-completer.c +++ b/tools/virsh-completer.c @@ -21,21 +21,8 @@ #include <config.h> #include "virsh-completer.h" -#include "virsh-domain.h" -#include "virsh.h" -#include "virsh-pool.h" -#include "virsh-nodedev.h" -#include "virsh-util.h" -#include "virsh-secret.h" -#include "virsh-network.h" -#include "internal.h" -#include "virutil.h" #include "viralloc.h" -#include "virmacaddr.h" #include "virstring.h" -#include "virxml.h" -#include "conf/node_device_conf.h" - /** * A completer callback is a function that accepts three arguments: diff --git a/tools/virsh-completer.h b/tools/virsh-completer.h index bfe304411b..7edb8e2f7e 100644 --- a/tools/virsh-completer.h +++ b/tools/virsh-completer.h @@ -20,8 +20,6 @@ #pragma once -#include "vsh.h" - #include "virsh-completer-domain.h" #include "virsh-completer-host.h" #include "virsh-completer-interface.h" -- 2.21.0

This completer can be used to complete pool types. Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- tools/virsh-completer-pool.c | 27 +++++++++++++++++++++++++++ tools/virsh-completer-pool.h | 4 ++++ tools/virsh-pool.c | 1 + 3 files changed, 32 insertions(+) diff --git a/tools/virsh-completer-pool.c b/tools/virsh-completer-pool.c index fc01550908..9703589522 100644 --- a/tools/virsh-completer-pool.c +++ b/tools/virsh-completer-pool.c @@ -21,6 +21,7 @@ #include <config.h> #include "virsh-completer-pool.h" +#include "conf/storage_conf.h" #include "viralloc.h" #include "virsh-pool.h" #include "virsh.h" @@ -91,3 +92,29 @@ virshPoolEventNameCompleter(vshControl *ctl ATTRIBUTE_UNUSED, VIR_STEAL_PTR(ret, tmp); return ret; } + + +char ** +virshPoolTypeCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags) +{ + VIR_AUTOSTRINGLIST tmp = NULL; + const char *type_str = NULL; + size_t i = 0; + + virCheckFlags(0, NULL); + + if (vshCommandOptStringQuiet(ctl, cmd, "type", &type_str) < 0) + return NULL; + + if (VIR_ALLOC_N(tmp, VIR_STORAGE_POOL_LAST + 1) < 0) + return NULL; + + for (i = 0; i < VIR_STORAGE_POOL_LAST; i++) { + if (VIR_STRDUP(tmp[i], virStoragePoolTypeToString(i)) < 0) + return NULL; + } + + return virshCommaStringListComplete(type_str, (const char **)tmp); +} diff --git a/tools/virsh-completer-pool.h b/tools/virsh-completer-pool.h index 778ab25df2..510233fb65 100644 --- a/tools/virsh-completer-pool.h +++ b/tools/virsh-completer-pool.h @@ -29,3 +29,7 @@ char ** virshStoragePoolNameCompleter(vshControl *ctl, char ** virshPoolEventNameCompleter(vshControl *ctl, const vshCmd *cmd, unsigned int flags); + +char ** virshPoolTypeCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags); diff --git a/tools/virsh-pool.c b/tools/virsh-pool.c index 510d41b508..96ef626346 100644 --- a/tools/virsh-pool.c +++ b/tools/virsh-pool.c @@ -1095,6 +1095,7 @@ static const vshCmdOptDef opts_pool_list[] = { }, {.name = "type", .type = VSH_OT_STRING, + .completer = virshPoolTypeCompleter, .help = N_("only list pool of specified type(s) (if supported)") }, {.name = "details", -- 2.21.0

On Wed, 2019-08-07 at 10:30 +0200, Michal Privoznik wrote:
v2 of:
https://www.redhat.com/archives/libvir-list/2019-July/msg01225.html
diff to v1: - Rebase and adapt to new checkpoint completer
Michal Prívozník (15): tools: s/Nodedev/NodeDevice/ tools: Expose virshCommaStringListComplete() tools: Separate domain related completers into a file tools: Separate storage pool related completers into a file tools: Separate storage volume related completers into a file tools: Separate interface related completers into a file tools: Separate network related completers into a file tools: Separate nodedev related completers into a file tools: Separate nwfilter related completers into a file tools: Separate secret related completers into a file tools: Separate snapshot related completers into a file tools: Separate host related completers into a file tools: Separate checkpoint related completers into a file virsh-completer: Drop needless #include virsh: Introduce virshPoolTypeCompleter
tools/Makefile.am | 11 + tools/virsh-completer-checkpoint.c | 78 +++ tools/virsh-completer-checkpoint.h | 27 + tools/virsh-completer-domain.c | 314 +++++++++ tools/virsh-completer-domain.h | 55 ++ tools/virsh-completer-host.c | 148 ++++ tools/virsh-completer-host.h | 31 + tools/virsh-completer-interface.c | 67 ++ tools/virsh-completer-interface.h | 27 + tools/virsh-completer-network.c | 145 ++++ tools/virsh-completer-network.h | 35 + tools/virsh-completer-nodedev.c | 117 ++++ tools/virsh-completer-nodedev.h | 35 + tools/virsh-completer-nwfilter.c | 105 +++ tools/virsh-completer-nwfilter.h | 31 + tools/virsh-completer-pool.c | 120 ++++ tools/virsh-completer-pool.h | 35 + tools/virsh-completer-secret.c | 91 +++ tools/virsh-completer-secret.h | 31 + tools/virsh-completer-snapshot.c | 73 ++ tools/virsh-completer-snapshot.h | 27 + tools/virsh-completer-volume.c | 73 ++ tools/virsh-completer-volume.h | 28 + tools/virsh-completer.c | 1028 +------------------------- -- tools/virsh-completer.h | 114 +-- tools/virsh-nodedev.c | 16 +- tools/virsh-nodedev.h | 6 +- tools/virsh-pool.c | 1 + 28 files changed, 1730 insertions(+), 1139 deletions(-) create mode 100644 tools/virsh-completer-checkpoint.c create mode 100644 tools/virsh-completer-checkpoint.h create mode 100644 tools/virsh-completer-domain.c create mode 100644 tools/virsh-completer-domain.h create mode 100644 tools/virsh-completer-host.c create mode 100644 tools/virsh-completer-host.h create mode 100644 tools/virsh-completer-interface.c create mode 100644 tools/virsh-completer-interface.h create mode 100644 tools/virsh-completer-network.c create mode 100644 tools/virsh-completer-network.h create mode 100644 tools/virsh-completer-nodedev.c create mode 100644 tools/virsh-completer-nodedev.h create mode 100644 tools/virsh-completer-nwfilter.c create mode 100644 tools/virsh-completer-nwfilter.h create mode 100644 tools/virsh-completer-pool.c create mode 100644 tools/virsh-completer-pool.h create mode 100644 tools/virsh-completer-secret.c create mode 100644 tools/virsh-completer-secret.h create mode 100644 tools/virsh-completer-snapshot.c create mode 100644 tools/virsh-completer-snapshot.h create mode 100644 tools/virsh-completer-volume.c create mode 100644 tools/virsh-completer-volume.h
Series looks good to me, though you may want to wait for somebody with more experience to properly ack it. Reviewed-by: Jonathon Jongsma <jjongsma@redhat.com>

On 8/8/19 6:54 PM, Jonathon Jongsma wrote:
Series looks good to me, though you may want to wait for somebody with more experience to properly ack it.
Reviewed-by: Jonathon Jongsma <jjongsma@redhat.com>
That's okay. I've taken your R-b and pushed it. These are simple enough so if there was any problem, it would be obvious. Thanks! Michal
participants (2)
-
Jonathon Jongsma
-
Michal Privoznik