diff options
Diffstat (limited to 'src')
-rwxr-xr-x | src/parseconf.sh | 41 | ||||
-rw-r--r-- | src/shtests.sh | 22 |
2 files changed, 33 insertions, 30 deletions
diff --git a/src/parseconf.sh b/src/parseconf.sh index f92bf0e..929ce05 100755 --- a/src/parseconf.sh +++ b/src/parseconf.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/sh usage () { printf "Usage $0 " @@ -13,15 +13,10 @@ Arguments: EOF } -getlevel() { - for i in $*; do - printf $i - printf '.' - done -} - +# parse a single config file line +# parse_line() { - [ $# == "0" ] && return + [ $# = "0" ] && return local line="$@" local key=$1 @@ -29,21 +24,21 @@ parse_line() { local value="$@" [ "$key" = "include" ] && cat $value | parse && return - [ "$key" = "]" ] && unset list[-1] && printf "\n" && return - [ "$key" = "}" ] && unset level[-1] && return + [ "$key" = "]" ] && unset list=${list%.*} && printf "\n" && return + [ "$key" = "}" ] && unset level=${level%.*} && return - case ${value: -1} in + case ${value##* } in "{") - level+=("$key") + level="${level}${key}." ;; "[") - list+=("$key") - printf "$(getlevel ${level[@]})$key:" + list="${list}${list:+.}${key}" + printf "$level$key:" ;; *) - [ "${#list[@]}" == "0" ] && - printf "$(getlevel ${level[@]})$key:$value\n" || + [ "${#list}" = "0" ] && + printf "$level$key:$value\n" || printf "$line " ;; esac @@ -54,10 +49,9 @@ parse_line() { parse () { local file="$1" - export level=() - export list=() + export level="" + export list="" while IFS= read -r line; do - # strip whitespace parse_line $line done < "/dev/stdin" } @@ -91,10 +85,13 @@ shift $((OPTIND-1)) [ $# = 0 ] && pattern=".*" || - pattern=$(sed "s/\*/[^:]*/g" <<< "$@") + pattern=$(echo $@ | sed "s/\*/[^:]*/g") $print_keys && pattern="s/^($pattern:.+)/\1/p;${count}" || pattern="s/^$pattern:(.+)/\1/p;${count}" -sed "s/^#.*$\|\s(\s\+)\|^\s\|\s^\|;*$//g" $CONF_FILE | parse $@ | sed -rn $pattern +# strip whitespace +sed "s/^#.*$\|\s(\s\+)\|^\s\|\s^\|;*$//g" $CONF_FILE | + parse $@ | + sed -rn $pattern diff --git a/src/shtests.sh b/src/shtests.sh index 9994d7b..70dc21b 100644 --- a/src/shtests.sh +++ b/src/shtests.sh @@ -14,29 +14,35 @@ PASS="${BLUE}[ ${GREEN}PASS${BLUE} ]${RESET}" FAIL="${BLUE}[ ${RED}FAIL${BLUE} ]${RESET}" +V=false + runtest () { - test_name=$(sed "s/_/ /g" <<< "$1") + test_name=$(echo $1 | sed "s/_/ /g") test_func="$2" - printf "${BLUE}[ ] ${RESET}$test_name\r"; + printf "${BLUE}[ ] ${RESET}$test_name "; if "$test_func" ; then - printf "$PASS\n" + printf "\r$PASS\n" return 0 else - printf "$FAIL\n" + printf "\r$FAIL\n" return 1 fi } +# TODO use getopt for this +if [ "$1" = "-v" ]; then + shift + V=true +fi + if [ $# = "0" ]; then printf "${RED}No tests file has been provided\n" exit 1; else - source $@ + . $@ fi - -tests=$(declare -F | sed -rn "s/declare -f test_(.+)/\1/p") - +tests=$(sed -n "s/^test_\(.*\)\s*()\s*{/\1/p" $@) total=$(echo $tests | wc -w) passed=0 failed=0 |