mirror of
https://github.com/sheumann/hush.git
synced 2025-01-18 07:31:34 +00:00
ash: fix handling of ${VAR: -2}
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
parent
0fb0045aa9
commit
f8ddbe1ccc
12
shell/ash.c
12
shell/ash.c
@ -6323,6 +6323,8 @@ subevalvar(char *p, char *varname, int strloc, int subtype,
|
|||||||
|
|
||||||
#if ENABLE_ASH_BASH_COMPAT
|
#if ENABLE_ASH_BASH_COMPAT
|
||||||
case VSSUBSTR:
|
case VSSUBSTR:
|
||||||
|
//TODO: support more general format ${v:EXPR:EXPR},
|
||||||
|
// where EXPR follows $(()) rules
|
||||||
loc = str = stackblock() + strloc;
|
loc = str = stackblock() + strloc;
|
||||||
/* Read POS in ${var:POS:LEN} */
|
/* Read POS in ${var:POS:LEN} */
|
||||||
pos = atoi(loc); /* number(loc) errors out on "1:4" */
|
pos = atoi(loc); /* number(loc) errors out on "1:4" */
|
||||||
@ -11577,15 +11579,18 @@ parsesub: {
|
|||||||
STPUTC('=', out);
|
STPUTC('=', out);
|
||||||
flags = 0;
|
flags = 0;
|
||||||
if (subtype == 0) {
|
if (subtype == 0) {
|
||||||
|
static const char types[] ALIGN1 = "}-+?=";
|
||||||
/* ${VAR...} but not $VAR or ${#VAR} */
|
/* ${VAR...} but not $VAR or ${#VAR} */
|
||||||
/* c == first char after VAR */
|
/* c == first char after VAR */
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case ':':
|
case ':':
|
||||||
c = pgetc();
|
c = pgetc();
|
||||||
#if ENABLE_ASH_BASH_COMPAT
|
#if ENABLE_ASH_BASH_COMPAT
|
||||||
if (c == ':' || c == '$' || isdigit(c)) {
|
/* This check is only needed to not misinterpret
|
||||||
//TODO: support more general format ${v:EXPR:EXPR},
|
* ${VAR:-WORD}, ${VAR:+WORD}, ${VAR:=WORD}, ${VAR:?WORD}
|
||||||
// where EXPR follows $(()) rules
|
* constructs.
|
||||||
|
*/
|
||||||
|
if (!strchr(types, c)) {
|
||||||
subtype = VSSUBSTR;
|
subtype = VSSUBSTR;
|
||||||
pungetc();
|
pungetc();
|
||||||
break; /* "goto do_pungetc" is bigger (!) */
|
break; /* "goto do_pungetc" is bigger (!) */
|
||||||
@ -11594,7 +11599,6 @@ parsesub: {
|
|||||||
flags = VSNUL;
|
flags = VSNUL;
|
||||||
/*FALLTHROUGH*/
|
/*FALLTHROUGH*/
|
||||||
default: {
|
default: {
|
||||||
static const char types[] ALIGN1 = "}-+?=";
|
|
||||||
const char *p = strchr(types, c);
|
const char *p = strchr(types, c);
|
||||||
if (p == NULL)
|
if (p == NULL)
|
||||||
goto badsub;
|
goto badsub;
|
||||||
|
6
shell/ash_test/ash-vars/var_bash1a.right
Normal file
6
shell/ash_test/ash-vars/var_bash1a.right
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
parameter 'abcdef'
|
||||||
|
varoffset2 'cdef'
|
||||||
|
varoffset-2 'ef'
|
||||||
|
literal '2' 'cdef'
|
||||||
|
literal '-2' 'abcdef'
|
||||||
|
literal ' -2' 'ef'
|
11
shell/ash_test/ash-vars/var_bash1a.tests
Executable file
11
shell/ash_test/ash-vars/var_bash1a.tests
Executable file
@ -0,0 +1,11 @@
|
|||||||
|
parameter=abcdef
|
||||||
|
offset=2
|
||||||
|
noffset=-2
|
||||||
|
echo "parameter '${parameter}'"
|
||||||
|
echo "varoffset2 '${parameter:${offset}}'"
|
||||||
|
echo "varoffset-2 '${parameter:${noffset}}'"
|
||||||
|
echo "literal '2' '${parameter:2}'"
|
||||||
|
# This is not inrpreted as ${VAR:POS{:LEN}},
|
||||||
|
# but as ${VAR:=WORD} - if VAR is unset or null, substitute WORD
|
||||||
|
echo "literal '-2' '${parameter:-2}'"
|
||||||
|
echo "literal ' -2' '${parameter: -2}'"
|
Loading…
x
Reference in New Issue
Block a user