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 8976 aaronmk
alias rethrow='return "$e"'
49
alias rethrow_subshell='exit "$e"'
50
51 8978 aaronmk
fi # load new aliases
52
if self_being_included; then
53
54 9021 aaronmk
# usage: save_e cmd... || { use $e (or $?); rethrow; }
55 8978 aaronmk
function save_e () { "$@"; e=$?; rethrow; } # rethrow because e= clears $?
56
alias save_e='declare e; save_e ' # last space alias-expands next word
57
58 8981 aaronmk
function log_e () # usage: log_e cmd... [ || use $e (or $?) ]
59
{
60 8982 aaronmk
	if ! "save_e" "$@"; then
61 8981 aaronmk
		echo "! $*" >&2
62
		echo "! command exited with error $e" >&2
63
		rethrow
64
	fi
65
}
66 8982 aaronmk
alias log_e='declare e; log_e ' # last space alias-expands next word
67 8981 aaronmk
68 8886 aaronmk
# usage: try cmd...; ignore status; if catch status; then ...; fi; end_try
69
70 8980 aaronmk
function try () { "save_e" "$@" || true; }
71
alias try='declare e; try ' # last space alias-expands next word
72 8886 aaronmk
73
catch () { test "$e" -eq "$1"; e=0; }
74
75
ignore () { catch "$@" || true; }
76
77 8977 aaronmk
alias end_try='rethrow'
78
alias end_try_subshell='rethrow_subshell'
79 8886 aaronmk
80 9020 aaronmk
fi # load new aliases
81
if self_being_included; then
82 9017 aaronmk
83 9020 aaronmk
84 8899 aaronmk
#### integers
85
86
let () { builtin let "$@" || true; }
87
	# "If the last ARG evaluates to 0, let returns 1" (`help let`)
