Reimplement umount completion to not use gawk's gensub.

Gbp-Pq: Name umount-completion.patch
This commit is contained in:
Étienne Mollier 2022-05-14 03:14:50 +08:00 committed by openKylinBot
parent 2581103b71
commit 8de924cffc
1 changed files with 37 additions and 24 deletions

View File

@ -1,3 +1,33 @@
_umount_points_list()
{
# List of characters to escape, shamelessly stolen from "scp" comp.
local escape_chars='[][(){}<>\",:;^&!$=?`|\\'\'' \t\f\n\r\v]'
# This is most odd, but we are adding artifically a space after the
# file name because, somehow, it enables proper escaping of dangerous
# characters, e.g. "|" -> "\|". Without space, it is possible to get
# either 0 "|" or 2 "\\|" backslashes, but 1 does not work. Also,
# sticking to sub() and gsub(), instead of gensub(), allows to be AWK
# implementation agnostic.
findmnt -lno TARGET | awk '{
if ($0 ~ "^"ENVIRON["HOME"]) {
homeless = $0
sub("^"ENVIRON["HOME"], "~", homeless)
gsub("'"$escape_chars"'", "\\\\&", homeless)
print homeless " "
}
if ($0 ~ "^"ENVIRON["PWD"]) {
reldir = $0
sub("^"ENVIRON["PWD"]"/?", "", reldir)
gsub("'"$escape_chars"'", "\\\\&", reldir)
print "./" reldir " "
print reldir " "
}
gsub("'"$escape_chars"'", "\\\\&")
print $0 " "
}'
}
_umount_module()
{
local cur prev OPTS
@ -48,28 +78,11 @@ _umount_module()
return 0
;;
esac
local oldifs=$IFS
IFS=$'\n'
COMPREPLY=( $( compgen -W "$(findmnt -lno TARGET | awk \
'{
if ($0 ~ ENVIRON["HOME"]) {
homeless = $0
homeless = gensub(ENVIRON["HOME"], "\\\\~", "g", homeless)
homeless = gensub(/(\s)/, "\\\\\\1", "g", homeless)
print homeless
}
if ($0 ~ ENVIRON["PWD"]) {
reldir = $0
reldir = gensub(ENVIRON["PWD"]"/", "", "g", reldir)
reldir = gensub(/(\s)/, "\\\\\\1", "g", reldir)
print "./" reldir
print reldir
}
gsub(/\s/, "\\\\&")
print $0
}'
)" -- "$cur" ) )
IFS=$oldifs
local IFS=$'\n'
COMPREPLY=( $( compgen -W '$( _umount_points_list )' -- "$cur" ) )
}
complete -F _umount_module umount
# counteract the artificial addition of " " in _gen_mount_points() by
# disabling spaces automatically appended to the end of the file name
# completion via "-o nospace".
complete -F _umount_module -o nospace umount