diff options
author | davidovski <david@davidovski.xyz> | 2022-03-03 18:38:37 +0000 |
---|---|---|
committer | davidovski <david@davidovski.xyz> | 2022-03-03 18:38:37 +0000 |
commit | d9091aea868392a181317a0609870273adf2c094 (patch) | |
tree | 671eb4b33036ff095978030bef62b40fb09f1733 /src/parseconf.sh | |
parent | e989ac4788a3f947ce0097effdccaabd62c5a8ff (diff) |
fixed everything to work with posix shellv1.0.2
Diffstat (limited to 'src/parseconf.sh')
-rwxr-xr-x | src/parseconf.sh | 41 |
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 |