NBash

Форк
0
257 строк · 6.0 Кб
1
#!/bin/sh
2
#
3
# Copyright (C) 2012, 2013, 2016-2020  Etersoft
4
# Copyright (C) 2012, 2013, 2016-2020  Vitaly Lipatov <lav@etersoft.ru>
5
#
6
# This program is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU Affero General Public License as published by
8
# the Free Software Foundation, either version 3 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU Affero General Public License for more details.
15
#
16
# You should have received a copy of the GNU Affero General Public License
17
# along with this program. If not, see <http://www.gnu.org/licenses/>.
18
#
19

20
load_helper epm-check_updated_repo
21
load_helper epm-sh-warmup
22

23
__epm_search_output()
24
{
25
local CMD
26
local string="$*"
27
case $PMTYPE in
28
    apt-rpm|apt-dpkg)
29
        CMD="apt-cache search --"
30
        ;;
31
    aptitude-dpkg)
32
        CMD="aptitude search --"
33
        ;;
34
    deepsolver-rpm)
35
        CMD="ds-require --"
36
        ;;
37
    packagekit)
38
        CMD="pkcon search name"
39
        ;;
40
    urpm-rpm)
41
        # urpmq does not support --
42
        CMD="urpmq -y"
43
        ;;
44
    pkgsrc)
45
        CMD="pkg_info -x --"
46
        ;;
47
    pkgng)
48
        CMD="pkg search -i --"
49
        ;;
50
    emerge)
51
        CMD="emerge --search --"
52
        ;;
53
    pacman)
54
        CMD="pacman -Ss --"
55
        ;;
56
    aura)
57
        CMD="aura -As --"
58
        ;;
59
    eopkg)
60
        CMD="eopkg search --"
61
        ;;
62
    yum-rpm)
63
        CMD="yum search --"
64
        ;;
65
    dnf-rpm)
66
        CMD="dnf search --"
67
        ;;
68
    zypper-rpm)
69
        CMD="zypper search -d --"
70
        ;;
71
    mpkg)
72
        CMD="mpkg search"
73
        ;;
74
    apk)
75
        CMD="apk search"
76
        ;;
77
    tce)
78
        CMD="tce-ab"
79
        ;;
80
    conary)
81
        CMD="conary repquery"
82
        ;;
83
    npackd)
84
        docmd npackdcl search --query="$string" --status=all
85
        return
86
        ;;
87
    choco)
88
        CMD="choco list"
89
        ;;
90
    slackpkg)
91
        # FIXME
92
        echo "Note: case sensitive search"
93
        if [ -n "$verbose" ] ; then
94
            CMD="/usr/sbin/slackpkg search"
95
        else
96
            LC_ALL=C docmd /usr/sbin/slackpkg search $string | grep " - " | sed -e 's|.* - ||g'
97
            return
98
        fi
99
        ;;
100
    opkg)
101
        CMD="opkg find"
102
        ;;
103
    homebrew)
104
        CMD="brew search"
105
        ;;
106
    guix)
107
        CMD="guix package -A"
108
        ;;
109
    android)
110
        CMD="pm list packages"
111
        ;;
112
    termux-pkg)
113
        CMD="pkg search"
114
        ;;
115
    aptcyg)
116
        CMD="apt-cyg searchall"
117
        ;;
118
    xbps)
119
        CMD="xbps-query -s"
120
        ;;
121
    appget|winget)
122
        CMD="$PMTYPE search"
123
        ;;
124
    *)
125
        fatal "Have no suitable search command for $PMTYPE"
126
        ;;
127
esac
128

129
LC_ALL=C docmd $CMD $string
130
epm play $short --list-all | sed -e 's|^ *||g' -e 's|[[:space:]]\+| |g' -e "s|\$| (use \'epm play\' to install it)|"
131
}
132

133
# copied from eget's filter_glob
134
# check man glob
135
__convert_glob__to_regexp()
136
{
137
    # translate glob to regexp
138
    echo "$1" | sed -e "s|\*|.*|g" -e "s|?|.|g"
139
}
140

