X-Git-Url: http://git.ipfire.org/?p=people%2Fstevee%2Fnetwork.git;a=blobdiff_plain;f=src%2Ffunctions%2Ffunctions.util;h=aff803a97ea7fa68962c499b8cc42d7a9f22eb3f;hp=79ec87db7e579fa078f0f37f8443332ee4feec19;hb=1c6a4e3050e4ea8baf1b57a73b411c631457794f;hpb=5ca9dc303abeeea7aa66c47d66c17b153bf419e6 diff --git a/src/functions/functions.util b/src/functions/functions.util index 79ec87db..aff803a9 100644 --- a/src/functions/functions.util +++ b/src/functions/functions.util @@ -20,7 +20,7 @@ ############################################################################### # A simple print statement -function print() { +print() { local fmt=${1}; shift printf -- "${fmt}\n" "$@" @@ -29,11 +29,11 @@ function print() { # 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() { +args() { echo "$@" | xargs printf "%s\n" } -function unquote() { +unquote() { local var="$@" if [ "${var:0:1}" = "\"" ]; then @@ -48,11 +48,11 @@ function unquote() { print "${var}" } -function quote() { +quote() { print "\"%s\"" "$@" } -function strip() { +strip() { local value="$@" # remove leading whitespace characters @@ -65,43 +65,43 @@ function strip() { } # Print a pretty error message -function error() { +error() { echo -e " ${CLR_RED_B}ERROR${CLR_RESET} : $@" >&2 } -function error_log() { +error_log() { log ERROR "$@" } # Print a pretty warn message -function warning() { +warning() { echo -e " ${CLR_YELLOW_B}WARNING${CLR_RESET}: $@" >&2 } -function warning_log() { +warning_log() { log WARNING "$@" } # The next three functions are kept for backwards # compatibility. The need to be dropped at some time. -function listsort() { +listsort() { list_sort $@ } -function listmatch() { +listmatch() { list_match $@ } -function listlength() { +listlength() { list_length $@ } # Speedup function to avoid a call of the basename binary -function basename() { +basename() { echo "${1##*/}" } -function format() { +format() { local key=${1} assert isset key @@ -113,7 +113,7 @@ function format() { printf -v "${key}" "${format}" "$@" } -function assign() { +assign() { local key=${1} assert isset key shift @@ -121,7 +121,7 @@ function assign() { format "${key}" "%s" "$@" } -function fread() { +fread() { local file=${1} assert isset file @@ -130,21 +130,26 @@ function fread() { print "$(<${file})" } -function fwrite() { +fwrite() { local file=${1} assert isset file shift - print "%s" "$@" >> ${file} + if [ ! -w "${file}" ]; then + log ERROR "${file}: No such file" + return ${EXIT_ERROR} + fi + + print "%s" "$@" >> ${file} 2>/dev/null } -function enabled() { +enabled() { local param=${1} list_match "${!param}" yes on true 1 } -function mac_generate() { +mac_generate() { # Get a bunch of random hex digits # and remove all dashes from the input. local random=$(/dev/null } -function cmd_exec() { +cmd_exec() { local cmd=$@ log DEBUG "Exec'ing command: ${cmd}" @@ -347,11 +352,16 @@ function cmd_exec() { exit ${EXIT_ERROR} } -function cmd_not_implemented() { +cmd_not_implemented() { assert false "not implemented" } -function seq() { +# Increase security of the read command +read() { + builtin read -r $@ +} + +seq() { if [ $# -eq 2 ]; then eval echo {${1}..${2}} elif [ $# -eq 3 ]; then @@ -359,16 +369,16 @@ function seq() { fi } -function which() { +which() { type -P $@ } # Prints the number of seconds since epoch. -function timestamp() { +timestamp() { date -u "+%s" } -function beautify_time() { +beautify_time() { local value=${1} local unit @@ -394,7 +404,7 @@ function beautify_time() { echo "${value}${unit}" } -function beautify_bytes() { +beautify_bytes() { local value=${1} local unit @@ -407,7 +417,7 @@ function beautify_bytes() { echo "${value}${unit}" } -function module_load() { +module_load() { local module=${1} if ! grep -q "^${module}" /proc/modules; then @@ -416,7 +426,7 @@ function module_load() { fi } -function binary_exists() { +binary_exists() { local binary=${1} if [ -n "$(type -p ${binary})" ]; then @@ -426,7 +436,17 @@ function binary_exists() { return ${EXIT_ERROR} } -function process_kill() { +function_exists() { + local function="${1}" + + if [ "$(type -t "${function}")" = "function" ]; then + return ${EXIT_TRUE} + fi + + return ${EXIT_FALSE} +} + +process_kill() { local process=${1} if ! isinteger process; then @@ -445,7 +465,7 @@ function process_kill() { done } -function dec() { +dec() { local hex=${1} if [ "${hex:0:2}" != "0x" ]; then @@ -455,7 +475,7 @@ function dec() { printf "%d\n" "${hex}" } -function chr() { +chr() { local char="${1}" [ ${char} -lt 256 ] || return ${EXIT_ERROR} @@ -463,20 +483,20 @@ function chr() { printf "\\$(( ${char} / 64 * 100 + ${char} % 64 / 8 * 10 + ${char} % 8 ))\n" } -function ord() { +ord() { LC_CTYPE="C" printf "%d\n" "'${1}" } -function hex() { +hex() { printf "%X\n" "${1}" } -function network_is_running() { +network_is_running() { # Check, if the network service is running. service_is_active network } -function contains_spaces() { +contains_spaces() { local var="$@" # Eliminate spaces. @@ -489,7 +509,7 @@ function contains_spaces() { return ${EXIT_FALSE} } -function string_split() { +string_split() { local string="$@" local pos=0 @@ -501,7 +521,7 @@ function string_split() { return ${EXIT_OK} } -function string_reverse() { +string_reverse() { local string="$@" local output @@ -515,7 +535,7 @@ function string_reverse() { return ${EXIT_OK} } -function dec2bin() { +dec2bin() { local number="${1}" local output @@ -541,7 +561,7 @@ function dec2bin() { print "${output}" } -function bin2dec() { +bin2dec() { local string="${1}" local number=0 @@ -567,31 +587,31 @@ function bin2dec() { return ${EXIT_OK} } -function char2bin() { +char2bin() { local dec="$(ord "${1}")" dec2bin "${dec}" } -function bin2char() { +bin2char() { local dec="$(bin2dec "$@")" chr "${dec}" } -function bin2hex() { +bin2hex() { local dec="$(bin2dec "$@")" dec2hex "${dec}" } -function hex2bin() { +hex2bin() { local dec="$(hex2dec "$@")" dec2bin "${dec}" } -function hex2dec() { +hex2dec() { local hex="${1}" # Prepend 0x if necessary. @@ -600,6 +620,6 @@ function hex2dec() { printf "%d\n" "${hex}" } -function dec2hex() { +dec2hex() { printf "%02x\n" "${1}" }