]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
vshCommandOpt: Allow caller avoiding assert()
authorMichal Privoznik <mprivozn@redhat.com>
Thu, 9 Nov 2017 17:06:11 +0000 (18:06 +0100)
committerMichal Privoznik <mprivozn@redhat.com>
Thu, 11 Jan 2018 17:53:04 +0000 (18:53 +0100)
In the future, completer callbacks will receive partially parsed
command (and thus possibly incomplete). However, we still want
them to use command options fetching APIs we already have (e.g.
vshCommandOpt*()) and at the same time don't report any errors
(nor call any asserts).

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: John Ferlan <jferlan@redhat.com>
tools/vsh.c
tools/vsh.h

index 54e59fff69c8841b82cc1c71188b929e9162b179..b113c8c9579b1f100739697e2e5e37e8f9c1125a 100644 (file)
@@ -815,8 +815,8 @@ vshCommandFree(vshCmd *cmd)
  * to the option if found, 0 with *OPT set to NULL if the name is
  * valid and the option is not required, -1 with *OPT set to NULL if
  * the option is required but not present, and assert if NAME is not
- * valid (which indicates a programming error).  No error messages are
- * issued if a value is returned.
+ * valid (which indicates a programming error) unless cmd->skipChecks
+ * is set. No error messages are issued if a value is returned.
  */
 static int
 vshCommandOpt(const vshCmd *cmd, const char *name, vshCmdOpt **opt,
@@ -828,15 +828,19 @@ vshCommandOpt(const vshCmd *cmd, const char *name, vshCmdOpt **opt,
 
     /* See if option is valid and/or required.  */
     *opt = NULL;
-    while (valid) {
-        assert(valid->name);
-        if (STREQ(name, valid->name))
-            break;
-        valid++;
+
+    if (!cmd->skipChecks) {
+        while (valid && valid->name) {
+            if (STREQ(name, valid->name))
+                break;
+            valid++;
+        }
+
+        assert(valid && (!needData || valid->type != VSH_OT_BOOL));
+
+        if (valid->flags & VSH_OFLAG_REQ)
+            ret = -1;
     }
-    assert(!needData || valid->type != VSH_OT_BOOL);
-    if (valid->flags & VSH_OFLAG_REQ)
-        ret = -1;
 
     /* See if option is present on command line.  */
     while (candidate) {
index 8f7df9ff83a8e090a6e1f70250dd413411c2e1d3..112b1b57d8d8fce6851e4370de08c58caa153d1f 100644 (file)
@@ -188,7 +188,8 @@ struct _vshCmdDef {
 struct _vshCmd {
     const vshCmdDef *def;       /* command definition */
     vshCmdOpt *opts;            /* list of command arguments */
-    vshCmd *next;      /* next command */
+    vshCmd *next;               /* next command */
+    bool skipChecks;            /* skip validity checks when retrieving opts */
 };
 
 /*