From 834f64ca4777f9fc20e0bed5e6cb7b59a76366fd Mon Sep 17 00:00:00 2001 From: Eric Blake Date: Fri, 22 Mar 2019 14:02:39 -0500 Subject: [PATCH] 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 Acked-by: Michal Privoznik --- 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..cbe6686af3 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, 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 will then start a minimal interpreter waiting for your commands, and the B 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, 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 will then start a minimal interpreter waiting for your commands, and the B 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 ';' */