Project

General

Profile

1 8291 aaronmk
#!/bin/bash -e
2 8952 aaronmk
set -o errexit # in case caller did not have -e in #! line
3
4 9018 aaronmk
if test ! "$_util_sh_include_guard_utils"; then
5
_util_sh_include_guard_utils=1
6
7 8957 aaronmk
function extern () { (exec "$@") || return; }
8 8917 aaronmk
9 9002 aaronmk
isset () { test -n "${!1+isset}"; }
10
11 8716 aaronmk
realpath () { readlink -f -- "$1"; }
12 8703 aaronmk
13 8917 aaronmk
include_guard_var () { realpath "$1"|"extern" sed 's/[^a-zA-Z0-9_]/_/g'; }
14 8716 aaronmk
15 8703 aaronmk
self_not_included () # usage: if self_not_included; then ... fi
16
{
17 8971 aaronmk
	test $# -ge 1 || set -- "${BASH_SOURCE[1]}"
18 8703 aaronmk
	local include_guard="$(include_guard_var "$1")"
19 8793 aaronmk
	alias self_being_included=false
20 9003 aaronmk
	! isset "$include_guard" && \
21 8793 aaronmk
	{ eval "$include_guard"=1; alias self_being_included=true; }
22 8703 aaronmk
}
23
24 8793 aaronmk
# to load newly-defined aliases for use in functions in the same file:
25
## fi # load new aliases
26
## if self_being_included; then
27
# this is needed because aliases defined inside an if statement are not
28
# available inside that if statement
29
30 9018 aaronmk
fi
31 9017 aaronmk
32 9018 aaronmk
33 8704 aaronmk
if self_not_included "${BASH_SOURCE[0]}"; then
34
35 9017 aaronmk
36
#### options
37
38 8709 aaronmk
shopt -s expand_aliases
39
40 9017 aaronmk
41
#### aliases
42
43 8889 aaronmk
unalias () { builtin unalias "$@" 2>&- || true; } # no error if undefined
44
45 9017 aaronmk
46 8886 aaronmk
#### exceptions
47
48 9032 aaronmk
# usage: cmd || { save_e; ...; rethrow; }
49 9031 aaronmk
alias export_e='e=$?'
50 9032 aaronmk
alias save_e='declare e=$?'
51 8976 aaronmk
alias rethrow='return "$e"'
52
alias rethrow_subshell='exit "$e"'
53
54 8978 aaronmk
fi # load new aliases
55
if self_being_included; then
56
57 9036 aaronmk
# usage: cmd || die msg
58
die () { save_e; echo "$1" >&2; rethrow; }
59
60 9035 aaronmk
# usage: cmd || { save_e; log_e; ...; rethrow; }
61
log_e () { echo "! command exited with error $e" >&2; }
62 8981 aaronmk
63 8886 aaronmk
# usage: try cmd...; ignore status; if catch status; then ...; fi; end_try
64
65 9031 aaronmk
function try () { e=0; "$@" || { export_e; true; }; }
66 8980 aaronmk
alias try='declare e; try ' # last space alias-expands next word
67 8886 aaronmk
68
catch () { test "$e" -eq "$1"; e=0; }
69
70
ignore () { catch "$@" || true; }
71
72 8977 aaronmk
alias end_try='rethrow'
73
alias end_try_subshell='rethrow_subshell'
74 8886 aaronmk
75 9020 aaronmk
fi # load new aliases
76
if self_being_included; then
77 9017 aaronmk
78 9020 aaronmk
79 9023 aaronmk
#### functions
80
81 9024 aaronmk
func_exists () { declare -f "$1" >/dev/null; }
82
83 9023 aaronmk
copy_func () # usage: from=... to=... copy_func
84 9037 aaronmk
# $to must not exist. to get around the no-clobber restriction, use `unset -f`.
85 9023 aaronmk
{
86
	: "${from:?}" "${to:?}"
87 9037 aaronmk
	func_exists "$from" || die "function does not exist: $from"
88
	! func_exists "$to" || die "function already exists: $to"
89 9023 aaronmk
	local from_def="$(declare -f "$from")"
90
	eval "$to${from_def#$from}"
91
}
92
93 9038 aaronmk
func_override () # usage: func_override old_name__suffix
94
{ from="${1%%__*}" to="$1" copy_func; }
95 9023 aaronmk
96 9038 aaronmk
97 8899 aaronmk
#### integers
98
99 9041 aaronmk
let! () { let "$@" || true; } # always returns true; safe to use for setting
100 8899 aaronmk
	# "If the last ARG evaluates to 0, let returns 1" (`help let`)
