Skip to content

Commit 54ca5fe

Browse files
authored
Merge pull request #22 from rical/master
tipc: add tipc completions
2 parents 6170f07 + d815b46 commit 54ca5fe

File tree

4 files changed

+327
-0
lines changed

4 files changed

+327
-0
lines changed

completions/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@ bashcomp_DATA = 2to3 \
361361
tcpkill \
362362
tcpnice \
363363
timeout \
364+
tipc \
364365
tracepath \
365366
tshark \
366367
tune2fs \

completions/tipc

Lines changed: 307 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,307 @@
1+
# tipc(8) completion -*- shell-script -*-
2+
3+
_tipc_media() {
4+
local optind=$1
5+
6+
if [[ $cword -eq $optind ]]; then
7+
COMPREPLY=( $( compgen -W 'media' -- $cur) )
8+
return 0
9+
elif [[ $cword -eq $optind+1 ]]; then
10+
COMPREPLY=( $( compgen -W 'udp eth ib' -- $cur) )
11+
return 0
12+
fi
13+
14+
return 1
15+
}
16+
17+
_tipc_bearer() {
18+
local optind=$1
19+
local media i
20+
21+
if _tipc_media $optind; then
22+
return
23+
fi
24+
25+
for ((i = 0; i < $cword; i++)); do
26+
if [[ ${words[$i]} == 'media' ]]; then
27+
media=${words[$(($i + 1))]}
28+
fi
29+
done
30+
31+
if [[ $cword -eq $optind+2 ]]; then
32+
case "$media" in
33+
"udp")
34+
COMPREPLY=( $( compgen -W 'name' -- $cur) )
35+
;;
36+
"eth" | "ib")
37+
COMPREPLY=( $( compgen -W 'device' -- $cur) )
38+
;;
39+
esac
40+
elif [[ $cword -eq $optind+3 ]]; then
41+
case "$media" in
42+
"udp")
43+
local names=$(tipc bearer list | awk -F: '/^udp:/ {print $2}')
44+
COMPREPLY=( $( compgen -W '$names' -- $cur) )
45+
;;
46+
"eth")
47+
local interfaces=$(ls /sys/class/net/)
48+
COMPREPLY=( $( compgen -W '$interfaces' -- $cur ) )
49+
;;
50+
esac
51+
fi
52+
}
53+
54+
_tipc_link_opts() {
55+
COMPREPLY=( $( compgen -W 'priority tolerance window' -- $cur) )
56+
}
57+
58+
_tipc_link() {
59+
local optind=$1
60+
local filter=$2
61+
62+
if [[ $cword -eq $optind ]]; then
63+
COMPREPLY=( $( compgen -W 'link' -- $cur) )
64+
elif [[ $cword -eq $optind+1 ]]; then
65+
# awk drops link state and last trailing :
66+
local links=$(tipc link list | awk '{print substr($1, 0, length($1)-1)}')
67+
if [[ $filter == "peers" ]]; then
68+
links=$(command sed '/broadcast-link/d' <<<"$links")
69+
fi
70+
COMPREPLY=( $( compgen -W '$links' -- $cur ) )
71+
fi
72+
}
73+
74+
_tipc()
75+
{
76+
local cur prev words cword optind i p
77+
_init_completion || return
78+
79+
optind=1
80+
COMPREPLY=()
81+
82+
# Flags can be placed anywhere in the commandline
83+
case "$cur" in
84+
-*)
85+
COMPREPLY=( $( compgen -W '-h --help' -- $cur ) )
86+
return
87+
;;
88+
esac
89+
90+
case "${words[$optind]}" in
91+
bearer)
92+
let optind++
93+
94+
case "${words[$optind]}" in
95+
enable)
96+
local media params
97+
let optind++
98+
99+
if [[ $cword -lt $optind+4 ]]; then
100+
_tipc_bearer $optind
101+
return
102+
fi
103+
104+
for ((i = 0; i < $cword; i++)); do
105+
if [[ ${words[$i]} == 'media' ]]; then
106+
media=${words[$(($i + 1))]}
107+
fi
108+
done
109+
case "$media" in
110+
"udp")
111+
declare -a params=("localip" "localport" "remoteip"
112+
"remoteport" "domain" "priority")
113+
;;
114+
"eth" | "ib")
115+
declare -a params=("domain" "priority")
116+
;;
117+
*)
118+
return
119+
;;
120+
esac
121+
122+
# If the previous word was a known paramater we assume a value for
123+
# that key Note that this would break if the user attempts to use a
124+
# kown key as value
125+
for i in ${params[@]}; do
126+
if [[ $prev == $i ]]; then
127+
return
128+
fi
129+
done
130+
131+
# In order not to print already used options we remove them
132+
for p in ${words[@]}; do
133+
for i in ${params[@]}; do
134+
if [[ $p == $i ]]; then
135+
params=( "${params[@]/$i}" )
136+
fi
137+
done
138+
done
139+
140+
COMPREPLY=( $( compgen -W '${params[@]}' -- $cur) )
141+
return
142+
;;
143+
disable)
144+
let optind++
145+
146+
_tipc_bearer $optind
147+
return
148+
;;
149+
get)
150+
let optind++
151+
152+
if [[ $cword -eq $optind ]]; then
153+
_tipc_link_opts
154+
elif [[ $cword -ge $optind+1 ]]; then
155+
_tipc_bearer $(($optind + 1))
156+
fi
157+
return
158+
;;
159+
set)
160+
let optind++
161+
162+
if [[ $cword -eq $optind ]]; then
163+
_tipc_link_opts
164+
elif [[ $cword -ge $optind+2 ]]; then
165+
_tipc_bearer $(($optind + 2))
166+
fi
167+
return
168+
;;
169+
list)
170+
return
171+
;;
172+
esac
173+
174+
COMPREPLY=( $( compgen -W 'enable disable set get list' -- $cur ) )
175+
return
176+
;;
177+
link)
178+
let optind++
179+
180+
case "${words[$optind]}" in
181+
get)
182+
let optind++
183+
184+
if [[ $cword -eq $optind ]]; then
185+
_tipc_link_opts
186+
elif [[ $cword -ge $optind+1 ]]; then
187+
_tipc_link $(($optind + 1)) "peers"
188+
fi
189+
return
190+
;;
191+
set)
192+
let optind++
193+
194+
if [[ $cword -eq $optind ]]; then
195+
_tipc_link_opts
196+
elif [[ $cword -ge $optind+2 ]]; then
197+
_tipc_link $(($optind + 2)) "peers"
198+
fi
199+
return
200+
;;
201+
list)
202+
return
203+
;;
204+
statistics)
205+
let optind++
206+
207+
case "${words[$optind]}" in
208+
show|reset)
209+
_tipc_link $(($optind + 1))
210+
return
211+
;;
212+
esac
213+
214+
COMPREPLY=( $( compgen -W 'show reset' -- $cur) )
215+
return
216+
;;
217+
esac
218+
219+
COMPREPLY=( $( compgen -W 'get set list statistics' -- $cur ) )
220+
return
221+
;;
222+
media)
223+
let optind++
224+
225+
case "${words[$optind]}" in
226+
get)
227+
let optind++
228+
229+
if [[ $cword -eq $optind ]]; then
230+
_tipc_link_opts
231+
elif [[ $cword -ge $optind+1 ]]; then
232+
_tipc_media $(($optind + 1))
233+
fi
234+
return
235+
;;
236+
set)
237+
let optind++
238+
239+
if [[ $cword -eq $optind ]]; then
240+
_tipc_link_opts
241+
elif [[ $cword -ge $optind+2 ]]; then
242+
_tipc_media $(($optind + 2))
243+
fi
244+
return
245+
;;
246+
list)
247+
return
248+
;;
249+
esac
250+
251+
COMPREPLY=( $( compgen -W 'get set list' -- $cur ) )
252+
return
253+
;;
254+
nametable)
255+
let optind++
256+
257+
case "${words[$optind]}" in
258+
show)
259+
return
260+
;;
261+
esac
262+
263+
COMPREPLY=( $( compgen -W 'show' -- $cur ) )
264+
return
265+
;;
266+
node)
267+
let optind++
268+
269+
case "${words[$optind]}" in
270+
get|set)
271+
let optind++
272+
273+
case "${words[$optind]}" in
274+
address|netid)
275+
return
276+
;;
277+
esac
278+
COMPREPLY=( $( compgen -W 'address netid' -- $cur ) )
279+
return
280+
;;
281+
list)
282+
return
283+
;;
284+
esac
285+
286+
COMPREPLY=( $( compgen -W 'list get set' -- $cur ) )
287+
return
288+
;;
289+
socket)
290+
let optind++
291+
292+
case "${words[$optind]}" in
293+
list)
294+
return
295+
;;
296+
esac
297+
298+
COMPREPLY=( $( compgen -W 'list' -- $cur ) )
299+
return
300+
;;
301+
esac
302+
303+
COMPREPLY=( $( compgen -W 'bearer link media nametable node socket' -- $cur ) )
304+
}
305+
complete -F _tipc tipc
306+
307+
# ex: ts=4 sw=4 et filetype=sh

test/completion/tipc.exp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
assert_source_completions tipc

test/lib/completions/tipc.exp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
proc setup {} {
2+
save_env
3+
}
4+
5+
6+
proc teardown {} {
7+
assert_env_unmodified
8+
}
9+
10+
11+
setup
12+
13+
14+
assert_complete_any "tipc "
15+
sync_after_int
16+
17+
18+
teardown

0 commit comments

Comments
 (0)