-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.func
59 lines (58 loc) · 1.65 KB
/
config.func
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
read_ini () {
local l v hv sect INIsection=()
(( $# )) || return 1
local iniFile2Read="$1"
[[ -f $iniFile2Read && -r $iniFile2Read ]] || return 2
sect=''
while read -r l; do
[[ $l =~ ^[[:space:]]*([\#\;]|$) ]] && continue
if [[ $l =~ ^[[:space:]]*\[([^\]]+)\][[:space:]]*$ ]]; then
sect=${BASH_REMATCH[1]}
INIsection+=($sect)
unset INI${sect}
declare -A INI${sect}
elif [[ $sect && $l =~ ^[[:space:]]*([A-Za-z_0-9]+)[[:space:]]*=[[:space:]]*(.*)$ ]]; then
eval "INI${sect}[${BASH_REMATCH[1]}]='${BASH_REMATCH[2]}'"
else
echo 'Wrong ini file format: section ID must preceed any parameters' >&2
return 3
fi
done <$iniFile2Read
for sect in ${INIsection[@]}; do
for v in $(eval "echo \${!INI$sect[@]}"); do
hv="INI$sect[$v]"
eval "$hv=\"$(doMacroSub ${!hv})\""
done
done
for sect in ${INIsection[@]} section; do
echo -n "unset INI${sect}; "
declare -p INI${sect} 2>/dev/null
done
return 0
}
doMacroSub () {
local ERRC_FUNC_REQUIRE_ARG=1 ERRC_NOTHING_TO_DO=2 ERRC_COMMAND_CANTBE_EVAL=3
(( $# )) || return $ERRC_FUNC_REQUIRE_ARG
local v="$@" m o
[[ $v =~ \{\{.+\}\} ]] || { echo "$v"; return $ERRC_NOTHING_TO_DO; }
while read -r m; do
if [[ $m =~ [[:space:]] ]]; then
o=$(eval "$m" 2>/dev/null) || return $ERRC_COMMAND_CANTBE_EVAL
elif [[ $m =~ \. ]]; then
eval "o=\${INI${m%%.*}[${m#*.}]}"
else
o="${!m}"
fi
v=${v/\{\{$m\}\}/$o}
done < <(
sed -r -e 's/\}\}[^{]+\{\{/}}{{/g; s/^[^{]+\{/{/; s/}[^}{]+$/}/; s%\}\}$%%; s%\}\}%\n%g; s%\{\{%%g' -e '/^$/d' <<<"$v" | \
sort | uniq
)
if [[ $v =~ \{\{.+\}\} ]]; then
doMacroSub $v
return $?
else
echo "$v"
fi
return 0
}