Fix ini functions to handle spaces in section names

This allows section names to look like:

[ default ]

OpenSSL is the primary offender for this usage.

Change-Id: If5c711107e73cebab9d4a26ca02a7ce572224377
This commit is contained in:
Dean Troyer 2012-11-27 17:00:11 -06:00
parent 140b58ba19
commit e83356217b
2 changed files with 17 additions and 7 deletions

View File

@ -370,7 +370,7 @@ function inicomment() {
local file=$1 local file=$1
local section=$2 local section=$2
local option=$3 local option=$3
sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" $file sed -ie "/^\[ *$section *\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" $file
} }
# Uncomment an option in an INI file # Uncomment an option in an INI file
@ -379,7 +379,7 @@ function iniuncomment() {
local file=$1 local file=$1
local section=$2 local section=$2
local option=$3 local option=$3
sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" $file sed -ie "/^\[ *$section *\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" $file
} }
@ -390,7 +390,7 @@ function iniget() {
local section=$2 local section=$2
local option=$3 local option=$3
local line local line
line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" $file) line=$(sed -ne "/^\[ *$section *\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" $file)
echo ${line#*=} echo ${line#*=}
} }
@ -402,18 +402,18 @@ function iniset() {
local section=$2 local section=$2
local option=$3 local option=$3
local value=$4 local value=$4
if ! grep -q "^\[$section\]" $file; then if ! grep -q "^\[ *$section *\]" $file; then
# Add section at the end # Add section at the end
echo -e "\n[$section]" >>$file echo -e "\n[$section]" >>$file
fi fi
if [[ -z "$(iniget $file $section $option)" ]]; then if [[ -z "$(iniget $file $section $option)" ]]; then
# Add it # Add it
sed -i -e "/^\[$section\]/ a\\ sed -ie "/^\[ *$section *\]/ a\\
$option = $value $option = $value
" $file " $file
else else
# Replace it # Replace it
sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" $file sed -ie "/^\[ *$section *\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" $file
fi fi
} }

View File

@ -54,6 +54,9 @@ handlers = aa, bb
[bbb] [bbb]
handlers=ee,ff handlers=ee,ff
[ ccc ]
spaces = yes
EOF EOF
# Test with spaces # Test with spaces
@ -74,6 +77,14 @@ else
echo "iniget failed: $VAL" echo "iniget failed: $VAL"
fi fi
# Test with spaces in section header
VAL=$(iniget test.ini ccc spaces)
if [[ "$VAL" == "yes" ]]; then
echo "OK: $VAL"
else
echo "iniget failed: $VAL"
fi
# Test without spaces, end of file # Test without spaces, end of file
@ -112,7 +123,6 @@ else
echo "iniget failed: $VAL" echo "iniget failed: $VAL"
fi fi
# Test option not exist # Test option not exist
VAL=$(iniget test.ini aaa debug) VAL=$(iniget test.ini aaa debug)