1 |
commit: d220fc272337b216bff6ac781a7b6be4e6f3caee |
2 |
Author: William Hubbs <w.d.hubbs <AT> gmail <DOT> com> |
3 |
AuthorDate: Wed Dec 6 19:21:25 2017 +0000 |
4 |
Commit: William Hubbs <williamh <AT> gentoo <DOT> org> |
5 |
CommitDate: Wed Dec 6 19:25:26 2017 +0000 |
6 |
URL: https://gitweb.gentoo.org/proj/openrc.git/commit/?id=d220fc27 |
7 |
|
8 |
add bash completion support |
9 |
|
10 |
This fixes #188. |
11 |
|
12 |
Makefile | 3 +- |
13 |
bash-completion/Makefile | 11 ++++ |
14 |
bash-completion/openrc | 24 ++++++++ |
15 |
bash-completion/openrc-service-script | 29 +++++++++ |
16 |
bash-completion/rc-service | 113 ++++++++++++++++++++++++++++++++++ |
17 |
bash-completion/rc-status | 31 ++++++++++ |
18 |
bash-completion/rc-update | 42 +++++++++++++ |
19 |
mk/sys.mk | 2 + |
20 |
8 files changed, 254 insertions(+), 1 deletion(-) |
21 |
|
22 |
diff --git a/Makefile b/Makefile |
23 |
index 05cfb796..663e103e 100644 |
24 |
--- a/Makefile |
25 |
+++ b/Makefile |
26 |
@@ -13,7 +13,8 @@ MK= ${TOP}/mk |
27 |
|
28 |
include ${TOP}/Makefile.inc |
29 |
|
30 |
-SUBDIR= conf.d etc init.d local.d man scripts sh src support sysctl.d |
31 |
+SUBDIR= bash-completion conf.d etc init.d local.d man scripts sh src \ |
32 |
+ support sysctl.d |
33 |
|
34 |
# Build pkgconfig or not |
35 |
MKPKGCONFIG?= yes |
36 |
|
37 |
diff --git a/bash-completion/Makefile b/bash-completion/Makefile |
38 |
new file mode 100644 |
39 |
index 00000000..3d9d886a |
40 |
--- /dev/null |
41 |
+++ b/bash-completion/Makefile |
42 |
@@ -0,0 +1,11 @@ |
43 |
+DIR= ${BASHCOMPDIR} |
44 |
+CONF= openrc \ |
45 |
+ openrc-service-script \ |
46 |
+ rc-service \ |
47 |
+ rc-status \ |
48 |
+ rc-update \ |
49 |
+ |
50 |
+MK= ../mk |
51 |
+include ${MK}/os.mk |
52 |
+ |
53 |
+include ${MK}/scripts.mk |
54 |
|
55 |
diff --git a/bash-completion/openrc b/bash-completion/openrc |
56 |
new file mode 100644 |
57 |
index 00000000..e2718e0b |
58 |
--- /dev/null |
59 |
+++ b/bash-completion/openrc |
60 |
@@ -0,0 +1,24 @@ |
61 |
+# Copyright (c) 2017 The OpenRC Authors. |
62 |
+# See the Authors file at the top-level directory of this distribution and |
63 |
+# https://github.com/OpenRC/openrc/blob/master/AUTHORS |
64 |
+# |
65 |
+# This file is part of OpenRC. It is subject to the license terms in |
66 |
+# the LICENSE file found in the top-level directory of this |
67 |
+# distribution and at https://github.com/OpenRC/openrc/blob/master/LICENSE |
68 |
+# This file may not be copied, modified, propagated, or distributed |
69 |
+# except according to the terms contained in the LICENSE file. |
70 |
+ |
71 |
+# |
72 |
+# openrc completion command |
73 |
+# |
74 |
+_openrc() |
75 |
+{ |
76 |
+ local cur |
77 |
+ COMPREPLY=() |
78 |
+ cur="${COMP_WORDS[COMP_CWORD]}" |
79 |
+ if [[ ${#COMP_WORDS[*]} -le 2 ]]; then |
80 |
+ COMPREPLY=($(compgen -W "$(rc-status --list)" -- $cur)) |
81 |
+ fi |
82 |
+ return 0 |
83 |
+} && |
84 |
+complete -F _openrc openrc |
85 |
|
86 |
diff --git a/bash-completion/openrc-service-script b/bash-completion/openrc-service-script |
87 |
new file mode 100644 |
88 |
index 00000000..455ad05f |
89 |
--- /dev/null |
90 |
+++ b/bash-completion/openrc-service-script |
91 |
@@ -0,0 +1,29 @@ |
92 |
+# Copyright (c) 2017 The OpenRC Authors. |
93 |
+# See the Authors file at the top-level directory of this distribution and |
94 |
+# https://github.com/OpenRC/openrc/blob/master/AUTHORS |
95 |
+# |
96 |
+# This file is part of OpenRC. It is subject to the license terms in |
97 |
+# the LICENSE file found in the top-level directory of this |
98 |
+# distribution and at https://github.com/OpenRC/openrc/blob/master/LICENSE |
99 |
+# This file may not be copied, modified, propagated, or distributed |
100 |
+# except according to the terms contained in the LICENSE file. |
101 |
+ |
102 |
+_openrc_service_script() |
103 |
+{ |
104 |
+ local script="${COMP_WORDS[0]}" |
105 |
+ local cur="${COMP_WORDS[$COMP_CWORD]}" |
106 |
+ |
107 |
+ if [[ ( -f "${script}" || -h "${script}" ) && -r "${script}" ]] \ |
108 |
+ && [[ "$(head -n 1 "${script}")" =~ \#\!.*/openrc-run ]] |
109 |
+ then |
110 |
+ [[ $COMP_CWORD -gt 1 ]] && return 1 |
111 |
+ COMPREPLY=($(opts="start stop status restart pause zap ineed needsme iuse usesme broken"; \ |
112 |
+ eval "$(grep '^opts=' "${script}")"; echo "${opts}")) |
113 |
+ [[ -n "$COMPREPLY" ]] || COMPREPLY=(start stop restart zap) |
114 |
+ COMPREPLY=($(compgen -W "${COMPREPLY[*]}" -- "${cur}")) |
115 |
+ else |
116 |
+ COMPREPLY=($(compgen -o default -- "${cur}")) |
117 |
+ fi |
118 |
+ return 0 |
119 |
+} |
120 |
+complete -F _openrc_service_script */etc/init.d/* |
121 |
|
122 |
diff --git a/bash-completion/rc-service b/bash-completion/rc-service |
123 |
new file mode 100644 |
124 |
index 00000000..b4e7709f |
125 |
--- /dev/null |
126 |
+++ b/bash-completion/rc-service |
127 |
@@ -0,0 +1,113 @@ |
128 |
+# Copyright (c) 2017 The OpenRC Authors. |
129 |
+# See the Authors file at the top-level directory of this distribution and |
130 |
+# https://github.com/OpenRC/openrc/blob/master/AUTHORS |
131 |
+# |
132 |
+# This file is part of OpenRC. It is subject to the license terms in |
133 |
+# the LICENSE file found in the top-level directory of this |
134 |
+# distribution and at https://github.com/OpenRC/openrc/blob/master/LICENSE |
135 |
+# This file may not be copied, modified, propagated, or distributed |
136 |
+# except according to the terms contained in the LICENSE file. |
137 |
+ |
138 |
+# |
139 |
+# rc-service completion command |
140 |
+# |
141 |
+_rc_service() |
142 |
+{ |
143 |
+ local cur prev numwords opts |
144 |
+ local words i x filename |
145 |
+ local action actionpos |
146 |
+ COMPREPLY=() |
147 |
+ cur="${COMP_WORDS[COMP_CWORD]}" |
148 |
+ prev="${COMP_WORDS[COMP_CWORD-1]}" |
149 |
+ numwords=${#COMP_WORDS[*]} |
150 |
+ |
151 |
+ if [[ ${prev} == '>' || ${prev} == '<' ]] ; then |
152 |
+ COMPREPLY=($(compgen -f -- ${cur})) |
153 |
+ return 0 |
154 |
+ fi |
155 |
+ |
156 |
+ # find action |
157 |
+ for x in ${COMP_LINE} ; do |
158 |
+ if [[ ${x} =~ --(list|exists|resolve) ]] || [[ ${x} =~ -(l|e|r) ]] |
159 |
+ then |
160 |
+ action=${x} |
161 |
+ break |
162 |
+ fi |
163 |
+ done |
164 |
+ if [[ -n ${action} ]]; then |
165 |
+ for ((i = 0; i < ${numwords}; i++ )); do |
166 |
+ if [[ ${COMP_WORDS[${i}]} == "${action}" ]]; then |
167 |
+ actionpos=${i} |
168 |
+ break |
169 |
+ fi |
170 |
+ done |
171 |
+ |
172 |
+ for ((i = 1; i < ${numwords}; i++ )); do |
173 |
+ if [[ ! ${COMP_WORDS[$i]} == -* ]]; then |
174 |
+ break |
175 |
+ fi |
176 |
+ done |
177 |
+ fi |
178 |
+ |
179 |
+ if [[ ${COMP_CWORD} -eq 3 ]]; then |
180 |
+ return 1 |
181 |
+ fi |
182 |
+ |
183 |
+ # check if an option was typed |
184 |
+ if [[ ${cur} == -* ]]; then |
185 |
+ if [[ ${cur} == --* ]]; then |
186 |
+ opts="--list --exists --resolve" |
187 |
+ COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) |
188 |
+ return 0 |
189 |
+ elif [[ ${cur} == -* ]]; then |
190 |
+ opts="-l -e -r" |
191 |
+ COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) |
192 |
+ return 0 |
193 |
+ fi |
194 |
+ |
195 |
+ # NOTE: This slows things down! |
196 |
+ # (Adapted from bash_completion by Ian Macdonald <ian@×××××××.org>) |
197 |
+ # This removes any options from the list of completions that have |
198 |
+ # already been specified on the command line. |
199 |
+ COMPREPLY=($(echo "${COMP_WORDS[@]}" | \ |
200 |
+ (while read -d ' ' i; do |
201 |
+ [[ -z ${i} ]] && continue |
202 |
+ # flatten array with spaces on either side, |
203 |
+ # otherwise we cannot grep on word boundaries of |
204 |
+ # first and last word |
205 |
+ COMPREPLY=" ${COMPREPLY[@]} " |
206 |
+ # remove word from list of completions |
207 |
+ COMPREPLY=(${COMPREPLY/ ${i%% *} / }) |
208 |
+ done |
209 |
+ echo ${COMPREPLY[@]}))) |
210 |
+ |
211 |
+ return 0 |
212 |
+ else |
213 |
+ # no option was typed |
214 |
+ if [[ ${COMP_CWORD} -eq 1 ]]; then # if first word typed |
215 |
+ words="$(rc-service --list | grep ^${cur})" # complete for init scripts |
216 |
+ COMPREPLY=($(for i in ${words} ; do \ |
217 |
+ [[ ${i} == ${cur}* ]] && echo ${i} ; \ |
218 |
+ done)) |
219 |
+ return 0 |
220 |
+ elif [[ ${COMP_CWORD} -eq 2 ]] && [[ ${prev} != -* ]]; then # if second word typed and we didn't type in a function |
221 |
+ filename=$(rc-service --resolve ${prev}) |
222 |
+ opts=$(cat ${filename} | grep "^\w*()" | sed "s/().*$//") # Greps the functions included in the init script |
223 |
+ if [[ "x${opts}" == "x" ]] ; then # if no options found loosen the grep algorhythm |
224 |
+ opts=$(cat ${filename} | grep "\w*()" | sed "s/().*$//") |
225 |
+ fi |
226 |
+ COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) |
227 |
+ return 0 |
228 |
+ fi |
229 |
+ fi |
230 |
+ if [[ ${action} == '--exists' ]] || [[ ${action} == '-e' ]] || \ |
231 |
+ [[ ${action} == '--resolve' ]] || [[ ${action} == '-r' ]]; then |
232 |
+ words="$(rc-service --list | grep ^${cur})" |
233 |
+ COMPREPLY=($(for i in ${words} ; do \ |
234 |
+ [[ ${i} == ${cur}* ]] && echo ${i} ; \ |
235 |
+ done)) |
236 |
+ return 0 |
237 |
+ fi |
238 |
+ return 0 |
239 |
+} && |
240 |
+complete -F _rc_service rc-service |
241 |
|
242 |
diff --git a/bash-completion/rc-status b/bash-completion/rc-status |
243 |
new file mode 100644 |
244 |
index 00000000..d75ed148 |
245 |
--- /dev/null |
246 |
+++ b/bash-completion/rc-status |
247 |
@@ -0,0 +1,31 @@ |
248 |
+# Copyright (c) 2017 The OpenRC Authors. |
249 |
+# See the Authors file at the top-level directory of this distribution and |
250 |
+# https://github.com/OpenRC/openrc/blob/master/AUTHORS |
251 |
+# |
252 |
+# This file is part of OpenRC. It is subject to the license terms in |
253 |
+# the LICENSE file found in the top-level directory of this |
254 |
+# distribution and at https://github.com/OpenRC/openrc/blob/master/LICENSE |
255 |
+# This file may not be copied, modified, propagated, or distributed |
256 |
+# except according to the terms contained in the LICENSE file. |
257 |
+ |
258 |
+# |
259 |
+# rc-status completion command |
260 |
+# |
261 |
+_rcstatus() |
262 |
+{ |
263 |
+ local cur |
264 |
+ cur="${COMP_WORDS[COMP_CWORD]}" |
265 |
+ if [[ $COMP_CWORD -eq 1 ]]; then |
266 |
+ if [[ "${cur}" == --* ]]; then |
267 |
+ COMPREPLY=($(compgen -W '--all --list --unused' -- ${cur})) |
268 |
+ elif [[ "${cur}" == -* ]]; then |
269 |
+ COMPREPLY=($(compgen -W '-a -l -u' -- ${cur})) |
270 |
+ else |
271 |
+ COMPREPLY=($(compgen -W "$(rc-status --list)" -- ${cur})) |
272 |
+ fi |
273 |
+ else |
274 |
+ unset COMPREPLY |
275 |
+ fi |
276 |
+ return 0 |
277 |
+} && |
278 |
+complete -F _rcstatus rc-status |
279 |
|
280 |
diff --git a/bash-completion/rc-update b/bash-completion/rc-update |
281 |
new file mode 100644 |
282 |
index 00000000..7fd310df |
283 |
--- /dev/null |
284 |
+++ b/bash-completion/rc-update |
285 |
@@ -0,0 +1,42 @@ |
286 |
+# Copyright (c) 2017 The OpenRC Authors. |
287 |
+# See the Authors file at the top-level directory of this distribution and |
288 |
+# https://github.com/OpenRC/openrc/blob/master/AUTHORS |
289 |
+# |
290 |
+# This file is part of OpenRC. It is subject to the license terms in |
291 |
+# the LICENSE file found in the top-level directory of this |
292 |
+# distribution and at https://github.com/OpenRC/openrc/blob/master/LICENSE |
293 |
+# This file may not be copied, modified, propagated, or distributed |
294 |
+# except according to the terms contained in the LICENSE file. |
295 |
+ |
296 |
+# |
297 |
+# rc-update completion command |
298 |
+# |
299 |
+_rc_update() |
300 |
+{ |
301 |
+ local cur show |
302 |
+ COMPREPLY=() |
303 |
+ cur="${COMP_WORDS[COMP_CWORD]}" |
304 |
+ if [[ $COMP_CWORD -eq 1 ]]; then |
305 |
+ if [[ "${cur}" == -* ]]; then |
306 |
+ COMPREPLY=($(compgen -W '-a -d -s' -- ${cur})) |
307 |
+ else |
308 |
+ COMPREPLY=($(compgen -W 'add del show' ${cur})) |
309 |
+ fi |
310 |
+ else |
311 |
+ if [[ "${COMP_WORDS[1]}" == "show" ]] || [[ "${COMP_WORDS[1]}" == "-s" ]]; then |
312 |
+ show="TRUE" |
313 |
+ fi |
314 |
+ if ([[ $COMP_CWORD -eq 3 ]] && [[ -z "$show" ]]) || \ |
315 |
+ ([[ $COMP_CWORD -eq 2 ]] && [[ -n "$show" ]]); then |
316 |
+ COMPREPLY=($(compgen -W "$(rc-status --list)" -- $cur)) |
317 |
+ elif [[ $COMP_CWORD -eq 2 ]]; then |
318 |
+ COMPREPLY=($(compgen -W "$(rc-service --list)" $cur)) |
319 |
+ elif [[ ${#COMP_WORDS[*]} -gt 2 ]] ; then |
320 |
+ COMPREPLY=($(compgen -W "$(rc-status --list)" -- $cur)) |
321 |
+ else |
322 |
+ unset COMPREPLY |
323 |
+ fi |
324 |
+ fi |
325 |
+ return 0 |
326 |
+} && |
327 |
+complete -F _rc_update rc-update |
328 |
|
329 |
diff --git a/mk/sys.mk b/mk/sys.mk |
330 |
index 62793739..82373dfe 100644 |
331 |
--- a/mk/sys.mk |
332 |
+++ b/mk/sys.mk |
333 |
@@ -59,6 +59,8 @@ MANPREFIX?= ${UPREFIX}/share |
334 |
MANDIR?= ${MANPREFIX}/man |
335 |
MANMODE?= 0444 |
336 |
|
337 |
+BASHCOMPDIR?= ${UPREFIX}/share/bash-completion/completions |
338 |
+ |
339 |
DATADIR?= ${UPREFIX}/share/openrc |
340 |
DATAMODE?= 0644 |