]>
Commit | Line | Data |
---|---|---|
d611dadc | 1 | # udevadm(8) completion -*- shell-script -*- |
7059062c | 2 | # SPDX-License-Identifier: LGPL-2.1+ |
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 () { | |
a72d698d DR |
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() { | |
29 | local -a devs=(/sys/bus/*/devices/*/ /sys/class/*/*/) | |
30 | printf '%s\n' "${devs[@]%/}" | |
31 | } | |
32 | ||
19582502 YW |
33 | __get_all_devs() { |
34 | local i | |
35 | for i in /dev/* /dev/*/*; do | |
36 | echo $i | |
37 | done | |
38 | } | |
39 | ||
1f795124 YW |
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 | ||
d611dadc | 45 | _udevadm() { |
19582502 | 46 | local i verb comps builtin |
d611dadc | 47 | local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]} |
19582502 YW |
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' | |
f77bb3a3 | 53 | [TRIGGER_STANDALONE]='-v --verbose -n --dry-run -w --settle --wait-daemon' |
19582502 YW |
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' | |
f77bb3a3 | 58 | [CONTROL_STANDALONE]='-e --exit -s --stop-exec-queue -S --start-exec-queue -R --reload --ping' |
19582502 YW |
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 | ) | |
d611dadc | 64 | |
19582502 YW |
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) | |
d611dadc | 67 | |
3ce09b7d | 68 | for ((i=0; i < COMP_CWORD; i++)); do |
19582502 | 69 | if __contains_word "${COMP_WORDS[i]}" "${verbs[@]}"; then |
d611dadc MB |
70 | verb=${COMP_WORDS[i]} |
71 | break | |
72 | fi | |
73 | done | |
74 | ||
b5b7ea75 | 75 | if [[ -z $verb ]]; then |
19582502 YW |
76 | if [[ "$cur" = -* ]]; then |
77 | COMPREPLY=( $(compgen -W '${OPTS[COMMON]} ${OPTS[DEBUG]}' -- "$cur") ) | |
78 | else | |
79 | COMPREPLY=( $(compgen -W '${verbs[*]}' -- "$cur") ) | |
80 | fi | |
d611dadc MB |
81 | return 0 |
82 | fi | |
83 | ||
b5b7ea75 DR |
84 | case $verb in |
85 | 'info') | |
19582502 YW |
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 | ||
b5b7ea75 | 106 | if [[ $cur = -* ]]; then |
19582502 | 107 | comps="${OPTS[COMMON]} ${OPTS[INFO_STANDALONE]} ${OPTS[INFO_ARG]}" |
b5b7ea75 | 108 | else |
1f795124 | 109 | comps=$( __get_all_sysdevs; __get_all_device_units ) |
19582502 | 110 | local IFS=$'\n' |
b5b7ea75 DR |
111 | fi |
112 | ;; | |
113 | 'trigger') | |
19582502 YW |
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 | |
1f795124 | 140 | comps=$( __get_all_sysdevs; __get_all_device_units ) |
19582502 | 141 | local IFS=$'\n' |
19582502 | 142 | fi |
b5b7ea75 DR |
143 | ;; |
144 | 'settle') | |
19582502 YW |
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]}" | |
b5b7ea75 DR |
159 | ;; |
160 | 'control') | |
19582502 YW |
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]}" | |
b5b7ea75 DR |
175 | ;; |
176 | 'monitor') | |
19582502 YW |
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]}" | |
b5b7ea75 DR |
188 | ;; |
189 | 'test') | |
19582502 YW |
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 | ||
b5b7ea75 | 203 | if [[ $cur = -* ]]; then |
19582502 | 204 | comps="${OPTS[COMMON]} ${OPTS[TEST]}" |
b5b7ea75 DR |
205 | else |
206 | comps=$( __get_all_sysdevs ) | |
19582502 | 207 | local IFS=$'\n' |
b5b7ea75 DR |
208 | fi |
209 | ;; | |
210 | 'test-builtin') | |
19582502 YW |
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 | |
b5b7ea75 DR |
226 | ;; |
227 | *) | |
228 | comps=${VERBS[*]} | |
229 | ;; | |
230 | esac | |
d611dadc MB |
231 | |
232 | COMPREPLY=( $(compgen -W '$comps' -- "$cur") ) | |
233 | return 0 | |
234 | } | |
235 | ||
236 | complete -F _udevadm udevadm |