[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH 02/10 V2] improve the iteration of VSH_OT_ARGV options



Signed-off-by: Lai Jiangshan <laijs cn fujitsu com>
---
 tools/virsh.c |   47 ++++++++++++++++++++++++-----------------------
 1 files changed, 24 insertions(+), 23 deletions(-)

diff --git a/tools/virsh.c b/tools/virsh.c
index 61eb11e..638029c 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -280,7 +280,27 @@ static int vshCommandOptULongLong(const vshCmd *cmd, const char *name,
                                   unsigned long long *value)
     ATTRIBUTE_NONNULL(3) ATTRIBUTE_RETURN_CHECK;
 static bool vshCommandOptBool(const vshCmd *cmd, const char *name);
-static char *vshCommandOptArgv(const vshCmd *cmd, int count);
+
+/*
+ * Iterate all the argv arguments.
+ *
+ * Requires that a VSH_OT_ARGV option  be last in the
+ * list of supported options in CMD->def->opts.
+ */
+static inline const vshCmdOpt *__variable_arg(const vshCmdOpt *opt)
+{
+    while (opt) {
+         if (opt->def && opt->def->type == VSH_OT_ARGV)
+             break;
+         opt = opt->next;
+    }
+
+    return opt;
+}
+
+#define for_each_variable_arg(cmd, opt)                                       \
+    for (opt = __variable_arg(cmd->opts); opt; opt = __variable_arg(opt->next))
+
 
 #define VSH_BYID     (1 << 1)
 #define VSH_BYUUID   (1 << 2)
@@ -10341,6 +10361,7 @@ cmdEcho (vshControl *ctl ATTRIBUTE_UNUSED, const vshCmd *cmd)
     bool shell = false;
     bool xml = false;
     int count = 0;
+    const vshCmdOpt *opt;
     char *arg;
     virBuffer buf = VIR_BUFFER_INITIALIZER;
 
@@ -10349,10 +10370,11 @@ cmdEcho (vshControl *ctl ATTRIBUTE_UNUSED, const vshCmd *cmd)
     if (vshCommandOptBool(cmd, "xml"))
         xml = true;
 
-    while ((arg = vshCommandOptArgv(cmd, count)) != NULL) {
+    for_each_variable_arg(cmd, opt) {
         bool close_quote = false;
         char *q;
 
+        arg = opt->data;
         if (count)
             virBufferAddChar(&buf, ' ');
         /* Add outer '' only if arg included shell metacharacters.  */
@@ -11803,27 +11825,6 @@ vshCommandOptBool(const vshCmd *cmd, const char *name)
     return vshCommandOpt(cmd, name) != NULL;
 }
 
-/*
- * Returns the COUNT argv argument, or NULL after last argument.
- *
- * Requires that a VSH_OT_ARGV option with the name "" be last in the
- * list of supported options in CMD->def->opts.
- */
-static char *
-vshCommandOptArgv(const vshCmd *cmd, int count)
-{
-    vshCmdOpt *opt = cmd->opts;
-
-    while (opt) {
-        if (opt->def && opt->def->type == VSH_OT_ARGV) {
-            if (count-- == 0)
-                return opt->data;
-        }
-        opt = opt->next;
-    }
-    return NULL;
-}
-
 /* Determine whether CMD->opts includes an option with name OPTNAME.
    If not, give a diagnostic and return false.
    If so, return true.  */
-- 
1.7.4.4


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]