summaryrefslogtreecommitdiff
path: root/expand.c
diff options
context:
space:
mode:
Diffstat (limited to 'expand.c')
-rw-r--r--expand.c80
1 files changed, 39 insertions, 41 deletions
diff --git a/expand.c b/expand.c
index 2c8b4b6..4440192 100644
--- a/expand.c
+++ b/expand.c
@@ -304,51 +304,49 @@ variable_expand_string (char *line, char *string, long length)
if (v == 0)
warn_undefined (beg, colon - beg);
+ /* If the variable is not empty, perform the
+ substitution. */
if (v != 0 && *v->value != '\0')
{
- char *value = (v->recursive ? recursively_expand (v)
+ char *pattern, *replace, *ppercent, *rpercent;
+ char *value = (v->recursive
+ ? recursively_expand (v)
: v->value);
- char *pattern, *percent;
- if (free_beg)
- {
- *subst_end = '\0';
- pattern = subst_beg;
- }
- else
- {
- pattern = (char *) alloca (subst_end - subst_beg
- + 1);
- bcopy (subst_beg, pattern, subst_end - subst_beg);
- pattern[subst_end - subst_beg] = '\0';
- }
- percent = find_percent (pattern);
- if (percent != 0)
- {
- char *replace;
- if (free_beg)
- {
- *replace_end = '\0';
- replace = replace_beg;
- }
- else
- {
- replace = (char *) alloca (replace_end
- - replace_beg
- + 1);
- bcopy (replace_beg, replace,
- replace_end - replace_beg);
- replace[replace_end - replace_beg] = '\0';
- }
-
- o = patsubst_expand (o, value, pattern, replace,
- percent, (char *) 0);
- }
+
+ /* Copy the pattern and the replacement. Add in an
+ extra % at the beginning to use in case there
+ isn't one in the pattern. */
+ pattern = (char *) alloca (subst_end - subst_beg + 2);
+ *(pattern++) = '%';
+ bcopy (subst_beg, pattern, subst_end - subst_beg);
+ pattern[subst_end - subst_beg] = '\0';
+
+ replace = (char *) alloca (replace_end
+ - replace_beg + 2);
+ *(replace++) = '%';
+ bcopy (replace_beg, replace,
+ replace_end - replace_beg);
+ replace[replace_end - replace_beg] = '\0';
+
+ /* Look for %. Set the percent pointers properly
+ based on whether we find one or not. */
+ ppercent = find_percent (pattern);
+ if (ppercent)
+ {
+ ++ppercent;
+ rpercent = 0;
+ }
else
- o = subst_expand (o, value,
- pattern, replace_beg,
- strlen (pattern),
- end - replace_beg,
- 0, 1);
+ {
+ ppercent = pattern;
+ rpercent = replace;
+ --pattern;
+ --replace;
+ }
+
+ o = patsubst_expand (o, value, pattern, replace,
+ ppercent, rpercent);
+
if (v->recursive)
free (value);
}