chiark / gitweb /
shell-completion: replace printf with echo
[elogind.git] / shell-completion / bash / udevadm
1 # udevadm(8) completion                                   -*- shell-script -*-
2 #
3 # This file is part of systemd.
4 #
5 # Copyright 2010 Ran Benita
6 #
7 # systemd is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU Lesser General Public License as published by
9 # the Free Software Foundation; either version 2.1 of the License, or
10 # (at your option) any later version.
11 #
12 # systemd is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public License
18 # along with systemd; If not, see <http://www.gnu.org/licenses/>.
19
20 __contains_word () {
21         local word=$1; shift
22         for w in $*; do [[ $w = $word ]] && return 0; done
23         return 1
24 }
25
26 __get_all_sysdevs() {
27         local -a devs=(/sys/bus/*/devices/*/ /sys/class/*/*/)
28         printf '%s\n' "${devs[@]%/}"
29 }
30
31 _udevadm() {
32         local i verb comps
33         local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
34         local OPTS='-h --help --version --debug'
35
36         local -A VERBS=(
37                 [INFO]='info'
38                 [TRIGGER]='trigger'
39                 [SETTLE]='settle'
40                 [CONTROL]='control'
41                 [MONITOR]='monitor'
42                 [HWDB]='hwdb'
43                 [TESTBUILTIN]='test-builtin'
44                 [TEST]='test'
45         )
46
47         for ((i=0; $i <= $COMP_CWORD; i++)); do
48                 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
49                  ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
50                         verb=${COMP_WORDS[i]}
51                         break
52                 fi
53         done
54
55         if [[ -z $verb  && $cur = -* ]]; then
56                 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
57                 return 0
58         fi
59
60         if [[ -z $verb ]]; then
61                 comps=${VERBS[*]}
62
63         elif __contains_word "$verb" ${VERBS[INFO]}; then
64                 if [[ $cur = -* ]]; then
65                         comps='--help --query= --path= --name= --root --attribute-walk --export-db --cleanup-db'
66                 else
67                         comps=$( __get_all_sysdevs )
68                 fi
69
70         elif __contains_word "$verb" ${VERBS[TRIGGER]}; then
71                 comps='--help --verbose --dry-run --type= --action= --subsystem-match=
72                        --subsystem-nomatch= --attr-match= --attr-nomatch= --property-match=
73                        --tag-match= --sysname-match= --parent-match='
74
75         elif __contains_word "$verb" ${VERBS[SETTLE]}; then
76                 comps='--help --timeout= --seq-start= --seq-end= --exit-if-exists= --quiet'
77
78         elif __contains_word "$verb" ${VERBS[CONTROL]}; then
79                 comps='--help --exit --log-priority= --stop-exec-queue --start-exec-queue
80                        --reload --property= --children-max= --timeout='
81
82         elif __contains_word "$verb" ${VERBS[MONITOR]}; then
83                 comps='--help --kernel --udev --property --subsystem-match= --tag-match='
84
85         elif __contains_word "$verb" ${VERBS[HWDB]}; then
86                 comps='--help --update --test='
87
88         elif __contains_word "$verb" ${VERBS[TEST]}; then
89                 if [[ $cur = -* ]]; then
90                         comps='--help --action='
91                 else
92                         comps=$( __get_all_sysdevs )
93                 fi
94
95         elif __contains_word "$verb" ${VERBS[TESTBUILTIN]}; then
96                       comps='blkid btrfs hwdb input_id kmod net_id path_id usb_id uaccess'
97         fi
98
99         COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
100         return 0
101 }
102
103 complete -F _udevadm udevadm