88
89 9004 aaronmk
bool2int () { save_e test -z "$1"; echo "$e"; } # empty->0; non-empty->1
90
91 9017 aaronmk
92 8854 aaronmk
#### arrays
93 8694 aaronmk
94 8833 aaronmk
join () { local IFS="$delim"; echo "$*"; } # usage: delim=... join elems...
95 8816 aaronmk
96 8691 aaronmk
reverse () # usage: array=($(reverse args...))
97
{
98
	local i
99
	for (( i=$#; i >= 1; i-- )); do printf '%q ' "${!i}"; done
100
}
101
102 9017 aaronmk
103 8854 aaronmk
#### verbose output
104
105 8973 aaronmk
# usage: (stdout2stderr; cmd...) || return
106
# `|| return` needed on Mac because of bug where -e doesn't apply to ()
107 8914 aaronmk
stdout2stderr () { exec >&2; }
108
109 9006 aaronmk
# set verbosity
110 9005 aaronmk
if isset verbose; then : "${verbosity:=$(bool2int "$verbose")}"; fi
111 9006 aaronmk
: "${verbosity=3}" # default
112
: "${verbosity:=0}" # ensure non-empty
113 9007 aaronmk
declare -i verbosity # ensure integer
114 9000 aaronmk
export verbosity # propagate the verbosity to invoked commands
115 8710 aaronmk
116 8898 aaronmk
can_log () { test "$verbosity" -gt 0; } # verbosity=0 turns off all logging
117 8895 aaronmk
118 8920 aaronmk
: "${log_indent=  }"
119 8919 aaronmk
120 8900 aaronmk
# usage: in func:      inc_log_level; ...
121
#        outside func: inc_log_level; ...; dec_log_level
122 8919 aaronmk
alias inc_log_level='declare verbosity="$verbosity" PS4="$log_indent$PS4"
123
let verbosity--'
124
alias dec_log_level='declare verbosity="$verbosity" PS4="${PS4#$log_indent}"
125
let verbosity++'
126 8900 aaronmk
127 8995 aaronmk
fi # load new aliases
128
if self_being_included; then
129
130 8994 aaronmk
# usage: (limit_stderr; cmd...) || return
131 8973 aaronmk
# `|| return` needed on Mac because of bug where -e doesn't apply to ()
132 8995 aaronmk
limit_stderr () { inc_log_level; if ! can_log; then exec 2>/dev/null; fi; }
133 8904 aaronmk
134 8994 aaronmk
limit_stderr_cmd () # usage: [stdout2stderr=1] limit_stderr_cmd cmd...
135 8915 aaronmk
{
136 9010 aaronmk
	case "$1" in echo_run) shift; echo_cmd "$@";; esac
137 8994 aaronmk
	(limit_stderr
138 8915 aaronmk
		if test -n "$stdout2stderr"; then stdout2stderr; fi
139
		"$@"
140 8955 aaronmk
	) || return
141 8915 aaronmk
}
142 9008 aaronmk
alias limit_stderr_cmd='limit_stderr_cmd ' # last space alias-expands next word
143 8911 aaronmk
144 8897 aaronmk
echo_cmd () { if can_log; then echo "$PS4$*" >&2; fi; }
145 8272 aaronmk
146 8278 aaronmk
echo_run () { echo_cmd "$@"; "$@"; }
147
148 8984 aaronmk
# auto-echo common external commands
149
for cmd in rm; do alias "$cmd=echo_run $cmd"; done; unset cmd
150
151 9011 aaronmk
# echo all external commands
152
alias extern="echo_run extern " # last space alias-expands next word
153 8907 aaronmk
154 8929 aaronmk
alias self='extern "$FUNCNAME"' # usage: wrapper () { self ...; }
155
156 8908 aaronmk
# commands that are always external
157
for cmd in env; do alias "$cmd=extern $cmd"; done; unset cmd
158 8872 aaronmk
159 8646 aaronmk
canon_rel_path ()
160
{
161
	local path="$1"
162 8716 aaronmk
	path="$(realpath "$path")" # canonicalize
163 8773 aaronmk
	path="${path#$(pwd -P)/}" # remove any shared prefix with the current dir
164 8646 aaronmk
	echo "$path"
165
}
166
167 8909 aaronmk
function echo_func ()
168 8463 aaronmk
{
169 8901 aaronmk
	inc_log_level
170 8647 aaronmk
	local script="$(canon_rel_path "${BASH_SOURCE[1]}")"
171
	echo_cmd "$script:${BASH_LINENO[0]}" "${FUNCNAME[1]}" "$@"
172 8463 aaronmk
}
173 8885 aaronmk
174 8997 aaronmk
# usage: cmd1 | { pipe_delay; cmd2; }
175
alias pipe_delay='sleep 0.1' # s; display after leading output of cmd1
176
177
fi # load new aliases
178
if self_being_included; then
179
180 8702 aaronmk
echo_stdin () # usage: input|echo_stdin|cmd
181
{
182 8901 aaronmk
	inc_log_level
183 8897 aaronmk
	if can_log; then
184 8998 aaronmk
		pipe_delay
185 8897 aaronmk
		echo ----- >&2
186
		tee -a /dev/stderr;
187
		echo ----- >&2
188
	else cat
189
	fi
190 8702 aaronmk
}
191 8275 aaronmk
192 8856 aaronmk
echo_vars () # usage: echo_vars var...
193 8901 aaronmk
{
194 8922 aaronmk
	inc_log_level; inc_log_level
195 8921 aaronmk
	if can_log; then
196
		local var
197
		for var in "${@%%=*}"; do { echo -n "$PS4"; declare -p "$var";} >&2;done
198
	fi
199 8901 aaronmk
}
200 8641 aaronmk
201 9019 aaronmk
echo_export () { builtin export "$@"; echo_vars "$@"; }
202 8641 aaronmk
203 8713 aaronmk
if test "$verbosity" -ge 2; then
204 8711 aaronmk
	alias export="echo_export" # automatically echo env vars when they are set
205
fi
206 8642 aaronmk
207 8272 aaronmk
usage () { echo "Usage: $1" >&2; (exit 2); }
208
209 8873 aaronmk
fi # load new aliases
210
if self_being_included; then
211
212 9017 aaronmk
213 8871 aaronmk
#### strings
214
215
sed_ere_flag="$(test "$(uname)" = Darwin && echo E || echo r)"
216
217 8930 aaronmk
sed () { self -"$sed_ere_flag" "$@";}
218 8871 aaronmk
219 9017 aaronmk
220 8854 aaronmk
#### vars
221
222
set_var () { eval "$1"'="$2"'; }
223
224 8857 aaronmk
set_inv () { set_var no_"$1" "$(test -n "${!1}" || echo 1)"; }
225
226 8859 aaronmk
# usage: local var=...; local_inv
227 8888 aaronmk
alias local_inv='declare "no_$var=$(test -n "${!var}" || echo 1)"'
228 8859 aaronmk
229 8863 aaronmk
# usage: local prefix=..._; import_vars
230
alias import_vars="$(cat <<'EOF'
231
: "${prefix:?}"
232
local src_var dest_var
233
for src_var in $(eval echo '${!'$prefix'*}'); do
234
	dest_var="${src_var#$prefix}"
235
	local "$dest_var=${!src_var}"; echo_vars "$dest_var"
236
done
237
EOF
238
)"
239
240 9017 aaronmk
241 8854 aaronmk
#### commands
242
243 8934 aaronmk
top_script="$0" # outermost script
244
top_dir="$(dirname "$top_script")"
245 8272 aaronmk
246 8986 aaronmk
# auto-removes a command's output file on error (like make's .DELETE_ON_ERROR)
247
function to_file () # usage: stdout=... to_file cmd...
248
{ : "${stdout?}"; log_e "$@" >"$stdout" || { rm "$stdout"; rethrow; }; }
249
alias to_file='to_file ' # last space alias-expands next word
250
251 8465 aaronmk
run_args_cmd () # runs the command line args command
252 8272 aaronmk
{
253 8971 aaronmk
	test $? -eq 0 || return
254 8693 aaronmk
	eval set -- "$(reverse "${BASH_ARGV[@]}")"
255 8971 aaronmk
	test $# -ge 1 || set -- all
256 8648 aaronmk
	echo_cmd "$(canon_rel_path "$0")" "$@"; "$@"
257 8272 aaronmk
}
258
259 8284 aaronmk
fwd () # usage: subdirs=(...); fwd "$FUNCNAME" "$@"
260 8272 aaronmk
{
261 8881 aaronmk
	echo_func
262 8284 aaronmk
	: "${subdirs?}"
263
264 8272 aaronmk
	for subdir in "${subdirs[@]}"; do
265 8280 aaronmk
		"$(dirname "${BASH_SOURCE[1]}")"/"$subdir"/run "$@"
266 8272 aaronmk
	done
267
}
268
269 9017 aaronmk
270 8966 aaronmk
#### URLs
271
272
localize_url () { test _"$1" = _"$(hostname -f)" || echo "$1"; }
273
274 8704 aaronmk
fi