[PATCH 3/4] virsh: Create completer for commands related to domains

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

 



This completer allows selection of active and/or inactive domains so
only domains in valid state for a command are listed, e.g. only inactive
for 'start' command, active for 'shutdown' and so on.
---
 tools/virsh.c |  250 +++++++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 199 insertions(+), 51 deletions(-)

diff --git a/tools/virsh.c b/tools/virsh.c
index 3dabb10..6a63363 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -600,6 +600,119 @@ vshReconnect(vshControl *ctl)
 }
 
 /* ---------------
+ * Completers
+ * ---------------
+ */
+#define ACTIVE (1<<0)
+#define INACTIVE (1<<1)
+static char *
+complt_listDomainsFlags(const char *text ATTRIBUTE_UNUSED, int state, unsigned int flags)
+{
+    static int len = 0;
+    static int index_active = 0;
+    static int index_inactive = 0;
+    static int maxid = 0;
+    static int *ids = NULL;
+    static int maxname = 0;
+    static char **names = NULL;
+    char *ret;
+
+    /*
+     * TODO:
+     * If we are not connected, should we connect here
+     * or simply return NULL and thus not complete 'live data'?
+     */
+    if (!conn)
+        return NULL;
+
+    if (!state) {
+        len = strlen(text);
+        maxid = 0;
+        maxname = 0;
+
+        if (flags & ACTIVE) {
+            maxid = virConnectNumOfDomains(conn);
+            if (maxid < 0)
+                goto cleanup;
+            if (maxid) {
+                ids = vshMalloc(NULL, sizeof(int) * maxid);
+                if ((maxid = virConnectListDomains(conn, ids, maxid)) < 0)
+                    goto cleanup;
+
+                qsort(ids, maxid, sizeof(int), idsorter);
+            }
+        }
+        if (flags & INACTIVE) {
+            maxname = virConnectNumOfDefinedDomains(conn);
+            if (maxname < 0)
+                goto cleanup;
+            if (maxname) {
+                names = vshMalloc(NULL, sizeof(char *) * maxname);
+                if ((maxname =
+                     virConnectListDefinedDomains(conn, names, maxname)) < 0)
+                    goto cleanup;
+
+                qsort(names, maxname, sizeof(char *), namesorter);
+            }
+        }
+
+        index_active = 0;
+        index_inactive = 0;
+    }
+
+    while (index_active < maxid) {
+        virDomainPtr dom = virDomainLookupByID(conn, ids[index_active]);
+        index_active++;
+
+        if (!dom)
+            continue;
+
+        ret = (char *) virDomainGetName(dom);
+        if (STRNEQLEN(ret, text, len)) {
+            virDomainFree(dom);
+            continue;
+        }
+        ret = vshStrdup(NULL, ret);
+        virDomainFree(dom);
+
+        return ret;
+    }
+
+    while (index_inactive < maxname) {
+        ret = names[index_inactive];
+        index_inactive++;
+
+        if (STREQLEN(ret, text, len))
+            return ret;
+
+        VIR_FREE(ret);
+    }
+
+cleanup:
+    VIR_FREE(ids);
+    VIR_FREE(names);
+    return NULL;
+}
+
+static char *
+complt_listActiveDomains(const char *text, int state)
+{
+    return complt_listDomainsFlags(text, state, ACTIVE);
+}
+
+static char *
+complt_listInactiveDomains(const char *text, int state)
+{
+    return complt_listDomainsFlags(text, state, INACTIVE);
+}
+
+static char *
+complt_listAllDomains(const char *text, int state)
+{
+    return complt_listDomainsFlags(text, state, ACTIVE | INACTIVE);
+}
+
+/* ---------------
  * Commands
  * ---------------
  */
