]> git.ipfire.org Git - people/ms/network.git/blobdiff - src/functions/functions.hook
Remove the function keyword which is a bashism
[people/ms/network.git] / src / functions / functions.hook
index 8146277bbc169f3a8f6c12f4a7118a3de98d150f..88a99ff76ff2000e00060a99d9e11b608255e18a 100644 (file)
 #                                                                             #
 ###############################################################################
 
-HOOK_COMMANDS_CONFIG="hook_create hook_down hook_edit hook_status hook_remove"
-HOOK_COMMANDS_CONFIG="${HOOK_COMMANDS_CONFIG} hook_up"
-
-HOOK_COMMANDS_PORT="hook_new hook_edit hook_create hook_remove hook_up hook_down \
-       hook_hotplug hook_hotplug_rename hook_info hook_status"
-
-HOOK_COMMANDS_ZONE="hook_add hook_create hook_discover hook_down hook_edit hook_help \
-       hook_info hook_remove hook_status hook_up hook_hotplug \
-       \
-       hook_config_create hook_config_edit hook_config_remove hook_config_show \
-       \
-       hook_port hook_port_add hook_port_edit hook_port_remove hook_port_show \
-       hook_port_status hook_port_up hook_port_down \
-       \
-       hook_ppp_ip_pre_up hook_ppp_ipv4_down hook_ppp_ipv4_up \
-       hook_ipv6_down hook_ipv6_up hook_ppp_write_config"
-
-function hook_dir() {
+hook_dir() {
        local type=${1}
 
        if [ -n "${type}" ]; then
@@ -47,7 +30,23 @@ function hook_dir() {
 }
 NETWORK_HOOKS_DIR_ZONES="$(hook_dir zone)"
 
-function hook_exists() {
+hook_list() {
+       local type="${1}"
+       assert isoneof type port zone
+
+       local dir="$(hook_dir "${type}")"
+       assert isset dir
+
+       local hook
+       for hook in ${dir}/*; do
+               hook_exists "${hook}" || continue
+               print "${hook}"
+       done
+
+       return ${EXIT_OK}
+}
+
+hook_exists() {
        local type=${1}
        local hook=${2}
 
@@ -60,7 +59,7 @@ function hook_exists() {
        [ ! -d "${hook}" ] && [ -x "${hook}" ]
 }
 
-function hook_exec() {
+hook_exec() {
        local type="${1}"
        assert isset type
 
@@ -77,19 +76,7 @@ function hook_exec() {
        local hook_cmd="hook_${cmd}"
 
        # Check if the hook action is valid.
-       local valid_commands
-       case "${type}" in
-               "config")
-                       valid_commands="${HOOK_COMMANDS_CONFIG}"
-                       ;;
-               "port")
-                       valid_commands="${HOOK_COMMANDS_PORT}"
-                       ;;
-               "zone")
-                       valid_commands="${HOOK_COMMANDS_ZONE}"
-                       ;;
-       esac
-       isset valid_commands && assert list_match "${hook_cmd}" ${valid_commands}
+       assert hook_valid_command "${type}" "${cmd}"
 
        local hook_path="$(hook_dir ${type})/${hook}"
 
@@ -126,7 +113,7 @@ function hook_exec() {
        return ${ret}
 }
 
-function hook_list() {
+hook_list() {
        local type="${1}"
 
        local dir="$(hook_dir "${type}")"
@@ -143,7 +130,7 @@ function hook_list() {
 }
 
 # The default help function.
-function hook_help() {
+hook_help() {
        # If no man page has been configured, we print an error message.
        if [ -z "${HOOK_MANPAGE}" ]; then
                error "There is no help available for hook '${HOOK}'. Exiting."
@@ -155,7 +142,7 @@ function hook_help() {
        exit $?
 }
 
-function config_get_hook() {
+config_get_hook() {
        local config=${1}
 
        assert isset config
@@ -167,26 +154,135 @@ function config_get_hook() {
        )
 }
 
-function hook_zone_exists() {
+hook_zone_exists() {
        hook_exists zone $@
 }
 
-function hook_zone_exec() {
+hook_zone_exec() {
        hook_exec zone $@
 }
 
-function hook_zone_get_all() {
+hook_zone_get_all() {
        hook_list zone
 }
 
-function hook_config_exists() {
+hook_config_exists() {
        hook_exists config $@
 }
 
-function hook_config_exec() {
+hook_config_exec() {
        hook_exec config $@
 }
 
-function hook_config_get_all() {
+hook_config_get_all() {
        hook_list config
 }
+
+hook_valid_command() {
+       local type="${1}"
+       local cmd="${2}"
+
+       case "${type}" in
+               config)
+                       hook_valid_command_config "${cmd}"
+                       return ${?}
+                       ;;
+               port)
+                       hook_valid_command_port "${cmd}"
+                       return ${?}
+                       ;;
+               zone)
+                       hook_valid_command_zone "${cmd}"
+                       return ${?}
+                       ;;
+       esac
+
+       return ${EXIT_FALSE}
+}
+
+hook_valid_command_config() {
+       local cmd="${1}"
+
+       case "${cmd}" in
+               create|remove|edit|up|down|status)
+                       return ${EXIT_TRUE}
+                       ;;
+       esac
+
+       return ${EXIT_FALSE}
+}
+
+hook_valid_command_port() {
+       local cmd="${1}"
+
+       case "${cmd}" in
+               # Configuration hooks
+               new|edit|destroy)
+                       return ${EXIT_TRUE}
+                       ;;
+
+               # Control hooks
+               create|remove|up|down)
+                       return ${EXIT_TRUE}
+                       ;;
+
+               # Hotplug
+               hotplug|hotplug_rename)
+                       return ${EXIT_TRUE}
+                       ;;
+
+               # Status
+               status|info)
+                       return ${EXIT_TRUE}
+                       ;;
+       esac
+
+       return ${EXIT_FALSE}
+}
+
+hook_valid_command_zone() {
+       local cmd="${1}"
+
+       case "${cmd}" in
+               # Configuration hooks
+               new|edit|destroy)
+                       return ${EXIT_TRUE}
+                       ;;
+
+               config_create|config_edit|config_remove|config_show)
+                       return ${EXIT_TRUE}
+                       ;;
+
+               # Control hooks
+               up|down)
+                       return ${EXIT_TRUE}
+                       ;;
+
+               # Hotplug
+               hotplug)
+                       return ${EXIT_TRUE}
+                       ;;
+
+               # Ports
+               port_attach|port_detach|port_edit|port_create|port_remove|port_status|port_up|port_down)
+                       return ${EXIT_TRUE}
+                       ;;
+
+               # Status
+               status|info|help)
+                       return ${EXIT_TRUE}
+                       ;;
+
+               # Discovery
+               discover)
+                       return ${EXIT_TRUE}
+                       ;;
+
+               # PPP
+               ppp_ip_pre_up|ppp_ipv[64]_up|ppp_ipv[64]_down|ppp_write_config)
+                       return ${EXIT_TRUE}
+                       ;;
+       esac
+
+       return ${EXIT_FALSE}
+}