blob: 1f12343ae04b1ee044800504f47ef64e67379a18 [file] [log] [blame]
Dean Troyerbf2ad702015-03-09 15:16:10 -05001#!/bin/bash
2#
3# **inc/ini-config** - Configuration/INI functions
4#
5# Support for manipulating INI-style configuration files
6#
7# These functions have no external dependencies and no side-effects
8
9# Save trace setting
10INC_CONF_TRACE=$(set +o | grep xtrace)
11set +o xtrace
12
13
14# Config Functions
15# ================
16
17# Append a new option in an ini file without replacing the old value
Ian Wienandf44a0242015-07-22 10:34:47 +100018# iniadd [-sudo] config-file section option value1 value2 value3 ...
Dean Troyerbf2ad702015-03-09 15:16:10 -050019function iniadd {
Ian Wienand433a9b12015-10-07 13:29:31 +110020 local xtrace
21 xtrace=$(set +o | grep xtrace)
Dean Troyerbf2ad702015-03-09 15:16:10 -050022 set +o xtrace
Ian Wienandf44a0242015-07-22 10:34:47 +100023 local sudo=""
24 if [ $1 == "-sudo" ]; then
25 sudo="-sudo "
26 shift
27 fi
Dean Troyerbf2ad702015-03-09 15:16:10 -050028 local file=$1
29 local section=$2
30 local option=$3
31 shift 3
32
Ian Wienand7ae97292016-02-16 14:50:53 +110033 local values
34 values="$(iniget_multiline $file $section $option) $@"
Ian Wienandf44a0242015-07-22 10:34:47 +100035 iniset_multiline $sudo $file $section $option $values
Dean Troyerbf2ad702015-03-09 15:16:10 -050036 $xtrace
37}
38
39# Comment an option in an INI file
Ian Wienandf44a0242015-07-22 10:34:47 +100040# inicomment [-sudo] config-file section option
Dean Troyerbf2ad702015-03-09 15:16:10 -050041function inicomment {
Ian Wienand433a9b12015-10-07 13:29:31 +110042 local xtrace
43 xtrace=$(set +o | grep xtrace)
Dean Troyerbf2ad702015-03-09 15:16:10 -050044 set +o xtrace
Ian Wienandf44a0242015-07-22 10:34:47 +100045 local sudo=""
46 if [ $1 == "-sudo" ]; then
47 sudo="sudo "
48 shift
49 fi
Dean Troyerbf2ad702015-03-09 15:16:10 -050050 local file=$1
51 local section=$2
52 local option=$3
53
Ian Wienandf44a0242015-07-22 10:34:47 +100054 $sudo sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" "$file"
Dean Troyerbf2ad702015-03-09 15:16:10 -050055 $xtrace
56}
57
58# Get an option from an INI file
59# iniget config-file section option
60function iniget {
Ian Wienand433a9b12015-10-07 13:29:31 +110061 local xtrace
62 xtrace=$(set +o | grep xtrace)
Dean Troyerbf2ad702015-03-09 15:16:10 -050063 set +o xtrace
64 local file=$1
65 local section=$2
66 local option=$3
67 local line
68
69 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
70 echo ${line#*=}
71 $xtrace
72}
73
74# Get a multiple line option from an INI file
75# iniget_multiline config-file section option
76function iniget_multiline {
Ian Wienand433a9b12015-10-07 13:29:31 +110077 local xtrace
78 xtrace=$(set +o | grep xtrace)
Dean Troyerbf2ad702015-03-09 15:16:10 -050079 set +o xtrace
80 local file=$1
81 local section=$2
82 local option=$3
83 local values
84
85 values=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { s/^$option[ \t]*=[ \t]*//gp; }" "$file")
86 echo ${values}
87 $xtrace
88}
89
90# Determinate is the given option present in the INI file
91# ini_has_option config-file section option
92function ini_has_option {
Ian Wienand433a9b12015-10-07 13:29:31 +110093 local xtrace
94 xtrace=$(set +o | grep xtrace)
Dean Troyerbf2ad702015-03-09 15:16:10 -050095 set +o xtrace
96 local file=$1
97 local section=$2
98 local option=$3
99 local line
100
101 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
102 $xtrace
103 [ -n "$line" ]
104}
105
106# Add another config line for a multi-line option.
107# It's normally called after iniset of the same option and assumes
108# that the section already exists.
109#
110# Note that iniset_multiline requires all the 'lines' to be supplied
111# in the argument list. Doing that will cause incorrect configuration
112# if spaces are used in the config values.
113#
Ian Wienandf44a0242015-07-22 10:34:47 +1000114# iniadd_literal [-sudo] config-file section option value
Dean Troyerbf2ad702015-03-09 15:16:10 -0500115function iniadd_literal {
Ian Wienand433a9b12015-10-07 13:29:31 +1100116 local xtrace
117 xtrace=$(set +o | grep xtrace)
Dean Troyerbf2ad702015-03-09 15:16:10 -0500118 set +o xtrace
Ian Wienandf44a0242015-07-22 10:34:47 +1000119 local sudo=""
120 if [ $1 == "-sudo" ]; then
121 sudo="sudo "
122 shift
123 fi
Dean Troyerbf2ad702015-03-09 15:16:10 -0500124 local file=$1
125 local section=$2
126 local option=$3
127 local value=$4
128
Ian Wienand92884ed2015-07-22 12:16:45 +1000129 if [[ -z $section || -z $option ]]; then
130 $xtrace
131 return
132 fi
Dean Troyerbf2ad702015-03-09 15:16:10 -0500133
134 # Add it
Ian Wienandf44a0242015-07-22 10:34:47 +1000135 $sudo sed -i -e "/^\[$section\]/ a\\
Dean Troyerbf2ad702015-03-09 15:16:10 -0500136$option = $value
137" "$file"
138
139 $xtrace
140}
141
142# Remove an option from an INI file
Ian Wienandf44a0242015-07-22 10:34:47 +1000143# inidelete [-sudo] config-file section option
Dean Troyerbf2ad702015-03-09 15:16:10 -0500144function inidelete {
Ian Wienand433a9b12015-10-07 13:29:31 +1100145 local xtrace
146 xtrace=$(set +o | grep xtrace)
Dean Troyerbf2ad702015-03-09 15:16:10 -0500147 set +o xtrace
Ian Wienandf44a0242015-07-22 10:34:47 +1000148 local sudo=""
149 if [ $1 == "-sudo" ]; then
150 sudo="sudo "
151 shift
152 fi
Dean Troyerbf2ad702015-03-09 15:16:10 -0500153 local file=$1
154 local section=$2
155 local option=$3
156
Ian Wienand92884ed2015-07-22 12:16:45 +1000157 if [[ -z $section || -z $option ]]; then
158 $xtrace
159 return
160 fi
Dean Troyerbf2ad702015-03-09 15:16:10 -0500161
162 # Remove old values
Ian Wienandf44a0242015-07-22 10:34:47 +1000163 $sudo sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file"
Dean Troyerbf2ad702015-03-09 15:16:10 -0500164
165 $xtrace
166}
167
168# Set an option in an INI file
Ian Wienandf44a0242015-07-22 10:34:47 +1000169# iniset [-sudo] config-file section option value
Ian Wienandcede7872015-07-22 13:36:12 +1000170# - if the file does not exist, it is created
Dean Troyerbf2ad702015-03-09 15:16:10 -0500171function iniset {
Ian Wienand433a9b12015-10-07 13:29:31 +1100172 local xtrace
173 xtrace=$(set +o | grep xtrace)
Dean Troyerbf2ad702015-03-09 15:16:10 -0500174 set +o xtrace
Ian Wienandf44a0242015-07-22 10:34:47 +1000175 local sudo=""
176 if [ $1 == "-sudo" ]; then
177 sudo="sudo "
178 shift
179 fi
Dean Troyerbf2ad702015-03-09 15:16:10 -0500180 local file=$1
181 local section=$2
182 local option=$3
183 local value=$4
184
Ian Wienand92884ed2015-07-22 12:16:45 +1000185 if [[ -z $section || -z $option ]]; then
186 $xtrace
187 return
188 fi
Dean Troyerbf2ad702015-03-09 15:16:10 -0500189
190 if ! grep -q "^\[$section\]" "$file" 2>/dev/null; then
191 # Add section at the end
Ian Wienandf44a0242015-07-22 10:34:47 +1000192 echo -e "\n[$section]" | $sudo tee --append "$file" > /dev/null
Dean Troyerbf2ad702015-03-09 15:16:10 -0500193 fi
194 if ! ini_has_option "$file" "$section" "$option"; then
195 # Add it
Ian Wienandf44a0242015-07-22 10:34:47 +1000196 $sudo sed -i -e "/^\[$section\]/ a\\
Dean Troyerbf2ad702015-03-09 15:16:10 -0500197$option = $value
198" "$file"
199 else
Ian Wienandada886d2015-10-07 14:06:26 +1100200 local sep
201 sep=$(echo -ne "\x01")
Dean Troyerbf2ad702015-03-09 15:16:10 -0500202 # Replace it
Ian Wienandf44a0242015-07-22 10:34:47 +1000203 $sudo sed -i -e '/^\['${section}'\]/,/^\[.*\]/ s'${sep}'^\('${option}'[ \t]*=[ \t]*\).*$'${sep}'\1'"${value}"${sep} "$file"
Dean Troyerbf2ad702015-03-09 15:16:10 -0500204 fi
205 $xtrace
206}
207
208# Set a multiple line option in an INI file
Atsushi SAKAI5509ed52015-11-30 20:20:21 +0900209# iniset_multiline [-sudo] config-file section option value1 value2 value3 ...
Dean Troyerbf2ad702015-03-09 15:16:10 -0500210function iniset_multiline {
Ian Wienand433a9b12015-10-07 13:29:31 +1100211 local xtrace
212 xtrace=$(set +o | grep xtrace)
Dean Troyerbf2ad702015-03-09 15:16:10 -0500213 set +o xtrace
Ian Wienandf44a0242015-07-22 10:34:47 +1000214 local sudo=""
215 if [ $1 == "-sudo" ]; then
216 sudo="sudo "
217 shift
218 fi
Dean Troyerbf2ad702015-03-09 15:16:10 -0500219 local file=$1
220 local section=$2
221 local option=$3
222
223 shift 3
224 local values
225 for v in $@; do
226 # The later sed command inserts each new value in the line next to
227 # the section identifier, which causes the values to be inserted in
228 # the reverse order. Do a reverse here to keep the original order.
229 values="$v ${values}"
230 done
231 if ! grep -q "^\[$section\]" "$file"; then
232 # Add section at the end
Ian Wienandf44a0242015-07-22 10:34:47 +1000233 echo -e "\n[$section]" | $sudo tee --append "$file" > /dev/null
Dean Troyerbf2ad702015-03-09 15:16:10 -0500234 else
235 # Remove old values
Ian Wienandf44a0242015-07-22 10:34:47 +1000236 $sudo sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file"
Dean Troyerbf2ad702015-03-09 15:16:10 -0500237 fi
238 # Add new ones
239 for v in $values; do
Ian Wienandf44a0242015-07-22 10:34:47 +1000240 $sudo sed -i -e "/^\[$section\]/ a\\
Dean Troyerbf2ad702015-03-09 15:16:10 -0500241$option = $v
242" "$file"
243 done
244 $xtrace
245}
246
247# Uncomment an option in an INI file
248# iniuncomment config-file section option
249function iniuncomment {
Ian Wienand433a9b12015-10-07 13:29:31 +1100250 local xtrace
251 xtrace=$(set +o | grep xtrace)
Dean Troyerbf2ad702015-03-09 15:16:10 -0500252 set +o xtrace
Ian Wienandf44a0242015-07-22 10:34:47 +1000253 local sudo=""
254 if [ $1 == "-sudo" ]; then
255 sudo="sudo "
256 shift
257 fi
Dean Troyerbf2ad702015-03-09 15:16:10 -0500258 local file=$1
259 local section=$2
260 local option=$3
Ian Wienandf44a0242015-07-22 10:34:47 +1000261 $sudo sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" "$file"
Dean Troyerbf2ad702015-03-09 15:16:10 -0500262 $xtrace
263}
264
vsaienko135bd482015-12-11 11:03:52 +0200265# Get list of sections from an INI file
266# iniget_sections config-file
267function iniget_sections {
268 local xtrace
269 xtrace=$(set +o | grep xtrace)
270 set +o xtrace
271 local file=$1
272
273 echo $(sed -ne "s/^\[\(.*\)\]/\1/p" "$file")
274 $xtrace
275}
276
Dean Troyerbf2ad702015-03-09 15:16:10 -0500277# Restore xtrace
278$INC_CONF_TRACE
279
280# Local variables:
281# mode: shell-script
282# End: