blob: d23f4743f1d2812a0e3d5df4e7803e5b0ad13135 [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 {
20 local xtrace=$(set +o | grep xtrace)
21 set +o xtrace
Ian Wienandf44a0242015-07-22 10:34:47 +100022 local sudo=""
23 if [ $1 == "-sudo" ]; then
24 sudo="-sudo "
25 shift
26 fi
Dean Troyerbf2ad702015-03-09 15:16:10 -050027 local file=$1
28 local section=$2
29 local option=$3
30 shift 3
31
32 local values="$(iniget_multiline $file $section $option) $@"
Ian Wienandf44a0242015-07-22 10:34:47 +100033 iniset_multiline $sudo $file $section $option $values
Dean Troyerbf2ad702015-03-09 15:16:10 -050034 $xtrace
35}
36
37# Comment an option in an INI file
Ian Wienandf44a0242015-07-22 10:34:47 +100038# inicomment [-sudo] config-file section option
Dean Troyerbf2ad702015-03-09 15:16:10 -050039function inicomment {
40 local xtrace=$(set +o | grep xtrace)
41 set +o xtrace
Ian Wienandf44a0242015-07-22 10:34:47 +100042 local sudo=""
43 if [ $1 == "-sudo" ]; then
44 sudo="sudo "
45 shift
46 fi
Dean Troyerbf2ad702015-03-09 15:16:10 -050047 local file=$1
48 local section=$2
49 local option=$3
50
Ian Wienandf44a0242015-07-22 10:34:47 +100051 $sudo sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" "$file"
Dean Troyerbf2ad702015-03-09 15:16:10 -050052 $xtrace
53}
54
55# Get an option from an INI file
56# iniget config-file section option
57function iniget {
58 local xtrace=$(set +o | grep xtrace)
59 set +o xtrace
60 local file=$1
61 local section=$2
62 local option=$3
63 local line
64
65 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
66 echo ${line#*=}
67 $xtrace
68}
69
70# Get a multiple line option from an INI file
71# iniget_multiline config-file section option
72function iniget_multiline {
73 local xtrace=$(set +o | grep xtrace)
74 set +o xtrace
75 local file=$1
76 local section=$2
77 local option=$3
78 local values
79
80 values=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { s/^$option[ \t]*=[ \t]*//gp; }" "$file")
81 echo ${values}
82 $xtrace
83}
84
85# Determinate is the given option present in the INI file
86# ini_has_option config-file section option
87function ini_has_option {
88 local xtrace=$(set +o | grep xtrace)
89 set +o xtrace
90 local file=$1
91 local section=$2
92 local option=$3
93 local line
94
95 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
96 $xtrace
97 [ -n "$line" ]
98}
99
100# Add another config line for a multi-line option.
101# It's normally called after iniset of the same option and assumes
102# that the section already exists.
103#
104# Note that iniset_multiline requires all the 'lines' to be supplied
105# in the argument list. Doing that will cause incorrect configuration
106# if spaces are used in the config values.
107#
Ian Wienandf44a0242015-07-22 10:34:47 +1000108# iniadd_literal [-sudo] config-file section option value
Dean Troyerbf2ad702015-03-09 15:16:10 -0500109function iniadd_literal {
110 local xtrace=$(set +o | grep xtrace)
111 set +o xtrace
Ian Wienandf44a0242015-07-22 10:34:47 +1000112 local sudo=""
113 if [ $1 == "-sudo" ]; then
114 sudo="sudo "
115 shift
116 fi
Dean Troyerbf2ad702015-03-09 15:16:10 -0500117 local file=$1
118 local section=$2
119 local option=$3
120 local value=$4
121
Ian Wienand92884ed2015-07-22 12:16:45 +1000122 if [[ -z $section || -z $option ]]; then
123 $xtrace
124 return
125 fi
Dean Troyerbf2ad702015-03-09 15:16:10 -0500126
127 # Add it
Ian Wienandf44a0242015-07-22 10:34:47 +1000128 $sudo sed -i -e "/^\[$section\]/ a\\
Dean Troyerbf2ad702015-03-09 15:16:10 -0500129$option = $value
130" "$file"
131
132 $xtrace
133}
134
135# Remove an option from an INI file
Ian Wienandf44a0242015-07-22 10:34:47 +1000136# inidelete [-sudo] config-file section option
Dean Troyerbf2ad702015-03-09 15:16:10 -0500137function inidelete {
138 local xtrace=$(set +o | grep xtrace)
139 set +o xtrace
Ian Wienandf44a0242015-07-22 10:34:47 +1000140 local sudo=""
141 if [ $1 == "-sudo" ]; then
142 sudo="sudo "
143 shift
144 fi
Dean Troyerbf2ad702015-03-09 15:16:10 -0500145 local file=$1
146 local section=$2
147 local option=$3
148
Ian Wienand92884ed2015-07-22 12:16:45 +1000149 if [[ -z $section || -z $option ]]; then
150 $xtrace
151 return
152 fi
Dean Troyerbf2ad702015-03-09 15:16:10 -0500153
154 # Remove old values
Ian Wienandf44a0242015-07-22 10:34:47 +1000155 $sudo sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file"
Dean Troyerbf2ad702015-03-09 15:16:10 -0500156
157 $xtrace
158}
159
160# Set an option in an INI file
Ian Wienandf44a0242015-07-22 10:34:47 +1000161# iniset [-sudo] config-file section option value
Dean Troyerbf2ad702015-03-09 15:16:10 -0500162function iniset {
163 local xtrace=$(set +o | grep xtrace)
164 set +o xtrace
Ian Wienandf44a0242015-07-22 10:34:47 +1000165 local sudo=""
166 if [ $1 == "-sudo" ]; then
167 sudo="sudo "
168 shift
169 fi
Dean Troyerbf2ad702015-03-09 15:16:10 -0500170 local file=$1
171 local section=$2
172 local option=$3
173 local value=$4
174
Ian Wienand92884ed2015-07-22 12:16:45 +1000175 if [[ -z $section || -z $option ]]; then
176 $xtrace
177 return
178 fi
Dean Troyerbf2ad702015-03-09 15:16:10 -0500179
180 if ! grep -q "^\[$section\]" "$file" 2>/dev/null; then
181 # Add section at the end
Ian Wienandf44a0242015-07-22 10:34:47 +1000182 echo -e "\n[$section]" | $sudo tee --append "$file" > /dev/null
Dean Troyerbf2ad702015-03-09 15:16:10 -0500183 fi
184 if ! ini_has_option "$file" "$section" "$option"; then
185 # Add it
Ian Wienandf44a0242015-07-22 10:34:47 +1000186 $sudo sed -i -e "/^\[$section\]/ a\\
Dean Troyerbf2ad702015-03-09 15:16:10 -0500187$option = $value
188" "$file"
189 else
190 local sep=$(echo -ne "\x01")
191 # Replace it
Ian Wienandf44a0242015-07-22 10:34:47 +1000192 $sudo sed -i -e '/^\['${section}'\]/,/^\[.*\]/ s'${sep}'^\('${option}'[ \t]*=[ \t]*\).*$'${sep}'\1'"${value}"${sep} "$file"
Dean Troyerbf2ad702015-03-09 15:16:10 -0500193 fi
194 $xtrace
195}
196
197# Set a multiple line option in an INI file
Ian Wienandf44a0242015-07-22 10:34:47 +1000198# iniset_multiline [-sudo] config-file section option value1 value2 valu3 ...
Dean Troyerbf2ad702015-03-09 15:16:10 -0500199function iniset_multiline {
200 local xtrace=$(set +o | grep xtrace)
201 set +o xtrace
Ian Wienandf44a0242015-07-22 10:34:47 +1000202 local sudo=""
203 if [ $1 == "-sudo" ]; then
204 sudo="sudo "
205 shift
206 fi
Dean Troyerbf2ad702015-03-09 15:16:10 -0500207 local file=$1
208 local section=$2
209 local option=$3
210
211 shift 3
212 local values
213 for v in $@; do
214 # The later sed command inserts each new value in the line next to
215 # the section identifier, which causes the values to be inserted in
216 # the reverse order. Do a reverse here to keep the original order.
217 values="$v ${values}"
218 done
219 if ! grep -q "^\[$section\]" "$file"; then
220 # Add section at the end
Ian Wienandf44a0242015-07-22 10:34:47 +1000221 echo -e "\n[$section]" | $sudo tee --append "$file" > /dev/null
Dean Troyerbf2ad702015-03-09 15:16:10 -0500222 else
223 # Remove old values
Ian Wienandf44a0242015-07-22 10:34:47 +1000224 $sudo sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file"
Dean Troyerbf2ad702015-03-09 15:16:10 -0500225 fi
226 # Add new ones
227 for v in $values; do
Ian Wienandf44a0242015-07-22 10:34:47 +1000228 $sudo sed -i -e "/^\[$section\]/ a\\
Dean Troyerbf2ad702015-03-09 15:16:10 -0500229$option = $v
230" "$file"
231 done
232 $xtrace
233}
234
235# Uncomment an option in an INI file
236# iniuncomment config-file section option
237function iniuncomment {
238 local xtrace=$(set +o | grep xtrace)
239 set +o xtrace
Ian Wienandf44a0242015-07-22 10:34:47 +1000240 local sudo=""
241 if [ $1 == "-sudo" ]; then
242 sudo="sudo "
243 shift
244 fi
Dean Troyerbf2ad702015-03-09 15:16:10 -0500245 local file=$1
246 local section=$2
247 local option=$3
Ian Wienandf44a0242015-07-22 10:34:47 +1000248 $sudo sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" "$file"
Dean Troyerbf2ad702015-03-09 15:16:10 -0500249 $xtrace
250}
251
Dean Troyerbf2ad702015-03-09 15:16:10 -0500252# Restore xtrace
253$INC_CONF_TRACE
254
255# Local variables:
256# mode: shell-script
257# End: