summaryrefslogtreecommitdiffstats
path: root/completions/dpkg
blob: 7ce5c1972ff6ed7ff9d34b04eea9a7c6e5d98109 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# dpkg(1) and related commands completion                  -*- shell-script -*-

# @since 2.12
_comp_xfunc_dpkg_compgen_installed_packages()
{
    _comp_compgen_split -- "$(
        grep-status -P -e "^${cur-}" -a \
            -FStatus 'ok installed' \
            -n -s Package 2>/dev/null ||
            _comp_awk -F '\n' -v RS="" "
            index(\$1, \"Package: ${cur-}\") == 1 &&
            \$2 ~ /ok installed|half-installed|unpacked|half-configured|^Essential: yes/ {
                print(substr(\$1, 10));
            }" /var/lib/dpkg/status 2>/dev/null
    )"
}

# @since 2.12
_comp_xfunc_dpkg_compgen_purgeable_packages()
{
    _comp_compgen_split -- "$(
        grep-status -P -e "^${cur-}" -a \
            -FStatus 'ok installed' -o -FStatus 'ok config-files' \
            -n -s Package 2>/dev/null ||
            _comp_awk -F '\n' -v RS="" "
            index(\$1, \"Package: ${cur-}\") == 1 &&
            \$2 ~ /ok installed|half-installed|unpacked|half-configured|config-files|^Essential: yes/ {
                print(substr(\$1, 10));
            }" /var/lib/dpkg/status 2>/dev/null
    )"
}

# @since 2.12
_comp_xfunc_dpkg_compgen_held_packages()
{
    _comp_compgen_split -- "$(
        dpkg --get-selections ${cur:+"$cur}"} |
            _comp_awk '{for(i=2;i<=NF;i++){ if($i=="hold"){ print $1;break }}}'
    )"
}

# @deprecated 2.12 use _comp_xfunc_dpkg_compgen_installed_packages instead
_comp_dpkg_installed_packages()
{
    local COMPREPLY=() cur="${1-}"
    # shellcheck disable=SC2119
    _comp_xfunc_dpkg_compgen_installed_packages
    printf "%s\n" "${COMPREPLY[@]}"
}
# @deprecated 2.12 use _comp_xfunc_dpkg_compgen_purgeable_packages instead
_comp_dpkg_purgeable_packages()
{
    local COMPREPLY=() cur="${1-}"
    # shellcheck disable=SC2119
    _comp_xfunc_dpkg_compgen_purgeable_packages
    printf "%s\n" "${COMPREPLY[@]}"
}

# Debian dpkg(1) completion
#
_comp_cmd_dpkg()
{
    local cur prev words cword was_split comp_args
    _comp_initialize -s -- "$@" || return

    local i=$cword

    # find the last option flag
    if [[ $cur != -* ]]; then
        while [[ $prev != -* && $i -ne 1 ]]; do
            prev=${words[--i - 1]}
        done
    fi

    local noargopts='!(-*|*[ciAIfexXbsplWSrVLPD]*)'
    # shellcheck disable=SC2254
    case $prev in
        --install | --unpack | --record-avail | --contents | --info | --fsys-tarfile | \
            --field | --control | --extract | --vextract | --raw-extract | -${noargopts}[ciAIfexX])
            _comp_compgen_filedir '?(u|d)deb'
            return
            ;;
        --build | --admindir | --instdir | --root | -${noargopts}b)
            _comp_compgen_filedir -d
            return
            ;;
        --status | --print-avail | --list | -${noargopts}[spl])
            _comp_compgen -x apt-cache packages
            return
            ;;
        --show | -${noargopts}W)
            if [[ $1 == *dpkg-query ]]; then
                _comp_compgen -x apt-cache packages
            else
                _comp_compgen_filedir '?(u|d)deb'
            fi
            return
            ;;
        --search | -${noargopts}S)
            _comp_compgen_filedir
            return
            ;;
        --remove | --verify | -${noargopts}[rV])
            _comp_xfunc_dpkg_compgen_installed_packages
            return
            ;;
        --listfiles | --purge | -${noargopts}[LP])
            _comp_xfunc_dpkg_compgen_purgeable_packages
            return
            ;;
        --debug | -${noargopts}D)
            _comp_compgen -- -W 'help'
            return
            ;;
        --ignore-depends)
            local packages
            _comp_compgen -v packages -c "${cur##*,}" -x apt-cache packages
            _comp_delimited , -W '"${packages[@]}"'
            return
            ;;
        --log)
            _comp_compgen_filedir log
            return
            ;;
        --path-exclude | --path-include)
            return
            ;;
        --status-logger)
            _comp_compgen_commands
            return
            ;;
        --verify-format)
            _comp_compgen -- -W 'rpm'
            return
            ;;
    esac

    [[ $was_split ]] && return

    _comp_compgen_help
    for i in ${!COMPREPLY[*]}; do
        # remove ones ending with a dash (known parse issue, hard to fix)
        [[ ${COMPREPLY[i]} != *- ]] || unset -v 'COMPREPLY[i]'
    done
    [[ ${COMPREPLY-} == *= ]] && compopt -o nospace
} &&
    complete -F _comp_cmd_dpkg dpkg dpkg-deb dpkg-query

_comp_cmd_dpkg_reconfigure()
{
    local cur prev words cword comp_args
    _comp_initialize -- "$@" || return

    local opt

    local noargopts='!(-*|*[fp]*)'
    # shellcheck disable=SC2254
    case $prev in
        --frontend | -${noargopts}f)
            if _comp_expand_glob opt '/usr/share/perl5/Debconf/FrontEnd/*'; then
                opt=("${opt[@]##*/}")
                opt=("${opt[@]%.pm}")
                _comp_compgen -- -W '"${opt[@]}"'
            fi
            return
            ;;
        --priority | -${noargopts}p)
            _comp_compgen -- -W 'low medium high critical'
            return
            ;;
    esac

    if [[ $cur == -* ]]; then
        _comp_compgen -- -W '--frontend --priority --all --unseen-only --help
            --showold --force --terse'
    else
        _comp_xfunc_dpkg_compgen_installed_packages
    fi
} &&
    complete -F _comp_cmd_dpkg_reconfigure -o default dpkg-reconfigure

# ex: filetype=sh