From 4db8ffeb2ef26930a7042febc95fb9411168315c Mon Sep 17 00:00:00 2001 From: Peter Krempa Date: Wed, 11 Aug 2021 15:22:59 +0200 Subject: [PATCH] virsh: Add testing for vshStringToArray MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add a '--split' switch for the 'virsh echo' command and add few test cases to the virshtest. Signed-off-by: Peter Krempa Reviewed-by: Martin Kletzander Reviewed-by: Ján Tomko --- tests/virshtest.c | 11 +++++++++++ tools/vsh.c | 15 +++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/tests/virshtest.c b/tests/virshtest.c index 07c27428ae..751e8ffc49 100644 --- a/tests/virshtest.c +++ b/tests/virshtest.c @@ -487,6 +487,17 @@ mymain(void) DO_TEST(46, "a\n", "#unbalanced; 'quotes\"\necho a # b"); DO_TEST(47, "a\n", "\\# ignored;echo a\n'#also' ignored"); + /* test of splitting in vshStringToArray */ + DO_TEST(48, "a\nb,c,\nd,,e,,\nf,,,e\n", + "-q", "echo", "--split", "a,b,,c,,,d,,,,e,,,,,f,,,,,,e"); + DO_TEST(49, "\na\nb,c,\nd,,e,,\nf,,,e\n\n", + "-q", "echo", "--split", ",a,b,,c,,,d,,,,e,,,,,f,,,,,,e,"); + DO_TEST(50, ",a\nb,c,\nd,,e,,\nf,,,e,\n", + "-q", "echo", "--split", ",,a,b,,c,,,d,,,,e,,,,,f,,,,,,e,,"); + DO_TEST(51, ",\na\nb,c,\nd,,e,,\nf,,,e,\n\n", + "-q", "echo", "--split", ",,,a,b,,c,,,d,,,,e,,,,,f,,,,,,e,,,"); + DO_TEST(52, ",,a\nb,c,\nd,,e,,\nf,,,e,,\n", + "-q", "echo", "--split", ",,,,a,b,,c,,,d,,,,e,,,,,f,,,,,,e,,,,"); # undef DO_TEST VIR_FREE(custom_uri); diff --git a/tools/vsh.c b/tools/vsh.c index cca2920711..e67c0b35db 100644 --- a/tools/vsh.c +++ b/tools/vsh.c @@ -3115,6 +3115,10 @@ const vshCmdOptDef opts_echo[] = { .type = VSH_OT_BOOL, .help = N_("escape for XML use") }, + {.name = "split", + .type = VSH_OT_BOOL, + .help = N_("split each argument on ','; ',,' is an escape sequence") + }, {.name = "err", .type = VSH_OT_BOOL, .help = N_("output to stderr"), @@ -3153,11 +3157,14 @@ cmdEcho(vshControl *ctl, const vshCmd *cmd) bool shell = vshCommandOptBool(cmd, "shell"); bool xml = vshCommandOptBool(cmd, "xml"); bool err = vshCommandOptBool(cmd, "err"); + bool split = vshCommandOptBool(cmd, "split"); const vshCmdOpt *opt = NULL; g_autofree char *arg = NULL; g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER; VSH_EXCLUSIVE_OPTIONS_VAR(shell, xml); + VSH_EXCLUSIVE_OPTIONS_VAR(shell, split); + VSH_EXCLUSIVE_OPTIONS_VAR(xml, split); while ((opt = vshCommandOptArgv(ctl, cmd, opt))) { const char *curr = opt->data; @@ -3166,6 +3173,14 @@ cmdEcho(vshControl *ctl, const vshCmd *cmd) virBufferEscapeString(&buf, "%s", curr); } else if (shell) { virBufferEscapeShell(&buf, curr); + } else if (split) { + g_auto(GStrv) spl = NULL; + GStrv n; + + vshStringToArray(curr, &spl); + + for (n = spl; *n; n++) + virBufferAsprintf(&buf, "%s\n", *n); } else { virBufferAdd(&buf, curr, -1); }