101
102 9028 aaronmk
bool2int () { try test -z "$1"; echo "$e"; } # empty->0; non-empty->1
103 9004 aaronmk
104 9017 aaronmk
105 8854 aaronmk
#### arrays
106 8694 aaronmk
107 8833 aaronmk
join () { local IFS="$delim"; echo "$*"; } # usage: delim=... join elems...
108 8816 aaronmk
109 8691 aaronmk
reverse () # usage: array=($(reverse args...))
110
{
111
	local i
112
	for (( i=$#; i >= 1; i-- )); do printf '%q ' "${!i}"; done
113
}
114
115 9017 aaronmk
116 9040 aaronmk
#### paths
117
118
canon_rel_path ()
119
{
120
	local path="$1"
121
	path="$(realpath "$path")" # canonicalize
122
	path="${path#$(pwd -P)/}" # remove any shared prefix with the current dir
123
	echo "$path"
124
}
125
126
127 8854 aaronmk
#### verbose output
128
129 8973 aaronmk
# usage: (stdout2stderr; cmd...) || return
130
# `|| return` needed on Mac because of bug where -e doesn't apply to ()
131 8914 aaronmk
stdout2stderr () { exec >&2; }
132
133 9006 aaronmk
# set verbosity
134 9005 aaronmk
if isset verbose; then : "${verbosity:=$(bool2int "$verbose")}"; fi
135 9006 aaronmk
: "${verbosity=3}" # default
136
: "${verbosity:=0}" # ensure non-empty
137 9007 aaronmk
declare -i verbosity # ensure integer
138 9000 aaronmk
export verbosity # propagate the verbosity to invoked commands
139 8710 aaronmk
140 8898 aaronmk
can_log () { test "$verbosity" -gt 0; } # verbosity=0 turns off all logging
141 8895 aaronmk
142 8920 aaronmk
: "${log_indent=  }"
143 8919 aaronmk
144 8900 aaronmk
# usage: in func:      inc_log_level; ...
145
#        outside func: inc_log_level; ...; dec_log_level
146 8919 aaronmk
alias inc_log_level='declare verbosity="$verbosity" PS4="$log_indent$PS4"
147 9041 aaronmk
let! verbosity--'
148 8919 aaronmk
alias dec_log_level='declare verbosity="$verbosity" PS4="${PS4#$log_indent}"
149 9041 aaronmk
let! verbosity++'
150 8900 aaronmk
151 8995 aaronmk
fi # load new aliases
152
if self_being_included; then
153
154 8994 aaronmk
# usage: (limit_stderr; cmd...) || return
155 8973 aaronmk
# `|| return` needed on Mac because of bug where -e doesn't apply to ()
156 8995 aaronmk
limit_stderr () { inc_log_level; if ! can_log; then exec 2>/dev/null; fi; }
157 8904 aaronmk
158 8994 aaronmk
limit_stderr_cmd () # usage: [stdout2stderr=1] limit_stderr_cmd cmd...
159 8915 aaronmk
{
160 9010 aaronmk
	case "$1" in echo_run) shift; echo_cmd "$@";; esac
161 8994 aaronmk
	(limit_stderr
162 8915 aaronmk
		if test -n "$stdout2stderr"; then stdout2stderr; fi
163
		"$@"
164 8955 aaronmk
	) || return
165 8915 aaronmk
}
166 9008 aaronmk
alias limit_stderr_cmd='limit_stderr_cmd ' # last space alias-expands next word
167 8911 aaronmk
168 8897 aaronmk
echo_cmd () { if can_log; then echo "$PS4$*" >&2; fi; }
169 8272 aaronmk
170 8278 aaronmk
echo_run () { echo_cmd "$@"; "$@"; }
171
172 8984 aaronmk
# auto-echo common external commands
173
for cmd in rm; do alias "$cmd=echo_run $cmd"; done; unset cmd
174
175 9011 aaronmk
# echo all external commands
176
alias extern="echo_run extern " # last space alias-expands next word
177 8907 aaronmk
178 8929 aaronmk
alias self='extern "$FUNCNAME"' # usage: wrapper () { self ...; }
179
180 8908 aaronmk
# commands that are always external
181
for cmd in env; do alias "$cmd=extern $cmd"; done; unset cmd
182 8872 aaronmk
183 8909 aaronmk
function echo_func ()
184 8463 aaronmk
{
185 8901 aaronmk
	inc_log_level
186 8647 aaronmk
	local script="$(canon_rel_path "${BASH_SOURCE[1]}")"
187
	echo_cmd "$script:${BASH_LINENO[0]}" "${FUNCNAME[1]}" "$@"
188 8463 aaronmk
}
189 8885 aaronmk
190 8997 aaronmk
# usage: cmd1 | { pipe_delay; cmd2; }
191
alias pipe_delay='sleep 0.1' # s; display after leading output of cmd1
192
193
fi # load new aliases
194
if self_being_included; then
195
196 8702 aaronmk
echo_stdin () # usage: input|echo_stdin|cmd
197
{
198 8901 aaronmk
	inc_log_level
199 8897 aaronmk
	if can_log; then
200 8998 aaronmk
		pipe_delay
201 8897 aaronmk
		echo ----- >&2
202
		tee -a /dev/stderr;
203
		echo ----- >&2
204
	else cat
205
	fi
206 8702 aaronmk
}
207 8275 aaronmk
208 8856 aaronmk
echo_vars () # usage: echo_vars var...
209 8901 aaronmk
{
210 8922 aaronmk
	inc_log_level; inc_log_level
211 8921 aaronmk
	if can_log; then
212
		local var
213
		for var in "${@%%=*}"; do { echo -n "$PS4"; declare -p "$var";} >&2;done
214
	fi
215 8901 aaronmk
}
216 8641 aaronmk
217 9019 aaronmk
echo_export () { builtin export "$@"; echo_vars "$@"; }
218 8641 aaronmk
219 8713 aaronmk
if test "$verbosity" -ge 2; then
220 8711 aaronmk
	alias export="echo_export" # automatically echo env vars when they are set
221
fi
222 8642 aaronmk
223 8272 aaronmk
usage () { echo "Usage: $1" >&2; (exit 2); }
224
225 8873 aaronmk
fi # load new aliases
226
if self_being_included; then
227
228 9017 aaronmk
229 8871 aaronmk
#### strings
230
231
sed_ere_flag="$(test "$(uname)" = Darwin && echo E || echo r)"
232
233 8930 aaronmk
sed () { self -"$sed_ere_flag" "$@";}
234 8871 aaronmk
235 9017 aaronmk
236 8854 aaronmk
#### vars
237
238
set_var () { eval "$1"'="$2"'; }
239
240 8857 aaronmk
set_inv () { set_var no_"$1" "$(test -n "${!1}" || echo 1)"; }
241
242 8859 aaronmk
# usage: local var=...; local_inv
243 8888 aaronmk
alias local_inv='declare "no_$var=$(test -n "${!var}" || echo 1)"'
244 8859 aaronmk
245 8863 aaronmk
# usage: local prefix=..._; import_vars
246
alias import_vars="$(cat <<'EOF'
247
: "${prefix:?}"
248
local src_var dest_var
249
for src_var in $(eval echo '${!'$prefix'*}'); do
250
	dest_var="${src_var#$prefix}"
251
	local "$dest_var=${!src_var}"; echo_vars "$dest_var"
252
done
253
EOF
254
)"
255
256 9017 aaronmk
257 8854 aaronmk
#### commands
258
259 8934 aaronmk
top_script="$0" # outermost script
260
top_dir="$(dirname "$top_script")"
261 8272 aaronmk
262 8986 aaronmk
# auto-removes a command's output file on error (like make's .DELETE_ON_ERROR)
263
function to_file () # usage: stdout=... to_file cmd...
264 9035 aaronmk
{ : "${stdout?}"; "$@" >"$stdout" || { save_e; log_e; rm "$stdout"; rethrow;}; }
265 8986 aaronmk
alias to_file='to_file ' # last space alias-expands next word
266
267 8465 aaronmk
run_args_cmd () # runs the command line args command
268 8272 aaronmk
{
269 8971 aaronmk
	test $? -eq 0 || return
270 8693 aaronmk
	eval set -- "$(reverse "${BASH_ARGV[@]}")"
271 8971 aaronmk
	test $# -ge 1 || set -- all
272 8648 aaronmk
	echo_cmd "$(canon_rel_path "$0")" "$@"; "$@"
273 8272 aaronmk
}
274
275 8284 aaronmk
fwd () # usage: subdirs=(...); fwd "$FUNCNAME" "$@"
276 8272 aaronmk
{
277 8881 aaronmk
	echo_func
278 8284 aaronmk
	: "${subdirs?}"
279
280 8272 aaronmk
	for subdir in "${subdirs[@]}"; do
281 8280 aaronmk
		"$(dirname "${BASH_SOURCE[1]}")"/"$subdir"/run "$@"
282 8272 aaronmk
	done
283
}
284
285 9017 aaronmk
286 8966 aaronmk
#### URLs
287
288
localize_url () { test _"$1" = _"$(hostname -f)" || echo "$1"; }
289
290 8704 aaronmk
fi