summaryrefslogtreecommitdiff
path: root/completions/getent
blob: 4c54a242447b678d6f01e25732892d3d6012a583 (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
# bash completion for getent                               -*- shell-script -*-

_getent()
{
    local cur prev words cword split
    _init_completion -s || return

    local i db
    for ((i = 1; i < cword; i++)); do
        case ${words[i]} in
            --version | --usage | --help | -!(-*)[V?])
                return
                ;;
            --service | -!(-*)s)
                ((i++))
                ;;
            -*) ;;

            *)
                # First non-option value is the db
                db=${words[i]}
                break
                ;;
        esac
    done

    case ${db-} in
        passwd)
            COMPREPLY=($(compgen -u -- "$cur"))
            return
            ;;
        group)
            COMPREPLY=($(compgen -g -- "$cur"))
            return
            ;;
        services)
            COMPREPLY=($(compgen -s -- "$cur"))
            return
            ;;
        hosts)
            COMPREPLY=($(compgen -A hostname -- "$cur"))
            return
            ;;
        protocols | networks | ahosts | ahostsv4 | ahostsv6 | rpc)
            COMPREPLY=($(compgen -W "$($1 $db |
                awk '{ print $1 }')" -- "$cur"))
            return
            ;;
        aliases | shadow | gshadow)
            COMPREPLY=($(compgen -W "$($1 $db | cut -d: -f1)" -- "$cur"))
            return
            ;;
        ethers | netgroup)
            return
            ;;
    esac

    case $prev in
        -s | --service)
            return
            ;;
    esac

    $split && return

    if [[ $cur == -* ]]; then
        COMPREPLY=($(compgen -W '$(_parse_help "$1")' -- "$cur"))
        [[ ${COMPREPLY-} == *= ]] && compopt -o nospace
    elif [[ ! -v db ]]; then
        COMPREPLY=($(compgen -W 'passwd group hosts services protocols
            networks ahosts ahostsv4 ahostsv6 aliases ethers netgroup rpc
            shadow gshadow' -- "$cur"))
    fi
} &&
    complete -F _getent getent

# ex: filetype=sh