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

* [U-Boot] [PATCH 2/3] hush: Don't parse the contents of a dereferenced var
  2012-08-17 20:26 [U-Boot] [PATCH 1/3] hush: Add default value substitution support Joe Hershberger
@ 2012-08-17 20:26 ` 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
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 9+ messages in thread
From: Joe Hershberger @ 2012-08-17 20:26 UTC (permalink / raw)
  To: u-boot

When a variable which contains a user-supplied value is dereferenced
(e.g. to be echo'ed), make sure that the value is not further parsed
by hush.

Set the hush local variable "HUSH_NO_EVAL=1" to enable this behavior.

Without this patch, a sequence like this occurs:

	Panda # env set my_user_string Bob\'s favorite device
	Panda # print my_user_string
	my_user_string=Bob's favorite device
	Panda # echo $my_user_string
	syntax error hush.c:3007

With this patch, it looks like this:

	Panda # HUSH_NO_EVAL=1
	Panda # env set my_user_string Bob\'s favorite device
	Panda # print my_user_string
	my_user_string=Bob's favorite device
	Panda # echo $my_user_string
	Bob's favorite device

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

diff --git a/common/hush.c b/common/hush.c
index 39cf203..4c84c2f 100644
--- a/common/hush.c
+++ b/common/hush.c
@@ -127,6 +127,7 @@
 #endif
 #endif
 #define SPECIAL_VAR_SYMBOL 03
+#define SUBSTED_VAR_SYMBOL 04
 #ifndef __U_BOOT__
 #define FLAG_EXIT_FROM_LOOP 1
 #define FLAG_PARSE_SEMICOLON (1 << 1)		/* symbol ';' is special for parser */
@@ -499,6 +500,7 @@ static void remove_bg_job(struct pipe *pi);
 /*     local variable support */
 static char **make_list_in(char **inp, char *name);
 static char *insert_var_value(char *inp);
+static char *insert_var_value_sub(char *inp, int tag_subst);
 
 #ifndef __U_BOOT__
 /* Table of built-in functions.  They can be forked or not, depending on
@@ -3088,6 +3090,21 @@ int parse_stream(o_string *dest, struct p_context *ctx,
 			return 1;
 			break;
 #endif
+		case SUBSTED_VAR_SYMBOL:
+			dest->nonnull = 1;
+			while (ch = b_getch(input), ch != EOF &&
+			    ch != SUBSTED_VAR_SYMBOL) {
+				debug_printf("subst, pass=%d\n", ch);
+				if (input->__promptme == 0)
+					return 1;
+				b_addchr(dest, ch);
+			}
+			debug_printf("subst, term=%d\n", ch);
+			if (ch == EOF) {
+				syntax();
+				return 1;
+			}
+			break;
 		default:
 			syntax();   /* this is really an internal logic error */
 			return 1;
@@ -3129,6 +3146,10 @@ void update_ifs_map(void)
 	mapset((uchar *)"\\$'\"`", 3);      /* never flow through */
 	mapset((uchar *)"<>;&|(){}#", 1);   /* flow through if quoted */
 #else
+	{
+		uchar subst[2] = {SUBSTED_VAR_SYMBOL, 0};
+		mapset(subst, 3);       /* never flow through */
+	}
 	mapset((uchar *)"\\$'\"", 3);       /* never flow through */
 	mapset((uchar *)";&|#", 1);         /* flow through if quoted */
 #endif
