]> git.ipfire.org Git - thirdparty/systemd.git/blame - shell-completion/bash/udevadm
Merge pull request #14488 from yuwata/networkctl-show-logs
[thirdparty/systemd.git] / shell-completion / bash / udevadm
CommitLineData
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 () {
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() {
843cfcb1
ZJS
41 systemctl list-units -t device --full --no-legend --no-pager 2>/dev/null | \
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
YW
51 [INFO_STANDALONE]='-r --root -a --attribute-walk -x --export -e --export-db -c --cleanup-db
52 -w --wait-for-initialization'
843cfcb1
ZJS
53 [INFO_ARG]='-q --query -p --path -n --name -P --export-prefix -d --device-id-of-file'
54 [TRIGGER_STANDALONE]='-v --verbose -n --dry-run -w --settle --wait-daemon'
55 [TRIGGER_ARG]='-t --type -c --action -s --subsystem-match -S --subsystem-nomatch
56 -a --attr-match -A --attr-nomatch -p --property-match
57 -g --tag-match -y --sysname-match --name-match -b --parent-match'
58 [SETTLE]='-t --timeout -E --exit-if-exists'
59 [CONTROL_STANDALONE]='-e --exit -s --stop-exec-queue -S --start-exec-queue -R --reload --ping'
60 [CONTROL_ARG]='-l --log-priority -p --property -m --children-max -t --timeout'
61 [MONITOR_STANDALONE]='-k --kernel -u --udev -p --property'
62 [MONITOR_ARG]='-s --subsystem-match -t --tag-match'
63 [TEST]='-a --action -N --resolve-names'
64 )
65
66 local verbs=(info trigger settle control monitor test-builtin test)
67 local builtins=(blkid btrfs hwdb input_id keyboard kmod net_id net_setup_link path_id usb_id uaccess)
68
69 for ((i=0; i < COMP_CWORD; i++)); do
70 if __contains_word "${COMP_WORDS[i]}" "${verbs[@]}"; then
71 verb=${COMP_WORDS[i]}
72 break
d611dadc 73 fi
843cfcb1 74 done
d611dadc 75
843cfcb1
ZJS
76 if [[ -z $verb ]]; then
77 if [[ "$cur" = -* ]]; then
78 COMPREPLY=( $(compgen -W '${OPTS[COMMON]} ${OPTS[DEBUG]}' -- "$cur") )
79 else
80 COMPREPLY=( $(compgen -W '${verbs[*]}' -- "$cur") )
81 fi
d611dadc 82 return 0
843cfcb1
ZJS
83 fi
84
85 case $verb in
86 'info')
87 if __contains_word "$prev" ${OPTS[INFO_ARG]}; then
88 case $prev in
89 -q|--query)
90 comps='name symlink path property all'
91 ;;
92 -p|--path)
93 comps=$( __get_all_sysdevs )
94 local IFS=$'\n'
95 ;;
96 -n|--name)
97 comps=$( __get_all_devs )
98 ;;
99 *)
100 comps=''
101 ;;
102 esac
103 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
104 return 0
105 fi
106
107 if [[ $cur = -* ]]; then
108 comps="${OPTS[COMMON]} ${OPTS[INFO_STANDALONE]} ${OPTS[INFO_ARG]}"
109 else
110 comps=$( __get_all_sysdevs; __get_all_device_units )
111 local IFS=$'\n'
112 fi
113 ;;
114
115 'trigger')
116 if __contains_word "$prev" ${OPTS[TRIGGER_ARG]}; then
117 case $prev in
118 -t|--type)
119 comps='devices subsystems'
120 ;;
121 -c|--action)
7ce72782 122 comps=$( udevadm trigger --action help )
843cfcb1
ZJS
123 ;;
124 -y|--sysname-match|-b|--parent-match)
125 comps=$( __get_all_sysdevs )
126 local IFS=$'\n'
127 ;;
128 --name-match)
129 comps=$( __get_all_devs )
130 ;;
131 *)
132 comps=''
133 ;;
134 esac
135 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
136 return 0
137 fi
138
139 if [[ $cur = -* ]]; then
140 comps="${OPTS[COMMON]} ${OPTS[TRIGGER_STANDALONE]} ${OPTS[TRIGGER_ARG]}"
141 else
142 comps=$( __get_all_sysdevs; __get_all_device_units )
143 local IFS=$'\n'
144 fi
145 ;;
146
147 'settle')
148 if __contains_word "$prev" ${OPTS[SETTLE]}; then
149 case $prev in
150 -E|--exit-if-exists)
151 comps=$( compgen -A file -- "$cur" )
152 ;;
153 *)
154 comps=''
155 ;;
156 esac
157 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
158 return 0
159 fi
160
161 comps="${OPTS[COMMON]} ${OPTS[SETTLE]}"
162 ;;
163
164 'control')
165 if __contains_word "$prev" ${OPTS[CONTROL_ARG]}; then
166 case $prev in
167 -l|--log-priority)
168 comps='alert crit debug emerg err info notice warning'
169 ;;
170 *)
171 comps=''
172 ;;
173 esac
174 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
175 return 0
176 fi
177
178 comps="${OPTS[COMMON]} ${OPTS[CONTROL_STANDALONE]} ${OPTS[CONTROL_ARG]}"
179 ;;
180
181 'monitor')
182 if __contains_word "$prev" ${OPTS[MONITOR_ARG]}; then
183 case $prev in
184 *)
185 comps=''
186 ;;
187 esac
188 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
189 return 0
190 fi
191
192 comps="${OPTS[COMMON]} ${OPTS[MONITOR_STANDALONE]} ${OPTS[MONITOR_ARG]}"
193 ;;
194
195 'test')
196 if __contains_word "$prev" ${OPTS[TEST]}; then
197 case $prev in
198 -a|--action)
7ce72782 199 comps=$( udevadm test --action help )
843cfcb1
ZJS
200 ;;
201 -N|--resolve-names)
202 comps='early late never'
203 ;;
204 esac
205 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
206 return 0
207 fi
208
209 if [[ $cur = -* ]]; then
210 comps="${OPTS[COMMON]} ${OPTS[TEST]}"
211 else
212 comps=$( __get_all_sysdevs )
213 local IFS=$'\n'
214 fi
215 ;;
216
217 'test-builtin')
218 for ((i=0; i < COMP_CWORD; i++)); do
219 if __contains_word "${COMP_WORDS[i]}" "${builtins[@]}"; then
220 builtin=${COMP_WORDS[i]}
221 break
222 fi
223 done
224
225 if [[ -z $builtin ]]; then
226 comps="${builtins[@]}"
227 elif [[ $cur = -* ]]; then
228 comps="${OPTS[COMMON]}"
229 else
230 comps=$( __get_all_sysdevs )
231 local IFS=$'\n'
232 fi
233 ;;
234
235 *)
236 comps=${VERBS[*]}
237 ;;
238 esac
239
240 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
241 return 0
d611dadc
MB
242}
243
244complete -F _udevadm udevadm