public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
* [U-Boot] [PATCH 1/3] hush: Add default value substitution support
@ 2012-08-17 20:26 Joe Hershberger
  2012-08-17 20:26 ` [U-Boot] [PATCH 2/3] hush: Don't parse the contents of a dereferenced var Joe Hershberger
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: Joe Hershberger @ 2012-08-17 20:26 UTC (permalink / raw)
  To: u-boot

Use standard sh syntax:
${VAR:-default}
	Use default value: if VAR is set and non-null, expands to $VAR.
	Otherwise, expands to default.
${VAR:=default}
	Set default value: if VAR is set and non-null, expands to $VAR.
	Otherwise, sets hush VAR to default and expands to default.
${VAR:+default}
	If VAR is set and non-null, expands to the empty string.
	Otherwise, expands to default.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>
---
 common/hush.c | 43 ++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 40 insertions(+), 3 deletions(-)

diff --git a/common/hush.c b/common/hush.c
index 1eff182..39cf203 100644
--- a/common/hush.c
+++ b/common/hush.c
@@ -2743,13 +2743,50 @@ static int parse_group(o_string *dest, struct p_context *ctx,
 static char *lookup_param(char *src)
 {
 	char *p;
+	char *sep;
+	char *default_val = NULL;
+	int assign = 0;
+	int expand_empty = 0;
 
 	if (!src)
 		return NULL;
 
-		p = getenv(src);
-		if (!p)
-			p = get_local_var(src);
+	sep = strchr(src, ':');
+
+	if (sep) {
+		*sep = '\0';
+		if (*(sep + 1) == '-')
+			default_val = sep+2;
+		if (*(sep + 1) == '=') {
+			default_val = sep+2;
+			assign = 1;
+		}
+		if (*(sep + 1) == '+') {
+			default_val = sep+2;
+			expand_empty = 1;
+		}
+	}
+
+	p = getenv(src);
+	if (!p)
+		p = get_local_var(src);
+
+	if (!p || strlen(p) == 0) {
+		p = default_val;
+		if (assign) {
+			char *var = malloc(strlen(src)+strlen(default_val)+2);
+			if (var) {
+				sprintf(var, "%s=%s", src, default_val);
+				set_local_var(var, 0);
+			}
+			free(var);
+		}
+	} else if (expand_empty) {
+		p += strlen(p);
+	}
+
+	if (sep)
+		*sep = ':';
 
 	return p;
 }
-- 
1.7.11.5

^ permalink raw reply related	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2012-09-02 18:35 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-08-17 20:26 [U-Boot] [PATCH 1/3] hush: Add default value substitution support Joe Hershberger
2012-08-17 20:26 ` [U-Boot] [PATCH 2/3] hush: Don't parse the contents of a dereferenced var Joe Hershberger
2012-09-02 18:31   ` Wolfgang Denk
2012-08-17 20:26 ` [U-Boot] [PATCH 3/3] hush: Include file and line number when reporting syntax errors Joe Hershberger
2012-08-17 23:33   ` Mike Frysinger
2012-09-02 18:33   ` Wolfgang Denk
2012-08-17 23:31 ` [U-Boot] [PATCH 1/3] hush: Add default value substitution support Mike Frysinger
2012-09-02 18:35   ` Wolfgang Denk
2012-09-02 18:30 ` Wolfgang Denk

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox