| 1 |
# shellcheck shell=bash |
| 2 |
# udevadm(8) completion -*- shell-script -*- |
| 3 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
# |
| 5 |
# This file is part of systemd. |
| 6 |
# |
| 7 |
# Copyright © 2010 Ran Benita |
| 8 |
# |
| 9 |
# systemd is free software; you can redistribute it and/or modify it |
| 10 |
# under the terms of the GNU Lesser General Public License as published by |
| 11 |
# the Free Software Foundation; either version 2.1 of the License, or |
| 12 |
# (at your option) any later version. |
| 13 |
# |
| 14 |
# systemd is distributed in the hope that it will be useful, but |
| 15 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 |
# General Public License for more details. |
| 18 |
# |
| 19 |
# You should have received a copy of the GNU Lesser General Public License |
| 20 |
# along with systemd; If not, see <https://www.gnu.org/licenses/>. |
| 21 |
|
| 22 |
__contains_word () { |
| 23 |
local w word=$1; shift |
| 24 |
for w in "$@"; do |
| 25 |
[[ $w = "$word" ]] && return |
| 26 |
done |
| 27 |
} |
| 28 |
|
| 29 |
__get_all_sysdevs() { |
| 30 |
local -a devs=(/sys/bus/*/devices/*/ /sys/class/*/*/) |
| 31 |
printf '%s\n' "${devs[@]%/}" |
| 32 |
} |
| 33 |
|
| 34 |
__get_all_device_nodes() { |
| 35 |
find /dev -xtype b -o -xtype c |
| 36 |
} |
| 37 |
|
| 38 |
__get_all_device_units() { |
| 39 |
systemctl list-units -t device --full --no-legend --no-pager --plain 2>/dev/null | |
| 40 |
{ while read -r a b; do echo "$a"; done; } |
| 41 |
} |
| 42 |
|
| 43 |
__get_all_devices() { |
| 44 |
__get_all_sysdevs |
| 45 |
__get_all_device_nodes |
| 46 |
__get_all_device_units |
| 47 |
} |
| 48 |
|
| 49 |
__get_root() { |
| 50 |
local i |
| 51 |
|
| 52 |
for ((i=0; i < COMP_CWORD; i++)); do |
| 53 |
if [[ "${COMP_WORDS[i]}" = --root=* ]]; then |
| 54 |
echo "${COMP_WORDS[i]#--root=}" |
| 55 |
break |
| 56 |
fi |
| 57 |
if (( i > 0 )) && [[ "${COMP_WORDS[i-1]}" == "--root" ]]; then |
| 58 |
echo "${COMP_WORDS[i]}" |
| 59 |
break |
| 60 |
fi |
| 61 |
done |
| 62 |
} |
| 63 |
|
| 64 |
__get_udev_rules_files() { |
| 65 |
local root=$( __get_root ) |
| 66 |
|
| 67 |
ls "$root"/usr/lib/udev/rules.d/*.rules \ |
| 68 |
"$root"/usr/local/lib/udev/rules.d/*.rules \ |
| 69 |
"$root"/run/udev/rules.d/*.rules \ |
| 70 |
"$root"/etc/udev/rules.d/*.rules 2>/dev/null |
| 71 |
} |
| 72 |
|
| 73 |
__get_udev_rules_names() { |
| 74 |
local -a rules=( $( __get_udev_rules_files ) ) |
| 75 |
printf '%s\n' "${rules[@]##*/}" |
| 76 |
} |
| 77 |
|
| 78 |
_udevadm() { |
| 79 |
local i verb comps builtin |
| 80 |
local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]} |
| 81 |
local -A OPTS=( |
| 82 |
[COMMON]='-h --help -V --version' |
| 83 |
[DEBUG]='-d --debug' |
| 84 |
[INFO_STANDALONE]='-r --root -a --attribute-walk -t --tree -x --export -e --export-db -c --cleanup-db |
| 85 |
-w --wait-for-initialization --value --no-pager --initialized-match --initialized-nomatch' |
| 86 |
[INFO_ARG]='-q --query -p --path -n --name -P --export-prefix -d --device-id-of-file --property |
| 87 |
--json --subsystem-match --subsystem-nomatch --attr-match --attr-nomatch --property-match |
| 88 |
--tag-match --sysname-match --name-match --parent-match' |
| 89 |
[TRIGGER_STANDALONE]='-v --verbose -n --dry-run -q --quiet -w --settle --wait-daemon --uuid |
| 90 |
--initialized-match --initialized-nomatch --include-parents' |
| 91 |
[TRIGGER_ARG]='-t --type -c --action -s --subsystem-match -S --subsystem-nomatch |
| 92 |
-a --attr-match -A --attr-nomatch -p --property-match |
| 93 |
-g --tag-match -y --sysname-match --name-match -b --parent-match |
| 94 |
--prioritized-subsystem' |
| 95 |
[SETTLE]='-t --timeout -E --exit-if-exists' |
| 96 |
[CONTROL_STANDALONE]='-e --exit -s --stop-exec-queue -S --start-exec-queue -R --reload --ping --revert |
| 97 |
--load-credentials' |
| 98 |
[CONTROL_ARG]='-l --log-priority -p --property -m --children-max -t --timeout --trace' |
| 99 |
[MONITOR_STANDALONE]='-k --kernel -u --udev -p --property' |
| 100 |
[MONITOR_ARG]='-s --subsystem-match -t --tag-match' |
| 101 |
[TEST_STANDALONE]='-v --verbose' |
| 102 |
[TEST_ARG]='-a --action -N --resolve-names -D --extra-rules-dir --json' |
| 103 |
[TEST_BUILTIN]='-a --action' |
| 104 |
[VERIFY_STANDALONE]='--no-summary --no-style' |
| 105 |
[VERIFY_ARG]='-N --resolve-names --root' |
| 106 |
[CAT_STANDALONE]='--tldr --config' |
| 107 |
[CAT_ARG]='--root' |
| 108 |
[WAIT]='-t --timeout --initialized=no --removed --settle' |
| 109 |
[LOCK]='-t --timeout -d --device -b --backing -p --print' |
| 110 |
) |
| 111 |
|
| 112 |
local verbs=(info trigger settle control monitor test-builtin test verify cat wait lock) |
| 113 |
local builtins=(blkid btrfs dissect_image factory_reset hwdb input_id keyboard kmod net_driver net_id net_setup_link path_id uaccess usb_id) |
| 114 |
|
| 115 |
for ((i=0; i < COMP_CWORD; i++)); do |
| 116 |
if __contains_word "${COMP_WORDS[i]}" "${verbs[@]}"; then |
| 117 |
verb=${COMP_WORDS[i]} |
| 118 |
break |
| 119 |
fi |
| 120 |
done |
| 121 |
|
| 122 |
if [[ -z ${verb-} ]]; then |
| 123 |
if [[ "$cur" = -* ]]; then |
| 124 |
COMPREPLY=( $(compgen -W '${OPTS[COMMON]} ${OPTS[DEBUG]}' -- "$cur") ) |
| 125 |
else |
| 126 |
COMPREPLY=( $(compgen -W '${verbs[*]}' -- "$cur") ) |
| 127 |
fi |
| 128 |
return 0 |
| 129 |
fi |
| 130 |
|
| 131 |
case $verb in |
| 132 |
'info') |
| 133 |
if __contains_word "$prev" ${OPTS[INFO_ARG]}; then |
| 134 |
case $prev in |
| 135 |
-q|--query) |
| 136 |
comps='name symlink path property all' |
| 137 |
;; |
| 138 |
-p|--path) |
| 139 |
comps=$( __get_all_sysdevs ) |
| 140 |
local IFS=$'\n' |
| 141 |
;; |
| 142 |
-n|--name) |
| 143 |
comps=$( __get_all_device_nodes ) |
| 144 |
;; |
| 145 |
--json) |
| 146 |
comps=$( udevadm info --json=help ) |
| 147 |
;; |
| 148 |
--parent-match) |
| 149 |
comps=$( __get_all_sysdevs ) |
| 150 |
local IFS=$'\n' |
| 151 |
;; |
| 152 |
--name-match) |
| 153 |
comps=$( __get_all_device_nodes ) |
| 154 |
;; |
| 155 |
*) |
| 156 |
comps='' |
| 157 |
;; |
| 158 |
esac |
| 159 |
COMPREPLY=( $(compgen -W '$comps' -- "$cur") ) |
| 160 |
return 0 |
| 161 |
fi |
| 162 |
|
| 163 |
if [[ $cur = -* ]]; then |
| 164 |
comps="${OPTS[COMMON]} ${OPTS[INFO_STANDALONE]} ${OPTS[INFO_ARG]}" |
| 165 |
else |
| 166 |
comps=$( __get_all_devices ) |
| 167 |
local IFS=$'\n' |
| 168 |
fi |
| 169 |
;; |
| 170 |
|
| 171 |
'trigger') |
| 172 |
if __contains_word "$prev" ${OPTS[TRIGGER_ARG]}; then |
| 173 |
case $prev in |
| 174 |
-t|--type) |
| 175 |
comps='all devices subsystems' |
| 176 |
;; |
| 177 |
-c|--action) |
| 178 |
comps=$( udevadm trigger --action help ) |
| 179 |
;; |
| 180 |
-y|--sysname-match|-b|--parent-match) |
| 181 |
comps=$( __get_all_sysdevs ) |
| 182 |
local IFS=$'\n' |
| 183 |
;; |
| 184 |
--name-match) |
| 185 |
comps=$( __get_all_device_nodes ) |
| 186 |
;; |
| 187 |
*) |
| 188 |
comps='' |
| 189 |
;; |
| 190 |
esac |
| 191 |
COMPREPLY=( $(compgen -W '$comps' -- "$cur") ) |
| 192 |
return 0 |
| 193 |
fi |
| 194 |
|
| 195 |
if [[ $cur = -* ]]; then |
| 196 |
comps="${OPTS[COMMON]} ${OPTS[TRIGGER_STANDALONE]} ${OPTS[TRIGGER_ARG]}" |
| 197 |
else |
| 198 |
comps=$( __get_all_devices ) |
| 199 |
local IFS=$'\n' |
| 200 |
fi |
| 201 |
;; |
| 202 |
|
| 203 |
'settle') |
| 204 |
if __contains_word "$prev" ${OPTS[SETTLE]}; then |
| 205 |
case $prev in |
| 206 |
-E|--exit-if-exists) |
| 207 |
comps=$( compgen -A file -- "$cur" ) |
| 208 |
;; |
| 209 |
*) |
| 210 |
comps='' |
| 211 |
;; |
| 212 |
esac |
| 213 |
COMPREPLY=( $(compgen -W '$comps' -- "$cur") ) |
| 214 |
return 0 |
| 215 |
fi |
| 216 |
|
| 217 |
comps="${OPTS[COMMON]} ${OPTS[SETTLE]}" |
| 218 |
;; |
| 219 |
|
| 220 |
'control') |
| 221 |
if __contains_word "$prev" ${OPTS[CONTROL_ARG]}; then |
| 222 |
case $prev in |
| 223 |
-l|--log-priority) |
| 224 |
comps='alert crit debug emerg err info notice warning' |
| 225 |
;; |
| 226 |
--trace) |
| 227 |
comps='yes no' |
| 228 |
;; |
| 229 |
*) |
| 230 |
comps='' |
| 231 |
;; |
| 232 |
esac |
| 233 |
COMPREPLY=( $(compgen -W '$comps' -- "$cur") ) |
| 234 |
return 0 |
| 235 |
fi |
| 236 |
|
| 237 |
comps="${OPTS[COMMON]} ${OPTS[CONTROL_STANDALONE]} ${OPTS[CONTROL_ARG]}" |
| 238 |
;; |
| 239 |
|
| 240 |
'monitor') |
| 241 |
if __contains_word "$prev" ${OPTS[MONITOR_ARG]}; then |
| 242 |
case $prev in |
| 243 |
*) |
| 244 |
comps='' |
| 245 |
;; |
| 246 |
esac |
| 247 |
COMPREPLY=( $(compgen -W '$comps' -- "$cur") ) |
| 248 |
return 0 |
| 249 |
fi |
| 250 |
|
| 251 |
comps="${OPTS[COMMON]} ${OPTS[MONITOR_STANDALONE]} ${OPTS[MONITOR_ARG]}" |
| 252 |
;; |
| 253 |
|
| 254 |
'test') |
| 255 |
if __contains_word "$prev" ${OPTS[TEST_ARG]}; then |
| 256 |
case $prev in |
| 257 |
-a|--action) |
| 258 |
comps=$( udevadm test --action help ) |
| 259 |
;; |
| 260 |
-N|--resolve-names) |
| 261 |
comps=$( udevadm test --resolve-names help ) |
| 262 |
;; |
| 263 |
-D|--extra-rules-dir) |
| 264 |
comps='' |
| 265 |
compopt -o dirnames |
| 266 |
;; |
| 267 |
--json) |
| 268 |
comps=$( udevadm test --json help ) |
| 269 |
;; |
| 270 |
esac |
| 271 |
elif [[ $cur = -* ]]; then |
| 272 |
comps="${OPTS[COMMON]} ${OPTS[TEST_ARG]} ${OPTS[TEST_STANDALONE]}" |
| 273 |
else |
| 274 |
comps=$( __get_all_devices ) |
| 275 |
local IFS=$'\n' |
| 276 |
fi |
| 277 |
;; |
| 278 |
|
| 279 |
'test-builtin') |
| 280 |
if __contains_word "$prev" ${OPTS[TEST_BUILTIN]}; then |
| 281 |
case $prev in |
| 282 |
-a|--action) |
| 283 |
comps=$( udevadm test-builtin --action help ) |
| 284 |
;; |
| 285 |
esac |
| 286 |
COMPREPLY=( $(compgen -W '$comps' -- "$cur") ) |
| 287 |
return 0 |
| 288 |
fi |
| 289 |
|
| 290 |
for ((i=0; i < COMP_CWORD; i++)); do |
| 291 |
if __contains_word "${COMP_WORDS[i]}" "${builtins[@]}"; then |
| 292 |
builtin=${COMP_WORDS[i]} |
| 293 |
break |
| 294 |
fi |
| 295 |
done |
| 296 |
|
| 297 |
if [[ -z $builtin ]]; then |
| 298 |
comps="${builtins[@]}" |
| 299 |
elif [[ $cur = -* ]]; then |
| 300 |
comps="${OPTS[COMMON]} ${OPTS[TEST_BUILTIN]}" |
| 301 |
else |
| 302 |
comps=$( __get_all_devices ) |
| 303 |
local IFS=$'\n' |
| 304 |
fi |
| 305 |
;; |
| 306 |
|
| 307 |
'verify') |
| 308 |
if __contains_word "$prev" ${OPTS[VERIFY_ARG]}; then |
| 309 |
case $prev in |
| 310 |
-N|--resolve-names) |
| 311 |
comps=$( udevadm test --resolve-names help ) |
| 312 |
;; |
| 313 |
--root) |
| 314 |
comps='' |
| 315 |
compopt -o dirnames |
| 316 |
;; |
| 317 |
*) |
| 318 |
comps='' |
| 319 |
;; |
| 320 |
esac |
| 321 |
elif [[ $cur = -* ]]; then |
| 322 |
comps="${OPTS[COMMON]} ${OPTS[VERIFY_ARG]} ${OPTS[VERIFY_STANDALONE]}" |
| 323 |
elif [[ $cur = */* ]]; then |
| 324 |
comps=$( __get_udev_rules_files ) |
| 325 |
compopt -o dirnames |
| 326 |
else |
| 327 |
comps=$( __get_udev_rules_names ) |
| 328 |
compopt -o default |
| 329 |
fi |
| 330 |
;; |
| 331 |
|
| 332 |
'cat') |
| 333 |
if __contains_word "$prev" ${OPTS[CAT_ARG]}; then |
| 334 |
case $prev in |
| 335 |
--root) |
| 336 |
comps='' |
| 337 |
compopt -o dirnames |
| 338 |
;; |
| 339 |
*) |
| 340 |
comps='' |
| 341 |
;; |
| 342 |
esac |
| 343 |
elif [[ $cur = -* ]]; then |
| 344 |
comps="${OPTS[COMMON]} ${OPTS[CAT_ARG]} ${OPTS[CAT_STANDALONE]}" |
| 345 |
elif __contains_word "--config" ${COMP_WORDS[*]}; then |
| 346 |
comps="${OPTS[COMMON]} ${OPTS[CAT_ARG]} ${OPTS[CAT_STANDALONE]}" |
| 347 |
elif [[ $cur = */* ]]; then |
| 348 |
comps=$( __get_udev_rules_files ) |
| 349 |
compopt -o dirnames |
| 350 |
else |
| 351 |
comps=$( __get_udev_rules_names ) |
| 352 |
compopt -o default |
| 353 |
fi |
| 354 |
;; |
| 355 |
|
| 356 |
'wait') |
| 357 |
if __contains_word "$prev" ${OPTS[WAIT]}; then |
| 358 |
case $prev in |
| 359 |
*) |
| 360 |
comps='' |
| 361 |
;; |
| 362 |
esac |
| 363 |
COMPREPLY=( $(compgen -W '$comps' -- "$cur") ) |
| 364 |
return 0 |
| 365 |
fi |
| 366 |
|
| 367 |
if [[ $cur = -* ]]; then |
| 368 |
comps="${OPTS[COMMON]} ${OPTS[WAIT]}" |
| 369 |
else |
| 370 |
comps=$( __get_all_devices ) |
| 371 |
local IFS=$'\n' |
| 372 |
fi |
| 373 |
;; |
| 374 |
|
| 375 |
'lock') |
| 376 |
if __contains_word "$prev" ${OPTS[LOCK]}; then |
| 377 |
case $prev in |
| 378 |
*) |
| 379 |
comps='' |
| 380 |
;; |
| 381 |
esac |
| 382 |
COMPREPLY=( $(compgen -W '$comps' -- "$cur") ) |
| 383 |
return 0 |
| 384 |
fi |
| 385 |
|
| 386 |
if [[ $cur = -* ]]; then |
| 387 |
comps="${OPTS[COMMON]} ${OPTS[LOCK]}" |
| 388 |
else |
| 389 |
comps='' |
| 390 |
fi |
| 391 |
;; |
| 392 |
|
| 393 |
*) |
| 394 |
comps=${VERBS[*]} |
| 395 |
;; |
| 396 |
esac |
| 397 |
|
| 398 |
COMPREPLY=( $(compgen -W '$comps' -- "$cur") ) |
| 399 |
return 0 |
| 400 |
} |
| 401 |
|
| 402 |
complete -F _udevadm udevadm |