Skip to content

fix(mdtool,rcs,etc): fix uses of -G 'pat' and -f -X '!pat' #1358

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 6 commits into from
Apr 9, 2025
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
8 changes: 8 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ Also, please bear the following coding guidelines in mind:
expansions will be unexpectedly performed, which becomes a vulnerability. In
the latter case, checks by shellcheck and shfmt will not be performed inside
`'...'`. Also, `_comp_compgen_split` is `IFS`-safe.

Avoid using `_comp_compgen -- -G "pattern"` to generate completions. The
result is not filtered by the current word `cur` due to the Bash design of
`compgen`. Also, this cannot be used to generate filenames with a specified
extension because the `-G` specification only generates the matching
filepaths in the current directory. It does not look into subdirectories
even when `$cur` implies completion in a subdirectory. One can instead use
`_comp_compgen -- -f -X '!pattern'`.

- When completing available options, offer only the most descriptive
ones as completion results if there are multiple options that do the
Expand Down
4 changes: 2 additions & 2 deletions completions/feh
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ _comp_cmd_feh()
fi
local font_path
# font_path="$(imlib2-config --prefix 2>/dev/null)/share/imlib2/data/fonts"
# _comp_compgen -C "$font_path" -- -f -X "!*.@([tT][tT][fF])" -S /
# _comp_compgen -C "$font_path" -- -f -X "!*.[tT][tT][fF]" -S /
for ((i = ${#words[@]} - 2; i > 0; i--)); do
if [[ ${words[i]} == -@(C|-fontpath) ]]; then
font_path="${words[i + 1]}"
if [[ -d $font_path ]]; then
_comp_compgen -aC "$font_path" -- \
-f -X "!*.@([tT][tT][fF])" -S /
-f -X "!*.[tT][tT][fF]" -S /
fi
fi
done
Expand Down
6 changes: 3 additions & 3 deletions completions/mdtool
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ _comp_cmd_mdtool()
#if [[ "$prev" == *: ]]; then
# case $prev in
# @(--p:|--project:))
# _comp_compgen -- -f -G "*.mdp"
# _comp_compgen -- -f -X '!*.mdp'
# ;;
# @(--f:|--buildfile:))
# _comp_compgen -- -f -G "*.mdp" -G "*.mds"
# _comp_compgen -- -f -X '!*.md[ps]'
# ;;
# esac
#fi
return
;;
"generate-makefiles")
compopt -o filenames
_comp_compgen -- -o filenames -G"*.mds"
_comp_compgen -- -o filenames -f -X '!*.mds'
if [[ $prev == *mds ]]; then
_comp_compgen -- -W '--simple-makefiles --s --d:'
fi
Expand Down
7 changes: 5 additions & 2 deletions completions/rcs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ _comp_cmd_rcs()
local cur prev words cword comp_args
_comp_initialize -- "$@" || return

local file dir i
local file dir

file=${cur##*/}
dir=${cur%/*}
Expand All @@ -21,8 +21,11 @@ _comp_cmd_rcs()
COMPREPLY[i]=$dir$file
done

_comp_compgen -aR -- -G "$dir/$file*,v"
local files
_comp_expand_glob files '"$dir/$file"*,v' &&
_comp_compgen -aR -- -W '"${files[@]}"'

local i
for i in ${!COMPREPLY[*]}; do
COMPREPLY[i]=${COMPREPLY[i]%,v}
done
Expand Down
2 changes: 1 addition & 1 deletion completions/upgradepkg
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ _comp_cmd_upgradepkg()
cur="${cur#*%}"
local nofiles=""
compopt -o filenames
_comp_compgen -- -P "$prev%" -f -X "!*.@(t[bgxl]z)" || nofiles=set
_comp_compgen -- -P "$prev%" -f -X "!*.t[bgxl]z" || nofiles=set
_comp_compgen -a -- -P "$prev%" -S '/' -d
[[ $nofiles ]] && compopt -o nospace
return
Expand Down
2 changes: 1 addition & 1 deletion completions/valgrind
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ _comp_cmd_valgrind()

local i
for ((i = 1; i <= cword; i++)); do
if [[ ${words[i]} != @([-=])* ]]; then
if [[ ${words[i]} != [-=]* ]]; then
_comp_command_offset $i
return
fi
Expand Down
3 changes: 3 additions & 0 deletions test/runLint
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ gitgrep '(?<!command)'"$cmdstart"'(grep|ls|sed|cd)(\s|$)' \
gitgrep '(?<!command)'"$cmdstart"'awk(\s|$)' \
'invoke awk through "_comp_awk"'

gitgrep '@\([^()|$]+\)' \
'@(...) may not be needed when ... does not contain |.'

#------------------------------------------------------------------------------
# Bash pitfalls/styles/compatibilities (which are not detected by shellcheck)

Expand Down