@@ -11615,84 +11728,119 @@ cleanup:
 
 static const vshCmdDef domManagementCmds[] = {
     {"attach-device", cmdAttachDevice, opts_attach_device,
-     info_attach_device, 0, NULL},
+     info_attach_device, 0, complt_listAllDomains},
     {"attach-disk", cmdAttachDisk, opts_attach_disk,
-     info_attach_disk, 0, NULL},
+     info_attach_disk, 0, complt_listAllDomains},
     {"attach-interface", cmdAttachInterface, opts_attach_interface,
-     info_attach_interface, 0, NULL},
-    {"autostart", cmdAutostart, opts_autostart, info_autostart, 0, NULL},
-    {"blkiotune", cmdBlkiotune, opts_blkiotune, info_blkiotune, 0, NULL},
+     info_attach_interface, 0, complt_listAllDomains},
+    {"autostart", cmdAutostart, opts_autostart,
+     info_autostart, 0, complt_listAllDomains},
+    {"blkiotune", cmdBlkiotune, opts_blkiotune,
+      info_blkiotune, 0, complt_listAllDomains},
 #ifndef WIN32
-    {"console", cmdConsole, opts_console, info_console, 0, NULL},
+    {"console", cmdConsole, opts_console,
+      info_console, 0, complt_listActiveDomains},
 #endif
     {"cpu-baseline", cmdCPUBaseline, opts_cpu_baseline,
      info_cpu_baseline, 0, NULL},
     {"cpu-compare", cmdCPUCompare, opts_cpu_compare, info_cpu_compare, 0, NULL},
     {"create", cmdCreate, opts_create, info_create, 0, NULL},
     {"define", cmdDefine, opts_define, info_define, 0, NULL},
-    {"destroy", cmdDestroy, opts_destroy, info_destroy, 0, NULL},
+    {"destroy", cmdDestroy, opts_destroy,
+      info_destroy, 0, complt_listActiveDomains},
     {"detach-device", cmdDetachDevice, opts_detach_device,
-     info_detach_device, 0, NULL},
-    {"detach-disk", cmdDetachDisk, opts_detach_disk, info_detach_disk, 0, NULL},
+     info_detach_device, 0, complt_listAllDomains},
+    {"detach-disk", cmdDetachDisk, opts_detach_disk,
+     info_detach_disk, 0, complt_listAllDomains},
     {"detach-interface", cmdDetachInterface, opts_detach_interface,
-     info_detach_interface, 0, NULL},
-    {"domid", cmdDomid, opts_domid, info_domid, 0, NULL},
+     info_detach_interface, 0, complt_listAllDomains},
+    {"domid", cmdDomid, opts_domid, info_domid, 0, complt_listActiveDomains},
     {"domjobabort", cmdDomjobabort, opts_domjobabort,
-     info_domjobabort, 0, NULL},
-    {"domjobinfo", cmdDomjobinfo, opts_domjobinfo, info_domjobinfo, 0, NULL},
-    {"domname", cmdDomname, opts_domname, info_domname, 0, NULL},
-    {"domuuid", cmdDomuuid, opts_domuuid, info_domuuid, 0, NULL},
+     info_domjobabort, 0, complt_listActiveDomains},
+    {"domjobinfo", cmdDomjobinfo, opts_domjobinfo,
+      info_domjobinfo, 0, complt_listActiveDomains},
+    {"domname", cmdDomname, opts_domname,
+     info_domname, 0, complt_listAllDomains},
+    {"domuuid", cmdDomuuid, opts_domuuid,
+     info_domuuid, 0, complt_listAllDomains},
     {"domxml-from-native", cmdDomXMLFromNative, opts_domxmlfromnative,
      info_domxmlfromnative, 0, NULL},
     {"domxml-to-native", cmdDomXMLToNative, opts_domxmltonative,
      info_domxmltonative, 0, NULL},
