blob: 58386e24417177f5d9f3ba5428bdb45cd90c50eb [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
Ian Wienandcede7872015-07-22 13:36:12 +1000162# - if the file does not exist, it is created
Dean Troyerbf2ad702015-03-09 15:16:10 -0500163function iniset {
164 local xtrace=$(set +o | grep xtrace)
165 set +o xtrace
Ian Wienandf44a0242015-07-22 10:34:47 +1000166 local sudo=""
167 if [ $1 == "-sudo" ]; then
168 sudo="sudo "
169 shift
170 fi
Dean Troyerbf2ad702015-03-09 15:16:10 -0500171 local file=$1
172 local section=$2
173 local option=$3
174 local value=$4
175
Ian Wienand92884ed2015-07-22 12:16:45 +1000176 if [[ -z $section || -z $option ]]; then
177 $xtrace
178 return
179 fi
Dean Troyerbf2ad702015-03-09 15:16:10 -0500180
181 if ! grep -q "^\[$section\]" "$file" 2>/dev/null; then
182 # Add section at the end
Ian Wienandf44a0242015-07-22 10:34:47 +1000183 echo -e "\n[$section]" | $sudo tee --append "$file" > /dev/null
Dean Troyerbf2ad702015-03-09 15:16:10 -0500184 fi
185 if ! ini_has_option "$file" "$section" "$option"; then
186 # Add it
Ian Wienandf44a0242015-07-22 10:34:47 +1000187 $sudo sed -i -e "/^\[$section\]/ a\\
Dean Troyerbf2ad702015-03-09 15:16:10 -0500188$option = $value
189" "$file"
190 else
191 local sep=$(echo -ne "\x01")
192 # Replace it
Ian Wienandf44a0242015-07-22 10:34:47 +1000193 $sudo sed -i -e '/^\['${section}'\]/,/^\[.*\]/ s'${sep}'^\('${option}'[ \t]*=[ \t]*\).*$'${sep}'\1'"${value}"${sep} "$file"
Dean Troyerbf2ad702015-03-09 15:16:10 -0500194 fi
195 $xtrace
196}
197
198# Set a multiple line option in an INI file
Ian Wienandf44a0242015-07-22 10:34:47 +1000199# iniset_multiline [-sudo] config-file section option value1 value2 valu3 ...
Dean Troyerbf2ad702015-03-09 15:16:10 -0500200function iniset_multiline {
201 local xtrace=$(set +o | grep xtrace)
202 set +o xtrace
Ian Wienandf44a0242015-07-22 10:34:47 +1000203 local sudo=""
204 if [ $1 == "-sudo" ]; then
205 sudo="sudo "
206 shift
207 fi
Dean Troyerbf2ad702015-03-09 15:16:10 -0500208 local file=$1
209 local section=$2
210 local option=$3
211
212 shift 3
213 local values
214 for v in $@; do
215 # The later sed command inserts each new value in the line next to
216 # the section identifier, which causes the values to be inserted in
217 # the reverse order. Do a reverse here to keep the original order.
218 values="$v ${values}"
219 done
220 if ! grep -q "^\[$section\]" "$file"; then
221 # Add section at the end
Ian Wienandf44a0242015-07-22 10:34:47 +1000222 echo -e "\n[$section]" | $sudo tee --append "$file" > /dev/null
Dean Troyerbf2ad702015-03-09 15:16:10 -0500223 else
224 # Remove old values
Ian Wienandf44a0242015-07-22 10:34:47 +1000225 $sudo sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file"
Dean Troyerbf2ad702015-03-09 15:16:10 -0500226 fi
227 # Add new ones
228 for v in $values; do
Ian Wienandf44a0242015-07-22 10:34:47 +1000229 $sudo sed -i -e "/^\[$section\]/ a\\
Dean Troyerbf2ad702015-03-09 15:16:10 -0500230$option = $v
231" "$file"
232 done
233 $xtrace
234}
235
236# Uncomment an option in an INI file
237# iniuncomment config-file section option
238function iniuncomment {
239 local xtrace=$(set +o | grep xtrace)
240 set +o xtrace
Ian Wienandf44a0242015-07-22 10:34:47 +1000241 local sudo=""
242 if [ $1 == "-sudo" ]; then
243 sudo="sudo "
244 shift
245 fi
Dean Troyerbf2ad702015-03-09 15:16:10 -0500246 local file=$1
247 local section=$2
248 local option=$3
Ian Wienandf44a0242015-07-22 10:34:47 +1000249 $sudo sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" "$file"
Dean Troyerbf2ad702015-03-09 15:16:10 -0500250 $xtrace
251}
252
Dean Troyerbf2ad702015-03-09 15:16:10 -0500253# Restore xtrace
254$INC_CONF_TRACE
255
256# Local variables:
257# mode: shell-script
258# End: