blob: 26401f39176c44a09c9090ef6c814e61a9a84a49 [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
107 [[ -z $section || -z $option ]] && return
108
109 # Add it
110 sed -i -e "/^\[$section\]/ a\\
111$option = $value
112" "$file"
113
114 $xtrace
115}
116
117# Remove an option from an INI file
118# inidelete config-file section option
119function inidelete {
120 local xtrace=$(set +o | grep xtrace)
121 set +o xtrace
122 local file=$1
123 local section=$2
124 local option=$3
125
126 [[ -z $section || -z $option ]] && return
127
128 # Remove old values
129 sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file"
130
131 $xtrace
132}
133
134# Set an option in an INI file
135# iniset config-file section option value
136function iniset {
137 local xtrace=$(set +o | grep xtrace)
138 set +o xtrace
139 local file=$1
140 local section=$2
141 local option=$3
142 local value=$4
143
144 [[ -z $section || -z $option ]] && return
145
146 if ! grep -q "^\[$section\]" "$file" 2>/dev/null; then
147 # Add section at the end
148 echo -e "\n[$section]" >>"$file"
149 fi
150 if ! ini_has_option "$file" "$section" "$option"; then
151 # Add it
152 sed -i -e "/^\[$section\]/ a\\
153$option = $value
154" "$file"
155 else
156 local sep=$(echo -ne "\x01")
157 # Replace it
158 sed -i -e '/^\['${section}'\]/,/^\[.*\]/ s'${sep}'^\('${option}'[ \t]*=[ \t]*\).*$'${sep}'\1'"${value}"${sep} "$file"
159 fi
160 $xtrace
161}
162
163# Set a multiple line option in an INI file
164# iniset_multiline config-file section option value1 value2 valu3 ...
165function iniset_multiline {
166 local xtrace=$(set +o | grep xtrace)
167 set +o xtrace
168 local file=$1
169 local section=$2
170 local option=$3
171
172 shift 3
173 local values
174 for v in $@; do
175 # The later sed command inserts each new value in the line next to
176 # the section identifier, which causes the values to be inserted in
177 # the reverse order. Do a reverse here to keep the original order.
178 values="$v ${values}"
179 done
180 if ! grep -q "^\[$section\]" "$file"; then
181 # Add section at the end
182 echo -e "\n[$section]" >>"$file"
183 else
184 # Remove old values
185 sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file"
186 fi
187 # Add new ones
188 for v in $values; do
189 sed -i -e "/^\[$section\]/ a\\
190$option = $v
191" "$file"
192 done
193 $xtrace
194}
195
196# Uncomment an option in an INI file
197# iniuncomment config-file section option
198function iniuncomment {
199 local xtrace=$(set +o | grep xtrace)
200 set +o xtrace
201 local file=$1
202 local section=$2
203 local option=$3
204 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" "$file"
205 $xtrace
206}
207
Dean Troyerbf2ad702015-03-09 15:16:10 -0500208# Restore xtrace
209$INC_CONF_TRACE
210
211# Local variables:
212# mode: shell-script
213# End: