]> xenbits.xensource.com Git - libvirt.git/commitdiff
virsh: Parse # comments in batch mode
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:42 +0000 (09:01 -0500)
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@redhat.com>
Acked-by: Michal Privoznik <mprivozn@redhat.com>
tests/virshtest.c
tools/virsh.pod
tools/virt-admin.pod
tools/vsh.c

index 7e59ad7da64a7367fee86a501a1351c76a6bd6a6..cbe6686af3e39ea21c43bfde5dca1f7293b416d3 100644 (file)
@@ -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
 
index db723431590c5752b3e9bbccc0ed69c84820b55f..05adb568db78957ea8237e5e566b971dfdcee8db 100644 (file)
@@ -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.
index 9bbff428469dc6f6a94ac243176172b330400846..8489325ca942a98053a6de05fd890e78732059f6 100644 (file)
@@ -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.
index 5903f129c1630972b26caef8931d2b99c0d30df9..9a88ee29b75ae26f74b6c7dea08bcc0109904fe3 100644 (file)
@@ -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 ';' */