Skip to content

fix: use if [[ $flag ]] for IFS-safe boolean tests #931

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 5 commits into from
Apr 23, 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
1 change: 1 addition & 0 deletions .shellcheckrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ disable=SC1090 # not really fixable usually (ever?)
disable=SC2034 # for localizing variables set in called functions
disable=SC2128 # intentional style choice
disable=SC2206 # suggested alternatives fail in posix mode or use temp files
disable=SC2209 # interferes with our `set` (literal) vs empty booleans
disable=SC2207 # suggested alternatives fail in posix mode or use temp files

# These disables are to be investigated and decided
Expand Down
24 changes: 12 additions & 12 deletions bash_completion
Original file line number Diff line number Diff line change
Expand Up @@ -339,12 +339,12 @@ _comp_expand_glob()
# generated.
_comp_split()
{
local _append=false IFS=$' \t\n'
local _append="" IFS=$' \t\n'

local OPTIND=1 OPTARG="" OPTERR=0 _opt
while getopts ':alF:' _opt "$@"; do
case $_opt in
a) _append=true ;;
a) _append=set ;;
l) IFS=$'\n' ;;
F) IFS=$OPTARG ;;
*)
Expand All @@ -367,7 +367,7 @@ _comp_split()
set -o noglob

local _old_size _new_size
if "$_append"; then
if [[ $_append ]]; then
eval "$1+=()" # in case $1 is unset
eval "_old_size=\${#$1[@]}"
eval "$1+=(\$2)"
Expand Down Expand Up @@ -409,13 +409,13 @@ _comp_split()
# array instead.
_comp_compgen()
{
local _append=false IFS=$' \t\n'
local _append="" IFS=$' \t\n'
local -a _split_options=(-l)

local OPTIND=1 OPTARG="" OPTERR=0 _opt
while getopts ':alF:' _opt "$@"; do
case $_opt in
a) _append=true _split_options+=(-a) ;;
a) _append=set _split_options+=(-a) ;;
l) IFS=$'\n' ;;
F) IFS=$OPTARG ;;
*)
Expand Down Expand Up @@ -447,7 +447,7 @@ _comp_compgen()
local _result
_result=$(compgen "${@:2}") || {
local _status=$?
if "$_append"; then
if [[ $_append ]]; then
# make sure existence of variable
eval -- "$1+=()"
else
Expand Down Expand Up @@ -859,16 +859,16 @@ _variables()
# -k: do not filter out already present tokens in value
_comp_delimited()
{
local prefix="" delimiter=$1 deduplicate=true
local prefix="" delimiter=$1 deduplicate=set
shift
if [[ $delimiter == -k ]]; then
deduplicate=false
deduplicate=""
delimiter=$1
shift
fi
[[ $cur == *$delimiter* ]] && prefix=${cur%"$delimiter"*}$delimiter

if $deduplicate; then
if [[ $deduplicate ]]; then
# We could construct a -X pattern to feed to compgen, but that'd
# conflict with possibly already set -X in $@, as well as have
# glob char escaping issues to deal with. Do removals by hand instead.
Expand Down Expand Up @@ -1959,7 +1959,7 @@ _known_hosts_real()
local OPTIND=1
while getopts "ac46F:p:" flag "$@"; do
case $flag in
a) aliases='yes' ;;
a) aliases=set ;;
c) suffix=':' ;;
F)
if [[ ! $OPTARG ]]; then
Expand All @@ -1969,8 +1969,8 @@ _known_hosts_real()
configfile=$OPTARG
;;
p) prefix=$OPTARG ;;
4) ipv4=1 ;;
6) ipv6=1 ;;
4) ipv4=set ;;
6) ipv6=set ;;
*)
echo "bash_completion: $FUNCNAME: usage error" >&2
return 1
Expand Down
6 changes: 3 additions & 3 deletions completions/_adb
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ _comp_cmd_adb()
;;
esac

local cmd has_cmd=false i
local cmd has_cmd="" i
for ((i = 1; i < cword; i++)); do
if [[ ${words[i]} != -* && ${words[i - 1]} != -[sp] ]]; then
cmd="${words[i]}"
has_cmd=true
has_cmd=set
break
fi
done

if ! "$has_cmd"; then
if [[ ! $has_cmd ]]; then
local tmp=()
if [[ ! $cur || $cur == -* ]]; then
tmp+=($(compgen -W '$(_parse_help "$1" help)' -- "$cur"))
Expand Down
6 changes: 3 additions & 3 deletions completions/_chsh
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ _comp_cmd_chsh()
local cur prev words cword comp_args
_comp_initialize -- "$@" || return

