123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- #!/bin/bash
-
-
-
- __ini_cat() {
-
-
-
- local line
- while read -r line; do
- printf -- '%s\n' "$line"
- done
- }
-
- __ini_expand() {
-
-
-
- local line
- while read -r line; do
- __ini_expandln "$line"
- done
- }
-
- __ini_expandln() {
-
-
-
- local line_orig=$1
- local line_todo=$line_orig
- local line_done
- local Depth=0
- local MaxDepth=10
- while true; do
- ((Depth++))
- debug_var line_todo
- test "$Depth" -le "$MaxDepth" || {
- warn "expansion error: reached maximum depth: $Depth > $MaxDepth" \
- " original line: $line_orig" \
- " expanded line: $line_todo"
- return 3
- }
- line_done=$(__ini_expandln_once "$line_todo")
- debug_var line_done
- test "$line_done" == "$line_todo" && break
- line_todo=$line_done
- done
- echo "$line_done"
- }
-
- __ini_expandln_once() {
-
-
-
- local line=$1
- local ref
- local ipath
- local value
- local refs=()
- mapfile -t refs <<<"$(grep -Eo '[[][^]]+[]]' <<< "$line_todo")"
- debug_var refs
- for ref in "${refs[@]}"; do
- test -n "$ref" || continue
- ipath=${ref#[}; ipath=${ipath%]}
- value=$(ini 1value "$ipath")
- debug_var line ref ipath value
- line=${line//"[$ipath]"/"$value"}
- done
- echo "$line"
- }
-
- __ini_grepcmt() {
-
-
-
- grep -v '^[[:space:]]*#'
- }
-
- __ini_grepkey() {
-
-
-
- local wnt=$1
- grep '.' \
- | grep -e "^$wnt=" \
- | cut -d= -f2- \
- | __ini_maybe_expand
- }
-
- __ini_greppath() {
-
-
-
-
-
-
-
-
- local wnt=$1
- local wntkey=${wnt##*:}
- local wntsec=${wnt%:"$wntkey"}
- local override
- if test "$wntsec" = 'ENV'; then
- override=${!wntkey}
- test -n "$override" \
- && echo "$override" \
- && return
- fi
- __ini_grepsec "$wntsec" | __ini_grepkey "$wntkey"
- }
-
- __ini_grepsec() {
-
-
-
- local wnt=$1
- local ok=false
- local line
- grep '.' \
- | while read -r line; do
- case "$line" in
- \["$wnt"\]) ok=true; continue ;;
- \[*\]) ok=false; continue ;;
- esac
- $ok || continue
- printf -- '%s\n' "$line"
- done \
- | grep '^[[:space:]]*[^[:space:]:][^:]*[[:space:]]*[=]' \
- | sed -e 's/[[:space:]]*=[[:space:]]*/=/; s/[[:space:]]*$//; s/^[[:space:]]*//;'
- }
-
- __ini_lskeys() {
-
-
-
- local sct=$1
- __ini_grepsec "$sct" | cut -d= -f1 | awk '!x[$0]++'
- }
-
- __ini_lssect() {
-
-
-
- grep -x '\[.*\]' | sed 's/^.//; s/.$//'
- }
-
- __ini_maybe_expand() {
-
-
-
- if test "$MKIT_INI_EXPAND" -gt 0; then
- MKIT_INI_EXPAND=$(( --MKIT_INI_EXPAND )) __ini_expand
- else
- __ini_cat
- fi
- }
-
- __ini_body() {
-
-
-
-
-
- local inc
- local incre='\[INCLUDE:.*\]'
- local iline
- if iline=$(grep -m1 -xe "$incre" "$MKIT_INI"); then
- inc=${iline#*:}; inc=${inc%]}
- grep -vxe "$incre" "$inc"
- grep -vxe "$incre" "$MKIT_INI"
- else
- cat "$MKIT_INI"
- fi | __ini_grepcmt
- }
-
- ini() {
-
-
-
- local op=$1
- local arg=$2
- local fn
- local limit=__ini_cat
- case $op in
- lskeys) fn=__ini_lskeys ;;
- lssect) fn=__ini_lssect ;;
- sec) fn=__ini_grepsec ;;
- values) fn=__ini_greppath ;;
- 1value) fn=__ini_greppath; limit="tail -1" ;;
- *) die "incorrect use of \`ini()\`"
- esac
- __ini_body | $fn "$arg" | $limit
- }
|