Bash-4.3 distribution sources and documentation
[platform/upstream/bash.git] / examples / complete / complete-examples
1 #
2 # Completion examples
3 #
4 #  Chet Ramey <chet.ramey@case.edu>
5 #
6 #  Copyright 2002 Chester Ramey
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 #   TThis 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 #
23 # This encapsulates the default bash completion code
24 # call with the word to be completed as $1
25 #
26 # Since programmable completion does not use the bash default completions
27 # or the readline default of filename completion when the compspec does
28 # not generate any matches, this may be used as a `last resort' in a
29 # completion function to mimic the default bash completion behavior.
30 #
31 _bash_def_completion ()
32 {
33         local h t
34         COMPREPLY=()
35
36         # command substitution
37         if [[ "$1" == \$\(* ]]; then
38                 t=${1#??}
39                 COMPREPLY=( $(compgen -c -P '$(' $t) )
40         fi
41         # variables with a leading `${'
42         if [ ${#COMPREPLY[@]} -eq 0 ] && [[ "$1" == \$\{* ]]; then
43                 t=${1#??}
44                 COMPREPLY=( $(compgen -v -P '${' -S '}' $t) )
45         fi
46         # variables with a leading `$'
47         if [ ${#COMPREPLY[@]} -eq 0 ] && [[ "$1" == \$* ]]; then
48                 t=${1#?}
49                 COMPREPLY=( $(compgen -v -P '$' $t ) )
50         fi
51         # username expansion
52         if [ ${#COMPREPLY[@]} -eq 0 ] && [[ "$1" == ~* ]] && [[ "$1" != */* ]]; then
53                 t=${1#?}
54                 COMPREPLY=( $( compgen -u -P '~' $t ) )
55         fi
56         # hostname
57         if [ ${#COMPREPLY[@]} -eq 0 ] && [[ "$1" == *@* ]]; then
58                 h=${1%%@*}
59                 t=${1#*@}
60                 COMPREPLY=( $( compgen -A hostname -P "${h}@" $t ) )
61         fi
62         # glob pattern
63         if [ ${#COMPREPLY[@]} -eq 0 ]; then
64                 # sh-style glob pattern
65                 if [[ $1 == *[*?[]* ]]; then
66                         COMPREPLY=( $( compgen -G "$1" ) )
67                 # ksh-style extended glob pattern - must be complete
68                 elif shopt -q extglob && [[ $1 == *[?*+\!@]\(*\)* ]]; then
69                         COMPREPLY=( $( compgen -G "$1" ) )
70                 fi
71         fi
72
73         # final default is filename completion
74         if [ ${#COMPREPLY[@]} -eq 0 ]; then
75                 COMPREPLY=( $(compgen -f "$1" ) )
76         fi
77 }
78
79
80 # Return 1 if $1 appears to contain a redirection operator.  Handles backslash
81 # quoting (barely).
82 #
83 _redir_op()
84 {
85         case "$1" in
86         *\\'[\<\>]'*)   return 1;;
87         *[\<\>]*)       return 0;;
88         *)              return 1;;
89         esac
90 }
91
92
93 # _redir_test tests the current word ($1) and the previous word ($2) for
94 # redirection operators and does filename completion on the current word
95 # if either one contains a redirection operator
96 _redir_test()
97 {
98         if _redir_op "$1" ; then
99                 COMPREPLY=( $( compgen -f "$1" ) )
100                 return 0
101         elif _redir_op "$2" ; then
102                 COMPREPLY=( $( compgen -f "$1" ) )
103                 return 0
104         fi
105         return 1
106 }
107
108 # optional, but without this you can't use extended glob patterns
109 shopt -s extglob
110
111 #
112 # Easy ones for the shell builtins
113 #
114 # nothing for: alias, break, continue, dirs, echo, eval, exit, getopts,
115 # let, logout, popd, printf, pwd, return, shift, suspend, test, times,
116 # umask
117 #
118
119 complete -f -- . source
120 complete -A enabled builtin
121 complete -d cd
122
123 # this isn't exactly right yet -- needs to skip shell functions and
124 # do $PATH lookup (or do compgen -c and filter out matches that also
125 # appear in compgen -A function)
126 complete -c command
127
128 # could add -S '=', but that currently screws up because readline appends
129 # a space unconditionally
130
131 complete -v export local readonly
132 complete -A helptopic help      # currently same as builtins
133
134 complete -d pushd
135
136 complete -A shopt shopt
137
138 complete -c type
139
140 complete -a unalias
141 complete -v unset 
142
143 #
144 # Job control builtins: fg, bg, disown, kill, wait
145 # kill not done yet
146 #
147
148 complete -A stopped -P '%' bg
149 complete -j -P '%' fg jobs disown
150
151 # this is not quite right at this point
152
153 _wait_func ()
154 {
155         local cur
156         cur=${COMP_WORDS[COMP_CWORD]}
157
158         case "$cur" in
159         %*)     COMPREPLY=( $(compgen -A running -P '%' ${cur#?} ) ) ;;
160         [0-9]*) COMPREPLY=( $(jobs -p | grep ^${cur}) ) ;;
161         *)      COMPREPLY=( $(compgen -A running -P '%') $(jobs -p) )
162                 ;;
163         esac
164 }
165 complete -F _wait_func wait
166
167 #
168 # more complicated things, several as yet unimplemented
169 #
170
171 #complete -F _bind_func bind
172
173 _declare_func()
174 {
175         local cur prev nflag opts
176
177         cur=${COMP_WORDS[COMP_CWORD]}
178         prev=${COMP_WORDS[COMP_CWORD-1]}
179
180         COMPREPLY=()
181         if (( $COMP_CWORD <= 1 )) || [[ $cur == '-' ]]; then
182                 COMPREPLY=(-a -f -F -i -p -r -t -x)
183                 return 0;
184         fi
185         if [[ $cur == '+' ]]; then
186                 COMPREPLY=(+i +t +x)
187                 return 0;
188         fi
189         if [[ $prev == '-p' ]]; then
190                 COMPREPLY=( $(compgen -v $cur) )
191                 return 0;
192         fi
193         return 1
194 }
195 complete -F _declare_func declare typeset
196
197 _enable_func()
198 {
199         local cur prev nflag opts
200
201         cur=${COMP_WORDS[COMP_CWORD]}
202         prev=${COMP_WORDS[COMP_CWORD-1]}
203
204         COMPREPLY=()
205         if (( $COMP_CWORD <= 1 )) || [[ $cur == '-' ]]; then
206                 COMPREPLY=(-a -d -f -n -p -s)
207                 return 0;
208         fi
209         if [[ $prev == '-f' ]]; then
210                 COMPREPLY=( $( compgen -f $cur ) )
211                 return 0;
212         fi
213         for opts in "${COMP_WORDS[@]}" ; do
214                 if [[ $opts == -*n* ]]; then nflag=1; fi
215         done
216
217         if [ -z "$nflag" ] ; then
218                 COMPREPLY=( $( compgen -A enabled $cur ) )
219         else
220                 COMPREPLY=( $( compgen -A disabled $cur ) )
221         fi
222         return 0;
223 }
224 complete -F _enable_func enable
225
226 _exec_func()
227 {
228         local cur prev
229
230         cur=${COMP_WORDS[COMP_CWORD]}
231         prev=${COMP_WORDS[COMP_CWORD-1]}
232
233         if (( $COMP_CWORD <= 1 )) || [[ $cur == '-' ]]; then
234                 COMPREPLY=(-a -c -l)
235                 return 0;
236         fi
237         if [[ $prev != -*a* ]]; then
238                 COMPREPLY=( $( compgen -c $cur ) )
239                 return 0
240         fi
241         return 1;
242 }
243 complete -F _exec_func exec
244
245 _fc_func()
246 {
247         local cur prev
248
249         cur=${COMP_WORDS[COMP_CWORD]}
250         prev=${COMP_WORDS[COMP_CWORD-1]}
251
252         if (( $COMP_CWORD <= 1 )) || [[ $cur == '-' ]]; then
253                 COMPREPLY=(-e -n -l -r -s)
254                 return 0;
255         fi
256         if [[ $prev == -*e ]]; then
257                 COMPREPLY=( $(compgen -c $cur) )
258                 return 0
259         fi
260         return 1
261 }
262 complete -F _fc_func fc
263
264 _hash_func()
265 {
266         local cur prev
267
268         cur=${COMP_WORDS[COMP_CWORD]}
269         prev=${COMP_WORDS[COMP_CWORD-1]}
270
271         if (( $COMP_CWORD <= 1 )) || [[ $cur == '-' ]]; then
272                 COMPREPLY=(-p -r -t)
273                 return 0;
274         fi
275
276         if [[ $prev == '-p' ]]; then
277                 COMPREPLY=( $( compgen -f $cur ) )
278                 return 0;
279         fi
280         COMPREPLY=( $( compgen -c $cur ) )
281         return 0
282 }
283 complete -F _hash_func hash
284
285 _history_func()
286 {
287         local cur prev
288
289         cur=${COMP_WORDS[COMP_CWORD]}
290         prev=${COMP_WORDS[COMP_CWORD-1]}
291
292         COMPREPLY=()
293         if (( $COMP_CWORD <= 1 )) || [[ $cur == '-' ]]; then
294                 COMPREPLY=(-a -c -d -n -r -w -p -s)
295                 return 0;
296         fi
297         if [[ $prev == -[anrw] ]]; then
298                 COMPREPLY=( $( compgen -f $cur ) )
299         fi
300         return 0
301 }
302 complete -F _history_func history
303
304 #complete -F _read_func read
305
306 _set_func ()
307 {
308         local cur prev
309
310         cur=${COMP_WORDS[COMP_CWORD]}
311         prev=${COMP_WORDS[COMP_CWORD-1]}
312
313         COMPREPLY=()
314
315         _redir_test "$cur" "$prev" && return 0;
316
317         if (( $COMP_CWORD <= 1 )) || [[ $cur == '-' ]]; then
318                 COMPREPLY=(-a -b -e -f -k -m -n -o -p -t -u -v -x -B -C -H -P --)
319                 return 0;
320         fi
321         if [[ $cur == '+' ]]; then
322                 COMPREPLY=(+a +b +e +f +k +m +n +o +p +t +u +v +x +B +C +H +P)
323                 return 0;
324         fi
325         if [[ $prev == [+-]o ]]; then
326                 COMPREPLY=( $(compgen -A setopt $cur) )
327                 return 0;
328         fi
329         return 1;
330 }
331 complete -F _set_func set
332
333 _trap_func ()
334 {
335         local cur
336         cur=${COMP_WORDS[COMP_CWORD]}
337
338         if (( $COMP_CWORD <= 1 )) || [[ $cur == '-' ]]; then
339                 COMPREPLY=(-l -p)
340                 return 0;
341         fi
342         COMPREPLY=( $( compgen -A signal ${cur}) )
343         return 0
344 }
345 complete -F _trap_func trap
346
347 #
348 # meta-completion (completion for complete/compgen)
349 #
350 _complete_meta_func()
351 {
352         local cur prev cmd
353         COMPREPLY=()
354
355         cmd=$1
356
357         cur=${COMP_WORDS[COMP_CWORD]}
358         prev=${COMP_WORDS[COMP_CWORD-1]}
359
360         _redir_test "$cur" "$prev" && return 0;
361
362         if (( $COMP_CWORD <= 1 )) || [[ "$cur" == '-' ]]; then
363                 case "$cmd" in
364                 complete) COMPREPLY=(-a -b -c -d -e -f -j -k -s -v -u -r -p -A -G -W -P -S -X -F -C);;
365                 compgen)  COMPREPLY=(-a -b -c -d -e -f -j -k -s -v -u -A -G -W -P -S -X -F -C);;
366                 esac
367                 return 0
368         fi
369
370         if [[ $prev == -A ]]; then
371                 COMPREPLY=(alias arrayvar binding builtin command directory \
372 disabled enabled export file 'function' helptopic hostname job keyword \
373 running service setopt shopt signal stopped variable)
374                 return 0
375         elif [[ $prev == -F ]]; then
376                 COMPREPLY=( $( compgen -A function $cur ) )
377         elif [[ $prev == -C ]]; then
378                 COMPREPLY=( $( compgen -c $cur ) )
379         else
380                 COMPREPLY=( $( compgen -c $cur ) )
381         fi
382         return 0
383 }
384 complete -F _complete_meta_func complete compgen
385
386 #
387 # some completions for shell reserved words
388 #
389 #complete -c -k time do if then else elif '{'
390
391 #
392 # external commands
393 #
394
395 complete -e printenv
396
397 complete -c nohup exec nice eval trace truss strace sotruss gdb
398
399 _make_targets ()
400 {
401         local mdef makef gcmd cur prev i
402
403         COMPREPLY=()
404         cur=${COMP_WORDS[COMP_CWORD]}
405         prev=${COMP_WORDS[COMP_CWORD-1]}
406
407         # if prev argument is -f, return possible filename completions.
408         # we could be a little smarter here and return matches against
409         # `makefile Makefile *.mk', whatever exists
410         case "$prev" in
411         -*f)    COMPREPLY=( $(compgen -f $cur ) ); return 0;;
412         esac
413
414         # if we want an option, return the possible posix options
415         case "$cur" in
416         -)      COMPREPLY=(-e -f -i -k -n -p -q -r -S -s -t); return 0;;
417         esac
418
419         # make reads `makefile' before `Makefile'
420         # GNU make reads `GNUmakefile' before all other makefiles, but we
421         # check that we're completing `gmake' before checking for it
422         if [ -f GNUmakefile ] && [ ${COMP_WORDS[0]} == gmake ]; then
423                 mdef=GNUmakefile
424         elif [ -f makefile ]; then
425                 mdef=makefile
426         elif [ -f Makefile ]; then
427                 mdef=Makefile
428         else
429                 mdef=*.mk               # local convention
430         fi
431
432         # before we scan for targets, see if a makefile name was specified
433         # with -f
434         for (( i=0; i < ${#COMP_WORDS[@]}; i++ )); do
435                 if [[ ${COMP_WORDS[i]} == -*f ]]; then
436                         eval makef=${COMP_WORDS[i+1]}   # eval for tilde expansion
437                         break
438                 fi
439         done
440
441         [ -z "$makef" ] && makef=$mdef
442
443         # if we have a partial word to complete, restrict completions to
444         # matches of that word
445         if [ -n "$2" ]; then gcmd='grep "^$2"' ; else gcmd=cat ; fi
446
447         # if we don't want to use *.mk, we can take out the cat and use
448         # test -f $makef and input redirection  
449         COMPREPLY=( $(cat $makef 2>/dev/null | awk 'BEGIN {FS=":"} /^[^.#       ][^=]*:/ {print $1}' | tr -s ' ' '\012' | sort -u | eval $gcmd ) )
450 }
451 complete -F _make_targets -X '+($*|*.[cho])' make gmake pmake
452
453 _umount_func ()
454 {
455         COMPREPLY=( $(mount | awk '{print $1}') )
456 }
457 complete -F _umount_func umount
458
459 _configure_func ()
460 {
461         case "$2" in
462         -*)     ;;
463         *)      return ;;
464         esac
465
466         case "$1" in
467         \~*)    eval cmd=$1 ;;
468         *)      cmd="$1" ;;
469         esac
470
471         COMPREPLY=( $("$cmd" --help | awk '{if ($1 ~ /--.*/) print $1}' | grep ^"$2" | sort -u) )
472 }
473 complete -F _configure_func configure
474
475 complete -W '"${GROUPS[@]}"' newgrp
476
477 complete -f chown ln more cat
478 complete -d mkdir rmdir
479 complete -f strip
480
481 complete -f -X '*.gz' gzip
482 complete -f -X '*.bz2' bzip2
483 complete -f -X '*.Z' compress
484 complete -f -X '!*.+(gz|tgz|Gz)' gunzip gzcat zcat zmore
485 complete -f -X '!*.Z' uncompress zmore zcat
486 complete -f -X '!*.bz2' bunzip2 bzcat
487 complete -f -X '!*.zip' unzip
488 complete -f -X '!*.+(gif|jpg|jpeg|GIF|JPG|JPEG|bmp)' xv
489
490 complete -f -X '!*.pl' perl perl5
491
492 complete -A hostname rsh telnet rlogin ftp ping xping host traceroute nslookup
493 complete -A hostname rxterm rxterm3 rxvt2
494
495 complete -u su
496 complete -g newgrp groupdel groupmod
497
498 complete -f -X '!*.+(ps|PS)' gs gv ghostview psselect pswrap
499 complete -f -X '!*.+(dvi|DVI)' dvips xdvi dviselect dvitype catdvi
500 complete -f -X '!*.+(pdf|PDF)' acroread4
501 complete -f -X '!*.texi*' makeinfo texi2dvi texi2html
502 complete -f -X '!*.+(tex|TEX)' tex latex slitex
503
504 complete -f -X '!*.+(mp3|MP3)' mpg123
505 complete -f -X '!*.+(htm|html)' links w3m lynx
506
507 #
508 # other possibilities, left as exercises
509 #
510 #complete -F _find_func find
511 #complete -F _man_func man
512 #complete -F _stty_func stty