@@ -3468,25 +3489,57 @@ final_return:
 
 static char *insert_var_value(char *inp)
 {
+	return insert_var_value_sub(inp, 0);
+}
+
+static char *insert_var_value_sub(char *inp, int tag_subst)
+{
 	int res_str_len = 0;
 	int len;
 	int done = 0;
 	char *p, *p1, *res_str = NULL;
 
 	while ((p = strchr(inp, SPECIAL_VAR_SYMBOL))) {
+		/* check the beginning of the string for normal charachters */
 		if (p != inp) {
+			/* copy any charachters to the result string */
 			len = p - inp;
 			res_str = xrealloc(res_str, (res_str_len + len));
 			strncpy((res_str + res_str_len), inp, len);
 			res_str_len += len;
 		}
 		inp = ++p;
+		/* find the ending marker */
 		p = strchr(inp, SPECIAL_VAR_SYMBOL);
 		*p = '\0';
+		/* look up the value to substitute */
 		if ((p1 = lookup_param(inp))) {
-			len = res_str_len + strlen(p1);
+			if (tag_subst)
+				len = res_str_len + strlen(p1) + 2;
+			else
+				len = res_str_len + strlen(p1);
 			res_str = xrealloc(res_str, (1 + len));
-			strcpy((res_str + res_str_len), p1);
+			if (tag_subst) {
+				/*
+				 * copy the variable value to the result
+				 * string
+				 */
+				strcpy((res_str + res_str_len + 1), p1);
+
+				/*
+				 * mark the replaced text to be accepted as
+				 * is
+				 */
+				res_str[res_str_len] = SUBSTED_VAR_SYMBOL;
+				res_str[res_str_len + 1 + strlen(p1)] =
+					SUBSTED_VAR_SYMBOL;
+			} else
+				/*
+				 * copy the variable value to the result
+				 * string
+				 */
+				strcpy((res_str + res_str_len), p1);
+
 			res_str_len = len;
 		}
 		*p = SPECIAL_VAR_SYMBOL;
@@ -3550,9 +3603,14 @@ static char * make_string(char ** inp)
 	char *str = NULL;
 	int n;
 	int len = 2;
+	char *noeval_str;
+	int noeval = 0;
 
+	noeval_str = get_local_var("HUSH_NO_EVAL");
+	if (noeval_str != NULL && *noeval_str != '0' && *noeval_str != '\0')
+		noeval = 1;
 	for (n = 0; inp[n]; n++) {
-		p = insert_var_value(inp[n]);
+		p = insert_var_value_sub(inp[n], noeval);
 		str = xrealloc(str, (len + strlen(p)));
 		if (n) {
 			strcat(str, " ");
-- 
1.7.11.5

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

* [U-Boot] [PATCH 3/3] hush: Include file and line number when reporting syntax errors
  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-08-17 20:26 ` 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:30 ` Wolfgang Denk
  3 siblings, 2 replies; 9+ messages in thread
From: Joe Hershberger @ 2012-08-17 20:26 UTC (permalink / raw)
  To: u-boot

Make debugging script problems easier just like non-u-boot.

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

diff --git a/common/hush.c b/common/hush.c
index 4c84c2f..1db8a53 100644
--- a/common/hush.c
+++ b/common/hush.c
@@ -383,9 +383,11 @@ static inline void debug_printf(const char *format, ...) { }
 #define final_printf debug_printf
 
 #ifdef __U_BOOT__
-static void syntax_err(void) {
-	 printf("syntax error\n");
+static void __syntax_err(char *file, int line)
+{
+	 printf("syntax error %s:%d\n", file, line);
 }
+#define syntax_err() __syntax_err(__FILE__, __LINE__)
 #else
 static void __syntax(char *file, int line) {
 	error_msg("syntax error %s:%d", file, line);
-- 
1.7.11.5

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

* [U-Boot] [PATCH 1/3] hush: Add default value substitution support
  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-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:31 ` Mike Frysinger
  2012-09-02 18:35   ` Wolfgang Denk
  2012-09-02 18:30 ` Wolfgang Denk
  3 siblings, 1 reply; 9+ messages in thread
From: Mike Frysinger @ 2012-08-17 23:31 UTC (permalink / raw)
  To: u-boot

On Friday 17 August 2012 16:26:29 Joe Hershberger wrote:
> 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.

how about ${VAR-default} and ${VAR=default} and ${VAR+default} ?

> --- a/common/hush.c
> +++ b/common/hush.c
>
> +	if (sep) {
> +		*sep = '\0';
> +		if (*(sep + 1) == '-')

switch (sep[1]) {
case '-':
	...

> +			default_val = sep+2;

sep + 2

> +	if (!p || strlen(p) == 0) {

if (!p || !p[0])

> +		if (assign) {
> +			char *var = malloc(strlen(src)+strlen(default_val)+2);

please put spaces around the "+"

> +			if (var) {
> +				sprintf(var, "%s=%s", src, default_val);
> +				set_local_var(var, 0);
> +			}

isn't there a helper func for this ?
-mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20120817/bebecff0/attachment.pgp>

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

* [U-Boot] [PATCH 3/3] hush: Include file and line number when reporting syntax errors
  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
  1 sibling, 0 replies; 9+ messages in thread
From: Mike Frysinger @ 2012-08-17 23:33 UTC (permalink / raw)
  To: u-boot

On Friday 17 August 2012 16:26:31 Joe Hershberger wrote:
> Make debugging script problems easier just like non-u-boot.

err, but you're posting "hush.c" all the time, and the line number of the 
source code in hush.c, not the line of the shell script

> --- a/common/hush.c
> +++ b/common/hush.c
> 
>  #ifdef __U_BOOT__
> -static void syntax_err(void) {
> -	 printf("syntax error\n");
> +static void __syntax_err(char *file, int line)

const char *file

> +{
> +	 printf("syntax error %s:%d\n", file, line);
>  }
> +#define syntax_err() __syntax_err(__FILE__, __LINE__)

shouldn't this be behind a DEBUG define ?
-mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20120817/eb3fd5ac/attachment.pgp>

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

* [U-Boot] [PATCH 1/3] hush: Add default value substitution support
  2012-08-17 20:26 [U-Boot] [PATCH 1/3] hush: Add default value substitution support Joe Hershberger
                   ` (2 preceding siblings ...)
  2012-08-17 23:31 ` [U-Boot] [PATCH 1/3] hush: Add default value substitution support Mike Frysinger
@ 2012-09-02 18:30 ` Wolfgang Denk
  3 siblings, 0 replies; 9+ messages in thread
From: Wolfgang Denk @ 2012-09-02 18:30 UTC (permalink / raw)
  To: u-boot

Dear Joe Hershberger,

In message <1345235191-13757-1-git-send-email-joe.hershberger@ni.com> you wrote:
> 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(-)

Applied, thanks.

Best regards,

Wolfgang Denk

-- 
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
It is the quality rather than the quantity that matters.
- Lucius Annaeus Seneca (4 B.C. - A.D. 65)

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

* [U-Boot] [PATCH 2/3] hush: Don't parse the contents of a dereferenced var
  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
  0 siblings, 0 replies; 9+ messages in thread
From: Wolfgang Denk @ 2012-09-02 18:31 UTC (permalink / raw)
  To: u-boot

Dear Joe Hershberger,

In message <1345235191-13757-2-git-send-email-joe.hershberger@ni.com> you wrote:
> When a variable which contains a user-supplied value is dereferenced
> (e.g. to be echo'ed), make sure that the value is not further parsed
> by hush.
> 
> Set the hush local variable "HUSH_NO_EVAL=1" to enable this behavior.
> 
> Without this patch, a sequence like this occurs:
> 
> 	Panda # env set my_user_string Bob\'s favorite device
> 	Panda # print my_user_string
> 	my_user_string=Bob's favorite device
> 	Panda # echo $my_user_string
> 	syntax error hush.c:3007
> 
> With this patch, it looks like this:
> 
> 	Panda # HUSH_NO_EVAL=1
> 	Panda # env set my_user_string Bob\'s favorite device
> 	Panda # print my_user_string
> 	my_user_string=Bob's favorite device
> 	Panda # echo $my_user_string
> 	Bob's favorite device
> 
> Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>
> ---
>  common/hush.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
>  1 file changed, 61 insertions(+), 3 deletions(-)

Applied, thanks.

Best regards,

Wolfgang Denk

-- 
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
Democracy is mob rule, but with income taxes.

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

* [U-Boot] [PATCH 3/3] hush: Include file and line number when reporting syntax errors
  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
  1 sibling, 0 replies; 9+ messages in thread
From: Wolfgang Denk @ 2012-09-02 18:33 UTC (permalink / raw)
  To: u-boot

Dear Joe Hershberger,

In message <1345235191-13757-3-git-send-email-joe.hershberger@ni.com> you wrote:
> Make debugging script problems easier just like non-u-boot.
> 
> Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>
> ---
>  common/hush.c | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)

Skipped (i. e. not applied).  See Mike's comments.

Best regards,

Wolfgang Denk

-- 
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
News is what a chap who doesn't care much  about  anything  wants  to
read. And it's only news until he's read it. After that it's dead.
                           - Evelyn Waugh _Scoop_ (1938) bk. 1, ch. 5

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

* [U-Boot] [PATCH 1/3] hush: Add default value substitution support
  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
  0 siblings, 0 replies; 9+ messages in thread
From: Wolfgang Denk @ 2012-09-02 18:35 UTC (permalink / raw)
  To: u-boot

Dear Joe,

In message <201208171931.35608.vapier@gentoo.org> Mike Frysinger wrote:
>
> > ${VAR:-default}
...
> > ${VAR:=default}
...
> > ${VAR:+default}
...
> how about ${VAR-default} and ${VAR=default} and ${VAR+default} ?

Maybe we (= you?) can add these, too?

> > +		if (assign) {
> > +			char *var = malloc(strlen(src)+strlen(default_val)+2);
> 
> please put spaces around the "+"

Ah, I missed that.

> > +			if (var) {
> > +				sprintf(var, "%s=%s", src, default_val);
> > +				set_local_var(var, 0);
> > +			}
> 
> isn't there a helper func for this ?

Eventually we can lean this up in another patch, please?

Best regards,

Wolfgang Denk

-- 
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
The ideal situation is to have massive computing power right at home.
Something that dims the streetlights and shrinks the picture  on  the
neighbours' TVs when you boot it up.

^ permalink raw reply	[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