Skip to content

Commit 82042d2

Browse files
committed
refactor(pkgadd): fix various problems
* fix a leaked variable `filedir` (written directly in the global context) * fix a problem that filenames containing IFS would be splitted `$(/bin/ls ...)`. * fix a problem that the fixed path `/bin/ls' always uses the system `ls' even when the user wants to replace it with another version for virtual environments, NixOS, etc. * fix a problem that elements of tmplist would be added multiple times with `tmplist+=(${tmplist[@]-} new_elem)` * fix a problem that word splitting and pathname expansions are performed for `${tmplist[@]-}.` * fix a problem that the package names can be splitted by (unusual) IFS * fix localvar_inherit for `pkginst_list`
1 parent b7396b5 commit 82042d2

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

completions/pkgadd

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,21 @@ _pkgadd()
3939
local -a opts=(-a -A -d -k -n -M -P -r -R -s -v -V -x)
4040
COMPREPLY=($(compgen -W '"${opts[@]}"' -- "${cur}"))
4141
else
42-
local pkginst_list
42+
local -a pkginst_list
4343
if [[ -d $device ]]; then
44-
local -a tmplist
45-
for filedir in $(/bin/ls -1 "$device"); do
46-
if [[ -d "$device/$filedir" && -f "$device/$filedir/pkginfo" ]]; then
47-
tmplist+=(${tmplist[@]-} "$filedir")
48-
fi
49-
done
50-
pkginst_list="${tmplist[*]}"
44+
_comp_expand_glob pkginst_list '"$device"/*/pkginfo'
45+
if ((${#pkginst_list[@]})); then
46+
pkginst_list=("${pkginst_list[@]#"$device/"}")
47+
pkginst_list=("${pkginst_list[@]%/pkginfo}")
48+
fi
5149
else
5250
local ret
5351
_comp_dequote "$device"
54-
pkginst_list="$(strings "$ret" |
55-
command grep ^PKG= | sort -u | cut -d= -f2)"
52+
_comp_split -l pkginst_list "$(strings "${ret-}" |
53+
command sed -n 's/^PKG=//p' | sort -u)"
5654
fi
57-
COMPREPLY=($(compgen -W "$pkginst_list" -- "${cur}"))
55+
((${#pkginst_list[@]})) &&
56+
COMPREPLY=($(compgen -W '"${pkginst_list[@]}"' -- "${cur}"))
5857
fi
5958
;;
6059
esac

0 commit comments

Comments
 (0)