root/dotfiles/bash/kana/dot.bash.d/bash_completion

Revision 132, 210.7 kB (checked in by kana, 16 months ago)

dotfiles/bash/kana-bash.d, dotfiles/bash/kana-bash_profile, dotfiles/bash/kana-bashrc:
* Import from my repository.
dotfiles/screen/kana-screenrc:
* Import from my repository.
dotfiles/vim/kana-vim, dotfiles/vim/kana-vimrc:
* Import from my repository.

Line 
1#   bash_completion - programmable completion functions for bash 3.x
2#                     (backwards compatible with bash 2.05b)
3#
4#   $Id: bash_completion,v 1.872 2006/03/01 16:20:18 ianmacd Exp $
5#
6#   Copyright (C) Ian Macdonald <ian@caliban.org>
7#
8#   This program is free software; you can redistribute it and/or modify
9#   it under the terms of the GNU General Public License as published by
10#   the Free Software Foundation; either version 2, or (at your option)
11#   any later version.
12#
13#   This program is distributed in the hope that it will be useful,
14#   but WITHOUT ANY WARRANTY; without even the implied warranty of
15#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16#   GNU General Public License for more details.
17#
18#   You should have received a copy of the GNU General Public License
19#   along with this program; if not, write to the Free Software Foundation,
20#   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21#
22#   The latest version of this software can be obtained here:
23#
24#   http://www.caliban.org/bash/index.shtml#completion
25#
26#   RELEASE: 20060301
27
28[ -n "${BASH_COMPLETION_DEBUG:-}" ] && set -v || set +v
29
30# Alter the following to reflect the location of this file.
31#
32{
33  # These declarations must go within braces in order to be able to silence
34  # readonly variable errors.
35  BASH_COMPLETION="${BASH_COMPLETION:-/etc/bash_completion}"
36  BASH_COMPLETION_DIR="${BASH_COMPLETION_DIR:=/etc/bash_completion.d}"
37} 2>/dev/null || :
38readonly BASH_COMPLETION BASH_COMPLETION_DIR
39
40# Set a couple of useful vars
41#
42UNAME=$( uname -s )
43# strip OS type and version under Cygwin (e.g. CYGWIN_NT-5.1 => Cygwin)
44UNAME=${UNAME/CYGWIN_*/Cygwin}
45RELEASE=$( uname -r )
46
47# features supported by bash 2.05 and higher
48if [ ${BASH_VERSINFO[0]} -eq 2 ] && [[ ${BASH_VERSINFO[1]} > 04 ]] ||
49   [ ${BASH_VERSINFO[0]} -gt 2 ]; then
50        declare -r bash205=$BASH_VERSION 2>/dev/null || :
51        default="-o default"
52        dirnames="-o dirnames"
53        filenames="-o filenames"
54fi
55# features supported by bash 2.05b and higher
56if [ ${BASH_VERSINFO[0]} -eq 2 ] && [[ ${BASH_VERSINFO[1]} = "05b" ]] ||
57   [ ${BASH_VERSINFO[0]} -gt 2 ]; then
58        declare -r bash205b=$BASH_VERSION 2>/dev/null || :
59        nospace="-o nospace"
60fi
61# features supported by bash 3.0 and higher
62if [ ${BASH_VERSINFO[0]} -gt 2 ]; then
63        declare -r bash3=$BASH_VERSION 2>/dev/null || :
64        bashdefault="-o bashdefault"
65        plusdirs="-o plusdirs"
66fi
67
68# Turn on extended globbing and programmable completion
69shopt -s extglob progcomp
70
71# A lot of the following one-liners were taken directly from the
72# completion examples provided with the bash 2.04 source distribution
73
74# Make directory commands see only directories
75complete -d pushd
76
77# The following section lists completions that are redefined later
78# Do NOT break these over multiple lines.
79#
80# START exclude -- do NOT remove this line
81complete -f -X '!*.?(t)bz?(2)' bunzip2 bzcat bzcmp bzdiff bzegrep bzfgrep bzgrep
82complete -f -X '!*.@(zip|ZIP|jar|JAR|exe|EXE|pk3|war|wsz|ear|zargo|xpi|sxw|ott)' unzip zipinfo
83complete -f -X '*.Z' compress znew
84complete -f -X '!*.@(Z|gz|tgz|Gz|dz)' gunzip zcmp zdiff zcat zegrep zfgrep zgrep zless zmore
85complete -f -X '!*.Z' uncompress
86complete -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 display
87complete -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
88complete -f -X '!*.@(@(?(e)ps|?(E)PS|pdf|PDF)?(.gz|.GZ|.bz2|.BZ2|.Z))' gv ggv kghostview
89complete -f -X '!*.@(dvi|DVI)?(.@(gz|Z|bz2))' xdvi
90complete -f -X '!*.@(dvi|DVI)' dvips dviselect dvitype kdvi dvipdf advi
91complete -f -X '!*.@(pdf|PDF)' acroread gpdf xpdf kpdf
92complete -f -X '!*.@(@(?(e)ps|?(E)PS)?(.gz|.GZ)|pdf|PDF|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
93complete -f -X '!*.@(?(e)ps|?(E)PS)' ps2pdf
94complete -f -X '!*.texi*' makeinfo texi2html
95complete -f -X '!*.@(?(la)tex|?(LA)TEX|texi|TEXI|dtx|DTX|ins|INS)' tex latex slitex jadetex pdfjadetex pdftex pdflatex texi2dvi
96complete -f -X '!*.@(mp3|MP3)' mpg123 mpg321 madplay
97complete -f -X '!*.@(mp?(e)g|MP?(E)G|wma|avi|AVI|asf|vob|VOB|bin|dat|vcd|ps|pes|fli|viv|rm|ram|yuv|mov|MOV|qt|QT|wmv|mp3|MP3|ogg|OGG|ogm|OGM|mp4|MP4|wav|WAV|asx|ASX|mng|MNG)' xine aaxine fbxine kaffeine
98complete -f -X '!*.@(avi|asf|wmv)' aviplay
99complete -f -X '!*.@(rm?(j)|ra?(m)|smi?(l))' realplay
100complete -f -X '!*.@(mpg|mpeg|avi|mov|qt)' xanim
101complete -f -X '!*.@(ogg|OGG|m3u|flac|spx)' ogg123
102complete -f -X '!*.@(mp3|MP3|ogg|OGG|pls|m3u)' gqmpeg freeamp
103complete -f -X '!*.fig' xfig
104complete -f -X '!*.@(mid?(i)|MID?(I))' playmidi
105complete -f -X '!*.@(mid?(i)|MID?(I)|rmi|RMI|rcp|RCP|[gr]36|[GR]36|g18|G18|mod|MOD|xm|XM|it|IT|x3m|X3M)' timidity
106complete -f -X '*.@(o|so|so.!(conf)|a|t@(ar?(.@(Z|gz|bz?(2)))|gz|bz?(2))|rpm|zip|ZIP|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
107complete -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
108complete -f -X '!*.@(exe|EXE|com|COM|scr|SCR|exe.so)' wine
109complete -f -X '!*.@(zip|ZIP|z|Z|gz|GZ|tgz|TGZ)' bzme
110complete -f -X '!*.@(?([xX]|[sS])[hH][tT][mM]?([lL]))' netscape mozilla lynx opera galeon curl dillo elinks amaya
111complete -f -X '!*.@(sxw|stw|sxg|sgl|doc|dot|rtf|txt|htm|html|odt|ott|odm)' oowriter
112complete -f -X '!*.@(sxi|sti|pps|ppt|pot|odp|otp)' ooimpress
113complete -f -X '!*.@(sxc|stc|xls|xlw|xlt|csv|ods|ots)' oocalc
114complete -f -X '!*.@(sxd|std|sda|sdd|odg|otg)' oodraw
115complete -f -X '!*.@(sxm|smf|mml|odf)' oomath
116complete -f -X '!*.odb' oobase
117complete -f -X '!*.rpm' rpm2cpio
118# FINISH exclude -- do not remove this line
119
120# start of section containing compspecs that can be handled within bash
121
122# user commands see only users
123complete -u su usermod userdel passwd chage write chfn groups slay w
124
125# group commands see only groups
126[ -n "$bash205" ] && complete -g groupmod groupdel newgrp 2>/dev/null
127
128# bg completes with stopped jobs
129complete -A stopped -P '%' bg
130
131# other job commands
132complete -j -P '%' fg jobs disown
133
134# readonly and unset complete with shell variables
135complete -v readonly unset
136
137# set completes with set options
138complete -A setopt set
139
140# shopt completes with shopt options
141complete -A shopt shopt
142
143# helptopics
144complete -A helptopic help
145
146# unalias completes with aliases
147complete -a unalias
148
149# bind completes with readline bindings (make this more intelligent)
150complete -A binding bind
151
152# type and which complete on commands
153complete -c command type which
154
155# builtin completes on builtins
156complete -b builtin
157
158# start of section containing completion functions called by other functions
159
160# This function checks whether we have a given program on the system.
161# No need for bulky functions in memory if we don't.
162#
163have()
164{
165        unset -v have
166        PATH=$PATH:/sbin:/usr/sbin:/usr/local/sbin type $1 &>/dev/null &&
167                have="yes"
168}
169
170# use GNU sed if we have it, since its extensions are still used in our code
171#
172[ $UNAME != Linux ] && have gsed && alias sed=gsed
173
174# This function checks whether a given readline variable
175# is `on'.
176#
177_rl_enabled()
178{
179    [[ "$( bind -v )" = *$1+([[:space:]])on* ]]
180}
181
182
183# This function performs file and directory completion. It's better than
184# simply using 'compgen -f', because it honours spaces in filenames.
185# If passed -d, it completes only on directories. If passed anything else,
186# it's assumed to be a file glob to complete on.
187#
188_filedir()
189{
190        local IFS=$'\t\n' xspec #glob
191
192        _expand || return 0
193
194        #glob=$(set +o|grep noglob) # save glob setting.
195        #set -f          # disable pathname expansion (globbing)
196
197        if [ "${1:-}" = -d ]; then
198                COMPREPLY=( ${COMPREPLY[@]:-} $( compgen -d -- $cur ) )
199                #eval "$glob"    # restore glob setting.
200                return 0
201        fi
202
203        xspec=${1:+"!*.$1"}     # set only if glob passed in as $1
204        COMPREPLY=( ${COMPREPLY[@]:-} $( compgen -f -X "$xspec" -- "$cur" ) \
205                    $( compgen -d -- "$cur" ) )
206        #eval "$glob"    # restore glob setting.
207}
208
209# This function completes on signal names
210#
211_signals()
212{
213        local i
214
215        # standard signal completion is rather braindead, so we need
216        # to hack around to get what we want here, which is to
217        # complete on a dash, followed by the signal name minus
218        # the SIG prefix
219        COMPREPLY=( $( compgen -A signal SIG${cur#-} ))
220        for (( i=0; i < ${#COMPREPLY[@]}; i++ )); do
221                COMPREPLY[i]=-${COMPREPLY[i]#SIG}
222        done
223}
224
225# This function completes on configured network interfaces
226#
227_configured_interfaces()
228{
229        if [ -f /etc/debian_version ]; then
230                # Debian system
231                COMPREPLY=( $( sed -ne 's|^iface \([^ ]\+\).*$|\1|p' \
232                               /etc/network/interfaces ) )
233        elif [ -f /etc/SuSE-release ]; then
234                # SuSE system
235                COMPREPLY=( $( command ls \
236                        /etc/sysconfig/network/ifcfg-* | \
237                        sed -ne 's|.*ifcfg-\('$cur'.*\)|\1|p' ) )
238        elif [ -f /etc/pld-release ]; then
239                # PLD Linux
240                COMPREPLY=( $( command ls -B \
241                        /etc/sysconfig/interfaces | \
242                        sed -ne 's|.*ifcfg-\('$cur'.*\)|\1|p' ) )
243        else
244                # Assume Red Hat
245                COMPREPLY=( $( command ls \
246                        /etc/sysconfig/network-scripts/ifcfg-* | \
247                        sed -ne 's|.*ifcfg-\('$cur'.*\)|\1|p' ) )
248        fi
249}
250
251# This function completes on all available network interfaces
252# -a: restrict to active interfaces only
253# -w: restrict to wireless interfaces only
254#
255_available_interfaces()
256{
257        local cmd
258
259        if [ "${1:-}" = -w ]; then
260                cmd="iwconfig"
261        elif [ "${1:-}" = -a ]; then
262                cmd="ifconfig"
263        else
264                cmd="ifconfig -a"
265        fi
266
267        COMPREPLY=( $( eval $cmd 2>/dev/null | \
268                sed -ne 's|^\('$cur'[^[:space:][:punct:]]\{1,\}\).*$|\1|p') )
269}
270
271# This function expands tildes in pathnames
272#
273_expand()
274{
275        [ "$cur" != "${cur%\\}" ] && cur="$cur\\"
276
277        # expand ~username type directory specifications
278        if [[ "$cur" == \~*/* ]]; then
279                eval cur=$cur
280               
281        elif [[ "$cur" == \~* ]]; then
282                cur=${cur#\~}
283                COMPREPLY=( $( compgen -P '~' -u $cur ) )
284                return ${#COMPREPLY[@]}
285        fi
286}
287
288# This function completes on process IDs.
289# AIX and Solaris ps prefers X/Open syntax.
290[ $UNAME = SunOS -o $UNAME = AIX ] &&
291_pids()
292{
293        COMPREPLY=( $( compgen -W '$( command ps -efo pid | sed 1d )' -- $cur ))
294} ||
295_pids()
296{
297        COMPREPLY=( $( compgen -W '$( command ps axo pid | sed 1d )' -- $cur ) )
298}
299
300# This function completes on process group IDs.
301# AIX and SunOS prefer X/Open, all else should be BSD.
302[ $UNAME = SunOS -o $UNAME = AIX ] &&
303_pgids()
304{
305        COMPREPLY=( $( compgen -W '$( command ps -efo pgid | sed 1d )' -- $cur ))
306} ||
307_pgids()
308{
309        COMPREPLY=( $( compgen -W '$( command ps axo pgid | sed 1d )' -- $cur ))
310}
311
312# This function completes on user IDs
313#
314_uids()
315{
316        if type getent &>/dev/null; then
317            COMPREPLY=( $( getent passwd | \
318                            awk -F: '{if ($3 ~ /^'$cur'/) print $3}' ) )
319        elif type perl &>/dev/null; then
320            COMPREPLY=( $( compgen -W '$( perl -e '"'"'while (($uid) = (getpwent)[2]) { print $uid . "\n" }'"'"' )' -- $cur ) )
321        else
322            # make do with /etc/passwd
323            COMPREPLY=( $( awk 'BEGIN {FS=":"} {if ($3 ~ /^'$cur'/) print $3}'\
324                            /etc/passwd ) )
325        fi
326}
327
328# This function completes on group IDs
329#
330_gids()
331{
332        if type getent &>/dev/null; then
333            COMPREPLY=( $( getent group | \
334                            awk -F: '{if ($3 ~ /^'$cur'/) print $3}' ) )
335        elif type perl &>/dev/null; then
336            COMPREPLY=( $( compgen -W '$( perl -e '"'"'while (($gid) = (getgrent)[2]) { print $gid . "\n" }'"'"' )' -- $cur ) )
337        else
338            # make do with /etc/group
339            COMPREPLY=( $( awk 'BEGIN {FS=":"} {if ($3 ~ /^'$cur'/) print $3}'\
340                            /etc/group ) )
341        fi
342}
343
344# This function completes on services
345#
346_services()
347{
348        local sysvdir famdir
349        [ -d /etc/rc.d/init.d ] && sysvdir=/etc/rc.d/init.d || sysvdir=/etc/init.d
350        famdir=/etc/xinetd.d
351        COMPREPLY=( $( builtin echo $sysvdir/!(*.rpmsave|*.rpmorig|*~|functions)) )
352
353        if [ -d $famdir ]; then
354                COMPREPLY=( ${COMPREPLY[@]} $( builtin echo $famdir/!(*.rpmsave|*.rpmorig|*~)) )
355        fi
356
357        COMPREPLY=( $( compgen -W '${COMPREPLY[@]#@($sysvdir|$famdir)/}' -- $cur ) )
358}
359
360# This function complete on modules
361#
362_modules()
363{
364        local modpath
365        modpath=/lib/modules/$1
366        COMPREPLY=( $( command ls -R $modpath | \
367                        sed -ne 's/^\('$cur'.*\)\.k\?o\(\|.gz\)$/\1/p') )
368}
369
370# this function complete on user:group format
371#
372_usergroup()
373{
374        local IFS=$'\n'
375        cur=${cur//\\\\ / }
376        if [[ $cur = *@(\\:|.)* ]] && [ -n "$bash205" ]; then
377                user=${cur%%*([^:.])}
378                COMPREPLY=( $(compgen -P ${user/\\\\} -g -- ${cur##*[.:]}) )
379        elif [[ $cur = *:* ]] && [ -n "$bash205" ]; then
380                COMPREPLY=( $( compgen -g -- ${cur##*[.:]} ) )
381        else
382                COMPREPLY=( $( compgen -S : -u -- $cur ) )
383        fi
384}
385
386# this function count the number of mandatory args
387#
388_count_args()
389{
390        args=1
391        for (( i=1; i < COMP_CWORD; i++ )); do
392                if [[ "${COMP_WORDS[i]}" != -* ]]; then
393                        args=$(($args+1))
394                fi
395        done
396}
397
398# start of section containing completion functions for bash built-ins
399
400# bash alias completion
401#
402_alias()
403{
404        local cur
405
406        COMPREPLY=()
407        cur=${COMP_WORDS[$COMP_CWORD]}
408
409        case "$COMP_LINE" in
410        *[^=])
411                COMPREPLY=( $( compgen -A alias -S '=' -- $cur ) )
412                ;;
413        *=)
414                COMPREPLY=( "$( alias ${cur%=} 2>/dev/null | \
415                             sed -e 's|^alias '$cur'\(.*\)$|\1|' )" )
416                ;;
417        esac
418}
419complete -F _alias $nospace alias
420
421# bash export completion
422#
423_export()
424{
425        local cur
426
427        COMPREPLY=()
428        cur=${COMP_WORDS[$COMP_CWORD]}
429
430        case "$COMP_LINE" in
431        *=\$*)
432                COMPREPLY=( $( compgen -v -P '$' -- ${cur#*=\$} ) )
433                ;;
434        *[^=])
435                COMPREPLY=( $( compgen -v -S '=' -- $cur ) )
436                ;;
437        *=)
438                COMPREPLY=( "$( eval echo -n \"$`echo ${cur%=}`\" |
439                        ( echo -n \'
440                          sed -e 's/'\''/'\''\\\'\'''\''/g'
441                          echo -n \' ) )" )
442                ;;
443        esac
444}
445complete -F _export $default $nospace export
446
447# bash shell function completion
448#
449_function()
450{
451        local cur prev
452
453        COMPREPLY=()
454        cur=${COMP_WORDS[COMP_CWORD]}
455        prev=${COMP_WORDS[COMP_CWORD-1]}
456
457        if [[ $1 == @(declare|typeset) ]]; then
458                if [ "$prev" = -f ]; then
459                        COMPREPLY=( $( compgen -A function -- $cur ) )
460                elif [[ "$cur" == -* ]]; then
461                        COMPREPLY=( $( compgen -W '-a -f -F -i -r -x -p' -- \
462                                       $cur ) )
463                fi
464        elif [ $COMP_CWORD -eq 1 ]; then
465                COMPREPLY=( $( compgen -A function -- $cur ) )
466        else
467                COMPREPLY=( "() $( type -- ${COMP_WORDS[1]} | sed -e 1,2d )" )
468        fi
469}
470complete -F _function function declare typeset
471
472# bash complete completion
473#
474_complete()
475{
476        local cur prev options
477
478        COMPREPLY=()
479        cur=${COMP_WORDS[COMP_CWORD]}
480        prev=${COMP_WORDS[COMP_CWORD-1]}
481
482        case $prev in
483                -o)
484                        options="default dirnames filenames"
485                        [ -n "$bash205b" ] && options="$options nospace"
486                        [ -n "$bash3" ] && options="$options bashdefault plusdirs"
487                        COMPREPLY=( $( compgen -W "$options" -- $cur ) )
488                        return 0
489                        ;;
490
491                -A)
492                        COMPREPLY=( $( compgen -W 'alias arrayvar binding \
493                                builtin command directory disabled enabled \
494                                export file function group helptopic hostname \
495                                job keyword running service setopt shopt \
496                                signal stopped user variable' -- $cur ) )
497                        return 0
498                        ;;
499
500                -C)
501                        COMPREPLY=( $( compgen -A command -- $cur ) )
502                        return 0
503                        ;;
504                -F)
505                        COMPREPLY=( $( compgen -A function -- $cur ) )
506                        return 0
507                        ;;
508                -@(p|r))
509                        COMPREPLY=( $( complete -p | sed -e 's|.* ||' | \
510                                        grep "^$cur" ) )
511                        return 0
512                        ;;
513
514        esac
515
516        if [[ "$cur" == -* ]]; then
517                # relevant options completion
518                options="-a -b -c -d -e -f -g -j -k -s -v -u -A -G -W -P -S -X -F -C"
519                [ -n "$bash205" ] && options="$options -o"
520                COMPREPLY=( $( compgen -W "$options" -- $cur ) )
521        else
522                COMPREPLY=( $( compgen -A command -- $cur ) )
523        fi
524}
525complete -F _complete complete
526
527# start of section containing completion functions for external programs
528
529# a little help for FreeBSD ports users
530[ $UNAME = FreeBSD ] && complete -W 'index search fetch fetch-list \
531        extract patch configure build install reinstall \
532        deinstall clean clean-depends kernel buildworld' make
533
534# This completes on a list of all available service scripts for the
535# 'service' command and/or the SysV init.d directory, followed by
536# that script's available commands
537#
538{ have service || [ -d /etc/init.d/ ]; } &&
539_service()
540{
541        local cur sysvdir
542
543        COMPREPLY=()
544        prev=${COMP_WORDS[COMP_CWORD-1]}
545        cur=${COMP_WORDS[COMP_CWORD]}
546
547        # don't complete for things like killall, ssh and mysql if it's
548        # the standalone command, rather than the init script
549        [[ ${COMP_WORDS[0]} != @(*init.d/!(functions|~)|service) ]] && return 0
550
551        # don't complete past 2nd token
552        [ $COMP_CWORD -gt 2 ] && return 0
553
554        [ -d /etc/rc.d/init.d ] && sysvdir=/etc/rc.d/init.d \
555                                || sysvdir=/etc/init.d
556
557        if [[ $COMP_CWORD -eq 1 ]] && [[ $prev == "service" ]]; then
558                _services
559        else
560                COMPREPLY=( $( compgen -W '`sed -ne "y/|/ /; \
561                                s/^.*Usage.*{\(.*\)}.*$/\1/p" \
562                                $sysvdir/${prev##*/} 2>/dev/null`' -- $cur ) )
563        fi
564
565        return 0
566} &&
567complete -F _service service
568[ -d /etc/init.d/ ] && complete -F _service $default \
569        $(for i in /etc/init.d/*; do echo ${i##*/}; done)
570
571# chown(1) completion
572#
573_chown()
574{
575        local cur
576        cur=${COMP_WORDS[COMP_CWORD]}
577
578        # options completion
579        if [[ "$cur" == -* ]]; then
580                COMPREPLY=( $( compgen -W '-c -h -f -R -v --changes \
581                --dereference --no-dereference --from= --silent --quiet \
582                --reference= --recursive --verbose --help --version' -- $cur ) )
583        else
584                _count_args
585
586                case $args in
587                        1)
588                                _usergroup
589                                ;;
590                        *)
591                                _filedir
592                                ;;
593                esac
594        fi
595}
596complete -F _chown $filenames chown
597
598# chgrp(1) completion
599#
600_chgrp()
601{
602        local cur prev
603
604        COMPREPLY=()
605        cur=${COMP_WORDS[COMP_CWORD]}
606        cur=${cur//\\\\/}
607        prev=${COMP_WORDS[COMP_CWORD-1]}
608
609        # options completion
610        if [[ "$cur" == -* ]]; then
611                COMPREPLY=( $( compgen -W '-c -h -f -R -v --changes \
612                --dereference --no-dereference --silent --quiet \
613                --reference= --recursive --verbose --help --version' -- $cur ) )
614                return 0
615        fi
616
617        # first parameter on line or first since an option?
618        if [ $COMP_CWORD -eq 1 ] && [[ "$cur" != -* ]] || \
619           [[ "$prev" == -* ]] && [ -n "$bash205" ]; then
620                local IFS=$'\n'
621                COMPREPLY=( $( compgen -g $cur 2>/dev/null ) )
622        else
623                _filedir || return 0
624        fi
625
626        return 0
627}
628complete -F _chgrp $filenames chgrp
629
630# umount(8) completion. This relies on the mount point being the third
631# space-delimited field in the output of mount(8)
632#
633_umount()
634{
635        local cur
636
637        COMPREPLY=()
638        cur=${COMP_WORDS[COMP_CWORD]}
639
640        COMPREPLY=( $( compgen -W '$( mount | cut -d" " -f 3 )' -- $cur ) )
641
642        return 0
643}
644complete -F _umount $dirnames umount
645
646# mount(8) completion. This will pull a list of possible mounts out of
647# /etc/{,v}fstab, unless the word being completed contains a ':', which
648# would indicate the specification of an NFS server. In that case, we
649# query the server for a list of all available exports and complete on
650# that instead.
651#
652_mount()
653{       local cur i sm host
654
655        COMPREPLY=()
656        cur=${COMP_WORDS[COMP_CWORD]}
657        [[ "$cur" == \\ ]] && cur="/"
658
659        for i in {,/usr}/{,s}bin/showmount; do [ -x $i ] && sm=$i && break; done
660
661        if [ -n "$sm" ] && [[ "$cur" == *:* ]]; then
662                COMPREPLY=( $( $sm -e ${cur%%:*} | sed 1d | \
663                               grep ^${cur#*:} | awk '{print $1}' ) )
664        elif [[ "$cur" == //* ]]; then
665                host=${cur#//}
666                host=${host%%/*}
667                if [ -n "$host" ]; then
668                        COMPREPLY=( $( compgen -W "$( echo $( smbclient -d 0 -NL $host 2>/dev/null|
669                        sed -ne '/^['"$'\t '"']*Sharename/,/^$/p' |
670                        sed -ne '3,$s|^[^A-Za-z]*\([^'"$'\t '"']*\).*$|//'$host'/\1|p' ) )" -- "$cur" ) )
671                fi
672        elif [ -r /etc/vfstab ]; then
673                # Solaris
674                COMPREPLY=( $( awk '! /^[ \t]*#/ {if ($3 ~ /\//) print $3}' \
675                                /etc/vfstab | grep "^$cur" ) )
676        elif [ ! -e /etc/fstab ]; then
677                # probably Cygwin
678                COMPREPLY=( $( mount | awk '! /^[ \t]*#/ {if ($3 ~ /\//) print $3}' \
679                                 | grep "^$cur" ) )
680        else
681                # probably Linux
682                COMPREPLY=( $( awk '! /^[ \t]*#/ {if ($2 ~ /\//) print $2}' \
683                                /etc/fstab | grep "^$cur" ) )
684        fi
685
686        return 0
687}
688complete -F _mount $default $filenames mount
689
690# Linux rmmod(8) completion. This completes on a list of all currently
691# installed kernel modules.
692#
693have rmmod && {
694_rmmod()
695{
696        local cur
697
698        COMPREPLY=()
699        cur=${COMP_WORDS[COMP_CWORD]}
700
701        COMPREPLY=( $( /sbin/lsmod | \
702                  awk '{if (NR != 1 && $1 ~ /^'$cur'/) print $1}' 2>/dev/null ))
703        return 0
704}
705complete -F _rmmod rmmod
706
707# Linux insmod(8), modprobe(8) and modinfo(8) completion. This completes on a
708# list of all available modules for the version of the kernel currently
709# running.
710#
711_insmod()
712{
713        local cur prev modpath
714
715        COMPREPLY=()
716        cur=${COMP_WORDS[COMP_CWORD]}
717        prev=${COMP_WORDS[COMP_CWORD-1]}
718
719        # behave like lsmod for modprobe -r
720        if [ $1 = "modprobe" ] &&
721           [ "${COMP_WORDS[1]}" = "-r" ]; then
722                COMPREPLY=( $( /sbin/lsmod | \
723                                awk '{if (NR != 1 && $1 ~ /^'$cur'/) print $1}' ) )
724                return 0
725        fi
726
727        # do filename completion if we're giving a path to a module
728        if [[ "$cur" == */* ]]; then
729                _filedir '@(?(k)o?(.gz))'
730                return 0
731        fi
732
733        if [ $COMP_CWORD -gt 1 ] &&
734           [[ "${COMP_WORDS[COMP_CWORD-1]}" != -* ]]; then
735                # do module parameter completion
736                COMPREPLY=( $( /sbin/modinfo -p ${COMP_WORDS[1]} 2>/dev/null | \
737                       awk '{if ($1 ~ /^parm:/ && $2 ~ /^'$cur'/) { print $2 } \
738                        else if ($1 !~ /:/ && $1 ~ /^'$cur'/) { print $1 }}' ) )
739        else
740                _modules $(uname -r)
741        fi
742
743        return 0
744}
745complete -F _insmod $filenames insmod modprobe modinfo
746}
747
748# man(1) completion
749#
750[ $UNAME = GNU -o $UNAME = Linux -o $UNAME = Darwin \
751  -o $UNAME = FreeBSD -o $UNAME = SunOS -o $UNAME = Cygwin \
752  -o $UNAME = OpenBSD ] &&
753_man()
754{
755        local cur prev sect manpath UNAME
756
757        COMPREPLY=()
758        cur=${COMP_WORDS[COMP_CWORD]}
759        prev=${COMP_WORDS[COMP_CWORD-1]}
760
761        _expand || return 0
762
763        # default completion if parameter contains /
764        if [[ "$cur" == */* ]]; then
765                _filedir
766                return 0
767        fi
768
769        UNAME=$( uname -s )
770        # strip OS type and version under Cygwin
771        UNAME=${UNAME/CYGWIN_*/Cygwin}
772        if [ $UNAME = GNU -o $UNAME = Linux -o $UNAME = FreeBSD \
773             -o $UNAME = Cygwin ]; then
774                manpath=$( manpath 2>/dev/null || command man --path )
775        else
776                manpath=$MANPATH
777        fi
778
779        if [ -z "$manpath" ]; then
780                COMPREPLY=( $( compgen -c -- $cur ) )
781                return 0
782        fi
783
784        # determine manual section to search
785        [[ "$prev" == [0-9ln] ]] && sect=$prev || sect='*'
786
787        manpath=$manpath:
788        if [ -n "$cur" ]; then
789                manpath="${manpath//://*man$sect/$cur* } ${manpath//://*cat$sect/$cur* }"
790        else
791                manpath="${manpath//://*man$sect/ } ${manpath//://*cat$sect/ }"
792        fi
793               
794        # redirect stderr for when path doesn't exist
795        COMPREPLY=( $( eval command ls "$manpath" 2>/dev/null ) )
796        # weed out directory path names and paths to man pages
797        COMPREPLY=( ${COMPREPLY[@]##*/?(:)} )
798        # strip suffix from man pages
799        COMPREPLY=( ${COMPREPLY[@]%.@(gz|bz2)} )
800        COMPREPLY=( $( compgen -W '${COMPREPLY[@]%.*}' -- "${cur//\\\\/}" ) )
801
802        [[ "$prev" != [0-9ln] ]] && _filedir '[0-9ln]'
803
804        return 0
805}
806[ $UNAME = GNU -o $UNAME = Linux -o $UNAME = Darwin \
807  -o $UNAME = FreeBSD -o $UNAME = SunOS -o $UNAME = Cygwin \
808  -o $UNAME = OpenBSD ] && \
809complete -F _man $filenames man
810
811# renice(8) completion
812#
813_renice()
814{
815        local command cur curopt i
816
817        COMPREPLY=()
818        cur=${COMP_WORDS[COMP_CWORD]}
819        command=$1
820
821        i=0
822        # walk back through command line and find last option
823        while [ $i -le $COMP_CWORD -a ${#COMPREPLY[@]} -eq 0 ]; do
824                curopt=${COMP_WORDS[COMP_CWORD-$i]}
825                case "$curopt" in
826                -u)
827                        COMPREPLY=( $( compgen -u -- $cur ) )
828                        ;;
829                -g)
830                        _pgids
831                        ;;
832                -p|$command)
833                        _pids
834                        ;;
835                esac
836                i=$(( ++i ))
837        done
838}
839complete -F _renice renice
840
841# kill(1) completion
842#
843_kill()
844{
845        local cur
846
847        COMPREPLY=()
848        cur=${COMP_WORDS[COMP_CWORD]}
849
850        if [ $COMP_CWORD -eq 1 ] && [[ "$cur" == -* ]]; then
851                # return list of available signals
852                _signals
853        else
854                # return list of available PIDs
855                _pids
856        fi
857}
858complete -F _kill kill
859
860# Linux and FreeBSD killall(1) completion.
861#
862[ $UNAME = Linux -o $UNAME = FreeBSD ] &&
863_killall()
864{
865        local cur
866
867        COMPREPLY=()
868        cur=${COMP_WORDS[COMP_CWORD]}
869
870        if [ $COMP_CWORD -eq 1 ] && [[ "$cur" == -* ]]; then
871                _signals
872        else
873                COMPREPLY=( $( compgen -W '$( command ps axo command | \
874                              sed -ne "1d; s/^\[\?\([^-][^] ]*\).*$/\1/p" | \
875                              sed -e "s/.*\///" )' -- $cur ) )
876        fi
877
878        return 0
879}
880[ $UNAME = Linux -o $UNAME = FreeBSD ] && complete -F _killall killall pkill
881
882# Linux and FreeBSD pgrep(1) completion.
883#
884[ $UNAME = Linux -o $UNAME = FreeBSD ] &&
885_pgrep()
886{
887        local cur
888
889        COMPREPLY=()
890        cur=${COMP_WORDS[COMP_CWORD]}
891
892        COMPREPLY=( $( compgen -W '$( command ps axo command | \
893                      sed -ne "1d; s/^\[\?\([^-][^] ]*\).*$/\1/p" | \
894                      sed -e "s/.*\///" )' -- $cur ) )
895
896        return 0
897}
898[ $UNAME = Linux -o $UNAME = FreeBSD ] && complete -F _pgrep pgrep
899# Linux pidof(8) completion.
900[ $UNAME = Linux ] && complete -F _pgrep pidof
901
902# GNU find(1) completion. This makes heavy use of ksh style extended
903# globs and contains Linux specific code for completing the parameter
904# to the -fstype option.
905#
906_find()
907{
908        local cur prev i exprfound onlyonce
909
910        COMPREPLY=()
911        cur=${COMP_WORDS[COMP_CWORD]}
912        prev=${COMP_WORDS[COMP_CWORD-1]}
913
914        case "$prev" in
915        -@(max|min)depth)
916                COMPREPLY=( $( compgen -W '0 1 2 3 4 5 6 7 8 9' -- $cur ) )
917                return 0
918                ;;
919        -?(a|c)newer|-fls|-fprint?(0|f)|-?(i)?(l)name)
920                _filedir
921                return 0
922                ;;
923        -fstype)
924                # this is highly non-portable
925                [ -e /proc/filesystems ] &&
926                COMPREPLY=( $( cut -d$'\t' -f 2 /proc/filesystems | \
927                                grep "^$cur" ) )
928                return 0
929                ;;
930        -gid)
931                _gids
932                return 0
933                ;;
934        -group)
935                if [ -n "$bash205" ]; then
936                        COMPREPLY=( $( compgen -g -- $cur 2>/dev/null) )
937                fi
938                return 0
939                ;;
940        -?(x)type)
941                COMPREPLY=( $( compgen -W 'b c d p f l s' -- $cur ) )
942                return 0
943                ;;
944        -uid)
945                _uids
946                return 0
947                ;;
948        -user)
949                COMPREPLY=( $( compgen -u -- $cur ) )
950                return 0
951                ;;
952        -exec|-ok)
953                COMP_WORDS=(COMP_WORDS[0] $cur)
954                COMP_CWORD=1
955                _command
956                return 0
957                ;;
958        -[acm]min|-[acm]time|-?(i)?(l)name|-inum|-?(i)path|-?(i)regex| \
959        -links|-perm|-size|-used|-printf)
960                # do nothing, just wait for a parameter to be given
961                return 0
962                ;;
963        esac
964
965        _expand || return 0
966
967        # set exprfound to 1 if there is already an expression present
968        for i in ${COMP_WORDS[@]}; do
969                [[ "$i" = [-\(\),\!]* ]] && exprfound=1 && break
970        done
971
972        # handle case where first parameter is not a dash option
973        if [ "$exprfound" != 1 ] && [[ "$cur" != [-\(\),\!]* ]]; then
974                _filedir -d
975                return 0
976        fi
977
978        # complete using basic options
979        COMPREPLY=( $( compgen -W '-daystart -depth -follow -help -maxdepth \
980                        -mindepth -mount -noleaf -version -xdev -amin -anewer \
981                        -atime -cmin -cnewer -ctime -empty -false -fstype \
982                        -gid -group -ilname -iname -inum -ipath -iregex \
983                        -links -lname -mmin -mtime -name -newer -nouser \
984                        -nogroup -perm -regex -size -true -type -uid -used \
985                        -user -xtype -exec -fls -fprint -fprint0 -fprintf -ok \
986                        -print -print0 -printf -prune -ls' -- $cur ) )
987
988        # this removes any options from the list of completions that have
989        # already been specified somewhere on the command line, as long as
990        # these options can only be used once (in a word, "options", in
991        # opposition to "tests" and "actions", as in the find(1) manpage).
992        onlyonce=' -daystart -depth -follow -help -maxdepth -mindepth -mount \
993                   -noleaf -version -xdev '
994        COMPREPLY=( $( echo "${COMP_WORDS[@]}" | \
995                       (while read -d ' ' i; do
996                            [ "$i" == "" ] ||
997                            [ "${onlyonce/ ${i%% *} / }" == "$onlyonce" ] &&
998                            continue
999                            # flatten array with spaces on either side,
1000                            # otherwise we cannot grep on word boundaries of
1001                            # first and last word
1002                            COMPREPLY=" ${COMPREPLY[@]} "
1003                            # remove word from list of completions
1004                            COMPREPLY=( ${COMPREPLY/ ${i%% *} / } )
1005                        done
1006                        echo ${COMPREPLY[@]})
1007                  ) )
1008       
1009        _filedir
1010       
1011        return 0
1012}
1013complete -F _find $filenames find
1014
1015# Linux iwconfig(8) completion
1016#
1017[ $UNAME = Linux ] && have iwconfig &&
1018_iwconfig()
1019{
1020        local cur prev
1021
1022        COMPREPLY=()
1023        cur=${COMP_WORDS[COMP_CWORD]}
1024        prev=${COMP_WORDS[COMP_CWORD-1]}
1025       
1026        case $prev in
1027                mode)
1028                        COMPREPLY=( $( compgen -W 'managed ad-hoc master \
1029                                repeater secondary monitor' -- $cur ) )
1030                        return 0
1031                        ;;
1032                essid)
1033                        COMPREPLY=( $( compgen -W 'on off any' -- $cur ) )
1034                        if [ -n "${COMP_IWLIST_SCAN:-}" ]; then
1035                                COMPREPLY=( ${COMPREPLY[@]:-} \
1036                                        $( iwlist ${COMP_WORDS[1]} scan | \
1037                                        awk -F '"' '/ESSID/ {print $2}' | \
1038                                        grep "^$cur" ))
1039                        fi
1040                        return 0
1041                        ;;
1042                nwid)
1043                        COMPREPLY=( $( compgen -W 'on off' -- $cur ) )
1044                        return 0
1045                        ;;
1046                channel)
1047                        COMPREPLY=( $( iwlist ${COMP_WORDS[1]} channel | \
1048                                awk '/^[[:space:]]*Channel/ {print $2}' | \
1049                                grep "^$cur" ) )
1050                        return 0
1051                        ;;
1052
1053                freq)
1054                        COMPREPLY=( $( iwlist ${COMP_WORDS[1]} channel | \
1055                                awk '/^[[:space:]]*Channel/ {print $4"G"}' | \
1056                                grep "^$cur" ) )
1057                        return 0
1058                        ;;
1059                ap)
1060                        COMPREPLY=( $( compgen -W 'on off any' -- $cur ) )
1061                        if [ -n "${COMP_IWLIST_SCAN:-}" ]; then
1062                                COMPREPLY=( ${COMPREPLY[@]:-} \
1063                                        $( iwlist ${COMP_WORDS[1]} scan | \
1064                                        awk -F ': ' '/Address/ {print $2}' | \
1065                                        grep "^$cur" ) )
1066                        fi
1067                        return 0
1068                        ;;
1069                rate)
1070                        COMPREPLY=( $( compgen -W 'auto fixed' -- $cur ) )
1071                        COMPREPLY=( ${COMPREPLY[@]:-} \
1072                                $( iwlist ${COMP_WORDS[1]} rate | \
1073                                awk '/^[[:space:]]*[0-9]/ {print $1"M"}' | \
1074                                grep "^$cur" ) )
1075                        return 0
1076                        ;;
1077                rts)
1078                        COMPREPLY=( $( compgen -W 'auto fixed off' -- $cur ) )
1079                        return 0
1080                        ;;
1081                frag)
1082                        COMPREPLY=( $( compgen -W 'auto fixed off' -- $cur ) )
1083                        return 0
1084                        ;;
1085                key)
1086                        COMPREPLY=( $( compgen -W 'off on open restricted' -- $cur ) )
1087                        return 0
1088                        ;;
1089                enc)
1090                        COMPREPLY=( $( compgen -W 'off on open restricted' -- $cur ) )
1091                        return 0
1092                        ;;
1093                power)
1094                        COMPREPLY=( $( compgen -W 'period timeout off on' -- $cur ) )
1095                        return 0
1096                        ;;
1097                txpower)
1098                        COMPREPLY=( $( compgen -W 'off on auto' -- $cur ) )
1099                        return 0
1100                        ;;
1101                retry)
1102                        COMPREPLY=( $( compgen -W 'limit lifetime' -- $cur ) )
1103                        return 0
1104                        ;;
1105        esac
1106
1107        if [ $COMP_CWORD -eq 1 ]; then
1108                if [[ "$cur" == -* ]]; then
1109                        COMPREPLY=( $( compgen -W '--help --version' -- $cur ) )
1110                else
1111                        _available_interfaces -w
1112                fi
1113        else
1114                COMPREPLY=( $( compgen -W 'essid nwid mode freq channel sens mode \
1115                        ap nick rate rts frag enc key power txpower commit' -- $cur ) )
1116        fi
1117
1118} &&
1119complete -F _iwconfig iwconfig
1120
1121# Linux iwlist(8) completion
1122#
1123[ $UNAME = Linux ] && have iwlist &&
1124_iwlist()
1125{
1126        local cur prev
1127
1128        COMPREPLY=()
1129        cur=${COMP_WORDS[COMP_CWORD]}
1130        prev=${COMP_WORDS[COMP_CWORD-1]}
1131       
1132        if [ $COMP_CWORD -eq 1 ]; then
1133                if [[ "$cur" == -* ]]; then
1134                        COMPREPLY=( $( compgen -W '--help --version' -- $cur ) )
1135                else
1136                        _available_interfaces -w
1137                fi
1138        else
1139                COMPREPLY=( $( compgen -W 'scan scanning freq frequency \
1140                        channel rate bit bitrate key enc encryption power \
1141                        txpower retry ap accesspoint peers event' -- $cur ) )
1142        fi
1143} &&
1144complete -F _iwlist iwlist
1145
1146# Linux iwspy(8) completion
1147#
1148[ $UNAME = Linux ] && have iwspy &&
1149_iwspy()
1150{
1151        local cur
1152
1153        COMPREPLY=()
1154        cur=${COMP_WORDS[COMP_CWORD]}
1155
1156        if [ $COMP_CWORD -eq 1 ]; then
1157                if [[ "$cur" == -* ]]; then
1158                        COMPREPLY=( $( compgen -W '--help --version' -- $cur ) )
1159                else
1160                        _available_interfaces -w
1161                fi
1162        else
1163                COMPREPLY=( $( compgen -W 'setthr getthr off' -- $cur ) )
1164        fi
1165} &&
1166complete -F _iwspy iwspy
1167
1168# Linux iwpriv(8) completion
1169#
1170[ $UNAME = Linux ] && have iwpriv &&
1171_iwpriv()
1172{
1173        local cur prev
1174
1175        COMPREPLY=()
1176        cur=${COMP_WORDS[COMP_CWORD]}
1177        prev=${COMP_WORDS[COMP_CWORD-1]}
1178
1179        case "$prev" in
1180                roam)
1181                        COMPREPLY=( $( compgen -W 'on off' -- $cur ) )
1182                        return 0
1183                        ;;
1184                port)
1185                        COMPREPLY=( $( compgen -W 'ad-hoc managed' -- $cur ) )
1186                        return 0
1187                        ;;
1188        esac
1189
1190        if [ $COMP_CWORD -eq 1 ]; then
1191                if [[ "$cur" == -* ]]; then
1192                        COMPREPLY=( $( compgen -W '--help --version' -- $cur ) )
1193                else
1194                        _available_interfaces -w
1195                fi
1196        else
1197                COMPREPLY=( $( compgen -W '--all roam port' -- $cur ) )
1198        fi
1199} &&
1200complete -F _iwpriv iwpriv
1201
1202# RedHat & Debian GNU/Linux if{up,down} completion
1203#
1204[ $UNAME = Linux ] && { have ifup || have ifdown; } &&
1205_ifupdown()
1206{
1207        local cur
1208
1209        COMPREPLY=()
1210        cur=${COMP_WORDS[COMP_CWORD]}
1211
1212        if [ $COMP_CWORD -eq 1 ]; then
1213                _configured_interfaces
1214       fi
1215
1216       return 0
1217} &&
1218complete -F _ifupdown ifup ifdown
1219[ $UNAME = Linux ] && have ifstatus && complete -F _ifupdown ifstatus
1220
1221# Linux ipsec(8) completion (for FreeS/WAN)
1222#
1223[ $UNAME = Linux ] && have ipsec &&
1224_ipsec()
1225{
1226        local cur
1227
1228        COMPREPLY=()
1229        cur=${COMP_WORDS[COMP_CWORD]}
1230
1231       
1232        if [ $COMP_CWORD -eq 1 ]; then
1233                COMPREPLY=( $( compgen -W 'auto barf eroute klipsdebug look \
1234                                           manual pluto ranbits rsasigkey \
1235                                           setup showdefaults showhostkey spi \
1236                                           spigrp tncfg whack' -- $cur ) )
1237                return 0
1238        fi
1239
1240        case ${COMP_WORDS[1]} in
1241        auto)
1242                COMPREPLY=( $( compgen -W '--asynchronous --up --add --delete \
1243                                           --replace --down --route --unroute \
1244                                           --ready --status --rereadsecrets' \
1245                                        -- $cur ) )
1246                ;;
1247        manual)
1248                COMPREPLY=( $( compgen -W '--up --down --route --unroute \
1249                                           --union' -- $cur ) )
1250                ;;
1251        ranbits)
1252                COMPREPLY=( $( compgen -W '--quick --continuous --bytes' \
1253                                          -- $cur ) )
1254                ;;
1255        setup)
1256                COMPREPLY=( $( compgen -W '--start --stop --restart' -- $cur ) )
1257                ;;
1258
1259        *)
1260                ;;
1261        esac
1262
1263        return 0
1264} &&
1265complete -F _ipsec ipsec
1266
1267# Postfix completion.
1268#
1269have postfix && {
1