]> git.ipfire.org Git - people/stevee/network.git/blobdiff - functions.util
wireless: Add function to find DFS channels.
[people/stevee/network.git] / functions.util
index 96b83b5156dc0a0c24b7d1c56733f511cedf8501..79ec87db7e579fa078f0f37f8443332ee4feec19 100644 (file)
@@ -33,6 +33,37 @@ 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 " ${CLR_RED_B}ERROR${CLR_RESET}  : $@" >&2
@@ -70,12 +101,24 @@ 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
 
-       printf -v "${key}" "%s" "$@"
+       format "${key}" "%s" "$@"
 }
 
 function fread() {
@@ -217,7 +260,14 @@ function backtrace() {
        local i source
        for i in $(seq ${start} ${#BASH_SOURCE[*]}); do
                [ -z "${FUNCNAME[${i}]}" ] && continue
-               [ "${FUNCNAME[${i}]}" == "main" ] && continue
+
+               # Print called binary with arguments.
+               if [ "${FUNCNAME[${i}]}" == "main" ]; then
+                       local args="$(list_reverse ${BASH_ARGV[*]})"
+                       printf -v source "%20s" "$0"
+                       error_log "  ${source} ${args}"
+                       continue
+               fi
 
                source=${BASH_SOURCE[$(( ${i} + 1 ))]}
                error_log "  $(printf "%20s" "'${FUNCNAME[${i}]}'") called from ${source:-<shell>}:${BASH_LINENO[${i}]}"
@@ -297,6 +347,10 @@ function cmd_exec() {
        exit ${EXIT_ERROR}
 }
 
+function cmd_not_implemented() {
+       assert false "not implemented"
+}
+
 function seq() {
        if [ $# -eq 2 ]; then
                eval echo {${1}..${2}}
@@ -309,6 +363,11 @@ function which() {
        type -P $@
 }
 
+# Prints the number of seconds since epoch.
+function timestamp() {
+       date -u "+%s"
+}
+
 function beautify_time() {
        local value=${1}
 
@@ -396,6 +455,22 @@ function dec() {
        printf "%d\n" "${hex}"
 }
 
+function chr() {
+       local char="${1}"
+
+       [ ${char} -lt 256 ] || return ${EXIT_ERROR}
+
+       printf "\\$(( ${char} / 64 * 100 + ${char} % 64 / 8 * 10 + ${char} % 8 ))\n"
+}
+
+function ord() {
+       LC_CTYPE="C" printf "%d\n" "'${1}"
+}
+
+function hex() {
+       printf "%X\n" "${1}"
+}
+
 function network_is_running() {
        # Check, if the network service is running.
        service_is_active network
@@ -413,3 +488,118 @@ function contains_spaces() {
 
        return ${EXIT_FALSE}
 }
+
+function string_split() {
+       local string="$@"
+
+       local pos=0
+       while [ ${pos} -lt ${#string} ]; do
+               print "${string:${pos}:1}"
+               pos=$(( ${pos} + 1 ))
+       done
+
+       return ${EXIT_OK}
+}
+
+function string_reverse() {
+       local string="$@"
+
+       local output
+       local pos=0
+       while [ ${pos} -lt ${#string} ]; do
+               output="${string:${pos}:1}${output}"
+               pos=$(( ${pos} + 1 ))
+       done
+
+       print "${output}"
+       return ${EXIT_OK}
+}
+
+function dec2bin() {
+       local number="${1}"
+
+       local output
+
+       local i div
+       for i in 7 6 5 4 3 2 1; do
+               div=$(( 2 ** ${i} ))
+
+               if [ $(( ${number} / ${div} )) -eq 1 ]; then
+                       output="${output}1"
+               else
+                       output="${output}0"
+               fi
+               number="$(( ${number} % ${div} ))"
+       done
+
+       if [ $(( ${number} % 2 )) -eq 1 ]; then
+               output="${output}1"
+       else
+               output="${output}0"
+       fi
+
+       print "${output}"
+}
+
+function bin2dec() {
+       local string="${1}"
+       local number=0
+
+       local pos=0 char
+       while [ ${pos} -lt ${#string} ]; do
+               char="${string:${pos}:1}"
+               pos=$(( ${pos} + 1 ))
+
+               number=$(( ${number} << 1 ))
+
+               case "${char}" in
+                       0) ;;
+                       1)
+                               number=$(( ${number} + 1 ))
+                               ;;
+                       *)
+                               assert false "Invalid character: ${char}"
+                               ;;
+               esac
+       done
+
+       print "${number}"
+       return ${EXIT_OK}
+}
+
+function char2bin() {
+       local dec="$(ord "${1}")"
+
+       dec2bin "${dec}"
+}
+
+function bin2char() {
+       local dec="$(bin2dec "$@")"
+
+       chr "${dec}"
+}
+
+function bin2hex() {
+       local dec="$(bin2dec "$@")"
+
+       dec2hex "${dec}"
+}
+
+function hex2bin() {
+       local dec="$(hex2dec "$@")"
+
+       dec2bin "${dec}"
+}
+
+function hex2dec() {
+       local hex="${1}"
+
+       # Prepend 0x if necessary.
+       [ "${hex:0:2}" = "0x" ] || hex="0x${hex}"
+
+       printf "%d\n" "${hex}"
+}
+
+function dec2hex() {
+       printf "%02x\n" "${1}"
+}