blob: 79936823d206003d231ee740b1f74aa97b8bb85c [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
Yi Wang698796f2018-12-14 10:35:26 +080091# ini_has_option [-sudo] config-file section option
Dean Troyerbf2ad702015-03-09 15:16:10 -050092function 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
Yi Wang698796f2018-12-14 10:35:26 +080096 local sudo=""
97 if [ $1 == "-sudo" ]; then
98 sudo="sudo "
99 shift
100 fi
Dean Troyerbf2ad702015-03-09 15:16:10 -0500101 local file=$1
102 local section=$2
103 local option=$3
104 local line
105
Yi Wang698796f2018-12-14 10:35:26 +0800106 line=$($sudo sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
Dean Troyerbf2ad702015-03-09 15:16:10 -0500107 $xtrace
108 [ -n "$line" ]
109}
110
111# Add another config line for a multi-line option.
112# It's normally called after iniset of the same option and assumes
113# that the section already exists.
114#
115# Note that iniset_multiline requires all the 'lines' to be supplied
116# in the argument list. Doing that will cause incorrect configuration
117# if spaces are used in the config values.
118#
Ian Wienandf44a0242015-07-22 10:34:47 +1000119# iniadd_literal [-sudo] config-file section option value
Dean Troyerbf2ad702015-03-09 15:16:10 -0500120function iniadd_literal {
Ian Wienand433a9b12015-10-07 13:29:31 +1100121 local xtrace
122 xtrace=$(set +o | grep xtrace)
Dean Troyerbf2ad702015-03-09 15:16:10 -0500123 set +o xtrace
Ian Wienandf44a0242015-07-22 10:34:47 +1000124 local sudo=""
125 if [ $1 == "-sudo" ]; then
126 sudo="sudo "
127 shift
128 fi
Dean Troyerbf2ad702015-03-09 15:16:10 -0500129 local file=$1
130 local section=$2
131 local option=$3
132 local value=$4
133
Ian Wienand92884ed2015-07-22 12:16:45 +1000134 if [[ -z $section || -z $option ]]; then
135 $xtrace
136 return
137 fi
Dean Troyerbf2ad702015-03-09 15:16:10 -0500138
139 # Add it
Ian Wienandf44a0242015-07-22 10:34:47 +1000140 $sudo sed -i -e "/^\[$section\]/ a\\
Dean Troyerbf2ad702015-03-09 15:16:10 -0500141$option = $value
142" "$file"
143
144 $xtrace
145}
146
147# Remove an option from an INI file
Ian Wienandf44a0242015-07-22 10:34:47 +1000148# inidelete [-sudo] config-file section option
Dean Troyerbf2ad702015-03-09 15:16:10 -0500149function inidelete {
Ian Wienand433a9b12015-10-07 13:29:31 +1100150 local xtrace
151 xtrace=$(set +o | grep xtrace)
Dean Troyerbf2ad702015-03-09 15:16:10 -0500152 set +o xtrace
Ian Wienandf44a0242015-07-22 10:34:47 +1000153 local sudo=""
154 if [ $1 == "-sudo" ]; then
155 sudo="sudo "
156 shift
157 fi
Dean Troyerbf2ad702015-03-09 15:16:10 -0500158 local file=$1
159 local section=$2
160 local option=$3
161
Ian Wienand92884ed2015-07-22 12:16:45 +1000162 if [[ -z $section || -z $option ]]; then
163 $xtrace
164 return
165 fi
Dean Troyerbf2ad702015-03-09 15:16:10 -0500166
167 # Remove old values
Ian Wienandf44a0242015-07-22 10:34:47 +1000168 $sudo sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file"
Dean Troyerbf2ad702015-03-09 15:16:10 -0500169
170 $xtrace
171}
172
173# Set an option in an INI file
Ian Wienandf44a0242015-07-22 10:34:47 +1000174# iniset [-sudo] config-file section option value
Ian Wienandcede7872015-07-22 13:36:12 +1000175# - if the file does not exist, it is created
Dean Troyerbf2ad702015-03-09 15:16:10 -0500176function iniset {
Ian Wienand433a9b12015-10-07 13:29:31 +1100177 local xtrace
178 xtrace=$(set +o | grep xtrace)
Dean Troyerbf2ad702015-03-09 15:16:10 -0500179 set +o xtrace
Ian Wienandf44a0242015-07-22 10:34:47 +1000180 local sudo=""
Yi Wang698796f2018-12-14 10:35:26 +0800181 local sudo_option=""
Ian Wienandf44a0242015-07-22 10:34:47 +1000182 if [ $1 == "-sudo" ]; then
183 sudo="sudo "
Yi Wang698796f2018-12-14 10:35:26 +0800184 sudo_option="-sudo "
Ian Wienandf44a0242015-07-22 10:34:47 +1000185 shift
186 fi
Dean Troyerbf2ad702015-03-09 15:16:10 -0500187 local file=$1
188 local section=$2
189 local option=$3
190 local value=$4
191
Ian Wienand92884ed2015-07-22 12:16:45 +1000192 if [[ -z $section || -z $option ]]; then
193 $xtrace
194 return
195 fi
Dean Troyerbf2ad702015-03-09 15:16:10 -0500196
Yi Wang698796f2018-12-14 10:35:26 +0800197 if ! $sudo grep -q "^\[$section\]" "$file" 2>/dev/null; then
Dean Troyerbf2ad702015-03-09 15:16:10 -0500198 # Add section at the end
Ian Wienandf44a0242015-07-22 10:34:47 +1000199 echo -e "\n[$section]" | $sudo tee --append "$file" > /dev/null
Dean Troyerbf2ad702015-03-09 15:16:10 -0500200 fi
Yi Wang698796f2018-12-14 10:35:26 +0800201 if ! ini_has_option $sudo_option "$file" "$section" "$option"; then
Dean Troyerbf2ad702015-03-09 15:16:10 -0500202 # Add it
Ian Wienandf44a0242015-07-22 10:34:47 +1000203 $sudo sed -i -e "/^\[$section\]/ a\\
Dean Troyerbf2ad702015-03-09 15:16:10 -0500204$option = $value
205" "$file"
206 else
Ian Wienandada886d2015-10-07 14:06:26 +1100207 local sep
208 sep=$(echo -ne "\x01")
Dean Troyerbf2ad702015-03-09 15:16:10 -0500209 # Replace it
Luigi Toscanoc7c67652018-06-04 10:59:57 +0200210 $sudo sed -i -e '/^\['${section}'\]/,/^\[.*\]/ s'${sep}'^\('"${option}"'[ \t]*=[ \t]*\).*$'${sep}'\1'"${value}"${sep} "$file"
Dean Troyerbf2ad702015-03-09 15:16:10 -0500211 fi
212 $xtrace
213}
214
215# Set a multiple line option in an INI file
Atsushi SAKAI5509ed52015-11-30 20:20:21 +0900216# iniset_multiline [-sudo] config-file section option value1 value2 value3 ...
Dean Troyerbf2ad702015-03-09 15:16:10 -0500217function iniset_multiline {
Ian Wienand433a9b12015-10-07 13:29:31 +1100218 local xtrace
219 xtrace=$(set +o | grep xtrace)
Dean Troyerbf2ad702015-03-09 15:16:10 -0500220 set +o xtrace
Ian Wienandf44a0242015-07-22 10:34:47 +1000221 local sudo=""
222 if [ $1 == "-sudo" ]; then
223 sudo="sudo "
224 shift
225 fi
Dean Troyerbf2ad702015-03-09 15:16:10 -0500226 local file=$1
227 local section=$2
228 local option=$3
229
230 shift 3
231 local values
232 for v in $@; do
233 # The later sed command inserts each new value in the line next to
234 # the section identifier, which causes the values to be inserted in
235 # the reverse order. Do a reverse here to keep the original order.
236 values="$v ${values}"
237 done
Yi Wang698796f2018-12-14 10:35:26 +0800238 if ! $sudo grep -q "^\[$section\]" "$file"; then
Dean Troyerbf2ad702015-03-09 15:16:10 -0500239 # Add section at the end
Ian Wienandf44a0242015-07-22 10:34:47 +1000240 echo -e "\n[$section]" | $sudo tee --append "$file" > /dev/null
Dean Troyerbf2ad702015-03-09 15:16:10 -0500241 else
242 # Remove old values
Ian Wienandf44a0242015-07-22 10:34:47 +1000243 $sudo sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file"
Dean Troyerbf2ad702015-03-09 15:16:10 -0500244 fi
245 # Add new ones
246 for v in $values; do
Ian Wienandf44a0242015-07-22 10:34:47 +1000247 $sudo sed -i -e "/^\[$section\]/ a\\
Dean Troyerbf2ad702015-03-09 15:16:10 -0500248$option = $v
249" "$file"
250 done
251 $xtrace
252}
253
254# Uncomment an option in an INI file
255# iniuncomment config-file section option
256function iniuncomment {
Ian Wienand433a9b12015-10-07 13:29:31 +1100257 local xtrace
258 xtrace=$(set +o | grep xtrace)
Dean Troyerbf2ad702015-03-09 15:16:10 -0500259 set +o xtrace
Ian Wienandf44a0242015-07-22 10:34:47 +1000260 local sudo=""
261 if [ $1 == "-sudo" ]; then
262 sudo="sudo "
263 shift
264 fi
Dean Troyerbf2ad702015-03-09 15:16:10 -0500265 local file=$1
266 local section=$2
267 local option=$3
Ian Wienandf44a0242015-07-22 10:34:47 +1000268 $sudo sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" "$file"
Dean Troyerbf2ad702015-03-09 15:16:10 -0500269 $xtrace
270}
271
vsaienko135bd482015-12-11 11:03:52 +0200272# Get list of sections from an INI file
273# iniget_sections config-file
274function iniget_sections {
275 local xtrace
276 xtrace=$(set +o | grep xtrace)
277 set +o xtrace
278 local file=$1
279
280 echo $(sed -ne "s/^\[\(.*\)\]/\1/p" "$file")
281 $xtrace
282}
283
Sean Daguebb357152016-06-07 11:20:55 -0400284# Set a localrc var
285function localrc_set {
286 local file=$1
287 local group="local"
288 local conf="localrc"
289 local section=""
290 local option=$2
291 local value=$3
292 localconf_set "$file" "$group" "$conf" "$section" "$option" "$value"
293}
294
295# Check if local.conf has section.
296function localconf_has_section {
297 local file=$1
298 local group=$2
299 local conf=$3
300 local section=$4
301 local sep
302 sep=$(echo -ne "\x01")
303 local line
304 line=$(sed -ne "\\${sep}^\[\[${group}|${conf}\]\]${sep},\\${sep}\[\[.*\]\]${sep}{
305 /\[${section}\]/p
306 }" "$file")
307 [ -n "$line" ]
308}
309
310# Check if local.conf has option.
311function localconf_has_option {
312 local file=$1
313 local group=$2
314 local conf=$3
315 local section=$4
316 local option=$5
317 local sep
318 sep=$(echo -ne "\x01")
319 local line
320 if [[ -z "$section" ]]; then
321 line=$(sed -ne "\\${sep}^\[\[${group}|${conf}\]\]${sep},\\${sep}\[\[.*\]\]${sep}{
322 /${option}[ \t]*=.*$/p
323 }" "$file")
324 else
325 line=$(sed -ne "\\${sep}^\[\[${group}|${conf}\]\]${sep},\\${sep}\[\[.*\]\]${sep}{
326 /\[${section}\]/,/\[\[.*\]\]\|\[.*\]/{
327 /${option}[ \t]*=.*$/p}
328 }" "$file")
329 fi
330 [ -n "$line" ]
331}
332
333# Update option in local.conf.
334function localconf_update_option {
335 local sudo=$1
336 local file=$2
337 local group=$3
338 local conf=$4
339 local section=$5
340 local option=$6
341 local value=$7
342 local sep
343 sep=$(echo -ne "\x01")
344 if [[ -z "$section" ]]; then
345 $sudo sed -i -e "\\${sep}^\[\[${group}|${conf}\]\]${sep},\\${sep}\[\[.*\]\]${sep}{
346 s${sep}^\(${option}[ \t]*=[ \t]*\).*\$${sep}\1${value}${sep}
347 }" "$file"
348 else
349 $sudo sed -i -e "\\${sep}^\[\[${group}|${conf}\]\]${sep},\\${sep}\[\[.*\]\]${sep}{
350 /\[${section}\]/,/\[\[.*\]\]\|\[.*\]/s${sep}^\(${option}[ \t]*=[ \t]*\).*\$${sep}\1${value}${sep}
351 }" "$file"
352 fi
353}
354
355# Add option in local.conf.
356function localconf_add_option {
357 local sudo=$1
358 local file=$2
359 local group=$3
360 local conf=$4
361 local section=$5
362 local option=$6
363 local value=$7
364 local sep
365 sep=$(echo -ne "\x01")
366 if [[ -z "$section" ]]; then
367 $sudo sed -i -e "\\${sep}^\[\[${group}|${conf}\]\]${sep} a $option=$value" "$file"
368 else
369 $sudo sed -i -e "\\${sep}^\[\[${group}|${conf}\]\]${sep},\\${sep}\[\[.*\]\]${sep}{
370 /\[${section}\]/ a $option=$value
371 }" "$file"
372 fi
373}
374
375# Add section and option in local.conf.
376function localconf_add_section_and_option {
377 local sudo=$1
378 local file=$2
379 local group=$3
380 local conf=$4
381 local section=$5
382 local option=$6
383 local value=$7
384 local sep
385 sep=$(echo -ne "\x01")
386 $sudo sed -i -e "\\${sep}^\[\[${group}|${conf}\]\]${sep} {
387 a [$section]
388 a $option=$value
389 }" "$file"
390}
391
392# Set an option in a local.conf file.
393# localconf_set [-sudo] config-file group conf-name section option value
394# - if the file does not exist, it is created
395function localconf_set {
396 local xtrace
397 xtrace=$(set +o | grep xtrace)
398 set +o xtrace
399 local sep
400 sep=$(echo -ne "\x01")
401 local sudo=""
402 if [ $1 == "-sudo" ]; then
403 sudo="sudo "
404 shift
405 fi
406 local file=$1
407 local group=$2
408 local conf=$3
409 local section=$4
410 local option=$5
411 local value=$6
412
413 if [[ -z $group || -z $conf || -z $option || -z $value ]]; then
414 $xtrace
415 return
416 fi
417
418 if ! grep -q "^\[\[${group}|${conf}\]\]" "$file" 2>/dev/null; then
419 # Add meta section at the end if it does not exist
420 echo -e "\n[[${group}|${conf}]]" | $sudo tee --append "$file" > /dev/null
421 # Add section at the end
422 if [[ -n "$section" ]]; then
423 echo -e "[$section]" | $sudo tee --append "$file" > /dev/null
424 fi
425 # Add option at the end
426 echo -e "$option=$value" | $sudo tee --append "$file" > /dev/null
427 elif [[ -z "$section" ]]; then
428 if ! localconf_has_option "$file" "$group" "$conf" "$section" "$option"; then
429 # Add option
430 localconf_add_option "$sudo" "$file" "$group" "$conf" "$section" "$option" "$value"
431 else
432 # Replace it
433 localconf_update_option "$sudo" "$file" "$group" "$conf" "$section" "$option" "$value"
434 fi
435 elif ! localconf_has_section "$file" "$group" "$conf" "$section"; then
436 # Add section and option in specified meta section
437 localconf_add_section_and_option "$sudo" "$file" "$group" "$conf" "$section" "$option" "$value"
438 elif ! localconf_has_option "$file" "$group" "$conf" "$section" "$option"; then
439 # Add option
440 localconf_add_option "$sudo" "$file" "$group" "$conf" "$section" "$option" "$value"
441 else
442 # Replace it
443 localconf_update_option "$sudo" "$file" "$group" "$conf" "$section" "$option" "$value"
444 fi
445 $xtrace
446}
447
Dean Troyerbf2ad702015-03-09 15:16:10 -0500448# Restore xtrace
449$INC_CONF_TRACE
450
451# Local variables:
452# mode: shell-script
453# End: