ash: fix breakage introduced in rev 21481.

Fixes ash-vars/var_posix1.tests testsuite entry.
This commit is contained in:
Denis Vlasenko 2008-04-14 01:59:53 +00:00
parent b15ebe46fc
commit c7131c3e58

View File

@ -5691,13 +5691,39 @@ static char *
scanleft(char *startp, char *rmesc, char *rmescend ATTRIBUTE_UNUSED, char *str, int quotes, scanleft(char *startp, char *rmesc, char *rmescend ATTRIBUTE_UNUSED, char *str, int quotes,
int zero) int zero)
{ {
char *loc, *loc2, *full; // This commented out code was added by James Simmons <jsimmons@infradead.org>
// as part of a larger change when he added support for ${var/a/b}.
// However, it broke # and % operators:
//
//var=ababcdcd
// ok bad
//echo ${var#ab} abcdcd abcdcd
//echo ${var##ab} abcdcd abcdcd
//echo ${var#a*b} abcdcd ababcdcd (!)
//echo ${var##a*b} cdcd cdcd
//echo ${var#?} babcdcd ababcdcd (!)
//echo ${var##?} babcdcd babcdcd
//echo ${var#*} ababcdcd babcdcd (!)
//echo ${var##*}
//echo ${var%cd} ababcd ababcd
//echo ${var%%cd} ababcd abab (!)
//echo ${var%c*d} ababcd ababcd
//echo ${var%%c*d} abab ababcdcd (!)
//echo ${var%?} ababcdc ababcdc
//echo ${var%%?} ababcdc ababcdcd (!)
//echo ${var%*} ababcdcd ababcdcd
//echo ${var%%*}
//
// Commenting it back out helped. Remove it completely if it really
// is not needed.
char *loc, *loc2; //, *full;
char c; char c;
loc = startp; loc = startp;
loc2 = rmesc; loc2 = rmesc;
do { do {
int match = strlen(str); int match; // = strlen(str);
const char *s = loc2; const char *s = loc2;
c = *loc2; c = *loc2;
@ -5705,23 +5731,24 @@ scanleft(char *startp, char *rmesc, char *rmescend ATTRIBUTE_UNUSED, char *str,
*loc2 = '\0'; *loc2 = '\0';
s = rmesc; s = rmesc;
} }
match = pmatch(str, s); // this line was deleted
// chop off end if its '*' // // chop off end if its '*'
full = strrchr(str, '*'); // full = strrchr(str, '*');
if (full && full != str) // if (full && full != str)
match--; // match--;
//
// If str starts with '*' replace with s. // // If str starts with '*' replace with s.
if ((*str == '*') && strlen(s) >= match) { // if ((*str == '*') && strlen(s) >= match) {
full = xstrdup(s); // full = xstrdup(s);
strncpy(full+strlen(s)-match+1, str+1, match-1); // strncpy(full+strlen(s)-match+1, str+1, match-1);
} else // } else
full = xstrndup(str, match); // full = xstrndup(str, match);
match = strncmp(s, full, strlen(full)); // match = strncmp(s, full, strlen(full));
free(full); // free(full);
//
*loc2 = c; *loc2 = c;
if (!match) if (match) // if (!match)
return loc; return loc;
if (quotes && *loc == CTLESC) if (quotes && *loc == CTLESC)
loc++; loc++;