Browse code

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

Dean Troyer authored on 2012/11/28 08:00:11
Showing 2 changed files
... ...
@@ -370,7 +370,7 @@ function inicomment() {
370 370
     local file=$1
371 371
     local section=$2
372 372
     local option=$3
373
-    sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" $file
373
+    sed -ie "/^\[ *$section *\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" $file
374 374
 }
375 375
 
376 376
 # Uncomment an option in an INI file
... ...
@@ -379,7 +379,7 @@ function iniuncomment() {
379 379
     local file=$1
380 380
     local section=$2
381 381
     local option=$3
382
-    sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" $file
382
+    sed -ie "/^\[ *$section *\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" $file
383 383
 }
384 384
 
385 385
 
... ...
@@ -390,7 +390,7 @@ function iniget() {
390 390
     local section=$2
391 391
     local option=$3
392 392
     local line
393
-    line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" $file)
393
+    line=$(sed -ne "/^\[ *$section *\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" $file)
394 394
     echo ${line#*=}
395 395
 }
396 396
 
... ...
@@ -402,18 +402,18 @@ function iniset() {
402 402
     local section=$2
403 403
     local option=$3
404 404
     local value=$4
405
-    if ! grep -q "^\[$section\]" $file; then
405
+    if ! grep -q "^\[ *$section *\]" $file; then
406 406
         # Add section at the end
407 407
         echo -e "\n[$section]" >>$file
408 408
     fi
409 409
     if [[ -z "$(iniget $file $section $option)" ]]; then
410 410
         # Add it
411
-        sed -i -e "/^\[$section\]/ a\\
411
+        sed -ie "/^\[ *$section *\]/ a\\
412 412
 $option = $value
413 413
 " $file
414 414
     else
415 415
         # Replace it
416
-        sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" $file
416
+        sed -ie "/^\[ *$section *\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" $file
417 417
     fi
418 418
 }
419 419
 
... ...
@@ -54,6 +54,9 @@ handlers = aa, bb
54 54
 
55 55
 [bbb]
56 56
 handlers=ee,ff
57
+
58
+[ ccc ]
59
+spaces  =  yes
57 60
 EOF
58 61
 
59 62
 # Test with spaces
... ...
@@ -74,6 +77,14 @@ else
74 74
     echo "iniget failed: $VAL"
75 75
 fi
76 76
 
77
+# Test with spaces in section header
78
+
79
+VAL=$(iniget test.ini ccc spaces)
80
+if [[ "$VAL" == "yes" ]]; then
81
+    echo "OK: $VAL"
82
+else
83
+    echo "iniget failed: $VAL"
84
+fi
77 85
 
78 86
 # Test without spaces, end of file
79 87
 
... ...
@@ -112,7 +123,6 @@ else
112 112
     echo "iniget failed: $VAL"
113 113
 fi
114 114
 
115
-
116 115
 # Test option not exist
117 116
 
118 117
 VAL=$(iniget test.ini aaa debug)