virsh: Parse # comments in batch mode

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>
This commit is contained in:
Eric Blake 2019-03-22 14:02:39 -05:00
parent 9884b2d185
commit 834f64ca47
4 changed files with 17 additions and 3 deletions

View 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

View 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.

View 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.

View 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 ';' */