[libvirt] [PATCH 3/3] virsh: qemu-monitor-command: Simplify control flow

Peter Krempa pkrempa at redhat.com
Mon Aug 1 04:30:08 UTC 2016


Construct the query string by using virBufferTrim rather than having to
remember to add a space and simplify cleanup path.
---
 tools/virsh-domain.c | 20 ++++++++------------
 1 file changed, 8 insertions(+), 12 deletions(-)

diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c
index 45fce76..120b768 100644
--- a/tools/virsh-domain.c
+++ b/tools/virsh-domain.c
@@ -8942,22 +8942,19 @@ cmdQemuMonitorCommand(vshControl *ctl, const vshCmd *cmd)
     unsigned int flags = 0;
     const vshCmdOpt *opt = NULL;
     virBuffer buf = VIR_BUFFER_INITIALIZER;
-    bool pad = false;
     virJSONValuePtr pretty = NULL;
     char *prettystr = NULL;

     VSH_EXCLUSIVE_OPTIONS("hmp", "pretty");

-    dom = virshCommandOptDomain(ctl, cmd, NULL);
-    if (dom == NULL)
-        goto cleanup;
+    if (!(dom = virshCommandOptDomain(ctl, cmd, NULL)))
+        return false;
+
+    while ((opt = vshCommandOptArgv(ctl, cmd, opt)))
+        virBufferAsprintf(&buf, "%s ", opt->data);
+
+    virBufferTrim(&buf, " ", -1);

-    while ((opt = vshCommandOptArgv(ctl, cmd, opt))) {
-        if (pad)
-            virBufferAddChar(&buf, ' ');
-        pad = true;
-        virBufferAdd(&buf, opt->data, -1);
-    }
     if (virBufferError(&buf)) {
         vshPrint(ctl, "%s", _("Failed to collect command"));
         goto cleanup;
@@ -8986,8 +8983,7 @@ cmdQemuMonitorCommand(vshControl *ctl, const vshCmd *cmd)
     VIR_FREE(prettystr);
     VIR_FREE(monitor_cmd);
     virJSONValueFree(pretty);
-    if (dom)
-        virDomainFree(dom);
+    virDomainFree(dom);

     return ret;
 }
-- 
2.9.2




More information about the libvir-list mailing list