From: Robert Pengelly Date: Wed, 17 Sep 2025 10:25:01 +0000 (+0100) Subject: Implemented subst X-Git-Url: https://git.candlhat.org/?a=commitdiff_plain;h=918d6ba1953e0374af03bff9538efe1a9af8fdbf;p=xmake.git Implemented subst --- diff --git a/variable.c b/variable.c index ef98a44..b6f645f 100644 --- a/variable.c +++ b/variable.c @@ -50,6 +50,77 @@ static char *func_error (const char *filename, unsigned long line_no, char *inpu } +static char *func_subst (const char *filename, unsigned long line_no, char *input) { + + char *src, *dst, *new_body, *old_body, *p; + unsigned long size, pos, l, dst_len, src_len; + + (void) filename; + (void) line_no; + + src = input; + + if (!(dst = strstr (src, ",")) || !dst[0]) { + return 0; + } + + *dst++ = 0; + + if (!(old_body = strstr (dst, ",")) || !old_body[0]) { + return 0; + } + + *old_body++ = 0; + + size = 16 + strlen (old_body); + new_body = xmalloc (size); + + dst_len = strlen (dst); + src_len = strlen (src); + + pos = 0; + + while (*old_body) { + + if ((p = strstr (old_body, src))) { + + l = p - old_body; + + if (l + dst_len + 1 >= size - pos) { + + size += l + dst_len + 1 + strlen (old_body); + new_body = xrealloc (new_body, size); + + } + + memcpy (new_body + pos, old_body, l); + pos += l; + + memcpy (new_body + pos, dst, dst_len + 1); + pos += dst_len; + + old_body = p + src_len; + + } else { + + if (strlen (old_body) >= size - pos) { + + size += strlen (old_body) + size - pos + 1; + new_body = xrealloc (new_body, size); + + } + + memcpy (new_body + pos, old_body, strlen (old_body) + 1); + break; + + } + + } + + return new_body; + +} + #if defined (_WIN32) || defined (__WIN32__) # include static char *internal_commands[] = { @@ -462,6 +533,8 @@ static struct builtin_function builtin_functions[] ={ #endif { "eval", &func_eval }, + { "subst", &func_subst }, + { 0, 0 } };