]> git.ipfire.org Git - thirdparty/systemd.git/blame - shell-completion/bash/udevadm
Merge pull request #22791 from keszybz/bootctl-invert-order
[thirdparty/systemd.git] / shell-completion / bash / udevadm
CommitLineData
d611dadc 1# udevadm(8) completion -*- shell-script -*-
db9ecf05 2# SPDX-License-Identifier: LGPL-2.1-or-later
d611dadc
MB
3#
4# This file is part of systemd.
5#
96b2fb93 6# Copyright © 2010 Ran Benita
d611dadc
MB
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 () {
843cfcb1
ZJS
22 local w word=$1; shift
23 for w in "$@"; do
24 [[ $w = "$word" ]] && return
25 done
d611dadc
MB
26}
27
28__get_all_sysdevs() {
843cfcb1
ZJS
29 local -a devs=(/sys/bus/*/devices/*/ /sys/class/*/*/)
30 printf '%s\n' "${devs[@]%/}"
d611dadc
MB
31}
32
19582502 33__get_all_devs() {
843cfcb1
ZJS
34 local i
35 for i in /dev/* /dev/*/*; do
36 echo $i
37 done
19582502
YW
38}
39
1f795124 40__get_all_device_units() {
1cabd2d0 41 systemctl list-units -t device --full --no-legend --no-pager --plain 2>/dev/null | \
843cfcb1 42 { while read -r a b; do echo "$a"; done; }
1f795124
YW
43}
44
d611dadc 45_udevadm() {
843cfcb1
ZJS
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'
ae760f4b 51 [INFO_STANDALONE]='-r --root -a --attribute-walk -x --export -e --export-db -c --cleanup-db
6c1482b2
FS
52 -w --wait-for-initialization --value'
53 [INFO_ARG]='-q --query -p --path -n --name -P --export-prefix -d --device-id-of-file --property'
678f2b16
DK
54 [TRIGGER_STANDALONE]='-v --verbose -n --dry-run -q --quiet -w --settle --wait-daemon --uuid
55 --initialized-match --initialized-nomatch'
843cfcb1
ZJS
56 [TRIGGER_ARG]='-t --type -c --action -s --subsystem-match -S --subsystem-nomatch
57 -a --attr-match -A --attr-nomatch -p --property-match
873cf95c
YW
58 -g --tag-match -y --sysname-match --name-match -b --parent-match
59 --prioritized-subsystem'
843cfcb1
ZJS
60 [SETTLE]='-t --timeout -E --exit-if-exists'
61 [CONTROL_STANDALONE]='-e --exit -s --stop-exec-queue -S --start-exec-queue -R --reload --ping'
62 [CONTROL_ARG]='-l --log-priority -p --property -m --children-max -t --timeout'
63 [MONITOR_STANDALONE]='-k --kernel -u --udev -p --property'
64 [MONITOR_ARG]='-s --subsystem-match -t --tag-match'
65 [TEST]='-a --action -N --resolve-names'
7ce05a8d 66 [TEST_BUILTIN]='-a --action'
843cfcb1
ZJS
67 )
68
69 local verbs=(info trigger settle control monitor test-builtin test)
70 local builtins=(blkid btrfs hwdb input_id keyboard kmod net_id net_setup_link path_id usb_id uaccess)
71
72 for ((i=0; i < COMP_CWORD; i++)); do
73 if __contains_word "${COMP_WORDS[i]}" "${verbs[@]}"; then
74 verb=${COMP_WORDS[i]}
75 break
d611dadc 76 fi
843cfcb1 77 done
d611dadc 78
36ec0268 79 if [[ -z ${verb-} ]]; then
843cfcb1
ZJS
80 if [[ "$cur" = -* ]]; then
81 COMPREPLY=( $(compgen -W '${OPTS[COMMON]} ${OPTS[DEBUG]}' -- "$cur") )
82 else
83 COMPREPLY=( $(compgen -W '${verbs[*]}' -- "$cur") )
84 fi
d611dadc 85 return 0
843cfcb1
ZJS
86 fi
87
88 case $verb in
89 'info')
90 if __contains_word "$prev" ${OPTS[INFO_ARG]}; then
91 case $prev in
92 -q|--query)
93 comps='name symlink path property all'
94 ;;
95 -p|--path)
96 comps=$( __get_all_sysdevs )
97 local IFS=$'\n'
98 ;;
99 -n|--name)
100 comps=$( __get_all_devs )
101 ;;
102 *)
103 comps=''
104 ;;
105 esac
106 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
107 return 0
108 fi
109
110 if [[ $cur = -* ]]; then
111 comps="${OPTS[COMMON]} ${OPTS[INFO_STANDALONE]} ${OPTS[INFO_ARG]}"
112 else
113 comps=$( __get_all_sysdevs; __get_all_device_units )
114 local IFS=$'\n'
115 fi
116 ;;
117
118 'trigger')
119 if __contains_word "$prev" ${OPTS[TRIGGER_ARG]}; then
120 case $prev in
121 -t|--type)
1baeee57 122 comps='all devices subsystems'
843cfcb1
ZJS
123 ;;
124 -c|--action)
7ce72782 125 comps=$( udevadm trigger --action help )
843cfcb1
ZJS
126 ;;
127 -y|--sysname-match|-b|--parent-match)
128 comps=$( __get_all_sysdevs )
129 local IFS=$'\n'
130 ;;
131 --name-match)
132 comps=$( __get_all_devs )
133 ;;
134 *)
135 comps=''
136 ;;
137 esac
138 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
139 return 0
140 fi
141
142 if [[ $cur = -* ]]; then
143 comps="${OPTS[COMMON]} ${OPTS[TRIGGER_STANDALONE]} ${OPTS[TRIGGER_ARG]}"
144 else
145 comps=$( __get_all_sysdevs; __get_all_device_units )
146 local IFS=$'\n'
147 fi
148 ;;
149
150 'settle')
151 if __contains_word "$prev" ${OPTS[SETTLE]}; then
152 case $prev in
153 -E|--exit-if-exists)
154 comps=$( compgen -A file -- "$cur" )
155 ;;
156 *)
157 comps=''
158 ;;
159 esac
160 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
161 return 0
162 fi
163
164 comps="${OPTS[COMMON]} ${OPTS[SETTLE]}"
165 ;;
166
167 'control')
168 if __contains_word "$prev" ${OPTS[CONTROL_ARG]}; then
169 case $prev in
170 -l|--log-priority)
171 comps='alert crit debug emerg err info notice warning'
172 ;;
173 *)
174 comps=''
175 ;;
176 esac
177 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
178 return 0
179 fi
180
181 comps="${OPTS[COMMON]} ${OPTS[CONTROL_STANDALONE]} ${OPTS[CONTROL_ARG]}"
182 ;;
183
184 'monitor')
185 if __contains_word "$prev" ${OPTS[MONITOR_ARG]}; then
186 case $prev in
187 *)
188 comps=''
189 ;;
190 esac
191 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
192 return 0
193 fi
194
195 comps="${OPTS[COMMON]} ${OPTS[MONITOR_STANDALONE]} ${OPTS[MONITOR_ARG]}"
196 ;;
197
198 'test')
199 if __contains_word "$prev" ${OPTS[TEST]}; then
200 case $prev in
201 -a|--action)
7ce72782 202 comps=$( udevadm test --action help )
843cfcb1
ZJS
203 ;;
204 -N|--resolve-names)
205 comps='early late never'
206 ;;
207 esac
208 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
209 return 0
210 fi
211
212 if [[ $cur = -* ]]; then
213 comps="${OPTS[COMMON]} ${OPTS[TEST]}"
214 else
215 comps=$( __get_all_sysdevs )
216 local IFS=$'\n'
217 fi
218 ;;
219
220 'test-builtin')
7ce05a8d
YW
221 if __contains_word "$prev" ${OPTS[TEST_BUILTIN]}; then
222 case $prev in
223 -a|--action)
224 comps=$( udevadm test-builtin --action help )
225 ;;
226 esac
227 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
228 return 0
229 fi
230
843cfcb1
ZJS
231 for ((i=0; i < COMP_CWORD; i++)); do
232 if __contains_word "${COMP_WORDS[i]}" "${builtins[@]}"; then
233 builtin=${COMP_WORDS[i]}
234 break
235 fi
236 done
237
238 if [[ -z $builtin ]]; then
239 comps="${builtins[@]}"
240 elif [[ $cur = -* ]]; then
7ce05a8d 241 comps="${OPTS[COMMON]} ${OPTS[TEST_BUILTIN]}"
843cfcb1
ZJS
242 else
243 comps=$( __get_all_sysdevs )
244 local IFS=$'\n'
245 fi
246 ;;
247
248 *)
249 comps=${VERBS[*]}
250 ;;
251 esac
252
253 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
254 return 0
d611dadc
MB
255}
256
257complete -F _udevadm udevadm