From: Kevin Locke Date: Thu, 19 Sep 2019 13:45:54 +0000 (-0600) Subject: bash-completion: Add non-canonical device fallback X-Git-Tag: v2.35-rc1~189^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0c8865f843c1a9552a13a51bcb4c4c31a4e1242d;p=thirdparty%2Futil-linux.git bash-completion: Add non-canonical device fallback It is desirable for bash-completion to complete block devices via symlinks (e.g. under /dev/disk) and with non-canonical locations (e.g. ./sda if $PWD is /dev, and /chroot/dev/sda). Unfortunately, this is a non-trivial task due to how bash-completion works. It is necessary to un-escape the last partial argument, search, then escape the results, ideally handling tilde and variable expansion/completion. See [_get_comp_words_by_ref] and [_filedir] in the bash-completion project for details. Given the development costs of a complete and correct implementation, the annoyance/frustration which would result from an incomplete/buggy implementation, and the trade-offs between under- and over-completion, this commit adds fallback to bash default completion if the argument does not match any canonical device names. This correctly completes in the cases mentioned above, although it incorrectly completes on non-block-device files as well. [_filedir]: https://github.com/scop/bash-completion/blob/2.9/bash_completion#L552 [_get_comp_words_by_ref]: https://github.com/scop/bash-completion/blob/2.9/bash_completion#L365 Signed-off-by: Kevin Locke --- diff --git a/bash-completion/addpart b/bash-completion/addpart index 3179639088..b7e93d1029 100644 --- a/bash-completion/addpart +++ b/bash-completion/addpart @@ -6,6 +6,7 @@ _addpart_module() case $COMP_CWORD in 1) OPTS="--help --version $(lsblk -pnro name)" + compopt -o bashdefault -o default COMPREPLY=( $(compgen -W "${OPTS[*]}" -- $cur) ) ;; 2) diff --git a/bash-completion/blkdiscard b/bash-completion/blkdiscard index 64a5c34d5f..f28e5d4c26 100644 --- a/bash-completion/blkdiscard +++ b/bash-completion/blkdiscard @@ -29,6 +29,7 @@ _blkdiscard_module() return 0 ;; esac + compopt -o bashdefault -o default COMPREPLY=( $(compgen -W "$(lsblk -pnro name)" -- $cur) ) return 0 } diff --git a/bash-completion/blkid b/bash-completion/blkid index 129a677cb0..74de34284c 100644 --- a/bash-completion/blkid +++ b/bash-completion/blkid @@ -93,6 +93,7 @@ _blkid_module() return 0 ;; esac + compopt -o bashdefault -o default COMPREPLY=( $(compgen -W "$(lsblk -pnro name)" -- $cur) ) return 0 } diff --git a/bash-completion/blkzone b/bash-completion/blkzone index a50684df97..b642953485 100755 --- a/bash-completion/blkzone +++ b/bash-completion/blkzone @@ -46,6 +46,7 @@ _blkzone_module() return 0 ;; esac + compopt -o bashdefault -o default COMPREPLY=( $(compgen -W "$(lsblk -pnro name)" -- $cur) ) return 0 } diff --git a/bash-completion/blockdev b/bash-completion/blockdev index e7aca26de7..e673acf543 100644 --- a/bash-completion/blockdev +++ b/bash-completion/blockdev @@ -38,6 +38,7 @@ _blockdev_module() return 0 ;; esac + compopt -o bashdefault -o default COMPREPLY=( $(compgen -W "${OPTS[*]}" -- $cur) ) return 0 } diff --git a/bash-completion/delpart b/bash-completion/delpart index d5a92ef288..3855812089 100644 --- a/bash-completion/delpart +++ b/bash-completion/delpart @@ -12,6 +12,7 @@ _delpart_module() case $COMP_CWORD in 1) OPTS="--help --version $(lsblk -pnro name)" + compopt -o bashdefault -o default COMPREPLY=( $(compgen -W "${OPTS[*]}" -- $cur) ) ;; 2) diff --git a/bash-completion/eject b/bash-completion/eject index 327ef1b713..27d04656c5 100644 --- a/bash-completion/eject +++ b/bash-completion/eject @@ -58,6 +58,7 @@ _eject_module() IFS=$OLD_IFS fi done)" + compopt -o bashdefault -o default COMPREPLY=( $(compgen -W "$DEVS" $cur) ) return 0 } diff --git a/bash-completion/lsblk b/bash-completion/lsblk index a1e9e61b62..e54fc4d5e5 100644 --- a/bash-completion/lsblk +++ b/bash-completion/lsblk @@ -85,6 +85,7 @@ _lsblk_module() return 0 ;; esac + compopt -o bashdefault -o default COMPREPLY=( $(compgen -W "$($1 -pnro name)" -- $cur) ) return 0 } diff --git a/bash-completion/resizepart b/bash-completion/resizepart index 7e38503661..63ca5336b6 100644 --- a/bash-completion/resizepart +++ b/bash-completion/resizepart @@ -12,6 +12,7 @@ _resizepart_module() case $COMP_CWORD in 1) OPTS="--help --version $(lsblk -pnro name)" + compopt -o bashdefault -o default COMPREPLY=( $(compgen -W "${OPTS[*]}" -- $cur) ) ;; 2)