Fix: Don't override user variables within the build system
[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
87_lttng_cmd_create() {
ba28686d 88 options=$(lttng create --list-options)
fc256d99
DG
89
90 case $prev in
91 --output|-o)
92 _filedir -d
93 return
94 ;;
95 esac
96
97 case $cur in
98 -*)
ba28686d 99 _lttng_complete_options
fc256d99
DG
100 return
101 ;;
102 esac
103}
104
105_lttng_cmd_destroy() {
ba28686d 106 options=$(lttng destroy --list-options)
fc256d99
DG
107
108 case $cur in
109 -*)
ba28686d
SM
110 _lttng_complete_options
111 return
fc256d99
DG
112 ;;
113 *)
114 _lttng_complete_sessions
ba28686d 115 return
fc256d99
DG
116 ;;
117 esac
118}
07bd6635 119_lttng_cmd_disablechannel() {
ba28686d 120 options=$(lttng disable-channel --list-options)
fc256d99
DG
121
122 case $prev in
123 --session|-s)
124 _lttng_complete_sessions
125 return
126 ;;
127 esac
128
129 case $cur in
130 -*)
ba28686d 131 _lttng_complete_options
fc256d99
DG
132 return
133 ;;
134 esac
135}
07bd6635 136_lttng_cmd_disableevent() {
ba28686d 137 options=$(lttng disable-event --list-options)
fc256d99
DG
138
139 case $prev in
140 --session|-s)
141 _lttng_complete_sessions
142 return
143 ;;
144 --channel|-c)
145 return
146 ;;
fc256d99
DG
147 esac
148
149 case $cur in
150 -*)
ba28686d 151 _lttng_complete_options
fc256d99
DG
152 return
153 ;;
154 esac
155}
156
07bd6635 157_lttng_cmd_enablechannel() {
ba28686d 158 options=$(lttng enable-channel --list-options)
fc256d99
DG
159
160 case $prev in
161 --session|-s)
162 _lttng_complete_sessions
163 return
164 ;;
165 esac
166
167 case $cur in
168 -*)
ba28686d 169 _lttng_complete_options
fc256d99
DG
170 return
171 ;;
172 esac
173}
174
07bd6635 175_lttng_cmd_enableevent() {
ba28686d 176 options=$(lttng enable-event --list-options)
fc256d99
DG
177
178 case $prev in
179 --session|-s)
180 _lttng_complete_sessions
181 return
182 ;;
183 --channel|-c)
184 return
185 ;;
07bd6635
SM
186 --probe)
187 return
188 ;;
189 --function)
190 return
191 ;;
fc256d99
DG
192 esac
193
7335fad6
JRJ
194
195 #Check if we want kernel event completion
196 if [[ "$COMP_LINE" == *"-k"* ]]; then
197 _lttng_complete_kernel_events
198 return
199 fi
200
201 #Check if we want ust event completion
202 if [[ "$COMP_LINE" == *"-u"* ]]; then
203 _lttng_complete_ust_events
204 return
205 fi
206
207 #Check if we want jul event completion
208 if [[ "$COMP_LINE" == *"-j"* ]]; then
209 _lttng_complete_jul_events
210 return
211 fi
212
fc256d99
DG
213 case $cur in
214 -*)
ba28686d 215 _lttng_complete_options
fc256d99
DG
216 return
217 ;;
218 esac
7335fad6 219
fc256d99
DG
220}
221
222_lttng_cmd_list() {
ba28686d 223 options=$(lttng list --list-options)
fc256d99
DG
224
225 case $prev in
226 --channel|-c)
227 return
228 ;;
229 esac
230
231 case $cur in
232 -*)
ba28686d 233 _lttng_complete_options
fc256d99
DG
234 return
235 ;;
4a096a5b
SM
236 *)
237 _lttng_complete_sessions
238 return
fc256d99
DG
239 esac
240}
241
6c806062 242_lttng_cmd_setsession() {
ba28686d 243 options=$(lttng set-session --list-options)
fc256d99
DG
244
245 case $cur in
246 -*)
ba28686d 247 _lttng_complete_options
fc256d99
DG
248 return
249 ;;
4a096a5b
SM
250 *)
251 _lttng_complete_sessions
252 return
253 ;;
fc256d99
DG
254 esac
255}
256
ba28686d
SM
257_lttng_cmd_snapshot() {
258 options=$(lttng snapshot --list-options)
259 commands=$(lttng snapshot --list-commands)
260
261 _lttng_find_command $((command_found_index + 1))
262
263 if _lttng_cursor_is_after_command; then
264 case $prev in
265 --session|-s)
266 _lttng_complete_sessions
267 return
268 ;;
269 esac
270
271 case $cur in
272 -*)
273 _lttng_complete_options
274 ;;
275 esac
276 else
277 _lttng_complete_commands
278 fi
279}
280
fc256d99 281_lttng_cmd_start() {
ba28686d 282 options=$(lttng start --list-options)
fc256d99
DG
283
284 case $cur in
285 -*)
ba28686d
SM
286 _lttng_complete_options
287 return
fc256d99
DG
288 ;;
289 *)
290 _lttng_complete_sessions
ba28686d 291 return
fc256d99
DG
292 ;;
293 esac
294}
295
296_lttng_cmd_stop() {
ba28686d 297 options=$(lttng stop --list-options)
fc256d99
DG
298
299 case $cur in
300 -*)
ba28686d 301 _lttng_complete_options
fc256d99
DG
302 ;;
303 *)
304 _lttng_complete_sessions
305 ;;
306 esac
307}
308
309_lttng_cmd_version() {
ba28686d 310 options=$(lttng version --list-options)
fc256d99
DG
311
312 case $cur in
313 -*)
ba28686d 314 _lttng_complete_options
fc256d99
DG
315 ;;
316 esac
317}
318
0c95f5b2 319_lttng_cmd_view() {
ba28686d 320 options=$(lttng view --list-options)
0c95f5b2 321
ba28686d
SM
322 case $cur in
323 -*)
324 _lttng_complete_options
0c95f5b2
DG
325 ;;
326 esac
327}
328
fc256d99 329
fc256d99
DG
330
331_lttng_before_command() {
332 # Check if the previous word should alter the behavior
333 case $prev in
334 --group|-g)
335 COMPREPLY=( $(compgen -g -- $cur) )
336 return
337 ;;
338 --sessiond-path)
339 _filedir
340 return
341 ;;
342 esac
343
344 case $cur in
345 -*)
346 # If the current word starts with a dash, complete with options
ba28686d 347 _lttng_complete_options
fc256d99
DG
348 ;;
349 *)
350 # Otherwise complete with commands
ba28686d 351 _lttng_complete_commands
fc256d99
DG
352 ;;
353 esac
354}
355
356_lttng_after_command() {
357 local cmd_name
358
ba28686d 359 cmd_name=_lttng_cmd_${command_found//-/}
fc256d99
DG
360
361 type -t $cmd_name | grep -q 'function' && $cmd_name
362}
363
ba28686d
SM
364# Check if the word passed as the first parameter corresponds to a
365# command. $command must be set to the list of possible commands.
fc256d99
DG
366_lttng_is_command() {
367 for command in $commands; do
368 if [ "$1" == "$command" ]; then
369 return 0
370 fi
371 done
372
373 return 1
374}
375
ba28686d
SM
376# Try to find a command in the current command line. Possible commands
377# are passed in $commands.
378#
379# This function takes an optional parameter that indicates the index
380# where to start the search in COMP_WORDS. If omitted, it defaults to 1.
381#
382# If a command is found, $command_found is filled with the name of the
383# command and $command_found_index is set to the index of the command in
384# $COMP_WORDS. If no command is found, $command_found is an empty string
385# and $command_found_index is set to -1.
386_lttng_find_command() {
387 start=${1:-1}
fc256d99
DG
388
389 # The text of the found command
390 command_found=""
391
392 # The index of the found command in COMP_WORDS
393 command_found_index=-1
394
ba28686d 395 for (( i = start ; i < ${#COMP_WORDS[@]} ; i++ )); do
fc256d99
DG
396 _lttng_is_command ${COMP_WORDS[$i]}
397 if [ $? -eq 0 ]; then
398 command_found=${COMP_WORDS[$i]}
399 command_found_index=$i
400 break
401 fi
fc256d99 402 done
ba28686d
SM
403}
404
405_lttng_cursor_is_after_command() {
406 [ -n "$command_found" ] && [ "$COMP_CWORD" -gt "$command_found_index" ]
407}
408
409_lttng() {
410 local cur prev commands command_found command_found_index
411
412 # Get the current and previous word
413 _get_comp_words_by_ref cur prev
414
415 # Get the valid first-level LTTng commands and options
416 commands=$(lttng --list-commands)
417 options=$(lttng --list-options)
418
419 _lttng_find_command
fc256d99
DG
420
421 # Check if the cursor is before or after the command keyword
ba28686d 422 if _lttng_cursor_is_after_command; then
fc256d99
DG
423 _lttng_after_command
424 else
425 _lttng_before_command
426 fi
427}
428
429complete -F _lttng lttng
This page took 0.077125 seconds and 4 git commands to generate.