Revision 9233
Added by Aaron Marcuse-Kubitza over 11 years ago
lib/sh/util.sh | ||
---|---|---|
230 | 230 |
die() { save_e; params type; "log_${type:-err}" "$1"; rethrow; } |
231 | 231 |
|
232 | 232 |
|
233 |
#### functions |
|
234 |
|
|
235 |
func_exists() { declare -f "$1" >/dev/null; } |
|
236 |
|
|
237 |
copy_func() # usage: from=... to=... copy_func |
|
238 |
# $to must not exist. to get around the no-clobber restriction, use `unset -f`. |
|
239 |
{ |
|
240 |
: "${from:?}" "${to:?}" |
|
241 |
func_exists "$from" || die "function does not exist: $from" |
|
242 |
! func_exists "$to" || die "function already exists: $to" |
|
243 |
local from_def="$(declare -f "$from")" |
|
244 |
eval "$to${from_def#$from}" |
|
245 |
} |
|
246 |
|
|
247 |
func_override() # usage: func_override old_name__suffix |
|
248 |
{ from="${1%%__*}" to="$1" copy_func; } |
|
249 |
|
|
250 |
ensure_nested_func() # usage: func__nested_func() { ensure_nested_func; ... } |
|
251 |
{ |
|
252 |
local nested_func="${FUNCNAME[1]}" |
|
253 |
local func="${nested_func%%__*}" |
|
254 |
contains "$func" "${FUNCNAME[@]}" || \ |
|
255 |
die "$nested_func() must be used by $func()" |
|
256 |
} |
|
257 |
|
|
258 |
|
|
233 | 259 |
### command echoing |
234 | 260 |
|
235 | 261 |
alias echo_params='log "$*"' |
... | ... | |
383 | 409 |
if test "$explicit_errors_only"; then disable_logging; fi |
384 | 410 |
|
385 | 411 |
|
386 |
#### functions |
|
387 |
|
|
388 |
func_exists() { declare -f "$1" >/dev/null; } |
|
389 |
|
|
390 |
copy_func() # usage: from=... to=... copy_func |
|
391 |
# $to must not exist. to get around the no-clobber restriction, use `unset -f`. |
|
392 |
{ |
|
393 |
: "${from:?}" "${to:?}" |
|
394 |
func_exists "$from" || die "function does not exist: $from" |
|
395 |
! func_exists "$to" || die "function already exists: $to" |
|
396 |
local from_def="$(declare -f "$from")" |
|
397 |
eval "$to${from_def#$from}" |
|
398 |
} |
|
399 |
|
|
400 |
func_override() # usage: func_override old_name__suffix |
|
401 |
{ from="${1%%__*}" to="$1" copy_func; } |
|
402 |
|
|
403 |
ensure_nested_func() # usage: func__nested_func() { ensure_nested_func; ... } |
|
404 |
{ |
|
405 |
local nested_func="${FUNCNAME[1]}" |
|
406 |
local func="${nested_func%%__*}" |
|
407 |
contains "$func" "${FUNCNAME[@]}" || \ |
|
408 |
die "$nested_func() must be used by $func()" |
|
409 |
} |
|
410 |
|
|
411 |
|
|
412 | 412 |
#### commands |
413 | 413 |
|
414 | 414 |
top_script="$(canon_rel_path "$0")" # outermost script |
Also available in: Unified diff
lib/sh/util.sh: moved functions section right after exceptions, so that other sections can use it. in particular, func_override is needed by verbose output to override non-verbose versions of functions.