[PATCHv3 4/6] virsh: Add vshDomainCompleter

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

 



Function vshDomainCompler returns domains names which can be used
by various virsh commands, for example:

virsh> start <TAB>
fedora      domain_foo    domain_bar
virsh> start f<TAB>
virsh> start fedora

Michal Privoznik recommended to add global variable virConnectPtr *__my_conn
so we can get the list of domains from the virConnecTListAllDomains().

vshReconnect() is called before the first command is executed
in order to provide autocompletion for the very first command.
---
v2
* global variable __my_conn renamed to vshConn
* @name is now const char *
* label cleanup renamed to error

v3
* removed useless if
* used virStringFreeList() instead of iteration
* removed vshReconnect() call from main(), reconnecting now takes place
  in vshReadlineCommandCompletionGenerator() and only for relevant cmds
* added vshControl *vshCtl instead of virConnectPtr *vshConn because
  vshCtl is needed in order to call vshReconnect()
* moved all .completer = vshDomainCompleter initializations from
  other patches into this

 tools/virsh-domain-monitor.c |  32 +++++--
 tools/virsh-domain.c         | 221 ++++++++++++++++++++++++++++++++++---------
 tools/virsh-snapshot.c       |  45 +++++++--
 tools/virsh.c                |  48 ++++++++++
 tools/virsh.h                |   2 +
 5 files changed, 285 insertions(+), 63 deletions(-)

