This patch adds cd and pwd commands to virsh. These can be useful
together with commands that refer to files in the local file systems,
especially save and restore.
I explicitly decided not to provide any other command, e.g. mkdir,
to avoid going down a slippery slope (now you want mkdir, tomorrow
ls and rm and so on...). If anything, it would be possible to add
a generic shell command---but cd cannot be implemented that way,
so pwd shall remain the sole "exception".
I did not implement "cd -".
Ok?
* docs/virsh.pod: Document cd and pwd commands.
* src/virsh.c (info_cd, opts_cd, cmdCd, info_pwd, cmdPwd): New.
(commands): Add cd and pwd commands.
* virsh.1: Regenerate.
---
Compared to v1, I didn't add the getcwd module from gnulib
because it is unnecessary on modern OSes and brings in too
many dependencies, some of them GPLed. This was somehow
masked before Jim's recent reorganization of gnulib.
docs/virsh.pod | 12 ++++++++
src/virsh.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
virsh.1 | 12 ++++++++-
3 files changed, 100 insertions(+), 1 deletions(-)
diff --git a/docs/virsh.pod b/docs/virsh.pod
index 6434d78..9530ba6 100644
--- a/docs/virsh.pod
+++ b/docs/virsh.pod
@@ -82,6 +82,18 @@ Running hypervisor: Xen 3.0.0
=back
+=item B<cd> I<directory> optional
+
+Will change current directory to I<directory>. The default directory
+for the B<cd> command is the home directory or, if there is no I<HOME>
+variable in the environment, the root directory.
+
+This command is only available in interactive mode.
+
+=item B<pwd>
+
+Will print the current directory.
+
=item B<connect> I<URI> optional I<--readonly>
(Re)-Connect to the hypervisor. This is a build-in command after shell
diff --git a/src/virsh.c b/src/virsh.c
index 5623499..856e5a0 100644
--- a/src/virsh.c
+++ b/src/virsh.c
@@ -6047,6 +6047,81 @@ editReadBackFile (vshControl *ctl, const char *filename)
}
/*
+ * "cd" command
+ */
+static const vshCmdInfo info_cd[] = {
+ {"help", gettext_noop("change the current directory")},
+ {"desc", gettext_noop("Change the current directory.")},
+ {NULL, NULL}
+};
+
+static const vshCmdOptDef opts_cd[] = {
+ {"dir", VSH_OT_DATA, 0, gettext_noop("directory to switch to (default:
home or else root)")},
+ {NULL, 0, 0, NULL}
+};
+
+static int
+cmdCd(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
+{
+ const char *dir;
+ int found;
+
+ if (!ctl->imode) {
+ vshError(ctl, FALSE, _("cd: command valid only in interactive mode"));
+ return -1;
+ }
+
+ dir = vshCommandOptString(cmd, "dir", &found);
+ if (!found)
+ dir = getenv ("HOME");
+ if (!dir)
+ dir = "/";
+
+ if (chdir (dir) == -1) {
+ vshError(ctl, FALSE, _("cd: %s: %s"), strerror (errno), dir);
+ return -1;
+ }
+
+ return 0;
+}
+
+/*
+ * "pwd" command
+ */
+static const vshCmdInfo info_pwd[] = {
+ {"help", gettext_noop("print the current directory")},
+ {"desc", gettext_noop("Print the current directory.")},
+ {NULL, NULL}
+};
+
+static int
+cmdPwd(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
+{
+ char *cwd;
+ size_t path_max;
+ int err = TRUE;
+
+ path_max = (size_t) PATH_MAX + 2;
+ cwd = vshMalloc (ctl, path_max);
+ while (cwd) {
+ err = getcwd (cwd, path_max) == NULL;
+ if (!err || errno != ERANGE)
+ break;
+
+ path_max *= 2;
+ cwd = vshRealloc (ctl, cwd, path_max);
+ }
+
+ if (err)
+ vshError(ctl, FALSE, _("pwd: cannot get current directory: %s"),
strerror (errno));
+ else
+ vshPrint (ctl, _("%s\n"), cwd);
+
+ free (cwd);
+ return !err;
+}
+
+/*
* "edit" command
*/
static const vshCmdInfo info_edit[] = {
@@ -6209,6 +6284,7 @@ static const vshCmdDef commands[] = {
{"attach-interface", cmdAttachInterface, opts_attach_interface,
info_attach_interface},
{"autostart", cmdAutostart, opts_autostart, info_autostart},
{"capabilities", cmdCapabilities, NULL, info_capabilities},
+ {"cd", cmdCd, opts_cd, info_cd},
{"connect", cmdConnect, opts_connect, info_connect},
{"console", cmdConsole, opts_console, info_console},
{"create", cmdCreate, opts_create, info_create},
@@ -6277,6 +6353,7 @@ static const vshCmdDef commands[] = {
{"pool-undefine", cmdPoolUndefine, opts_pool_undefine,
info_pool_undefine},
{"pool-uuid", cmdPoolUuid, opts_pool_uuid, info_pool_uuid},
+ {"pwd", cmdPwd, NULL, info_pwd},
{"quit", cmdQuit, NULL, info_quit},
{"reboot", cmdReboot, opts_reboot, info_reboot},
{"restore", cmdRestore, opts_restore, info_restore},
diff --git a/virsh.1 b/virsh.1
index 45ea614..ca03cc1 100644
--- a/virsh.1
+++ b/virsh.1
@@ -132,7 +132,7 @@
.\" ========================================================================
.\"
.IX Title "VIRSH 1"
-.TH VIRSH 1 "2009-04-16" "libvirt-0.6.2" "Virtualization
Support"
+.TH VIRSH 1 "2009-07-02" "libvirt-0.6.4" "Virtualization
Support"
.\" For nroff, turn off justification. Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.if n .ad l
@@ -214,6 +214,16 @@ Running hypervisor: Xen 3.0.0
.RE
.RS 4
.RE
+.IP "\fBcd\fR \fIdirectory\fR optional" 4
+.IX Item "cd directory optional"
+Will change current directory to \fIdirectory\fR. The default directory
+for the \fBcd\fR command is the home directory or, if there is no \fI\s-1HOME\s0\fR
+variable in the environment, the root directory.
+.Sp
+This command is only available in interactive mode.
+.IP "\fBpwd\fR" 4
+.IX Item "pwd"
+Will print the current directory.
.IP "\fBconnect\fR \fI\s-1URI\s0\fR optional \fI\-\-readonly\fR" 4
.IX Item "connect URI optional --readonly"
(Re)\-Connect to the hypervisor. This is a build-in command after shell
--
1.6.2.5