-    {"dump", cmdDump, opts_dump, info_dump, 0, NULL},
-    {"dumpxml", cmdDumpXML, opts_dumpxml, info_dumpxml, 0, NULL},
-    {"edit", cmdEdit, opts_edit, info_edit, 0, NULL},
-    {"inject-nmi", cmdInjectNMI, opts_inject_nmi, info_inject_nmi, 0, NULL},
+    {"dump", cmdDump, opts_dump, info_dump, 0, complt_listActiveDomains},
+    {"dumpxml", cmdDumpXML, opts_dumpxml,
+     info_dumpxml, 0, complt_listAllDomains},
+    {"edit", cmdEdit, opts_edit, info_edit, 0, complt_listAllDomains},
+    {"inject-nmi", cmdInjectNMI, opts_inject_nmi,
+     info_inject_nmi, 0, complt_listActiveDomains},
     {"managedsave", cmdManagedSave, opts_managedsave,
-     info_managedsave, 0, NULL},
+     info_managedsave, 0, complt_listActiveDomains},
     {"managedsave-remove", cmdManagedSaveRemove, opts_managedsaveremove,
-     info_managedsaveremove, 0, NULL},
+     info_managedsaveremove, 0, complt_listAllDomains},
     {"maxvcpus", cmdMaxvcpus, opts_maxvcpus, info_maxvcpus, 0, NULL},
-    {"memtune", cmdMemtune, opts_memtune, info_memtune, 0, NULL},
-    {"migrate", cmdMigrate, opts_migrate, info_migrate, 0, NULL},
+    {"memtune", cmdMemtune, opts_memtune,
+     info_memtune, 0, complt_listAllDomains},
+    {"migrate", cmdMigrate, opts_migrate,
+     info_migrate, 0, complt_listAllDomains},
     {"migrate-setmaxdowntime", cmdMigrateSetMaxDowntime,
-     opts_migrate_setmaxdowntime, info_migrate_setmaxdowntime, 0, NULL},
+     opts_migrate_setmaxdowntime, info_migrate_setmaxdowntime, 0,
+     complt_listAllDomains},
     {"migrate-setspeed", cmdMigrateSetMaxSpeed,
-     opts_migrate_setspeed, info_migrate_setspeed, 0, NULL},
-    {"reboot", cmdReboot, opts_reboot, info_reboot, 0, NULL},
-    {"restore", cmdRestore, opts_restore, info_restore, 0, NULL},
-    {"resume", cmdResume, opts_resume, info_resume, 0, NULL},
-    {"save", cmdSave, opts_save, info_save, 0, NULL},
-    {"schedinfo", cmdSchedinfo, opts_schedinfo, info_schedinfo, 0, NULL},
-    {"screenshot", cmdScreenshot, opts_screenshot, info_screenshot, 0, NULL},
-    {"setmaxmem", cmdSetmaxmem, opts_setmaxmem, info_setmaxmem, 0, NULL},
-    {"setmem", cmdSetmem, opts_setmem, info_setmem, 0, NULL},
-    {"setvcpus", cmdSetvcpus, opts_setvcpus, info_setvcpus, 0, NULL},
-    {"shutdown", cmdShutdown, opts_shutdown, info_shutdown, 0, NULL},
-    {"start", cmdStart, opts_start, info_start, 0, NULL},
-    {"suspend", cmdSuspend, opts_suspend, info_suspend, 0, NULL},
-    {"ttyconsole", cmdTTYConsole, opts_ttyconsole, info_ttyconsole, 0, NULL},
-    {"undefine", cmdUndefine, opts_undefine, info_undefine, 0, NULL},
+     opts_migrate_setspeed, info_migrate_setspeed, 0, complt_listAllDomains},
+    {"reboot", cmdReboot, opts_reboot,
+     info_reboot, 0, complt_listActiveDomains},
+    {"restore", cmdRestore, opts_restore,
+     info_restore, 0, complt_listInactiveDomains},
+    {"resume", cmdResume, opts_resume,
+     info_resume, 0, complt_listActiveDomains},
+    {"save", cmdSave, opts_save, info_save, 0, complt_listActiveDomains},
+    {"schedinfo", cmdSchedinfo, opts_schedinfo,
+     info_schedinfo, 0, complt_listAllDomains},
+    {"screenshot", cmdScreenshot, opts_screenshot,
+     info_screenshot, 0, complt_listActiveDomains},
+    {"setmaxmem", cmdSetmaxmem, opts_setmaxmem,
+     info_setmaxmem, 0, complt_listAllDomains},
+    {"setmem", cmdSetmem, opts_setmem, info_setmem, 0, complt_listAllDomains},
+    {"setvcpus", cmdSetvcpus, opts_setvcpus,
+     info_setvcpus, 0, complt_listAllDomains},
+    {"shutdown", cmdShutdown, opts_shutdown,
+     info_shutdown, 0, complt_listActiveDomains},
+    {"start", cmdStart, opts_start, info_start, 0, complt_listInactiveDomains},
+    {"suspend", cmdSuspend, opts_suspend,
+     info_suspend, 0, complt_listActiveDomains},
+    {"ttyconsole", cmdTTYConsole, opts_ttyconsole,
+     info_ttyconsole, 0, complt_listActiveDomains},
+    {"undefine", cmdUndefine, opts_undefine,
+     info_undefine, 0, complt_listInactiveDomains},
     {"update-device", cmdUpdateDevice, opts_update_device,
-     info_update_device, 0, NULL},
-    {"vcpucount", cmdVcpucount, opts_vcpucount, info_vcpucount, 0, NULL},
-    {"vcpuinfo", cmdVcpuinfo, opts_vcpuinfo, info_vcpuinfo, 0, NULL},
-    {"vcpupin", cmdVcpuPin, opts_vcpupin, info_vcpupin, 0, NULL},
+     info_update_device, 0, complt_listAllDomains},
+    {"vcpucount", cmdVcpucount, opts_vcpucount,
+     info_vcpucount, 0, complt_listAllDomains},
+    {"vcpuinfo", cmdVcpuinfo, opts_vcpuinfo,
+     info_vcpuinfo, 0, complt_listAllDomains},
+    {"vcpupin", cmdVcpuPin, opts_vcpupin,
+     info_vcpupin, 0, complt_listAllDomains},
     {"version", cmdVersion, opts_version, info_version, 0, NULL},
