2 # Copyright (c) - 2012 Simon Marchi <simon.marchi@polymtl.ca>
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.
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
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.
18 # Generates COMPREPLY with the existing session names
19 _lttng_complete_sessions() {
21 # This code does nothing for now. When there is a mecanism to get the
22 # existing sessions, use it to fill the sessions variable.
25 COMPREPLY=( $(compgen -W "${sessions}" -- $cur) )
29 # Generates COMPREPLY with whatever is in the $options variable.
30 _lttng_complete_options() {
31 COMPREPLY=( $(compgen -W "${options}" -- $cur) )
34 # Generates COMPREPLY with whatever is in the $commands variable.
35 _lttng_complete_commands() {
36 COMPREPLY=( $(compgen -W "${commands}" -- $cur) )
39 _lttng_cmd_addcontext() {
40 options=$(lttng add-context --list-options)
44 _lttng_complete_sessions
57 _lttng_complete_options
63 _lttng_cmd_calibrate() {
64 options=$(lttng calibrate --list-options)
68 _lttng_complete_options
75 options=$(lttng create --list-options)
86 _lttng_complete_options
92 _lttng_cmd_destroy() {
93 options=$(lttng destroy --list-options)
97 _lttng_complete_options
101 _lttng_complete_sessions
106 _lttng_cmd_disablechannel() {
107 options=$(lttng disable-channel --list-options)
111 _lttng_complete_sessions
118 _lttng_complete_options
123 _lttng_cmd_disableevent() {
124 options=$(lttng disable-event --list-options)
128 _lttng_complete_sessions
138 _lttng_complete_options
144 _lttng_cmd_enablechannel() {
145 options=$(lttng enable-channel --list-options)
149 _lttng_complete_sessions
156 _lttng_complete_options
162 _lttng_cmd_enableevent() {
163 options=$(lttng enable-event --list-options)
167 _lttng_complete_sessions
183 _lttng_complete_options
190 options=$(lttng list --list-options)
200 _lttng_complete_options
204 _lttng_complete_sessions
209 _lttng_cmd_setsession() {
210 options=$(lttng set-session --list-options)
214 _lttng_complete_options
218 _lttng_complete_sessions
224 _lttng_cmd_snapshot() {
225 options=$(lttng snapshot --list-options)
226 commands=$(lttng snapshot --list-commands)
228 _lttng_find_command $((command_found_index + 1))
230 if _lttng_cursor_is_after_command; then
233 _lttng_complete_sessions
240 _lttng_complete_options
244 _lttng_complete_commands
249 options=$(lttng start --list-options)
253 _lttng_complete_options
257 _lttng_complete_sessions
264 options=$(lttng stop --list-options)
268 _lttng_complete_options
271 _lttng_complete_sessions
276 _lttng_cmd_version() {
277 options=$(lttng version --list-options)
281 _lttng_complete_options
287 options=$(lttng view --list-options)
291 _lttng_complete_options
298 _lttng_before_command() {
299 # Check if the previous word should alter the behavior
302 COMPREPLY=( $(compgen -g -- $cur) )
313 # If the current word starts with a dash, complete with options
314 _lttng_complete_options
317 # Otherwise complete with commands
318 _lttng_complete_commands
323 _lttng_after_command() {
326 cmd_name=_lttng_cmd_${command_found//-/}
328 type -t $cmd_name | grep -q 'function' && $cmd_name
331 # Check if the word passed as the first parameter corresponds to a
332 # command. $command must be set to the list of possible commands.
333 _lttng_is_command() {
334 for command in $commands; do
335 if [ "$1" == "$command" ]; then
343 # Try to find a command in the current command line. Possible commands
344 # are passed in $commands.
346 # This function takes an optional parameter that indicates the index
347 # where to start the search in COMP_WORDS. If omitted, it defaults to 1.
349 # If a command is found, $command_found is filled with the name of the
350 # command and $command_found_index is set to the index of the command in
351 # $COMP_WORDS. If no command is found, $command_found is an empty string
352 # and $command_found_index is set to -1.
353 _lttng_find_command() {
356 # The text of the found command
359 # The index of the found command in COMP_WORDS
360 command_found_index=-1
362 for (( i = start ; i < ${#COMP_WORDS[@]} ; i++ )); do
363 _lttng_is_command ${COMP_WORDS[$i]}
364 if [ $? -eq 0 ]; then
365 command_found=${COMP_WORDS[$i]}
366 command_found_index=$i
372 _lttng_cursor_is_after_command() {
373 [ -n "$command_found" ] && [ "$COMP_CWORD" -gt "$command_found_index" ]
377 local cur prev commands command_found command_found_index
379 # Get the current and previous word
380 _get_comp_words_by_ref cur prev
382 # Get the valid first-level LTTng commands and options
383 commands=$(lttng --list-commands)
384 options=$(lttng --list-options)
388 # Check if the cursor is before or after the command keyword
389 if _lttng_cursor_is_after_command; then
392 _lttng_before_command
396 complete -F _lttng lttng