commit:     d220fc272337b216bff6ac781a7b6be4e6f3caee
Author:     William Hubbs <w.d.hubbs <AT> gmail <DOT> com>
AuthorDate: Wed Dec  6 19:21:25 2017 +0000
Commit:     William Hubbs <williamh <AT> gentoo <DOT> org>
CommitDate: Wed Dec  6 19:25:26 2017 +0000
URL:        https://gitweb.gentoo.org/proj/openrc.git/commit/?id=d220fc27

add bash completion support

This fixes #188.

 Makefile                              |   3 +-
 bash-completion/Makefile              |  11 ++++
 bash-completion/openrc                |  24 ++++++++
 bash-completion/openrc-service-script |  29 +++++++++
 bash-completion/rc-service            | 113 ++++++++++++++++++++++++++++++++++
 bash-completion/rc-status             |  31 ++++++++++
 bash-completion/rc-update             |  42 +++++++++++++
 mk/sys.mk                             |   2 +
 8 files changed, 254 insertions(+), 1 deletion(-)

diff --git a/Makefile b/Makefile
index 05cfb796..663e103e 100644
--- a/Makefile
+++ b/Makefile
@@ -13,7 +13,8 @@ MK=                   ${TOP}/mk
 
 include ${TOP}/Makefile.inc
 
-SUBDIR=                conf.d etc init.d local.d man scripts sh src support 
sysctl.d
+SUBDIR=                bash-completion conf.d etc init.d local.d man scripts 
sh src \
+                       support sysctl.d
 
 # Build pkgconfig or not
 MKPKGCONFIG?=  yes

diff --git a/bash-completion/Makefile b/bash-completion/Makefile
new file mode 100644
index 00000000..3d9d886a
--- /dev/null
+++ b/bash-completion/Makefile
@@ -0,0 +1,11 @@
+DIR=   ${BASHCOMPDIR}
+CONF=  openrc \
+               openrc-service-script \
+               rc-service \
+               rc-status \
+               rc-update \
+
+MK=    ../mk
+include ${MK}/os.mk
+
+include ${MK}/scripts.mk