local word chroot="" has_chroot=false
local word chroot="" has_chroot=""
for word in "${words[@]}"; do
if "$has_chroot"; then
if [[ $has_chroot ]]; then
chroot=$word
break
fi
[[ $word != -@(R|-root) ]] || has_chroot=true
[[ $word != -@(R|-root) ]] || has_chroot=set
done

case $prev in
Expand Down
10 changes: 5 additions & 5 deletions completions/_mount.linux
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@ _comp_cmd_mount()
local cur prev words cword comp_args
_comp_initialize -n =: -- "$@" || return

local split=false
local split=""
case "$prev" in
-t | --types)
# find /lib/modules/$(uname -r)/ -type f -path '*/fs/*.ko' -printf '%f\n' | cut -d. -f1
# FIXME: no<fstype>
if [[ $cur == ?*,* ]]; then
prev="${cur%,*}"
cur="${cur##*,}"
split=true
split=set
fi
COMPREPLY=($(compgen -W 'auto adfs affs autofs btrfs cifs coda
cramfs davfs debugfs devpts efs ext2 ext3 ext4 fuse hfs hfsplus
hpfs iso9660 jffs2 jfs minix msdos ncpfs nfs nfs4 ntfs ntfs-3g
proc qnx4 ramfs reiserfs romfs squashfs smbfs sysv tmpfs ubifs
udf ufs umsdos usbfs vfat xfs' -- "$cur"))
_fstypes
$split && COMPREPLY=(${COMPREPLY[@]/#/$prev,})
[[ $split ]] && COMPREPLY=(${COMPREPLY[@]/#/$prev,})
return
;;
--bind | -B | --rbind | -R)
Expand Down Expand Up @@ -74,7 +74,7 @@ _comp_cmd_mount()
if [[ $cur == ?*,* ]]; then
prev="${cur%,*}"
cur="${cur##*,}"
split=true
split=set
fi
# no completion if $cur is opt=smth
[[ $cur == *=* ]] && return
Expand Down Expand Up @@ -206,7 +206,7 @@ _comp_cmd_mount()
esac
# COMP_WORDBREAKS is a real pain in the ass
prev="${prev##*["$COMP_WORDBREAKS"]}"
$split && COMPREPLY=(${COMPREPLY[@]/#/"$prev,"})
[[ $split ]] && COMPREPLY=(${COMPREPLY[@]/#/"$prev,"})
[[ ${COMPREPLY-} == *= ]] && compopt -o nospace
return
;;
Expand Down
6 changes: 3 additions & 3 deletions completions/_slackpkg
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ _comp_cmd_slackpkg()
local cur prev words cword comp_args
_comp_initialize -n = -- "$@" || return

local split=false
local split=""
if [[ $cur == -?*=* ]]; then
prev="${cur%%?(\\)=*}"
cur="${cur#*=}"
split=true
split=set
fi

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

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

if [[ $cur == -* ]]; then
compopt -o nospace
Expand Down
6 changes: 3 additions & 3 deletions completions/_udevadm
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ _comp_cmd_udevadm()
local cur prev words cword split comp_args
_comp_initialize -s -- "$@" || return

local i udevcmd has_udevcmd=false
local i udevcmd has_udevcmd=""
for ((i = 1; i < cword; i++)); do
if [[ ${words[i]} != -* ]]; then
udevcmd=${words[i]}
has_udevcmd=true
has_udevcmd=set
break
fi
done
Expand Down Expand Up @@ -54,7 +54,7 @@ _comp_cmd_udevadm()

$split && return

if ! "$has_udevcmd"; then
if [[ ! $has_udevcmd ]]; then
case $cur in
-*)
COMPREPLY=($(compgen -W '--help --version --debug' -- "$cur"))
Expand Down
6 changes: 3 additions & 3 deletions completions/_umount.linux
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,19 @@ _comp_cmd_umount()
case "$prev" in
-t)
# FIXME: no<fstype>
local split=false
local split=""
if [[ $cur == ?*,* ]]; then
prev="${cur%,*}"
cur="${cur##*,}"
split=true
split=set
fi
COMPREPLY=($(compgen -W 'adfs affs autofs btrfs cifs coda
cramfs debugfs devpts efs ext2 ext3 ext4 fuse hfs hfsplus hpfs
iso9660 jfs minix msdos ncpfs nfs nfs4 ntfs ntfs-3g proc qnx4
ramfs reiserfs romfs squashfs smbfs sysv tmpfs ubifs udf ufs
umsdos usbfs vfat xfs' -- "$cur"))
_fstypes
$split && COMPREPLY=(${COMPREPLY[@]/#/$prev,})
[[ $split ]] && COMPREPLY=(${COMPREPLY[@]/#/$prev,})
return
;;
-O)
Expand Down
8 changes: 4 additions & 4 deletions completions/carton
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ _carton()
local cur prev words cword split comp_args
_comp_initialize -s -- "$@" || return

local i command="" has_command=false
local i command="" has_command=""
for ((i = 1; i < cword; i++)); do
case ${words[i]} in
-*) ;;
*)
command=${words[i]}
has_command=true
has_command=set
break
;;
esac
done

if ! "$has_command"; then
if [[ ! $has_command ]]; then
_carton_commands "$1"
return
fi
Expand All @@ -40,7 +40,7 @@ _carton()
return
;;
--help | -h)
"$has_command" || _carton_commands "$1"
[[ $has_command ]] || _carton_commands "$1"
return
;;
--cpanfile)
Expand Down
10 changes: 5 additions & 5 deletions completions/cvs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ _cvs()
local cur prev words cword comp_args
_comp_initialize -n : -- "$@" || return

local count mode="" i cvsroot="" has_cvsroot=false cvsroots pwd
local count mode="" i cvsroot="" has_cvsroot="" cvsroots pwd
local -a flags files entries changed newremoved

local noargopts='!(-*|*[d]*)'
Expand All @@ -74,7 +74,7 @@ _cvs()
-${noargopts}d)
mode=cvsroot
cvsroot=${words[count + 1]}
has_cvsroot=true
has_cvsroot=set
;;
add | ad | new)
mode=add
Expand Down Expand Up @@ -232,7 +232,7 @@ _cvs()
esac

if [[ $cur != -* ]]; then
! "$has_cvsroot" && cvsroot=${CVSROOT-}
[[ ! $has_cvsroot ]] && cvsroot=${CVSROOT-}
COMPREPLY=($(cvs -d "$cvsroot" co -c 2>/dev/null |
awk '{print $1}'))
((${#COMPREPLY[@]})) &&
Expand Down Expand Up @@ -317,7 +317,7 @@ _cvs()
esac

if [[ $cur != -* ]]; then
! "$has_cvsroot" && cvsroot=${CVSROOT-}
[[ ! $has_cvsroot ]] && cvsroot=${CVSROOT-}
COMPREPLY=($(cvs -d "$cvsroot" co -c | awk '{print $1}'))
((${#COMPREPLY[@]})) &&
COMPREPLY=($(compgen -W '"${COMPREPLY[@]}"' -- "$cur"))
Expand All @@ -339,7 +339,7 @@ _cvs()

if [[ $cur != -* ]]; then
# starts with same algorithm as checkout
! "$has_cvsroot" && cvsroot=${CVSROOT-}
[[ ! $has_cvsroot ]] && cvsroot=${CVSROOT-}
local prefix=${cur%/*}
if [[ -r ${cvsroot}/${prefix} ]]; then
_cvs_modules
Expand Down
6 changes: 3 additions & 3 deletions completions/dmypy
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ _comp_cmd_dmypy()
;;
esac

local cmd has_cmd=false i
local cmd has_cmd="" i
for ((i = 1; i < cword; i++)); do
if [[ ${words[i]} != -* && ${words[i - 1]} != --status-file ]]; then
cmd=${words[i]}
has_cmd=true
has_cmd=set
break
fi
done
Expand All @@ -36,7 +36,7 @@ _comp_cmd_dmypy()
return
fi

if ! "$has_cmd"; then
if [[ ! $has_cmd ]]; then
local cmds=$("$1" --help 2>&1 |
command sed -ne '/positional arguments/{p;n;p;q;}' |
command sed -ne 's/{\(.*\)}/\1/p')
Expand Down
6 changes: 3 additions & 3 deletions completions/export
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ _comp_cmd_export()
local cur prev words cword comp_args
_comp_initialize -n := -- "$@" || return

local i action=variable remove=false
local i action=variable remove=""
for ((i = 1; i < cword; i++)); do
case ${words[i]} in
-p)
Expand All @@ -15,7 +15,7 @@ _comp_cmd_export()
action=function
;;&
-*n*)
remove=true
remove=set
;;
-*)
continue
Expand Down Expand Up @@ -52,7 +52,7 @@ _comp_cmd_export()
return
fi
local suffix=""
if ! $remove && [[ $action != function ]]; then
if [[ ! $remove && $action != function ]]; then
suffix="="
compopt -o nospace
fi
Expand Down
6 changes: 3 additions & 3 deletions completions/find
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ _comp_cmd_find()
;;
esac

local i exprfound=false
local i exprfound=""
# set exprfound to true if there is already an expression present
for i in "${words[@]}"; do
[[ $i == [-\(\),\!]* ]] && exprfound=true && break
[[ $i == [-\(\),\!]* ]] && exprfound=set && break
done

# handle case where first parameter is not a dash option
if ! $exprfound && [[ $cur != [-\(\),\!]* ]]; then
if [[ ! $exprfound && $cur != [-\(\),\!]* ]]; then
_filedir -d
return
fi
Expand Down
Loading