[PATCH 4/6] virsh: cmdEcho: Rewrite with new buffer helpers

Peter Krempa pkrempa at redhat.com
Thu Aug 12 13:01:34 UTC 2021


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

Signed-off-by: Peter Krempa <pkrempa at redhat.com>
---
 tools/vsh.c | 21 +++++++--------------
 1 file changed, 7 insertions(+), 14 deletions(-)

diff --git a/tools/vsh.c b/tools/vsh.c
index 009c93254c..2456267426 100644
--- a/tools/vsh.c
+++ b/tools/vsh.c
@@ -3156,7 +3156,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;
@@ -3164,27 +3163,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)
-- 
2.31.1




More information about the libvir-list mailing list