mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-01-02 19:15:20 +00: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:
parent
e5124770db
commit
1f61d7129f
@ -2076,9 +2076,9 @@ virCommandToStringFull(virCommandPtr cmd,
|
|||||||
{
|
{
|
||||||
size_t i;
|
size_t i;
|
||||||
g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
|
g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
|
||||||
bool prevopt = false;
|
|
||||||
const char *command = cmd->args[0];
|
const char *command = cmd->args[0];
|
||||||
g_autofree char *basename = NULL;
|
g_autofree char *basename = NULL;
|
||||||
|
bool had_option = false;
|
||||||
|
|
||||||
/* Cannot assume virCommandRun will be called; so report the error
|
/* Cannot assume virCommandRun will be called; so report the error
|
||||||
* now. If virCommandRun is called, it will report the same error. */
|
* now. If virCommandRun is called, it will report the same error. */
|
||||||
@ -2111,16 +2111,33 @@ virCommandToStringFull(virCommandPtr cmd,
|
|||||||
virBufferEscapeShell(&buf, command);
|
virBufferEscapeShell(&buf, command);
|
||||||
for (i = 1; i < cmd->nargs; i++) {
|
for (i = 1; i < cmd->nargs; i++) {
|
||||||
virBufferAddChar(&buf, ' ');
|
virBufferAddChar(&buf, ' ');
|
||||||
|
|
||||||
if (linebreaks) {
|
if (linebreaks) {
|
||||||
/* Line break if this is a --arg or if
|
/* we don't want a linebreak only if
|
||||||
* the previous arg was a positional option
|
* - the previous argument is an option (starts with '-')
|
||||||
|
* - there was already an option and another option follows
|
||||||
*/
|
*/
|
||||||
if (cmd->args[i][0] == '-' ||
|
bool linebreak = true;
|
||||||
!prevopt)
|
|
||||||
|
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");
|
virBufferAddLit(&buf, "\\\n");
|
||||||
}
|
}
|
||||||
virBufferEscapeShell(&buf, cmd->args[i]);
|
virBufferEscapeShell(&buf, cmd->args[i]);
|
||||||
prevopt = (cmd->args[i][0] == '-');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return virBufferContentAndReset(&buf);
|
return virBufferContentAndReset(&buf);
|
||||||
|
@ -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
|
||||||
|
@ -997,6 +997,7 @@ static int test26(const void *unused G_GNUC_UNUSED)
|
|||||||
"--oooh \\\n"
|
"--oooh \\\n"
|
||||||
"-f \\\n"
|
"-f \\\n"
|
||||||
"--wizz 'eek eek' \\\n"
|
"--wizz 'eek eek' \\\n"
|
||||||
|
"--m-m-m-multiarg arg arg2 \\\n"
|
||||||
"-w \\\n"
|
"-w \\\n"
|
||||||
"-z \\\n"
|
"-z \\\n"
|
||||||
"-l \\\n"
|
"-l \\\n"
|
||||||
@ -1009,7 +1010,9 @@ static int test26(const void *unused G_GNUC_UNUSED)
|
|||||||
virCommandAddEnvPair(cmd, "A", "B");
|
virCommandAddEnvPair(cmd, "A", "B");
|
||||||
virCommandAddEnvPair(cmd, "C", "D E");
|
virCommandAddEnvPair(cmd, "C", "D E");
|
||||||
virCommandAddArgList(cmd, "--foo", "bar", "--oooh", "-f",
|
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",
|
"--mmm", "flash", "bang", "wallop",
|
||||||
NULL);
|
NULL);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user