diff --git a/bash-completion/openrc b/bash-completion/openrc
new file mode 100644
index 00000000..e2718e0b
--- /dev/null
+++ b/bash-completion/openrc
@@ -0,0 +1,24 @@
+# Copyright (c) 2017 The OpenRC Authors.
+# See the Authors file at the top-level directory of this distribution and
+# https://github.com/OpenRC/openrc/blob/master/AUTHORS
+#
+# This file is part of OpenRC. It is subject to the license terms in
+# the LICENSE file found in the top-level directory of this
+# distribution and at https://github.com/OpenRC/openrc/blob/master/LICENSE
+# This file may not be copied, modified, propagated, or distributed
+# except according to the terms contained in the LICENSE file.
+
+#
+# openrc completion command
+#
+_openrc()
+{
+       local cur
+       COMPREPLY=()
+       cur="${COMP_WORDS[COMP_CWORD]}"
+       if [[ ${#COMP_WORDS[*]} -le 2 ]]; then
+               COMPREPLY=($(compgen -W "$(rc-status --list)" -- $cur))
+       fi
+       return 0
+} &&
+complete -F _openrc openrc

diff --git a/bash-completion/openrc-service-script 
b/bash-completion/openrc-service-script
new file mode 100644
index 00000000..455ad05f
--- /dev/null
+++ b/bash-completion/openrc-service-script
@@ -0,0 +1,29 @@
+# Copyright (c) 2017 The OpenRC Authors.
+# See the Authors file at the top-level directory of this distribution and
+# https://github.com/OpenRC/openrc/blob/master/AUTHORS
+#
+# This file is part of OpenRC. It is subject to the license terms in
+# the LICENSE file found in the top-level directory of this
+# distribution and at https://github.com/OpenRC/openrc/blob/master/LICENSE
+# This file may not be copied, modified, propagated, or distributed
+# except according to the terms contained in the LICENSE file.
+
+_openrc_service_script()
+{
+       local script="${COMP_WORDS[0]}"
+       local cur="${COMP_WORDS[$COMP_CWORD]}"
+
+       if [[ ( -f "${script}" || -h "${script}" ) && -r "${script}" ]] \
+       && [[ "$(head -n 1 "${script}")" =~ \#\!.*/openrc-run ]]
+       then
+               [[ $COMP_CWORD -gt 1 ]] && return 1
+               COMPREPLY=($(opts="start stop status restart pause zap ineed 
needsme iuse usesme broken"; \
+                       eval "$(grep '^opts=' "${script}")"; echo "${opts}"))
+               [[ -n "$COMPREPLY" ]] || COMPREPLY=(start stop restart zap)
+               COMPREPLY=($(compgen -W "${COMPREPLY[*]}" -- "${cur}"))
+       else
+               COMPREPLY=($(compgen -o default -- "${cur}"))
+       fi
+       return 0
+}
+complete -F _openrc_service_script */etc/init.d/*

diff --git a/bash-completion/rc-service b/bash-completion/rc-service
new file mode 100644
index 00000000..b4e7709f
--- /dev/null
+++ b/bash-completion/rc-service
@@ -0,0 +1,113 @@
+# Copyright (c) 2017 The OpenRC Authors.
+# See the Authors file at the top-level directory of this distribution and
+# https://github.com/OpenRC/openrc/blob/master/AUTHORS
+#
+# This file is part of OpenRC. It is subject to the license terms in
+# the LICENSE file found in the top-level directory of this
+# distribution and at https://github.com/OpenRC/openrc/blob/master/LICENSE
+# This file may not be copied, modified, propagated, or distributed
+# except according to the terms contained in the LICENSE file.
+
+#
+# rc-service completion command
+#
+_rc_service()
+{
+       local cur prev numwords opts
+       local words i x filename
+       local action actionpos
+       COMPREPLY=()
+       cur="${COMP_WORDS[COMP_CWORD]}"
+       prev="${COMP_WORDS[COMP_CWORD-1]}"
+       numwords=${#COMP_WORDS[*]}
+
+       if [[ ${prev} == '>' || ${prev} == '<' ]] ; then
+               COMPREPLY=($(compgen -f -- ${cur}))
+               return 0
+       fi
+
+       # find action
+       for x in ${COMP_LINE} ; do
+               if [[ ${x} =~ --(list|exists|resolve) ]] || [[ ${x} =~ -(l|e|r) 
]]
+               then
+                       action=${x}
+                       break
+               fi
+       done
+       if [[ -n ${action} ]]; then
+               for ((i = 0; i < ${numwords}; i++ )); do
+                       if [[ ${COMP_WORDS[${i}]} == "${action}" ]]; then
+                               actionpos=${i}
+                               break
+                       fi
+               done
+
+               for ((i = 1; i < ${numwords}; i++ )); do
+                       if [[ ! ${COMP_WORDS[$i]} == -* ]]; then
+                               break
+                       fi
+               done
+       fi
+
+       if [[ ${COMP_CWORD} -eq 3 ]]; then
+               return 1
+       fi
+
+       # check if an option was typed
+       if [[ ${cur} == -* ]]; then
+               if [[ ${cur} == --* ]]; then
+                       opts="--list --exists --resolve"
+                       COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
+                       return 0
+               elif [[ ${cur} == -* ]]; then
+                       opts="-l -e -r"
+                       COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
+                       return 0
+               fi
+
+               # NOTE: This slows things down!
+               # (Adapted from bash_completion by Ian Macdonald 
<i...@caliban.org>)
+               # This removes any options from the list of completions that 
have
+               # already been specified on the command line.
+               COMPREPLY=($(echo "${COMP_WORDS[@]}" | \
+               (while read -d ' ' i; do
+                       [[ -z ${i} ]] && continue
+                       # flatten array with spaces on either side,
+                       # otherwise we cannot grep on word boundaries of
+                       # first and last word
+                       COMPREPLY=" ${COMPREPLY[@]} "
+                       # remove word from list of completions
+                       COMPREPLY=(${COMPREPLY/ ${i%% *} / })
+               done
+               echo ${COMPREPLY[@]})))
+
+               return 0
+       else
+               # no option was typed
+               if [[ ${COMP_CWORD} -eq 1 ]]; then                        # if 
first word typed
+                       words="$(rc-service --list | grep ^${cur})"             
  # complete for init scripts
+                       COMPREPLY=($(for i in ${words} ; do \
+                       [[ ${i} == ${cur}* ]] && echo ${i} ; \
+                       done))
+                       return 0
+               elif [[ ${COMP_CWORD} -eq 2 ]] && [[ ${prev} != -* ]]; then # 
if second word typed and we didn't type in a function
+                       filename=$(rc-service --resolve ${prev})
+                       opts=$(cat ${filename} | grep "^\w*()" | sed 
"s/().*$//")       # Greps the functions included in the init script
+                       if [[ "x${opts}" == "x" ]] ; then                       
   # if no options found loosen the grep algorhythm
+                               opts=$(cat ${filename} | grep "\w*()" | sed 
"s/().*$//")
+                       fi
+                       COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
+                       return 0
+               fi
+       fi
+       if [[ ${action} == '--exists' ]] || [[ ${action} == '-e' ]] || \
+               [[ ${action} == '--resolve' ]]  || [[ ${action} == '-r' ]]; then
+               words="$(rc-service --list | grep ^${cur})"
+               COMPREPLY=($(for i in ${words} ; do \
+                       [[ ${i} == ${cur}* ]] && echo ${i} ; \
+               done))
+               return 0
+       fi
+       return 0
+} &&
+complete -F _rc_service rc-service

diff --git a/bash-completion/rc-status b/bash-completion/rc-status
new file mode 100644
index 00000000..d75ed148
--- /dev/null
+++ b/bash-completion/rc-status
@@ -0,0 +1,31 @@
+# Copyright (c) 2017 The OpenRC Authors.
+# See the Authors file at the top-level directory of this distribution and
+# https://github.com/OpenRC/openrc/blob/master/AUTHORS
+#
+# This file is part of OpenRC. It is subject to the license terms in
+# the LICENSE file found in the top-level directory of this
+# distribution and at https://github.com/OpenRC/openrc/blob/master/LICENSE
+# This file may not be copied, modified, propagated, or distributed
+# except according to the terms contained in the LICENSE file.
+
+#
+# rc-status completion command
+#
+_rcstatus()
+{
+       local cur
+       cur="${COMP_WORDS[COMP_CWORD]}"
+       if [[ $COMP_CWORD -eq 1 ]]; then
+               if [[ "${cur}" == --* ]]; then
+                       COMPREPLY=($(compgen -W '--all --list --unused' -- 
${cur}))
+               elif [[ "${cur}" == -* ]]; then
+                       COMPREPLY=($(compgen -W '-a -l -u' -- ${cur}))
+               else
+                       COMPREPLY=($(compgen -W "$(rc-status --list)" -- 
${cur}))
+               fi
+       else
+               unset COMPREPLY
+       fi
+       return 0
+} &&
+complete -F _rcstatus rc-status

diff --git a/bash-completion/rc-update b/bash-completion/rc-update
new file mode 100644
index 00000000..7fd310df
--- /dev/null
+++ b/bash-completion/rc-update
@@ -0,0 +1,42 @@
+# Copyright (c) 2017 The OpenRC Authors.
+# See the Authors file at the top-level directory of this distribution and
+# https://github.com/OpenRC/openrc/blob/master/AUTHORS
+#
+# This file is part of OpenRC. It is subject to the license terms in
+# the LICENSE file found in the top-level directory of this
+# distribution and at https://github.com/OpenRC/openrc/blob/master/LICENSE
+# This file may not be copied, modified, propagated, or distributed
+# except according to the terms contained in the LICENSE file.
+
+#
+# rc-update completion command
+#
+_rc_update()
+{
+       local cur show
+       COMPREPLY=()
+       cur="${COMP_WORDS[COMP_CWORD]}"
+       if [[ $COMP_CWORD -eq 1 ]]; then
+               if [[ "${cur}" == -* ]]; then
+                       COMPREPLY=($(compgen -W '-a -d -s' -- ${cur}))
+               else
+                       COMPREPLY=($(compgen -W 'add del show' ${cur}))
+               fi
+       else
+               if [[ "${COMP_WORDS[1]}" == "show" ]] || [[ "${COMP_WORDS[1]}" 
== "-s" ]]; then
+                       show="TRUE"
+               fi
+               if ([[ $COMP_CWORD -eq 3 ]] && [[ -z "$show" ]]) || \
+                       ([[ $COMP_CWORD -eq 2 ]] && [[ -n "$show" ]]); then
+               COMPREPLY=($(compgen -W "$(rc-status --list)" -- $cur))
+               elif [[ $COMP_CWORD -eq 2 ]]; then
+                       COMPREPLY=($(compgen -W "$(rc-service --list)" $cur))
+               elif [[ ${#COMP_WORDS[*]} -gt 2 ]] ; then
+                       COMPREPLY=($(compgen -W "$(rc-status --list)" -- $cur))
+               else
+                       unset COMPREPLY
+               fi
+       fi
+       return 0
+} &&
+complete -F _rc_update rc-update

diff --git a/mk/sys.mk b/mk/sys.mk
index 62793739..82373dfe 100644
--- a/mk/sys.mk
+++ b/mk/sys.mk
@@ -59,6 +59,8 @@ MANPREFIX?=           ${UPREFIX}/share
 MANDIR?=               ${MANPREFIX}/man
 MANMODE?=              0444
 
+BASHCOMPDIR?=          ${UPREFIX}/share/bash-completion/completions
+
 DATADIR?=              ${UPREFIX}/share/openrc
 DATAMODE?=             0644
 

Reply via email to