]> git.ipfire.org Git - people/ms/network.git/blobdiff - functions.util
Merge branch 'master' of ssh://git.ipfire.org/pub/git/network
[people/ms/network.git] / functions.util
index 6b9daf3530a775c64bc59e608309b053d5188f26..1fb1951529c5eb22c306aac60b264ed52d940afa 100644 (file)
 function print() {
        local fmt=${1}; shift
 
-       printf "${fmt}\n" "$@"
+       printf -- "${fmt}\n" "$@"
+}
+
+# The args() function takes a number of arguments like
+#   var1="abc d" var2="abc" var3="abcd e"
+# and splits them into several arguments, devided by newline
+function args() {
+       echo "$@" | xargs printf "%s\n"
+}
+
+function unquote() {
+       local var="$@"
+
+       if [ "${var:0:1}" = "\"" ]; then
+               var=${var:1}
+       fi
+
+       local last=$(( ${#var} - 1 ))
+       if [ ${last} -ge 0 ] && [ "${var:${last}:1}" = "\"" ]; then
+               var=${var:0:${last}}
+       fi
+
+       print "${var}"
+}
+
+function quote() {
+       print "\"%s\"" "$@"
+}
+
+function strip() {
+       local value="$@"
+
+       # remove leading whitespace characters
+       value="${value#"${value%%[![:space:]]*}"}"
+
+       # remove trailing whitespace characters
+       value="${value%"${value##*[![:space:]]}"}"
+
+       print "${value}"
 }
 
 # Print a pretty error message
 function error() {
-       echo -e " ${COLOUR_ERROR}ERROR${COLOUR_NORMAL}  : $@" >&2
+       echo -e " ${CLR_RED_B}ERROR${CLR_RESET}  : $@" >&2
 }
 
 function error_log() {
@@ -37,42 +75,25 @@ function error_log() {
 
 # Print a pretty warn message
 function warning() {
-       echo -e " ${COLOUR_WARN}WARNING${COLOUR_NORMAL}: $@" >&2
+       echo -e " ${CLR_YELLOW_B}WARNING${CLR_RESET}: $@" >&2
 }
 
 function warning_log() {
        log WARNING "$@"
 }
 
+# The next three functions are kept for backwards
+# compatibility. The need to be dropped at some time.
 function listsort() {
-       local i
-       for i in $@; do
-               echo "${i}"
-       done | sort | tr '\n' ' '
-       echo
+       list_sort $@
 }
 
 function listmatch() {
-       local match=${1}
-       shift
-
-       local i
-       for i in $@; do
-               [ "${match}" = "${i}" ] && return ${EXIT_OK}
-       done
-
-       return ${EXIT_ERROR}
+       list_match $@
 }
 
 function listlength() {
-       local length=0
-
-       local i
-       for i in $@; do
-               length=$(( ${length} + 1 ))
-       done
-
-       echo "${length}"
+       list_length $@
 }
 
 # Speedup function to avoid a call of the basename binary
@@ -80,10 +101,47 @@ function basename() {
        echo "${1##*/}"
 }
 
+function format() {
+       local key=${1}
+       assert isset key
+
+       local format=${2}
+       assert isset format
+
+       shift 2
+
+       printf -v "${key}" "${format}" "$@"
+}
+
+function assign() {
+       local key=${1}
+       assert isset key
+       shift
+
+       format "${key}" "%s" "$@"
+}
+
+function fread() {
+       local file=${1}
+       assert isset file
+
+       [ -r "${file}" ] || return ${EXIT_ERROR}
+
+       print "$(<${file})"
+}
+
+function fwrite() {
+       local file=${1}
+       assert isset file
+       shift
+
+       print "%s" "$@" >> ${file}
+}
+
 function enabled() {
        local param=${1}
 
-       listmatch "${!param}" yes on true 1
+       list_match "${!param}" yes on true 1
 }
 
 function mac_generate() {
@@ -123,9 +181,13 @@ function mac_generate() {
 
 function mac_format() {
        local mac=${1}
+       assert isset mac
 
-       local output
+       # Remove all colons and make the rest lowercase.
+       mac=${mac//:/}
+       mac=${mac,,}
 
+       local output
        if [ "${#mac}" = "12" ]; then
                # Add colons (:) to mac address
                output=${mac:0:2}
@@ -133,11 +195,13 @@ function mac_format() {
                for i in 2 4 6 8 10; do
                        output="${output}:${mac:${i}:2}"
                done
+       else
+               output=${mac}
        fi
 
        assert mac_is_valid ${output}
 
-       echo "${output}"
+       print "${output}"
 }
 
 function mac_is_valid() {
@@ -160,7 +224,7 @@ function isoneof() {
        local var=${!1}
        shift
 
-       listmatch "${var}" "$@"
+       list_match "${var}" "$@"
 }
 
 function isbool() {
@@ -181,6 +245,12 @@ function ismac() {
        mac_is_valid ${mac}
 }
 
+function isipaddress() {
+       local addr=${!1}
+
+       ip_is_valid ${addr}
+}
+
 function backtrace() {
        local start=1
 
@@ -209,6 +279,18 @@ function assert() {
        return ${EXIT_OK}
 }
 
+# This function checks, if the given argument is an assert error
+# exit code. If this is the case, the script will halt immediately.
+function assert_check_retval() {
+       local ret=${1}
+
+       if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then
+               exit ${EXIT_ERROR_ASSERT}
+       fi
+
+       return ${ret}
+}
+
 function exec_cmd() {
        local cmd=$@
 
@@ -247,6 +329,17 @@ function cmd_quiet() {
        cmd $@ &>/dev/null
 }
 
+function cmd_exec() {
+       local cmd=$@
+
+       log DEBUG "Exec'ing command: ${cmd}"
+
+       exec ${cmd}
+
+       log ERROR "Could not exec-ute: ${cmd}"
+       exit ${EXIT_ERROR}
+}
+
 function seq() {
        if [ $# -eq 2 ]; then
                eval echo {${1}..${2}}
@@ -255,6 +348,15 @@ function seq() {
        fi
 }
 
+function which() {
+       type -P $@
+}
+
+# Prints the number of seconds since epoch.
+function timestamp() {
+       date -u "+%s"
+}
+
 function beautify_time() {
        local value=${1}
 
@@ -346,3 +448,16 @@ function network_is_running() {
        # Check, if the network service is running.
        service_is_active network
 }
+
+function contains_spaces() {
+       local var="$@"
+
+       # Eliminate spaces.
+       local var2=${var// /}
+
+       if [ ${#var} -ne ${#var2} ]; then
+               return ${EXIT_TRUE}
+       fi
+
+       return ${EXIT_FALSE}
+}