Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 1 | #!/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 |
| 10 | INC_CONF_TRACE=$(set +o | grep xtrace) |
| 11 | set +o xtrace |
| 12 | |
| 13 | |
| 14 | # Config Functions |
| 15 | # ================ |
| 16 | |
| 17 | # Append a new option in an ini file without replacing the old value |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 18 | # iniadd [-sudo] config-file section option value1 value2 value3 ... |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 19 | function iniadd { |
Ian Wienand | 433a9b1 | 2015-10-07 13:29:31 +1100 | [diff] [blame] | 20 | local xtrace |
| 21 | xtrace=$(set +o | grep xtrace) |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 22 | set +o xtrace |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 23 | local sudo="" |
| 24 | if [ $1 == "-sudo" ]; then |
| 25 | sudo="-sudo " |
| 26 | shift |
| 27 | fi |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 28 | local file=$1 |
| 29 | local section=$2 |
| 30 | local option=$3 |
| 31 | shift 3 |
| 32 | |
Ian Wienand | 7ae9729 | 2016-02-16 14:50:53 +1100 | [diff] [blame^] | 33 | local values |
| 34 | values="$(iniget_multiline $file $section $option) $@" |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 35 | iniset_multiline $sudo $file $section $option $values |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 36 | $xtrace |
| 37 | } |
| 38 | |
| 39 | # Comment an option in an INI file |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 40 | # inicomment [-sudo] config-file section option |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 41 | function inicomment { |
Ian Wienand | 433a9b1 | 2015-10-07 13:29:31 +1100 | [diff] [blame] | 42 | local xtrace |
| 43 | xtrace=$(set +o | grep xtrace) |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 44 | set +o xtrace |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 45 | local sudo="" |
| 46 | if [ $1 == "-sudo" ]; then |
| 47 | sudo="sudo " |
| 48 | shift |
| 49 | fi |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 50 | local file=$1 |
| 51 | local section=$2 |
| 52 | local option=$3 |
| 53 | |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 54 | $sudo sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" "$file" |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 55 | $xtrace |
| 56 | } |
| 57 | |
| 58 | # Get an option from an INI file |
| 59 | # iniget config-file section option |
| 60 | function iniget { |
Ian Wienand | 433a9b1 | 2015-10-07 13:29:31 +1100 | [diff] [blame] | 61 | local xtrace |
| 62 | xtrace=$(set +o | grep xtrace) |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 63 | set +o xtrace |
| 64 | local file=$1 |
| 65 | local section=$2 |
| 66 | local option=$3 |
| 67 | local line |
| 68 | |
| 69 | line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file") |
| 70 | echo ${line#*=} |
| 71 | $xtrace |
| 72 | } |
| 73 | |
| 74 | # Get a multiple line option from an INI file |
| 75 | # iniget_multiline config-file section option |
| 76 | function iniget_multiline { |
Ian Wienand | 433a9b1 | 2015-10-07 13:29:31 +1100 | [diff] [blame] | 77 | local xtrace |
| 78 | xtrace=$(set +o | grep xtrace) |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 79 | set +o xtrace |
| 80 | local file=$1 |
| 81 | local section=$2 |
| 82 | local option=$3 |
| 83 | local values |
| 84 | |
| 85 | values=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { s/^$option[ \t]*=[ \t]*//gp; }" "$file") |
| 86 | echo ${values} |
| 87 | $xtrace |
| 88 | } |
| 89 | |
| 90 | # Determinate is the given option present in the INI file |
| 91 | # ini_has_option config-file section option |
| 92 | function ini_has_option { |
Ian Wienand | 433a9b1 | 2015-10-07 13:29:31 +1100 | [diff] [blame] | 93 | local xtrace |
| 94 | xtrace=$(set +o | grep xtrace) |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 95 | set +o xtrace |
| 96 | local file=$1 |
| 97 | local section=$2 |
| 98 | local option=$3 |
| 99 | local line |
| 100 | |
| 101 | line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file") |
| 102 | $xtrace |
| 103 | [ -n "$line" ] |
| 104 | } |
| 105 | |
| 106 | # Add another config line for a multi-line option. |
| 107 | # It's normally called after iniset of the same option and assumes |
| 108 | # that the section already exists. |
| 109 | # |
| 110 | # Note that iniset_multiline requires all the 'lines' to be supplied |
| 111 | # in the argument list. Doing that will cause incorrect configuration |
| 112 | # if spaces are used in the config values. |
| 113 | # |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 114 | # iniadd_literal [-sudo] config-file section option value |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 115 | function iniadd_literal { |
Ian Wienand | 433a9b1 | 2015-10-07 13:29:31 +1100 | [diff] [blame] | 116 | local xtrace |
| 117 | xtrace=$(set +o | grep xtrace) |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 118 | set +o xtrace |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 119 | local sudo="" |
| 120 | if [ $1 == "-sudo" ]; then |
| 121 | sudo="sudo " |
| 122 | shift |
| 123 | fi |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 124 | local file=$1 |
| 125 | local section=$2 |
| 126 | local option=$3 |
| 127 | local value=$4 |
| 128 | |
Ian Wienand | 92884ed | 2015-07-22 12:16:45 +1000 | [diff] [blame] | 129 | if [[ -z $section || -z $option ]]; then |
| 130 | $xtrace |
| 131 | return |
| 132 | fi |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 133 | |
| 134 | # Add it |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 135 | $sudo sed -i -e "/^\[$section\]/ a\\ |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 136 | $option = $value |
| 137 | " "$file" |
| 138 | |
| 139 | $xtrace |
| 140 | } |
| 141 | |
| 142 | # Remove an option from an INI file |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 143 | # inidelete [-sudo] config-file section option |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 144 | function inidelete { |
Ian Wienand | 433a9b1 | 2015-10-07 13:29:31 +1100 | [diff] [blame] | 145 | local xtrace |
| 146 | xtrace=$(set +o | grep xtrace) |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 147 | set +o xtrace |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 148 | local sudo="" |
| 149 | if [ $1 == "-sudo" ]; then |
| 150 | sudo="sudo " |
| 151 | shift |
| 152 | fi |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 153 | local file=$1 |
| 154 | local section=$2 |
| 155 | local option=$3 |
| 156 | |
Ian Wienand | 92884ed | 2015-07-22 12:16:45 +1000 | [diff] [blame] | 157 | if [[ -z $section || -z $option ]]; then |
| 158 | $xtrace |
| 159 | return |
| 160 | fi |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 161 | |
| 162 | # Remove old values |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 163 | $sudo sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file" |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 164 | |
| 165 | $xtrace |
| 166 | } |
| 167 | |
| 168 | # Set an option in an INI file |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 169 | # iniset [-sudo] config-file section option value |
Ian Wienand | cede787 | 2015-07-22 13:36:12 +1000 | [diff] [blame] | 170 | # - if the file does not exist, it is created |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 171 | function iniset { |
Ian Wienand | 433a9b1 | 2015-10-07 13:29:31 +1100 | [diff] [blame] | 172 | local xtrace |
| 173 | xtrace=$(set +o | grep xtrace) |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 174 | set +o xtrace |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 175 | local sudo="" |
| 176 | if [ $1 == "-sudo" ]; then |
| 177 | sudo="sudo " |
| 178 | shift |
| 179 | fi |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 180 | local file=$1 |
| 181 | local section=$2 |
| 182 | local option=$3 |
| 183 | local value=$4 |
| 184 | |
Ian Wienand | 92884ed | 2015-07-22 12:16:45 +1000 | [diff] [blame] | 185 | if [[ -z $section || -z $option ]]; then |
| 186 | $xtrace |
| 187 | return |
| 188 | fi |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 189 | |
| 190 | if ! grep -q "^\[$section\]" "$file" 2>/dev/null; then |
| 191 | # Add section at the end |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 192 | echo -e "\n[$section]" | $sudo tee --append "$file" > /dev/null |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 193 | fi |
| 194 | if ! ini_has_option "$file" "$section" "$option"; then |
| 195 | # Add it |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 196 | $sudo sed -i -e "/^\[$section\]/ a\\ |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 197 | $option = $value |
| 198 | " "$file" |
| 199 | else |
Ian Wienand | ada886d | 2015-10-07 14:06:26 +1100 | [diff] [blame] | 200 | local sep |
| 201 | sep=$(echo -ne "\x01") |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 202 | # Replace it |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 203 | $sudo sed -i -e '/^\['${section}'\]/,/^\[.*\]/ s'${sep}'^\('${option}'[ \t]*=[ \t]*\).*$'${sep}'\1'"${value}"${sep} "$file" |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 204 | fi |
| 205 | $xtrace |
| 206 | } |
| 207 | |
| 208 | # Set a multiple line option in an INI file |
Atsushi SAKAI | 5509ed5 | 2015-11-30 20:20:21 +0900 | [diff] [blame] | 209 | # iniset_multiline [-sudo] config-file section option value1 value2 value3 ... |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 210 | function iniset_multiline { |
Ian Wienand | 433a9b1 | 2015-10-07 13:29:31 +1100 | [diff] [blame] | 211 | local xtrace |
| 212 | xtrace=$(set +o | grep xtrace) |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 213 | set +o xtrace |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 214 | local sudo="" |
| 215 | if [ $1 == "-sudo" ]; then |
| 216 | sudo="sudo " |
| 217 | shift |
| 218 | fi |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 219 | local file=$1 |
| 220 | local section=$2 |
| 221 | local option=$3 |
| 222 | |
| 223 | shift 3 |
| 224 | local values |
| 225 | for v in $@; do |
| 226 | # The later sed command inserts each new value in the line next to |
| 227 | # the section identifier, which causes the values to be inserted in |
| 228 | # the reverse order. Do a reverse here to keep the original order. |
| 229 | values="$v ${values}" |
| 230 | done |
| 231 | if ! grep -q "^\[$section\]" "$file"; then |
| 232 | # Add section at the end |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 233 | echo -e "\n[$section]" | $sudo tee --append "$file" > /dev/null |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 234 | else |
| 235 | # Remove old values |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 236 | $sudo sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file" |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 237 | fi |
| 238 | # Add new ones |
| 239 | for v in $values; do |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 240 | $sudo sed -i -e "/^\[$section\]/ a\\ |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 241 | $option = $v |
| 242 | " "$file" |
| 243 | done |
| 244 | $xtrace |
| 245 | } |
| 246 | |
| 247 | # Uncomment an option in an INI file |
| 248 | # iniuncomment config-file section option |
| 249 | function iniuncomment { |
Ian Wienand | 433a9b1 | 2015-10-07 13:29:31 +1100 | [diff] [blame] | 250 | local xtrace |
| 251 | xtrace=$(set +o | grep xtrace) |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 252 | set +o xtrace |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 253 | local sudo="" |
| 254 | if [ $1 == "-sudo" ]; then |
| 255 | sudo="sudo " |
| 256 | shift |
| 257 | fi |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 258 | local file=$1 |
| 259 | local section=$2 |
| 260 | local option=$3 |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 261 | $sudo sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" "$file" |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 262 | $xtrace |
| 263 | } |
| 264 | |
vsaienko | 135bd48 | 2015-12-11 11:03:52 +0200 | [diff] [blame] | 265 | # Get list of sections from an INI file |
| 266 | # iniget_sections config-file |
| 267 | function iniget_sections { |
| 268 | local xtrace |
| 269 | xtrace=$(set +o | grep xtrace) |
| 270 | set +o xtrace |
| 271 | local file=$1 |
| 272 | |
| 273 | echo $(sed -ne "s/^\[\(.*\)\]/\1/p" "$file") |
| 274 | $xtrace |
| 275 | } |
| 276 | |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 277 | # Restore xtrace |
| 278 | $INC_CONF_TRACE |
| 279 | |
| 280 | # Local variables: |
| 281 | # mode: shell-script |
| 282 | # End: |