blob: ba2d827ae9eee7e45b2a662b72b30ca2145e1aa3 [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
199 local sep=$(echo -ne "\x01")
200 # Replace it
Ian Wienandf44a0242015-07-22 10:34:47 +1000201 $sudo sed -i -e '/^\['${section}'\]/,/^\[.*\]/ s'${sep}'^\('${option}'[ \t]*=[ \t]*\).*$'${sep}'\1'"${value}"${sep} "$file"
Dean Troyerbf2ad702015-03-09 15:16:10 -0500202 fi
203 $xtrace
204}
205
206# Set a multiple line option in an INI file
Ian Wienandf44a0242015-07-22 10:34:47 +1000207# iniset_multiline [-sudo] config-file section option value1 value2 valu3 ...
Dean Troyerbf2ad702015-03-09 15:16:10 -0500208function iniset_multiline {
Ian Wienand433a9b12015-10-07 13:29:31 +1100209 local xtrace
210 xtrace=$(set +o | grep xtrace)
Dean Troyerbf2ad702015-03-09 15:16:10 -0500211 set +o xtrace
Ian Wienandf44a0242015-07-22 10:34:47 +1000212 local sudo=""
213 if [ $1 == "-sudo" ]; then
214 sudo="sudo "
215 shift
216 fi
Dean Troyerbf2ad702015-03-09 15:16:10 -0500217 local file=$1
218 local section=$2
219 local option=$3
220
221 shift 3
222 local values
223 for v in $@; do
224 # The later sed command inserts each new value in the line next to
225 # the section identifier, which causes the values to be inserted in
226 # the reverse order. Do a reverse here to keep the original order.
227 values="$v ${values}"
228 done
229 if ! grep -q "^\[$section\]" "$file"; then
230 # Add section at the end
Ian Wienandf44a0242015-07-22 10:34:47 +1000231 echo -e "\n[$section]" | $sudo tee --append "$file" > /dev/null
Dean Troyerbf2ad702015-03-09 15:16:10 -0500232 else
233 # Remove old values
Ian Wienandf44a0242015-07-22 10:34:47 +1000234 $sudo sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file"
Dean Troyerbf2ad702015-03-09 15:16:10 -0500235 fi
236 # Add new ones
237 for v in $values; do
Ian Wienandf44a0242015-07-22 10:34:47 +1000238 $sudo sed -i -e "/^\[$section\]/ a\\
Dean Troyerbf2ad702015-03-09 15:16:10 -0500239$option = $v
240" "$file"
241 done
242 $xtrace
243}
244
245# Uncomment an option in an INI file
246# iniuncomment config-file section option
247function iniuncomment {
Ian Wienand433a9b12015-10-07 13:29:31 +1100248 local xtrace
249 xtrace=$(set +o | grep xtrace)
Dean Troyerbf2ad702015-03-09 15:16:10 -0500250 set +o xtrace
Ian Wienandf44a0242015-07-22 10:34:47 +1000251 local sudo=""
252 if [ $1 == "-sudo" ]; then
253 sudo="sudo "
254 shift
255 fi
Dean Troyerbf2ad702015-03-09 15:16:10 -0500256 local file=$1
257 local section=$2
258 local option=$3
Ian Wienandf44a0242015-07-22 10:34:47 +1000259 $sudo sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" "$file"
Dean Troyerbf2ad702015-03-09 15:16:10 -0500260 $xtrace
261}
262
Dean Troyerbf2ad702015-03-09 15:16:10 -0500263# Restore xtrace
264$INC_CONF_TRACE
265
266# Local variables:
267# mode: shell-script
268# End: