hush: handle ${var:NUM:} too
authorDenys Vlasenko <vda.linux@googlemail.com>
Fri, 21 May 2010 13:24:12 +0000 (15:24 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Fri, 21 May 2010 13:24:12 +0000 (15:24 +0200)
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
shell/hush.c
shell/hush_test/hush-vars/param_expand_bash_substring.right
shell/hush_test/hush-vars/param_expand_bash_substring.tests

index 945077d87a9e03bcee15a514bf59058ff3ceaf28..6cf8899b02a476a4d6b12a77945661fd0a10affa 100644 (file)
@@ -2649,12 +2649,17 @@ static NOINLINE int expand_vars_to_list(o_string *output, int n, char *arg, char
                                                beg = bb_strtou(exp_word, &end, 0);
                                        //bb_error_msg("beg:'%s'=%u end:'%s'", exp_word, beg, end);
                                        if (*end == ':') {
-                                               len = bb_strtou(end + 1, &end, 0);
+                                               if (end[1] != '\0') /* not ${var:NUM:} */
+                                                       len = bb_strtou(end + 1, &end, 0);
+                                               else {
+                                                       len = 0;
+                                                       end++;
+                                               }
                                                //bb_error_msg("len:%u end:'%s'", len, end);
                                        }
                                        if (*end == '\0') {
                                                //bb_error_msg("from val:'%s'", val);
-                                               if (!val || beg >= strlen(val))
+                                               if (len == 0 || !val || beg >= strlen(val))
                                                        val = "";
                                                else
                                                        val = dyn_val = xstrndup(val + beg, len);
@@ -2663,6 +2668,7 @@ static NOINLINE int expand_vars_to_list(o_string *output, int n, char *arg, char
 #endif
                                        {
                                                die_if_script("malformed ${%s...}", var);
+                                               val = "";
                                        }
                                } else { /* one of "-=+?" */
        /* Standard-mandated substitution ops:
index 9cd465938a4c0c65cc18402e417d909899386437..6e3eb3ba636b508f2bfe51ef1eea478df7d1ec5d 100644 (file)
@@ -7,23 +7,35 @@ hush: syntax error: unterminated ${name}
 1:1  =||
 1:1:2=||
 1::2 =||
+1:1: =||
+1::  =||
 1    =|0123|
 1:1  =|123|
 1:1:2=|12|
 1::2 =|01|
+1:1: =||
+1::  =||
 f    =||
 f:1  =||
 f:1:2=||
 f::2 =||
+f:1: =||
+f::  =||
 f    =||
 f:1  =||
 f:1:2=||
 f::2 =||
+f:1: =||
+f::  =||
 f    =|a|
 f:1  =||
 f:1:2=||
 f::2 =|a|
+f:1: =||
+f::  =||
 f    =|0123456789|
 f:1  =|123456789|
 f:1:2=|12|
 f::2 =|01|
+f:1: =||
+f::  =||
index 6a1765559cc47e51c122807bd7bf048f6dd92dbf..eedd435ed53974eaed38290ddaaf3a9acece7bcf 100755 (executable)
@@ -19,28 +19,40 @@ export var=0123456789
 "$THIS_SH" -c 'set --; echo "1:1  =|${1:1}|"'
 "$THIS_SH" -c 'set --; echo "1:1:2=|${1:1:2}|"'
 "$THIS_SH" -c 'set --; echo "1::2 =|${1::2}|"'
+"$THIS_SH" -c 'set --; echo "1:1: =|${1:1:}|"'
+"$THIS_SH" -c 'set --; echo "1::  =|${1::}|"'
 
 "$THIS_SH" -c 'set -- 0123; echo "1    =|${1}|"'
 "$THIS_SH" -c 'set -- 0123; echo "1:1  =|${1:1}|"'
 "$THIS_SH" -c 'set -- 0123; echo "1:1:2=|${1:1:2}|"'
 "$THIS_SH" -c 'set -- 0123; echo "1::2 =|${1::2}|"'
+"$THIS_SH" -c 'set -- 0123; echo "1:1: =|${1:1:}|"'
+"$THIS_SH" -c 'set -- 0123; echo "1::  =|${1::}|"'
 
 "$THIS_SH" -c 'unset f; echo "f    =|$f|"'
 "$THIS_SH" -c 'unset f; echo "f:1  =|${f:1}|"'
 "$THIS_SH" -c 'unset f; echo "f:1:2=|${f:1:2}|"'
 "$THIS_SH" -c 'unset f; echo "f::2 =|${f::2}|"'
+"$THIS_SH" -c 'unset f; echo "f:1: =|${f:1:}|"'
+"$THIS_SH" -c 'unset f; echo "f::  =|${f::}|"'
 
 "$THIS_SH" -c 'f=; echo "f    =|$f|"'
 "$THIS_SH" -c 'f=; echo "f:1  =|${f:1}|"'
 "$THIS_SH" -c 'f=; echo "f:1:2=|${f:1:2}|"'
 "$THIS_SH" -c 'f=; echo "f::2 =|${f::2}|"'
+"$THIS_SH" -c 'f=; echo "f:1: =|${f:1:}|"'
+"$THIS_SH" -c 'f=; echo "f::  =|${f::}|"'
 
 "$THIS_SH" -c 'f=a; echo "f    =|$f|"'
 "$THIS_SH" -c 'f=a; echo "f:1  =|${f:1}|"'
 "$THIS_SH" -c 'f=a; echo "f:1:2=|${f:1:2}|"'
 "$THIS_SH" -c 'f=a; echo "f::2 =|${f::2}|"'
+"$THIS_SH" -c 'f=a; echo "f:1: =|${f:1:}|"'
+"$THIS_SH" -c 'f=a; echo "f::  =|${f::}|"'
 
 "$THIS_SH" -c 'f=0123456789; echo "f    =|$f|"'
 "$THIS_SH" -c 'f=0123456789; echo "f:1  =|${f:1}|"'
 "$THIS_SH" -c 'f=0123456789; echo "f:1:2=|${f:1:2}|"'
 "$THIS_SH" -c 'f=0123456789; echo "f::2 =|${f::2}|"'
+"$THIS_SH" -c 'f=0123456789; echo "f:1: =|${f:1:}|"'
+"$THIS_SH" -c 'f=0123456789; echo "f::  =|${f::}|"'