aboutsummaryrefslogtreecommitdiff
blob: da770b29229cebeb54f843c0fccfbf14e26284c6 (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#autoload

setopt extendedglob bareglobqual
#Description:
# functions for gentoo packages
# inspired by _deb_packages
#Usage: _gentoo_packages installed|available|installed_versions|available_versions|binary|category|useflag


_portdir() {
    local mainreponame mainrepopath overlayname overlaypath

    if [[ -e /usr/share/portage/config/repos.conf ]]; then
        if [[ ${1} == "-o" ]]; then
            for overlayname in $(_parsereposconf -l); do
                overlaypath+=($(_parsereposconf ${overlayname} location))
            done

            source /etc/make.conf 2>/dev/null
            source /etc/portage/make.conf 2>/dev/null

            overlaypath+=(${(@)PORTDIR_OVERLAY})

            echo "${(@u)overlaypath}"
        else
            mainreponame=$(_parsereposconf DEFAULT main-repo)
            mainrepopath=$(_parsereposconf ${mainreponame} location)

            echo "${mainrepopath}"
        fi
    else
        source /usr/share/portage/config/make.globals 2>/dev/null
        source /etc/make.conf 2>/dev/null
        source /etc/portage/make.conf 2>/dev/null

        if [[ ${1} == "-o" ]]; then
            echo "${(@u)PORTDIR_OVERLAY}"
        else
            echo "${PORTDIR}"
        fi
    fi
}

_parsereposconf() {
    local v f insection section arr

    for f in /usr/share/portage/config/repos.conf \
        /etc/portage/repos.conf \
        /etc/portage/repos.conf/*.conf; do

        [[ -f ${f} ]] || continue
        insection=0
        declare -A arr
        IFS='= '

        while read -r name value; do
            [[ -z ${name} || ${name} == '#'* ]] && continue

            if [[ (${name} == '['*']') && (-z ${value}) ]]; then
                value=${name//(\]|\[)}
                name="section"
            fi
            arr[${name}]=${value}

            if [[ ${insection} == 1 && ${name} == "section" ]]; then
                break
            elif [[ ${name} == "section" ]]; then
                [[ ${value} == ${1} ]] && insection=1
                secname+=(${value})
            elif [[ ${insection} == 1 ]]; then
                if [[ ${name} == ${2} ]]; then
                    v=${value}
                fi
            fi
            continue
        done < ${f}
    done

    if [[ ${1} == "-l" ]]; then
        echo "${(@)secname}"
    else
        echo "${v}"
    fi
}

_parsesetsconf() {
    [[ -d ${1} ]] || continue

    local v f places sections setsconf insection section arr

    if [[ -d ${1}/sets ]]; then
        setsconf=(${1}/sets/*.conf(N))
       [[ (($#setsconf > 0)) ]] && places=(${setsconf})
    elif [[ -f ${1}/sets.conf ]]; then
        places=(${1}/sets.conf)
    fi

    for f in ${(@)places}; do
        if [[ -r ${f} ]]; then
            insection=0
            declare -A arr
            IFS='= '
            while read -r name value; do
                [[ -z ${name} || ${name} == '#'* ]] && continue

                if [[ (${name} == '['*']') && (-z ${value}) ]]; then
                    value=${name//(\]|\[)}
                    name="section"
                fi
                arr[${name}]=${value}

                if [[ ${insection} == 1 && ${name} == "section" ]]; then
                    [[ ${2} == "sets" ]] && [[ ! ${value} =~ "sets" ]] && sections+=(${value})
                    insection=0
                elif [[ ${name} == "section" ]]; then
                    [[ ${value} =~ "${1:t} sets" ]] && insection=1
                    [[ ${2} == "sets" ]] && [[ ! ${value} =~ "sets" ]] && sections+=(${value})
                elif [[ ${insection} == 1 ]]; then
                    [[ ${2} == "sets" ]] && continue
                    if [[ ${name} == "directory" ]]; then
                        v=${value}
                    fi
                fi
                continue
            done < ${f}
        fi
    done

    if [[ ${2} == "sets" ]]; then
        [[ ((${#sections} > 0)) ]] && echo ${(@)sections}
    else
        [[ ((${#v} > 0)) ]] && echo ${v:t}
    fi
}

_gentoo_packages_update_installed_sets() {
    local sets
    sets=($(</var/lib/portage/world_sets))
    if [[ ((${#sets} > 0)) ]]; then
         _wanted installed_sets expl 'installed set' compadd "$@" "${(o@)^sets}"
    fi
}

_gentoo_packages_update_available_sets() {
    trees=($(_portdir -o) /etc/portage /usr/share/portage/config)
    for PORTDIR in ${(@)trees}; do
        if [[ -d ${PORTDIR} ]]; then
            setsdir="$(_parsesetsconf ${PORTDIR})"
            [[ ! -z "${setsdir}" ]] && setspath="${PORTDIR}/${setsdir}" || setspath="${PORTDIR}/sets"
            if [[ -d "${setspath}" ]]; then
                setsfiles=(${setspath}/*~*.conf(N))
                for set in ${setsfiles[@]}; do
                    sets+=(${set}(:t))
                done
                sets+=($(_parsesetsconf ${PORTDIR} sets))
            fi
        fi
    done
    if [[ ((${#sets} > 0)) ]]; then
         _wanted available_sets expl 'available set' compadd "$@" "@${(o@)^sets}"
    fi
}

# Completion function to show useflags.
_gentoo_packages_update_useflag(){
    local flags trees

    flags=()
    trees=($(_portdir) $(_portdir -o))

    for PORTDIR in ${trees[@]}; do
       [[ -r ${PORTDIR}/profiles/use.desc ]] &&
        flags+=(${${(M)${(f)"$(<$PORTDIR/profiles/use.desc)"}:#* - *}%% - *})
       [[ -r ${PORTDIR}/profiles/use.local.desc ]] &&
        flags+=(${${${(M)${(f)"$(<$PORTDIR/profiles/use.local.desc)"}#* - *}%% - *}#*:})
    done

    compadd $flags
}

_gentoo_packages_update_active_useflag(){
    local flags USE
    var=USE
    [[ -z ${(P)var} && -r /etc/portage/make.conf ]] &&
      local $var="`. /etc/portage/make.conf 2>/dev/null; echo ${(P)var}`"
    [[ -z ${(P)var} && -r /etc/make.conf ]] &&
      local $var="`. /etc/make.conf 2>/dev/null; echo ${(P)var}`"
    flags=(${${${=USE}%-*}%\\*})
    compadd $flags
}
_gentoo_packages_update_category(){
    local trees category

    trees=($(_portdir) $(_portdir -o))
    category=( $trees/*-*(/:t) )
    _wanted cat_packages expl 'category' compadd "$@" $category
}

_gentoo_packages_update_installed(){
   local installed_dir installed_portage installed_list expl
   installed_dir="/var/db/pkg"
   installed_portage=($installed_dir/*-*/*)

   installed_pkgname=(${${installed_portage:t}%%-[0-9]*})
   _wanted packages expl 'package' compadd "$@" ${installed_pkgname}

   installed_list=( ${${installed_portage#$installed_dir/}%%-[0-9]*} )
   _wanted cat_packages expl 'category/package' _multi_parts "$@" / installed_list
}

_gentoo_packages_update_installed_versions(){
    local installed_list installed_portage expl

    installed_portage=(/var/db/pkg/*-*/*)
    _wanted packages expl 'package' compadd "$@" ${installed_portage:t}

    installed_list=(${installed_portage##*/pkg/})
    _wanted cat_packages expl 'category/package' _multi_parts "$@" / installed_list
}

_gentoo_packages_update_available_pkgnames_only(){
    local trees packages

    trees=($(_portdir) $(_portdir -o))

    packages=($trees/*-*/*(:t))
    _wanted packages expl 'package' compadd - "${(@)packages}"
}

_gentoo_packages_update_available(){
    local trees category packages pkg expl

    trees=($(_portdir) $(_portdir -o))
    category=($trees/*-*(/:t))

    packages=($trees/*-*/*(:t))
    _wanted packages expl 'package' compadd - "${(@)packages}"

    # Complete cat/pkg. _multi_parts is much to slow for such a large task,
    # _sep_parts removes the dash from gnome-<tab>, and _path_files wants to
    # complete cat/pkg/files (if "files" is ignored with -F, miscfiles, etc...
    # don't get completed).
    if [[ $PREFIX != */* ]] ; then
        _wanted cat_packages expl 'category/package' compadd -S '/' $category
    else
        compset -P '*/'
        pkg=($trees/$IPREFIX/*(:t))
        _wanted cat_packages expl 'category/package' compadd $pkg
    fi
}

_gentoo_packages_update_available_versions(){
    local var overlay_ebuilds portage_ebuilds expl trees category

    PORTDIR=$(_portdir)
    PORTDIR_OVERLAY=$(_portdir -o)

    trees=($PORTDIR $=PORTDIR_OVERLAY)
    category=($trees/*-*(/:t))
    typeset -U category

    if [[ $#PREFIX -ge 1 && -z $words[(r)(--inject|-i)] ]] ; then
        overlay_ebuilds=($=PORTDIR_OVERLAY/*-*/${PREFIX%%-[0-9]#*}*/*.ebuild(:t:r) )
        portage_ebuilds=($PORTDIR/metadata/cache/*-*/${PREFIX%%-[0-9]#*}*(:t))
        _wanted packages expl 'package' compadd $portage_ebuilds $overlay_ebuilds
    fi
    pkg=( $trees/${PREFIX%%/*}/*/*.ebuild(:t:r) )
    _wanted cat_packages expl 'category/package' _sep_parts category / pkg
}

#Function to show tbz2 files available
_gentoo_packages_update_binary() {
    [[ -z $PKGDIR && -r /etc/portage/make.conf ]] &&
        local PKGDIR="`. /etc/portage/make.conf 2>/dev/null; echo $PKGDIR`"
    [[ -z $PKGDIR && -r /etc/make.conf ]] &&
        local PKGDIR="`. /etc/make.conf 2>/dev/null; echo $PKGDIR`"
    [[ -z $PKGDIR && -r /usr/share/portage/config/make.globals ]] &&
        local PKGDIR="`. /usr/share/portage/config/make.globals 2>/dev/null; echo $PKGDIR`"

    # this doesn't take care of ${PORTAGE_BINHOST}. If Gentoo official
    # binary mirror will be available we should rewrite it accordingly.
    _path_files -g \*.tbz2 -W "$PKGDIR/All"
}

_gentoo_packages () {
    local command="$argv[$#]" expl cachevar pkgset update_policy
    zstyle -s ":completion:*:*:$service:*" cache-policy update_policy
    if [[ -z "$update_policy" ]]; then
        zstyle ":completion:*:*:$service:*" cache-policy _gentoo_cache_policy
    fi
    [[ "$command" == (installed(_versions|_sets|)|available(_versions|_sets|_pkgnames_only|)|binary|category|(active_|)useflag) ]] || {
        _message "unknown command: $command"
        return
    }
    [[ "$pkgset" == (installed(_versions|_sets|)|available(_versions|_sets|_pkgnames_only|)|binary|category|(active_|)useflag) ]] || {
        pkgset="$command"
    }
    expl=("${(@)argv[1,-2]}")
    _gentoo_packages_update_$pkgset
}

_gentoo_cache_policy () {
  # rebuild if cache is more than a week old
    oldp=( "$1"(mw+1) )
      (( $#oldp )) && return 0
}


_gentoo_packages "$@"