From d9091aea868392a181317a0609870273adf2c094 Mon Sep 17 00:00:00 2001
From: davidovski <david@davidovski.xyz>
Date: Thu, 3 Mar 2022 18:38:37 +0000
Subject: fixed everything to work with posix shell

---
 Makefile          |  1 -
 src/parseconf.sh  | 41 +++++++++++++++++++----------------------
 src/shtests.sh    | 22 ++++++++++++++--------
 test/hbar.sh      |  2 +-
 test/parseconf.sh | 21 ++++++++++-----------
 test/test.conf    | 21 +++++++++++++++++++++
 6 files changed, 65 insertions(+), 43 deletions(-)

diff --git a/Makefile b/Makefile
index a73c92b..8cba2c4 100755
--- a/Makefile
+++ b/Makefile
@@ -33,7 +33,6 @@ install-glyphs: src/glyphs.sh
 check-parseconf: 
 	shtests ./test/parseconf.sh
 
-
 build-hbar: src/hbar.c install-colors
 	mkdir -pv bin
 	${CC} src/hbar.c -o bin/hbar ${FLAGS}
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
diff --git a/test/hbar.sh b/test/hbar.sh
index e7f43ef..9daf606 100755
--- a/test/hbar.sh
+++ b/test/hbar.sh
@@ -1,4 +1,4 @@
-#!/bin/bash
+#!/bin/sh
 
 HBAR=./bin/hbar
 TEXT="Hello there"
diff --git a/test/parseconf.sh b/test/parseconf.sh
index 69dd78c..0a87188 100755
--- a/test/parseconf.sh
+++ b/test/parseconf.sh
@@ -1,7 +1,6 @@
 #!/bin/sh
 
 PARSECONF="./src/parseconf.sh"
-
 SIMPLECONF="./test/simple.conf"
 
 test_simple_loading () {
@@ -12,7 +11,7 @@ test_simple_parsing () {
     config="
 key value
     "
-    retval=$(${PARSECONF} key <<< "$config")
+    retval=$(printf "$config" | ${PARSECONF} key)
     [ "$retval" = "key:value" ]
 }
 
@@ -20,7 +19,7 @@ test_bad_formatting () {
     config="
       key       value    
     "
-    retval=$(${PARSECONF} key <<< "$config")
+    retval=$(printf "$config" | ${PARSECONF} key) 
     [ "$retval" = "key:value" ]
 }
 
@@ -28,7 +27,7 @@ test_unecessary_semicolons () {
     config="
 key value;
     "
-    retval=$(${PARSECONF} key <<< "$config")
+    retval=$(printf "$config" | ${PARSECONF} key)
     [ "$retval" = "key:value" ]
 }
 
@@ -36,7 +35,7 @@ test_extra_unecessary_semicolons () {
     config="
 key value;;;;;
     "
-    retval=$(${PARSECONF} key <<< "$config")
+    retval=$(printf "$config" | ${PARSECONF} key)
     [ "$retval" = "key:value" ]
 }
 
@@ -47,7 +46,7 @@ key2 value2
 key3 value3
 key4 value4
     "
-    retval=$(${PARSECONF} key2 <<< "$config")
+    retval=$(printf "$config" | ${PARSECONF} key2)
     [ "$retval" = "key2:value2" ]
 }
 
@@ -62,7 +61,7 @@ list [
     e
 ]
     "
-    retval=$(${PARSECONF} list <<< "$config")
+    retval=$(printf "$config" | ${PARSECONF} list)
     [ "$retval" = "list:a b c d e " ]
 }
 
@@ -76,7 +75,7 @@ dict {
     e 5
 }
     "
-    retval=$(${PARSECONF} dict.a <<< "$config")
+    retval=$(printf "$config" | ${PARSECONF} dict.a)
     [ "$retval" = "dict.a:1" ]
 }
 
@@ -89,7 +88,7 @@ test_include () {
     config="
 include test/simple.conf
     "
-    retval=$(${PARSECONF} key2 <<< "$config")
+    retval=$(printf "$config" | ${PARSECONF} key2)
     [ "$retval" = "key2:value2" ]
 }
 
@@ -102,7 +101,7 @@ dict {
     d 4
 }
     "
-    retval=$(${PARSECONF} "dict.*" <<< "$config" | wc -l)
+    retval=$(printf "$config" | ${PARSECONF} "dict.*" | wc -l)
     [ "$retval" = "4" ]
 }
 
@@ -115,7 +114,7 @@ dict {
     d 4
 }
     "
-    retval=$(${PARSECONF} -c 1 "dict.*" <<< "$config")
+    retval=$(printf "$config" | ${PARSECONF} -c 1 "dict.*")
     [ "$retval" = "dict.a:1" ]
 }
 
diff --git a/test/test.conf b/test/test.conf
index 8ac673f..8c660e3 100644
--- a/test/test.conf
+++ b/test/test.conf
@@ -41,3 +41,24 @@ other_dict {
     }
 
 }
+
+
+k [
+    1 2 3 4
+]
+values after
+and then
+another {
+    dict here
+    for me
+    to try
+    with {
+        more sub
+        dicts {
+            and even
+            more {
+                dicts here
+            }
+        }
+    }
+}
-- 
cgit v1.2.1