mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: barebox@lists.infradead.org
Subject: [PATCH 1/3] Add shell_expand function
Date: Fri, 28 Mar 2014 10:14:41 +0100	[thread overview]
Message-ID: <1395998083-18979-2-git-send-email-s.hauer@pengutronix.de> (raw)
In-Reply-To: <1395998083-18979-1-git-send-email-s.hauer@pengutronix.de>

shell_expand expands shell variables in a string.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 common/hush.c    | 33 +++++++++++++++++++++++++++++++++
 include/common.h |  9 +++++++++
 2 files changed, 42 insertions(+)

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)
diff --git a/include/common.h b/include/common.h
index 6987b4f..bfd3ce8 100644
--- a/include/common.h
+++ b/include/common.h
@@ -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))
 
-- 
1.9.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  reply	other threads:[~2014-03-28  9:15 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-03-28  9:14 Add support for creating menus from a directory structure Sascha Hauer
2014-03-28  9:14 ` Sascha Hauer [this message]
2014-03-28  9:14 ` [PATCH 2/3] add menutree command Sascha Hauer
2014-03-28  9:14 ` [PATCH 3/3] defenv-2: replace menu with menutree Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1395998083-18979-2-git-send-email-s.hauer@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox