[Libvir] [PATCH] Add qemudEscapeShellArg for passing commandlines to qemu.

2007-08-13 Thread Jim Paris
Use this to escape a shell argument in a commandline passed to qemu.
First we need to escape certain characters to get them through the
qemu monitor interface.  On the shell side, the argument will be
enclosed in single quotes, so the only character that needs special
treatment is the single quote itself.

Signed-off-by: Jim Paris [EMAIL PROTECTED]
---
 src/qemu_driver.c |   66 +
 1 files changed, 66 insertions(+), 0 deletions(-)

diff --git a/src/qemu_driver.c b/src/qemu_driver.c
index e649060..8125622 100644
--- a/src/qemu_driver.c
+++ b/src/qemu_driver.c
@@ -1855,6 +1855,72 @@ static int qemudDomainGetInfo(virDomainPtr dom,
 }
 
 
+static char *qemudEscapeShellArg(const char *in)
+{
+int len = 0;
+int i, j;
+char *out;
+
+/* To pass through the QEMU monitor, we need to use escape
+   sequences: \r, \n, \, \\
+
+   To pass through both QEMU + the shell, we need to escape
+   the single character ' as the five characters '\\''
+*/
+
+for (i = 0; in[i] != '\0'; i++) {
+switch(in[i]) {
+case '\r':
+case '\n':
+case '':
+case '\\':
+len += 2;
+break;
+case '\'':
+len += 5;
+break;
+default:
+len += 1;
+break;
+}
+}
+
+if ((out = (char *)malloc(len + 1)) == NULL)
+return NULL;
+
+for (i = j = 0; in[i] != '\0'; i++) {
+switch(in[i]) {
+case '\r':
+out[j++] = '\\';
+out[j++] = 'r';
+break;
+case '\n':
+out[j++] = '\\';
+out[j++] = 'n';
+break;
+case '':
+case '\\':
+out[j++] = '\\';
+out[j++] = in[i];
+break;
+case '\'':
+out[j++] = '\'';
+out[j++] = '\\';
+out[j++] = '\\';
+out[j++] = '\'';
+out[j++] = '\'';
+break;
+default:
+out[j++] = in[i];
+break;
+}
+}
+out[j] = '\0';
+
+return out;
+}
+
+
 static int qemudDomainSave(virDomainPtr dom,
 const char *path ATTRIBUTE_UNUSED) {
 struct qemud_driver *driver = (struct qemud_driver 
*)dom-conn-privateData;
-- 
1.5.3.rc4

--
Libvir-list mailing list
Libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


Re: [Libvir] [PATCH] Add qemudEscapeShellArg for passing commandlines to qemu.

2007-08-13 Thread Daniel P. Berrange
On Mon, Aug 13, 2007 at 02:17:07PM -0400, Jim Paris wrote:
 Use this to escape a shell argument in a commandline passed to qemu.
 First we need to escape certain characters to get them through the
 qemu monitor interface.  On the shell side, the argument will be
 enclosed in single quotes, so the only character that needs special
 treatment is the single quote itself.

Looks good, committed to CVS.

Regards,
Dan.
-- 
|=- Red Hat, Engineering, Emerging Technologies, Boston.  +1 978 392 2496 -=|
|=-   Perl modules: http://search.cpan.org/~danberr/  -=|
|=-   Projects: http://freshmeat.net/~danielpb/   -=|
|=-  GnuPG: 7D3B9505   F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505  -=| 

--
Libvir-list mailing list
Libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list