141
# ^mc.*ext -> mc
142
_clean_from_regexp()
143
{
144
    sed -e "s/[?\^.*]/ /g"
145
}
146

147
# ^mc*e?t -> mc
148
__clean_from_glob()
149
{
150
    sed -e "s/[?*].*//" -e "s/[?\^.*]/ /g"
151
}
152

153

154
# produce grep sequence
155
__epm_search_make_grep()
156
{
157
    local i
158
    [ -z "$*" ] && return
159

160
    local list=
161
    local listN=
162
    for i in $@ ; do
163
        case "$i" in
164
            ~*)
165
                # will clean from ~ later (and have the bug here with empty arg if run with one ~ only)
166
                listN="$listN $i"
167
                ;;
168
            *)
169
                list="$list $i"
170
                ;;
171
        esac
172
    done
173

174
    #list=$(strip_spaces $list | sed -e "s/ /|/g")
175
    listN=$(strip_spaces $listN | sed -e "s/ /|/g" | sed -e "s/~//g")
176

177
    # TODO: only apt supports regexps?
178
    case $PMTYPE in
179
        apt-*)
180
            ;;
181
        *)
182
                list=$(echo "$list" | sed -e "s/[?\^.]/ /g")
183
                listN=$(echo "$listN" | sed -e "s/[?\^.]/ /g")
184
            ;;
185
    esac
186

187
    list=$(__convert_glob__to_regexp "$list")
188
    listN=$(__convert_glob__to_regexp "$listN")
189

190
    if [ -n "$short" ] ; then
191
        echon " | sed -e \"s| .*||g\""
192
    fi
193

194
    [ -n "$listN" ] && echon " | grep -E -i -v -- \"$listN\""
195

196
    # FIXME: The World has not idea how to do grep both string
197
    # http://stackoverflow.com/questions/10110051/grep-with-two-strings-logical-and-in-regex?rq=1
198

199
    # Need only if we have more than one word (with one word we will grep for colorify)
200
    if [ "$(echo "$list" | wc -w)" -gt 1 ] ; then
201
        for i in $list ; do
202
            # FIXME -n on MacOS?
203
            echon " | grep -E -i -- \"$i\""
204
        done
205
    fi
206

207
    # FIXME: move from it
208
    #isatty || return
209

210
    # TODO: sorts word by length from large to short
211

212
    local COLO=""
213
    # rule for colorife
214
    for i in $list $listN; do
215
        [ -n "$COLO" ] && COLO="$COLO|"
216
        COLO="$COLO$i"
217
    done
218

219
    # TODO: use some colorifer instead grep (check grep adove too)
220
    if [ -n "$list" ] ; then
221
        echon " | grep -E -i $EGREPCOLOR -- \"($COLO)\""
222
    fi
223
}
224

225
# we need internal implementation because regular epm_search uses quotes_args
226
__epm_search_internal()
227
{
228
    [ -n "$1" ] || fatal "Search: search argument(s) is missed"
229

230
    # it is useful for first time running
231
    update_repo_if_needed soft
232

233
    warmup_bases
234

235
    __epm_search_output $(get_firstarg $@) | grep "$*"
236
}
237

238
# copied from korinf/tools/run-script/scripts/search
239

240
epm_search()
241
{
242
    [ -n "$1" ] || fatal "Search: search argument(s) is missed"
243

244
    # it is useful for first time running
245
    update_repo_if_needed soft
246

247
    warmup_bases
248

249
    echo "$*" | grep -q "\.[*?]" && warning "Only glob symbols * and ? are supported. Don't use regexp here!"
250

251
    # FIXME: do it better
252
    local MGS
253
    MGS=$(eval __epm_search_make_grep $quoted_args)
254
    EXTRA_SHOWDOCMD="$MGS"
255
    # TODO: use search args for more optimal output
256
    eval "__epm_search_output \"$(eval get_firstarg $quoted_args | __clean_from_glob)\" $MGS"
257
}
258

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.