Add and use _sysvdirs() function that sets correct SysV init directory.

This function allows distros to fine tune their init directory location,
as simple checking for directories presence is not always sufficient.
This commit is contained in:
Igor Murzov 2011-11-09 05:06:42 +03:00
parent 588facb6b6
commit fbbd476df0
2 changed files with 26 additions and 13 deletions

View File

@ -102,6 +102,17 @@ _userland()
[[ $userland == $1 ]] [[ $userland == $1 ]]
} }
# This function sets correct SysV init directories
#
_sysvdirs()
{
sysvdirs=( )
[[ -d /etc/rc.d/init.d ]] && sysvdirs+=( /etc/rc.d/init.d )
[[ -d /etc/init.d ]] && sysvdirs+=( /etc/init.d )
# Slackware uses /etc/rc.d
[[ -f /etc/slackware-version ]] && sysvdirs=( /etc/rc.d )
}
# This function checks whether we have a given program on the system. # This function checks whether we have a given program on the system.
# #
_have() _have()
@ -1043,13 +1054,12 @@ _backup_glob='@(#*#|*@(~|.@(bak|orig|rej|swp|dpkg*|rpm@(orig|new|save))))'
# #
_services() _services()
{ {
local sysvdir famdir local sysvdirs famdir=/etc/xinetd.d
[ -d /etc/rc.d/init.d ] && sysvdir=/etc/rc.d/init.d || sysvdir=/etc/init.d _sysvdirs
famdir=/etc/xinetd.d
local restore_nullglob=$(shopt -p nullglob); shopt -s nullglob local restore_nullglob=$(shopt -p nullglob); shopt -s nullglob
COMPREPLY=( $( printf '%s\n' $sysvdir/!($_backup_glob|functions) ) ) COMPREPLY=( $( printf '%s\n' ${sysvdirs[0]}/!($_backup_glob|functions) ) )
if [ -d $famdir ]; then if [ -d $famdir ]; then
COMPREPLY+=( $( printf '%s\n' $famdir/!($_backup_glob) ) ) COMPREPLY+=( $( printf '%s\n' $famdir/!($_backup_glob) ) )
@ -1060,7 +1070,8 @@ _services()
COMPREPLY+=( $( systemctl list-units --full --all 2>/dev/null | \ COMPREPLY+=( $( systemctl list-units --full --all 2>/dev/null | \
awk '$1 ~ /\.service$/ { sub("\\.service$", "", $1); print $1 }' ) ) awk '$1 ~ /\.service$/ { sub("\\.service$", "", $1); print $1 }' ) )
COMPREPLY=( $( compgen -W '${COMPREPLY[@]#@($sysvdir|$famdir)/}' -- "$cur" ) ) COMPREPLY=( $( compgen -W '${COMPREPLY[@]#@(${sysvdirs[0]}|$famdir)/}' \
-- "$cur" ) )
} }
# This function completes on modules # This function completes on modules
@ -1838,7 +1849,7 @@ _completion_loader()
[[ $BASH_SOURCE == */* ]] && compdir="${BASH_SOURCE%/*}/completions" [[ $BASH_SOURCE == */* ]] && compdir="${BASH_SOURCE%/*}/completions"
# Special case for init.d scripts. # Special case for init.d scripts.
if [[ $1 == /etc?(/rc.d)/init.d/* ]]; then if [[ $1 == /etc/@(rc.d|init.d)/* ]]; then
. "$compdir/service" &>/dev/null && return 124 || return 1 . "$compdir/service" &>/dev/null && return 124 || return 1
fi fi

View File

@ -15,20 +15,22 @@ _service()
if [[ $cword -eq 1 && $prev == ?(*/)service ]]; then if [[ $cword -eq 1 && $prev == ?(*/)service ]]; then
_services _services
else else
local sysvdir local sysvdirs
[ -d /etc/rc.d/init.d ] && \ _sysvdirs
sysvdir=/etc/rc.d/init.d || sysvdir=/etc/init.d
COMPREPLY=( $( compgen -W '`sed -e "y/|/ /" \ COMPREPLY=( $( compgen -W '`sed -e "y/|/ /" \
-ne "s/^.*\(U\|msg_u\)sage.*{\(.*\)}.*$/\2/p" \ -ne "s/^.*\(U\|msg_u\)sage.*{\(.*\)}.*$/\2/p" \
$sysvdir/${prev##*/} 2>/dev/null` start stop' -- "$cur" ) ) ${sysvdirs[0]}/${prev##*/} 2>/dev/null` start stop' -- "$cur" ) )
fi fi
return 0 return 0
} && } &&
complete -F _service service complete -F _service service
for svc in /etc/init.d/!($_backup_glob) /etc/rc.d/init.d/!($_backup_glob); do _sysvdirs
for svcdir in ${sysvdirs[@]}; do
for svc in $svcdir/!($_backup_glob); do
[ -x "$svc" ] && complete -F _service $svc [ -x "$svc" ] && complete -F _service $svc
done
done done
unset svc unset svc svcdir sysvdirs
# ex: ts=4 sw=4 et filetype=sh # ex: ts=4 sw=4 et filetype=sh