]> git.ipfire.org Git - people/arne_f/network.git/blobdiff - functions.util
network: STP: Make protocol version configureable.
[people/arne_f/network.git] / functions.util
index 3023154a8140ce7cebb85de0182df2f746627876..8be825ee90db01de29afb9a2eadeba767f3192ea 100644 (file)
@@ -21,7 +21,7 @@
 
 # Print a pretty error message
 function error() {
-       echo -e " ${FAIL}ERROR${NORMAL}  : $@" >&2
+       echo -e " ${COLOUR_ERROR}ERROR${COLOUR_NORMAL}  : $@" >&2
 }
 
 function error_log() {
@@ -31,7 +31,7 @@ function error_log() {
 
 # Print a pretty warn message
 function warning() {
-       echo -e " ${WARN}WARNING${NORMAL}: $@" >&2
+       echo -e " ${COLOUR_WARN}WARNING${COLOUR_NORMAL}: $@" >&2
 }
 
 function warning_log() {
@@ -39,11 +39,55 @@ function warning_log() {
        log WARNING "$@"
 }
 
+# This function does not exist because we cannot use /usr/bin/sort.
+# It implements some kind of bubble sort which is generally very slow
+# but we only have to sort very small data.
 function listsort() {
+       local list=($@)
+       local list_prev
+
+       local i
+       local j
+       local var
+       while [ "${list[*]}" != "${list_prev}" ]; do
+               list_prev="${list[*]}"
+               for j in $(seq 1 ${#list[*]}); do
+                       [ ${j} -ge ${#list[*]} ] && continue
+                       i=$(( ${j} - 1 ))
+                       if [[ "${list[${j}]}" < "${list[${i}]}" ]]; then
+                               var="${list[${i}]}"
+                               list[${i}]="${list[${j}]}"
+                               list[${j}]="${var}"
+                       fi
+               done
+       done
+
+       echo "${list[*]}"
+}
+
+function listmatch() {
+       local match=${1}
+       shift
+
+       assert isset match
+
+       local i
+       for i in $@; do
+               [ "${match}" = "${i}" ] && return ${EXIT_OK}
+       done
+
+       return ${EXIT_ERROR}
+}
+
+function listlength() {
+       local length=0
+
        local i
        for i in $@; do
-               echo "${i}"
-       done | sort | tr "\n" " "
+               length=$(( ${length} + 1 ))
+       done
+
+       echo "${length}"
 }
 
 function config_read() {
@@ -155,6 +199,25 @@ function mac_generate() {
        echo ${output}
 }
 
+function mac_format() {
+       local mac=${1}
+
+       local output
+
+       if [ "${#mac}" = "12" ]; then
+               # Add colons (:) to mac address
+               output=${mac:0:2}
+               local i
+               for i in 2 4 6 8 10; do
+                       output="${output}:${mac:${i}:2}"
+               done
+       fi
+
+       assert mac_is_valid ${output}
+
+       echo "${output}"
+}
+
 function mac_is_valid() {
        local mac=${1}
 
@@ -162,7 +225,7 @@ function mac_is_valid() {
 }
 
 function uuid() {
-       cat /proc/sys/kernel/random/uuid
+       echo $(</proc/sys/kernel/random/uuid)
 }
 
 function isset() {
@@ -171,6 +234,7 @@ function isset() {
        [ -n "${!var}" ]
 }
 
+# XXX Nearly same as listmatch
 function isoneof() {
        local var=${!1}
        shift
@@ -200,13 +264,125 @@ function ismac() {
        mac_is_valid ${mac}
 }
 
+function backtrace() {
+       local start=1
+
+       echo # Empty line
+       error_log "Backtrace (most recent call in first line):"
+
+       local i
+       for i in $(seq ${start} ${#BASH_SOURCE[*]}); do
+               [ -z "${FUNCNAME[${i}]}" ] && continue
+               [ "${FUNCNAME[${i}]}" == "main" ] && continue
+
+               error_log "  $(printf "%20s" "'${FUNCNAME[${i}]}'") called from ${BASH_SOURCE[$(( ${i} + 1 ))]}:${BASH_LINENO[${i}]}"
+       done
+}
+
 function assert() {
        local assertion="$@"
 
        if ! ${assertion}; then
                error_log "Assertion '${assertion}' failed."
+               backtrace
                exit ${EXIT_ERROR}
        fi
 
        return ${EXIT_OK}
 }
+
+function exec_cmd() {
+       local cmd=$@
+
+       log DEBUG "Running command: ${cmd}"
+
+       ${SHELL} ${cmd}
+       local ret=$?
+
+       #log DEBUG "Returned with code '${ret}'"
+
+       if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then
+               error_log "Stopping parent process due to assertion error in child process: ${cmd}"
+               exit ${EXIT_ERROR_ASSERT}
+       fi
+
+       return ${ret}
+}
+
+function uppercase() {
+       local input
+       read input
+       echo "${input^^}"
+}
+
+function lowercase() {
+       local input
+       read input
+       echo "${input,,}"
+}
+
+function seq() {
+       if [ $# -eq 2 ]; then
+               eval echo {${1}..${2}}
+       elif [ $# -eq 3 ]; then
+               eval echo {${1}..${3}..${2}}
+       fi
+}
+
+function beautify_time() {
+       local value=${1}
+
+       local unit
+       local limit
+       for unit in s m h d w; do
+               case "${unit}" in
+                       s|m|h)
+                               limit=60
+                               ;;
+                       d)
+                               limit=24
+                               ;;
+                       w)
+                               limit=7
+                               ;;
+               esac
+
+               [ ${value} -lt ${limit} ] && break
+
+               value=$(( ${value} / ${limit} ))
+       done
+
+       echo "${value}${unit}"
+}
+
+function beautify_bytes() {
+       local value=${1}
+
+       local unit
+       local limit=1024
+       for unit in B k M G T; do
+               [ ${value} -lt ${limit} ] && break
+               value=$(( ${value} / ${limit} ))
+       done
+
+       echo "${value}${unit}"
+}
+
+function module_load() {
+       local module=${1}
+
+       if ! grep -q "^${module}" /proc/modules; then
+               log DEBUG "Loading module '${module}'."
+               modprobe ${module}
+       fi
+}
+
+function binary_exists() {
+       local binary=${1}
+
+       if [ -n "$(type -p ${binary})" ]; then
+               return ${EXIT_OK}
+       fi
+
+       return ${EXIT_ERROR}
+}