Skip to content

fix: quote $split of $split && return for custom IFS #918

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions bash_completion
Original file line number Diff line number Diff line change
Expand Up @@ -983,21 +983,21 @@ _comp_variable_assignments()
# argument $2 is the string before the cursor in the
# current word. The third argument $3 is the previous
# word.
# @var[out] cur Reconstructed current word
# @var[out] prev Reconstructed previous word
# @var[out] words Reconstructed words
# @var[out] cword Current word index in `words`
# @var[out] comp_args Original arguments specified to the completion function
# are saved in this array, if the arguments $1...$3 is
# specified.
# @var[out,opt] split When "-s" is specified, `true/false` is set depending on
# whether the split happened.
# @var[out] cur Reconstructed current word
# @var[out] prev Reconstructed previous word
# @var[out] words Reconstructed words
# @var[out] cword Current word index in `words`
# @var[out] comp_args Original arguments specified to the completion function
# are saved in this array, if the arguments $1...$3 is
# specified.
# @var[out,opt] was_split When "-s" is specified, `"set"/""` is set depending
# on whether the split happened.
# @return True (0) if completion needs further processing,
# False (> 0) no further processing is necessary.
#
_comp_initialize()
{
local exclude="" outx errx inx
local exclude="" opt_split="" outx errx inx

local flag OPTIND=1 OPTARG="" OPTERR=0
while getopts "n:e:o:i:s" flag "$@"; do
Expand All @@ -1007,7 +1007,8 @@ _comp_initialize()
o) outx=$OPTARG ;;
i) inx=$OPTARG ;;
s)
split=false
opt_split="set"
was_split=""
exclude+="="
;;
*)
Expand Down Expand Up @@ -1065,7 +1066,7 @@ _comp_initialize()
((cword <= 0)) && return 1
prev=${words[cword - 1]}

[[ ${split-} ]] && _split_longopt && split=true
[[ $opt_split ]] && _split_longopt && was_split="set"

return 0
}
Expand Down Expand Up @@ -2371,7 +2372,7 @@ _complete_as_root()

_longopt()
{
local cur prev words cword split comp_args
local cur prev words cword was_split comp_args
_comp_initialize -s -- "$@" || return

case "${prev,,}" in
Expand Down Expand Up @@ -2402,7 +2403,7 @@ _longopt()
;;
esac

$split && return
[[ $was_split ]] && return

if [[ $cur == -* ]]; then
_comp_compgen COMPREPLY -W "$(LC_ALL=C $1 --help 2>&1 |
Expand Down
54 changes: 53 additions & 1 deletion bash_completion.d/000_bash_completion_compat.bash
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ _comp_deprecate_func _userland _comp_userland
_comp_deprecate_func _sysvdirs _comp_sysvdirs
_comp_deprecate_func _have _comp_have_command
_comp_deprecate_func _rl_enabled _comp_readline_variable_on
_comp_deprecate_func _init_completion _comp_initialize
_comp_deprecate_func _command_offset _comp_command_offset
_comp_deprecate_func _command _comp_command
_comp_deprecate_func _root_command _comp_root_command
Expand Down Expand Up @@ -178,6 +177,59 @@ _realcommand()
return $rc
}

# Initialize completion and deal with various general things: do file
# and variable completion where appropriate, and adjust prev, words,
# and cword as if no redirections exist so that completions do not
# need to deal with them. Before calling this function, make sure
# cur, prev, words, and cword are local, ditto split if you use -s.
#
# Options:
# -n EXCLUDE Passed to _comp_get_words -n with redirection chars
# -e XSPEC Passed to _filedir as first arg for stderr redirections
# -o XSPEC Passed to _filedir as first arg for other output redirections
# -i XSPEC Passed to _filedir as first arg for stdin redirections
# -s Split long options with _split_longopt, implies -n =
# @var[out] cur Reconstructed current word
# @var[out] prev Reconstructed previous word
# @var[out] words Reconstructed words
# @var[out] cword Current word index in `words`
# @var[out,opt] split When "-s" is specified, `"true"/"false"` is set depending
# on whether the split happened.
# @return True (0) if completion needs further processing,
# False (> 0) no further processing is necessary.
#
# @deprecated Use the new interface `_comp_initialize`. The new interface
# supports the same set of options. The new interface receives additional
# arguments $1 (command name), $2 (part of current word before the cursor), and
# $3 (previous word) that are specified to the completion function by Bash.
# When `-s` is specified, instead of variable `split`, the new interface sets
# variable `was_split` to the value "set"/"" when the split happened/not
# happened.
_init_completion()
{
local was_split
_comp_initialize "$@"
local rc=$?

# When -s is specified, convert "split={set,}" to "split={true,false}"
local flag OPTIND=1 OPTARG="" OPTERR=0
while getopts "n:e:o:i:s" flag "$@"; do
case $flag in
[neoi]) ;;
s)
if [[ $was_split ]]; then
split=true
else
split=false
fi
break
;;
esac
done

