| 1 | #!/bin/bash |
| 2 | # LTTng Trace Control bash completion |
| 3 | # |
| 4 | |
| 5 | _lttng_complete_sessions() { |
| 6 | local sessions |
| 7 | if ! _complete_as_root ; then |
| 8 | sessions=$(for i in $(ls -d ~/.lttng/*/); do basename $i; done) |
| 9 | COMPREPLY=( $(compgen -W "${sessions}" -- $cur) ) |
| 10 | #else |
| 11 | # Permission denied, what should we do ? |
| 12 | # sessions=$(for i in $(ls -d ~root/.lttng/*/); do basename $i; done) |
| 13 | #COMPREPLY=( $(compgen -W "${sessions}" -- $cur) ) |
| 14 | fi |
| 15 | } |
| 16 | |
| 17 | _lttng_create() { |
| 18 | local create_opts |
| 19 | create_opts="-h --help -o --output" |
| 20 | |
| 21 | case $prev in |
| 22 | --output|-o) |
| 23 | _filedir -d |
| 24 | return |
| 25 | ;; |
| 26 | esac |
| 27 | |
| 28 | case $cur in |
| 29 | -*) |
| 30 | COMPREPLY=( $(compgen -W "${create_opts}" -- $cur) ) |
| 31 | return |
| 32 | ;; |
| 33 | esac |
| 34 | } |
| 35 | |
| 36 | _lttng_start_stop() { |
| 37 | local start_stop_opts |
| 38 | start_stop_opts="-h --help" |
| 39 | |
| 40 | case $cur in |
| 41 | -*) |
| 42 | COMPREPLY=( $(compgen -W "${start_stop_opts}" -- $cur) ) |
| 43 | ;; |
| 44 | *) |
| 45 | _lttng_complete_sessions |
| 46 | ;; |
| 47 | esac |
| 48 | } |
| 49 | |
| 50 | _lttng_opts() { |
| 51 | local opts |
| 52 | opts=$(lttng --dump-options) |
| 53 | |
| 54 | COMPREPLY=( $(compgen -W "${opts}" -- $cur) ) |
| 55 | } |
| 56 | |
| 57 | _lttng_commands() { |
| 58 | COMPREPLY=( $(compgen -W "$commands" -- $cur) ) |
| 59 | } |
| 60 | |
| 61 | _lttng_before_command() { |
| 62 | # Check if the previous word should alter the behavior |
| 63 | case $prev in |
| 64 | --group|-g) |
| 65 | COMPREPLY=( $(compgen -g -- $cur) ) |
| 66 | return |
| 67 | ;; |
| 68 | esac |
| 69 | |
| 70 | case $cur in |
| 71 | -*) |
| 72 | # If the current word starts with a dash, complete with options |
| 73 | _lttng_opts |
| 74 | ;; |
| 75 | *) |
| 76 | # Otherwise complete with commands |
| 77 | _lttng_commands |
| 78 | ;; |
| 79 | esac |
| 80 | } |
| 81 | |
| 82 | _lttng_after_command() { |
| 83 | case $command_found in |
| 84 | "create") |
| 85 | _lttng_create |
| 86 | ;; |
| 87 | "start"|"stop") |
| 88 | _lttng_start_stop |
| 89 | ;; |
| 90 | esac |
| 91 | } |
| 92 | |
| 93 | _lttng_is_command() { |
| 94 | for command in $commands; do |
| 95 | if [ "$1" == "$command" ]; then |
| 96 | return 0 |
| 97 | fi |
| 98 | done |
| 99 | |
| 100 | return 1 |
| 101 | } |
| 102 | |
| 103 | _lttng() { |
| 104 | local cur prev commands command_found command_found_index |
| 105 | |
| 106 | cur="${COMP_WORDS[COMP_CWORD]}" |
| 107 | prev="${COMP_WORDS[COMP_CWORD-1]}" |
| 108 | commands=$(lttng --dump-commands) |
| 109 | |
| 110 | command_found="" |
| 111 | command_found_index=-1 |
| 112 | |
| 113 | for (( i = 1 ; i < ${#COMP_WORDS[@]} ; i++ )); do |
| 114 | _lttng_is_command ${COMP_WORDS[$i]} |
| 115 | if [ $? -eq 0 ]; then |
| 116 | command_found=${COMP_WORDS[$i]} |
| 117 | command_found_index=$i |
| 118 | break |
| 119 | fi |
| 120 | |
| 121 | done |
| 122 | |
| 123 | if [ -n "$command_found" ] && [ "$COMP_CWORD" -gt "$command_found_index" ]; then |
| 124 | _lttng_after_command |
| 125 | else |
| 126 | _lttng_before_command |
| 127 | fi |
| 128 | } |
| 129 | |
| 130 | complete -F _lttng lttng |
| 131 | |