1a8b4c701SRamkumar Ramachandra# perf bash and zsh completion 2*b2441318SGreg Kroah-Hartman# SPDX-License-Identifier: GPL-2.0 3a8b4c701SRamkumar Ramachandra 4a8b4c701SRamkumar Ramachandra# Taken from git.git's completion script. 5a8b4c701SRamkumar Ramachandra__my_reassemble_comp_words_by_ref() 6a8b4c701SRamkumar Ramachandra{ 7a8b4c701SRamkumar Ramachandra local exclude i j first 8a8b4c701SRamkumar Ramachandra # Which word separators to exclude? 9a8b4c701SRamkumar Ramachandra exclude="${1//[^$COMP_WORDBREAKS]}" 10a8b4c701SRamkumar Ramachandra cword_=$COMP_CWORD 11a8b4c701SRamkumar Ramachandra if [ -z "$exclude" ]; then 12a8b4c701SRamkumar Ramachandra words_=("${COMP_WORDS[@]}") 13a8b4c701SRamkumar Ramachandra return 14a8b4c701SRamkumar Ramachandra fi 15a8b4c701SRamkumar Ramachandra # List of word completion separators has shrunk; 16a8b4c701SRamkumar Ramachandra # re-assemble words to complete. 17a8b4c701SRamkumar Ramachandra for ((i=0, j=0; i < ${#COMP_WORDS[@]}; i++, j++)); do 18a8b4c701SRamkumar Ramachandra # Append each nonempty word consisting of just 19a8b4c701SRamkumar Ramachandra # word separator characters to the current word. 20a8b4c701SRamkumar Ramachandra first=t 21a8b4c701SRamkumar Ramachandra while 22a8b4c701SRamkumar Ramachandra [ $i -gt 0 ] && 23a8b4c701SRamkumar Ramachandra [ -n "${COMP_WORDS[$i]}" ] && 24a8b4c701SRamkumar Ramachandra # word consists of excluded word separators 25a8b4c701SRamkumar Ramachandra [ "${COMP_WORDS[$i]//[^$exclude]}" = "${COMP_WORDS[$i]}" ] 26a8b4c701SRamkumar Ramachandra do 27a8b4c701SRamkumar Ramachandra # Attach to the previous token, 28a8b4c701SRamkumar Ramachandra # unless the previous token is the command name. 29a8b4c701SRamkumar Ramachandra if [ $j -ge 2 ] && [ -n "$first" ]; then 30a8b4c701SRamkumar Ramachandra ((j--)) 31a8b4c701SRamkumar Ramachandra fi 32a8b4c701SRamkumar Ramachandra first= 33a8b4c701SRamkumar Ramachandra words_[$j]=${words_[j]}${COMP_WORDS[i]} 34a8b4c701SRamkumar Ramachandra if [ $i = $COMP_CWORD ]; then 35a8b4c701SRamkumar Ramachandra cword_=$j 36a8b4c701SRamkumar Ramachandra fi 37a8b4c701SRamkumar Ramachandra if (($i < ${#COMP_WORDS[@]} - 1)); then 38a8b4c701SRamkumar Ramachandra ((i++)) 39a8b4c701SRamkumar Ramachandra else 40a8b4c701SRamkumar Ramachandra # Done. 41a8b4c701SRamkumar Ramachandra return 42a8b4c701SRamkumar Ramachandra fi 43a8b4c701SRamkumar Ramachandra done 44a8b4c701SRamkumar Ramachandra words_[$j]=${words_[j]}${COMP_WORDS[i]} 45a8b4c701SRamkumar Ramachandra if [ $i = $COMP_CWORD ]; then 46a8b4c701SRamkumar Ramachandra cword_=$j 47a8b4c701SRamkumar Ramachandra fi 48a8b4c701SRamkumar Ramachandra done 49a8b4c701SRamkumar Ramachandra} 50a8b4c701SRamkumar Ramachandra 511312c8a8SYunlong Song# Define preload_get_comp_words_by_ref="false", if the function 521312c8a8SYunlong Song# __perf_get_comp_words_by_ref() is required instead. 531312c8a8SYunlong Songpreload_get_comp_words_by_ref="true" 541312c8a8SYunlong Song 551312c8a8SYunlong Songif [ $preload_get_comp_words_by_ref = "true" ]; then 56a8b4c701SRamkumar Ramachandra type _get_comp_words_by_ref &>/dev/null || 571312c8a8SYunlong Song preload_get_comp_words_by_ref="false" 581312c8a8SYunlong Songfi 591312c8a8SYunlong Song[ $preload_get_comp_words_by_ref = "true" ] || 601312c8a8SYunlong Song__perf_get_comp_words_by_ref() 61a8b4c701SRamkumar Ramachandra{ 62a8b4c701SRamkumar Ramachandra local exclude cur_ words_ cword_ 63a8b4c701SRamkumar Ramachandra if [ "$1" = "-n" ]; then 64a8b4c701SRamkumar Ramachandra exclude=$2 65a8b4c701SRamkumar Ramachandra shift 2 66a8b4c701SRamkumar Ramachandra fi 67a8b4c701SRamkumar Ramachandra __my_reassemble_comp_words_by_ref "$exclude" 68a8b4c701SRamkumar Ramachandra cur_=${words_[cword_]} 69a8b4c701SRamkumar Ramachandra while [ $# -gt 0 ]; do 70a8b4c701SRamkumar Ramachandra case "$1" in 71a8b4c701SRamkumar Ramachandra cur) 72a8b4c701SRamkumar Ramachandra cur=$cur_ 73a8b4c701SRamkumar Ramachandra ;; 74a8b4c701SRamkumar Ramachandra prev) 75a8b4c701SRamkumar Ramachandra prev=${words_[$cword_-1]} 76a8b4c701SRamkumar Ramachandra ;; 77a8b4c701SRamkumar Ramachandra words) 78a8b4c701SRamkumar Ramachandra words=("${words_[@]}") 79a8b4c701SRamkumar Ramachandra ;; 80a8b4c701SRamkumar Ramachandra cword) 81a8b4c701SRamkumar Ramachandra cword=$cword_ 82a8b4c701SRamkumar Ramachandra ;; 83a8b4c701SRamkumar Ramachandra esac 84a8b4c701SRamkumar Ramachandra shift 85a8b4c701SRamkumar Ramachandra done 86a8b4c701SRamkumar Ramachandra} 87a8b4c701SRamkumar Ramachandra 881312c8a8SYunlong Song# Define preload__ltrim_colon_completions="false", if the function 891312c8a8SYunlong Song# __perf__ltrim_colon_completions() is required instead. 901312c8a8SYunlong Songpreload__ltrim_colon_completions="true" 911312c8a8SYunlong Song 921312c8a8SYunlong Songif [ $preload__ltrim_colon_completions = "true" ]; then 93a8b4c701SRamkumar Ramachandra type __ltrim_colon_completions &>/dev/null || 941312c8a8SYunlong Song preload__ltrim_colon_completions="false" 951312c8a8SYunlong Songfi 961312c8a8SYunlong Song[ $preload__ltrim_colon_completions = "true" ] || 971312c8a8SYunlong Song__perf__ltrim_colon_completions() 98a8b4c701SRamkumar Ramachandra{ 99a8b4c701SRamkumar Ramachandra if [[ "$1" == *:* && "$COMP_WORDBREAKS" == *:* ]]; then 100a8b4c701SRamkumar Ramachandra # Remove colon-word prefix from COMPREPLY items 101a8b4c701SRamkumar Ramachandra local colon_word=${1%"${1##*:}"} 102a8b4c701SRamkumar Ramachandra local i=${#COMPREPLY[*]} 103a8b4c701SRamkumar Ramachandra while [[ $((--i)) -ge 0 ]]; do 104a8b4c701SRamkumar Ramachandra COMPREPLY[$i]=${COMPREPLY[$i]#"$colon_word"} 105a8b4c701SRamkumar Ramachandra done 106a8b4c701SRamkumar Ramachandra fi 107a8b4c701SRamkumar Ramachandra} 108a8b4c701SRamkumar Ramachandra 109a8b4c701SRamkumar Ramachandra__perfcomp () 110a8b4c701SRamkumar Ramachandra{ 111a8b4c701SRamkumar Ramachandra COMPREPLY=( $( compgen -W "$1" -- "$2" ) ) 112a8b4c701SRamkumar Ramachandra} 113a8b4c701SRamkumar Ramachandra 114a8b4c701SRamkumar Ramachandra__perfcomp_colon () 115a8b4c701SRamkumar Ramachandra{ 116a8b4c701SRamkumar Ramachandra __perfcomp "$1" "$2" 1171312c8a8SYunlong Song if [ $preload__ltrim_colon_completions = "true" ]; then 118a8b4c701SRamkumar Ramachandra __ltrim_colon_completions $cur 1191312c8a8SYunlong Song else 1201312c8a8SYunlong Song __perf__ltrim_colon_completions $cur 1211312c8a8SYunlong Song fi 122a8b4c701SRamkumar Ramachandra} 123a8b4c701SRamkumar Ramachandra 12467afff48SYunlong Song__perf_prev_skip_opts () 12567afff48SYunlong Song{ 12667afff48SYunlong Song local i cmd_ cmds_ 12767afff48SYunlong Song 12867afff48SYunlong Song let i=cword-1 129eee200a6SYunlong Song cmds_=$($cmd $1 --list-cmds) 13067afff48SYunlong Song prev_skip_opts=() 13167afff48SYunlong Song while [ $i -ge 0 ]; do 132eee200a6SYunlong Song if [[ ${words[i]} == $1 ]]; then 133eee200a6SYunlong Song return 134eee200a6SYunlong Song fi 13567afff48SYunlong Song for cmd_ in $cmds_; do 13667afff48SYunlong Song if [[ ${words[i]} == $cmd_ ]]; then 13767afff48SYunlong Song prev_skip_opts=${words[i]} 13867afff48SYunlong Song return 13967afff48SYunlong Song fi 14067afff48SYunlong Song done 14167afff48SYunlong Song ((i--)) 14267afff48SYunlong Song done 14367afff48SYunlong Song} 1446fdd9cb7SYunlong Song 145a8b4c701SRamkumar Ramachandra__perf_main () 146a8b4c701SRamkumar Ramachandra{ 147a8b4c701SRamkumar Ramachandra local cmd 148a8b4c701SRamkumar Ramachandra 149a8b4c701SRamkumar Ramachandra cmd=${words[0]} 150a8b4c701SRamkumar Ramachandra COMPREPLY=() 151a8b4c701SRamkumar Ramachandra 15267afff48SYunlong Song # Skip options backward and find the last perf command 15367afff48SYunlong Song __perf_prev_skip_opts 154a8b4c701SRamkumar Ramachandra # List perf subcommands or long options 155e003ce54SYunlong Song if [ -z $prev_skip_opts ]; then 156a8b4c701SRamkumar Ramachandra if [[ $cur == --* ]]; then 1577335399aSYunlong Song cmds=$($cmd --list-opts) 158a8b4c701SRamkumar Ramachandra else 159a8b4c701SRamkumar Ramachandra cmds=$($cmd --list-cmds) 160a8b4c701SRamkumar Ramachandra fi 1617335399aSYunlong Song __perfcomp "$cmds" "$cur" 1626fdd9cb7SYunlong Song # List possible events for -e option 1636fdd9cb7SYunlong Song elif [[ $prev == @("-e"|"--event") && 1646fdd9cb7SYunlong Song $prev_skip_opts == @(record|stat|top) ]]; then 165a8b4c701SRamkumar Ramachandra evts=$($cmd list --raw-dump) 166a8b4c701SRamkumar Ramachandra __perfcomp_colon "$evts" "$cur" 16702fde323SYunlong Song else 1683bca2354SRamkumar Ramachandra # List subcommands for perf commands 1696fdd9cb7SYunlong Song if [[ $prev_skip_opts == @(kvm|kmem|mem|lock|sched| 1706fdd9cb7SYunlong Song |data|help|script|test|timechart|trace) ]]; then 17167afff48SYunlong Song subcmds=$($cmd $prev_skip_opts --list-cmds) 1728f2f5adaSRamkumar Ramachandra __perfcomp_colon "$subcmds" "$cur" 17302fde323SYunlong Song fi 174a8b4c701SRamkumar Ramachandra # List long option names 17502fde323SYunlong Song if [[ $cur == --* ]]; then 176eee200a6SYunlong Song subcmd=$prev_skip_opts 177eee200a6SYunlong Song __perf_prev_skip_opts $subcmd 178eee200a6SYunlong Song subcmd=$subcmd" "$prev_skip_opts 179a8b4c701SRamkumar Ramachandra opts=$($cmd $subcmd --list-opts) 180a8b4c701SRamkumar Ramachandra __perfcomp "$opts" "$cur" 181a8b4c701SRamkumar Ramachandra fi 18202fde323SYunlong Song fi 183a8b4c701SRamkumar Ramachandra} 184a8b4c701SRamkumar Ramachandra 185a8b4c701SRamkumar Ramachandraif [[ -n ${ZSH_VERSION-} ]]; then 186a8b4c701SRamkumar Ramachandra autoload -U +X compinit && compinit 187a8b4c701SRamkumar Ramachandra 188a8b4c701SRamkumar Ramachandra __perfcomp () 189a8b4c701SRamkumar Ramachandra { 190a8b4c701SRamkumar Ramachandra emulate -L zsh 191a8b4c701SRamkumar Ramachandra 192a8b4c701SRamkumar Ramachandra local c IFS=$' \t\n' 193a8b4c701SRamkumar Ramachandra local -a array 194a8b4c701SRamkumar Ramachandra 195a8b4c701SRamkumar Ramachandra for c in ${=1}; do 196a8b4c701SRamkumar Ramachandra case $c in 197a8b4c701SRamkumar Ramachandra --*=*|*.) ;; 198a8b4c701SRamkumar Ramachandra *) c="$c " ;; 199a8b4c701SRamkumar Ramachandra esac 200a8b4c701SRamkumar Ramachandra array[${#array[@]}+1]="$c" 201a8b4c701SRamkumar Ramachandra done 202a8b4c701SRamkumar Ramachandra 203a8b4c701SRamkumar Ramachandra compset -P '*[=:]' 204a8b4c701SRamkumar Ramachandra compadd -Q -S '' -a -- array && _ret=0 205a8b4c701SRamkumar Ramachandra } 206a8b4c701SRamkumar Ramachandra 207a8b4c701SRamkumar Ramachandra __perfcomp_colon () 208a8b4c701SRamkumar Ramachandra { 209a8b4c701SRamkumar Ramachandra emulate -L zsh 210a8b4c701SRamkumar Ramachandra 211a8b4c701SRamkumar Ramachandra local cur_="${2-$cur}" 212a8b4c701SRamkumar Ramachandra local c IFS=$' \t\n' 213a8b4c701SRamkumar Ramachandra local -a array 214a8b4c701SRamkumar Ramachandra 215a8b4c701SRamkumar Ramachandra if [[ "$cur_" == *:* ]]; then 216a8b4c701SRamkumar Ramachandra local colon_word=${cur_%"${cur_##*:}"} 217a8b4c701SRamkumar Ramachandra fi 218a8b4c701SRamkumar Ramachandra 219a8b4c701SRamkumar Ramachandra for c in ${=1}; do 220a8b4c701SRamkumar Ramachandra case $c in 221a8b4c701SRamkumar Ramachandra --*=*|*.) ;; 222a8b4c701SRamkumar Ramachandra *) c="$c " ;; 223a8b4c701SRamkumar Ramachandra esac 224a8b4c701SRamkumar Ramachandra array[$#array+1]=${c#"$colon_word"} 225a8b4c701SRamkumar Ramachandra done 226a8b4c701SRamkumar Ramachandra 227a8b4c701SRamkumar Ramachandra compset -P '*[=:]' 228a8b4c701SRamkumar Ramachandra compadd -Q -S '' -a -- array && _ret=0 229a8b4c701SRamkumar Ramachandra } 230a8b4c701SRamkumar Ramachandra 231a8b4c701SRamkumar Ramachandra _perf () 232a8b4c701SRamkumar Ramachandra { 233a8b4c701SRamkumar Ramachandra local _ret=1 cur cword prev 234a8b4c701SRamkumar Ramachandra cur=${words[CURRENT]} 235a8b4c701SRamkumar Ramachandra prev=${words[CURRENT-1]} 236a8b4c701SRamkumar Ramachandra let cword=CURRENT-1 237a8b4c701SRamkumar Ramachandra emulate ksh -c __perf_main 238a8b4c701SRamkumar Ramachandra let _ret && _default && _ret=0 239a8b4c701SRamkumar Ramachandra return _ret 240a8b4c701SRamkumar Ramachandra } 241a8b4c701SRamkumar Ramachandra 242a8b4c701SRamkumar Ramachandra compdef _perf perf 243a8b4c701SRamkumar Ramachandra return 244a8b4c701SRamkumar Ramachandrafi 245a8b4c701SRamkumar Ramachandra 246a8b4c701SRamkumar Ramachandratype perf &>/dev/null && 247a8b4c701SRamkumar Ramachandra_perf() 248a8b4c701SRamkumar Ramachandra{ 249a8b4c701SRamkumar Ramachandra local cur words cword prev 2501312c8a8SYunlong Song if [ $preload_get_comp_words_by_ref = "true" ]; then 251a8b4c701SRamkumar Ramachandra _get_comp_words_by_ref -n =: cur words cword prev 2521312c8a8SYunlong Song else 2531312c8a8SYunlong Song __perf_get_comp_words_by_ref -n =: cur words cword prev 2541312c8a8SYunlong Song fi 255a8b4c701SRamkumar Ramachandra __perf_main 256a8b4c701SRamkumar Ramachandra} && 257a8b4c701SRamkumar Ramachandra 258a8b4c701SRamkumar Ramachandracomplete -o bashdefault -o default -o nospace -F _perf perf 2>/dev/null \ 259a8b4c701SRamkumar Ramachandra || complete -o default -o nospace -F _perf perf 260