virsh: cmdEcho: Rewrite with new buffer helpers

Remove the need for temporary strings by filling the output buffer
directly.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Martin Kletzander <mkletzan@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2021-08-11 11:10:12 +02:00
parent 86de16b317
commit 31fa241b46

View File

@ -3153,7 +3153,6 @@ cmdEcho(vshControl *ctl, const vshCmd *cmd)
bool shell = vshCommandOptBool(cmd, "shell");
bool xml = vshCommandOptBool(cmd, "xml");
bool err = vshCommandOptBool(cmd, "err");
int count = 0;
const vshCmdOpt *opt = NULL;
g_autofree char *arg = NULL;
g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
@ -3161,27 +3160,21 @@ cmdEcho(vshControl *ctl, const vshCmd *cmd)
VSH_EXCLUSIVE_OPTIONS_VAR(shell, xml);
while ((opt = vshCommandOptArgv(ctl, cmd, opt))) {
g_autofree char *str = NULL;
g_auto(virBuffer) xmlbuf = VIR_BUFFER_INITIALIZER;
const char *curr = opt->data;
if (count)
virBufferAddChar(&buf, ' ');
if (xml) {
virBufferEscapeString(&xmlbuf, "%s", curr);
str = virBufferContentAndReset(&xmlbuf);
virBufferEscapeString(&buf, "%s", curr);
} else if (shell) {
virBufferEscapeShell(&buf, curr);
} else {
str = g_strdup(curr);
virBufferAdd(&buf, curr, -1);
}
if (shell)
virBufferEscapeShell(&buf, str);
else
virBufferAdd(&buf, str, -1);
count++;
virBufferAddChar(&buf, ' ');
}
virBufferTrim(&buf, " ");
arg = virBufferContentAndReset(&buf);
if (arg) {
if (err)