]> git.ipfire.org Git - thirdparty/systemd.git/blob - shell-completion/bash/udevadm
shell-completion: use 4 space indentation too
[thirdparty/systemd.git] / shell-completion / bash / udevadm
1 # udevadm(8) completion -*- shell-script -*-
2 # SPDX-License-Identifier: LGPL-2.1+
3 #
4 # This file is part of systemd.
5 #
6 # Copyright © 2010 Ran Benita
7 #
8 # systemd is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU Lesser General Public License as published by
10 # the Free Software Foundation; either version 2.1 of the License, or
11 # (at your option) any later version.
12 #
13 # systemd is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # General Public License for more details.
17 #
18 # You should have received a copy of the GNU Lesser General Public License
19 # along with systemd; If not, see <http://www.gnu.org/licenses/>.
20
21 __contains_word () {
22 local w word=$1; shift
23 for w in "$@"; do
24 [[ $w = "$word" ]] && return
25 done
26 }
27
28 __get_all_sysdevs() {
29 local -a devs=(/sys/bus/*/devices/*/ /sys/class/*/*/)
30 printf '%s\n' "${devs[@]%/}"
31 }
32
33 __get_all_devs() {
34 local i
35 for i in /dev/* /dev/*/*; do
36 echo $i
37 done
38 }
39
40 __get_all_device_units() {
41 systemctl list-units -t device --full --no-legend --no-pager 2>/dev/null | \
42 { while read -r a b; do echo "$a"; done; }
43 }
44
45 _udevadm() {
46 local i verb comps builtin
47 local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
48 local -A OPTS=(
49 [COMMON]='-h --help -V --version'
50 [DEBUG]='-d --debug'
51 [INFO_STANDALONE]='-r --root -a --attribute-walk -x --export -e --export-db -c --cleanup-db'
52 [INFO_ARG]='-q --query -p --path -n --name -P --export-prefix -d --device-id-of-file'
53 [TRIGGER_STANDALONE]='-v --verbose -n --dry-run -w --settle --wait-daemon'
54 [TRIGGER_ARG]='-t --type -c --action -s --subsystem-match -S --subsystem-nomatch
55 -a --attr-match -A --attr-nomatch -p --property-match
56 -g --tag-match -y --sysname-match --name-match -b --parent-match'
57 [SETTLE]='-t --timeout -E --exit-if-exists'
58 [CONTROL_STANDALONE]='-e --exit -s --stop-exec-queue -S --start-exec-queue -R --reload --ping'
59 [CONTROL_ARG]='-l --log-priority -p --property -m --children-max -t --timeout'
60 [MONITOR_STANDALONE]='-k --kernel -u --udev -p --property'
61 [MONITOR_ARG]='-s --subsystem-match -t --tag-match'
62 [TEST]='-a --action -N --resolve-names'
63 )
64
65 local verbs=(info trigger settle control monitor test-builtin test)
66 local builtins=(blkid btrfs hwdb input_id keyboard kmod net_id net_setup_link path_id usb_id uaccess)
67
68 for ((i=0; i < COMP_CWORD; i++)); do
69 if __contains_word "${COMP_WORDS[i]}" "${verbs[@]}"; then
70 verb=${COMP_WORDS[i]}
71 break
72 fi
73 done
74
75 if [[ -z $verb ]]; then
76 if [[ "$cur" = -* ]]; then
77 COMPREPLY=( $(compgen -W '${OPTS[COMMON]} ${OPTS[DEBUG]}' -- "$cur") )
78 else
79 COMPREPLY=( $(compgen -W '${verbs[*]}' -- "$cur") )
80 fi
81 return 0
82 fi
83
84 case $verb in
85 'info')
86 if __contains_word "$prev" ${OPTS[INFO_ARG]}; then
87 case $prev in
88 -q|--query)
89 comps='name symlink path property all'
90 ;;
91 -p|--path)
92 comps=$( __get_all_sysdevs )
93 local IFS=$'\n'
94 ;;
95 -n|--name)
96 comps=$( __get_all_devs )
97 ;;
98 *)
99 comps=''
100 ;;
101 esac
102 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
103 return 0
104 fi
105
106 if [[ $cur = -* ]]; then
107 comps="${OPTS[COMMON]} ${OPTS[INFO_STANDALONE]} ${OPTS[INFO_ARG]}"
108 else
109 comps=$( __get_all_sysdevs; __get_all_device_units )
110 local IFS=$'\n'
111 fi
112 ;;
113
114 'trigger')
115 if __contains_word "$prev" ${OPTS[TRIGGER_ARG]}; then
116 case $prev in
117 -t|--type)
118 comps='devices subsystems'
119 ;;
120 -c|--action)
121 comps='add change remove bind unbind'
122 ;;
123 -y|--sysname-match|-b|--parent-match)
124 comps=$( __get_all_sysdevs )
125 local IFS=$'\n'
126 ;;
127 --name-match)
128 comps=$( __get_all_devs )
129 ;;
130 *)
131 comps=''
132 ;;
133 esac
134 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
135 return 0
136 fi
137
138 if [[ $cur = -* ]]; then
139 comps="${OPTS[COMMON]} ${OPTS[TRIGGER_STANDALONE]} ${OPTS[TRIGGER_ARG]}"
140 else
141 comps=$( __get_all_sysdevs; __get_all_device_units )
142 local IFS=$'\n'
143 fi
144 ;;
145
146 'settle')
147 if __contains_word "$prev" ${OPTS[SETTLE]}; then
148 case $prev in
149 -E|--exit-if-exists)
150 comps=$( compgen -A file -- "$cur" )
151 ;;
152 *)
153 comps=''
154 ;;
155 esac
156 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
157 return 0
158 fi
159
160 comps="${OPTS[COMMON]} ${OPTS[SETTLE]}"
161 ;;
162
163 'control')
164 if __contains_word "$prev" ${OPTS[CONTROL_ARG]}; then
165 case $prev in
166 -l|--log-priority)
167 comps='alert crit debug emerg err info notice warning'
168 ;;
169 *)
170 comps=''
171 ;;
172 esac
173 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
174 return 0
175 fi
176
177 comps="${OPTS[COMMON]} ${OPTS[CONTROL_STANDALONE]} ${OPTS[CONTROL_ARG]}"
178 ;;
179
180 'monitor')
181 if __contains_word "$prev" ${OPTS[MONITOR_ARG]}; then
182 case $prev in
183 *)
184 comps=''
185 ;;
186 esac
187 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
188 return 0
189 fi
190
191 comps="${OPTS[COMMON]} ${OPTS[MONITOR_STANDALONE]} ${OPTS[MONITOR_ARG]}"
192 ;;
193
194 'test')
195 if __contains_word "$prev" ${OPTS[TEST]}; then
196 case $prev in
197 -a|--action)
198 comps='add change remove bind unbind'
199 ;;
200 -N|--resolve-names)
201 comps='early late never'
202 ;;
203 esac
204 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
205 return 0
206 fi
207
208 if [[ $cur = -* ]]; then
209 comps="${OPTS[COMMON]} ${OPTS[TEST]}"
210 else
211 comps=$( __get_all_sysdevs )
212 local IFS=$'\n'
213 fi
214 ;;
215
216 'test-builtin')
217 for ((i=0; i < COMP_CWORD; i++)); do
218 if __contains_word "${COMP_WORDS[i]}" "${builtins[@]}"; then
219 builtin=${COMP_WORDS[i]}
220 break
221 fi
222 done
223
224 if [[ -z $builtin ]]; then
225 comps="${builtins[@]}"
226 elif [[ $cur = -* ]]; then
227 comps="${OPTS[COMMON]}"
228 else
229 comps=$( __get_all_sysdevs )
230 local IFS=$'\n'
231 fi
232 ;;
233
234 *)
235 comps=${VERBS[*]}
236 ;;
237 esac
238
239 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
240 return 0
241 }
242
243 complete -F _udevadm udevadm