blob: d2830d79cd153c6948a67c3c35c01e969c040461 [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
33 local values="$(iniget_multiline $file $section $option) $@"
Ian Wienandf44a0242015-07-22 10:34:47 +100034 iniset_multiline $sudo $file $section $option $values
Dean Troyerbf2ad702015-03-09 15:16:10 -050035 $xtrace
36}
37
38# Comment an option in an INI file
Ian Wienandf44a0242015-07-22 10:34:47 +100039# inicomment [-sudo] config-file section option
Dean Troyerbf2ad702015-03-09 15:16:10 -050040function inicomment {
Ian Wienand433a9b12015-10-07 13:29:31 +110041 local xtrace
42 xtrace=$(set +o | grep xtrace)
Dean Troyerbf2ad702015-03-09 15:16:10 -050043 set +o xtrace
Ian Wienandf44a0242015-07-22 10:34:47 +100044 local sudo=""
45 if [ $1 == "-sudo" ]; then
46 sudo="sudo "
47 shift
48 fi
Dean Troyerbf2ad702015-03-09 15:16:10 -050049 local file=$1
50 local section=$2
51 local option=$3
52
Ian Wienandf44a0242015-07-22 10:34:47 +100053 $sudo sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" "$file"
Dean Troyerbf2ad702015-03-09 15:16:10 -050054 $xtrace
55}
56
57# Get an option from an INI file
58# iniget config-file section option
59function iniget {
Ian Wienand433a9b12015-10-07 13:29:31 +110060 local xtrace
61 xtrace=$(set +o | grep xtrace)
Dean Troyerbf2ad702015-03-09 15:16:10 -050062 set +o xtrace
63 local file=$1
64 local section=$2
65 local option=$3
66 local line
67
68 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
69 echo ${line#*=}
70 $xtrace
71}
72
73# Get a multiple line option from an INI file
74# iniget_multiline config-file section option
75function iniget_multiline {
Ian Wienand433a9b12015-10-07 13:29:31 +110076 local xtrace
77 xtrace=$(set +o | grep xtrace)
Dean Troyerbf2ad702015-03-09 15:16:10 -050078 set +o xtrace
79 local file=$1
80 local section=$2
81 local option=$3
82 local values
83
84 values=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { s/^$option[ \t]*=[ \t]*//gp; }" "$file")
85 echo ${values}
86 $xtrace
87}
88
89# Determinate is the given option present in the INI file
90# ini_has_option config-file section option
91function ini_has_option {
Ian Wienand433a9b12015-10-07 13:29:31 +110092 local xtrace
93 xtrace=$(set +o | grep xtrace)
Dean Troyerbf2ad702015-03-09 15:16:10 -050094 set +o xtrace
95 local file=$1
96 local section=$2
97 local option=$3
98 local line
99
100 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
101 $xtrace
102 [ -n "$line" ]
103}
104
105# Add another config line for a multi-line option.
106# It's normally called after iniset of the same option and assumes
107# that the section already exists.
108#
109# Note that iniset_multiline requires all the 'lines' to be supplied
110# in the argument list. Doing that will cause incorrect configuration
111# if spaces are used in the config values.
112#
Ian Wienandf44a0242015-07-22 10:34:47 +1000113# iniadd_literal [-sudo] config-file section option value
Dean Troyerbf2ad702015-03-09 15:16:10 -0500114function iniadd_literal {
Ian Wienand433a9b12015-10-07 13:29:31 +1100115 local xtrace
116 xtrace=$(set +o | grep xtrace)
Dean Troyerbf2ad702015-03-09 15:16:10 -0500117 set +o xtrace
Ian Wienandf44a0242015-07-22 10:34:47 +1000118 local sudo=""
119 if [ $1 == "-sudo" ]; then
120 sudo="sudo "
121 shift
122 fi
Dean Troyerbf2ad702015-03-09 15:16:10 -0500123 local file=$1
124 local section=$2
125 local option=$3
126 local value=$4
127
Ian Wienand92884ed2015-07-22 12:16:45 +1000128 if [[ -z $section || -z $option ]]; then
129 $xtrace
130 return
131 fi
Dean Troyerbf2ad702015-03-09 15:16:10 -0500132
133 # Add it
Ian Wienandf44a0242015-07-22 10:34:47 +1000134 $sudo sed -i -e "/^\[$section\]/ a\\
Dean Troyerbf2ad702015-03-09 15:16:10 -0500135$option = $value
136" "$file"
137
138 $xtrace
139}
140
141# Remove an option from an INI file
Ian Wienandf44a0242015-07-22 10:34:47 +1000142# inidelete [-sudo] config-file section option
Dean Troyerbf2ad702015-03-09 15:16:10 -0500143function inidelete {
Ian Wienand433a9b12015-10-07 13:29:31 +1100144 local xtrace
145 xtrace=$(set +o | grep xtrace)
Dean Troyerbf2ad702015-03-09 15:16:10 -0500146 set +o xtrace
Ian Wienandf44a0242015-07-22 10:34:47 +1000147 local sudo=""
148 if [ $1 == "-sudo" ]; then
149 sudo="sudo "
150 shift
151 fi
Dean Troyerbf2ad702015-03-09 15:16:10 -0500152 local file=$1
153 local section=$2
154 local option=$3
155
Ian Wienand92884ed2015-07-22 12:16:45 +1000156 if [[ -z $section || -z $option ]]; then
157 $xtrace
158 return
159 fi
Dean Troyerbf2ad702015-03-09 15:16:10 -0500160
161 # Remove old values
Ian Wienandf44a0242015-07-22 10:34:47 +1000162 $sudo sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file"
Dean Troyerbf2ad702015-03-09 15:16:10 -0500163
164 $xtrace
165}
166
167# Set an option in an INI file
Ian Wienandf44a0242015-07-22 10:34:47 +1000168# iniset [-sudo] config-file section option value
Ian Wienandcede7872015-07-22 13:36:12 +1000169# - if the file does not exist, it is created
Dean Troyerbf2ad702015-03-09 15:16:10 -0500170function iniset {
Ian Wienand433a9b12015-10-07 13:29:31 +1100171 local xtrace
172 xtrace=$(set +o | grep xtrace)
Dean Troyerbf2ad702015-03-09 15:16:10 -0500173 set +o xtrace
Ian Wienandf44a0242015-07-22 10:34:47 +1000174 local sudo=""
175 if [ $1 == "-sudo" ]; then
176 sudo="sudo "
177 shift
178 fi
Dean Troyerbf2ad702015-03-09 15:16:10 -0500179 local file=$1
180 local section=$2
181 local option=$3
182 local value=$4
183
Ian Wienand92884ed2015-07-22 12:16:45 +1000184 if [[ -z $section || -z $option ]]; then
185 $xtrace
186 return
187 fi
Dean Troyerbf2ad702015-03-09 15:16:10 -0500188
189 if ! grep -q "^\[$section\]" "$file" 2>/dev/null; then
190 # Add section at the end
Ian Wienandf44a0242015-07-22 10:34:47 +1000191 echo -e "\n[$section]" | $sudo tee --append "$file" > /dev/null
Dean Troyerbf2ad702015-03-09 15:16:10 -0500192 fi
193 if ! ini_has_option "$file" "$section" "$option"; then
194 # Add it
Ian Wienandf44a0242015-07-22 10:34:47 +1000195 $sudo sed -i -e "/^\[$section\]/ a\\
Dean Troyerbf2ad702015-03-09 15:16:10 -0500196$option = $value
197" "$file"
198 else
Ian Wienandada886d2015-10-07 14:06:26 +1100199 local sep
200 sep=$(echo -ne "\x01")
Dean Troyerbf2ad702015-03-09 15:16:10 -0500201 # Replace it
Ian Wienandf44a0242015-07-22 10:34:47 +1000202 $sudo sed -i -e '/^\['${section}'\]/,/^\[.*\]/ s'${sep}'^\('${option}'[ \t]*=[ \t]*\).*$'${sep}'\1'"${value}"${sep} "$file"
Dean Troyerbf2ad702015-03-09 15:16:10 -0500203 fi
204 $xtrace
205}
206
207# Set a multiple line option in an INI file
Atsushi SAKAI5509ed52015-11-30 20:20:21 +0900208# iniset_multiline [-sudo] config-file section option value1 value2 value3 ...
Dean Troyerbf2ad702015-03-09 15:16:10 -0500209function iniset_multiline {
Ian Wienand433a9b12015-10-07 13:29:31 +1100210 local xtrace
211 xtrace=$(set +o | grep xtrace)
Dean Troyerbf2ad702015-03-09 15:16:10 -0500212 set +o xtrace
Ian Wienandf44a0242015-07-22 10:34:47 +1000213 local sudo=""
214 if [ $1 == "-sudo" ]; then
215 sudo="sudo "
216 shift
217 fi
Dean Troyerbf2ad702015-03-09 15:16:10 -0500218 local file=$1
219 local section=$2
220 local option=$3
221
222 shift 3
223 local values
224 for v in $@; do
225 # The later sed command inserts each new value in the line next to
226 # the section identifier, which causes the values to be inserted in
227 # the reverse order. Do a reverse here to keep the original order.
228 values="$v ${values}"
229 done
230 if ! grep -q "^\[$section\]" "$file"; then
231 # Add section at the end
Ian Wienandf44a0242015-07-22 10:34:47 +1000232 echo -e "\n[$section]" | $sudo tee --append "$file" > /dev/null
Dean Troyerbf2ad702015-03-09 15:16:10 -0500233 else
234 # Remove old values
Ian Wienandf44a0242015-07-22 10:34:47 +1000235 $sudo sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file"
Dean Troyerbf2ad702015-03-09 15:16:10 -0500236 fi
237 # Add new ones
238 for v in $values; do
Ian Wienandf44a0242015-07-22 10:34:47 +1000239 $sudo sed -i -e "/^\[$section\]/ a\\
Dean Troyerbf2ad702015-03-09 15:16:10 -0500240$option = $v
241" "$file"
242 done
243 $xtrace
244}
245
246# Uncomment an option in an INI file
247# iniuncomment config-file section option
248function iniuncomment {
Ian Wienand433a9b12015-10-07 13:29:31 +1100249 local xtrace
250 xtrace=$(set +o | grep xtrace)
Dean Troyerbf2ad702015-03-09 15:16:10 -0500251 set +o xtrace
Ian Wienandf44a0242015-07-22 10:34:47 +1000252 local sudo=""
253 if [ $1 == "-sudo" ]; then
254 sudo="sudo "
255 shift
256 fi
Dean Troyerbf2ad702015-03-09 15:16:10 -0500257 local file=$1
258 local section=$2
259 local option=$3
Ian Wienandf44a0242015-07-22 10:34:47 +1000260 $sudo sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" "$file"
Dean Troyerbf2ad702015-03-09 15:16:10 -0500261 $xtrace
262}
263
Dean Troyerbf2ad702015-03-09 15:16:10 -0500264# Restore xtrace
265$INC_CONF_TRACE
266
267# Local variables:
268# mode: shell-script
269# End: