3 static Word *subsub(Word*, char*, char*);
4 static Word *expandvar(char**);
5 static Bufblock *varname(char**);
6 static Word *extractpat(char*, char**, char*, char*);
7 static int submatch(char*, Word*, Word*, int*, char**);
8 static Word *varmatch(char *);
16 if(**s == '{') /* either ${name} or ${name: A%B==C%D}*/
23 w = varmatch(b->start);
29 * extract a variable name
42 n = chartorune(&r, cp);
48 if (b->current == b->start){
50 fprint(2, "missing variable name <%s>\n", *s);
65 sym = symlook(name, S_VAR, 0);
67 /* check for at least one non-NULL value */
68 for (w = sym->u.ptr; w; w = w->next)
81 char *cp, *begin, *end;
84 (*s)++; /* skip the '{' */
89 if (*cp == '}') { /* ${name} variant*/
90 (*s)++; /* skip the '}' */
91 w = varmatch(buf->start);
97 fprint(2, "bad variable name <%s>\n", buf->start);
102 end = charin(cp , "}");
105 fprint(2, "missing '}': %s\n", begin);
111 sym = symlook(buf->start, S_VAR, 0);
112 if(sym == 0 || sym->u.value == 0)
113 w = newword(buf->start);
115 w = subsub(sym->u.ptr, cp, end);
121 extractpat(char *s, char **r, char *term, char *end)
127 cp = charin(s, term);
144 subsub(Word *v, char *s, char *end)
147 Word *head, *tail, *w, *h;
152 a = extractpat(s, &cp, "=%&", end);
155 b = extractpat(cp+1, &cp, "=", end);
157 c = extractpat(cp+1, &cp, "&%", end);
165 for(; v; v = v->next){
167 if(submatch(v->s, a, b, &nmid, &enda)){
168 /* enda points to end of A match in source;
169 * nmid = number of chars between end of A and start of B
176 if(PERCENT(*cp) && nmid > 0){
178 bufcpy(buf, w->s, strlen(w->s));
179 bufcpy(buf, enda, nmid);
182 w->s = strdup(buf->start);
184 bufcpy(buf, enda, nmid);
186 h = w = newword(buf->start);
188 buf->current = buf->start;
193 bufcpy(buf, w->s, strlen(w->s));
194 bufcpy(buf, d->s, strlen(d->s));
197 w->s = strdup(buf->start);
198 w->next = wdup(d->next);
201 buf->current = buf->start;
207 h = w = newword(v->s);
224 submatch(char *s, Word *a, Word *b, int *nmid, char **enda)
231 for(w = a; w; w = w->next){
233 if(strncmp(s, w->s, n) == 0)
236 if(a && w == 0) /* a == NULL matches everything*/
239 *enda = s+n; /* pointer to end a A part match */
240 *nmid = strlen(s)-n; /* size of remainder of source */
242 for(w = b; w; w = w->next){
244 if(strcmp(w->s, end-n) == 0){
249 if(b && w == 0) /* b == NULL matches everything */