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

[libvirt] [PATCH v2 4/9] virsh: Add vshDomainCompleter



* global variable __my_conn renamed to vshConn
* @name is now const char *
* label cleanup renamed to error
---
 tools/virsh.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 tools/virsh.h |  2 ++
 2 files changed, 55 insertions(+)

diff --git a/tools/virsh.c b/tools/virsh.c
index af6e939..13c27df 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -88,6 +88,8 @@ static char *progname;
 
 static const vshCmdGrp cmdGroups[];
 
+virConnectPtr *vshConn;
+
 /* Bypass header poison */
 #undef strdup
 
@@ -2503,6 +2505,51 @@ vshCloseLogFile(vshControl *ctl)
 
 #ifdef USE_READLINE
 
+/* -------------
+ * Completers
+ * -------------
+ */
+
+char **
+vshDomainCompleter(unsigned int flags)
+{
+    virDomainPtr *domains;
+    size_t i;
+    char **names = NULL;
+    int ndomains;
+
+    if (!*vshConn)
+        return NULL;
+
+    ndomains = virConnectListAllDomains(*vshConn, &domains, flags);
+
+    if (ndomains < 0)
+        return NULL;
+
+    names = vshMalloc(NULL, sizeof(char *) * (ndomains + 1));
+
+    if (!names)
+        return NULL;
+
+    for (i = 0; i < ndomains; i++) {
+        const char *name = virDomainGetName(domains[i]);
+        if (VIR_STRDUP(names[i], name) < 0) {
+            virDomainFree(domains[i]);
+            goto error;
+        }
+        virDomainFree(domains[i]);
+    }
+    names[i] = NULL;
+    VIR_FREE(domains);
+    return names;
+
+error:
+    for (i = 0; names[i]; i++)
+        VIR_FREE(names[i]);
+    VIR_FREE(names);
+    return NULL;
+}
+
 /* -----------------
  * Readline stuff
  * -----------------
@@ -3510,6 +3557,7 @@ main(int argc, char **argv)
     ctl->debug = VSH_DEBUG_DEFAULT;
     ctl->escapeChar = "^]";     /* Same default as telnet */
 
+    vshConn = &ctl->conn;
 
     if (!setlocale(LC_ALL, "")) {
         perror("setlocale");
@@ -3571,6 +3619,11 @@ main(int argc, char **argv)
             exit(EXIT_FAILURE);
         }
 
+        /* Need to connect immediately after start in order to provide
+         * autocompletion for the very first command.
+         */
+        vshReconnect(ctl);
+
         do {
             const char *prompt = ctl->readonly ? VSH_PROMPT_RO : VSH_PROMPT_RW;
             ctl->cmdstr =
diff --git a/tools/virsh.h b/tools/virsh.h
index 064acde..68414e4 100644
--- a/tools/virsh.h
+++ b/tools/virsh.h
@@ -255,6 +255,8 @@ struct _vshCmdGrp {
     const vshCmdDef *commands;
 };
 
+char **vshDomainCompleter(unsigned int flags);
+
 void vshError(vshControl *ctl, const char *format, ...)
     ATTRIBUTE_FMT_PRINTF(2, 3);
 void vshOpenLogFile(vshControl *ctl);
-- 
1.8.3.1


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