]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virsh: Treat any command name starting with # as comment
authorEric Blake <eblake@redhat.com>
Fri, 22 Mar 2019 19:02:39 +0000 (14:02 -0500)
committerEric Blake <eblake@redhat.com>
Mon, 25 Mar 2019 14:01:53 +0000 (09:01 -0500)
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@redhat.com>
Acked-by: Michal Privoznik <mprivozn@redhat.com>
tests/virshtest.c
tools/virsh.pod
tools/virt-admin.pod
tools/vsh.c

index cbe6686af3e39ea21c43bfde5dca1f7293b416d3..673c13fe07f77da436731eb8235693e3a2b5e903 100644 (file)
@@ -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
 
index 05adb568db78957ea8237e5e566b971dfdcee8db..95cab7b57d9519a01094698deb01ac70270395c9 100644 (file)
@@ -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>
index 8489325ca942a98053a6de05fd890e78732059f6..f06ee9247a01504e06af47b373e17acecbad68c7 100644 (file)
@@ -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>
index 9a88ee29b75ae26f74b6c7dea08bcc0109904fe3..d90ce8d102b081e37278805ae8a34c674f8249c5 100644 (file)
@@ -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? */