[cmdline] Fix multi-layer variable expansion

Expansion of ${${foo}} will currently fail, because the first
opening "${" will be incorrectly matched against the first closing
"}", leading to an attempt to expand the variable "${foo".

Fix by ensuring that the most recent opening "${" is used to match
against the first closing "}".

Total cost: 8 bytes.  :)

Signed-off-by: Michael Brown <mcb30@ipxe.org>
pull/1/head
Michael Brown 2010-11-22 21:31:00 +00:00
parent a180c7526c
commit c1327e43ab
1 changed files with 12 additions and 8 deletions

View File

@ -115,17 +115,21 @@ static char * expand_command ( const char *command ) {
head = expcmd; head = expcmd;
/* Locate opener */ /* Locate setting to be expanded */
start = strstr ( expcmd, "${" ); start = NULL;
if ( ! start ) end = NULL;
for ( tmp = expcmd ; *tmp ; tmp++ ) {
if ( ( tmp[0] == '$' ) && ( tmp[1] == '{' ) )
start = tmp;
if ( tmp[0] == '}' )
end = tmp;
if ( start && end )
break;
}
if ( ! ( start && end ) )
break; break;
*start = '\0'; *start = '\0';
name = ( start + 2 ); name = ( start + 2 );
/* Locate closer */
end = strstr ( name, "}" );
if ( ! end )
break;
*end = '\0'; *end = '\0';
tail = ( end + 1 ); tail = ( end + 1 );