Debian Codegraph @

bash-completion/rc-service

raw
1 # Copyright (c) 2017 The OpenRC Authors.
2 # See the Authors file at the top-level directory of this distribution and
3 # https://github.com/OpenRC/openrc/blob/HEAD/AUTHORS
4 #
5 # This file is part of OpenRC. It is subject to the license terms in
6 # the LICENSE file found in the top-level directory of this
7 # distribution and at https://github.com/OpenRC/openrc/blob/HEAD/LICENSE
8 # This file may not be copied, modified, propagated, or distributed
9 # except according to the terms contained in the LICENSE file.
10
11 #
12 # rc-service completion command
13 #
14 _rc_service()
15 {
16 local cur prev numwords opts
17 local words i x filename
18 local action actionpos
19 COMPREPLY=()
20 cur="${COMP_WORDS[COMP_CWORD]}"
21 prev="${COMP_WORDS[COMP_CWORD-1]}"
22 numwords=${#COMP_WORDS[*]}
23
24 if [[ ${prev} == '>' || ${prev} == '<' ]] ; then
25 COMPREPLY=($(compgen -f -- ${cur}))
26 return 0
27 fi
28
29 # find action
30 for x in ${COMP_LINE} ; do
31 if [[ ${x} =~ --(list|exists|resolve) ]] || [[ ${x} =~ -(l|e|r) ]]
32 then
33 action=${x}
34 break
35 fi
36 done
37 if [[ -n ${action} ]]; then
38 for ((i = 0; i < ${numwords}; i++ )); do
39 if [[ ${COMP_WORDS[${i}]} == "${action}" ]]; then
40 actionpos=${i}
41 break
42 fi
43 done
44
45 for ((i = 1; i < ${numwords}; i++ )); do
46 if [[ ! ${COMP_WORDS[$i]} == -* ]]; then
47 break
48 fi
49 done
50 fi
51
52 if [[ ${COMP_CWORD} -eq 3 ]]; then
53 return 1
54 fi
55
56 # check if an option was typed
57 if [[ ${cur} == -* ]]; then
58 if [[ ${cur} == --* ]]; then
59 opts="--list --exists --resolve"
60 COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
61 return 0
62 elif [[ ${cur} == -* ]]; then
63 opts="-l -e -r"
64 COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
65 return 0
66 fi
67
68 # NOTE: This slows things down!
69 # (Adapted from bash_completion by Ian Macdonald <[email protected]>)
70 # This removes any options from the list of completions that have
71 # already been specified on the command line.
72 COMPREPLY=($(echo "${COMP_WORDS[@]}" | \
73 (while read -d ' ' i; do
74 [[ -z ${i} ]] && continue
75 # flatten array with spaces on either side,
76 # otherwise we cannot grep on word boundaries of
77 # first and last word
78 COMPREPLY=" ${COMPREPLY[@]} "
79 # remove word from list of completions
80 COMPREPLY=(${COMPREPLY/ ${i%% *} / })
81 done
82 echo ${COMPREPLY[@]})))
83
84 return 0
85 else
86 # no option was typed
87 if [[ ${COMP_CWORD} -eq 1 ]]; then # if first word typed
88 # complete for init scripts
89 COMPREPLY=($(for i in $(rc-service --list) ; do \
90 [[ ${i} == "${cur}"* ]] && echo ${i} ; \
91 done))
92 return 0
93 elif [[ ${COMP_CWORD} -eq 2 ]] && [[ ${prev} != -* ]]; then # if second word typed and we didn't type in a function
94 rc-service --exists "$prev" || return
95 while read -r _ line; do
96 if [[ $line == +([[:alnum:]_]):* ]]; then
97 opts+="${line%%:*} "
98 fi
99 done < <(rc-service "$prev" describe 2>&1)
100 COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
101 return 0
102 fi
103 fi
104 if [[ ${action} == '--exists' ]] || [[ ${action} == '-e' ]] || \
105 [[ ${action} == '--resolve' ]] || [[ ${action} == '-r' ]]; then
106 COMPREPLY=($(for i in $(rc-service --list) ; do \
107 [[ ${i} == "${cur}"* ]] && echo ${i} ; \
108 done))
109 return 0
110 fi
111 return 0
112 } &&
113 complete -F _rc_service rc-service