summaryrefslogtreecommitdiff
path: root/completions/service
blob: bccb17cdd6fef8adb2eb85c00c189d04e4d4df63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# service(8) and /etc/init.d/* completion                  -*- shell-script -*-

# This function completes on services
#
_services()
{
    local sysvdir famdir
    [ -d /etc/rc.d/init.d ] && sysvdir=/etc/rc.d/init.d || sysvdir=/etc/init.d
    famdir=/etc/xinetd.d

    local restore_nullglob=$(shopt -p nullglob); shopt -s nullglob

    COMPREPLY=( $( printf '%s\n' $sysvdir/!($_backup_glob|functions) ) )

    if [ -d $famdir ]; then
        COMPREPLY+=( $( printf '%s\n' $famdir/!($_backup_glob) ) )
    fi

    $restore_nullglob

    COMPREPLY+=( $( systemctl list-units --full --all 2>/dev/null | \
        awk '$1 ~ /\.service$/ { sub("\\.service$", "", $1); print $1 }' ) )

    COMPREPLY=( $( compgen -W '${COMPREPLY[@]#@($sysvdir|$famdir)/}' -- "$cur" ) )
}

# This completes on a list of all available service scripts for the
# 'service' command and/or the SysV init.d directory, followed by
# that script's available commands
#
_service()
{
    local cur prev words cword
    _init_completion || return

    # don't complete past 2nd token
    [ $cword -gt 2 ] && return 0

    if [[ $cword -eq 1 && $prev == ?(*/)service ]]; then
        _services
    else
        local sysvdir
        [ -d /etc/rc.d/init.d ] && \
            sysvdir=/etc/rc.d/init.d || sysvdir=/etc/init.d
        COMPREPLY=( $( compgen -W '`sed -e "y/|/ /" \
            -ne "s/^.*\(U\|msg_u\)sage.*{\(.*\)}.*$/\2/p" \
            $sysvdir/${prev##*/} 2>/dev/null` start stop' -- "$cur" ) )
    fi

    return 0
} &&
complete -F _service service
for svc in /etc/init.d/!($_backup_glob) /etc/rc.d/init.d/!($_backup_glob); do
    [ -x "$svc" ] && complete -F _service $svc
done
unset svc

# ex: ts=4 sw=4 et filetype=sh