[U-Boot] [PATCH] common: cli_hush: Do a simple recursive variables parsing

Nishanth Menon nm at ti.com
Fri Dec 4 20:02:24 CET 2015


When we use the following in bootargs:
v1=abc
v2=123-${v1}
echo ${v2}
we get 123-${v1}
when we should have got
123-abc
This is because we do not recursively check to see if v2 by itself has
a hidden variable. Fix the same with recursive call.

NOTE: this is a limited implementation as the next level variable
default assignment etc would not function.

Signed-off-by: Nishanth Menon <nm at ti.com>
---
For next level recursion of variables to work, this patch depends on
https://patchwork.ozlabs.org/patch/552823/

Tested on Sandbox.

 common/cli_hush.c | 24 ++++++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)

diff --git a/common/cli_hush.c b/common/cli_hush.c
index 5a26af80c758..d56d6cfb131e 100644
--- a/common/cli_hush.c
+++ b/common/cli_hush.c
@@ -471,7 +471,7 @@ static int redirect_opt_num(o_string *o);
 static int process_command_subs(o_string *dest, struct p_context *ctx, struct in_str *input, int subst_end);
 static int parse_group(o_string *dest, struct p_context *ctx, struct in_str *input, int ch);
 #endif
-static char *lookup_param(char *src);
+static char *lookup_param(char *src, bool *b);
 static char *make_string(char **inp, int *nonnull);
 static int handle_dollar(o_string *dest, struct p_context *ctx, struct in_str *input);
 #ifndef __U_BOOT__
@@ -2740,7 +2740,7 @@ static int parse_group(o_string *dest, struct p_context *ctx,
 
 /* basically useful version until someone wants to get fancier,
  * see the bash man page under "Parameter Expansion" */
-static char *lookup_param(char *src)
+static char *lookup_param(char *src, bool *b)
 {
 	char *p;
 	char *sep;
@@ -2748,9 +2748,10 @@ static char *lookup_param(char *src)
 	int assign = 0;
 	int expand_empty = 0;
 
-	if (!src)
+	if (!src || !b)
 		return NULL;
 
+	*b = false;
 	sep = strchr(src, ':');
 
 	if (sep) {
@@ -2783,6 +2784,16 @@ static char *lookup_param(char *src)
 		}
 	} else if (expand_empty) {
 		p += strlen(p);
+	} else {
+		char f[CONFIG_SYS_CBSIZE];
+
+		if (strlen(p) < CONFIG_SYS_CBSIZE) {
+			cli_simple_process_macros(p, f);
+			if (strncmp(p, f, CONFIG_SYS_CBSIZE)) {
+				p = strdup(f);
+				*b = true;
+			}
+		}
 	}
 
 	if (sep)
@@ -3501,6 +3512,7 @@ static char *insert_var_value_sub(char *inp, int tag_subst)
 	int len;
 	int done = 0;
 	char *p, *p1, *res_str = NULL;
+	bool b;
 
 	while ((p = strchr(inp, SPECIAL_VAR_SYMBOL))) {
 		/* check the beginning of the string for normal characters */
@@ -3516,7 +3528,8 @@ static char *insert_var_value_sub(char *inp, int tag_subst)
 		p = strchr(inp, SPECIAL_VAR_SYMBOL);
 		*p = '\0';
 		/* look up the value to substitute */
-		if ((p1 = lookup_param(inp))) {
+		p1 = lookup_param(inp, &b);
+		if (p1) {
 			if (tag_subst)
 				len = res_str_len + strlen(p1) + 2;
 			else
@@ -3544,6 +3557,9 @@ static char *insert_var_value_sub(char *inp, int tag_subst)
 				strcpy((res_str + res_str_len), p1);
 
 			res_str_len = len;
+
+			if (b)
+				free(p1);
 		}
 		*p = SPECIAL_VAR_SYMBOL;
 		inp = ++p;
-- 
2.6.2.402.g2635c2b



More information about the U-Boot mailing list