782cb699d0c81087ab9dcde005134342598b3576
[profile.git] / .profile.d / completion.bashrc
1 # Old bash doesn't have completion support.
2 if ! builtin complete 2>&1 | grep 'not a shell builtin' >/dev/null; then
3
4 # Helper!
5 COMPLETION_DIR_SSH=${PROFILE_HOME:-~}/.ssh/hosts
6 COMPLETION_DIR_TELNET=${PROFILE_HOME:-~}/.telnet/hosts
7 COMPLETION_DIR_RDP=${PROFILE_HOME:-~}/.rdp/hosts
8 COMPLETION_DIR_PING="$COMPLETION_DIR_SSH $COMPLETION_DIR_TELNET $COMPLETION_DIR_RDP"
9
10 function _generic_completion() {
11   COMPLETION_DIR=${1+"$@"}
12   COMPREPLY=()
13   cur=${COMP_WORDS[COMP_CWORD]}
14
15   HOSTS="$(find $COMPLETION_DIR -type f -perm -100 2>/dev/null | sed 's@.*/@@')"
16
17   COMPREPLY=($(compgen -W "$HOSTS" -- "$cur"))
18   return 0
19 }
20
21 # Process completion for killall.
22 function _process() {
23   cur=${COMP_WORDS[COMP_CWORD]}
24
25   PROCESSES="$(ps agx --no-heading | awk '{ print $5 }' | sed 's@.*/@@' | sort | uniq)"
26
27   COMPREPLY=($(compgen -W "$PROCESSES" -- "$cur"))
28   return 0
29 }
30
31 # Host completion for SSH known hosts.
32 function _known_hosts() {
33   cur=${COMP_WORDS[COMP_CWORD]}
34
35   HOSTS=$(sed 's/[      ].*//;s/,/\n/' ~/.ssh/known_hosts)
36
37   COMPREPLY=($(compgen -W "$HOSTS" -- "$cur"))
38   return 0
39 }
40
41 # Host completion from dsh groups.
42 function _dshgroup() {
43   cur=${COMP_WORDS[COMP_CWORD]}
44
45   HOSTS=$(grep -rh '^[A-Za-z0-9]' ~/.dsh/group)
46
47   COMPREPLY=($(compgen -W "$HOSTS" -- "$cur"))
48   return 0
49 }
50
51 function _omnissh() {
52   cur=${COMP_WORDS[COMP_CWORD]}
53
54   HOSTS="$(grep -rh '^[A-Za-z0-9]' ~/.dsh/group)
55 $(sed 's/[      ].*//;s/,/\n/' ~/.ssh/known_hosts)
56 $(find $COMPLETION_DIR_SSH -type f -perm -100 2>/dev/null | sed 's@.*/@@')"
57
58   COMPREPLY=($(compgen -W "$HOSTS" -- "$cur"))
59   return 0
60 }
61
62 # Process completion for kill, strace etc.
63 function _pid() {
64   cur=${COMP_WORDS[COMP_CWORD]}
65
66   PROCESSES="$(find /proc -name '[0-9]*' -maxdepth 1 2>/dev/null | sed 's@.*/@@')"
67
68   COMPREPLY=($(compgen -W "$PROCESSES" -- "$cur"))
69   return 0
70 }
71
72 # Complete ssh with hostnames from the Window Maker menu.
73 function _ssh() {
74   _generic_completion $COMPLETION_DIR_SSH
75   return 0
76 }
77
78 # Complete telnet with hostnames from the Window Maker menu.
79 function _telnet() {
80   _generic_completion $COMPLETION_DIR_TELNET
81   return 0
82 }
83
84 # Complete rdesktop with hostnames from the Window Maker menu.
85 function _rdp() {
86   _generic_completion $COMPLETION_DIR_RDP
87   return 0
88 }
89
90 # Ping hosts from any of the above lists.
91 function _ping() {
92   _generic_completion $COMPLETION_DIR_PING
93 }
94
95 # Hacked up make rule.
96 function _make() {
97   COMPREPLY=()
98   cur=${COMP_WORDS[COMP_CWORD]}
99
100   RULES="all check clean dep depend install reinstall setup uninstall"
101
102   COMPREPLY=($(compgen -W "$RULES" -- "$cur"))
103   return 0
104 }
105
106 # Look for modules.
107 function _modprobe() {
108   COMPREPLY=()
109   cur=${COMP_WORDS[COMP_CWORD]}
110
111   ver=$KERNEL
112   MODULES="$(find /lib/modules/$ver -name \*.ko | sed -n 's@^.*/\([^/]*\).ko$@\1@p')"
113
114   COMPREPLY=($(compgen -W "$MODULES" -- "$cur"))
115   return 0
116 }
117
118 # Look for loaded modules.
119 function _rmmod() {
120   COMPREPLY=()
121   cur=${COMP_WORDS[COMP_CWORD]}
122
123   MODULES="$(lsmod | sed '1d;s/ .*$//')"
124
125   COMPREPLY=($(compgen -W "$MODULES" -- "$cur"))
126   return 0
127 }
128
129 # Find tags with GNU GLOBAL.
130 function _global() {
131   COMPREPLY=()
132   cur=${COMP_WORDS[COMP_CWORD]}
133
134   COMPREPLY=($(global -c "$cur"))
135   return 0
136 }
137
138 complete -F _omnissh rollout
139 complete -F _omnissh ssh
140 complete -F _omnissh sshterm
141 complete -F _ping telnet
142 complete -F _telnet telnetterm
143 complete -F _rdp rdesktop
144 complete -F _ping ping
145 complete -F _ping traceroute
146 complete -F _ping dnsip
147 complete -F _make make
148 complete -F _process killall
149 complete -F _pid kill
150 complete -F _pid strace
151 complete -F _known_hosts kill_known_host
152 complete -F _known_hosts knh
153 complete -F _omnissh knh
154 complete -F _modprobe modprobe
155 complete -F _rmmod rmmod
156 complete -F _global global
157
158 fi