[PATCH] bash-completion: Fix argument passing to $1

Michal Privoznik posted 1 patch 3 years ago
Test syntax-check failed
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/8151dfefc6209eb3840b733b97d722ca77022191.1618925685.git.mprivozn@redhat.com
tools/bash-completion/vsh | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
[PATCH] bash-completion: Fix argument passing to $1
Posted by Michal Privoznik 3 years ago
Our vsh bash completion string is merely just a wrapper over
virsh/virt-admin complete (cmdComplete) - a hidden command that
uses internal readline completion to generate list of candidates.
But this means that we have to pass some additional arguments to
the helper process: e.g. connection URI and R/O flag.

Candidates are printed on a separate line each (and can contain
space), which means that when bash is reading the helper's output
into an array, it needs to split items on '\n' char - hence the
IFS=$'\n' prefix on the line executing the helper. This was
introduced in b889594a70.

But this introduced a regression - those extra arguments we might
pass are stored in a string and previously were split on a space
character (because $IFS was kept untouched and by default
contains space). But now, after the fix that's no longer the case
and thus virsh/virt-admin sees ' -r -c URI' as one argument.

The solution is to take $IFS out of the picture by storing the
extra arguments in an array instead of string.

Fixes: b889594a7092440dc916e3f43eeeaca2684571ee
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
---
 tools/bash-completion/vsh | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/tools/bash-completion/vsh b/tools/bash-completion/vsh
index bbb25fc3eb..402945a8c5 100644
--- a/tools/bash-completion/vsh
+++ b/tools/bash-completion/vsh
@@ -30,12 +30,12 @@ _vsh_complete()
         c=$((++c))
     done
 
-    CMDLINE=
+    CMDLINE=( )
     if [ -n "${RO}" ]; then
-        CMDLINE="${CMDLINE} -r"
+        CMDLINE+=("-r")
     fi
     if [ -n "${URI}" ]; then
-        CMDLINE="${CMDLINE} -c ${URI}"
+        CMDLINE+=("-c" "${URI}")
     fi
 
     INPUT=( "${COMP_WORDS[@]:$i:$COMP_CWORD}" )
@@ -56,7 +56,7 @@ _vsh_complete()
     # the name of the command whose arguments are being
     # completed.
     # Therefore, we might just run $1.
-    IFS=$'\n' A=($($1 ${CMDLINE} complete -- "${INPUT[@]}" 2>/dev/null))
+    IFS=$'\n' A=($($1 ${CMDLINE[@]} complete -- "${INPUT[@]}" 2>/dev/null))
 
     COMPREPLY=($(compgen -W "${A[*]%--}" -- ${cur}))
     __ltrim_colon_completions "$cur"
-- 
2.26.3

Re: [PATCH] bash-completion: Fix argument passing to $1
Posted by Erik Skultety 3 years ago
On Tue, Apr 20, 2021 at 03:34:53PM +0200, Michal Privoznik wrote:
> Our vsh bash completion string is merely just a wrapper over
> virsh/virt-admin complete (cmdComplete) - a hidden command that
> uses internal readline completion to generate list of candidates.
> But this means that we have to pass some additional arguments to
> the helper process: e.g. connection URI and R/O flag.
> 
> Candidates are printed on a separate line each (and can contain
> space), which means that when bash is reading the helper's output
> into an array, it needs to split items on '\n' char - hence the
> IFS=$'\n' prefix on the line executing the helper. This was
> introduced in b889594a70.
> 
> But this introduced a regression - those extra arguments we might
> pass are stored in a string and previously were split on a space
> character (because $IFS was kept untouched and by default
> contains space). But now, after the fix that's no longer the case
> and thus virsh/virt-admin sees ' -r -c URI' as one argument.
> 
> The solution is to take $IFS out of the picture by storing the
> extra arguments in an array instead of string.
> 
> Fixes: b889594a7092440dc916e3f43eeeaca2684571ee
> Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
> ---
>  tools/bash-completion/vsh | 8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)

Makes sense and works.
Reviewed-by: Erik Skultety <eskultet@redhat.com>