This repository was archived by the owner on Aug 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathutil.sh.in
More file actions
317 lines (283 loc) · 7.74 KB
/
Copy pathutil.sh.in
File metadata and controls
317 lines (283 loc) · 7.74 KB
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
310
311
312
313
314
315
316
317
#!/bin/bash
#
# util.sh: common functions
# This file is part of Yaourt (http://archlinux.fr/yaourt-en)
# Since we use some associated arrays, this file should be included from
# outside a function.
SUDONOVERIF=0
SUDOINSTALLED=0
SUDOREDIRECT=1
CLEANUP=()
ERROR_PKGS=()
declare -A LOADEDLIBS=()
type -p sudo &> /dev/null && SUDOINSTALLED=1
type -t gettext &> /dev/null || { gettext() { echo "$@"; }; }
# Programs arguments bitmask
(( A_PS = 1 << 0 )) # pacman -S
(( A_M = 1 << 1 )) # makepkg
(( A_PQ = 1 << 2 )) # pacman -Q
(( A_PC = 1 << 3 )) # pacman
(( A_PKC = 1 << 4 )) # package-query
(( A_CC = 1 << 5 )) # curl
(( A_PU = 1 << 6 )) # pacman -U
(( A_PO = 1 << 7 )) # pacman_out
# Programs arguments:
# pacman -S, makepkg, pacman -Q, pacman, package-query,
# curl, pacman -U, pacman_out
unset PACMAN_S_ARG MAKEPKG_ARG PACMAN_Q_ARG PACMAN_C_ARG PKGQUERY_C_ARG \
CURL_C_ARG PACMAN_U_ARG PACMAN_O_ARG
_gettext() {
local str=$1; shift
printf "$(gettext "$str")" "$@"
}
die() { exit ${1:-0}; }
# Called on exit
# CLEANUP is an array of commands to be run at exit.
cleanup() {
local line
for line in "${CLEANUP[@]}"; do eval $line; done
}
cleanup_add() {
CLEANUP+=("$(printf '%q ' "$@")")
}
trap cleanup 0
# Split command line arguments, i.e:
# -ab -> -a -b
# --foo=bar -> --foo bar
#
# Split arguments are stored in the OPTS array
#
# Parameters:
# $1,$2,$3,...,$n: arguments to split
explode_args() {
unset OPTS
local arg=$1 key value
while [[ $arg ]]; do
[[ $arg = "--" ]] && OPTS+=("$@") && break;
# Short options
if [[ ${arg:0:1} = "-" && ${arg:1:1} != "-" ]]; then
OPTS+=("-${arg:1:1}")
(( ${#arg} > 2 )) && arg="-${arg:2}" || { shift; arg=$1; }
# Long options
elif [[ ${arg:0:2} = "--" ]]; then
# Split argument at '=' :
# e.g --foo=bar -> key=--foo, value=bar
key=${arg%%=*}; value=${arg#*=}
OPTS+=("$key")
[[ $key != $value ]] && OPTS+=("$value")
shift; arg=$1
else
OPTS+=("$arg"); shift; arg=$1
fi
done
}
manage_error() { ERROR_PKGS+=("$1"); return 1; }
# Load library but never reload twice the same lib
load_lib() {
while [[ $1 ]]; do
((LOADEDLIBS[$1])) && return 0
if [[ ! -r "@libdir@/yaourt/$1.sh" ]]; then
error "$1.sh file is missing"
die 1
fi
. "@libdir@/yaourt/$1.sh" || warning "problem in $1.sh library"
LOADEDLIBS[$1]=1
shift
done
}
# Check if sudo is allowed for given command
_is_sudo_allowed() {
sudo -nl "$@" || \
(sudo -v && sudo -l "$@") && return 0
}
is_sudo_allowed() {
if (( SUDOINSTALLED )); then
(( SUDONOVERIF )) && return 0
if (( SUDOREDIRECT )); then
_is_sudo_allowed "$@" &> /dev/null && return 0
else
_is_sudo_allowed "$@" 2> /dev/null && return 0
fi
fi
return 1
}
fake_sudo() {
local errorfile=$(mktemp -u --tmpdir="$YAOURTTMPDIR")
local cmd=$(printf '%q ' "$@")
for i in 1 2 3; do
su --shell=$BASH --command "$cmd || touch $errorfile"
(( $? )) && [[ ! -f "$errorfile" ]] && continue
[[ -f "$errorfile" ]] && return 1
return 0
done
return 1
}
# Run "$@" as root using sudo or su
launch_with_su() {
if is_sudo_allowed "$@"; then
sudo "$@"
else
fake_sudo "$@"
fi
}
# Check array item existence (from makepkg)
# in_array ($needle, $array)
in_array() {
local needle=$1; shift
[[ "$1" ]] || return 1 # Not Found
local item
for item in "$@"; do
[[ "$item" = "$needle" ]] && return 0 # Found
done
return 1 # Not Found
}
# Check directory write permissions and set a cannonical name
# check_dir ($var)
# $var : name of variable containing directory
check_dir() {
[[ ! -d "${!1}" ]] && { error "${!1} $(gettext 'is not a directory')"; return 1; }
[[ ! -w "${!1}" ]] && { error "${!1} $(gettext 'is not writable')"; return 1; }
eval $1'="$(readlink -e "${!1}")"' # get cannonical name
return 0
}
# Check/init useful paths
init_paths() {
check_dir TMPDIR || die 1
YAOURTTMPDIR="$TMPDIR/yaourt-tmp-$(id -un)"
mkdir -p "$YAOURTTMPDIR" || check_dir YAOURTTMPDIR || die 1
(( EXPORT )) && [[ $EXPORTDIR ]] && { check_dir EXPORTDIR || die 1; }
[[ ! "$CACHE_DIR" ]] && CACHE_DIR="@cachedir@" &&
! [[ -d "$CACHE_DIR" ]] && { mkdir -p "$CACHE_DIR" || die 1; }
check_dir CACHE_DIR || die 1;
parse_pacman_conf
}
# Usage: program_arg ($dest, $arg)
program_arg() {
local dest=$1; shift
(( dest & A_PS )) && PACMAN_S_ARG+=("$@")
(( dest & A_M )) && MAKEPKG_ARG+=("$@")
(( dest & A_PQ )) && PACMAN_Q_ARG+=("$@")
(( dest & A_PC )) && PACMAN_C_ARG+=("$@")
(( dest & A_PKC )) && PKGQUERY_C_ARG+=("$@")
(( dest & A_CC )) && CURL_C_ARG+=("$@")
(( dest & A_PU )) && PACMAN_U_ARG+=("$@")
(( dest & A_PO )) && PACMAN_O_ARG+=("$@")
}
# programs call with command line options
pacman_parse() { LC_ALL=C pacman "${PACMAN_C_ARG[@]}" --color never "$@"; }
pacman_out() { $PACMAN "${PACMAN_C_ARG[@]}" "${PACMAN_O_ARG[@]}" "$@"; }
pkgquery() { package-query "${PKGQUERY_C_ARG[@]}" "$@"; }
y_makepkg() { $MAKEPKG "${MAKEPKG_ARG[@]}" "$@"; }
curl_fetch() { curl "${CURL_C_ARG[@]}" "$@"; }
# Run editor
# Usage: run_editor ($file, $default_answer)
# $file: file to edit
# $default_answer: 0: don't ask 1 (default): Y 2: N
run_editor() {
local edit_cmd
local file="$1"
local default_answer=${2:-1}
local answer_str=" YN"
local answer='Y'
if (( default_answer )); then
prompt "$(_gettext 'Edit %s ?' "$file") $(yes_no $default_answer) $(gettext '("A" to abort)')"
local answer=$(userinput "YNA" ${answer_str:$default_answer:1})
echo
[[ "$answer" = "A" ]] && msg "$(gettext 'Aborted...')" && return 2
[[ "$answer" = "N" ]] && return 1
fi
if [[ ! "$VISUAL" ]]; then
if [[ ! "$EDITOR" ]]; then
echo -e "$CRED$(gettext 'Please add $VISUAL to your environment variables')"
echo -e "$C0$(gettext 'for example:')"
echo -e "${CBLUE}export VISUAL=\"vim\"$C0 $(gettext '(in ~/.bashrc)')"
echo "$(gettext '(replace vim with your favorite editor)')"
echo
prompt2 "$(_gettext 'Edit %s with: ' "$file")"
read -e VISUAL
echo
else
VISUAL="$EDITOR"
fi
fi
[[ "$(basename "$VISUAL")" = "gvim" ]] && edit_cmd="$VISUAL --nofork" || edit_cmd="$VISUAL"
( $edit_cmd "$file" )
wait
}
# Main init
umask 0022
. '@libdir@/yaourt/io.sh'
. '@libdir@/yaourt/pacman.sh'
DBPATH='@dbpath@'
LOADEDLIBS+=([util]=1 [io]=1 [pacman]=1)
shopt -s extglob
# General
AUTOSAVEBACKUPFILE=0
DEVELBUILDDIR=''
DEVELSRCDIR=''
DEVEL=0
FORCE=0
SUDONOVERIF=0
NO_TESTDB=0
# ABS
USE_GIT=0
REPOS=()
SYNCSERVER=""
# AUR
AURURL='@aururl@'
AURCOMMENT=5
AURDEVELONLY=0
AURSEARCH=1
AURSHOWDIFF=0
AURUPGRADE=0
AURVOTE=1
# BUILD
EXPORT=0
EXPORTSRC=0
EXPORTDIR=""
# Prompt
NOCONFIRM=0
UP_NOCONFIRM=0
BUILD_NOCONFIRM=0
PU_NOCONFIRM=0
EDITFILES=1
NOENTER=1
# OUTPUT
USECOLOR=1
USEPAGER=0
DETAILUPGRADE=1
SHOWORPHANS=1
TERMINALTITLE=1
# Command
DIFFEDITCMD=${DIFFEDITCMD:-vimdiff}
# Load yaourt configuration
source_config() {
# Backup notable environment variables
local env_EDITOR="$EDITOR"
local env_VISUAL="$VISUAL"
# Source system-wide configuration file
[[ -r '@sysconfdir@/yaourtrc' ]] && source '@sysconfdir@/yaourtrc'
# Source user's configuration file:
# - $XDG_CONFIG_HOME/yaourt/yaourtrc
# - ~/.yaourtrc if the previous one does not exist
# Settings defined in the system-wide configuration file are overriden
XDG_YAOURT_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/yaourt"
if [[ -r $XDG_YAOURT_DIR/yaourtrc ]]; then
source "$XDG_YAOURT_DIR/yaourtrc"
elif [[ -r ~/.yaourtrc ]]; then
source ~/.yaourtrc
fi
# Restore environment variables if they were defined
# This overrides values from configuration files
[[ "$env_EDITOR" ]] && EDITOR="$env_EDITOR"
[[ "$env_VISUAL" ]] && VISUAL="$env_VISUAL"
}
source_config
TMPDIR=${TMPDIR:-/tmp}
check_dir TMPDIR || die 1
YAOURTTMPDIR="$TMPDIR/yaourt-tmp-$(id -un)"
export PACMAN=${PACMAN:-pacman}
export MAKEPKG=${MAKEPKG:-makepkg}
# DEVELBUILDDIR is depreceated
DEVELSRCDIR=${DEVELSRCDIR:-$DEVELBUILDDIR}
# vim: set ts=4 sw=4 noet: