Revision 13265
Added by Aaron Marcuse-Kubitza almost 11 years ago
trunk/lib/sh/util.sh | ||
---|---|---|
247 | 247 |
if self_being_included; then |
248 | 248 |
|
249 | 249 |
|
250 |
#### functions |
|
251 |
|
|
252 |
func_exists() { declare -f "$1" >/dev/null; } |
|
253 |
|
|
254 |
kw_params() # usage: func() { kw_params param_var...; }; ...; param_var=... cmd |
|
255 |
# removes keyword-param-only vars from the environment |
|
256 |
{ unexport "$@"; } |
|
257 |
|
|
258 |
# usage: cmd=... foreach_arg |
|
259 |
function foreach_arg() |
|
260 |
{ |
|
261 |
echo_func; kw_params cmd; : "${cmd:?}" |
|
262 |
local a; for a in "$@"; do |
|
263 |
a="$(log++ echo_run "$cmd" "$a")" || return; args+=("$a") |
|
264 |
done |
|
265 |
echo_vars args |
|
266 |
} |
|
267 |
alias foreach_arg='"foreach_arg" "$@"; set -- "${args[@]}"; unset args' |
|
268 |
|
|
269 |
alias self_name='echo "${FUNCNAME%%__*}"' # usage: "$(self_name)" |
|
270 |
|
|
271 |
alias self='command "$(self_name)"' # usage: wrapper() { self ...; } |
|
272 |
alias self_sys='sys_cmd "$(self_name)"' # wrapper() { self_sys ...; } |
|
273 |
alias self_builtin='builtin "${FUNCNAME%%__*}"' #wrapper() { self_builtin ...; } |
|
274 |
|
|
275 |
all_funcs() # usage: for func in $(all_funcs); do ...; done # all declared funcs |
|
276 |
{ declare -F|while read -r line; do echo -n "${line#declare -f } "; done; } |
|
277 |
|
|
278 |
copy_func() # usage: from=... to=... copy_func |
|
279 |
# $to must not exist. to get around the no-clobber restriction, use `unset -f`. |
|
280 |
{ |
|
281 |
: "${from:?}" "${to:?}" |
|
282 |
func_exists "$from" || die "function does not exist: $from" |
|
283 |
! func_exists "$to" || die "function already exists: $to" |
|
284 |
local from_def="$(declare -f "$from")" |
|
285 |
eval "$to${from_def#$from}" |
|
286 |
} |
|
287 |
|
|
288 |
func_override() # usage: func_override old_name__suffix |
|
289 |
{ from="${1%__*}" to="$1" copy_func; } |
|
290 |
|
|
291 |
ensure_nested_func() # usage: func__nested_func() { ensure_nested_func; ... } |
|
292 |
{ |
|
293 |
local nested_func="${FUNCNAME[1]}" |
|
294 |
local func="${nested_func%%__*}" |
|
295 |
contains "$func" "${FUNCNAME[@]}" || \ |
|
296 |
die "$nested_func() must be used by $func()" |
|
297 |
} |
|
298 |
|
|
299 |
|
|
250 | 300 |
#### aliases |
251 | 301 |
|
252 | 302 |
unalias() { builtin unalias "$@" 2>&- || true; } # no error if undefined |
... | ... | |
304 | 354 |
alias sudo='"sudo" ' # last space alias-expands next word |
305 | 355 |
|
306 | 356 |
|
307 |
#### functions |
|
308 |
|
|
309 |
func_exists() { declare -f "$1" >/dev/null; } |
|
310 |
|
|
311 |
kw_params() # usage: func() { kw_params param_var...; }; ...; param_var=... cmd |
|
312 |
# removes keyword-param-only vars from the environment |
|
313 |
{ unexport "$@"; } |
|
314 |
|
|
315 |
# usage: cmd=... foreach_arg |
|
316 |
function foreach_arg() |
|
317 |
{ |
|
318 |
echo_func; kw_params cmd; : "${cmd:?}" |
|
319 |
local a; for a in "$@"; do |
|
320 |
a="$(log++ echo_run "$cmd" "$a")" || return; args+=("$a") |
|
321 |
done |
|
322 |
echo_vars args |
|
323 |
} |
|
324 |
alias foreach_arg='"foreach_arg" "$@"; set -- "${args[@]}"; unset args' |
|
325 |
|
|
326 |
alias self_name='echo "${FUNCNAME%%__*}"' # usage: "$(self_name)" |
|
327 |
|
|
328 |
alias self='command "$(self_name)"' # usage: wrapper() { self ...; } |
|
329 |
alias self_sys='sys_cmd "$(self_name)"' # wrapper() { self_sys ...; } |
|
330 |
alias self_builtin='builtin "${FUNCNAME%%__*}"' #wrapper() { self_builtin ...; } |
|
331 |
|
|
332 |
all_funcs() # usage: for func in $(all_funcs); do ...; done # all declared funcs |
|
333 |
{ declare -F|while read -r line; do echo -n "${line#declare -f } "; done; } |
|
334 |
|
|
335 |
copy_func() # usage: from=... to=... copy_func |
|
336 |
# $to must not exist. to get around the no-clobber restriction, use `unset -f`. |
|
337 |
{ |
|
338 |
: "${from:?}" "${to:?}" |
|
339 |
func_exists "$from" || die "function does not exist: $from" |
|
340 |
! func_exists "$to" || die "function already exists: $to" |
|
341 |
local from_def="$(declare -f "$from")" |
|
342 |
eval "$to${from_def#$from}" |
|
343 |
} |
|
344 |
|
|
345 |
func_override() # usage: func_override old_name__suffix |
|
346 |
{ from="${1%__*}" to="$1" copy_func; } |
|
347 |
|
|
348 |
ensure_nested_func() # usage: func__nested_func() { ensure_nested_func; ... } |
|
349 |
{ |
|
350 |
local nested_func="${FUNCNAME[1]}" |
|
351 |
local func="${nested_func%%__*}" |
|
352 |
contains "$func" "${FUNCNAME[@]}" || \ |
|
353 |
die "$nested_func() must be used by $func()" |
|
354 |
} |
|
355 |
|
|
356 |
|
|
357 | 357 |
#### exceptions |
358 | 358 |
|
359 | 359 |
fi # load new aliases |
Also available in: Unified diff
lib/sh/util.sh: functions: moved before commands since commands are more complex