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