return "$rc"
}

# @deprecated Use the variable `_comp_backup_glob` instead. This is the
# backward-compatibility name.
# shellcheck disable=SC2154 # defined in the main "bash_completion"
Expand Down
4 changes: 2 additions & 2 deletions completions/2to3
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

_2to3()
{
local cur prev words cword split comp_args
local cur prev words cword was_split comp_args
_comp_initialize -s -- "$@" || return

case $prev in
Expand All @@ -24,7 +24,7 @@ _2to3()
;;
esac

$split && return
[[ $was_split ]] && return

if [[ $cur == -* ]]; then
COMPREPLY=($(compgen -W '$(_parse_help "$1")' -- "$cur"))
Expand Down
4 changes: 2 additions & 2 deletions completions/_mock
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

_comp_cmd_mock()
{
local cur prev words cword split comp_args
local cur prev words cword was_split comp_args
_comp_initialize -s -- "$@" || return

local plugins='tmpfs root_cache yum_cache bind_mount ccache'
Expand Down Expand Up @@ -55,7 +55,7 @@ _comp_cmd_mock()
;;
esac

$split && return
[[ $was_split ]] && return

if [[ $cur == -* ]]; then
COMPREPLY=($(compgen -W '$(_parse_help "$1")' -- "$cur"))
Expand Down
4 changes: 2 additions & 2 deletions completions/_repomanage
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

_comp_cmd_repomanage()
{
local cur prev words cword split comp_args
local cur prev words cword was_split comp_args
_comp_initialize -s -- "$@" || return

[[ $prev == -@([hk]|-help|-keep) ]] && return

$split && return
[[ $was_split ]] && return

if [[ $cur == -* ]]; then
COMPREPLY=($(compgen -W '$(_parse_help "$1")' -- "$cur"))
Expand Down
4 changes: 2 additions & 2 deletions completions/_rtcwake
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

_comp_cmd_rtcwake()
{
local cur prev words cword split comp_args
local cur prev words cword was_split comp_args
_comp_initialize -s -- "$@" || return

case "$prev" in
Expand All @@ -24,7 +24,7 @@ _comp_cmd_rtcwake()
;;
esac

$split && return
[[ $was_split ]] && return

COMPREPLY=($(compgen -W '$(_parse_help "$1")' -- "$cur"))
} &&
Expand Down
4 changes: 2 additions & 2 deletions completions/_su
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fi

_comp_cmd_su()
{ # linux-specific completion
local cur prev words cword split comp_args
local cur prev words cword was_split comp_args
_comp_initialize -s -- "$@" || return

case "$prev" in
Expand All @@ -26,7 +26,7 @@ _comp_cmd_su()
;;
esac

$split && return
[[ $was_split ]] && return

if [[ $cur == -* ]]; then
COMPREPLY=($(compgen -W '$(_parse_help "$1")' -- "$cur"))
Expand Down
4 changes: 2 additions & 2 deletions completions/_udevadm
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

_comp_cmd_udevadm()
{
local cur prev words cword split comp_args
local cur prev words cword was_split comp_args
_comp_initialize -s -- "$@" || return

local i udevcmd has_udevcmd=""
Expand Down Expand Up @@ -52,7 +52,7 @@ _comp_cmd_udevadm()
;;
esac

$split && return
[[ $was_split ]] && return

if [[ ! $has_udevcmd ]]; then
case $cur in
Expand Down
4 changes: 2 additions & 2 deletions completions/_yum
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ _yum_plugins()

_comp_cmd_yum()
{
local cur prev words cword split comp_args
local cur prev words cword was_split comp_args
_comp_initialize -s -- "$@" || return

local special="" i
Expand Down Expand Up @@ -136,7 +136,7 @@ _comp_cmd_yum()
;;
esac

$split && return
[[ $was_split ]] && return

if [[ $cur == -* ]]; then
COMPREPLY=($(compgen -W '$(_parse_help "$1")' -- "$cur"))
Expand Down
4 changes: 2 additions & 2 deletions completions/a2x
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

_a2x()
{
local cur prev words cword split comp_args
local cur prev words cword was_split comp_args
_comp_initialize -s -- "$@" || return

local noargopts='!(-*|*[aDd]*)'
Expand All @@ -26,7 +26,7 @@ _a2x()
;;
esac

$split && return
[[ $was_split ]] && return

if [[ $cur == -* ]]; then
COMPREPLY=($(compgen -W '$(_parse_help "$1" --help)' -- "$cur"))
Expand Down
4 changes: 2 additions & 2 deletions completions/aclocal
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

_aclocal()
{
local cur prev words cword split comp_args
local cur prev words cword was_split comp_args
_comp_initialize -s -- "$@" || return

case "$prev" in
Expand All @@ -25,7 +25,7 @@ _aclocal()
;;
esac

$split && return
[[ $was_split ]] && return

COMPREPLY=($(compgen -W '$(_parse_help "$1")' -- "$cur"))
[[ ${COMPREPLY-} == *= ]] && compopt -o nospace
Expand Down
4 changes: 2 additions & 2 deletions completions/add_members
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

_add_members()
{
local cur prev words cword split comp_args
local cur prev words cword was_split comp_args
_comp_initialize -s -- "$@" || return

case $prev in
Expand All @@ -16,7 +16,7 @@ _add_members()
;;
esac

$split && return
[[ $was_split ]] && return

if [[ $cur == -* ]]; then
COMPREPLY=($(compgen -W '--regular-members-file --digest-members-file
Expand Down
4 changes: 2 additions & 2 deletions completions/appdata-validate
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

_appdata_validate()
{
local cur prev words cword split comp_args
local cur prev words cword was_split comp_args
_comp_initialize -s -- "$@" || return

case $prev in
Expand All @@ -17,7 +17,7 @@ _appdata_validate()
;;
esac

$split && return
[[ $was_split ]] && return

if [[ $cur == -* ]]; then
COMPREPLY=($(compgen -W '$(_parse_help "$1")' -- "$cur"))
Expand Down
4 changes: 2 additions & 2 deletions completions/apt-mark
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

_comp_cmd_apt_mark()
{
local cur prev words cword split comp_args
local cur prev words cword was_split comp_args
_comp_initialize -s -- "$@" || return

local special="" i
Expand Down Expand Up @@ -49,7 +49,7 @@ _comp_cmd_apt_mark()
;;
esac

$split && return
[[ $was_split ]] && return

if [[ $cur == -* ]]; then
COMPREPLY=($(compgen -W '--file= --help --version --config-file
Expand Down
4 changes: 2 additions & 2 deletions completions/arch
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
_comp_have_command mailmanctl &&
_arch()
{
local cur prev words cword split comp_args
local cur prev words cword was_split comp_args
_comp_initialize -s -- "$@" || return

case $prev in
Expand All @@ -19,7 +19,7 @@ _comp_have_command mailmanctl &&
;;
esac

$split && return
[[ $was_split ]] && return

if [[ $cur == -* ]]; then
COMPREPLY=($(compgen -W '$(_parse_help "$1")' -- "$cur"))
Expand Down
4 changes: 2 additions & 2 deletions completions/asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ _comp_deprecate_func _asciidoc_doctype _comp_xfunc_asciidoc_doctype

_asciidoc()
{
local cur prev words cword split comp_args
local cur prev words cword was_split comp_args
_comp_initialize -s -- "$@" || return

local noargopts='!(-*|*[abfdo]*)'
Expand Down Expand Up @@ -40,7 +40,7 @@ _asciidoc()
;;
esac

$split && return
[[ $was_split ]] && return

if [[ $cur == -* ]]; then
COMPREPLY=($(compgen -W '$(_parse_help "$1" "--help manpage")' \
Expand Down
Loading