-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools
58 lines (53 loc) · 1.46 KB
/
tools
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
# Search for first existing binary of given list in PATH.
lookup_binary() {
for binary in "$@"; do
if which "$binary" >/dev/null 2>&1; then
echo "$binary"
return
fi
done
warn "Unable to find binary of '$*'."
return 1
}
# Ignore empty lines and comments started with '$1' (defaults to '#').
skip_comments() {
sed "s/\\s*${1:-#}.*\$//;/^\\s*\$/d"
}
# Only source that if not already enabled.
test -n "$skip_cleanup" ||
skip_cleanup=false
if ! $skip_cleanup; then
skip_cleanup=true
cleanup_actions=()
push_cleanup() {
local a=("$@")
debug +1 "push cleanup: `print_values_quoted "${a[@]}"`"
cleanup_actions=("(${a[*]@Q})" "${cleanup_actions[@]}")
}
pop_cleanup() {
test $# -eq 0 ||
deprecated "Argument is not supported anymore. Use 'rewind_cleanup()' instead."
local a
eval a="${cleanup_actions[0]}"
debug +2 "pop cleanup: `print_values_quoted "${a[@]}"`"
"${a[@]}"
cleanup_actions=("${cleanup_actions[@]:1}")
}
mark_cleanup() {
local mark=${#cleanup_actions[@]}
debug "Mark clean-up: $mark"
echo $mark
}
rewind_cleanup() {
local mark="$1"
debug +1 "Rewind clean-up: $mark"
while test ${#cleanup_actions[@]} -gt "$mark"; do
pop_cleanup
done
}
run_cleanup() {
rewind_cleanup 0
}
trap "run_cleanup" EXIT
fi
# vim: set ft=sh: