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

[libvirt] [PATCHv3 6/6] virsh: Add vshRebootShutdownModeCompleter



vshRebootShutdownModeCompleter returns available shutdown mode
names. This can be used for --mode auto completion for commands
such as reboot or shutdown.

for example:

virsh> reboot --mode <TAB>
acpi     agent    initctl  signal
virsh> reboot --mode i<TAB>
virsh> reboot --mode initctl
---
v3
* removed useless if
* used virStringFreeList() instead of iteration
* moved all .completer = vshRebootShutdownModeCompleter initializations
  into this patch

 tools/virsh-domain.c |  9 +++++----
 tools/virsh.c        | 23 +++++++++++++++++++++++
 tools/virsh.h        |  1 +
 3 files changed, 29 insertions(+), 4 deletions(-)

diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c
index 8a118ed..c60a01c 100644
--- a/tools/virsh-domain.c
+++ b/tools/virsh-domain.c
@@ -2756,8 +2756,7 @@ static const vshCmdOptDef opts_dom_pm_suspend[] = {
      .flags = VSH_OFLAG_REQ,
      .help = N_("mem(Suspend-to-RAM), "
                 "disk(Suspend-to-Disk), "
-                "hybrid(Hybrid-Suspend)"),
-     .completer = vshSuspendTargetCompleter
+                "hybrid(Hybrid-Suspend)")
     },
     {.name = NULL}
 };
@@ -4714,7 +4713,8 @@ static const vshCmdOptDef opts_shutdown[] = {
     },
     {.name = "mode",
      .type = VSH_OT_STRING,
-     .help = N_("shutdown mode: acpi|agent|initctl|signal")
+     .help = N_("shutdown mode: acpi|agent|initctl|signal"),
+     .completer = vshRebootShutdownModeCompleter
     },
     {.name = NULL}
 };
@@ -4800,7 +4800,8 @@ static const vshCmdOptDef opts_reboot[] = {
     },
     {.name = "mode",
      .type = VSH_OT_STRING,
-     .help = N_("shutdown mode: acpi|agent|initctl|signal")
+     .help = N_("shutdown mode: acpi|agent|initctl|signal"),
+     .completer = vshRebootShutdownModeCompleter
     },
     {.name = NULL}
 };
diff --git a/tools/virsh.c b/tools/virsh.c
index 5a5e520..e6b1309 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -2576,6 +2576,29 @@ error:
     return NULL;
 }
 
+char **
+vshRebootShutdownModeCompleter(unsigned int unused_flags ATTRIBUTE_UNUSED)
+{
+    const char *modes[] = {"acpi", "agent", "initctl", "signal"};
+    const unsigned int modes_size = ARRAY_CARDINALITY(modes);
+    char **names = NULL;
+    size_t i;
+
+    names = vshMalloc(NULL, sizeof(char *) * (modes_size + 1));
+
+    for (i = 0; i < modes_size; i++) {
+        if (VIR_STRDUP(names[i], modes[i]) < 0)
+            goto cleanup;
+    }
+
+    names[i] = NULL;
+    return names;
+
+cleanup:
+    virStringFreeList(names);
+    return NULL;
+}
+
 /* -----------------
  * Readline stuff
  * -----------------
diff --git a/tools/virsh.h b/tools/virsh.h
index 6767e65..61510b0 100644
--- a/tools/virsh.h
+++ b/tools/virsh.h
@@ -257,6 +257,7 @@ struct _vshCmdGrp {
 
 char **vshDomainCompleter(unsigned int flags);
 char **vshSuspendTargetCompleter(unsigned int unused_flags);
+char **vshRebootShutdownModeCompleter(unsigned int unused_flags);
 
 void vshError(vshControl *ctl, const char *format, ...)
     ATTRIBUTE_FMT_PRINTF(2, 3);
-- 
1.8.3.1


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