-    {"vncdisplay", cmdVNCDisplay, opts_vncdisplay, info_vncdisplay, 0, NULL},
+    {"vncdisplay", cmdVNCDisplay, opts_vncdisplay,
+     info_vncdisplay, 0, complt_listActiveDomains},
     {NULL, NULL, NULL, NULL, 0, NULL}
 };
 
 static const vshCmdDef domMonitoringCmds[] = {
-    {"domblkinfo", cmdDomblkinfo, opts_domblkinfo, info_domblkinfo, 0, NULL},
-    {"domblkstat", cmdDomblkstat, opts_domblkstat, info_domblkstat, 0, NULL},
-    {"domcontrol", cmdDomControl, opts_domcontrol, info_domcontrol, 0, NULL},
-    {"domifstat", cmdDomIfstat, opts_domifstat, info_domifstat, 0, NULL},
-    {"dominfo", cmdDominfo, opts_dominfo, info_dominfo, 0, NULL},
-    {"dommemstat", cmdDomMemStat, opts_dommemstat, info_dommemstat, 0, NULL},
-    {"domstate", cmdDomstate, opts_domstate, info_domstate, 0, NULL},
+    {"domblkinfo", cmdDomblkinfo, opts_domblkinfo,
+     info_domblkinfo, 0, complt_listActiveDomains},
+    {"domblkstat", cmdDomblkstat, opts_domblkstat,
+     info_domblkstat, 0, complt_listActiveDomains},
+    {"domcontrol", cmdDomControl, opts_domcontrol,
+     info_domcontrol, 0, complt_listActiveDomains},
+    {"domifstat", cmdDomIfstat, opts_domifstat,
+     info_domifstat, 0, complt_listActiveDomains},
+    {"dominfo", cmdDominfo, opts_dominfo,
+     info_dominfo, 0, complt_listAllDomains},
+    {"dommemstat", cmdDomMemStat, opts_dommemstat,
+     info_dommemstat, 0, complt_listActiveDomains},
+    {"domstate", cmdDomstate, opts_domstate,
+     info_domstate, 0, complt_listAllDomains},
     {"list", cmdList, opts_list, info_list, 0, NULL},
     {NULL, NULL, NULL, NULL, 0, NULL}
 };
-- 
1.7.5.rc3

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]