]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virsh: cmdEcho: Rewrite with new buffer helpers
authorPeter Krempa <pkrempa@redhat.com>
Wed, 11 Aug 2021 09:10:12 +0000 (11:10 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Wed, 18 Aug 2021 09:07:25 +0000 (11:07 +0200)
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>
tools/vsh.c

index fc20fb21ee416fc1cc7cb99170ca31df515407d3..cca2920711b1594ed38b6725da92b84be9978100 100644 (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)