summaryrefslogtreecommitdiffstats
path: root/common/hush.c
diff options
context:
space:
mode:
authorSascha Hauer <s.hauer@pengutronix.de>2014-03-28 09:32:56 +0100
committerSascha Hauer <s.hauer@pengutronix.de>2014-03-28 09:36:36 +0100
commit45693e0265d086b031739132d64d240b9afd038e (patch)
tree08ed7f38ed96e0538e79effaa0f8c89bf5b40f94 /common/hush.c
parent237cbf1ac7a4799bc6e6b4ce0233a5dafa8de94a (diff)
downloadbarebox-45693e0265d086b031739132d64d240b9afd038e.tar.gz
Add shell_expand function
shell_expand expands shell variables in a string. Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'common/hush.c')
-rw-r--r--common/hush.c33
1 files changed, 33 insertions, 0 deletions
diff --git a/common/hush.c b/common/hush.c
index bd534c1..1447fdb 100644
--- a/common/hush.c
+++ b/common/hush.c
@@ -1637,6 +1637,39 @@ static void update_ifs_map(void)
mapset(ifs, 2); /* also flow through if quoted */
}
+/*
+ * shell_expand - Expand shell variables in a string.
+ * @str: The input string containing shell variables like
+ * $var or ${var}
+ * Return: The expanded string. Must be freed with free().
+ */
+char *shell_expand(char *str)
+{
+ struct p_context ctx = {};
+ o_string o = {};
+ char *res, *parsed;
+
+ remove_quotes_in_str(str);
+
+ o.quote = 1;
+
+ initialize_context(&ctx);
+
+ parse_string(&o, &ctx, str);
+
+ parsed = xmemdup(o.data, o.length + 1);
+ parsed[o.length] = 0;
+
+ res = insert_var_value(parsed);
+ if (res != parsed)
+ free(parsed);
+
+ free_pipe_list(ctx.list_head, 0);
+ b_free(&o);
+
+ return res;
+}
+
/* most recursion does not come through here, the exeception is
* from builtin_source() */
static int parse_stream_outer(struct p_context *ctx, struct in_str *inp, int flag)