[libvirt] [PATCH v2 1/3] virt-admin: Tweak command parsing logic so that aliases point to new commands

Erik Skultety eskultet at redhat.com
Tue Sep 13 15:11:26 UTC 2016


Change the logic in a way, so that VSH_CMD_FLAG_ALIAS behaves similarly to
how VSH_OT_ALIAS for command options, i.e. there is no need for code duplication
for the alias and the aliased command structures. Along with that change,
switch any existing VSH_CMD_FLAG_ALIAS occurrences to this new format. Also,
since this patch introduces a new command structure element, adjust the
virsh-self-test test to make sure we won't ever miss to specify the '.alias'
member for an aliased command because doing that would lead to a segfault, as
would be the case for any missing element of the command structure.

Signed-off-by: Erik Skultety <eskultet at redhat.com>
---
 tools/virsh-nodedev.c |  6 ++----
 tools/virsh.c         | 10 ++++++----
 tools/virsh.pod       |  2 --
 tools/vsh.c           |  6 ++++++
 tools/vsh.h           |  1 +
 5 files changed, 15 insertions(+), 10 deletions(-)

diff --git a/tools/virsh-nodedev.c b/tools/virsh-nodedev.c
index 321f15c..9446664 100644
--- a/tools/virsh-nodedev.c
+++ b/tools/virsh-nodedev.c
@@ -986,10 +986,8 @@ const vshCmdDef nodedevCmds[] = {
      .flags = 0
     },
     {.name = "nodedev-dettach",
-     .handler = cmdNodeDeviceDetach,
-     .opts = opts_node_device_detach,
-     .info = info_node_device_detach,
-     .flags = VSH_CMD_FLAG_ALIAS
+     .flags = VSH_CMD_FLAG_ALIAS,
+     .alias = "nodedev-detach"
     },
     {.name = "nodedev-dumpxml",
      .handler = cmdNodeDeviceDumpXML,
diff --git a/tools/virsh.c b/tools/virsh.c
index cb60edc..918058c 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -373,10 +373,11 @@ cmdSelfTest(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
 
     for (grp = cmdGroups; grp->name; grp++) {
         for (def = grp->commands; def->name; def++) {
-            if (def->flags & VSH_CMD_FLAG_ALIAS)
-                continue;
+            const vshCmdDef *c = def;
+            if (c->flags & VSH_CMD_FLAG_ALIAS)
+                c = vshCmddefSearch(c->alias);
 
-            if (!vshCmddefHelp(ctl, def->name))
+            if (!vshCmddefHelp(ctl, c->name))
                 return false;
         }
     }
@@ -904,7 +905,8 @@ static const vshCmdDef virshCmds[] = {
      .handler = cmdSelfTest,
      .opts = NULL,
      .info = info_selftest,
-     .flags = VSH_CMD_FLAG_NOCONNECT | VSH_CMD_FLAG_ALIAS
+     .flags = VSH_CMD_FLAG_NOCONNECT | VSH_CMD_FLAG_ALIAS,
+     .alias = "self-test"
     },
     {.name = NULL}
 };
diff --git a/tools/virsh.pod b/tools/virsh.pod
index 3da7879..49abda9 100644
--- a/tools/virsh.pod
+++ b/tools/virsh.pod
@@ -2936,8 +2936,6 @@ make that device unusable by the rest of the physical host until a reboot.
 Detach I<nodedev> from the host, so that it can safely be used by
 guests via <hostdev> passthrough.  This is reversed with
 B<nodedev-reattach>, and is done automatically for managed devices.
-For compatibility purposes, this command can also be spelled
-B<nodedev-dettach>.
 
 Different backend drivers expect the device to be bound to different
 dummy devices. For example, QEMU's "kvm" backend driver (the default)
diff --git a/tools/vsh.c b/tools/vsh.c
index be6a073..ad3e1c7 100644
--- a/tools/vsh.c
+++ b/tools/vsh.c
@@ -1410,6 +1410,12 @@ vshCommandParse(vshControl *ctl, vshCommandParser *parser)
                     vshError(ctl, _("unknown command: '%s'"), tkdata);
                     goto syntaxError;   /* ... or ignore this command only? */
                 }
+                /* aliases need to be resolved to the actual commands */
+                if (cmd->flags & VSH_CMD_FLAG_ALIAS) {
+                    VIR_FREE(tkdata);
+                    tkdata = vshStrdup(ctl, cmd->alias);
+                    cmd = vshCmddefSearch(tkdata);
+                }
                 if (vshCmddefOptParse(cmd, &opts_need_arg,
                                       &opts_required) < 0) {
                     vshError(ctl,
diff --git a/tools/vsh.h b/tools/vsh.h
index e53910f..e383e54 100644
--- a/tools/vsh.h
+++ b/tools/vsh.h
@@ -179,6 +179,7 @@ struct _vshCmdDef {
     const vshCmdOptDef *opts;   /* definition of command options */
     const vshCmdInfo *info;     /* details about command */
     unsigned int flags;         /* bitwise OR of VSH_CMD_FLAG */
+    const char *alias;          /* name of the aliased command */
 };
 
 /*
-- 
2.5.5




More information about the libvir-list mailing list