[libvirt] [PATCH 3/5] virsh: Treat any command name starting with # as comment

Eric Blake eblake at redhat.com
Sun Mar 24 04:02:01 UTC 2019


As the previous commit mentioned, argv mode (such as when you feed
virsh via stdin with <<\EOF instead of via a single shell argument)
didn't permit comments. Do this by treating any command name token
that starts with # as a comment which silently eats all remaining
arguments to the next newline or semicolon.

Note that batch mode recognizes unquoted # at the start of any word as
a command as part of the tokenizer, while this patch only treats # at
the start of the command word as a comment (any other # remaining by
the time vshCommandParse() is processing things was already quoted
during the tokenzier, and as such was probably intended as the actual
argument to the command word earlier in the line).

Now I can do something like:

$ virsh -c test:///default <<EOF
  # setup
  snapshot-create-as test s1
  snapshot-create-as test s2
  # check
  snapshot-list test --name
EOF

Signed-off-by: Eric Blake <eblake at redhat.com>
---
 tests/virshtest.c    |  1 +
 tools/virsh.pod      |  3 ++-
 tools/virt-admin.pod |  4 +++-
 tools/vsh.c          | 11 +++++++++--
 4 files changed, 15 insertions(+), 4 deletions(-)

diff --git a/tests/virshtest.c b/tests/virshtest.c
index 5408db1387..3560e240a4 100644
--- a/tests/virshtest.c
+++ b/tests/virshtest.c
@@ -423,6 +423,7 @@ mymain(void)
     DO_TEST(44, "a # b\n", "echo a '#' b");
     DO_TEST(45, "a # b\n", "echo a \\# b");
     DO_TEST(46, "a\n", "#unbalanced 'quotes\"\necho a # b");
+    DO_TEST(47, "a\n", "\\# ignored;echo a\n'#also' ignored");

 # undef DO_TEST

diff --git a/tools/virsh.pod b/tools/virsh.pod
index 05adb568db..95cab7b57d 100644
--- a/tools/virsh.pod
+++ b/tools/virsh.pod
@@ -35,7 +35,8 @@ will be clear for each of those commands.  Note: it is permissible to
 give numeric names to domains, however, doing so will result in a
 domain that can only be identified by domain id. In other words, if a
 numeric value is supplied it will be interpreted as a domain id, not
-as a name.
+as a name. Any I<command> starting with B<#> is treated as a comment
+and silently ignored, all other unrecognized I<command>s are diagnosed.

 The B<virsh> program can be used either to run one I<COMMAND> by giving the
 command and its arguments on the shell command line, or a I<COMMAND_STRING>
diff --git a/tools/virt-admin.pod b/tools/virt-admin.pod
index 8489325ca9..f06ee9247a 100644
--- a/tools/virt-admin.pod
+++ b/tools/virt-admin.pod
@@ -18,7 +18,9 @@ The basic structure of most virt-admin usage is:

   virt-admin [OPTION]... <command> [ARG]...

-Where I<command> is one of the commands listed below.
+Where I<command> is one of the commands listed below. Any I<command>
+starting with B<#> is treated as a comment and silently ignored, all
+other unrecognized I<command>s are diagnosed.

 The B<virt-admin> program can be used either to run one I<COMMAND> by giving the
 command and its arguments on the shell command line, or a I<COMMAND_STRING>
diff --git a/tools/vsh.c b/tools/vsh.c
index 9a88ee29b7..d90ce8d102 100644
--- a/tools/vsh.c
+++ b/tools/vsh.c
@@ -1437,8 +1437,15 @@ vshCommandParse(vshControl *ctl, vshCommandParser *parser, vshCmd **partial)
             }

             if (cmd == NULL) {
-                /* first token must be command name */
-                if (!(cmd = vshCmddefSearch(tkdata))) {
+                /* first token must be command name or comment */
+                if (*tkdata == '#') {
+                    do {
+                        VIR_FREE(tkdata);
+                        tk = parser->getNextArg(ctl, parser, &tkdata, false);
+                    } while (tk == VSH_TK_ARG);
+                    VIR_FREE(tkdata);
+                    break;
+                } else if (!(cmd = vshCmddefSearch(tkdata))) {
                     if (!partial)
                         vshError(ctl, _("unknown command: '%s'"), tkdata);
                     goto syntaxError;   /* ... or ignore this command only? */
-- 
2.20.1




More information about the libvir-list mailing list