[libvirt] [PATCH] Make virsh support '~' and '$HOME' in interactive mode

Zhang Xiaohe zhangxh at cn.fujitsu.com
Thu Mar 21 09:00:58 UTC 2013


This patch makes '~' and '$HOME' can be recognized by virsh in
interactive mode. These two variables are replaced with real
path.
eg:
virsh # pwd
/home/libvirt

virsh # cd ~/rpmbuild

virsh # pwd
/root/rpmbuild

see https://bugzilla.redhat.com/show_bug.cgi?id=806793

Signed-off-by: Zhang Xiaohe <zhangxh at cn.fujitsu.com>
---
  tools/virsh.c |   25 +++++++++++++++++++++++++
  1 files changed, 25 insertions(+), 0 deletions(-)

diff --git a/tools/virsh.c b/tools/virsh.c
index b574d7e..5c8df6b 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -1232,6 +1232,27 @@ vshCmddefHelp(vshControl *ctl, const char *cmdname)
   * ---------------
   */
  static void
+vshExpandPath(vshControl *ctl, char **tkdata)
+{
+    char *argstr = NULL;
+    char *buf = NULL;
+    char *p = NULL;
+    const char *home = getenv("HOME");
+    size_t len = strlen(home) + strlen(*tkdata);
+
+    buf = vshMalloc(ctl, len);
+    p = buf;
+    buf = virStrcpy(buf, home, len);
+    argstr = strchr(*tkdata, '/');
+    if (argstr) {
+        buf += strlen(home);
+        buf = virStrcpy(buf, argstr, strlen(*tkdata));
+    }
+    VIR_FREE(*tkdata);
+    *tkdata = p;
+}
+
+static void
  vshCommandOptFree(vshCmdOpt * arg)
  {
      vshCmdOpt *a = arg;
@@ -1855,6 +1876,10 @@ get_data:
                  /* save option */
                  vshCmdOpt *arg = vshMalloc(ctl, sizeof(vshCmdOpt));

+                /* replace the ~ or $HOME with real path */
+                if (tkdata[0] == '~' || STRPREFIX(tkdata, "$HOME"))
+                    vshExpandPath(ctl, &tkdata);
+
                  arg->def = opt;
                  arg->data = tkdata;
                  arg->next = NULL;
-- 
1.7.1




More information about the libvir-list mailing list