diff mbox

[U-Boot] common: cli_simple: Recursively parse variables

Message ID 1449249451-8945-1-git-send-email-nm@ti.com
State Deferred
Delegated to: Tom Rini
Headers show

Commit Message

Nishanth Menon Dec. 4, 2015, 5:17 p.m. UTC
When we use the following in bootargs:
v1=abc
v2=123-${v1}
echo $v2
we get 123-${v1}
This is because we do not recursively check to see if v2 by itself has
a hidden variable. Fix the same with recursive call

Signed-off-by: Nishanth Menon <nm@ti.com>
---

Testing with sandbox: http://pastebin.ubuntu.com/13672432/

 common/cli_simple.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

Comments

Wolfgang Denk Dec. 4, 2015, 8:17 p.m. UTC | #1
Dear Nishanth Menon,

In message <1449249451-8945-1-git-send-email-nm@ti.com> you wrote:
> When we use the following in bootargs:
> v1=abc
> v2=123-${v1}
> echo $v2
> we get 123-${v1}
> This is because we do not recursively check to see if v2 by itself has
> a hidden variable. Fix the same with recursive call

Are you sure this is a good idea?

Current behaviour is what a standard shell would do as well:

bash$ v1=abc
bash$ v2='123-${v1}'
bash$ echo $v2
123-${v1}

I think your change would causes non-standard shell behaviour.

If you want to evaluate variables, you have to do so as part of a
"run" command...

Best regards,

Wolfgang Denk
diff mbox

Patch

diff --git a/common/cli_simple.c b/common/cli_simple.c
index 9c3d073d583b..63bda32c57e4 100644
--- a/common/cli_simple.c
+++ b/common/cli_simple.c
@@ -134,11 +134,17 @@  void cli_simple_process_macros(const char *input, char *output)
 				envval = getenv(envname);
 
 				/* Copy into the line if it exists */
-				if (envval != NULL)
-					while ((*envval) && outputcnt) {
-						*(output++) = *(envval++);
+				if (envval != NULL) {
+					char finalval[CONFIG_SYS_CBSIZE], *f;
+
+					cli_simple_process_macros(envval,
+								  finalval);
+					f = finalval;
+					while ((*f) && outputcnt) {
+						*(output++) = *(f++);
 						outputcnt--;
 					}
+				}
 				/* Look for another '$' */
 				state = 0;
 			}