diff options
author | davidovski <david@davidovski.xyz> | 2022-02-14 00:35:47 +0000 |
---|---|---|
committer | davidovski <david@davidovski.xyz> | 2022-02-14 00:35:47 +0000 |
commit | d3093d4d72283019226ab0f8f76de520a0509e2a (patch) | |
tree | ca681fa87d37c1ad526f38d25ef5926861e114cb /src | |
parent | 526e2bfcdb5f55f2bd94b29cf314f8cb4c37bf4f (diff) |
made parseconf insanely fast
Diffstat (limited to 'src')
-rwxr-xr-x | src/parseconf.sh | 41 |
1 files changed, 12 insertions, 29 deletions
diff --git a/src/parseconf.sh b/src/parseconf.sh index aa5885d..f92bf0e 100755 --- a/src/parseconf.sh +++ b/src/parseconf.sh @@ -28,7 +28,7 @@ parse_line() { shift local value="$@" - [ "$key" = "include" ] && parse $value && return + [ "$key" = "include" ] && cat $value | parse && return [ "$key" = "]" ] && unset list[-1] && printf "\n" && return [ "$key" = "}" ] && unset level[-1] && return @@ -53,30 +53,13 @@ parse_line() { # parse () { local file="$1" + export level=() export list=() while IFS= read -r line; do # strip whitespace - line=$(sed "s/^#.*$\|\s(\s\+)\|^\s\|\s^\|;*$//g" <<< "$line") - parse_line $line - done < "$file" -} - -# Filter the parsed file for specific keys -# -filter () { - local pattern= - - [ $# = 0 ] && - pattern=".*" || - pattern=$(sed "s/\*/[^:]*/g"<<< "$@") - - $print_keys && - pattern="s/^($pattern:.+)/\1/p" || - pattern="s/^$pattern:(.+)/\1/p" - - - parse $CONF_FILE | sed -rn $pattern + parse_line $line + done < "/dev/stdin" } # Use the env variable if exists @@ -98,7 +81,7 @@ while getopts ":f:c:v" opt; do print_keys=false ;; c) - count="${OPTARG}" + count="${OPTARG}q" ;; *) esac @@ -106,12 +89,12 @@ done shift $((OPTIND-1)) -if echo $@ | grep -q "."; then - [ -z ${count} ] && - filter "$@" || - filter "$@" | tail -n $count -else - parse $CONF_FILE -fi +[ $# = 0 ] && + pattern=".*" || + pattern=$(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 |