On 11/10/20 10:50 AM, Lin Ma wrote:
Signed-off-by: Lin Ma <l...@suse.com>
---
  tools/virsh-completer-domain.c | 55 ++++++++++++++++++++++++++++++++++
  tools/virsh-completer-domain.h |  4 +++
  tools/virsh-domain.c           |  1 +
  3 files changed, 60 insertions(+)

diff --git a/tools/virsh-completer-domain.c b/tools/virsh-completer-domain.c
index f653471b89..dc66317561 100644
--- a/tools/virsh-completer-domain.c
+++ b/tools/virsh-completer-domain.c
@@ -656,3 +656,58 @@ virshDomainVcpulistViaAgentCompleter(vshControl *ctl,
      virshDomainFree(dom);
      return ret;
  }
+
+char **
+virshDomainConsoleCompleter(vshControl *ctl,
+                            const vshCmd *cmd,
+                            unsigned int flags)
+{
+    virshControlPtr priv = ctl->privData;
+    g_autoptr(xmlDoc) xmldoc = NULL;
+    g_autoptr(xmlXPathContext) ctxt = NULL;
+    int nserials;
+    int nparallels;
+    g_autofree xmlNodePtr *serials = NULL;
+    g_autofree xmlNodePtr *parallels = NULL;
+    size_t i;
+    size_t offset = 0;
+    VIR_AUTOSTRINGLIST tmp = NULL;
+
+    virCheckFlags(0, NULL);
+
+    if (!priv->conn || virConnectIsAlive(priv->conn) <= 0)
+        return NULL;
+
+    if (virshDomainGetXML(ctl, cmd, 0, &xmldoc, &ctxt) < 0)
+        return NULL;
+
+    nserials = virXPathNodeSet("./devices/serial", ctxt, &serials);
+    if (nserials < 0)
+        return NULL;
+
+    nparallels = virXPathNodeSet("./devices/parallel", ctxt, &parallels);
+    if (nparallels < 0)
+        return NULL;
+
+    tmp = g_new0(char *, nserials + nparallels + 1);
+
+    for (i = 0; i < nserials; i++) {
+        ctxt->node = serials[i];
+
+        if (STRNEQ(virXPathString("string(./@type)", ctxt), "pty"))

virXPathString() returns an allocated string which is leaked here ..

+            continue;
+
+        tmp[offset++] = virXPathString("string(./alias/@name)", ctxt);
+    }
+
+    for (i = 0; i < nparallels; i++) {
+        ctxt->node = parallels[i];
+
+        if (STRNEQ(virXPathString("string(./@type)", ctxt), "pty"))

.. and here.

+            continue;
+
+        tmp[offset++] = virXPathString("string(./alias/@name)", ctxt);
+    }
+
+    return g_steal_pointer(&tmp);
+}
diff --git a/tools/virsh-completer-domain.h b/tools/virsh-completer-domain.h
index d5021f6aa6..02fea2fe94 100644
--- a/tools/virsh-completer-domain.h
+++ b/tools/virsh-completer-domain.h
@@ -94,3 +94,7 @@ char ** virshDomainCpulistCompleter(vshControl *ctl,
  char ** virshDomainVcpulistViaAgentCompleter(vshControl *ctl,
                                               const vshCmd *cmd,
                                               unsigned int flags);
+
+char ** virshDomainConsoleCompleter(vshControl *ctl,
+                                    const vshCmd *cmd,
+                                    unsigned int flags);
diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c
index 675d96440d..022dbdca3c 100644
--- a/tools/virsh-domain.c
+++ b/tools/virsh-domain.c
@@ -2960,6 +2960,7 @@ static const vshCmdOptDef opts_console[] = {
      VIRSH_COMMON_OPT_DOMAIN_FULL(VIR_CONNECT_LIST_DOMAINS_ACTIVE),
      {.name = "devname", /* sc_prohibit_devname */
       .type = VSH_OT_STRING,
+     .completer = virshDomainConsoleCompleter,
       .help = N_("character device name")
      },
      {.name = "force",


Michal

Reply via email to