summaryrefslogtreecommitdiff
blob: 9ad2ce1c9b53496a1bd971e279cffccb57324a9b (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# Gentoo Linux Bash Shell Command Completion
#
# Copyright 1999-2013 Gentoo Foundation
# Distributed under the terms of the GNU General Public License, v2 or later

source "@helpersdir@/gentoo-common.sh"

_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 <ian@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
    # if no option typed
    else
        if [[ ${COMP_CWORD} -eq 1 ]]; then              # if first word typed
            words="`rc-service -l | 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 -r ${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 -l | grep ^${cur}`"
        COMPREPLY=($(for i in ${words} ; do \
            [[ ${i} == ${cur}* ]] && echo ${i} ; \
        done))
        return 0
    fi

    return 0
} &&
complete -F _rc_service rc-service

# vim: ft=sh:et:ts=4:sw=4:tw=80