]> git.ipfire.org Git - thirdparty/systemd.git/blob - shell-completion/bash/udevadm
Merge pull request #11827 from keszybz/pkgconfig-variables
[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 'trigger')
114 if __contains_word "$prev" ${OPTS[TRIGGER_ARG]}; then
115 case $prev in
116 -t|--type)
117 comps='devices subsystems'
118 ;;
119 -c|--action)
120 comps='add change remove bind unbind'
121 ;;
122 -y|--sysname-match|-b|--parent-match)
123 comps=$( __get_all_sysdevs )
124 local IFS=$'\n'
125 ;;
126 --name-match)
127 comps=$( __get_all_devs )
128 ;;
129 *)
130 comps=''
131 ;;
132 esac
133 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
134 return 0
135 fi
136
137 if [[ $cur = -* ]]; then
138 comps="${OPTS[COMMON]} ${OPTS[TRIGGER_STANDALONE]} ${OPTS[TRIGGER_ARG]}"
139 else
140 comps=$( __get_all_sysdevs; __get_all_device_units )
141 local IFS=$'\n'
142 fi
143 ;;
144 'settle')
145 if __contains_word "$prev" ${OPTS[SETTLE]}; then
146 case $prev in
147 -E|--exit-if-exists)
148 comps=$( compgen -A file -- "$cur" )
149 ;;
150 *)
151 comps=''
152 ;;
153 esac
154 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
155 return 0
156 fi
157
158 comps="${OPTS[COMMON]} ${OPTS[SETTLE]}"
159 ;;
160 'control')
161 if __contains_word "$prev" ${OPTS[CONTROL_ARG]}; then
162 case $prev in
163 -l|--log-priority)
164 comps='alert crit debug emerg err info notice warning'
165 ;;
166 *)
167 comps=''
168 ;;
169 esac
170 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
171 return 0
172 fi
173
174 comps="${OPTS[COMMON]} ${OPTS[CONTROL_STANDALONE]} ${OPTS[CONTROL_ARG]}"
175 ;;
176 'monitor')
177 if __contains_word "$prev" ${OPTS[MONITOR_ARG]}; then
178 case $prev in
179 *)
180 comps=''
181 ;;
182 esac
183 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
184 return 0
185 fi
186
187 comps="${OPTS[COMMON]} ${OPTS[MONITOR_STANDALONE]} ${OPTS[MONITOR_ARG]}"
188 ;;
189 'test')
190 if __contains_word "$prev" ${OPTS[TEST]}; then
191 case $prev in
192 -a|--action)
193 comps='add change remove bind unbind'
194 ;;
195 -N|--resolve-names)
196 comps='early late never'
197 ;;
198 esac
199 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
200 return 0
201 fi
202
203 if [[ $cur = -* ]]; then
204 comps="${OPTS[COMMON]} ${OPTS[TEST]}"
205 else
206 comps=$( __get_all_sysdevs )
207 local IFS=$'\n'
208 fi
209 ;;
210 'test-builtin')
211 for ((i=0; i < COMP_CWORD; i++)); do
212 if __contains_word "${COMP_WORDS[i]}" "${builtins[@]}"; then
213 builtin=${COMP_WORDS[i]}
214 break
215 fi
216 done
217
218 if [[ -z $builtin ]]; then
219 comps="${builtins[@]}"
220 elif [[ $cur = -* ]]; then
221 comps="${OPTS[COMMON]}"
222 else
223 comps=$( __get_all_sysdevs )
224 local IFS=$'\n'
225 fi
226 ;;
227 *)
228 comps=${VERBS[*]}
229 ;;
230 esac
231
232 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
233 return 0
234 }
235
236 complete -F _udevadm udevadm