Improve bash completion with the use of mi and xmllint
[lttng-tools.git] / extras / lttng-bash_completion
CommitLineData
fc256d99
DG
1#
2# Copyright (c) - 2012 Simon Marchi <simon.marchi@polymtl.ca>
3#
4# This program is free software; you can redistribute it and/or modify it under
5# the terms of the GNU General Public License as published by as published by
6# the Free Software Foundation; only version 2 of the License.
7#
8# This program is distributed in the hope that it will be useful, but WITHOUT
9# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11# more details.
12#
13# You should have received a copy of the GNU General Public License along with
14# this program; if not, write to the Free Software Foundation, Inc., 51
15# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16#
17
ba28686d 18# Generates COMPREPLY with the existing session names
fc256d99 19_lttng_complete_sessions() {
4a096a5b 20 local sessions
7335fad6 21 sessions=$(lttng --mi xml list | xmllint --xpath "//command/output/sessions/session/name" - 2>/dev/null | sed -e 's/<name>//g' -e $'s/<\/name>/\\n/g')
4a096a5b 22 COMPREPLY=( $(compgen -W "${sessions}" -- $cur) )
fc256d99
DG
23 return
24}
7335fad6
JRJ
25#
26
27# Generates COMPREPLY with the available kernel event
28_lttng_complete_kernel_events() {
29 local kernel_event
30 kernel_event=$(lttng --mi xml list -k | xmllint --xpath "//command/output/domains/domain[./type = 'KERNEL']/events/event/name" - 2>/dev/null | sed -e "s/<name>//g" -e $"s/<\/name>/\\n/g")
31 COMPREPLY=( $(compgen -W "${kernel_event}" -- $cur) )
32 return
33}
34
35# Generates COMPREPLY with the available ust event
36_lttng_complete_ust_events() {
37 local ust_event
38 ust_event=$(lttng --mi xml list -u | xmllint --xpath "//command/output/domains/domain[./type = 'UST']/pids/pid/events/event/name" - 2>/dev/null | sed -e "s/<name>//g" -e $"s/<\/name>/\\n/g")
39 COMPREPLY=( $(compgen -W "${ust_event}" -- $cur) )
40 return
41}
42
43# Generates COMPREPLY with the available jul event
44_lttng_complete_jul_events() {
45 local jul_event
46 jul_event=$(lttng --mi xml list -j | xmllint --xpath "//command/output/domains/domain[./type = 'JUL']/pids/pid/events/event/name" - 2>/dev/null | sed -e "s/<name>//g" -e $"s/<\/name>/\\n/g")
47 COMPREPLY=( $(compgen -W "${jul_event}" -- $cur) )
48 return
49}
50
51
fc256d99 52
ba28686d
SM
53# Generates COMPREPLY with whatever is in the $options variable.
54_lttng_complete_options() {
55 COMPREPLY=( $(compgen -W "${options}" -- $cur) )
56}
57
58# Generates COMPREPLY with whatever is in the $commands variable.
59_lttng_complete_commands() {
60 COMPREPLY=( $(compgen -W "${commands}" -- $cur) )
61}
62
6c806062 63_lttng_cmd_addcontext() {
ba28686d 64 options=$(lttng add-context --list-options)
fc256d99
DG
65
66 case $prev in
67 --session|-s)
68 _lttng_complete_sessions
69 return
70 ;;
71 --channel|-c)
72 return
73 ;;
fc256d99
DG
74 --type|-t)
75 return
76 ;;
77 esac
78
79 case $cur in
80 -*)
ba28686d 81 _lttng_complete_options
fc256d99
DG
82 return
83 ;;
84 esac
85}
86
07bd6635 87_lttng_cmd_calibrate() {
ba28686d 88 options=$(lttng calibrate --list-options)
07bd6635
SM
89
90 case $cur in
91 -*)
ba28686d
SM
92 _lttng_complete_options
93 return
07bd6635
SM
94 ;;
95 esac
96}
97
fc256d99 98_lttng_cmd_create() {
ba28686d 99 options=$(lttng create --list-options)
fc256d99
DG
100
101 case $prev in
102 --output|-o)
103 _filedir -d
104 return
105 ;;
106 esac
107
108 case $cur in
109 -*)
ba28686d 110 _lttng_complete_options
fc256d99
DG
111 return
112 ;;
113 esac
114}
115
116_lttng_cmd_destroy() {
ba28686d 117 options=$(lttng destroy --list-options)
fc256d99
DG
118
119 case $cur in
120 -*)
ba28686d
SM
121 _lttng_complete_options
122 return
fc256d99
DG
123 ;;
124 *)
125 _lttng_complete_sessions
ba28686d 126 return
fc256d99
DG
127 ;;
128 esac
129}
07bd6635 130_lttng_cmd_disablechannel() {
ba28686d 131 options=$(lttng disable-channel --list-options)
fc256d99
DG
132
133 case $prev in
134 --session|-s)
135 _lttng_complete_sessions
136 return
137 ;;
138 esac
139
140 case $cur in
141 -*)
ba28686d 142 _lttng_complete_options
fc256d99
DG
143 return
144 ;;
145 esac
146}
07bd6635 147_lttng_cmd_disableevent() {
ba28686d 148 options=$(lttng disable-event --list-options)
fc256d99
DG
149
150 case $prev in
151 --session|-s)
152 _lttng_complete_sessions
153 return
154 ;;
155 --channel|-c)
156 return
157 ;;
fc256d99
DG
158 esac
159
160 case $cur in
161 -*)
ba28686d 162 _lttng_complete_options
fc256d99
DG
163 return
164 ;;
165 esac
166}
167
07bd6635 168_lttng_cmd_enablechannel() {
ba28686d 169 options=$(lttng enable-channel --list-options)
fc256d99
DG
170
171 case $prev in
172 --session|-s)
173 _lttng_complete_sessions
174 return
175 ;;
176 esac
177
178 case $cur in
179 -*)
ba28686d 180 _lttng_complete_options
fc256d99
DG
181 return
182 ;;
183 esac
184}
185
07bd6635 186_lttng_cmd_enableevent() {
ba28686d 187 options=$(lttng enable-event --list-options)
fc256d99
DG
188
189 case $prev in
190 --session|-s)
191 _lttng_complete_sessions
192 return
193 ;;
194 --channel|-c)
195 return
196 ;;
07bd6635
SM
197 --probe)
198 return
199 ;;
200 --function)
201 return
202 ;;
fc256d99
DG
203 esac
204
7335fad6
JRJ
205
206 #Check if we want kernel event completion
207 if [[ "$COMP_LINE" == *"-k"* ]]; then
208 _lttng_complete_kernel_events
209 return
210 fi
211
212 #Check if we want ust event completion
213 if [[ "$COMP_LINE" == *"-u"* ]]; then
214 _lttng_complete_ust_events
215 return
216 fi
217
218 #Check if we want jul event completion
219 if [[ "$COMP_LINE" == *"-j"* ]]; then
220 _lttng_complete_jul_events
221 return
222 fi
223
fc256d99
DG
224 case $cur in
225 -*)
ba28686d 226 _lttng_complete_options
fc256d99
DG
227 return
228 ;;
229 esac
7335fad6 230
fc256d99
DG
231}
232
233_lttng_cmd_list() {
ba28686d 234 options=$(lttng list --list-options)
fc256d99
DG
235
236 case $prev in
237 --channel|-c)
238 return
239 ;;
240 esac
241
242 case $cur in
243 -*)
ba28686d 244 _lttng_complete_options
fc256d99
DG
245 return
246 ;;
4a096a5b
SM
247 *)
248 _lttng_complete_sessions
249 return
fc256d99
DG
250 esac
251}
252
6c806062 253_lttng_cmd_setsession() {
ba28686d 254 options=$(lttng set-session --list-options)
fc256d99
DG
255
256 case $cur in
257 -*)
ba28686d 258 _lttng_complete_options
fc256d99
DG
259 return
260 ;;
4a096a5b
SM
261 *)
262 _lttng_complete_sessions
263 return
264 ;;
fc256d99
DG
265 esac
266}
267
ba28686d
SM
268_lttng_cmd_snapshot() {
269 options=$(lttng snapshot --list-options)
270 commands=$(lttng snapshot --list-commands)
271
272 _lttng_find_command $((command_found_index + 1))
273
274 if _lttng_cursor_is_after_command; then
275 case $prev in
276 --session|-s)
277 _lttng_complete_sessions
278 return
279 ;;
280 esac
281
282 case $cur in
283 -*)
284 _lttng_complete_options
285 ;;
286 esac
287 else
288 _lttng_complete_commands
289 fi
290}
291
fc256d99 292_lttng_cmd_start() {
ba28686d 293 options=$(lttng start --list-options)
fc256d99
DG
294
295 case $cur in
296 -*)
ba28686d
SM
297 _lttng_complete_options
298 return
fc256d99
DG
299 ;;
300 *)
301 _lttng_complete_sessions
ba28686d 302 return
fc256d99
DG
303 ;;
304 esac
305}
306
307_lttng_cmd_stop() {
ba28686d 308 options=$(lttng stop --list-options)
fc256d99
DG
309
310 case $cur in
311 -*)
ba28686d 312 _lttng_complete_options
fc256d99
DG
313 ;;
314 *)
315 _lttng_complete_sessions
316 ;;
317 esac
318}
319
320_lttng_cmd_version() {
ba28686d 321 options=$(lttng version --list-options)
fc256d99
DG
322
323 case $cur in
324 -*)
ba28686d 325 _lttng_complete_options
fc256d99
DG
326 ;;
327 esac
328}
329
0c95f5b2 330_lttng_cmd_view() {
ba28686d 331 options=$(lttng view --list-options)
0c95f5b2 332
ba28686d
SM
333 case $cur in
334 -*)
335 _lttng_complete_options
0c95f5b2
DG
336 ;;
337 esac
338}
339
fc256d99 340
fc256d99
DG
341
342_lttng_before_command() {
343 # Check if the previous word should alter the behavior
344 case $prev in
345 --group|-g)
346 COMPREPLY=( $(compgen -g -- $cur) )
347 return
348 ;;
349 --sessiond-path)
350 _filedir
351 return
352 ;;
353 esac
354
355 case $cur in
356 -*)
357 # If the current word starts with a dash, complete with options
ba28686d 358 _lttng_complete_options
fc256d99
DG
359 ;;
360 *)
361 # Otherwise complete with commands
ba28686d 362 _lttng_complete_commands
fc256d99
DG
363 ;;
364 esac
365}
366
367_lttng_after_command() {
368 local cmd_name
369
ba28686d 370 cmd_name=_lttng_cmd_${command_found//-/}
fc256d99
DG
371
372 type -t $cmd_name | grep -q 'function' && $cmd_name
373}
374
ba28686d
SM
375# Check if the word passed as the first parameter corresponds to a
376# command. $command must be set to the list of possible commands.
fc256d99
DG
377_lttng_is_command() {
378 for command in $commands; do
379 if [ "$1" == "$command" ]; then
380 return 0
381 fi
382 done
383
384 return 1
385}
386
ba28686d
SM
387# Try to find a command in the current command line. Possible commands
388# are passed in $commands.
389#
390# This function takes an optional parameter that indicates the index
391# where to start the search in COMP_WORDS. If omitted, it defaults to 1.
392#
393# If a command is found, $command_found is filled with the name of the
394# command and $command_found_index is set to the index of the command in
395# $COMP_WORDS. If no command is found, $command_found is an empty string
396# and $command_found_index is set to -1.
397_lttng_find_command() {
398 start=${1:-1}
fc256d99
DG
399
400 # The text of the found command
401 command_found=""
402
403 # The index of the found command in COMP_WORDS
404 command_found_index=-1
405
ba28686d 406 for (( i = start ; i < ${#COMP_WORDS[@]} ; i++ )); do
fc256d99
DG
407 _lttng_is_command ${COMP_WORDS[$i]}
408 if [ $? -eq 0 ]; then
409 command_found=${COMP_WORDS[$i]}
410 command_found_index=$i
411 break
412 fi
fc256d99 413 done
ba28686d
SM
414}
415
416_lttng_cursor_is_after_command() {
417 [ -n "$command_found" ] && [ "$COMP_CWORD" -gt "$command_found_index" ]
418}
419
420_lttng() {
421 local cur prev commands command_found command_found_index
422
423 # Get the current and previous word
424 _get_comp_words_by_ref cur prev
425
426 # Get the valid first-level LTTng commands and options
427 commands=$(lttng --list-commands)
428 options=$(lttng --list-options)
429
430 _lttng_find_command
fc256d99
DG
431
432 # Check if the cursor is before or after the command keyword
ba28686d 433 if _lttng_cursor_is_after_command; then
fc256d99
DG
434 _lttng_after_command
435 else
436 _lttng_before_command
437 fi
438}
439
440complete -F _lttng lttng
This page took 0.072493 seconds and 4 git commands to generate.