9
0
Fork 0

Add shell_expand function

shell_expand expands shell variables in a string.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
This commit is contained in:
Sascha Hauer 2014-03-28 09:32:56 +01:00
parent 237cbf1ac7
commit 45693e0265
2 changed files with 42 additions and 0 deletions

View File

@ -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)

View File

@ -165,6 +165,15 @@ void arch_shutdown(void);
int run_shell(void);
#ifdef CONFIG_SHELL_HUSH
char *shell_expand(char *str);
#else
static inline char *shell_expand(char *str)
{
return strdup(str);
}
#endif
/* Force a compilation error if condition is true */
#define BUILD_BUG_ON(condition) ((void)BUILD_BUG_ON_ZERO(condition))