diff --git a/tools/virsh-domain-monitor.c b/tools/virsh-domain-monitor.c
index b29b82a..0f30902 100644
--- a/tools/virsh-domain-monitor.c
+++ b/tools/virsh-domain-monitor.c
@@ -1876,7 +1876,9 @@ const vshCmdDef domMonitoringCmds[] = {
      .handler = cmdDomBlkError,
      .opts = opts_domblkerror,
      .info = info_domblkerror,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE
     },
     {.name = "domblkinfo",
      .handler = cmdDomblkinfo,
@@ -1888,7 +1890,10 @@ const vshCmdDef domMonitoringCmds[] = {
      .handler = cmdDomblklist,
      .opts = opts_domblklist,
      .info = info_domblklist,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE |
+                        VIR_CONNECT_LIST_DOMAINS_INACTIVE
     },
     {.name = "domblkstat",
      .handler = cmdDomblkstat,
@@ -1900,7 +1905,9 @@ const vshCmdDef domMonitoringCmds[] = {
      .handler = cmdDomControl,
      .opts = opts_domcontrol,
      .info = info_domcontrol,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE
     },
     {.name = "domif-getlink",
      .handler = cmdDomIfGetLink,
@@ -1912,7 +1919,10 @@ const vshCmdDef domMonitoringCmds[] = {
      .handler = cmdDomiflist,
      .opts = opts_domiflist,
      .info = info_domiflist,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE |
+                        VIR_CONNECT_LIST_DOMAINS_INACTIVE
     },
     {.name = "domifstat",
      .handler = cmdDomIfstat,
@@ -1924,19 +1934,27 @@ const vshCmdDef domMonitoringCmds[] = {
      .handler = cmdDominfo,
      .opts = opts_dominfo,
      .info = info_dominfo,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE |
+                        VIR_CONNECT_LIST_DOMAINS_INACTIVE
     },
     {.name = "dommemstat",
      .handler = cmdDomMemStat,
      .opts = opts_dommemstat,
      .info = info_dommemstat,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE
     },
     {.name = "domstate",
      .handler = cmdDomstate,
      .opts = opts_domstate,
      .info = info_domstate,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE |
+                        VIR_CONNECT_LIST_DOMAINS_INACTIVE
     },
     {.name = "list",
      .handler = cmdList,
diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c
index 5d4913d..5b200a3 100644
--- a/tools/virsh-domain.c
+++ b/tools/virsh-domain.c
@@ -10358,7 +10358,10 @@ const vshCmdDef domManagementCmds[] = {
      .handler = cmdAutostart,
      .opts = opts_autostart,
      .info = info_autostart,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE |
+                        VIR_CONNECT_LIST_DOMAINS_INACTIVE
     },
     {.name = "blkdeviotune",
      .handler = cmdBlkdeviotune,
@@ -10370,7 +10373,10 @@ const vshCmdDef domManagementCmds[] = {
      .handler = cmdBlkiotune,
      .opts = opts_blkiotune,
      .info = info_blkiotune,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE |
+                        VIR_CONNECT_LIST_DOMAINS_INACTIVE
     },
     {.name = "blockcommit",
      .handler = cmdBlockCommit,
@@ -10413,7 +10419,10 @@ const vshCmdDef domManagementCmds[] = {
      .handler = cmdConsole,
      .opts = opts_console,
      .info = info_console,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE |
+                        VIR_CONNECT_LIST_DOMAINS_INACTIVE
     },
 #endif
     {.name = "cpu-baseline",
@@ -10450,13 +10459,18 @@ const vshCmdDef domManagementCmds[] = {
      .handler = cmdDesc,
      .opts = opts_desc,
      .info = info_desc,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE |
+                        VIR_CONNECT_LIST_DOMAINS_INACTIVE
     },
     {.name = "destroy",
      .handler = cmdDestroy,
      .opts = opts_destroy,
      .info = info_destroy,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE
     },
     {.name = "detach-device",
      .handler = cmdDetachDevice,
@@ -10480,31 +10494,45 @@ const vshCmdDef domManagementCmds[] = {
      .handler = cmdDomDisplay,
      .opts = opts_domdisplay,
      .info = info_domdisplay,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE |
+                        VIR_CONNECT_LIST_DOMAINS_INACTIVE
     },
     {.name = "domfstrim",
      .handler = cmdDomFSTrim,
      .opts = opts_domfstrim,
      .info = info_domfstrim,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE |
+                        VIR_CONNECT_LIST_DOMAINS_INACTIVE
     },
     {.name = "domhostname",
      .handler = cmdDomHostname,
      .opts = opts_domhostname,
      .info = info_domhostname,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE |
+                        VIR_CONNECT_LIST_DOMAINS_INACTIVE
     },
     {.name = "domid",
      .handler = cmdDomid,
      .opts = opts_domid,
      .info = info_domid,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE |
+                        VIR_CONNECT_LIST_DOMAINS_INACTIVE
     },
     {.name = "domif-setlink",
      .handler = cmdDomIfSetLink,
      .opts = opts_domif_setlink,
      .info = info_domif_setlink,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE
     },
     {.name = "domiftune",
      .handler = cmdDomIftune,
@@ -10522,7 +10550,9 @@ const vshCmdDef domManagementCmds[] = {
      .handler = cmdDomjobinfo,
      .opts = opts_domjobinfo,
      .info = info_domjobinfo,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE
     },
     {.name = "domname",
      .handler = cmdDomname,
@@ -10534,19 +10564,27 @@ const vshCmdDef domManagementCmds[] = {
      .handler = cmdDomPMSuspend,
      .opts = opts_dom_pm_suspend,
      .info = info_dom_pm_suspend,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE |
+                        VIR_CONNECT_LIST_DOMAINS_RUNNING
     },
     {.name = "dompmwakeup",
      .handler = cmdDomPMWakeup,
      .opts = opts_dom_pm_wakeup,
      .info = info_dom_pm_wakeup,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE
     },
     {.name = "domuuid",
      .handler = cmdDomuuid,
      .opts = opts_domuuid,
      .info = info_domuuid,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE |
+                        VIR_CONNECT_LIST_DOMAINS_INACTIVE
     },
     {.name = "domxml-from-native",
      .handler = cmdDomXMLFromNative,
@@ -10564,31 +10602,46 @@ const vshCmdDef domManagementCmds[] = {
      .handler = cmdDump,
      .opts = opts_dump,
      .info = info_dump,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE |
+                        VIR_CONNECT_LIST_DOMAINS_RUNNING
     },
     {.name = "dumpxml",
      .handler = cmdDumpXML,
      .opts = opts_dumpxml,
      .info = info_dumpxml,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE |
+                        VIR_CONNECT_LIST_DOMAINS_INACTIVE
     },
     {.name = "edit",
      .handler = cmdEdit,
      .opts = opts_edit,
      .info = info_edit,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE |
+                        VIR_CONNECT_LIST_DOMAINS_INACTIVE
     },
     {.name = "inject-nmi",
      .handler = cmdInjectNMI,
      .opts = opts_inject_nmi,
      .info = info_inject_nmi,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE |
+                        VIR_CONNECT_LIST_DOMAINS_RUNNING
     },
     {.name = "send-key",
      .handler = cmdSendKey,
      .opts = opts_send_key,
      .info = info_send_key,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE |
+                        VIR_CONNECT_LIST_DOMAINS_RUNNING
     },
     {.name = "send-process-signal",
      .handler = cmdSendProcessSignal,
@@ -10600,19 +10653,29 @@ const vshCmdDef domManagementCmds[] = {
      .handler = cmdLxcEnterNamespace,
      .opts = opts_lxc_enter_namespace,
      .info = info_lxc_enter_namespace,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE |
+                        VIR_CONNECT_LIST_DOMAINS_INACTIVE
     },
     {.name = "managedsave",
      .handler = cmdManagedSave,
      .opts = opts_managedsave,
      .info = info_managedsave,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE |
+                        VIR_CONNECT_LIST_DOMAINS_RUNNING |
+                        VIR_CONNECT_LIST_DOMAINS_NO_MANAGEDSAVE
     },
     {.name = "managedsave-remove",
      .handler = cmdManagedSaveRemove,
      .opts = opts_managedsaveremove,
      .info = info_managedsaveremove,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_INACTIVE |
+                        VIR_CONNECT_LIST_DOMAINS_MANAGEDSAVE
     },
     {.name = "maxvcpus",
      .handler = cmdMaxvcpus,
@@ -10624,7 +10687,10 @@ const vshCmdDef domManagementCmds[] = {
      .handler = cmdMemtune,
      .opts = opts_memtune,
      .info = info_memtune,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE |
+                        VIR_CONNECT_LIST_DOMAINS_INACTIVE
     },
     {.name = "migrate",
      .handler = cmdMigrate,
@@ -10636,31 +10702,46 @@ const vshCmdDef domManagementCmds[] = {
      .handler = cmdMigrateSetMaxDowntime,
      .opts = opts_migrate_setmaxdowntime,
      .info = info_migrate_setmaxdowntime,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE |
+                        VIR_CONNECT_LIST_DOMAINS_INACTIVE
     },
     {.name = "migrate-compcache",
      .handler = cmdMigrateCompCache,
      .opts = opts_migrate_compcache,
      .info = info_migrate_compcache,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE |
+                        VIR_CONNECT_LIST_DOMAINS_RUNNING
     },
     {.name = "migrate-setspeed",
      .handler = cmdMigrateSetMaxSpeed,
      .opts = opts_migrate_setspeed,
      .info = info_migrate_setspeed,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE |
+                        VIR_CONNECT_LIST_DOMAINS_INACTIVE
     },
     {.name = "migrate-getspeed",
      .handler = cmdMigrateGetMaxSpeed,
      .opts = opts_migrate_getspeed,
      .info = info_migrate_getspeed,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE |
+                        VIR_CONNECT_LIST_DOMAINS_INACTIVE
     },
     {.name = "numatune",
      .handler = cmdNumatune,
      .opts = opts_numatune,
      .info = info_numatune,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE |
+                        VIR_CONNECT_LIST_DOMAINS_INACTIVE
     },
     {.name = "qemu-attach",
      .handler = cmdQemuAttach,
@@ -10672,25 +10753,35 @@ const vshCmdDef domManagementCmds[] = {
      .handler = cmdQemuMonitorCommand,
      .opts = opts_qemu_monitor_command,
      .info = info_qemu_monitor_command,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE |
+                        VIR_CONNECT_LIST_DOMAINS_INACTIVE
     },
     {.name = "qemu-agent-command",
      .handler = cmdQemuAgentCommand,
      .opts = opts_qemu_agent_command,
      .info = info_qemu_agent_command,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE |
+                        VIR_CONNECT_LIST_DOMAINS_INACTIVE
     },
     {.name = "reboot",
      .handler = cmdReboot,
      .opts = opts_reboot,
      .info = info_reboot,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE
     },
     {.name = "reset",
      .handler = cmdReset,
      .opts = opts_reset,
      .info = info_reset,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE
     },
     {.name = "restore",
      .handler = cmdRestore,
@@ -10702,7 +10793,10 @@ const vshCmdDef domManagementCmds[] = {
      .handler = cmdResume,
      .opts = opts_resume,
      .info = info_resume,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE |
+                        VIR_CONNECT_LIST_DOMAINS_PAUSED
     },
     {.name = "save",
      .handler = cmdSave,
@@ -10732,61 +10826,83 @@ const vshCmdDef domManagementCmds[] = {
      .handler = cmdSchedinfo,
      .opts = opts_schedinfo,
      .info = info_schedinfo,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE |
+                        VIR_CONNECT_LIST_DOMAINS_INACTIVE
     },
     {.name = "screenshot",
      .handler = cmdScreenshot,
      .opts = opts_screenshot,
      .info = info_screenshot,
-     .flags = 0
+     .flags = 0,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE
     },
     {.name = "setmaxmem",
      .handler = cmdSetmaxmem,
      .opts = opts_setmaxmem,
      .info = info_setmaxmem,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_INACTIVE
     },
     {.name = "setmem",
      .handler = cmdSetmem,
      .opts = opts_setmem,
      .info = info_setmem,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE
     },
     {.name = "setvcpus",
      .handler = cmdSetvcpus,
      .opts = opts_setvcpus,
      .info = info_setvcpus,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE
     },
     {.name = "shutdown",
      .handler = cmdShutdown,
      .opts = opts_shutdown,
      .info = info_shutdown,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE
     },
     {.name = "start",
      .handler = cmdStart,
      .opts = opts_start,
      .info = info_start,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_INACTIVE |
+                        VIR_CONNECT_LIST_DOMAINS_PERSISTENT
     },
     {.name = "suspend",
      .handler = cmdSuspend,
      .opts = opts_suspend,
      .info = info_suspend,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE
     },
     {.name = "ttyconsole",
      .handler = cmdTTYConsole,
      .opts = opts_ttyconsole,
      .info = info_ttyconsole,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE
     },
     {.name = "undefine",
      .handler = cmdUndefine,
      .opts = opts_undefine,
      .info = info_undefine,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE |
+                        VIR_CONNECT_LIST_DOMAINS_INACTIVE
     },
     {.name = "update-device",
      .handler = cmdUpdateDevice,
@@ -10804,25 +10920,36 @@ const vshCmdDef domManagementCmds[] = {
      .handler = cmdVcpuinfo,
      .opts = opts_vcpuinfo,
      .info = info_vcpuinfo,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE |
+                        VIR_CONNECT_LIST_DOMAINS_INACTIVE
     },
     {.name = "vcpupin",
      .handler = cmdVcpuPin,
      .opts = opts_vcpupin,
      .info = info_vcpupin,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE |
+                        VIR_CONNECT_LIST_DOMAINS_INACTIVE
     },
     {.name = "emulatorpin",
      .handler = cmdEmulatorPin,
      .opts = opts_emulatorpin,
      .info = info_emulatorpin,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE |
+                        VIR_CONNECT_LIST_DOMAINS_INACTIVE
     },
     {.name = "vncdisplay",
      .handler = cmdVNCDisplay,
      .opts = opts_vncdisplay,
      .info = info_vncdisplay,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE
     },
     {.name = NULL}
 };
diff --git a/tools/virsh-snapshot.c b/tools/virsh-snapshot.c
index e37a5b3..77f9273 100644
--- a/tools/virsh-snapshot.c
+++ b/tools/virsh-snapshot.c
@@ -2006,25 +2006,37 @@ const vshCmdDef snapshotCmds[] = {
      .handler = cmdSnapshotCreate,
      .opts = opts_snapshot_create,
      .info = info_snapshot_create,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE |
+                        VIR_CONNECT_LIST_DOMAINS_INACTIVE
     },
     {.name = "snapshot-create-as",
      .handler = cmdSnapshotCreateAs,
      .opts = opts_snapshot_create_as,
      .info = info_snapshot_create_as,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE |
+                        VIR_CONNECT_LIST_DOMAINS_INACTIVE
     },
     {.name = "snapshot-current",
      .handler = cmdSnapshotCurrent,
      .opts = opts_snapshot_current,
      .info = info_snapshot_current,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE |
+                        VIR_CONNECT_LIST_DOMAINS_INACTIVE
     },
     {.name = "snapshot-delete",
      .handler = cmdSnapshotDelete,
      .opts = opts_snapshot_delete,
      .info = info_snapshot_delete,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE |
+                        VIR_CONNECT_LIST_DOMAINS_INACTIVE
     },
     {.name = "snapshot-dumpxml",
      .handler = cmdSnapshotDumpXML,
@@ -2036,31 +2048,46 @@ const vshCmdDef snapshotCmds[] = {
      .handler = cmdSnapshotEdit,
      .opts = opts_snapshot_edit,
      .info = info_snapshot_edit,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE |
+                        VIR_CONNECT_LIST_DOMAINS_INACTIVE
     },
     {.name = "snapshot-info",
      .handler = cmdSnapshotInfo,
      .opts = opts_snapshot_info,
      .info = info_snapshot_info,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE |
+                        VIR_CONNECT_LIST_DOMAINS_INACTIVE
     },
     {.name = "snapshot-list",
      .handler = cmdSnapshotList,
      .opts = opts_snapshot_list,
      .info = info_snapshot_list,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE |
+                        VIR_CONNECT_LIST_DOMAINS_INACTIVE
     },
     {.name = "snapshot-parent",
      .handler = cmdSnapshotParent,
      .opts = opts_snapshot_parent,
      .info = info_snapshot_parent,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE |
+                        VIR_CONNECT_LIST_DOMAINS_INACTIVE
     },
     {.name = "snapshot-revert",
      .handler = cmdDomainSnapshotRevert,
      .opts = opts_snapshot_revert,
      .info = info_snapshot_revert,
-     .flags = 0
+     .flags = 0,
+     .completer = vshDomainCompleter,
+     .completer_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE |
+                        VIR_CONNECT_LIST_DOMAINS_INACTIVE
     },
     {.name = NULL}
 };
diff --git a/tools/virsh.c b/tools/virsh.c
index 5be8e7e..ae2dd55 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -88,6 +88,8 @@ static char *progname;
 
 static const vshCmdGrp cmdGroups[];
 
+vshControl *vshCtl;
+
 /* Bypass header poison */
 #undef strdup
 
@@ -317,6 +319,7 @@ vshCatchDisconnect(virConnectPtr conn ATTRIBUTE_UNUSED,
 static void
 vshReconnect(vshControl *ctl)
 {
+    printf("\n>>>\n");
     bool connected = false;
 
     if (ctl->conn) {
@@ -2510,6 +2513,46 @@ vshCloseLogFile(vshControl *ctl)
 
 #ifdef USE_READLINE
 
+/* -------------
+ * Completers
+ * -------------
+ */
+
+char **
+vshDomainCompleter(unsigned int flags)
+{
+    virDomainPtr *domains;
+    size_t i;
+    char **names = NULL;
+    int ndomains;
+
+    if (!vshCtl->conn)
+        return NULL;
+
+    ndomains = virConnectListAllDomains(vshCtl->conn, &domains, flags);
+
+    if (ndomains < 0)
+        return NULL;
+
+    names = vshMalloc(NULL, sizeof(char *) * (ndomains + 1));
+
+    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:
+    virStringFreeList(names);
+    return NULL;
+}
+
 /* -----------------
  * Readline stuff
  * -----------------
@@ -2643,6 +2686,10 @@ vshReadlineCommandCompletionGenerator(const char *text, int state)
     if (!cmd)
         return NULL;
 
+    if ((vshCtl->conn == NULL || disconnected) &&
+        !(cmd->flags & VSH_CMD_FLAG_NOCONNECT))
+        vshReconnect(vshCtl);
+
     if (!cmd->completer)
         return NULL;
 
@@ -3509,6 +3556,7 @@ main(int argc, char **argv)
     ctl->debug = VSH_DEBUG_DEFAULT;
     ctl->escapeChar = "^]";     /* Same default as telnet */
 
+    vshCtl = ctl;
 
     if (!setlocale(LC_ALL, "")) {
         perror("setlocale");
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

--
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]