2009-01-14 22:17:14 +02:00
# -*- mode: shell-script; sh-basic-offset: 8; indent-tabs-mode: t -*-
2009-01-17 11:38:33 +02:00
# ex: ts=8 sw=8 noet filetype=sh
2009-01-14 22:17:14 +02:00
#
2006-02-24 11:04:52 +00:00
# bash_completion - programmable completion functions for bash 3.x
# (backwards compatible with bash 2.05b)
2000-08-08 22:17:29 +00:00
#
2008-06-21 22:37:16 +02:00
# Copyright © 2006-2008, Ian Macdonald <ian@caliban.org>
2009-01-29 21:34:57 +01:00
# © 2009, Bash Completion Maintainers
# <bash-completion-devel@lists.alioth.debian.org>
2000-08-08 22:17:29 +00:00
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
2003-10-20 19:05:20 +00:00
#
# The latest version of this software can be obtained here:
#
2008-06-21 22:37:16 +02:00
# http://bash-completion.alioth.debian.org/
2003-10-20 19:05:20 +00:00
#
2009-02-17 09:23:24 +01:00
# RELEASE: 1.x
2002-01-05 20:04:01 +00:00
2008-06-23 12:03:06 +02:00
if [ [ $- = = *v* ] ] ; then
BASH_COMPLETION_ORIGINAL_V_VALUE = "-v"
else
BASH_COMPLETION_ORIGINAL_V_VALUE = "+v"
fi
if [ [ -n $BASH_COMPLETION_DEBUG ] ] ; then
set -v
else
set +v
fi
2002-01-29 20:33:49 +00:00
2005-07-07 23:47:14 +00:00
# Alter the following to reflect the location of this file.
#
2008-09-27 12:28:09 +02:00
[ -n " $BASH_COMPLETION " ] || BASH_COMPLETION = /etc/bash_completion
[ -n " $BASH_COMPLETION_DIR " ] || BASH_COMPLETION_DIR = /etc/bash_completion.d
2009-02-20 22:58:01 +01:00
[ -n " $BASH_COMPLETION_COMPAT_DIR " ] || BASH_COMPLETION_COMPAT_DIR = /etc/bash_completion.d
readonly BASH_COMPLETION BASH_COMPLETION_DIR BASH_COMPLETION_COMPAT_DIR
2002-01-29 20:33:49 +00:00
2001-07-08 23:14:13 +00:00
# Set a couple of useful vars
#
2002-04-22 05:59:08 +00:00
UNAME = $( uname -s )
2004-03-30 19:02:46 +00:00
# strip OS type and version under Cygwin (e.g. CYGWIN_NT-5.1 => Cygwin)
UNAME = ${ UNAME /CYGWIN_*/Cygwin }
2003-10-07 06:02:00 +00:00
2009-05-20 22:08:53 +02:00
case ${ UNAME } in
Linux| GNU| GNU/*) USERLAND = GNU ; ;
*) USERLAND = ${ UNAME } ; ;
esac
2003-10-07 06:02:00 +00:00
# features supported by bash 2.05 and higher
if [ ${ BASH_VERSINFO [0] } -eq 2 ] && [ [ ${ BASH_VERSINFO [1] } > 04 ] ] ||
[ ${ BASH_VERSINFO [0] } -gt 2 ] ; then
2005-07-08 02:20:58 +00:00
declare -r bash205 = $BASH_VERSION 2>/dev/null || :
2002-04-03 17:19:30 +00:00
default = "-o default"
dirnames = "-o dirnames"
filenames = "-o filenames"
fi
2003-10-07 06:02:00 +00:00
# features supported by bash 2.05b and higher
if [ ${ BASH_VERSINFO [0] } -eq 2 ] && [ [ ${ BASH_VERSINFO [1] } = "05b" ] ] ||
[ ${ BASH_VERSINFO [0] } -gt 2 ] ; then
2005-07-08 02:20:58 +00:00
declare -r bash205b = $BASH_VERSION 2>/dev/null || :
2003-10-07 06:02:00 +00:00
nospace = "-o nospace"
fi
# features supported by bash 3.0 and higher
if [ ${ BASH_VERSINFO [0] } -gt 2 ] ; then
2005-07-08 02:20:58 +00:00
declare -r bash3 = $BASH_VERSION 2>/dev/null || :
2003-10-07 06:02:00 +00:00
bashdefault = "-o bashdefault"
plusdirs = "-o plusdirs"
fi
2009-04-07 20:58:35 +03:00
# features supported by bash 4.0 and higher
if [ ${ BASH_VERSINFO [0] } -gt 3 ] ; then
declare -r bash4 = $BASH_VERSION 2>/dev/null || :
fi
2001-07-08 23:14:13 +00:00
2000-08-29 00:41:27 +00:00
# Turn on extended globbing and programmable completion
shopt -s extglob progcomp
2000-08-08 22:17:29 +00:00
# A lot of the following one-liners were taken directly from the
# completion examples provided with the bash 2.04 source distribution
2000-10-19 15:25:36 +00:00
# Make directory commands see only directories
mkdir and rmdir now bound to _longopt(), default to dirnames
a2ps, autoconf, automake, bc, gprof, ld, nm, objcopy, objdump, readelf, strip,
bison, cpio, diff, patch, enscript, cp, df, dir, du, ln, ls, mkfifo, mknod,
mv, rm, touch, vdir, xargs, awk, gperf, grep, gpg, grub, indent, less, m4,
sed, shar, date, env, seq, su, tee, uname, who, texindex, cat, csplit, cut,
expand, fmt, fold, head, md5sum, nl, od, paste, pr, ptx, sha1sum, sort,
split, tac, tail, tr, unexpand, uniq, wc, units and rsync now all have
GNU long option completion from _longopt()
gpc completion added into _gcc()
cat, less, more, ln and strip no longer bound to _filedir()
2002-02-18 08:26:34 +00:00
complete -d pushd
2000-10-19 15:25:36 +00:00
2002-03-02 00:13:23 +00:00
# The following section lists completions that are redefined later
# Do NOT break these over multiple lines.
#
2001-12-20 07:52:12 +00:00
# START exclude -- do NOT remove this line
2008-05-11 16:47:01 +02:00
complete -f -X '!*.?(t)bz?(2)' bunzip2
# TODO: see #455510
#complete -f -X '!*.?(t)bz?(2)' bzcat bzcmp bzdiff bzegrep bzfgrep bzgrep
2008-06-21 23:55:32 +02:00
complete -f -X '!*.*' bzcat bzcmp bzdiff bzegrep bzfgrep bzgrep
2008-09-06 16:53:04 +02:00
complete -f -X '!*.@(zip|ZIP|jar|JAR|exe|EXE|pk3|war|wsz|ear|zargo|xpi|sxw|ott|od[fgpst]|epub)' unzip zipinfo
2002-02-06 04:02:46 +00:00
complete -f -X '*.Z' compress znew
2008-05-11 16:47:01 +02:00
complete -f -X '!*.@(Z|gz|tgz|Gz|dz)' gunzip
# TODO: see #455510
#complete -f -X '!*.@(Z|gz|tgz|Gz|dz)' zcmp zdiff zcat zegrep zfgrep zgrep zless zmore
2008-06-21 23:55:32 +02:00
complete -f -X '!*.*' zcmp zdiff zcat zegrep zfgrep zgrep zless zmore
2001-12-20 07:52:12 +00:00
complete -f -X '!*.Z' uncompress
2009-07-20 20:00:33 +03:00
complete -f -X '!*.lzma' lzcat unlzma
2009-07-30 18:35:58 +03:00
complete -f -X '!*.@(xz|lzma)' unxz xzcat
2008-05-11 18:35:16 +02:00
complete -f -X '!*.@(gif|jp?(e)g|miff|tif?(f)|pn[gm]|p[bgp]m|bmp|xpm|ico|xwd|tga|pcx|GIF|JP?(E)G|MIFF|TIF?(F)|PN[GM]|P[BGP]M|BMP|XPM|ICO|XWD|TGA|PCX)' ee
2002-07-12 07:21:06 +00:00
complete -f -X '!*.@(gif|jp?(e)g|tif?(f)|png|p[bgp]m|bmp|x[bp]m|rle|rgb|pcx|fits|pm|GIF|JPG|JP?(E)G|TIF?(F)|PNG|P[BGP]M|BMP|X[BP]M|RLE|RGB|PCX|FITS|PM)' xv qiv
2004-09-30 07:48:57 +00:00
complete -f -X '!*.@(@(?(e)ps|?(E)PS|pdf|PDF)?(.gz|.GZ|.bz2|.BZ2|.Z))' gv ggv kghostview
2002-02-06 16:05:12 +00:00
complete -f -X '!*.@(dvi|DVI)?(.@(gz|Z|bz2))' xdvi
2008-05-01 22:22:11 +02:00
complete -f -X '!*.@(dvi|DVI)?(.@(gz|Z|bz2))' kdvi
2008-05-01 22:59:32 +02:00
complete -f -X '!*.@(dvi|DVI)' dvips dviselect dvitype dvipdf advi dvipdfm dvipdfmx
2008-05-01 22:22:11 +02:00
complete -f -X '!*.@(pdf|PDF)' acroread gpdf xpdf
complete -f -X '!*.@(?(e)ps|?(E)PS|pdf|PDF)' kpdf
2009-04-14 21:40:57 +02:00
complete -f -X '!*.@(@(?(e)ps|?(E)PS|pdf|PDF|dvi|DVI)?(.gz|.GZ|.bz2|.BZ2)|cb[rz]|CB[RZ]|djv?(u)|DJV?(U)|dvi|DVI|gif|jp?(e)g|miff|tif?(f)|pn[gm]|p[bgp]m|bmp|xpm|ico|xwd|tga|pcx|GIF|JP?(E)G|MIFF|TIF?(F)|PN[GM]|P[BGP]M|BMP|XPM|ICO|XWD|TGA|PCX)' evince okular
2009-02-26 23:20:39 +02:00
complete -f -X '!*.@(?(e)ps|?(E)PS|pdf|PDF)' ps2pdf ps2pdf12 ps2pdf13 ps2pdf14 ps2pdfwr
2002-12-04 04:02:12 +00:00
complete -f -X '!*.texi*' makeinfo texi2html
complete -f -X '!*.@(?(la)tex|?(LA)TEX|texi|TEXI|dtx|DTX|ins|INS)' tex latex slitex jadetex pdfjadetex pdftex pdflatex texi2dvi
2005-07-19 04:28:48 +00:00
complete -f -X '!*.@(mp3|MP3)' mpg123 mpg321 madplay
2009-08-05 23:47:15 +03:00
complete -f -X '!*@(.@(mp?(e)g|MP?(E)G|wma|avi|AVI|asf|vob|VOB|bin|dat|divx|DIVX|vcd|ps|pes|fli|flv|FLV|viv|rm|ram|yuv|mov|MOV|qt|QT|wmv|mp[234]|MP[234]|m4[pv]|M4[PV]|mkv|MKV|og[gmv]|OG[GMV]|wav|WAV|asx|ASX|mng|MNG|srt|m[eo]d|M[EO]D|s[3t]m|S[3T]M|it|IT|xm|XM)|+([0-9]).@(vdr|VDR))' xine aaxine fbxine kaffeine
2002-08-05 05:39:37 +00:00
complete -f -X '!*.@(avi|asf|wmv)' aviplay
2004-10-24 17:01:56 +00:00
complete -f -X '!*.@(rm?(j)|ra?(m)|smi?(l))' realplay
2003-07-20 07:52:57 +00:00
complete -f -X '!*.@(mpg|mpeg|avi|mov|qt)' xanim
2003-12-24 15:11:43 +00:00
complete -f -X '!*.@(ogg|OGG|m3u|flac|spx)' ogg123
2002-08-12 14:04:58 +00:00
complete -f -X '!*.@(mp3|MP3|ogg|OGG|pls|m3u)' gqmpeg freeamp
2002-02-10 20:32:39 +00:00
complete -f -X '!*.fig' xfig
2008-05-10 18:04:06 +02:00
complete -f -X '!*.@(mid?(i)|MID?(I)|cmf|CMF)' playmidi
2009-04-14 14:30:04 +03:00
complete -f -X '!*.@(mid?(i)|MID?(I)|rmi|RMI|rcp|RCP|[gr]36|[GR]36|g18|G18|mod|MOD|xm|XM|it|IT|x3m|X3M|s[3t]m|S[3T]M|kar|KAR)' timidity
2009-04-14 14:26:22 +03:00
complete -f -X '!*.@(m[eo]d|M[EO]D|s[3t]m|S[3T]M|xm|XM|it|IT)' modplugplay
2008-06-22 19:57:44 +02:00
complete -f -X '*.@(o|so|so.!(conf)|a|rpm|gif|GIF|jp?(e)g|JP?(E)G|mp3|MP3|mp?(e)g|MPG|avi|AVI|asf|ASF|ogg|OGG|class|CLASS)' vi vim gvim rvim view rview rgvim rgview gview
2004-07-03 22:03:19 +00:00
complete -f -X '*.@(o|so|so.!(conf)|a|rpm|gif|GIF|jp?(e)g|JP?(E)G|mp3|MP3|mp?(e)g|MPG|avi|AVI|asf|ASF|ogg|OGG|class|CLASS)' emacs
2006-02-24 10:51:39 +00:00
complete -f -X '!*.@(exe|EXE|com|COM|scr|SCR|exe.so)' wine
2002-05-21 03:35:40 +00:00
complete -f -X '!*.@(zip|ZIP|z|Z|gz|GZ|tgz|TGZ)' bzme
2005-01-25 00:41:25 +00:00
complete -f -X '!*.@(?([xX]|[sS])[hH][tT][mM]?([lL]))' netscape mozilla lynx opera galeon curl dillo elinks amaya
2009-07-02 22:54:05 +03:00
complete -f -X '!*.@(sxw|stw|sxg|sgl|doc?([mx])|dot?([mx])|rtf|txt|htm|html|odt|ott|odm)' oowriter
complete -f -X '!*.@(sxi|sti|pps?(x)|ppt?([mx])|pot?([mx])|odp|otp)' ooimpress
2009-07-02 22:57:01 +03:00
complete -f -X '!*.@(sxc|stc|xls?([bmx])|xlw|xlt?([mx])|[ct]sv|ods|ots)' oocalc
2005-07-19 15:38:39 +00:00
complete -f -X '!*.@(sxd|std|sda|sdd|odg|otg)' oodraw
complete -f -X '!*.@(sxm|smf|mml|odf)' oomath
complete -f -X '!*.odb' oobase
2005-07-11 22:24:19 +00:00
complete -f -X '!*.rpm' rpm2cpio
2009-01-12 00:35:56 +02:00
complete -f -X '!*.sqlite' sqlite3
2009-02-03 20:32:07 +01:00
complete -f -X '!*.aux' bibtex
2009-07-13 18:28:42 +03:00
complete -f -X '!*.po' poedit gtranslator kbabel lokalize
2001-12-20 07:52:12 +00:00
# FINISH exclude -- do not remove this line
2000-08-08 22:17:29 +00:00
2002-03-02 00:13:23 +00:00
# start of section containing compspecs that can be handled within bash
2000-08-08 22:17:29 +00:00
# user commands see only users
2009-05-19 22:54:48 +03:00
complete -u su passwd write chfn groups slay w sux
2001-11-29 00:37:54 +00:00
2000-08-08 22:17:29 +00:00
# bg completes with stopped jobs
2008-05-11 14:32:21 +02:00
complete -A stopped -P '"%' -S '"' bg
2000-08-08 22:17:29 +00:00
# other job commands
2008-05-11 14:32:21 +02:00
complete -j -P '"%' -S '"' fg jobs disown
2000-08-08 22:17:29 +00:00
2002-03-27 06:32:19 +00:00
# readonly and unset complete with shell variables
2002-02-11 01:49:26 +00:00
complete -v readonly unset
2000-08-08 22:17:29 +00:00
# set completes with set options
complete -A setopt set
# shopt completes with shopt options
complete -A shopt shopt
# helptopics
complete -A helptopic help
# unalias completes with aliases
complete -a unalias
# bind completes with readline bindings (make this more intelligent)
complete -A binding bind
2002-04-03 16:56:55 +00:00
# type and which complete on commands
complete -c command type which
2002-02-11 22:28:47 +00:00
2004-05-11 16:06:08 +00:00
# builtin completes on builtins
complete -b builtin
2002-03-02 00:13:23 +00:00
# start of section containing completion functions called by other functions
2000-08-08 22:17:29 +00:00
2004-10-13 18:58:48 +00:00
# This function checks whether we have a given program on the system.
2002-03-02 00:13:23 +00:00
# No need for bulky functions in memory if we don't.
2001-07-08 23:14:13 +00:00
#
have( )
{
2001-07-09 00:55:01 +00:00
unset -v have
2003-04-20 20:15:38 +00:00
PATH = $PATH :/sbin:/usr/sbin:/usr/local/sbin type $1 & >/dev/null &&
have = "yes"
2001-07-08 23:14:13 +00:00
}
2002-06-17 14:38:37 +00:00
# use GNU sed if we have it, since its extensions are still used in our code
#
2009-06-05 20:53:51 +02:00
[ $USERLAND != GNU ] && have gsed && alias sed = gsed
2002-06-17 14:38:37 +00:00
2004-07-03 22:56:08 +00:00
# This function checks whether a given readline variable
# is `on'.
#
2008-05-06 20:20:14 +01:00
_rl_enabled( )
2004-07-03 22:56:08 +00:00
{
2006-02-23 23:12:41 +00:00
[ [ " $( bind -v ) " = *$1 +( [ [ :space:] ] ) on* ] ]
2004-07-03 22:56:08 +00:00
}
2008-06-23 11:03:01 +02:00
# This function shell-quotes the argument
quote( )
{
echo \' ${ 1 // \' / \' \\ \' \' } \' #'# Help vim syntax highlighting
}
2009-07-25 13:38:42 +03:00
# This function quotes the argument in a way so that readline dequoting
2008-06-23 11:03:01 +02:00
# results in the original argument
quote_readline( )
{
2009-04-07 20:58:35 +03:00
if [ -n " $bash4 " ] ; then
2009-04-02 11:16:46 -04:00
# This function isn't really necessary on bash 4
# See: http://lists.gnu.org/archive/html/bug-bash/2009-03/msg00155.html
echo " ${ 1 } "
return
fi
2008-06-23 11:03:01 +02:00
local t = " ${ 1 // \\ / \\ \\ } "
echo \' ${ t // \' / \' \\ \' \' } \' #'# Help vim syntax highlighting
}
# This function shell-dequotes the argument
dequote( )
{
eval echo " $1 "
}
2008-05-01 22:22:11 +02:00
# Get the word to complete
# This is nicer than ${COMP_WORDS[$COMP_CWORD]}, since it handles cases
# where the user is completing in the middle of a word.
# (For example, if the line is "ls foobar",
# and the cursor is here --------> ^
# it will complete just "foo", not "foobar", which is what the user wants.)
2008-05-01 22:59:32 +02:00
#
#
# Accepts an optional parameter indicating which characters out of
# $COMP_WORDBREAKS should NOT be considered word breaks. This is useful
# for things like scp where we want to return host:path and not only path.
2008-05-01 22:22:11 +02:00
_get_cword( )
{
2008-06-23 11:09:32 +02:00
if [ [ " ${# COMP_WORDS [COMP_CWORD] } " -eq 0 ] ] || [ [ " $COMP_POINT " = = " ${# COMP_LINE } " ] ] ; then
2008-09-07 10:43:23 +02:00
printf "%s" " ${ COMP_WORDS [COMP_CWORD] } "
2008-06-23 11:09:32 +02:00
else
local i
local cur = " $COMP_LINE "
local index = " $COMP_POINT "
for ( ( i = 0; i <= COMP_CWORD; ++i ) ) ; do
2008-06-23 14:36:03 +02:00
while [ [ " ${# cur } " -ge ${# COMP_WORDS [i] } ] ] && [ [ " ${ cur : 0 : ${# COMP_WORDS [i] } } " != " ${ COMP_WORDS [i] } " ] ] ; do
2008-06-23 11:09:32 +02:00
cur = " ${ cur : 1 } "
index = " $(( index - 1 )) "
done
if [ [ " $i " -lt " $COMP_CWORD " ] ] ; then
local old_size = " ${# cur } "
cur = " ${ cur # ${ COMP_WORDS [i] } } "
local new_size = " ${# cur } "
index = " $(( index - old_size + new_size )) "
fi
2008-05-01 22:59:32 +02:00
done
2009-07-25 13:38:42 +03:00
2008-06-23 11:09:32 +02:00
if [ [ " ${ COMP_WORDS [COMP_CWORD] : 0 : ${# cur } } " != " $cur " ] ] ; then
2009-07-25 13:38:42 +03:00
# We messed up! At least return the whole word so things
2008-06-23 11:09:32 +02:00
# keep working
2008-09-07 10:43:23 +02:00
printf "%s" " ${ COMP_WORDS [COMP_CWORD] } "
2008-06-23 11:09:32 +02:00
else
2008-09-07 10:43:23 +02:00
printf "%s" " ${ cur : 0 : $index } "
2008-05-01 22:59:32 +02:00
fi
2008-06-23 11:09:32 +02:00
fi
2008-05-01 22:22:11 +02:00
}
2004-07-03 22:56:08 +00:00
2002-03-02 00:13:23 +00:00
# This function performs file and directory completion. It's better than
2002-05-18 17:05:08 +00:00
# simply using 'compgen -f', because it honours spaces in filenames.
# If passed -d, it completes only on directories. If passed anything else,
2002-05-19 14:56:19 +00:00
# it's assumed to be a file glob to complete on.
2002-03-02 00:13:23 +00:00
#
_filedir( )
{
2008-06-23 11:13:08 +02:00
local IFS = $'\t\n' xspec
2002-03-02 00:13:23 +00:00
_expand || return 0
2009-03-30 23:53:23 +03:00
local -a toks
local tmp
2009-07-25 13:38:42 +03:00
2009-03-01 16:59:38 +01:00
# TODO: I've removed a "[ -n $tmp ] &&" before `echo $tmp',
# and everything works again. If this bug
# suddenly appears again (i.e. "cd /b<TAB>"
# becomes "cd /"), remember to check for
# other similar conditionals (here and
# _filedir_xspec()). --David
2009-07-25 13:38:42 +03:00
# NOTE: The comment above has been moved outside of the subshell below,
2009-03-01 16:59:38 +01:00
# because quotes-in-comments-in-a-subshell cause errors on
2009-07-25 13:38:42 +03:00
# bash-3.1. See also:
2009-03-01 16:59:38 +01:00
# http://www.mail-archive.com/bug-bash@gnu.org/msg01667.html
2008-11-01 09:49:57 +01:00
toks = ( ${ toks [@]- } $(
2008-10-24 19:17:23 +02:00
compgen -d -- " $( quote_readline " $cur " ) " | {
while read -r tmp; do
2008-10-25 14:33:12 +02:00
echo $tmp
2008-10-24 19:17:23 +02:00
done
}
) )
2009-07-25 13:38:42 +03:00
2008-06-23 11:13:08 +02:00
if [ [ " $1 " != -d ] ] ; then
xspec = ${ 1 : + " !*. $1 " }
2008-11-01 09:49:57 +01:00
toks = ( ${ toks [@]- } $(
2008-10-24 19:17:23 +02:00
compgen -f -X " $xspec " -- " $( quote_readline " $cur " ) " | {
while read -r tmp; do
2008-10-25 14:33:12 +02:00
[ -n $tmp ] && echo $tmp
2008-10-24 19:17:23 +02:00
done
}
) )
2002-03-02 00:13:23 +00:00
fi
2002-05-18 17:05:08 +00:00
2008-06-23 11:13:08 +02:00
COMPREPLY = ( " ${ COMPREPLY [@] } " " ${ toks [@] } " )
2002-03-02 00:13:23 +00:00
}
2009-04-14 23:16:47 +03:00
# This function splits $cur=--foo=bar into $prev=--foo, $cur=bar, making it
# easier to support both "--foo bar" and "--foo=bar" style completions.
# Returns 0 if current option was split, 1 otherwise.
#
_split_longopt( )
{
if [ [ " $cur " = = --?*= * ] ] ; then
# Cut also backslash before '=' in case it ended up there
# for some reason.
prev = " ${ cur %%?( \\ )=* } "
cur = " ${ cur #*= } "
return 0
fi
return 1
}
2009-01-17 00:31:39 +01:00
# This function tries to parse the output of $command --help
#
_parse_help( ) {
local cmd
cmd = $1
$cmd --help | \
grep -- "^[[:space:]]*-" | \
tr "," " " | \
awk '{print $1; if ($2 ~ /-.*/) { print $2 } }' | \
sed -e "s:=.*::g"
}
2002-03-02 00:13:23 +00:00
# This function completes on signal names
#
_signals( )
{
local i
# standard signal completion is rather braindead, so we need
# to hack around to get what we want here, which is to
# complete on a dash, followed by the signal name minus
# the SIG prefix
COMPREPLY = ( $( compgen -A signal SIG${ cur #- } ) )
for ( ( i = 0; i < ${# COMPREPLY [@] } ; i++ ) ) ; do
COMPREPLY[ i] = -${ COMPREPLY [i]#SIG }
done
}
2005-01-10 22:39:56 +00:00
# This function completes on configured network interfaces
2003-08-03 00:57:49 +00:00
#
2005-01-10 22:39:56 +00:00
_configured_interfaces( )
2003-08-03 00:57:49 +00:00
{
if [ -f /etc/debian_version ] ; then
# Debian system
COMPREPLY = ( $( sed -ne 's|^iface \([^ ]\+\).*$|\1|p' \
/etc/network/interfaces ) )
2004-05-24 07:12:31 +00:00
elif [ -f /etc/SuSE-release ] ; then
# SuSE system
2005-01-13 01:22:45 +00:00
COMPREPLY = ( $( command ls \
2005-01-10 22:39:56 +00:00
/etc/sysconfig/network/ifcfg-* | \
sed -ne 's|.*ifcfg-\(' $cur '.*\)|\1|p' ) )
2006-02-25 10:37:32 +00:00
elif [ -f /etc/pld-release ] ; then
# PLD Linux
COMPREPLY = ( $( command ls -B \
/etc/sysconfig/interfaces | \
sed -ne 's|.*ifcfg-\(' $cur '.*\)|\1|p' ) )
2003-08-03 00:57:49 +00:00
else
# Assume Red Hat
COMPREPLY = ( $( command ls \
/etc/sysconfig/network-scripts/ifcfg-* | \
sed -ne 's|.*ifcfg-\(' $cur '.*\)|\1|p' ) )
fi
}
2009-01-25 23:39:09 +02:00
# This function completes on available kernels
2009-01-19 21:30:58 +01:00
#
_kernel_versions( )
{
2009-04-14 22:26:14 +03:00
COMPREPLY = ( $( compgen -W '$( command ls /lib/modules )' -- $cur ) )
2009-01-19 21:30:58 +01:00
}
2005-01-10 22:39:56 +00:00
# This function completes on all available network interfaces
# -a: restrict to active interfaces only
# -w: restrict to wireless interfaces only
#
_available_interfaces( )
{
local cmd
if [ " ${ 1 :- } " = -w ] ; then
cmd = "iwconfig"
elif [ " ${ 1 :- } " = -a ] ; then
cmd = "ifconfig"
else
cmd = "ifconfig -a"
fi
COMPREPLY = ( $( eval $cmd 2>/dev/null | \
sed -ne 's|^\(' $cur '[^[:space:][:punct:]]\{1,\}\).*$|\1|p' ) )
}
2002-03-02 00:13:23 +00:00
# This function expands tildes in pathnames
#
_expand( )
{
2008-06-23 11:14:11 +02:00
# FIXME: Why was this here?
#[ "$cur" != "${cur%\\}" ] && cur="$cur\\"
2002-06-24 19:40:04 +00:00
2009-02-06 23:26:34 +02:00
# Expand ~username type directory specifications. We want to expand
# ~foo/... to /home/foo/... to avoid problems when $cur starting with
# a tilde is fed to commands and ending up quoted instead of expanded.
2002-06-24 19:40:04 +00:00
if [ [ " $cur " = = \~ */* ] ] ; then
2009-02-06 23:26:34 +02:00
eval cur = $cur
2002-06-24 19:40:04 +00:00
elif [ [ " $cur " = = \~ * ] ] ; then
2002-03-02 00:13:23 +00:00
cur = ${ cur # \~ }
COMPREPLY = ( $( compgen -P '~' -u $cur ) )
2009-02-06 23:57:05 +02:00
[ ${# COMPREPLY [@] } -eq 1 ] && eval COMPREPLY[ 0] = ${ COMPREPLY [0] }
2002-03-02 00:13:23 +00:00
return ${# COMPREPLY [@] }
fi
}
2005-07-07 21:09:39 +00:00
# This function completes on process IDs.
# AIX and Solaris ps prefers X/Open syntax.
2004-06-01 06:08:18 +00:00
[ $UNAME = SunOS -o $UNAME = AIX ] &&
_pids( )
{
2005-01-13 01:22:45 +00:00
COMPREPLY = ( $( compgen -W '$( command ps -efo pid | sed 1d )' -- $cur ) )
2005-07-07 21:09:39 +00:00
} ||
_pids( )
2002-04-02 06:17:35 +00:00
{
2005-07-07 21:09:39 +00:00
COMPREPLY = ( $( compgen -W '$( command ps axo pid | sed 1d )' -- $cur ) )
2002-04-02 06:17:35 +00:00
}
2005-07-07 21:09:39 +00:00
# This function completes on process group IDs.
# AIX and SunOS prefer X/Open, all else should be BSD.
2004-06-01 06:08:18 +00:00
[ $UNAME = SunOS -o $UNAME = AIX ] &&
_pgids( )
{
2005-01-13 01:22:45 +00:00
COMPREPLY = ( $( compgen -W '$( command ps -efo pgid | sed 1d )' -- $cur ) )
2005-07-07 21:09:39 +00:00
} ||
_pgids( )
{
COMPREPLY = ( $( compgen -W '$( command ps axo pgid | sed 1d )' -- $cur ) )
2004-06-01 06:08:18 +00:00
}
2009-02-15 18:47:34 +02:00
# This function completes on process names.
# AIX and SunOS prefer X/Open, all else should be BSD.
[ $UNAME = SunOS -o $UNAME = AIX ] &&
_pnames( )
{
COMPREPLY = ( $( compgen -W ' $( command ps -efo comm | \
sed -e 1d -e "s:.*/::" -e "s/^-//" \
-e " s/^<defunct> $// " ) ' \
-- $cur ) )
} ||
_pnames( )
{
# FIXME: completes "[kblockd/0]" to "0". Previously it was completed
# to "kblockd" which isn't correct either. "kblockd/0" would be
# arguably most correct, but killall from psmisc 22 treats arguments
# containing "/" specially unless -r is given so that wouldn't quite
# work either. Perhaps it'd be best to not complete these to anything
# for now.
2009-02-15 18:51:09 +02:00
# Not using "ps axo comm" because under some Linux kernels, it
# truncates command names (see e.g. http://bugs.debian.org/497540#19)
2009-02-15 18:47:34 +02:00
COMPREPLY = ( $( compgen -W ' $( command ps axo command | \
sed -e " 1d; s/ .*//; s:.*/::; s/: $//; " \
-e " s/^[[(-]//; s/[])] $// " \
-e " s/^<defunct> $// " ) ' \
-- $cur ) )
}
2003-08-04 03:15:28 +00:00
# This function completes on user IDs
#
_uids( )
{
2004-07-03 22:31:33 +00:00
if type getent & >/dev/null; then
2009-04-14 22:26:14 +03:00
COMPREPLY = ( $( compgen -W '$( getent passwd | cut -d: -f3 )' -- $cur ) )
2004-07-03 22:31:33 +00:00
elif type perl & >/dev/null; then
2003-08-04 22:32:25 +00:00
COMPREPLY = ( $( compgen -W '$( perl -e ' "'" 'while (($uid) = (getpwent)[2]) { print $uid . "\n" }' "'" ' )' -- $cur ) )
else
# make do with /etc/passwd
2009-04-14 22:26:14 +03:00
COMPREPLY = ( $( compgen -W '$( cut -d: -f3 /etc/passwd )' -- $cur ) )
2003-08-04 22:32:25 +00:00
fi
2003-08-04 03:15:28 +00:00
}
# This function completes on group IDs
#
_gids( )
{
2004-07-03 22:31:33 +00:00
if type getent & >/dev/null; then
COMPREPLY = ( $( getent group | \
awk -F: '{if ($3 ~ /^' $cur '/) print $3}' ) )
elif type perl & >/dev/null; then
2003-08-04 22:32:25 +00:00
COMPREPLY = ( $( compgen -W '$( perl -e ' "'" 'while (($gid) = (getgrent)[2]) { print $gid . "\n" }' "'" ' )' -- $cur ) )
else
# make do with /etc/group
COMPREPLY = ( $( awk 'BEGIN {FS=":"} {if ($3 ~ /^' $cur '/) print $3}' \
/etc/group ) )
fi
2003-08-04 03:15:28 +00:00
}
2004-07-19 06:45:15 +00:00
# This function completes on services
2004-05-24 07:50:30 +00:00
#
_services( )
{
2004-07-03 22:26:29 +00:00
local sysvdir famdir
2004-05-24 07:50:30 +00:00
[ -d /etc/rc.d/init.d ] && sysvdir = /etc/rc.d/init.d || sysvdir = /etc/init.d
2005-01-13 01:22:45 +00:00
famdir = /etc/xinetd.d
2009-01-11 15:37:05 +02:00
COMPREPLY = ( $( builtin echo $sysvdir /!( *.rpm@( orig| new| save) | *~| functions) ) )
2004-05-24 07:50:30 +00:00
2004-07-03 22:26:29 +00:00
if [ -d $famdir ] ; then
2009-01-11 15:37:05 +02:00
COMPREPLY = ( " ${ COMPREPLY [@] } " $( builtin echo $famdir /!( *.rpm@( orig| new| save) | *~) ) )
2004-07-03 22:26:29 +00:00
fi
COMPREPLY = ( $( compgen -W '${COMPREPLY[@]#@($sysvdir|$famdir)/}' -- $cur ) )
2004-05-24 07:50:30 +00:00
}
2009-01-25 23:39:09 +02:00
# This function completes on modules
2005-01-03 01:24:14 +00:00
#
_modules( )
{
local modpath
modpath = /lib/modules/$1
COMPREPLY = ( $( command ls -R $modpath | \
sed -ne 's/^\(' $cur '.*\)\.k\?o\(\|.gz\)$/\1/p' ) )
}
2009-01-25 23:40:44 +02:00
# This function completes on installed modules
#
_installed_modules( )
{
COMPREPLY = ( $( compgen -W " $( /sbin/lsmod | \
awk '{if (NR != 1) print $1}' ) " -- $1 ) )
}
2009-01-25 23:39:09 +02:00
# This function completes on user:group format
2005-07-11 22:23:30 +00:00
#
_usergroup( )
{
local IFS = $'\n'
cur = ${ cur // \\ \\ / }
if [ [ $cur = *@( \\ :| .) * ] ] && [ -n " $bash205 " ] ; then
user = ${ cur %%*([^ : .]) }
COMPREPLY = ( $( compgen -P ${ user / \\ \\ } -g -- ${ cur ##*[. : ] } ) )
elif [ [ $cur = *:* ] ] && [ -n " $bash205 " ] ; then
COMPREPLY = ( $( compgen -g -- ${ cur ##*[. : ] } ) )
else
COMPREPLY = ( $( compgen -S : -u -- $cur ) )
fi
}
2009-04-19 17:33:55 +03:00
# This function completes on valid shells
#
_shells( )
{
COMPREPLY = ( " ${ COMPREPLY [@] } " $( compgen -W ' $( grep "^[[:space:]]*/" \
/etc/shells 2>/dev/null ) ' -- $cur ) )
}
2009-03-16 21:57:12 +01:00
# Get real command.
# - arg: $1 Command
# - stdout: Filename of command in PATH with possible symbolic links resolved.
# Empty string if command not found.
# - return: True (0) if command found, False (> 0) if not.
_realcommand( ) {
type -P " $1 " > /dev/null && {
if type -p realpath > /dev/null; then
realpath " $( type -P " $1 " ) "
elif type -p readlink > /dev/null; then
readlink -f " $( type -P " $1 " ) "
else
type -P " $1 "
fi
}
}
2005-07-11 22:23:30 +00:00
# this function count the number of mandatory args
#
_count_args( )
{
args = 1
for ( ( i = 1; i < COMP_CWORD; i++ ) ) ; do
if [ [ " ${ COMP_WORDS [i] } " != -* ] ] ; then
args = $(( $args + 1 ))
fi
done
}
2009-06-12 20:45:35 +03:00
# This function completes on PCI IDs
2009-02-10 23:50:04 +01:00
#
_pci_ids( )
{
COMPREPLY = ( ${ COMPREPLY [@] :- } \
$( compgen -W " $( lspci -n | awk '{print $3}' ) " -- $cur ) )
}
2009-06-12 20:45:35 +03:00
# This function completes on USB IDs
2009-02-10 23:50:04 +01:00
#
_usb_ids( )
{
COMPREPLY = ( ${ COMPREPLY [@] :- } \
$( compgen -W " $( lsusb | awk '{print $6}' ) " -- $cur ) )
}
2002-03-02 00:13:23 +00:00
# start of section containing completion functions for external programs
2003-08-03 23:40:25 +00:00
# a little help for FreeBSD ports users
[ $UNAME = FreeBSD ] && complete -W ' index search fetch fetch-list \
extract patch configure build install reinstall \
deinstall clean clean-depends kernel buildworld' make
2003-03-27 06:48:09 +00:00
# This completes on a list of all available service scripts for the
# 'service' command and/or the SysV init.d directory, followed by
# that script's available commands
#
{ have service || [ -d /etc/init.d/ ] ; } &&
_service( )
{
2009-07-23 00:37:33 +03:00
local cur prev sysvdir
2003-03-27 06:48:09 +00:00
COMPREPLY = ( )
prev = ${ COMP_WORDS [COMP_CWORD-1] }
2008-05-01 22:22:11 +02:00
cur = ` _get_cword`
2003-03-27 06:48:09 +00:00
# don't complete for things like killall, ssh and mysql if it's
# the standalone command, rather than the init script
2003-09-10 22:50:30 +00:00
[ [ ${ COMP_WORDS [0] } != @( *init.d/!( functions| ~) | service) ] ] && return 0
# don't complete past 2nd token
[ $COMP_CWORD -gt 2 ] && return 0
2003-03-27 06:48:09 +00:00
[ -d /etc/rc.d/init.d ] && sysvdir = /etc/rc.d/init.d \
|| sysvdir = /etc/init.d
if [ [ $COMP_CWORD -eq 1 ] ] && [ [ $prev = = "service" ] ] ; then
2004-05-24 07:50:30 +00:00
_services
2003-03-27 06:48:09 +00:00
else
COMPREPLY = ( $( compgen -W ' ` sed -ne " y/|/ /; \
2004-10-08 14:31:28 +00:00
s/^.*Usage.*{ \( .*\) } .*$/\1 /p" \
$sysvdir /${ prev ##*/ } 2>/dev/null` ' -- $cur ) )
2003-03-27 06:48:09 +00:00
fi
return 0
2005-07-07 23:36:51 +00:00
} &&
complete -F _service service
2003-03-27 06:48:09 +00:00
[ -d /etc/init.d/ ] && complete -F _service $default \
$( for i in /etc/init.d/*; do echo ${ i ##*/ } ; done )
2002-03-26 15:17:12 +00:00
# chown(1) completion
2000-08-08 22:17:29 +00:00
#
2000-08-29 00:41:27 +00:00
_chown( )
2000-08-08 22:17:29 +00:00
{
2009-04-14 23:43:39 +03:00
local cur prev split = false
2008-05-01 22:22:11 +02:00
cur = ` _get_cword`
2009-04-14 23:43:39 +03:00
prev = ${ COMP_WORDS [COMP_CWORD-1] }
_split_longopt && split = true
2009-04-14 23:46:38 +03:00
case " $prev " in
--from)
_usergroup
return 0
; ;
--reference)
_filedir
return 0
; ;
esac
2009-04-14 23:43:39 +03:00
$split && return 0
2000-08-08 22:17:29 +00:00
2002-09-08 20:04:26 +00:00
# options completion
if [ [ " $cur " = = -* ] ] ; then
COMPREPLY = ( $( compgen -W ' -c -h -f -R -v --changes \
2009-04-14 23:43:39 +03:00
--dereference --no-dereference --from --silent --quiet \
--reference --recursive --verbose --help --version' -- $cur ) )
2002-06-26 03:30:50 +00:00
else
2005-07-11 22:23:30 +00:00
_count_args
case $args in
1)
_usergroup
; ;
*)
_filedir
; ;
esac
2000-08-08 22:17:29 +00:00
fi
}
2002-06-26 03:30:50 +00:00
complete -F _chown $filenames chown
2000-08-08 22:17:29 +00:00
2002-01-08 04:49:06 +00:00
# chgrp(1) completion
#
_chgrp( )
{
2009-04-14 23:43:39 +03:00
local cur prev split = false
2002-01-08 04:49:06 +00:00
COMPREPLY = ( )
2008-05-01 22:22:11 +02:00
cur = ` _get_cword`
2005-01-12 21:45:16 +00:00
cur = ${ cur // \\ \\ / }
2002-01-08 04:49:06 +00:00
prev = ${ COMP_WORDS [COMP_CWORD-1] }
2009-04-14 23:43:39 +03:00
_split_longopt && split = true
2009-04-14 23:48:11 +03:00
if [ [ " $prev " = = --reference ] ] ; then
_filedir
return 0
fi
2009-04-14 23:43:39 +03:00
$split && return 0
2009-07-25 13:38:42 +03:00
2002-09-08 20:04:26 +00:00
# options completion
if [ [ " $cur " = = -* ] ] ; then
COMPREPLY = ( $( compgen -W ' -c -h -f -R -v --changes \
--dereference --no-dereference --silent --quiet \
2009-04-14 23:43:39 +03:00
--reference --recursive --verbose --help --version' -- $cur ) )
2002-09-08 20:04:26 +00:00
return 0
fi
2002-01-08 04:49:06 +00:00
# first parameter on line or first since an option?
2002-04-02 23:37:51 +00:00
if [ $COMP_CWORD -eq 1 ] && [ [ " $cur " != -* ] ] || \
2003-10-07 06:42:33 +00:00
[ [ " $prev " = = -* ] ] && [ -n " $bash205 " ] ; then
2005-01-13 01:22:45 +00:00
local IFS = $'\n'
2003-10-07 06:42:33 +00:00
COMPREPLY = ( $( compgen -g $cur 2>/dev/null ) )
2002-04-02 23:37:51 +00:00
else
2005-01-12 21:45:16 +00:00
_filedir || return 0
2002-01-08 04:49:06 +00:00
fi
return 0
}
2005-01-12 21:45:16 +00:00
complete -F _chgrp $filenames chgrp
2002-01-08 04:49:06 +00:00
2000-08-08 22:17:29 +00:00
# umount(8) completion. This relies on the mount point being the third
# space-delimited field in the output of mount(8)
#
2000-08-29 00:41:27 +00:00
_umount( )
2000-08-08 22:17:29 +00:00
{
2008-05-06 20:14:12 +01:00
local cur IFS = $'\n'
2000-08-08 22:17:29 +00:00
2001-12-18 03:43:25 +00:00
COMPREPLY = ( )
2008-05-01 22:22:11 +02:00
cur = ` _get_cword`
2000-08-08 22:17:29 +00:00
2002-04-03 16:56:01 +00:00
COMPREPLY = ( $( compgen -W '$( mount | cut -d" " -f 3 )' -- $cur ) )
2000-08-29 00:41:27 +00:00
2000-08-08 22:17:29 +00:00
return 0
}
2002-04-03 17:19:30 +00:00
complete -F _umount $dirnames umount
2000-08-08 22:17:29 +00:00
# mount(8) completion. This will pull a list of possible mounts out of
2003-01-17 09:01:29 +00:00
# /etc/{,v}fstab, unless the word being completed contains a ':', which
2000-08-08 22:17:29 +00:00
# would indicate the specification of an NFS server. In that case, we
# query the server for a list of all available exports and complete on
# that instead.
#
2000-08-29 00:41:27 +00:00
_mount( )
2008-05-10 18:04:06 +02:00
{
2009-04-14 21:24:20 +03:00
local cur i sm host prev
2000-08-08 22:17:29 +00:00
2001-12-18 03:43:25 +00:00
COMPREPLY = ( )
2008-05-01 22:22:11 +02:00
cur = ` _get_cword`
2002-10-05 06:03:21 +00:00
[ [ " $cur " = = \\ ] ] && cur = "/"
2009-04-14 21:24:20 +03:00
prev = ${ COMP_WORDS [COMP_CWORD-1] }
2002-10-05 06:03:21 +00:00
2002-03-24 18:57:37 +00:00
for i in { ,/usr} /{ ,s} bin/showmount; do [ -x $i ] && sm = $i && break; done
2000-08-08 22:17:29 +00:00
2001-12-21 07:56:18 +00:00
if [ -n " $sm " ] && [ [ " $cur " = = *:* ] ] ; then
2002-03-24 18:57:37 +00:00
COMPREPLY = ( $( $sm -e ${ cur %% : * } | sed 1d | \
2000-10-09 18:07:50 +00:00
grep ^${ cur #* : } | awk '{print $1}' ) )
2002-07-15 23:07:03 +00:00
elif [ [ " $cur " = = //* ] ] ; then
host = ${ cur #// }
host = ${ host %%/* }
2003-06-02 00:41:33 +00:00
if [ -n " $host " ] ; then
2006-02-23 17:01:37 +00:00
COMPREPLY = ( $( compgen -W " $( echo $( smbclient -d 0 -NL $host 2>/dev/null|
sed -ne '/^[' " $'\t ' " ']*Sharename/,/^$/p' |
sed -ne '3,$s|^[^A-Za-z]*\([^' " $'\t ' " ']*\).*$|//' $host '/\1|p' ) ) " -- " $cur " ) )
2002-07-15 23:07:03 +00:00
fi
2003-01-17 09:01:29 +00:00
elif [ -r /etc/vfstab ] ; then
# Solaris
2009-04-14 22:26:14 +03:00
COMPREPLY = ( $( compgen -W " $( awk '! /^[ \t]*#/ {if ($3 ~ /\//) print $3}' /etc/vfstab ) " -- $cur ) )
2005-01-03 02:42:12 +00:00
elif [ ! -e /etc/fstab ] ; then
# probably Cygwin
2009-04-14 22:26:14 +03:00
COMPREPLY = ( $( compgen -W " $( mount | awk '! /^[ \t]*#/ {if ($3 ~ /\//) print $3}' ) " -- $cur ) )
2000-08-29 00:41:27 +00:00
else
2003-01-17 09:01:29 +00:00
# probably Linux
2009-04-14 21:24:20 +03:00
if [ $prev = -L ] ; then
COMPREPLY = ( $( compgen -W '$(sed -ne "s/^[[:space:]]*LABEL=\([^[:space:]]*\).*/\1/p" /etc/fstab )' -- $cur ) )
elif [ $prev = -U ] ; then
COMPREPLY = ( $( compgen -W '$(sed -ne "s/^[[:space:]]*UUID=\([^[:space:]]*\).*/\1/p" /etc/fstab )' -- $cur ) )
else
2009-04-14 22:26:14 +03:00
COMPREPLY = ( $( compgen -W " $( awk '! /^[ \t]*#/ {if ($2 ~ /\//) print $2}' /etc/fstab ) " -- $cur ) )
2009-04-14 21:24:20 +03:00
fi
2000-08-29 00:41:27 +00:00
fi
return 0
2000-08-08 22:17:29 +00:00
}
2008-09-06 18:12:52 +02:00
complete -F _mount $default $dirnames mount
2000-08-08 22:17:29 +00:00
2002-12-05 04:22:22 +00:00
# Linux rmmod(8) completion. This completes on a list of all currently
2000-08-08 22:17:29 +00:00
# installed kernel modules.
#
2002-12-05 04:22:22 +00:00
have rmmod && {
2000-08-29 00:41:27 +00:00
_rmmod( )
2000-08-08 22:17:29 +00:00
{
local cur
2001-12-18 03:43:25 +00:00
COMPREPLY = ( )
2008-05-01 22:22:11 +02:00
cur = ` _get_cword`
2000-08-08 22:17:29 +00:00
2009-01-25 23:40:44 +02:00
_installed_modules " $cur "
2000-08-08 22:17:29 +00:00
return 0
}
2002-12-05 04:22:22 +00:00
complete -F _rmmod rmmod
2000-08-08 22:17:29 +00:00
2003-01-21 06:37:44 +00:00
# Linux insmod(8), modprobe(8) and modinfo(8) completion. This completes on a
# list of all available modules for the version of the kernel currently
# running.
2000-08-08 22:17:29 +00:00
#
2000-08-29 00:41:27 +00:00
_insmod( )
2000-08-08 22:17:29 +00:00
{
2000-09-25 21:38:11 +00:00
local cur prev modpath
2000-08-08 22:17:29 +00:00
2001-12-18 03:43:25 +00:00
COMPREPLY = ( )
2008-05-01 22:22:11 +02:00
cur = ` _get_cword`
2001-12-18 03:43:25 +00:00
prev = ${ COMP_WORDS [COMP_CWORD-1] }
2000-08-08 22:17:29 +00:00
2001-01-31 22:57:06 +00:00
# behave like lsmod for modprobe -r
2002-02-26 22:59:47 +00:00
if [ $1 = "modprobe" ] &&
2002-01-08 04:49:06 +00:00
[ " ${ COMP_WORDS [1] } " = "-r" ] ; then
2009-01-25 23:40:44 +02:00
_installed_modules " $cur "
2001-01-31 22:57:06 +00:00
return 0
fi
# do filename completion if we're giving a path to a module
2004-07-05 21:31:47 +00:00
if [ [ " $cur " = = */* ] ] ; then
_filedir '@(?(k)o?(.gz))'
2001-12-18 03:43:25 +00:00
return 0
fi
2001-01-31 22:57:06 +00:00
2008-05-06 20:20:14 +01:00
if [ $COMP_CWORD -gt 1 ] &&
2003-04-29 05:38:32 +00:00
[ [ " ${ COMP_WORDS [COMP_CWORD-1] } " != -* ] ] ; then
2001-01-31 22:57:06 +00:00
# do module parameter completion
2002-02-26 22:59:47 +00:00
COMPREPLY = ( $( /sbin/modinfo -p ${ COMP_WORDS [1] } 2>/dev/null | \
2002-03-15 19:02:06 +00:00
awk '{if ($1 ~ /^parm:/ && $2 ~ /^' $cur ' /) { print $2 } \
else if ( $1 !~ /:/ && $1 ~ /^'$cur' /) { print $1 } } ' ) )
2005-01-03 01:24:14 +00:00
else
_modules $( uname -r)
2000-09-25 21:38:11 +00:00
fi
2000-08-29 00:41:27 +00:00
2000-08-08 22:17:29 +00:00
return 0
}
2003-01-21 06:37:44 +00:00
complete -F _insmod $filenames insmod modprobe modinfo
2002-12-05 04:22:22 +00:00
}
2000-08-08 22:17:29 +00:00
2002-04-02 06:17:35 +00:00
# renice(8) completion
#
_renice( )
{
local command cur curopt i
COMPREPLY = ( )
2008-05-01 22:22:11 +02:00
cur = ` _get_cword`
2002-04-02 06:17:35 +00:00
command = $1
i = 0
2003-04-15 06:54:50 +00:00
# walk back through command line and find last option
2002-04-02 06:17:35 +00:00
while [ $i -le $COMP_CWORD -a ${# COMPREPLY [@] } -eq 0 ] ; do
curopt = ${ COMP_WORDS [COMP_CWORD- $i ] }
case " $curopt " in
-u)
COMPREPLY = ( $( compgen -u -- $cur ) )
; ;
-g)
_pgids
; ;
-p| $command )
_pids
; ;
esac
i = $(( + + i ))
done
}
complete -F _renice renice
2002-02-19 17:37:15 +00:00
# kill(1) completion
#
_kill( )
{
local cur
COMPREPLY = ( )
2008-05-01 22:22:11 +02:00
cur = ` _get_cword`
2002-02-19 17:37:15 +00:00
2002-03-07 18:04:05 +00:00
if [ $COMP_CWORD -eq 1 ] && [ [ " $cur " = = -* ] ] ; then
2002-02-19 17:37:15 +00:00
# return list of available signals
_signals
else
# return list of available PIDs
2002-04-02 06:22:27 +00:00
_pids
2002-02-19 17:37:15 +00:00
fi
}
complete -F _kill kill
2009-02-15 19:12:05 +02:00
# killall(1) (Linux and FreeBSD) and pkill(1) completion.
2000-08-08 22:17:29 +00:00
#
2009-02-15 19:12:05 +02:00
[ $UNAME = Linux -o $UNAME = FreeBSD ] || have pkill &&
2000-08-29 00:41:27 +00:00
_killall( )
2000-08-08 22:17:29 +00:00
{
2002-02-19 17:37:15 +00:00
local cur
2000-08-08 22:17:29 +00:00
2001-12-18 03:43:25 +00:00
COMPREPLY = ( )
2008-05-01 22:22:11 +02:00
cur = ` _get_cword`
2000-08-08 22:17:29 +00:00
2002-02-19 17:37:15 +00:00
if [ $COMP_CWORD -eq 1 ] && [ [ " $cur " = = -* ] ] ; then
_signals
else
2009-02-15 18:47:34 +02:00
_pnames
2000-08-08 22:17:29 +00:00
fi
return 0
}
2009-02-15 19:12:05 +02:00
[ $UNAME = Linux -o $UNAME = FreeBSD ] && complete -F _killall killall
have pkill && complete -F _killall pkill
2000-08-08 22:17:29 +00:00
2009-02-15 19:04:51 +02:00
# pgrep(1) completion.
2004-07-03 22:51:18 +00:00
#
2009-02-15 19:04:51 +02:00
[ $UNAME = Linux ] || have pgrep &&
2004-07-03 22:51:18 +00:00
_pgrep( )
{
local cur
COMPREPLY = ( )
2008-05-01 22:22:11 +02:00
cur = ` _get_cword`
2004-07-03 22:51:18 +00:00
2009-02-15 18:53:09 +02:00
_pnames
2004-07-03 22:51:18 +00:00
return 0
}
2009-02-15 19:04:51 +02:00
have pgrep && complete -F _pgrep pgrep
2004-07-03 22:51:18 +00:00
# Linux pidof(8) completion.
[ $UNAME = Linux ] && complete -F _pgrep pidof
2009-01-11 00:27:23 +02:00
# Red Hat & Debian GNU/Linux if{up,down} completion
2002-01-08 00:33:08 +00:00
#
2009-05-20 22:08:53 +02:00
[ $USERLAND = GNU ] && { have ifup || have ifdown; } &&
2002-01-08 00:33:08 +00:00
_ifupdown( )
{
2002-01-29 22:06:26 +00:00
local cur
2002-01-08 00:33:08 +00:00
2002-01-29 22:06:26 +00:00
COMPREPLY = ( )
2008-05-01 22:22:11 +02:00
cur = ` _get_cword`
2002-01-08 00:33:08 +00:00
2002-02-26 22:59:47 +00:00
if [ $COMP_CWORD -eq 1 ] ; then
2005-01-10 22:39:56 +00:00
_configured_interfaces
2008-05-01 22:22:11 +02:00
COMPREPLY = ( $( compgen -W '${COMPREPLY[@]}' -- " $cur " ) )
2002-01-08 00:33:08 +00:00
fi
return 0
2005-07-07 23:36:51 +00:00
} &&
complete -F _ifupdown ifup ifdown
2009-05-20 22:08:53 +02:00
[ $USERLAND = GNU ] && have ifstatus && complete -F _ifupdown ifstatus
2002-01-08 00:33:08 +00:00
2002-08-19 14:56:55 +00:00
# Linux ipsec(8) completion (for FreeS/WAN)
2000-08-08 22:17:29 +00:00
#
2002-04-22 05:59:08 +00:00
[ $UNAME = Linux ] && have ipsec &&
2000-08-29 00:41:27 +00:00
_ipsec( )
2000-08-08 22:17:29 +00:00
{
local cur
2001-12-18 03:43:25 +00:00
COMPREPLY = ( )
2008-05-01 22:22:11 +02:00
cur = ` _get_cword`
2000-08-08 22:17:29 +00:00
2008-05-06 20:20:14 +01:00
2002-02-26 22:59:47 +00:00
if [ $COMP_CWORD -eq 1 ] ; then
2002-02-13 16:02:57 +00:00
COMPREPLY = ( $( compgen -W ' auto barf eroute klipsdebug look \
manual pluto ranbits rsasigkey \
setup showdefaults showhostkey spi \
2003-10-07 04:45:03 +00:00
spigrp tncfg whack' -- $cur ) )
2000-10-09 18:07:50 +00:00
return 0
fi
case ${ COMP_WORDS [1] } in
auto)
COMPREPLY = ( $( compgen -W ' --asynchronous --up --add --delete \
--replace --down --route --unroute \
2002-02-13 16:02:57 +00:00
--ready --status --rereadsecrets' \
2003-10-07 04:45:03 +00:00
-- $cur ) )
2000-10-09 18:07:50 +00:00
; ;
manual)
COMPREPLY = ( $( compgen -W ' --up --down --route --unroute \
2003-10-07 04:45:03 +00:00
--union' -- $cur ) )
2000-10-09 18:07:50 +00:00
; ;
ranbits)
2003-10-07 04:45:03 +00:00
COMPREPLY = ( $( compgen -W '--quick --continuous --bytes' \
-- $cur ) )
2000-10-09 18:07:50 +00:00
; ;
setup)
2003-10-07 04:45:03 +00:00
COMPREPLY = ( $( compgen -W '--start --stop --restart' -- $cur ) )
2000-10-09 18:07:50 +00:00
; ;
*)
; ;
esac
2000-08-29 00:41:27 +00:00
return 0
2005-07-07 23:36:51 +00:00
} &&
complete -F _ipsec ipsec
2000-08-11 21:20:41 +00:00
2002-10-05 05:52:37 +00:00
# This function provides simple user@host completion
2002-07-22 16:57:28 +00:00
#
_user_at_host( ) {
local cur
COMPREPLY = ( )
2008-05-01 22:22:11 +02:00
cur = ` _get_cword`
2002-07-22 16:57:28 +00:00
if [ [ $cur = = *@* ] ] ; then
2009-07-03 22:28:13 +02:00
_known_hosts_real " $cur "
2002-07-22 16:57:28 +00:00
else
2004-10-15 04:42:27 +00:00
COMPREPLY = ( $( compgen -u -- " $cur " ) )
2002-07-22 16:57:28 +00:00
fi
return 0
}
2002-12-10 00:30:08 +00:00
shopt -u hostcomplete && complete -F _user_at_host $nospace talk ytalk finger
2002-07-22 16:57:28 +00:00
2009-07-25 13:38:42 +03:00
# NOTE: Using this function as a helper function is deprecated. Use
2009-06-13 23:54:14 +02:00
# `_known_hosts_real' instead.
2000-09-25 21:38:11 +00:00
_known_hosts( )
2009-05-03 15:18:31 +02:00
{
2009-07-25 09:35:03 +02:00
local options
2009-05-03 15:18:31 +02:00
COMPREPLY = ( )
2009-07-25 09:35:03 +02:00
# NOTE: Using `_known_hosts' as a helper function and passing options
# to `_known_hosts' is deprecated: Use `_known_hosts_real' instead.
[ " $1 " = -a ] || [ " $2 " = -a ] && options = -a
[ " $1 " = -c ] || [ " $2 " = -c ] && options = " $options -c "
_known_hosts_real $options " $( _get_cword) "
2009-05-03 15:18:31 +02:00
}
2009-06-13 08:38:52 +02:00
# Helper function for completing _known_hosts.
# This function performs host completion based on ssh's known_hosts files,
# defaulting to standard host completion if they don't exist.
2009-07-03 22:28:13 +02:00
# Usage: _known_hosts_real [OPTIONS] CWORD
# Options: -a Use aliases
# -c Use `:' suffix
# -F configfile Use `configfile' for configuration settings
# -p PREFIX Use PREFIX
# Return: Completions, starting with CWORD, are added to COMPREPLY[]
2009-05-03 15:18:31 +02:00
_known_hosts_real( )
2000-08-29 00:41:27 +00:00
{
2009-06-13 08:38:52 +02:00
local configfile flag prefix
2009-06-13 11:34:39 +02:00
local cur curd awkcur user suffix aliases global_kh user_kh hosts i host
2008-11-01 12:25:38 +01:00
local -a kh khd config
2000-08-29 00:41:27 +00:00
2008-11-01 12:25:38 +01:00
local OPTIND = 1
2009-07-03 22:28:13 +02:00
while getopts "acF:p:" flag " $@ " ; do
2008-11-01 12:25:38 +01:00
case $flag in
a) aliases = 'yes' ; ;
c) suffix = ':' ; ;
2009-06-13 08:38:52 +02:00
F) configfile = $OPTARG ; ;
p) prefix = $OPTARG ; ;
2008-11-01 12:25:38 +01:00
esac
done
2009-07-03 22:28:13 +02:00
[ $# -lt $OPTIND ] && echo " error: $FUNCNAME : missing mandatory argument CWORD "
cur = ${ !OPTIND } ; let "OPTIND += 1"
2009-06-18 21:51:01 +02:00
[ $# -ge $OPTIND ] && echo " error: $FUNCNAME ( " $@ "): unprocessed arguments:" \
2009-06-20 09:34:46 +02:00
$( while [ $# -ge $OPTIND ] ; do echo ${ !OPTIND } ; shift; done )
2009-05-02 17:13:07 +02:00
2002-01-16 07:14:14 +00:00
[ [ $cur = = *@* ] ] && user = ${ cur %@* } @ && cur = ${ cur #*@ }
2001-03-05 19:12:48 +00:00
kh = ( )
2000-09-11 18:46:39 +00:00
2002-05-05 23:31:28 +00:00
# ssh config files
2008-11-01 12:25:38 +01:00
if [ -n " $configfile " ] ; then
[ -r " $configfile " ] &&
config = ( " ${ config [@] } " " $configfile " )
else
[ -r /etc/ssh/ssh_config ] &&
config = ( " ${ config [@] } " "/etc/ssh/ssh_config" )
[ -r " ${ HOME } /.ssh/config " ] &&
config = ( " ${ config [@] } " " ${ HOME } /.ssh/config " )
[ -r " ${ HOME } /.ssh2/config " ] &&
config = ( " ${ config [@] } " " ${ HOME } /.ssh2/config " )
fi
2002-05-05 23:31:28 +00:00
if [ ${# config [@] } -gt 0 ] ; then
2009-07-07 23:02:27 +02:00
local OIFS = $IFS IFS = $'\n'
2009-04-16 23:25:22 +02:00
# expand path (if present) to global known hosts file
global_kh = ( $( sed -ne 's/^[ \t]*[Gg][Ll][Oo][Bb][Aa][Ll][Kk][Nn][Oo][Ww][Nn][Hh][Oo][Ss][Tt][Ss][Ff][Ii][Ll][Ee][' " $'\t ' " ']*\(.*\)$/\1/p' " ${ config [@] } " ) )
for ( ( i = 0; i < ${# global_kh [@] } ; i++ ) ) ; do
global_kh[ i] = $( echo " ${ global_kh [i]// \" / } " )
done
# expand path (if present) to user known hosts file
user_kh = ( $( sed -ne 's/^[ \t]*[Uu][Ss][Ee][Rr][Kk][Nn][Oo][Ww][Nn][Hh][Oo][Ss][Tt][Ss][Ff][Ii][Ll][Ee][' " $'\t ' " ']*\(.*\)$/\1/p' " ${ config [@] } " ) )
for ( ( i = 0; i < ${# user_kh [@] } ; i++ ) ) ; do
user_kh[ i] = $( echo " ${ user_kh [i]// \" / } " )
done
2009-07-07 23:02:27 +02:00
IFS = $OIFS
2002-05-05 23:31:28 +00:00
fi
2008-06-23 11:28:11 +02:00
# Global known_hosts files
[ -r " $global_kh " ] &&
2009-04-16 23:25:22 +02:00
kh = ( " ${ kh [@] } " " ${ global_kh [@] } " )
2008-11-01 12:25:38 +01:00
if [ -z " $configfile " ] ; then
[ -r /etc/ssh/ssh_known_hosts ] &&
kh = ( " ${ kh [@] } " /etc/ssh/ssh_known_hosts )
[ -r /etc/ssh/ssh_known_hosts2 ] &&
kh = ( " ${ kh [@] } " /etc/ssh/ssh_known_hosts2 )
[ -r /etc/known_hosts ] &&
kh = ( " ${ kh [@] } " /etc/known_hosts )
[ -r /etc/known_hosts2 ] &&
kh = ( " ${ kh [@] } " /etc/known_hosts2 )
[ -d /etc/ssh2/knownhosts ] &&
khd = ( " ${ khd [@] } " /etc/ssh2/knownhosts/*pub )
fi
2008-06-23 11:28:11 +02:00
# User known_hosts files
[ -r " $user_kh " ] &&
2009-04-16 23:25:22 +02:00
kh = ( " ${ kh [@] } " " ${ user_kh [@] } " )
2008-11-01 12:25:38 +01:00
if [ -z " $configfile " ] ; then
[ -r ~/.ssh/known_hosts ] &&
kh = ( " ${ kh [@] } " ~/.ssh/known_hosts )
[ -r ~/.ssh/known_hosts2 ] &&
kh = ( " ${ kh [@] } " ~/.ssh/known_hosts2 )
[ -d ~/.ssh2/hostkeys ] &&
khd = ( " ${ khd [@] } " ~/.ssh2/hostkeys/*pub )
fi
2000-08-29 00:41:27 +00:00
# If we have known_hosts files to use
2008-11-01 12:25:38 +01:00
if [ ${# kh [@] } -gt 0 -o ${# khd [@] } -gt 0 -o -n " $configfile " ] ; then
2009-08-11 21:08:25 +02:00
# Escape slashes and dots in paths for awk
awkcur = ${ cur // \/ / \\ \/ }
awkcur = ${ awkcur // \. / \\ \. }
curd = $awkcur
if [ [ " $awkcur " = = [ 0-9] *.* ] ] ; then
# Digits followed by a dot - just search for that
awkcur = " ^ $awkcur .* "
elif [ [ " $awkcur " = = [ 0-9] * ] ] ; then
# Digits followed by no dot - search for digits followed
# by a dot
awkcur = " ^ $awkcur .*\. "
elif [ -z " $awkcur " ] ; then
# A blank - search for a dot or an alpha character
awkcur = "[a-z.]"
else
awkcur = " ^ $awkcur "
fi
if [ ${# kh [@] } -gt 0 ] ; then
# FS needs to look for a comma separated list
COMPREPLY = ( $( awk ' BEGIN { FS = "," }
2009-01-16 23:05:07 +01:00
/^\s *[ ^| \# ] / { for ( i = 1; i<= 2; ++i) { \
2001-11-20 04:15:48 +00:00
gsub( " .* $" , "" , $i ) ; \
2009-06-13 11:34:39 +02:00
if ( $i ~ /'$awkcur' /) { print $i } \
2008-06-21 22:19:37 +02:00
} } ' " ${ kh [@] } " 2>/dev/null ) )
2009-08-11 21:08:25 +02:00
fi
if [ ${# khd [@] } -gt 0 ] ; then
# Needs to look for files called
# .../.ssh2/key_22_<hostname>.pub
# dont fork any processes, because in a cluster environment,
# there can be hundreds of hostkeys
for i in " ${ khd [@] } " ; do
if [ [ " $i " = = *key_22_$awkcurd *.pub ] ] && [ -r " $i " ] ; then
host = ${ i /#*key_22_/ }
host = ${ host /%.pub/ }
COMPREPLY = ( " ${ COMPREPLY [@] } " $host )
fi
done
fi
# append any available aliases from config files
if [ ${# config [@] } -gt 0 ] && [ -n " $aliases " ] ; then
local host_aliases = $( sed -ne 's/^[ \t]*[Hh][Oo][Ss][Tt]\([Nn][Aa][Mm][Ee]\)\?[' " $'\t ' " ']\+\([^#*?]*\)\(#.*\)\?$/\2/p' " ${ config [@] } " )
hosts = $( compgen -W " $host_aliases " -- $cur )
COMPREPLY = ( " ${ COMPREPLY [@] } " $hosts )
fi
# Add hosts reported by avahi, if it's available
# and if the daemon is started.
# The original call to avahi-browse also had "-k", to avoid
# lookups into avahi's services DB. We don't need the name
# of the service, and if it contains ";", it may mistify
# the result. But on Gentoo (at least), -k isn't available
# (even if mentioned in the manpage), so...
if type avahi-browse >& /dev/null; then
if [ -n " $( pidof avahi-daemon) " ] ; then
COMPREPLY = ( " ${ COMPREPLY [@] } " $(
compgen -W " $( avahi-browse -cpr _workstation._tcp | \
grep ^= | cut -d\; -f7 | sort -u ) " -- $cur ) )
fi
fi
# apply suffix and prefix
for ( ( i = 0; i < ${# COMPREPLY [@] } ; i++ ) ) ; do
COMPREPLY[ i] = $prefix $user ${ COMPREPLY [i] } $suffix
2005-01-19 03:41:36 +00:00
done
2009-08-12 22:20:56 +02:00
fi
# Add results of normal hostname completion, unless `COMP_KNOWN_HOSTS_WITH_HOSTFILE'
# is set to an empty value.
if [ -n " ${ COMP_KNOWN_HOSTS_WITH_HOSTFILE -1 } " ] ; then
COMPREPLY = ( " ${ COMPREPLY [@] } " $( compgen -A hostname -P " $prefix $user " -S " $suffix " -- $cur ) )
2000-08-29 00:41:27 +00:00
fi
return 0
}
2002-12-10 00:30:08 +00:00
complete -F _known_hosts traceroute traceroute6 tracepath tracepath6 \
2008-05-01 22:22:11 +02:00
ping ping6 fping fping6 telnet host nslookup rsh rlogin ftp dig ssh-installkeys mtr
2000-09-25 21:38:11 +00:00
2001-03-05 19:12:48 +00:00
# This meta-cd function observes the CDPATH variable, so that cd additionally
# completes on directories under those specified in CDPATH.
#
_cd( )
{
2008-05-01 22:22:11 +02:00
local IFS = $'\t\n' cur = ` _get_cword` i j k
2001-03-05 19:12:48 +00:00
2003-05-06 06:39:49 +00:00
# try to allow variable completion
if [ [ " $cur " = = ?( \\ ) \$ * ] ] ; then
COMPREPLY = ( $( compgen -v -P '$' -- " ${ cur #?( \\ ) $} " ) )
return 0
fi
2009-01-09 12:19:15 +01:00
# Enable -o filenames option, see Debian bug #272660
compgen -f /non-existing-dir/ >/dev/null
2002-03-29 02:43:09 +00:00
# Use standard dir completion if no CDPATH or parameter starts with /,
# ./ or ../
2002-12-18 18:15:46 +00:00
if [ -z " ${ CDPATH :- } " ] || [ [ " $cur " = = ?( .) ?( .) /* ] ] ; then
2002-02-27 15:48:20 +00:00
_filedir -d
2002-01-05 19:48:59 +00:00
return 0
fi
2002-05-13 21:40:40 +00:00
2004-07-03 23:15:29 +00:00
local -r mark_dirs = $( _rl_enabled mark-directories && echo y)
local -r mark_symdirs = $( _rl_enabled mark-symlinked-directories && echo y)
2002-03-29 02:43:09 +00:00
# we have a CDPATH, so loop on its contents
for i in ${ CDPATH // : / $'\t' } ; do
# create an array of matched subdirs
2008-06-21 22:19:37 +02:00
k = " ${# COMPREPLY [@] } "
2002-06-24 14:34:50 +00:00
for j in $( compgen -d $i /$cur ) ; do
2004-07-03 23:15:29 +00:00
if [ [ ( $mark_symdirs && -h $j || $mark_dirs && ! -h $j ) && ! -d ${ j # $i / } ] ] ; then
j = " ${ j } / "
fi
COMPREPLY[ k++] = ${ j # $i / }
2002-05-16 07:05:04 +00:00
done
2002-03-29 02:43:09 +00:00
done
2002-05-13 21:40:40 +00:00
2002-02-27 15:48:20 +00:00
_filedir -d
2001-03-05 19:12:48 +00:00
2004-07-03 23:15:29 +00:00
if [ [ ${# COMPREPLY [@] } -eq 1 ] ] ; then
i = ${ COMPREPLY [0] }
if [ " $i " = = " $cur " ] && [ [ $i != "*/" ] ] ; then
COMPREPLY[ 0] = " ${ i } / "
fi
fi
2008-05-06 20:20:14 +01:00
2001-03-05 19:12:48 +00:00
return 0
}
2003-01-13 02:30:37 +00:00
if shopt -q cdable_vars; then
2009-01-09 12:19:15 +01:00
complete -v -F _cd $nospace cd
2003-01-13 02:30:37 +00:00
else
2009-01-09 12:19:15 +01:00
complete -F _cd $nospace cd
2003-01-13 02:30:37 +00:00
fi
2001-07-09 00:55:01 +00:00
2009-02-12 22:12:20 +01:00
# a wrapper method for the next one, when the offset is unknown
_command( )
{
local offset i
# find actual offset, as position of the first non-option
offset = 1
for ( ( i = 1; i <= COMP_CWORD; i++ ) ) ; do
if [ [ " ${ COMP_WORDS [i] } " != -* ] ] ; then
offset = $i
break
fi
done
_command_offset $offset
2008-06-23 11:33:52 +02:00
}
2002-01-18 15:54:22 +00:00
# A meta-command completion function for commands like sudo(8), which need to
# first complete on a command, then complete according to that command's own
2002-01-23 23:13:21 +00:00
# completion definition - currently not quite foolproof (e.g. mount and umount
2005-01-19 03:19:45 +00:00
# don't work properly), but still quite useful.
2002-01-18 15:54:22 +00:00
#
2009-02-12 22:12:20 +01:00
_command_offset( )
2002-01-18 15:54:22 +00:00
{
2009-02-12 22:12:20 +01:00
local cur func cline cspec noglob cmd i char_offset word_offset \
2003-06-07 21:23:49 +00:00
_COMMAND_FUNC _COMMAND_FUNC_ARGS
2002-01-18 15:54:22 +00:00
2009-02-12 22:12:20 +01:00
word_offset = $1
# rewrite current completion context before invoking
# actual command completion
# find new first word position, then
# rewrite COMP_LINE and adjust COMP_POINT
local first_word = ${ COMP_WORDS [ $word_offset ] }
for ( ( i = 0; i <= ${# COMP_LINE } ; i++ ) ) ; do
if [ [ " ${ COMP_LINE : $i : ${# first_word } } " = = " $first_word " ] ] ; then
char_offset = $i
break
fi
done
COMP_LINE = ${ COMP_LINE : $char_offset }
COMP_POINT = $(( COMP_POINT - $char_offset ))
# shift COMP_WORDS elements and adjust COMP_CWORD
for ( ( i = 0; i <= COMP_CWORD - $word_offset ; i++ ) ) ; do
COMP_WORDS[ i] = ${ COMP_WORDS [i+ $word_offset ] }
done
for ( ( i; i <= COMP_CWORD; i++ ) ) ; do
unset COMP_WORDS[ i] ;
done
COMP_CWORD = $(( $COMP_CWORD - $word_offset ))
2002-01-18 15:54:22 +00:00
COMPREPLY = ( )
2008-05-01 22:22:11 +02:00
cur = ` _get_cword`
2002-01-18 15:54:22 +00:00
2008-06-23 15:05:28 +02:00
if [ [ $COMP_CWORD -eq 0 ] ] ; then
2002-02-27 01:02:43 +00:00
COMPREPLY = ( $( compgen -c -- $cur ) )
2009-02-12 22:12:20 +01:00
else
cmd = ${ COMP_WORDS [0] }
if complete -p $cmd & >/dev/null; then
2003-06-07 21:09:34 +00:00
cspec = $( complete -p $cmd )
if [ " ${ cspec #* -F } " != " $cspec " ] ; then
2002-01-18 15:54:22 +00:00
# complete -F <function>
# get function name
func = ${ cspec #*-F }
func = ${ func %% * }
2009-07-25 13:38:42 +03:00
2008-06-23 11:44:42 +02:00
if [ [ ${# COMP_WORDS [@] } -ge 2 ] ] ; then
2008-06-23 15:05:28 +02:00
$func $cmd " ${ COMP_WORDS [ ${# COMP_WORDS [@] } -1] } " " ${ COMP_WORDS [ ${# COMP_WORDS [@] } -2] } "
2008-06-23 11:44:42 +02:00
else
2008-06-23 13:34:05 +02:00
$func $cmd " ${ COMP_WORDS [ ${# COMP_WORDS [@] } -1] } "
2008-06-23 11:44:42 +02:00
fi
2002-03-30 18:20:51 +00:00
# remove any \: generated by a command that doesn't
# default to filenames or dirnames (e.g. sudo chown)
2008-06-23 11:44:42 +02:00
# FIXME: I'm pretty sure this does not work!
2002-03-30 18:20:51 +00:00
if [ " ${ cspec #*-o } " != " $cspec " ] ; then
cspec = ${ cspec #*-o }
cspec = ${ cspec %% * }
if [ [ " $cspec " != @( dir| file) names ] ] ; then
2004-02-09 20:40:41 +00:00
COMPREPLY = ( " ${ COMPREPLY [@]// \\ \\ : / : } " )
2002-03-30 18:20:51 +00:00
fi
fi
2003-06-07 21:09:34 +00:00
elif [ -n " $cspec " ] ; then
cspec = ${ cspec #complete } ;
cspec = ${ cspec %% $cmd } ;
COMPREPLY = ( $( eval compgen " $cspec " -- " $cur " ) ) ;
2002-01-18 15:54:22 +00:00
fi
2009-02-12 22:12:20 +01:00
fi
2002-01-18 15:54:22 +00:00
fi
2002-02-27 15:28:26 +00:00
[ ${# COMPREPLY [@] } -eq 0 ] && _filedir
2002-01-18 15:54:22 +00:00
}
2009-02-12 22:12:20 +01:00
complete -F _command $filenames nohup exec nice eval time ltrace then \
2008-05-06 20:34:22 +01:00
else do vsound command xargs tsocks
2002-01-18 15:54:22 +00:00
2002-02-26 22:21:39 +00:00
_root_command( )
{
2003-06-07 21:09:34 +00:00
PATH = $PATH :/sbin:/usr/sbin:/usr/local/sbin _command $1 $2 $3
2002-02-26 22:21:39 +00:00
}
2008-09-07 16:27:02 +02:00
complete -F _root_command $filenames sudo fakeroot really gksudo gksu kdesudo
2002-02-26 22:21:39 +00:00
2002-02-16 00:13:00 +00:00
_longopt( )
{
2009-04-14 23:55:50 +03:00
local cur prev
mkdir and rmdir now bound to _longopt(), default to dirnames
a2ps, autoconf, automake, bc, gprof, ld, nm, objcopy, objdump, readelf, strip,
bison, cpio, diff, patch, enscript, cp, df, dir, du, ln, ls, mkfifo, mknod,
mv, rm, touch, vdir, xargs, awk, gperf, grep, gpg, grub, indent, less, m4,
sed, shar, date, env, seq, su, tee, uname, who, texindex, cat, csplit, cut,
expand, fmt, fold, head, md5sum, nl, od, paste, pr, ptx, sha1sum, sort,
split, tac, tail, tr, unexpand, uniq, wc, units and rsync now all have
GNU long option completion from _longopt()
gpc completion added into _gcc()
cat, less, more, ln and strip no longer bound to _filedir()
2002-02-18 08:26:34 +00:00
2008-05-01 22:22:11 +02:00
cur = ` _get_cword`
2009-04-14 23:55:50 +03:00
prev = ${ COMP_WORDS [COMP_CWORD-1] }
2002-02-18 08:32:56 +00:00
2009-04-14 23:55:50 +03:00
if _split_longopt; then
2009-04-15 00:09:33 +03:00
case " $prev " in
*[ Dd] [ Ii] [ Rr] *)
_filedir -d
; ;
*[ Ff] [ Ii] [ Ll] [ Ee] *)
_filedir
; ;
esac
2002-03-01 22:41:42 +00:00
return 0
fi
2002-02-18 10:09:59 +00:00
if [ [ " $cur " = = -* ] ] ; then
2009-05-10 20:27:33 +03:00
COMPREPLY = ( $( compgen -W " $( $1 --help 2>& 1 | sed -e '/--/!d' \
-e 's/.*\(--[-A-Za-z0-9]\+\).*/\1/' | sort -u ) " \
-- $cur ) )
2008-05-11 15:32:49 +02:00
elif [ [ " $1 " = = rmdir ] ] ; then
2002-02-22 15:48:21 +00:00
_filedir -d
2002-02-18 10:09:59 +00:00
else
_filedir
fi
2002-02-16 00:13:00 +00:00
}
2005-01-16 17:42:01 +00:00
# makeinfo and texi2dvi are defined elsewhere.
2002-02-18 17:26:54 +00:00
for i in a2ps autoconf automake bc gprof ld nm objcopy objdump readelf strip \
2009-01-24 21:20:58 +02:00
bison diff patch enscript cp df dir du ln ls mkfifo mknod mv rm \
2005-01-19 03:22:03 +00:00
touch vdir awk gperf grep grub indent less m4 sed shar date \
2005-01-16 17:42:01 +00:00
tee who texindex cat csplit cut expand fmt fold head \
2002-02-18 17:26:54 +00:00
md5sum nl od paste pr ptx sha1sum sort split tac tail tr unexpand \
2005-01-16 17:42:01 +00:00
uniq wc ldd bash id irb mkdir rmdir; do
have $i && complete -F _longopt $filenames $i
done
2009-05-29 23:14:23 +03:00
# These commands do not use filenames, so '-o filenames' is not needed.
2005-01-16 17:42:01 +00:00
for i in env netstat seq uname units wget; do
have $i && complete -F _longopt $default $i
mkdir and rmdir now bound to _longopt(), default to dirnames
a2ps, autoconf, automake, bc, gprof, ld, nm, objcopy, objdump, readelf, strip,
bison, cpio, diff, patch, enscript, cp, df, dir, du, ln, ls, mkfifo, mknod,
mv, rm, touch, vdir, xargs, awk, gperf, grep, gpg, grub, indent, less, m4,
sed, shar, date, env, seq, su, tee, uname, who, texindex, cat, csplit, cut,
expand, fmt, fold, head, md5sum, nl, od, paste, pr, ptx, sha1sum, sort,
split, tac, tail, tr, unexpand, uniq, wc, units and rsync now all have
GNU long option completion from _longopt()
gpc completion added into _gcc()
cat, less, more, ln and strip no longer bound to _filedir()
2002-02-18 08:26:34 +00:00
done
unset i
2002-10-23 14:05:16 +00:00
# look(1) completion
#
2008-05-06 20:20:14 +01:00
have look &&
2002-10-23 14:05:16 +00:00
_look( )
{
local cur
2008-05-06 20:20:14 +01:00
2002-10-23 14:05:16 +00:00
COMPREPLY = ( )
2008-05-01 22:22:11 +02:00
cur = ` _get_cword`
2002-10-23 14:05:16 +00:00
if [ $COMP_CWORD = 1 ] ; then
2008-09-06 16:10:08 +02:00
COMPREPLY = ( $( compgen -W '$(look $cur 2>/dev/null)' ) )
2002-10-23 14:05:16 +00:00
fi
2005-07-07 23:36:51 +00:00
} &&
complete -F _look $default look
2002-10-23 14:05:16 +00:00
2005-07-11 22:19:05 +00:00
# id(1) completion
#
2005-07-12 17:13:02 +00:00
have id &&
2005-07-11 22:19:05 +00:00
_id( )
{
local cur
COMPREPLY = ( )
2008-05-01 22:22:11 +02:00
cur = ` _get_cword`
2005-07-11 22:19:05 +00:00
if [ [ " $cur " = = -* ] ] ; then
COMPREPLY = ( $( compgen -W ' -a -g --group -G --groups -n --name\
-r --real -u --user --help --version' -- $cur ) )
else
COMPREPLY = ( $( compgen -u $cur ) )
fi
} &&
complete -F _id id
2002-01-23 23:36:30 +00:00
_filedir_xspec( )
2001-12-20 07:52:12 +00:00
{
2002-01-04 04:34:21 +00:00
local IFS cur xspec
2001-12-20 07:52:12 +00:00
2008-06-22 21:42:22 +02:00
IFS = $'\t\n'
2001-12-20 07:52:12 +00:00
COMPREPLY = ( )
2008-05-01 22:22:11 +02:00
cur = ` _get_cword`
2002-01-23 23:13:21 +00:00
_expand || return 0
2001-12-20 07:52:12 +00:00
# get first exclusion compspec that matches this command
2004-07-11 16:18:24 +00:00
xspec = $( sed -ne $'/^complete .*[ \t]' ${ 1 ##*/ } $'\([ \t]\|$\)/{p;q;}' \
$BASH_COMPLETION )
2001-12-20 07:52:12 +00:00
# prune to leave nothing but the -X spec
xspec = ${ xspec #*-X }
xspec = ${ xspec %% * }
2009-03-30 23:53:23 +03:00
local -a toks
local tmp
2008-11-01 09:49:57 +01:00
toks = ( ${ toks [@]- } $(
2008-10-24 19:17:23 +02:00
compgen -d -- " $( quote_readline " $cur " ) " | {
while read -r tmp; do
2008-10-25 14:33:12 +02:00
# see long TODO comment in _filedir() --David
echo $tmp
2008-10-24 19:17:23 +02:00
done
}
) )
2009-07-25 13:38:42 +03:00
2008-11-01 09:49:57 +01:00
toks = ( ${ toks [@]- } $(
2009-01-08 16:35:03 +01:00
eval compgen -f -X " $xspec " -- "\$(quote_readline " \$ cur")" | {
2008-10-24 19:17:23 +02:00
while read -r tmp; do
2008-10-25 14:33:12 +02:00
[ -n $tmp ] && echo $tmp
2008-10-24 19:17:23 +02:00
done
}
) )
2009-07-25 13:38:42 +03:00
2008-06-23 11:58:40 +02:00
COMPREPLY = ( " ${ toks [@] } " )
2001-12-20 07:52:12 +00:00
}
2001-12-20 16:12:44 +00:00
list = ( $( sed -ne '/^# START exclude/,/^# FINISH exclude/p' \
2002-01-29 20:33:49 +00:00
$BASH_COMPLETION | \
2001-12-20 16:12:44 +00:00
# read exclusion compspecs
(
while read line
do
2002-01-23 20:35:17 +00:00
# ignore compspecs that are commented out
if [ " ${ line # \# } " != " $line " ] ; then continue ; fi
2001-12-20 16:12:44 +00:00
line = ${ line %# START exclude* }
line = ${ line %# FINISH exclude* }
line = ${ line ##* \' }
2008-06-23 12:00:22 +02:00
list = ( " ${ list [@] } " $line )
2001-12-20 16:12:44 +00:00
done
2008-06-21 22:19:37 +02:00
echo " ${ list [@] } "
2001-12-20 16:12:44 +00:00
)
2001-12-20 18:26:49 +00:00
) )
2001-12-20 07:52:12 +00:00
# remove previous compspecs
2002-01-03 00:16:41 +00:00
if [ ${# list [@] } -gt 0 ] ; then
eval complete -r ${ list [@] }
# install new compspecs
2008-06-21 22:19:37 +02:00
eval complete -F _filedir_xspec $filenames " ${ list [@] } "
2002-01-03 00:16:41 +00:00
fi
2003-02-26 00:17:54 +00:00
unset list
2002-03-08 18:50:24 +00:00
# source completion directory definitions
2009-02-20 22:58:01 +01:00
if [ -d $BASH_COMPLETION_COMPAT_DIR -a -r $BASH_COMPLETION_COMPAT_DIR -a \
-x $BASH_COMPLETION_COMPAT_DIR ] ; then
for i in $BASH_COMPLETION_COMPAT_DIR /*; do
[ [ ${ i ##*/ } != @( *~| *.bak| *.swp| \# *\# | *.dpkg*| *.rpm@( orig| new| save) ) ] ] &&
2009-07-25 13:38:42 +03:00
[ \( -f $i -o -h $i \) -a -r $i ] && . $i
2009-02-20 22:58:01 +01:00
done
fi
2009-01-29 15:10:18 +01:00
if [ -d $BASH_COMPLETION_DIR -a -r $BASH_COMPLETION_DIR -a \
2009-02-20 22:58:01 +01:00
$BASH_COMPLETION_DIR != $BASH_COMPLETION_COMPAT_DIR -a \
2009-01-29 15:10:18 +01:00
-x $BASH_COMPLETION_DIR ] ; then
for i in $BASH_COMPLETION_DIR /*; do
[ [ ${ i ##*/ } != @( *~| *.bak| *.swp| \# *\# | *.dpkg*| *.rpm@( orig| new| save) ) ] ] &&
[ \( -f $i -o -h $i \) -a -r $i ] && . $i
done
fi
2002-04-02 23:37:51 +00:00
unset i
2001-12-05 16:32:24 +00:00
# source user completion file
2002-03-05 23:23:30 +00:00
[ $BASH_COMPLETION != ~/.bash_completion -a -r ~/.bash_completion ] \
&& . ~/.bash_completion
2001-07-08 23:14:13 +00:00
unset -f have
2009-05-21 08:44:52 +02:00
unset UNAME USERLAND default dirnames filenames have nospace bashdefault plusdirs
2002-03-29 02:35:42 +00:00
2008-06-23 12:03:06 +02:00
set $BASH_COMPLETION_ORIGINAL_V_VALUE
unset BASH_COMPLETION_ORIGINAL_V_VALUE