From df8eeba2fb5b0dc460b5f6aac5dafc380e17242e Mon Sep 17 00:00:00 2001 From: Roy Marples Date: Mon, 21 Apr 2008 17:32:17 +0000 Subject: [PATCH] Punt is_function in favour of POSIX shell builtin command, thanks to Harald van Dijk for the idea. --- sh/functions.sh.in | 9 --------- sh/net.sh.in | 30 +++++++++++++++--------------- sh/runscript.sh.in | 10 +++++----- 3 files changed, 20 insertions(+), 29 deletions(-) diff --git a/sh/functions.sh.in b/sh/functions.sh.in index 8b7356aa..57917ed3 100644 --- a/sh/functions.sh.in +++ b/sh/functions.sh.in @@ -20,15 +20,6 @@ eoutdent() return 0 } -is_function() -{ - if [ -n "${BASH_VERSION}" ]; then - [ "$(type -t "$1")" = "function" ] - else - [ "$(type "$1" 2>/dev/null)" = "$1 is a shell function" ] - fi -} - yesno() { [ -z "$1" ] && return 1 diff --git a/sh/net.sh.in b/sh/net.sh.in index 2e913583..3f42f9ca 100644 --- a/sh/net.sh.in +++ b/sh/net.sh.in @@ -28,7 +28,7 @@ depend() *) after net.lo net.lo0;; esac - if is_function depend_${IFVAR}; then + if [ "$(command -v "depend_${IFVAR}")" = "depend_${IFVAR}" ]; then depend_${IFVAR} fi @@ -344,7 +344,7 @@ _load_modules() # Wrap our provides local f= for f in pre_start start post_start; do - eval "${provides}_${f}() { is_function ${mod}_${f} || return 0; ${mod}_${f} \"\$@\"; }" + eval "${provides}_${f}() { [ "$(command -v "${mod}_${f}")" = "${mod}_${f}" ] || return 0; ${mod}_${f} \"\$@\"; }" done eval module_${mod}_provides="${provides}" @@ -358,7 +358,7 @@ _load_modules() eval x=\$module_${mod}_provides [ -z "${x}" ] && continue for f in pre_start start post_start; do - eval "${x}_${f}() { is_function ${mod}_${f} || return 0; ${mod}_${f} \"\$@\"; }" + eval "${x}_${f}() { [ "$(command -v "${mod}_${f}")" = "${mod}_${f}" ] || return 0; ${mod}_${f} \"\$@\"; }" done eval module_${x}_providedby="${mod}" ;; @@ -462,7 +462,7 @@ start() # We up the iface twice if we have a preup to ensure it's up if # available in preup and afterwards incase the user inadvertently # brings it down - if is_function preup; then + if [ "$(command -v preup)" = "preup" ]; then _up 2>/dev/null ebegin "Running preup" eindent @@ -473,7 +473,7 @@ start() _up 2>/dev/null for module in ${MODULES}; do - if is_function "${module}_pre_start"; then + if [ "$(command -v "${module}_pre_start")" = "${module}_pre_start"]; then ${module}_pre_start || exit $? fi done @@ -522,7 +522,7 @@ start() null) :;; [0-9]*|*:*) _add_address ${config};; *) - if is_function "${config}_start"; then + if [ "$(command -v "${config}_start")" = "${config}_start" ]; then "${config}"_start else eerror "nothing provides \`${config}'" @@ -547,7 +547,7 @@ start() done if ! ${oneworked}; then - if is_function failup; then + if [ "$(command -v failup)" = "failup" ]; then ebegin "Running failup" eindent failup @@ -600,12 +600,12 @@ ${routes}" fi for module in ${MODULES}; do - if is_function "${module}_post_start"; then + if [ "$(command -v "${module}_post_start")" = "${module}_post_start" ]; then ${module}_post_start || exit $? fi done - if is_function postup; then + if [ "$(command -v postup)" = "postup" ]; then ebegin "Running postup" eindent postup @@ -628,7 +628,7 @@ stop() _load_modules false fi - if is_function predown; then + if [ "$(command -v predown)" = "predown" ]; then ebegin "Running predown" eindent predown || return 1 @@ -641,24 +641,24 @@ stop() fi for module in ${MODULES}; do - if is_function "${module}_pre_stop"; then + if [ "$(command -v "${module}_pre_stop")" = "${module}_pre_stop" ]; then ${module}_pre_stop || exit $? fi done for module in ${MODULES}; do - if is_function "${module}_stop"; then + if [ "$(command -v "${module}_stop")" = "${module}_stop" ]; then ${module}_stop fi done # Only delete addresses for non PPP interfaces - if ! is_function is_ppp || ! is_ppp; then + if ! [ "$(command -v is_ppp)" = "is_ppp" ] || ! is_ppp; then _delete_addresses "${IFACE}" fi for module in ${MODULES}; do - if is_function "${module}_post_stop"; then + if [ "$(command -v "${module}_post_stop")" = "${module}_post_stop" ]; then ${module}_post_stop fi done @@ -669,7 +669,7 @@ stop() type resolvconf >/dev/null 2>&1 && resolvconf -d "${IFACE}" 2>/dev/null - if is_function postdown; then + if [ "$(command -v "postdown")" = "postdown" ]; then ebegin "Running postdown" eindent postdown diff --git a/sh/runscript.sh.in b/sh/runscript.sh.in index a45e41d1..33804bc2 100644 --- a/sh/runscript.sh.in +++ b/sh/runscript.sh.in @@ -94,7 +94,7 @@ unset _f # If we have a default command then supply a default start function if [ -n "${command}" ]; then - if ! is_function start; then + if ! [ "$(command -v start)" != "start" ]; then start() { local _background= ebegin "Starting ${name:-${RC_SVCNAME}}" @@ -126,7 +126,7 @@ fi # If we have a default command, procname or pidfile then supply a default stop # function if [ -n "${command}" -o -n "${procname}" -o -n "${pidfile}" ]; then - if ! is_function stop; then + if ! [ "$(command -v stop)" != "stop" ]; then stop() { ebegin "Stopping ${name:-${RC_SVCNAME}}" start-stop-daemon --stop \ @@ -143,7 +143,7 @@ while [ -n "$1" ]; do for _cmd in describe start stop ${extra_commands:-${opts}} \ ${extra_started_commands}; do if [ "${_cmd}" = "$1" ]; then - if is_function "$1"; then + if [ "$(command -v "$1")" = "$1" ]; then # If we're in the background, we may wish to # fake some commands. We do this so we can # "start" ourselves from inactive which then @@ -168,11 +168,11 @@ while [ -n "$1" ]; do fi done unset _cmd - if is_function "$1"_pre >/dev/null 2>&1; then + if [ "$(command -v "$1_pre")" = "$1_pre" ]; then "$1"_pre || exit $? fi "$1" || exit $? - if is_function "$1"_post >/dev/null 2>&1; then + if [ "$(command -v "$1_post")" = "$1_post" ]; then "$1"_post || exit $? fi shift