chiark / gitweb /
bash-completion: use a better definition of __contains_word
[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 w word=$1; shift
22         for w in "$@"; do
23                 [[ $w = "$word" ]] && return
24         done
25 }
26
27 __get_all_sysdevs() {
28         local -a devs=(/sys/bus/*/devices/*/ /sys/class/*/*/)
29         printf '%s\n' "${devs[@]%/}"
30 }
31
32 _udevadm() {
33         local i verb comps
34         local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
35         local OPTS='-h --help --version --debug'
36
37         local -A VERBS=(
38                 [INFO]='info'
39                 [TRIGGER]='trigger'
40                 [SETTLE]='settle'
41                 [CONTROL]='control'
42                 [MONITOR]='monitor'
43                 [HWDB]='hwdb'
44                 [TESTBUILTIN]='test-builtin'
45                 [TEST]='test'
46         )
47
48         for ((i=0; $i <= $COMP_CWORD; i++)); do
49                 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
50                  ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
51                         verb=${COMP_WORDS[i]}
52                         break
53                 fi
54         done
55
56         if [[ -z $verb  && $cur = -* ]]; then
57                 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
58                 return 0
59         fi
60
61         if [[ -z $verb ]]; then
62                 comps=${VERBS[*]}
63
64         elif __contains_word "$verb" ${VERBS[INFO]}; then
65                 if [[ $cur = -* ]]; then
66                         comps='--help --query= --path= --name= --root --attribute-walk --export-db --cleanup-db'
67                 else
68                         comps=$( __get_all_sysdevs )
69                 fi
70
71         elif __contains_word "$verb" ${VERBS[TRIGGER]}; then
72                 comps='--help --verbose --dry-run --type= --action= --subsystem-match=
73                        --subsystem-nomatch= --attr-match= --attr-nomatch= --property-match=
74                        --tag-match= --sysname-match= --parent-match='
75
76         elif __contains_word "$verb" ${VERBS[SETTLE]}; then
77                 comps='--help --timeout= --seq-start= --seq-end= --exit-if-exists= --quiet'
78
79         elif __contains_word "$verb" ${VERBS[CONTROL]}; then
80                 comps='--help --exit --log-priority= --stop-exec-queue --start-exec-queue
81                        --reload --property= --children-max= --timeout='
82
83         elif __contains_word "$verb" ${VERBS[MONITOR]}; then
84                 comps='--help --kernel --udev --property --subsystem-match= --tag-match='
85
86         elif __contains_word "$verb" ${VERBS[HWDB]}; then
87                 comps='--help --update --test='
88
89         elif __contains_word "$verb" ${VERBS[TEST]}; then
90                 if [[ $cur = -* ]]; then
91                         comps='--help --action='
92                 else
93                         comps=$( __get_all_sysdevs )
94                 fi
95
96         elif __contains_word "$verb" ${VERBS[TESTBUILTIN]}; then
97                       comps='blkid btrfs hwdb input_id keyboard kmod net_id path_id usb_id uaccess'
98         fi
99
100         COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
101         return 0
102 }
103
104 complete -F _udevadm udevadm