feat(expansion) 10291 expansion

This commit is contained in:
william.valenduc 2026-01-19 17:55:16 +00:00
parent 2ebf56dde7
commit b443cd1876
4 changed files with 75 additions and 14 deletions

View file

@ -86,10 +86,21 @@ static bool expand_var(char **str, size_t pos, const struct hash_map *vars)
size_t r = parse_var_name(*str + pos, &var_name);
if (r > 0 && var_name != NULL)
{
char *value = get_var_or_env(vars, var_name);
if (value == NULL)
// Undefined variable: expand to empty string
value = "";
char *value;
char rnd_str[10]; // max 5 digits + null terminator
if (strcmp(var_name, "RANDOM") == 0)
{
short rnd = short_random();
snprintf(rnd_str, 10, "%d", rnd);
value = rnd_str;
}
else
{
value = get_var_or_env(vars, var_name);
if (value == NULL)
// Undefined variable: expand to empty string
value = "";
}
char *p = insert_into(*str, value, pos, r);
free(var_name);