blob: 8e7c0187aea282c91eb48e60c670b968fe0f46d1 [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
18# iniadd config-file section option value1 value2 value3 ...
19function iniadd {
20 local xtrace=$(set +o | grep xtrace)
21 set +o xtrace
22 local file=$1
23 local section=$2
24 local option=$3
25 shift 3
26
27 local values="$(iniget_multiline $file $section $option) $@"
28 iniset_multiline $file $section $option $values
29 $xtrace
30}
31
32# Comment an option in an INI file
33# inicomment config-file section option
34function inicomment {
35 local xtrace=$(set +o | grep xtrace)
36 set +o xtrace
37 local file=$1
38 local section=$2
39 local option=$3
40
41 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" "$file"
42 $xtrace
43}
44
45# Get an option from an INI file
46# iniget config-file section option
47function iniget {
48 local xtrace=$(set +o | grep xtrace)
49 set +o xtrace
50 local file=$1
51 local section=$2
52 local option=$3
53 local line
54
55 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
56 echo ${line#*=}
57 $xtrace
58}
59
60# Get a multiple line option from an INI file
61# iniget_multiline config-file section option
62function iniget_multiline {
63 local xtrace=$(set +o | grep xtrace)
64 set +o xtrace
65 local file=$1
66 local section=$2
67 local option=$3
68 local values
69
70 values=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { s/^$option[ \t]*=[ \t]*//gp; }" "$file")
71 echo ${values}
72 $xtrace
73}
74
75# Determinate is the given option present in the INI file
76# ini_has_option config-file section option
77function ini_has_option {
78 local xtrace=$(set +o | grep xtrace)
79 set +o xtrace
80 local file=$1
81 local section=$2
82 local option=$3
83 local line
84
85 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
86 $xtrace
87 [ -n "$line" ]
88}
89
90# Add another config line for a multi-line option.
91# It's normally called after iniset of the same option and assumes
92# that the section already exists.
93#
94# Note that iniset_multiline requires all the 'lines' to be supplied
95# in the argument list. Doing that will cause incorrect configuration
96# if spaces are used in the config values.
97#
98# iniadd_literal config-file section option value
99function iniadd_literal {
100 local xtrace=$(set +o | grep xtrace)
101 set +o xtrace
102 local file=$1
103 local section=$2
104 local option=$3
105 local value=$4
106
Ian Wienand92884ed2015-07-22 12:16:45 +1000107 if [[ -z $section || -z $option ]]; then
108 $xtrace
109 return
110 fi
Dean Troyerbf2ad702015-03-09 15:16:10 -0500111
112 # Add it
113 sed -i -e "/^\[$section\]/ a\\
114$option = $value
115" "$file"
116
117 $xtrace
118}
119
120# Remove an option from an INI file
121# inidelete config-file section option
122function inidelete {
123 local xtrace=$(set +o | grep xtrace)
124 set +o xtrace
125 local file=$1
126 local section=$2
127 local option=$3
128
Ian Wienand92884ed2015-07-22 12:16:45 +1000129 if [[ -z $section || -z $option ]]; then
130 $xtrace
131 return
132 fi
Dean Troyerbf2ad702015-03-09 15:16:10 -0500133
134 # Remove old values
135 sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file"
136
137 $xtrace
138}
139
140# Set an option in an INI file
141# iniset config-file section option value
142function iniset {
143 local xtrace=$(set +o | grep xtrace)
144 set +o xtrace
145 local file=$1
146 local section=$2
147 local option=$3
148 local value=$4
149
Ian Wienand92884ed2015-07-22 12:16:45 +1000150 if [[ -z $section || -z $option ]]; then
151 $xtrace
152 return
153 fi
Dean Troyerbf2ad702015-03-09 15:16:10 -0500154
155 if ! grep -q "^\[$section\]" "$file" 2>/dev/null; then
156 # Add section at the end
157 echo -e "\n[$section]" >>"$file"
158 fi
159 if ! ini_has_option "$file" "$section" "$option"; then
160 # Add it
161 sed -i -e "/^\[$section\]/ a\\
162$option = $value
163" "$file"
164 else
165 local sep=$(echo -ne "\x01")
166 # Replace it
167 sed -i -e '/^\['${section}'\]/,/^\[.*\]/ s'${sep}'^\('${option}'[ \t]*=[ \t]*\).*$'${sep}'\1'"${value}"${sep} "$file"
168 fi
169 $xtrace
170}
171
172# Set a multiple line option in an INI file
173# iniset_multiline config-file section option value1 value2 valu3 ...
174function iniset_multiline {
175 local xtrace=$(set +o | grep xtrace)
176 set +o xtrace
177 local file=$1
178 local section=$2
179 local option=$3
180
181 shift 3
182 local values
183 for v in $@; do
184 # The later sed command inserts each new value in the line next to
185 # the section identifier, which causes the values to be inserted in
186 # the reverse order. Do a reverse here to keep the original order.
187 values="$v ${values}"
188 done
189 if ! grep -q "^\[$section\]" "$file"; then
190 # Add section at the end
191 echo -e "\n[$section]" >>"$file"
192 else
193 # Remove old values
194 sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file"
195 fi
196 # Add new ones
197 for v in $values; do
198 sed -i -e "/^\[$section\]/ a\\
199$option = $v
200" "$file"
201 done
202 $xtrace
203}
204
205# Uncomment an option in an INI file
206# iniuncomment config-file section option
207function iniuncomment {
208 local xtrace=$(set +o | grep xtrace)
209 set +o xtrace
210 local file=$1
211 local section=$2
212 local option=$3
213 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" "$file"
214 $xtrace
215}
216
Dean Troyerbf2ad702015-03-09 15:16:10 -0500217# Restore xtrace
218$INC_CONF_TRACE
219
220# Local variables:
221# mode: shell-script
222# End: