Use virBufferEscapeShell in cmdEcho

This commit is contained in:
Guido Günther 2011-10-13 22:49:15 +02:00
parent 46a1168129
commit 07862822f3

View File

@ -12322,7 +12322,7 @@ static const vshCmdOptDef opts_echo[] = {
* quotes for later evaluation. * quotes for later evaluation.
*/ */
static bool static bool
cmdEcho (vshControl *ctl ATTRIBUTE_UNUSED, const vshCmd *cmd) cmdEcho (vshControl *ctl, const vshCmd *cmd)
{ {
bool shell = false; bool shell = false;
bool xml = false; bool xml = false;
@ -12337,34 +12337,31 @@ cmdEcho (vshControl *ctl ATTRIBUTE_UNUSED, const vshCmd *cmd)
xml = true; xml = true;
while ((opt = vshCommandOptArgv(cmd, opt))) { while ((opt = vshCommandOptArgv(cmd, opt))) {
bool close_quote = false; char *str;
char *q; virBuffer xmlbuf = VIR_BUFFER_INITIALIZER;
arg = opt->data; arg = opt->data;
if (count) if (count)
virBufferAddChar(&buf, ' '); virBufferAddChar(&buf, ' ');
/* Add outer '' only if arg included shell metacharacters. */
if (shell &&
(strpbrk(arg, "\r\t\n !\"#$&'()*;<>?[\\]^`{|}~") || !*arg)) {
virBufferAddChar(&buf, '\'');
close_quote = true;
}
if (xml) { if (xml) {
virBufferEscapeString(&buf, "%s", arg); virBufferEscapeString(&xmlbuf, "%s", arg);
if (virBufferError(&buf)) {
vshPrint(ctl, "%s", _("Failed to allocate XML buffer"));
return false;
}
str = virBufferContentAndReset(&xmlbuf);
} else { } else {
if (shell && (q = strchr(arg, '\''))) { str = vshStrdup(ctl, arg);
do {
virBufferAdd(&buf, arg, q - arg);
virBufferAddLit(&buf, "'\\''");
arg = q + 1;
q = strchr(arg, '\'');
} while (q);
} }
virBufferAdd(&buf, arg, strlen(arg));
} if (shell)
if (close_quote) virBufferEscapeShell(&buf, str);
virBufferAddChar(&buf, '\''); else
virBufferAdd(&buf, str, -1);
count++; count++;
VIR_FREE(str);
} }
if (virBufferError(&buf)) { if (virBufferError(&buf)) {