summaryrefslogtreecommitdiff
path: root/src/parseconf.sh
diff options
context:
space:
mode:
authordavidovski <david@davidovski.xyz>2022-03-03 18:38:37 +0000
committerdavidovski <david@davidovski.xyz>2022-03-03 18:38:37 +0000
commitd9091aea868392a181317a0609870273adf2c094 (patch)
tree671eb4b33036ff095978030bef62b40fb09f1733 /src/parseconf.sh
parente989ac4788a3f947ce0097effdccaabd62c5a8ff (diff)
fixed everything to work with posix shellv1.0.2
Diffstat (limited to 'src/parseconf.sh')
-rwxr-xr-xsrc/parseconf.sh41
1 files changed, 19 insertions, 22 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