#!/bin/bash
#  functions - Bash function support

set -e


is_function() {
	has_args 1 "$@"
	declare -pf "$1" >/dev/null 2>&1
}

# func_deprecated: Declare a deprectated function ($1) as an alias
#   to another function.
func_deprecate() {
	has_args 2 "$@"
	local old=$1
	local new=$2
	eval "$old() { warn \"$old: deprecated, use $new\"; $new \"\$@\"; }"
}

func_copy() {
	has_args 2 "$@"
	local src=$1
	local dst=$2
	local scode=$(declare -f $src)
	local dcode="$dst${scode#$src}"
	eval "$dcode"
}

# func_chain - Overrides an existing function with a new body, copying
#  the original function such that it can be called by the new function.
#  e.g. 'func chain a b' installs 'a_b' as 'b'; 'a_b' can use 'a_b_next'
#  to call the original 'b' (which may itself be a chained function).
func_chain() {
	min_args 2 "$@"
	local name=$1
	shift

	local func
	for func in "$@"; do
		func_copy "$func" "${name}_${func}_next"
		func_copy "${name}_${func}" "$func"
	done
}

func_delete() { min_args 1 "$@"; unset -f "$@"; }

func_save() { func_copy "$@"; }
func_restore() { func_copy "$2" "$1"; unset -f "$2"; }

func_trace() {
	local -a args=( "$@" )
	list_quote args
	debug "FUNC: ${FUNCNAME[1]} ${args[*]}"
}

View the Script Reference Index


Generated on Tue Apr 25 21:20:06 PDT 2017 by mcsh i7 v0.18.0.