fix empty string interpolation bugs (Michael Teichgräber)

This commit is contained in:
rsc 2007-03-26 17:27:08 +00:00
parent edd308cfa2
commit 6c4c5c5b95
2 changed files with 14 additions and 9 deletions

View file

@ -5,7 +5,7 @@ static Word *expandvar(char**);
static Bufblock *varname(char**); static Bufblock *varname(char**);
static Word *extractpat(char*, char**, char*, char*); static Word *extractpat(char*, char**, char*, char*);
static int submatch(char*, Word*, Word*, int*, char**); static int submatch(char*, Word*, Word*, int*, char**);
static Word *varmatch(char *, char**); static Word *varmatch(char *);
Word * Word *
varsub(char **s) varsub(char **s)
@ -20,7 +20,7 @@ varsub(char **s)
if(b == 0) if(b == 0)
return 0; return 0;
w = varmatch(b->start, s); w = varmatch(b->start);
freebuf(b); freebuf(b);
return w; return w;
} }
@ -57,11 +57,10 @@ varname(char **s)
} }
static Word* static Word*
varmatch(char *name, char **s) varmatch(char *name)
{ {
Word *w; Word *w;
Symtab *sym; Symtab *sym;
char *cp;
sym = symlook(name, S_VAR, 0); sym = symlook(name, S_VAR, 0);
if(sym){ if(sym){
@ -70,9 +69,6 @@ varmatch(char *name, char **s)
if(w->s && *w->s) if(w->s && *w->s)
return wdup(w); return wdup(w);
} }
for(cp = *s; *cp == ' ' || *cp == '\t'; cp++) /* skip trailing whitespace */
;
*s = cp;
return 0; return 0;
} }
@ -92,7 +88,7 @@ expandvar(char **s)
cp = *s; cp = *s;
if (*cp == '}') { /* ${name} variant*/ if (*cp == '}') { /* ${name} variant*/
(*s)++; /* skip the '}' */ (*s)++; /* skip the '}' */
w = varmatch(buf->start, s); w = varmatch(buf->start);
freebuf(buf); freebuf(buf);
return w; return w;
} }

View file

@ -97,12 +97,15 @@ nextword(char **s)
Word *head, *tail, *w; Word *head, *tail, *w;
Rune r; Rune r;
char *cp; char *cp;
int empty;
cp = *s; cp = *s;
b = newbuf(); b = newbuf();
restart:
head = tail = 0; head = tail = 0;
while(*cp == ' ' || *cp == '\t') /* leading white space */ while(*cp == ' ' || *cp == '\t') /* leading white space */
cp++; cp++;
empty = 1;
while(*cp){ while(*cp){
cp += chartorune(&r, cp); cp += chartorune(&r, cp);
switch(r) switch(r)
@ -114,6 +117,7 @@ nextword(char **s)
case '\\': case '\\':
case '\'': case '\'':
case '"': case '"':
empty = 0;
cp = shellt->expandquote(cp, r, b); cp = shellt->expandquote(cp, r, b);
if(cp == 0){ if(cp == 0){
fprint(2, "missing closing quote: %s\n", *s); fprint(2, "missing closing quote: %s\n", *s);
@ -122,8 +126,12 @@ nextword(char **s)
break; break;
case '$': case '$':
w = varsub(&cp); w = varsub(&cp);
if(w == 0) if(w == 0){
if(empty)
goto restart;
break; break;
}
empty = 0;
if(b->current != b->start){ if(b->current != b->start){
bufcpy(b, w->s, strlen(w->s)); bufcpy(b, w->s, strlen(w->s));
insert(b, 0); insert(b, 0);
@ -147,6 +155,7 @@ nextword(char **s)
tail = tail->next; tail = tail->next;
break; break;
default: default:
empty = 0;
rinsert(b, r); rinsert(b, r);
break; break;
} }