Allow multiple substitutions in a single format.

This commit is contained in:
nicm 2019-11-25 15:02:48 +00:00
parent 20c1f1aec6
commit 5d0504ee11

View File

@ -1567,8 +1567,8 @@ format_replace(struct format_tree *ft, const char *key, size_t keylen,
size_t valuelen; size_t valuelen;
int modifiers = 0, limit = 0, j; int modifiers = 0, limit = 0, j;
struct format_modifier *list, *fm, *cmp = NULL, *search = NULL; struct format_modifier *list, *fm, *cmp = NULL, *search = NULL;
struct format_modifier *sub = NULL; struct format_modifier **sub = NULL;
u_int i, count; u_int i, count, nsub = 0;
/* Make a copy of the key. */ /* Make a copy of the key. */
copy = copy0 = xstrndup(key, keylen); copy = copy0 = xstrndup(key, keylen);
@ -1597,7 +1597,9 @@ format_replace(struct format_tree *ft, const char *key, size_t keylen,
case 's': case 's':
if (fm->argc < 2) if (fm->argc < 2)
break; break;
sub = fm; sub = xreallocarray (sub, nsub + 1,
sizeof *sub);
sub[nsub++] = fm;
break; break;
case '=': case '=':
if (fm->argc < 1) if (fm->argc < 1)
@ -1809,10 +1811,10 @@ done:
} }
/* Perform substitution if any. */ /* Perform substitution if any. */
if (sub != NULL) { for (i = 0; i < nsub; i++) {
left = format_expand(ft, sub->argv[0]); left = format_expand(ft, sub[i]->argv[0]);
right = format_expand(ft, sub->argv[1]); right = format_expand(ft, sub[i]->argv[1]);
new = format_sub(sub, value, left, right); new = format_sub(sub[i], value, left, right);
format_log(ft, "substitute '%s' to '%s': %s", left, right, new); format_log(ft, "substitute '%s' to '%s': %s", left, right, new);
free(value); free(value);
value = new; value = new;
@ -1855,12 +1857,15 @@ done:
format_log(ft, "replaced '%s' with '%s'", copy0, value); format_log(ft, "replaced '%s' with '%s'", copy0, value);
free(value); free(value);
free(sub);
format_free_modifiers(list, count); format_free_modifiers(list, count);
free(copy0); free(copy0);
return (0); return (0);
fail: fail:
format_log(ft, "failed %s", copy0); format_log(ft, "failed %s", copy0);
free(sub);
format_free_modifiers(list, count); format_free_modifiers(list, count);
free(copy0); free(copy0);
return (-1); return (-1);