xref: /linux/tools/power/cpupower/cpupower-completion.sh (revision 8e07e0e3964ca4e23ce7b68e2096fe660a888942)
1# -*- shell-script -*-
2# bash completion script for cpupower
3# Taken from git.git's completion script.
4
5_cpupower_commands="frequency-info frequency-set idle-info idle-set set info monitor"
6
7_frequency_info ()
8{
9	local flags="-f -w -l -d -p -g -a -s -y -o -m -n --freq --hwfreq --hwlimits --driver --policy --governors --related-cpus --affected-cpus --stats --latency --proc --human --no-rounding"
10	local prev="${COMP_WORDS[COMP_CWORD-1]}"
11	local cur="${COMP_WORDS[COMP_CWORD]}"
12	case "$prev" in
13		frequency-info) COMPREPLY=($(compgen -W "$flags" -- "$cur")) ;;
14	esac
15}
16
17_frequency_set ()
18{
19	local flags="-f -g --freq --governor -d --min -u --max -r --related"
20	local prev="${COMP_WORDS[COMP_CWORD-1]}"
21	local cur="${COMP_WORDS[COMP_CWORD]}"
22	case "$prev" in
23		-f| --freq | -d | --min | -u | --max)
24		if [ -d /sys/devices/system/cpu/cpufreq/ ] ; then
25			COMPREPLY=($(compgen -W '$(cat $(ls -d /sys/devices/system/cpu/cpufreq/policy* | head -1)/scaling_available_frequencies)' -- "$cur"))
26		fi ;;
27		-g| --governor)
28		if [ -d /sys/devices/system/cpu/cpufreq/ ] ; then
29			COMPREPLY=($(compgen -W '$(cat $(ls -d /sys/devices/system/cpu/cpufreq/policy* | head -1)/scaling_available_governors)' -- "$cur"))
30		fi;;
31		frequency-set) COMPREPLY=($(compgen -W "$flags" -- "$cur")) ;;
32	esac
33}
34
35_idle_info()
36{
37	local flags="-f --silent"
38	local prev="${COMP_WORDS[COMP_CWORD-1]}"
39	local cur="${COMP_WORDS[COMP_CWORD]}"
40	case "$prev" in
41		idle-info) COMPREPLY=($(compgen -W "$flags" -- "$cur")) ;;
42	esac
43}
44
45_idle_set()
46{
47	local flags="-d --disable -e --enable -D --disable-by-latency -E --enable-all"
48	local prev="${COMP_WORDS[COMP_CWORD-1]}"
49	local cur="${COMP_WORDS[COMP_CWORD]}"
50	case "$prev" in
51		idle-set) COMPREPLY=($(compgen -W "$flags" -- "$cur")) ;;
52	esac
53}
54
55_set()
56{
57	local flags="--perf-bias, -b"
58	local prev="${COMP_WORDS[COMP_CWORD-1]}"
59	local cur="${COMP_WORDS[COMP_CWORD]}"
60	case "$prev" in
61		set) COMPREPLY=($(compgen -W "$flags" -- "$cur")) ;;
62	esac
63}
64
65_monitor()
66{
67	local flags="-l -m -i -c -v"
68	local prev="${COMP_WORDS[COMP_CWORD-1]}"
69	local cur="${COMP_WORDS[COMP_CWORD]}"
70	case "$prev" in
71		monitor) COMPREPLY=($(compgen -W "$flags" -- "$cur")) ;;
72	esac
73}
74
75_taskset()
76{
77	local prev_to_prev="${COMP_WORDS[COMP_CWORD-2]}"
78	local prev="${COMP_WORDS[COMP_CWORD-1]}"
79	local cur="${COMP_WORDS[COMP_CWORD]}"
80	case "$prev_to_prev" in
81		-c|--cpu) COMPREPLY=($(compgen -W "$_cpupower_commands" -- "$cur")) ;;
82	esac
83	case "$prev" in
84		frequency-info) _frequency_info ;;
85		frequency-set) _frequency_set ;;
86		idle-info) _idle_info ;;
87		idle-set) _idle_set ;;
88		set) _set ;;
89		monitor) _monitor ;;
90	esac
91
92}
93
94_cpupower ()
95{
96	local i
97	local c=1
98	local command
99
100	while test $c -lt $COMP_CWORD; do
101		if test $c == 1; then
102			command="${COMP_WORDS[c]}"
103		fi
104		c=$((++c))
105	done
106
107	# Complete name of subcommand if the user has not finished typing it yet.
108	if test $c -eq $COMP_CWORD -a -z "$command"; then
109		COMPREPLY=($(compgen -W "help -v --version -c --cpu $_cpupower_commands" -- "${COMP_WORDS[COMP_CWORD]}"))
110		return
111	fi
112
113	# Complete arguments to subcommands.
114	case "$command" in
115		-v|--version) return ;;
116		-c|--cpu) _taskset ;;
117		help) COMPREPLY=($(compgen -W "$_cpupower_commands" -- "${COMP_WORDS[COMP_CWORD]}")) ;;
118		frequency-info) _frequency_info ;;
119		frequency-set) _frequency_set ;;
120		idle-info) _idle_info ;;
121		idle-set) _idle_set ;;
122		set) _set ;;
123		monitor) _monitor ;;
124	esac
125}
126
127complete -o bashdefault -o default -F _cpupower cpupower 2>/dev/null \
128    || complete -o default -F _cpupower cpupower
129