added ssh completion

cleaned the code in some of the other functions
This commit is contained in:
ianmacd 2000-08-29 00:41:27 +00:00
parent a1c38f0261
commit 607c618f8e

View File

@ -2,7 +2,7 @@
# #
# <![CDATA[ # <![CDATA[
# #
# $Id: bash_completion,v 1.2 2000/08/11 23:20:41 ianmacd Exp $ # $Id: bash_completion,v 1.3 2000/08/29 02:41:27 ianmacd Exp $
# #
# Copyright (C) Ian Macdonald <ian@caliban.org> # Copyright (C) Ian Macdonald <ian@caliban.org>
# #
@ -20,8 +20,8 @@
# along with this program; if not, write to the Free Software Foundation, # along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# Turn on extended globbing # Turn on extended globbing and programmable completion
shopt -s extglob shopt -s extglob progcomp
# A lot of the following one-liners were taken directly from the # A lot of the following one-liners were taken directly from the
# completion examples provided with the bash 2.04 source distribution # completion examples provided with the bash 2.04 source distribution
@ -104,12 +104,11 @@ _chown ()
prev=${COMP_WORDS[COMP_CWORD-1]} prev=${COMP_WORDS[COMP_CWORD-1]}
# do not attempt completion if we're specifying an option # do not attempt completion if we're specifying an option
if [ "${cur:0:1}" = "-" ]; then return 0; fi if [ "$cur" == -* ]; then return 0; fi
# first parameter on line or first since an option? # first parameter on line or first since an option?
if [ $COMP_CWORD -eq 1 ] || [ "${prev:0:1}" = "-" ]; then if [ $COMP_CWORD -eq 1 ] || [[ "$prev" == -* ]]; then
case "$cur" in if [[ "$cur" == [a-zA-Z]*.* ]]; then
[a-zA-Z]*.*)
user=${cur%.*} user=${cur%.*}
group=${cur#*.} group=${cur#*.}
COMPREPLY=( $( awk 'BEGIN {FS=":"} \ COMPREPLY=( $( awk 'BEGIN {FS=":"} \
@ -118,13 +117,9 @@ _chown ()
for (( i=0; i < ${#COMPREPLY[@]}; i++ )); do for (( i=0; i < ${#COMPREPLY[@]}; i++ )); do
COMPREPLY[i]=$user.${COMPREPLY[i]} COMPREPLY[i]=$user.${COMPREPLY[i]}
done done
return 0 else
;;
*)
COMPREPLY=( $( compgen -u $cur -S '.' ) ) COMPREPLY=( $( compgen -u $cur -S '.' ) )
return 0 fi
;;
esac
else else
COMPREPLY=( $( compgen -f $cur ) ) COMPREPLY=( $( compgen -f $cur ) )
fi fi
@ -146,6 +141,7 @@ _umount ()
# could rewrite the cut | grep to be a sed command, but this is # could rewrite the cut | grep to be a sed command, but this is
# clearer and doesn't result in much overhead # clearer and doesn't result in much overhead
COMPREPLY=( $( mount | cut -d' ' -f 3 | grep ^$cur) ) COMPREPLY=( $( mount | cut -d' ' -f 3 | grep ^$cur) )
return 0 return 0
} }
complete -F _umount umount complete -F _umount umount
@ -178,18 +174,15 @@ _mount ()
COMPREPLY=() COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]} cur=${COMP_WORDS[COMP_CWORD]}
case "$cur" in if [[ "$cur" == *:* ]]; then
*:*)
COMPREPLY=( $( /usr/sbin/showmount -e --no-headers ${cur%%:*} |\ COMPREPLY=( $( /usr/sbin/showmount -e --no-headers ${cur%%:*} |\
grep ^${cur#*:} | awk '{print $1}')) grep ^${cur#*:} | awk '{print $1}'))
return 0 else
;;
*)
COMPREPLY=( $( awk '{if ($2 ~ /\//) print $2}' /etc/fstab | \ COMPREPLY=( $( awk '{if ($2 ~ /\//) print $2}' /etc/fstab | \
grep ^$cur )) grep ^$cur ))
fi
return 0 return 0
;;
esac
} }
complete -F _mount mount complete -F _mount mount
@ -203,7 +196,8 @@ _rmmod ()
COMPREPLY=() COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]} cur=${COMP_WORDS[COMP_CWORD]}
COMPREPLY=($( lsmod | awk '{if (NR != 1 && $1 ~ /^'$cur'/) print $1}')) COMPREPLY=($( /sbin/lsmod | \
awk '{if (NR != 1 && $1 ~ /^'$cur'/) print $1}'))
return 0 return 0
} }
complete -F _rmmod rmmod complete -F _rmmod rmmod
@ -220,6 +214,7 @@ _insmod ()
modpath=/lib/modules/`uname -r` modpath=/lib/modules/`uname -r`
COMPREPLY=($( ls -R $modpath | sed -ne 's/^\('$cur'.*\)\.o$/\1/p')) COMPREPLY=($( ls -R $modpath | sed -ne 's/^\('$cur'.*\)\.o$/\1/p'))
return 0 return 0
} }
complete -F _insmod insmod depmod modprobe modinfo complete -F _insmod insmod depmod modprobe modinfo
@ -248,8 +243,7 @@ _man ()
return 0 return 0
fi fi
case "$prev" in if [[ "$prev" == [0-9n] ]]; then
[0-9n])
# churn out a string of paths to search, with * appended to $cur # churn out a string of paths to search, with * appended to $cur
cmd=`awk '{if ($1 ~ /^MANPATH/) \ cmd=`awk '{if ($1 ~ /^MANPATH/) \
print $(NF)"/man'$prev'/'$cur'*"}' /etc/man.config | \ print $(NF)"/man'$prev'/'$cur'*"}' /etc/man.config | \
@ -263,9 +257,7 @@ _man ()
COMPREPLY=( ${COMPREPLY[@]##*/} ) COMPREPLY=( ${COMPREPLY[@]##*/} )
# strip suffix from man pages # strip suffix from man pages
COMPREPLY=( ${COMPREPLY[@]%%.*} ) COMPREPLY=( ${COMPREPLY[@]%%.*} )
return 0 else
;;
*)
cmd=`awk '{if ($1 ~ /^MANPATH/) \ cmd=`awk '{if ($1 ~ /^MANPATH/) \
print $(NF)"/man?/'$cur'*"}' /etc/man.config | sort -u` print $(NF)"/man?/'$cur'*"}' /etc/man.config | sort -u`
cmd=${cmd//\/\\*/\/} cmd=${cmd//\/\\*/\/}
@ -273,9 +265,9 @@ _man ()
COMPREPLY=( $( eval $cmd ) ) COMPREPLY=( $( eval $cmd ) )
COMPREPLY=( ${COMPREPLY[@]##*/} ) COMPREPLY=( ${COMPREPLY[@]##*/} )
COMPREPLY=( ${COMPREPLY[@]%%.*} ) COMPREPLY=( ${COMPREPLY[@]%%.*} )
fi
return 0 return 0
;;
esac
} }
complete -F _man man complete -F _man man
@ -293,16 +285,14 @@ _killall ()
cur=${COMP_WORDS[COMP_CWORD]} cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]} prev=${COMP_WORDS[COMP_CWORD-1]}
case "$prev" in if [[ "$prev" == -[A-Z0-9]* ]]; then
-[A-Z0-9]*)
# get a list of processes (the first sed evaluation # get a list of processes (the first sed evaluation
# takes care of swapped out processes, the second # takes care of swapped out processes, the second
# takes care of getting the basename of the process) # takes care of getting the basename of the process)
COMPREPLY=( $( ps ahx | awk '{if ($5 ~ /^'$cur'/) print $5}' | \ COMPREPLY=( $( ps ahx | awk '{if ($5 ~ /^'$cur'/) print $5}' | \
sed -e 's#[]\[]##g' -e 's#^.*/##' )) sed -e 's#[]\[]##g' -e 's#^.*/##' ))
return 0 return 0
;; fi
esac
# first parameter can be either a signal or a process # first parameter can be either a signal or a process
if [ $COMP_CWORD -eq 1 ]; then if [ $COMP_CWORD -eq 1 ]; then
@ -463,9 +453,12 @@ _ipsec ()
COMPREPLY=( $( compgen -W 'auto barf eroute klipsdebug look manual \ COMPREPLY=( $( compgen -W 'auto barf eroute klipsdebug look manual \
pluto ranbits rsasigkey setup showdefaults \ pluto ranbits rsasigkey setup showdefaults \
showhostkey spi spigrp tncfg whack' $cur )) showhostkey spi spigrp tncfg whack' $cur ))
return 0
} }
complete -F _ipsec ipsec complete -F _ipsec ipsec
# cvs(1) completion
#
_cvs() _cvs()
{ {
local cur prev local cur prev
@ -474,17 +467,21 @@ _cvs ()
cur=${COMP_WORDS[COMP_CWORD]} cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]} prev=${COMP_WORDS[COMP_CWORD-1]}
if [ $COMP_CWORD -eq 1 ] || [ "${prev:0:1}" = "-" ]; then if [ $COMP_CWORD -eq 1 ] || [[ "$prev" == -* ]]; then
COMPREPLY=( $( compgen -W 'add admin checkout commit diff \ COMPREPLY=( $( compgen -W 'add admin checkout commit diff \
export history import log rdiff release remove rtag status \ export history import log rdiff release remove rtag status \
tag update' $cur )) tag update' $cur ))
else else
COMPREPLY=( $( compgen -f $cur )) COMPREPLY=( $( compgen -f $cur ))
fi fi
return 0 return 0
} }
complete -F _cvs cvs complete -F _cvs cvs
# rpm(8) completion. This isn't exhaustive yet, but still provides
# quite a lot of functionality.
#
_rpm() _rpm()
{ {
dashify() dashify()
@ -526,7 +523,7 @@ _rpm()
COMPREPLY=( $( compgen -W 'help version initdb \ COMPREPLY=( $( compgen -W 'help version initdb \
checksig recompile rebuild resign addsign rebuilddb \ checksig recompile rebuild resign addsign rebuilddb \
showrc setperms setgids' ${cur_nodash#-} ) ) showrc setperms setgids' ${cur_nodash#-} ) )
dashify; dashify
return 0 return 0
;; ;;
*) *)
@ -538,6 +535,13 @@ _rpm()
esac esac
fi fi
case "$prev" in
--@(db|exclude)path|prefix|relocate|root)
COMPREPLY=( $( compgen -d $cur ) )
return 0
;;
esac
case "${COMP_WORDS[1]}" in case "${COMP_WORDS[1]}" in
-[iFU]*) -[iFU]*)
# complete on list of relevant options # complete on list of relevant options
@ -546,9 +550,9 @@ _rpm()
ignorearch dbpath prefix ignoreos nodeps allfiles ftpproxy \ ignorearch dbpath prefix ignoreos nodeps allfiles ftpproxy \
ftpport justdb httpproxy httpport noorder relocate badreloc \ ftpport justdb httpproxy httpport noorder relocate badreloc \
notriggers excludepath ignoresize oldpackage' ${cur_nodash#-} )) notriggers excludepath ignoresize oldpackage' ${cur_nodash#-} ))
dashify; dashify
# return if $cur is an option # return if $cur is an option
[ "${cur:0:1}" = "-" ] && return 0 [[ "$cur" == -* ]] && return 0
# add a list of RPMS to possible completions # add a list of RPMS to possible completions
COMPREPLY=( ${COMPREPLY[@]} $( compgen -G $cur\*.rpm ) ) COMPREPLY=( ${COMPREPLY[@]} $( compgen -G $cur\*.rpm ) )
return 0 return 0
@ -559,9 +563,9 @@ _rpm()
whatrequires requires triggeredby ftpport ftpproxy httpproxy \ whatrequires requires triggeredby ftpport ftpproxy httpproxy \
httpport provides triggers dump changelog dbpath filesbypkg' \ httpport provides triggers dump changelog dbpath filesbypkg' \
${cur_nodash#-} ) ) ${cur_nodash#-} ) )
dashify; dashify
# return if $cur is an option # return if $cur is an option
[ "${cur:0:1}" = "-" ] && return 0 [[ "$cur" == -* ]] && return 0
# add a list of RPMS to possible completions # add a list of RPMS to possible completions
COMPREPLY=( ${COMPREPLY[@]} $( compgen -G $cur\*.rpm ) ) COMPREPLY=( ${COMPREPLY[@]} $( compgen -G $cur\*.rpm ) )
return 0 return 0
@ -575,9 +579,9 @@ _rpm()
# complete on list of relevant options # complete on list of relevant options
COMPREPLY=( $( compgen -W 'allmatches noscripts notriggers \ COMPREPLY=( $( compgen -W 'allmatches noscripts notriggers \
nodeps test' ${cur_nodash#-} ) ) nodeps test' ${cur_nodash#-} ) )
dashify; dashify
# return if $cur is an option # return if $cur is an option
[ "${cur:0:1}" = "-" ] && return 0 [[ "$cur" == -* ]] && return 0
# complete on basename of installed RPMs # complete on basename of installed RPMs
COMPREPLY=( $( rpm -qa | \ COMPREPLY=( $( rpm -qa | \
sed -ne 's/^\('$cur'.*\)-[0-9a-zA-Z._]\+-[0-9.]\+$/\1/p' ) ) sed -ne 's/^\('$cur'.*\)-[0-9a-zA-Z._]\+-[0-9.]\+$/\1/p' ) )
@ -589,7 +593,7 @@ _rpm()
whatrequires requires triggeredby ftpport ftpproxy httpproxy \ whatrequires requires triggeredby ftpport ftpproxy httpproxy \
httpport provides triggers dump changelog dbpath specfile \ httpport provides triggers dump changelog dbpath specfile \
querybynumber last filesbypkg' ${cur_nodash#-} ) ) querybynumber last filesbypkg' ${cur_nodash#-} ) )
dashify; dashify
return 0 return 0
;; ;;
-q*) -q*)
@ -598,9 +602,9 @@ _rpm()
whatrequires requires triggeredby ftpport ftpproxy httpproxy \ whatrequires requires triggeredby ftpport ftpproxy httpproxy \
httpport provides triggers dump changelog dbpath specfile \ httpport provides triggers dump changelog dbpath specfile \
querybynumber last filesbypkg' ${cur_nodash#-} ) ) querybynumber last filesbypkg' ${cur_nodash#-} ) )
dashify; dashify
# return if $cur is an option # return if $cur is an option
[ "${cur:0:1}" = "-" ] && return 0 [[ "$cur" == -* ]] && return 0
# add a list of RPMS to possible completions # add a list of RPMS to possible completions
COMPREPLY=( ${COMPREPLY[@]} $( rpm -qa | \ COMPREPLY=( ${COMPREPLY[@]} $( rpm -qa | \
sed -ne 's/^\('$cur'.*\)-[0-9a-zA-Z._]\+-[0-9.]\+$/\1/p' ) ) sed -ne 's/^\('$cur'.*\)-[0-9a-zA-Z._]\+-[0-9.]\+$/\1/p' ) )
@ -610,9 +614,9 @@ _rpm()
# complete on list of relevant options # complete on list of relevant options
COMPREPLY=( $( compgen -W 'root rcfile dbpath nodeps nofiles \ COMPREPLY=( $( compgen -W 'root rcfile dbpath nodeps nofiles \
noscripts nomd5 nopgp' ${cur_nodash#-} ) ) noscripts nomd5 nopgp' ${cur_nodash#-} ) )
dashify; dashify
# return if $cur is an option # return if $cur is an option
[ "${cur:0:1}" = "-" ] && return 0 [[ "$cur" == -* ]] && return 0
# add a list of RPMS to possible completions # add a list of RPMS to possible completions
COMPREPLY=( ${COMPREPLY[@]} $( rpm -qa | \ COMPREPLY=( ${COMPREPLY[@]} $( rpm -qa | \
sed -ne 's/^\('$cur'.*\)-[0-9a-zA-Z._]\+-[0-9.]\+$/\1/p' ) ) sed -ne 's/^\('$cur'.*\)-[0-9a-zA-Z._]\+-[0-9.]\+$/\1/p' ) )
@ -623,9 +627,9 @@ _rpm()
COMPREPLY=( $( compgen -W 'short-circuit timecheck clean \ COMPREPLY=( $( compgen -W 'short-circuit timecheck clean \
rmsource test sign buildroot target buildarch buildos' \ rmsource test sign buildroot target buildarch buildos' \
${cur_nodash#-} ) ) ${cur_nodash#-} ) )
dashify; dashify
# return if $cur is an option # return if $cur is an option
[ "${cur:0:1}" = "-" ] && return 0 [[ "$cur" == -* ]] && return 0
# complete on .spec files # complete on .spec files
COMPREPLY=( $( compgen -G $cur\*.spec ) ) COMPREPLY=( $( compgen -G $cur\*.spec ) )
return 0 return 0
@ -635,9 +639,9 @@ _rpm()
COMPREPLY=( $( compgen -W 'short-circuit timecheck clean \ COMPREPLY=( $( compgen -W 'short-circuit timecheck clean \
rmsource test sign buildroot target buildarch buildos' \ rmsource test sign buildroot target buildarch buildos' \
${cur_nodash#-} ) ) ${cur_nodash#-} ) )
dashify; dashify
# return if $cur is an option # return if $cur is an option
[ "${cur:0:1}" = "-" ] && return 0 [[ "$cur" == -* ]] && return 0
# complete on .tar.gz files # complete on .tar.gz files
COMPREPLY=( $( compgen -G $cur\*.tar.gz ) ) COMPREPLY=( $( compgen -G $cur\*.tar.gz ) )
return 0 return 0
@ -659,9 +663,13 @@ _rpm()
return 0 return 0
;; ;;
esac esac
return 0
} }
complete -F _rpm rpm complete -F _rpm rpm
# chsh(1) completion
#
_chsh() _chsh()
{ {
local cur prev local cur prev
@ -675,9 +683,13 @@ _chsh()
else else
COMPREPLY=( $( compgen -u $cur ) ) COMPREPLY=( $( compgen -u $cur ) )
fi fi
return 0
} }
complete -F _chsh chsh complete -F _chsh chsh
# chkconfig(8) completion
#
_chkconfig() _chkconfig()
{ {
local cur prev local cur prev
@ -711,7 +723,48 @@ _chkconfig()
return 0 return 0
;; ;;
esac esac
return 0
} }
complete -F _chkconfig chkconfig complete -F _chkconfig chkconfig
# ssh(1) completion. Should be able to improve this with user@host notation,
# but the '@' seems to trigger some kind of bug in bash's completion.
#
_ssh()
{
local cur prev kh
kh=()
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]}
if [[ $prev == -*l ]]; then
COMPREPLY=( $( compgen -u $cur ) )
return 0
fi
[ -r /etc/known_hosts ] && kh[0]=/etc/known_hosts
[ -r ~/.ssh/known_hosts ] && kh[1]=~/.ssh/known_hosts
# If we have known_hosts files to use
if [ ${#kh[@]} -gt 0 ]; then
# If we're completing on a blank, only check fields containing
# a dot or an alpha character. Otherwise, we'll get the whole line
[ -z $cur ] && cur="[a-z.]" || cur="^$cur"
# FS needs to look for a comma separated list
COMPREPLY=( $( awk 'BEGIN {FS="[ ,]"} \
{for (i=1; i<=NR; ++i) \
{if ($i ~ /'$cur'/) \
{print $i}}}' ${kh[@]} ) )
else
# Just do normal hostname completion
COMPREPLY=( $( compgen -A hostname $cur ) )
fi
return 0
}
complete -F _ssh ssh
# ]]> # ]]>