[libvirt] [PATCH 2/5] virsh: Parse # comments in batch mode

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


Continuing from what I did in commit 4817dec0, now I want to write a
sequence that is self-documenting.  So I need comments :)

Now I can do something like:

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

Note that this does NOT accept comments in argv mode, another patch
will tackle that.

(If I'm not careful, I might turn virsh into a full-fledged 'sh'
replacement? Here's hoping I don't go that far...)

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

diff --git a/tests/virshtest.c b/tests/virshtest.c
index 7e59ad7da6..5408db1387 100644
--- a/tests/virshtest.c
+++ b/tests/virshtest.c
@@ -417,6 +417,12 @@ mymain(void)
     DO_TEST(38, "a\nb\n", "ec\\\nho a\n echo \\\n b;");
     DO_TEST(39, "a\n b\n", "\"ec\\\nho\" a\n echo \"\\\n b\";");
     DO_TEST(40, "a\n\\\n b\n", "ec\\\nho a\n echo '\\\n b';");
+    DO_TEST(41, "a\n", "echo a # b");
+    DO_TEST(42, "a\nc\n", "echo a #b\necho c");
+    DO_TEST(43, "a\nc\n", "echo a # b\\\necho c");
+    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");

 # undef DO_TEST

diff --git a/tools/virsh.pod b/tools/virsh.pod
index db72343159..05adb568db 100644
--- a/tools/virsh.pod
+++ b/tools/virsh.pod
@@ -44,7 +44,8 @@ and their arguments joined with whitespace and separated by semicolons or
 newlines between commands, where unquoted backslash-newline pairs are
 elided.  Within I<COMMAND_STRING>, virsh understands the
 same single, double, and backslash escapes as the shell, although you must
-add another layer of shell escaping in creating the single shell argument.
+add another layer of shell escaping in creating the single shell argument,
+and any word starting with unquoted I<#> begins a comment that ends at newline.
 If no command is given in the command line, B<virsh> will then start a minimal
 interpreter waiting for your commands, and the B<quit> command will then exit
 the program.
diff --git a/tools/virt-admin.pod b/tools/virt-admin.pod
index 9bbff42846..8489325ca9 100644
--- a/tools/virt-admin.pod
+++ b/tools/virt-admin.pod
@@ -27,7 +27,8 @@ and their arguments joined with whitespace and separated by semicolons or
 newlines between commands, where unquoted backslash-newline pairs are
 elided.  Within I<COMMAND_STRING>, virt-admin understands the
 same single, double, and backslash escapes as the shell, although you must
-add another layer of shell escaping in creating the single shell argument.
+add another layer of shell escaping in creating the single shell argument,
+and any word starting with unquoted I<#> begins a comment that ends at newline.
 If no command is given in the command line, B<virt-admin> will then start a minimal
 interpreter waiting for your commands, and the B<quit> command will then exit
 the program.
diff --git a/tools/vsh.c b/tools/vsh.c
index 5903f129c1..9a88ee29b7 100644
--- a/tools/vsh.c
+++ b/tools/vsh.c
@@ -1,7 +1,7 @@
 /*
  * vsh.c: common data to be used by clients to exercise the libvirt API
  *
- * Copyright (C) 2005, 2007-2015 Red Hat, Inc.
+ * Copyright (C) 2005-2019 Red Hat, Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -1693,6 +1693,12 @@ vshCommandStringGetArg(vshControl *ctl, vshCommandParser *parser, char **res,
         parser->pos = ++p;             /* = \0 or begin of next command */
         return VSH_TK_SUBCMD_END;
     }
+    if (*p == '#') { /* Argument starting with # is comment to end of line */
+        while (*p && *p != '\n')
+            p++;
+        parser->pos = p + !!*p;
+        return VSH_TK_SUBCMD_END;
+    }

     while (*p) {
         /* end of token is blank space or ';' */
-- 
2.20.1




More information about the libvir-list mailing list