forked from ictinnovations/ictcore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbash-ini-parser
executable file
·198 lines (188 loc) · 4.94 KB
/
bash-ini-parser
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#
# based on http://theoldschooldevops.com/2008/02/09/bash-ini-parser/
#
PREFIX="cfg_section_"
function debug {
return #abort debug
echo $*
echo --start--
echo "${ini[*]}"
echo --end--
echo
}
function cfg_parser {
shopt -p extglob &> /dev/null
CHANGE_EXTGLOB=$?
if [ $CHANGE_EXTGLOB = 1 ]
then
shopt -s extglob
fi
ini="$(<$1)" # read the file
ini=${ini//$'\r'/} # remove linefeed i.e dos2unix
ini="${ini//[/\\[}" # escape [
debug
ini="${ini//]/\\]}" # escape ]
debug
IFS=$'\n' && ini=( ${ini} ) # convert to line-array
debug
ini=( ${ini[*]//;*/} ) # remove comments with ;
debug
ini=( ${ini[*]/#+([[:space:]])/} ) # remove init whitespace
debug "whitespace around"
ini=( ${ini[*]/*([[:space:]])=*([[:space:]])/=} ) # remove whitespace around =
debug
ini=( ${ini[*]/#\\[/\}$'\n'"$PREFIX"} ) # set section prefix
debug
ini=( ${ini[*]/%\\]/ \(} ) # convert text2function (1)
debug
ini=( ${ini[*]/=/=\( } ) # convert item to array
debug
ini=( ${ini[*]/%/ \)} ) # close array parenthesis
debug
ini=( ${ini[*]/%\\ \)/ \\} ) # the multiline trick
debug
ini=( ${ini[*]/%\( \)/\(\) \{} ) # convert text2function (2)
debug
ini=( ${ini[*]/%\} \)/\}} ) # remove extra parenthesis
ini=( ${ini[*]/%\{/\{$'\n''cfg_unset ${FUNCNAME/#'$PREFIX'}'$'\n'} ) # clean previous definition of section
debug
ini[0]="" # remove first element
debug
ini[${#ini[*]} + 1]='}' # add the last brace
debug
eval "$(echo "${ini[*]}")" # eval the result
EVAL_STATUS=$?
if [ $CHANGE_EXTGLOB = 1 ]
then
shopt -u extglob
fi
return $EVAL_STATUS
}
function cfg_writer {
SECTION=$1
OLDIFS="$IFS"
IFS=' '$'\n'
if [ -z "$SECTION" ]
then
fun="$(declare -F)"
else
fun="$(declare -F $PREFIX$SECTION)"
if [ -z "$fun" ]
then
echo "section $SECTION not found" >2
exit 1
fi
fi
fun="${fun//declare -f/}"
for f in $fun; do
[ "${f#$PREFIX}" == "${f}" ] && continue
item="$(declare -f ${f})"
item="${item##*\{}" # remove function definition
item="${item##*FUNCNAME*$PREFIX\};}" # remove clear section
item="${item/\}}" # remove function close
item="${item%)*}" # remove everything after parenthesis
item="${item});" # add close parenthesis
vars=""
while [ "$item" != "" ]
do
newvar="${item%%=*}" # get item name
vars="$vars $newvar" # add name to collection
item="${item#*;}" # remove readed line
done
eval $f
echo "[${f#$PREFIX}]" # output section
for var in $vars; do
eval 'local length=${#'$var'[*]}' # test if var is an array
if [ $length == 1 ]
then
echo $var=\"${!var}\" #output var
else
echo ";$var is an array" # add comment denoting var is an array
eval 'echo $var=\"${'$var'[*]}\"' # output array var
fi
done
done
IFS="$OLDIFS"
}
function cfg_unset {
SECTION=$1
OLDIFS="$IFS"
IFS=' '$'\n'
if [ -z "$SECTION" ]
then
fun="$(declare -F)"
else
fun="$(declare -F $PREFIX$SECTION)"
if [ -z "$fun" ]
then
echo "section $SECTION not found" >2
return
fi
fi
fun="${fun//declare -f/}"
for f in $fun; do
[ "${f#$PREFIX}" == "${f}" ] && continue
item="$(declare -f ${f})"
item="${item##*\{}" # remove function definition
item="${item##*FUNCNAME*$PREFIX\};}" # remove clear section
item="${item/\}}" # remove function close
item="${item%)*}" # remove everything after parenthesis
item="${item});" # add close parenthesis
vars=""
while [ "$item" != "" ]
do
newvar="${item%%=*}" # get item name
vars="$vars $newvar" # add name to collection
item="${item#*;}" # remove readed line
done
for var in $vars; do
unset $var
done
done
IFS="$OLDIFS"
}
function cfg_clear {
SECTION=$1
OLDIFS="$IFS"
IFS=' '$'\n'
if [ -z "$SECTION" ]
then
fun="$(declare -F)"
else
fun="$(declare -F $PREFIX$SECTION)"
if [ -z "$fun" ]
then
echo "section $SECTION not found" >2
exit 1
fi
fi
fun="${fun//declare -f/}"
for f in $fun; do
[ "${f#$PREFIX}" == "${f}" ] && continue
unset -f ${f}
done
IFS="$OLDIFS"
}
function cfg_update {
SECTION=$1
VAR=$2
OLDIFS="$IFS"
IFS=' '$'\n'
fun="$(declare -F $PREFIX$SECTION)"
if [ -z "$fun" ]
then
echo "section $SECTION not found" >2
exit 1
fi
fun="${fun//declare -f/}"
item="$(declare -f ${fun})"
#item="${item##* $VAR=*}" # remove var declaration
item="${item/\}}" # remove function close
item="${item}
$VAR=(${!VAR})
"
item="${item}
}" # close function again
eval "function $item"
}
# vim: filetype=sh