Improve bash completion with the use of mi and xmllint
[lttng-tools.git] / extras / lttng-bash_completion
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
18 # Generates COMPREPLY with the existing session names
19 _lttng_complete_sessions() {
20 local sessions
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')
22 COMPREPLY=( $(compgen -W "${sessions}" -- $cur) )
23 return
24 }
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
52
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
63 _lttng_cmd_addcontext() {
64 options=$(lttng add-context --list-options)
65
66 case $prev in
67 --session|-s)
68 _lttng_complete_sessions
69 return
70 ;;
71 --channel|-c)
72 return
73 ;;
74 --type|-t)
75 return
76 ;;
77 esac
78
79 case $cur in
80 -*)
81 _lttng_complete_options
82 return
83 ;;
84 esac
85 }
86
87 _lttng_cmd_calibrate() {
88 options=$(lttng calibrate --list-options)
89
90 case $cur in
91 -*)
92 _lttng_complete_options
93 return
94 ;;
95 esac
96 }
97
98 _lttng_cmd_create() {
99 options=$(lttng create --list-options)
100
101 case $prev in
102 --output|-o)
103 _filedir -d
104 return
105 ;;
106 esac
107
108 case $cur in
109 -*)
110 _lttng_complete_options
111 return
112 ;;
113 esac
114 }
115
116 _lttng_cmd_destroy() {
117 options=$(lttng destroy --list-options)
118
119 case $cur in
120 -*)
121 _lttng_complete_options
122 return
123 ;;
124 *)
125 _lttng_complete_sessions
126 return
127 ;;
128 esac
129 }
130 _lttng_cmd_disablechannel() {
131 options=$(lttng disable-channel --list-options)
132
133 case $prev in
134 --session|-s)
135 _lttng_complete_sessions
136 return
137 ;;
138 esac
139
140 case $cur in
141 -*)
142 _lttng_complete_options
143 return
144 ;;
145 esac
146 }
147 _lttng_cmd_disableevent() {
148 options=$(lttng disable-event --list-options)
149
150 case $prev in
151 --session|-s)
152 _lttng_complete_sessions
153 return
154 ;;
155 --channel|-c)
156 return
157 ;;
158 esac
159
160 case $cur in
161 -*)
162 _lttng_complete_options
163 return
164 ;;
165 esac
166 }
167
168 _lttng_cmd_enablechannel() {
169 options=$(lttng enable-channel --list-options)
170
171 case $prev in
172 --session|-s)
173 _lttng_complete_sessions
174 return
175 ;;
176 esac
177
178 case $cur in
179 -*)
180 _lttng_complete_options
181 return
182 ;;
183 esac
184 }
185
186 _lttng_cmd_enableevent() {
187 options=$(lttng enable-event --list-options)
188
189 case $prev in
190 --session|-s)
191 _lttng_complete_sessions
192 return
193 ;;
194 --channel|-c)
195 return
196 ;;
197 --probe)
198 return
199 ;;
200 --function)
201 return
202 ;;
203 esac
204
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
224 case $cur in
225 -*)
226 _lttng_complete_options
227 return
228 ;;
229 esac
230
231 }
232
233 _lttng_cmd_list() {
234 options=$(lttng list --list-options)
235
236 case $prev in
237 --channel|-c)
238 return
239 ;;
240 esac
241
242 case $cur in
243 -*)
244 _lttng_complete_options
245 return
246 ;;
247 *)
248 _lttng_complete_sessions
249 return
250 esac
251 }
252
253 _lttng_cmd_setsession() {
254 options=$(lttng set-session --list-options)
255
256 case $cur in
257 -*)
258 _lttng_complete_options
259 return
260 ;;
261 *)
262 _lttng_complete_sessions
263 return
264 ;;
265 esac
266 }
267
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
292 _lttng_cmd_start() {
293 options=$(lttng start --list-options)
294
295 case $cur in
296 -*)
297 _lttng_complete_options
298 return
299 ;;
300 *)
301 _lttng_complete_sessions
302 return
303 ;;
304 esac
305 }
306
307 _lttng_cmd_stop() {
308 options=$(lttng stop --list-options)
309
310 case $cur in
311 -*)
312 _lttng_complete_options
313 ;;
314 *)
315 _lttng_complete_sessions
316 ;;
317 esac
318 }
319
320 _lttng_cmd_version() {
321 options=$(lttng version --list-options)
322
323 case $cur in
324 -*)
325 _lttng_complete_options
326 ;;
327 esac
328 }
329
330 _lttng_cmd_view() {
331 options=$(lttng view --list-options)
332
333 case $cur in
334 -*)
335 _lttng_complete_options
336 ;;
337 esac
338 }
339
340
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
358 _lttng_complete_options
359 ;;
360 *)
361 # Otherwise complete with commands
362 _lttng_complete_commands
363 ;;
364 esac
365 }
366
367 _lttng_after_command() {
368 local cmd_name
369
370 cmd_name=_lttng_cmd_${command_found//-/}
371
372 type -t $cmd_name | grep -q 'function' && $cmd_name
373 }
374
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.
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
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}
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
406 for (( i = start ; i < ${#COMP_WORDS[@]} ; i++ )); do
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
413 done
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
431
432 # Check if the cursor is before or after the command keyword
433 if _lttng_cursor_is_after_command; then
434 _lttng_after_command
435 else
436 _lttng_before_command
437 fi
438 }
439
440 complete -F _lttng lttng
This page took 0.05746 seconds and 4 git commands to generate.