summaryrefslogtreecommitdiff
path: root/pkg.bash_completion
blob: 2814ce42e4e103b11aa6f463e13c957b5d290a86 (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
# bash completion for pkg - rawnix.org package manager
# https://rawnix.org
#
# Install: /usr/share/bash-completion/completions/pkg
#
# Copyright (c) 2025 zorz@gmx.com
# SPDX-License-Identifier: BSD-2-Clause

# -- Data sources ----------------------------------------------------------

_pkg_installed() {
    # Package names from /var/lib/pkg/db: the first line of each
    # record (name, version, files..., blank line)
    local db="/var/lib/pkg/db"
    [[ -r "$db" ]] || return
    awk 'state==0 && NF { print; state=1; next }
         NF == 0  { state=0 }' "$db" 2>/dev/null
}

_pkg_available() {
    # Ports in the portdir directories of /etc/pkg.conf: every
    # subdirectory holding a MAKEPKG or a MAKEPKG.lock
    local conf="/etc/pkg.conf" d p
    [[ -r "$conf" ]] || return
    while read -r key d _; do
        [[ "$key" == portdir && -d "$d" ]] || continue
        for p in "$d"/*/MAKEPKG "$d"/*/MAKEPKG.lock; do
            [[ -f "$p" ]] && basename "${p%/*}"
        done
    done < "$conf" | sort -u
}

_pkg_locked() {
    # Locked packages from /var/lib/pkg/locked, without its comment
    local lockfile="/var/lib/pkg/locked"
    [[ -r "$lockfile" ]] || return
    grep -v '^[[:space:]]*\(#\|$\)' "$lockfile" 2>/dev/null
}

# -- Main completion function ----------------------------------------------

_pkg_complete() {
    local cur prev words cword
    _init_completion || return

    local commands="add del list isinst update build prepare bulk sysup \
                    autoremove orphans depends rdepends diff \
                    lock unlock locked checkdb initdb version help"
    local opts="-r --root -N --no-deps -b --use-bulk -y --yes -f --force
                -v --verbose -V --version -h --help"

    if [[ "$prev" == -r || "$prev" == --root ]]; then
        _filedir -d
        return
    fi
    if [[ "$cur" == -* ]]; then
        COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
        return
    fi

    # the command is the first word that is not an option or -r's path
    local i cmd=""
    for (( i = 1; i < cword; i++ )); do
        case "${words[i]}" in
            -r|--root) (( i++ )) ;;
            -*)        ;;
            *)         cmd="${words[i]}"; break ;;
        esac
    done

    if [[ -z "$cmd" ]]; then
        COMPREPLY=( $(compgen -W "$commands" -- "$cur") )
        return
    fi

    case "$cmd" in
        del|update|isinst|lock)
            COMPREPLY=( $(compgen -W "$(_pkg_installed)" -- "$cur") )
            ;;
        unlock)
            COMPREPLY=( $(compgen -W "$(_pkg_locked)" -- "$cur") )
            ;;
        add|build|prepare|bulk|depends)
            COMPREPLY=( $(compgen -W "$(_pkg_available)" -- "$cur") )
            ;;
        rdepends)
            COMPREPLY=( $(compgen -W "$(_pkg_installed)" -- "$cur") )
            ;;
        *)
            ;;
    esac
}

complete -F _pkg_complete pkg