blob: 68d48d197b4c8642af81c22c9942afb971d80b4b [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
Sean Daguebb357152016-06-07 11:20:55 -0400277# Set a localrc var
278function localrc_set {
279 local file=$1
280 local group="local"
281 local conf="localrc"
282 local section=""
283 local option=$2
284 local value=$3
285 localconf_set "$file" "$group" "$conf" "$section" "$option" "$value"
286}
287
288# Check if local.conf has section.
289function localconf_has_section {
290 local file=$1
291 local group=$2
292 local conf=$3
293 local section=$4
294 local sep
295 sep=$(echo -ne "\x01")
296 local line
297 line=$(sed -ne "\\${sep}^\[\[${group}|${conf}\]\]${sep},\\${sep}\[\[.*\]\]${sep}{
298 /\[${section}\]/p
299 }" "$file")
300 [ -n "$line" ]
301}
302
303# Check if local.conf has option.
304function localconf_has_option {
305 local file=$1
306 local group=$2
307 local conf=$3
308 local section=$4
309 local option=$5
310 local sep
311 sep=$(echo -ne "\x01")
312 local line
313 if [[ -z "$section" ]]; then
314 line=$(sed -ne "\\${sep}^\[\[${group}|${conf}\]\]${sep},\\${sep}\[\[.*\]\]${sep}{
315 /${option}[ \t]*=.*$/p
316 }" "$file")
317 else
318 line=$(sed -ne "\\${sep}^\[\[${group}|${conf}\]\]${sep},\\${sep}\[\[.*\]\]${sep}{
319 /\[${section}\]/,/\[\[.*\]\]\|\[.*\]/{
320 /${option}[ \t]*=.*$/p}
321 }" "$file")
322 fi
323 [ -n "$line" ]
324}
325
326# Update option in local.conf.
327function localconf_update_option {
328 local sudo=$1
329 local file=$2
330 local group=$3
331 local conf=$4
332 local section=$5
333 local option=$6
334 local value=$7
335 local sep
336 sep=$(echo -ne "\x01")
337 if [[ -z "$section" ]]; then
338 $sudo sed -i -e "\\${sep}^\[\[${group}|${conf}\]\]${sep},\\${sep}\[\[.*\]\]${sep}{
339 s${sep}^\(${option}[ \t]*=[ \t]*\).*\$${sep}\1${value}${sep}
340 }" "$file"
341 else
342 $sudo sed -i -e "\\${sep}^\[\[${group}|${conf}\]\]${sep},\\${sep}\[\[.*\]\]${sep}{
343 /\[${section}\]/,/\[\[.*\]\]\|\[.*\]/s${sep}^\(${option}[ \t]*=[ \t]*\).*\$${sep}\1${value}${sep}
344 }" "$file"
345 fi
346}
347
348# Add option in local.conf.
349function localconf_add_option {
350 local sudo=$1
351 local file=$2
352 local group=$3
353 local conf=$4
354 local section=$5
355 local option=$6
356 local value=$7
357 local sep
358 sep=$(echo -ne "\x01")
359 if [[ -z "$section" ]]; then
360 $sudo sed -i -e "\\${sep}^\[\[${group}|${conf}\]\]${sep} a $option=$value" "$file"
361 else
362 $sudo sed -i -e "\\${sep}^\[\[${group}|${conf}\]\]${sep},\\${sep}\[\[.*\]\]${sep}{
363 /\[${section}\]/ a $option=$value
364 }" "$file"
365 fi
366}
367
368# Add section and option in local.conf.
369function localconf_add_section_and_option {
370 local sudo=$1
371 local file=$2
372 local group=$3
373 local conf=$4
374 local section=$5
375 local option=$6
376 local value=$7
377 local sep
378 sep=$(echo -ne "\x01")
379 $sudo sed -i -e "\\${sep}^\[\[${group}|${conf}\]\]${sep} {
380 a [$section]
381 a $option=$value
382 }" "$file"
383}
384
385# Set an option in a local.conf file.
386# localconf_set [-sudo] config-file group conf-name section option value
387# - if the file does not exist, it is created
388function localconf_set {
389 local xtrace
390 xtrace=$(set +o | grep xtrace)
391 set +o xtrace
392 local sep
393 sep=$(echo -ne "\x01")
394 local sudo=""
395 if [ $1 == "-sudo" ]; then
396 sudo="sudo "
397 shift
398 fi
399 local file=$1
400 local group=$2
401 local conf=$3
402 local section=$4
403 local option=$5
404 local value=$6
405
406 if [[ -z $group || -z $conf || -z $option || -z $value ]]; then
407 $xtrace
408 return
409 fi
410
411 if ! grep -q "^\[\[${group}|${conf}\]\]" "$file" 2>/dev/null; then
412 # Add meta section at the end if it does not exist
413 echo -e "\n[[${group}|${conf}]]" | $sudo tee --append "$file" > /dev/null
414 # Add section at the end
415 if [[ -n "$section" ]]; then
416 echo -e "[$section]" | $sudo tee --append "$file" > /dev/null
417 fi
418 # Add option at the end
419 echo -e "$option=$value" | $sudo tee --append "$file" > /dev/null
420 elif [[ -z "$section" ]]; then
421 if ! localconf_has_option "$file" "$group" "$conf" "$section" "$option"; then
422 # Add option
423 localconf_add_option "$sudo" "$file" "$group" "$conf" "$section" "$option" "$value"
424 else
425 # Replace it
426 localconf_update_option "$sudo" "$file" "$group" "$conf" "$section" "$option" "$value"
427 fi
428 elif ! localconf_has_section "$file" "$group" "$conf" "$section"; then
429 # Add section and option in specified meta section
430 localconf_add_section_and_option "$sudo" "$file" "$group" "$conf" "$section" "$option" "$value"
431 elif ! localconf_has_option "$file" "$group" "$conf" "$section" "$option"; then
432 # Add option
433 localconf_add_option "$sudo" "$file" "$group" "$conf" "$section" "$option" "$value"
434 else
435 # Replace it
436 localconf_update_option "$sudo" "$file" "$group" "$conf" "$section" "$option" "$value"
437 fi
438 $xtrace
439}
440
Dean Troyerbf2ad702015-03-09 15:16:10 -0500441# Restore xtrace
442$INC_CONF_TRACE
443
444# Local variables:
445# mode: shell-script
446# End: