1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2024-12-22 17:34:18 +03:00

virCommandToStringFull: Improve linebreaking behaviour

Put multiple values for an option if followed by another option as used
in certain iptables arguments.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
This commit is contained in:
Peter Krempa 2021-04-06 15:17:42 +02:00
parent e5124770db
commit 1f61d7129f
3 changed files with 28 additions and 8 deletions

View File

@ -2076,9 +2076,9 @@ virCommandToStringFull(virCommandPtr cmd,
{
size_t i;
g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
bool prevopt = false;
const char *command = cmd->args[0];
g_autofree char *basename = NULL;
bool had_option = false;
/* Cannot assume virCommandRun will be called; so report the error
* now. If virCommandRun is called, it will report the same error. */
@ -2111,16 +2111,33 @@ virCommandToStringFull(virCommandPtr cmd,
virBufferEscapeShell(&buf, command);
for (i = 1; i < cmd->nargs; i++) {
virBufferAddChar(&buf, ' ');
if (linebreaks) {
/* Line break if this is a --arg or if
* the previous arg was a positional option
/* we don't want a linebreak only if
* - the previous argument is an option (starts with '-')
* - there was already an option and another option follows
*/
if (cmd->args[i][0] == '-' ||
!prevopt)
bool linebreak = true;
if (cmd->args[i][0] != '-') {
if (had_option) {
size_t j;
/* we know that arg[i - 1] is valid and arg[i] is not an option */
for (j = i - 1; j < cmd->nargs; j++) {
if (cmd->args[j][0] == '-') {
linebreak = false;
break;
}
}
}
} else {
had_option = true;
}
if (linebreak)
virBufferAddLit(&buf, "\\\n");
}
virBufferEscapeShell(&buf, cmd->args[i]);
prevopt = (cmd->args[i][0] == '-');
}
return virBufferContentAndReset(&buf);

View File

@ -1 +1 @@
A=B C=D E true --foo bar --oooh -f --wizz eek eek -w -z -l --mmm flash bang wallop
A=B C=D E true --foo bar --oooh -f --wizz eek eek --m-m-m-multiarg arg arg2 -w -z -l --mmm flash bang wallop

View File

@ -997,6 +997,7 @@ static int test26(const void *unused G_GNUC_UNUSED)
"--oooh \\\n"
"-f \\\n"
"--wizz 'eek eek' \\\n"
"--m-m-m-multiarg arg arg2 \\\n"
"-w \\\n"
"-z \\\n"
"-l \\\n"
@ -1009,7 +1010,9 @@ static int test26(const void *unused G_GNUC_UNUSED)
virCommandAddEnvPair(cmd, "A", "B");
virCommandAddEnvPair(cmd, "C", "D E");
virCommandAddArgList(cmd, "--foo", "bar", "--oooh", "-f",
"--wizz", "eek eek", "-w", "-z", "-l",
"--wizz", "eek eek",
"--m-m-m-multiarg", "arg", "arg2",
"-w", "-z", "-l",
"--mmm", "flash", "bang", "wallop",
NULL);