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 { |
| 20 | local xtrace=$(set +o | grep xtrace) |
| 21 | set +o xtrace |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 22 | local sudo="" |
| 23 | if [ $1 == "-sudo" ]; then |
| 24 | sudo="-sudo " |
| 25 | shift |
| 26 | fi |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 27 | local file=$1 |
| 28 | local section=$2 |
| 29 | local option=$3 |
| 30 | shift 3 |
| 31 | |
| 32 | local values="$(iniget_multiline $file $section $option) $@" |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 33 | iniset_multiline $sudo $file $section $option $values |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 34 | $xtrace |
| 35 | } |
| 36 | |
| 37 | # Comment an option in an INI file |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 38 | # inicomment [-sudo] config-file section option |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 39 | function inicomment { |
| 40 | local xtrace=$(set +o | grep xtrace) |
| 41 | set +o xtrace |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 42 | local sudo="" |
| 43 | if [ $1 == "-sudo" ]; then |
| 44 | sudo="sudo " |
| 45 | shift |
| 46 | fi |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 47 | local file=$1 |
| 48 | local section=$2 |
| 49 | local option=$3 |
| 50 | |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 51 | $sudo sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" "$file" |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 52 | $xtrace |
| 53 | } |
| 54 | |
| 55 | # Get an option from an INI file |
| 56 | # iniget config-file section option |
| 57 | function 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 |
| 72 | function 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 |
| 87 | function 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 Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 108 | # iniadd_literal [-sudo] config-file section option value |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 109 | function iniadd_literal { |
| 110 | local xtrace=$(set +o | grep xtrace) |
| 111 | set +o xtrace |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 112 | local sudo="" |
| 113 | if [ $1 == "-sudo" ]; then |
| 114 | sudo="sudo " |
| 115 | shift |
| 116 | fi |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 117 | local file=$1 |
| 118 | local section=$2 |
| 119 | local option=$3 |
| 120 | local value=$4 |
| 121 | |
Ian Wienand | 92884ed | 2015-07-22 12:16:45 +1000 | [diff] [blame] | 122 | if [[ -z $section || -z $option ]]; then |
| 123 | $xtrace |
| 124 | return |
| 125 | fi |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 126 | |
| 127 | # Add it |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 128 | $sudo sed -i -e "/^\[$section\]/ a\\ |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 129 | $option = $value |
| 130 | " "$file" |
| 131 | |
| 132 | $xtrace |
| 133 | } |
| 134 | |
| 135 | # Remove an option from an INI file |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 136 | # inidelete [-sudo] config-file section option |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 137 | function inidelete { |
| 138 | local xtrace=$(set +o | grep xtrace) |
| 139 | set +o xtrace |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 140 | local sudo="" |
| 141 | if [ $1 == "-sudo" ]; then |
| 142 | sudo="sudo " |
| 143 | shift |
| 144 | fi |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 145 | local file=$1 |
| 146 | local section=$2 |
| 147 | local option=$3 |
| 148 | |
Ian Wienand | 92884ed | 2015-07-22 12:16:45 +1000 | [diff] [blame] | 149 | if [[ -z $section || -z $option ]]; then |
| 150 | $xtrace |
| 151 | return |
| 152 | fi |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 153 | |
| 154 | # Remove old values |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 155 | $sudo sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file" |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 156 | |
| 157 | $xtrace |
| 158 | } |
| 159 | |
| 160 | # Set an option in an INI file |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 161 | # iniset [-sudo] config-file section option value |
Ian Wienand | cede787 | 2015-07-22 13:36:12 +1000 | [diff] [blame] | 162 | # - if the file does not exist, it is created |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 163 | function iniset { |
| 164 | local xtrace=$(set +o | grep xtrace) |
| 165 | set +o xtrace |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 166 | local sudo="" |
| 167 | if [ $1 == "-sudo" ]; then |
| 168 | sudo="sudo " |
| 169 | shift |
| 170 | fi |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 171 | local file=$1 |
| 172 | local section=$2 |
| 173 | local option=$3 |
| 174 | local value=$4 |
| 175 | |
Ian Wienand | 92884ed | 2015-07-22 12:16:45 +1000 | [diff] [blame] | 176 | if [[ -z $section || -z $option ]]; then |
| 177 | $xtrace |
| 178 | return |
| 179 | fi |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 180 | |
| 181 | if ! grep -q "^\[$section\]" "$file" 2>/dev/null; then |
| 182 | # Add section at the end |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 183 | echo -e "\n[$section]" | $sudo tee --append "$file" > /dev/null |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 184 | fi |
| 185 | if ! ini_has_option "$file" "$section" "$option"; then |
| 186 | # Add it |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 187 | $sudo sed -i -e "/^\[$section\]/ a\\ |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 188 | $option = $value |
| 189 | " "$file" |
| 190 | else |
| 191 | local sep=$(echo -ne "\x01") |
| 192 | # Replace it |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 193 | $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] | 194 | fi |
| 195 | $xtrace |
| 196 | } |
| 197 | |
| 198 | # Set a multiple line option in an INI file |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 199 | # iniset_multiline [-sudo] config-file section option value1 value2 valu3 ... |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 200 | function iniset_multiline { |
| 201 | local xtrace=$(set +o | grep xtrace) |
| 202 | set +o xtrace |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 203 | local sudo="" |
| 204 | if [ $1 == "-sudo" ]; then |
| 205 | sudo="sudo " |
| 206 | shift |
| 207 | fi |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 208 | 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 Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 222 | echo -e "\n[$section]" | $sudo tee --append "$file" > /dev/null |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 223 | else |
| 224 | # Remove old values |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 225 | $sudo sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file" |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 226 | fi |
| 227 | # Add new ones |
| 228 | for v in $values; do |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 229 | $sudo sed -i -e "/^\[$section\]/ a\\ |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 230 | $option = $v |
| 231 | " "$file" |
| 232 | done |
| 233 | $xtrace |
| 234 | } |
| 235 | |
| 236 | # Uncomment an option in an INI file |
| 237 | # iniuncomment config-file section option |
| 238 | function iniuncomment { |
| 239 | local xtrace=$(set +o | grep xtrace) |
| 240 | set +o xtrace |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 241 | local sudo="" |
| 242 | if [ $1 == "-sudo" ]; then |
| 243 | sudo="sudo " |
| 244 | shift |
| 245 | fi |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 246 | local file=$1 |
| 247 | local section=$2 |
| 248 | local option=$3 |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 249 | $sudo sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" "$file" |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 250 | $xtrace |
| 251 | } |
| 252 | |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 253 | # Restore xtrace |
| 254 | $INC_CONF_TRACE |
| 255 | |
| 256 | # Local variables: |
| 257 | # mode: shell-script |
| 258 | # End: |