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