Added options.bashrc for shopt stuff.
[profile.git] / .profile.d / completion.bashrc
1 # $Id$
2 # Helper!
3 export COMPLETION_DIR_SSH=~/.ssh/hosts
4 export COMPLETION_DIR_TELNET=~/.telnet/hosts
5 export COMPLETION_DIR_RDP=~/.ssh/hosts
6 export COMPLETION_DIR_PING="$COMPLETION_DIR_SSH $COMPLETION_DIR_TELNET $COMPLETION_DIR_RDP"
7
8 function _generic_completion() {
9   COMPLETION_DIR=${1+"$@"}
10   COMPREPLY=()
11   cur=${COMP_WORDS[COMP_CWORD]}
12
13   HOSTS="$(find $COMPLETION_DIR -type f -perm -100 2>/dev/null | sed 's@.*/@@')"
14
15   COMPREPLY=($(compgen -W "$HOSTS" -- "$cur"))
16   return 0
17 }
18
19 # Process completion for killall.
20 function _process() {
21   cur=${COMP_WORDS[COMP_CWORD]}
22
23   PROCESSES="$(ps agx --no-heading | awk '{ print $5 }' | sed 's@.*/@@' | sort | uniq)"
24
25   COMPREPLY=($(compgen -W "$PROCESSES" -- "$cur"))
26   return 0
27 }
28
29 # Host completion for SSH known hosts.
30 function _known_hosts() {
31   cur=${COMP_WORDS[COMP_CWORD]}
32
33   HOSTS=$(sed 's/[      ].*//;s/,/\
34 /' ~/.ssh/known_hosts)
35
36   COMPREPLY=($(compgen -W "$HOSTS" -- "$cur"))
37   return 0
38 }
39
40 # Process completion for kill, strace etc.
41 function _pid() {
42   cur=${COMP_WORDS[COMP_CWORD]}
43
44   PROCESSES="$(find /proc -name '[0-9]*' -maxdepth 1 2>/dev/null | sed 's@.*/@@')"
45
46   COMPREPLY=($(compgen -W "$PROCESSES" -- "$cur"))
47   return 0
48 }
49
50 # Complete ssh with hostnames from the Window Maker menu.
51 function _ssh() {
52   _generic_completion $COMPLETION_DIR_SSH
53   return 0
54 }
55
56 # Complete telnet with hostnames from the Window Maker menu.
57 function _telnet() {
58   _generic_completion $COMPLETION_DIR_TELNET
59   return 0
60 }
61
62 # Complete rdesktop with hostnames from the Window Maker menu.
63 function _rdp() {
64   _generic_completion $COMPLETION_DIR_RDP
65   return 0
66 }
67
68 # Ping hosts from any of the above lists.
69 function _ping() {
70   _generic_completion $COMPLETION_DIR_PING
71 }
72
73 # Hacked up make rule.
74 function _make() {
75   COMPREPLY=()
76   cur=${COMP_WORDS[COMP_CWORD]}
77
78   RULES="all check clean dep depend install reinstall setup uninstall"
79
80   COMPREPLY=($(compgen -W "$RULES" -- "$cur"))
81   return 0
82 }
83
84 # Look for modules.
85 function _modprobe() {
86   COMPREPLY=()
87   cur=${COMP_WORDS[COMP_CWORD]}
88
89   ver=$(uname -r)
90   MODULES="$(find /lib/modules/$ver -name \*.ko | sed -n 's@^.*/\([^/]*\).ko$@\1@p')"
91
92   COMPREPLY=($(compgen -W "$MODULES" -- "$cur"))
93   return 0
94 }
95
96 # Look for loaded modules.
97 function _rmmod() {
98   COMPREPLY=()
99   cur=${COMP_WORDS[COMP_CWORD]}
100
101   MODULES="$(lsmod | sed '1d;s/ .*$//')"
102
103   COMPREPLY=($(compgen -W "$MODULES" -- "$cur"))
104   return 0
105 }
106
107 complete -F _ssh rollout
108 complete -F _ssh ssh
109 complete -F _ssh sshterm
110 complete -F _ping telnet
111 complete -F _telnet telnetterm
112 complete -F _rdp rdesktop
113 complete -F _ping ping
114 complete -F _ping traceroute
115 complete -F _ping dnsip
116 complete -F _make make
117 complete -F _process killall
118 complete -F _pid kill
119 complete -F _pid strace
120 complete -F _known_hosts kill_known_host
121 complete -F _known_hosts knh
122 complete -F _modprobe modprobe
123 complete -F _rmmod rmmod