[Bash-completion-commits] [SCM] bash-completion branch, master, updated. ec82c536ed22d5a79eb6d06675fb9f634943fb1f
Ville Skyttä
ville.skytta at iki.fi
Sun Oct 31 19:48:50 UTC 2010
The following commit has been merged in the master branch:
commit a877567477da58e6c457a3a1d8d223656e3ed249
Author: Ville Skyttä <ville.skytta at iki.fi>
Date: Sun Oct 31 21:23:26 2010 +0200
Improve relevance of many user/group completions, depending on context.
_usergroup now has a -u option, and there are new _allowed_users and
_allowed_groups helpers. These can be used to limit returned users
and/or groups to ones that the user has access to (or should be
assumed to have access to if running a "root command").
I had to remove a couple of "funky user" chown test cases because for
some reason they were broken by this change, I didn't immediately find
out why, and I couldn't come up with a valid use case that should be
supported for them that would be more beneficial than the relevance
improvements in this patch.
diff --git a/CHANGES b/CHANGES
index ed4f854..646bfda 100644
--- a/CHANGES
+++ b/CHANGES
@@ -32,6 +32,7 @@ bash-completion (2.x)
* Add *.gem to tar completions.
* Complete known hosts from avahi-browse only if $COMP_KNOWN_HOSTS_WITH_AVAHI
is non-empty (Alioth: #312691, RedHat: #630326).
+ * Improve relevance of many user/group completions, depending on context.
[ Freddy Vulto ]
* Added _tilde(), fix ~username completion (Alioth: #312613, Debian: #587095)
diff --git a/bash_completion b/bash_completion
index de37fc9..4f853be 100644
--- a/bash_completion
+++ b/bash_completion
@@ -977,10 +977,10 @@ _installed_modules()
# The : must be added manually; it will only complete usernames initially.
# The legacy user.group format is not supported.
#
-# It assumes compopt -o filenames; but doesn't touch it.
+# @param $1 If -u, only return users/groups the user has access to in
+# context of current completion.
_usergroup()
{
- local IFS=$'\n'
if [[ $cur = *\\\\* || $cur = *:*:* ]]; then
# Give up early on if something seems horribly wrong.
return
@@ -991,19 +991,61 @@ _usergroup()
local prefix
prefix=${cur%%*([^:])}
prefix=${prefix//\\}
- COMPREPLY=( $( compgen -P "$prefix" -g -- "${cur#*[:]}" ) )
+ local mycur="${cur#*[:]}"
+ if [[ $1 == -u ]]; then
+ _allowed_groups "$mycur"
+ else
+ local IFS=$'\n'
+ COMPREPLY=( $( compgen -g -- "$mycur" ) )
+ fi
+ COMPREPLY=( $( compgen -P "$prefix" -W "${COMPREPLY[@]}" ) )
elif [[ $cur = *:* ]]; then
# Completing group after 'user:gr<TAB>'.
# Reply with a list of unprefixed groups since readline with split on :
# and only replace the 'gr' part
- COMPREPLY=( $( compgen -g -- "${cur#*:}" ) )
+ local mycur="${cur#*:}"
+ if [[ $1 == -u ]]; then
+ _allowed_groups "$mycur"
+ else
+ local IFS=$'\n'
+ COMPREPLY=( $( compgen -g -- "$mycur" ) )
+ fi
else
# Completing a partial 'usernam<TAB>'.
#
# Don't suffix with a : because readline will escape it and add a
# slash. It's better to complete into 'chown username ' than 'chown
# username\:'.
- COMPREPLY=( $( compgen -u -- "$cur" ) )
+ if [[ $1 == -u ]]; then
+ _allowed_users "$cur"
+ else
+ local IFS=$'\n'
+ COMPREPLY=( $( compgen -u -- "$cur" ) )
+ fi
+ fi
+}
+
+_allowed_users()
+{
+ if _complete_as_root; then
+ local IFS=$'\n'
+ COMPREPLY=( $( compgen -u -- "${1:-$cur}" ) )
+ else
+ local IFS=$'\n '
+ COMPREPLY=( $( compgen -W \
+ "$( id -un 2>/dev/null || whoami 2>/dev/null )" -- "${1:-$cur}" ) )
+ fi
+}
+
+_allowed_groups()
+{
+ if _complete_as_root; then
+ local IFS=$'\n'
+ COMPREPLY=( $( compgen -g -- "$1" ) )
+ else
+ local IFS=$'\n '
+ COMPREPLY=( $( compgen -W \
+ "$( id -Gn 2>/dev/null || groups 2>/dev/null )" -- "$1" ) )
fi
}
diff --git a/completions/coreutils b/completions/coreutils
index 65809fb..75ebaac 100644
--- a/completions/coreutils
+++ b/completions/coreutils
@@ -41,7 +41,7 @@ _chown()
_count_args :
if [[ $args == 1 ]]; then
- _usergroup
+ _usergroup -u
else
_filedir
fi
@@ -84,8 +84,7 @@ _chgrp()
# first parameter on line or first since an option?
if [[ $COMP_CWORD -eq 1 && "$cur" != -* || "$prev" == -* ]]; then
- local IFS=$'\n'
- COMPREPLY=( $( compgen -g "$cur" 2>/dev/null ) )
+ _allowed_groups
else
_filedir || return 0
fi
diff --git a/completions/crontab b/completions/crontab
index cb440a6..cc279f8 100644
--- a/completions/crontab
+++ b/completions/crontab
@@ -9,7 +9,7 @@ _crontab()
case $prev in
-u)
- COMPREPLY=( $( compgen -u -- "$cur" ) )
+ _allowed_users
return 0
;;
esac
diff --git a/completions/qemu b/completions/qemu
index 1c7a8da..ca4e318 100644
--- a/completions/qemu
+++ b/completions/qemu
@@ -96,7 +96,7 @@ _qemu()
return 0
;;
-runas)
- COMPREPLY=( $( compgen -u -- "$cur" ) )
+ _allowed_users
return 0
;;
esac
diff --git a/completions/shadow b/completions/shadow
index ffe66e3..ea265a3 100644
--- a/completions/shadow
+++ b/completions/shadow
@@ -166,7 +166,7 @@ _passwd()
return 0
fi
- COMPREPLY=( $( compgen -u -- "$cur" ) )
+ _allowed_users
} &&
complete -F _passwd passwd
@@ -319,9 +319,11 @@ _newgrp()
COMPREPLY=()
if [[ "`_get_cword`" == "-" ]]; then
COMPREPLY=( - )
+ else
+ _allowed_groups
fi
} &&
-complete -g -F _newgrp newgrp
+complete -F _newgrp newgrp
have gpasswd &&
_gpasswd()
diff --git a/completions/strace b/completions/strace
index d27c014..0ee2737 100644
--- a/completions/strace
+++ b/completions/strace
@@ -78,7 +78,7 @@ _strace()
return 0
;;
-u)
- COMPREPLY=( $( compgen -u -- "$cur" ) )
+ _allowed_users
return 0
;;
esac
diff --git a/completions/util-linux b/completions/util-linux
index 8017ebb..f45ffd5 100644
--- a/completions/util-linux
+++ b/completions/util-linux
@@ -17,7 +17,7 @@ _renice()
curopt=${COMP_WORDS[COMP_CWORD-$i]}
case "$curopt" in
-u)
- COMPREPLY=( $( compgen -u -- "$cur" ) )
+ _allowed_users
;;
-g)
_pgids
diff --git a/test/lib/completions/chown.exp b/test/lib/completions/chown.exp
index a758cc1..a34cbe9 100644
--- a/test/lib/completions/chown.exp
+++ b/test/lib/completions/chown.exp
@@ -1,4 +1,6 @@
proc setup {} {
+ # fake root command to get all users/groups completed at least for now
+ assert_bash_exec {root_command=sudo}
save_env
}
@@ -43,7 +45,7 @@ if {!$failed_find_unique_completion} {
sync_after_int
foreach prefix {
- "funky\\ user:" "funky\\ user\\:" "funky.user:" "funky\\.user:" "fu\\ nky.user\\:"
+ "funky\\ user:" "funky.user:" "funky\\.user:" "fu\\ nky.user:"
"f\\ o\\ o\\.\\bar:" "foo\\_b\\ a\\.r\\ :"
} {
set test "Check preserve special chars in $prefix$partgroup<TAB>"
--
bash-completion
More information about the Bash-completion-commits
mailing list