mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/2] hush: expand variables before globbing in for loops
@ 2013-02-21 14:17 Sascha Hauer
  2013-02-21 14:17 ` [PATCH 2/2] hush: implement $* Sascha Hauer
  2013-02-22  8:13 ` [PATCH 1/2] hush: expand variables before globbing in for loops Sascha Hauer
  0 siblings, 2 replies; 3+ messages in thread
From: Sascha Hauer @ 2013-02-21 14:17 UTC (permalink / raw)
  To: barebox

The following does work:

for i in *; do echo $i; done

but the following does not:

a="*"; for i in $a; do echo $i; done

This is because globbing in for loops takes place before the variable
is expanded. Fix this by explicitly expanding the variables before globbing.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 common/hush.c |    6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/common/hush.c b/common/hush.c
index 1f468f6..5927ad6 100644
--- a/common/hush.c
+++ b/common/hush.c
@@ -1008,7 +1008,11 @@ static int xglob(o_string *dest, int flags, glob_t *pglob, int glob_needed)
 			return 0;
 		}
 	} else if (glob_needed) {
-		gr = do_glob(dest->data, flags, NULL, pglob);
+		char *data;
+		data = insert_var_value(dest->data);
+		gr = do_glob(data, flags, NULL, pglob);
+		if (data != dest->data)
+			free(data);
 		debug("glob returned %d\n",gr);
 	} else {
 		gr = fake_glob(dest->data, flags, NULL, pglob);
-- 
1.7.10.4


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

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

* [PATCH 2/2] hush: implement $*
  2013-02-21 14:17 [PATCH 1/2] hush: expand variables before globbing in for loops Sascha Hauer
@ 2013-02-21 14:17 ` Sascha Hauer
  2013-02-22  8:13 ` [PATCH 1/2] hush: expand variables before globbing in for loops Sascha Hauer
  1 sibling, 0 replies; 3+ messages in thread
From: Sascha Hauer @ 2013-02-21 14:17 UTC (permalink / raw)
  To: barebox

To get all arguments a script is called with.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 common/hush.c |   21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/common/hush.c b/common/hush.c
index 5927ad6..43ce3ba 100644
--- a/common/hush.c
+++ b/common/hush.c
@@ -335,6 +335,19 @@ static int b_addchr(o_string *o, int ch)
 	return 0;
 }
 
+static int b_addstr(o_string *o, const char *str)
+{
+	int ret;
+
+	while (*str) {
+		ret = b_addchr(o, *str++);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+
 static void b_reset(o_string *o)
 {
 	o->length = 0;
@@ -1410,6 +1423,14 @@ static int handle_dollar(o_string *dest, struct p_context *ctx, struct in_str *i
 			}
 			b_addchr(dest, SPECIAL_VAR_SYMBOL);
 			break;
+		case '*':
+			for (i = 1; i < ctx->global_argc; i++) {
+				b_addstr(dest, ctx->global_argv[i]);
+				b_addchr(dest, ' ');
+			}
+
+			advance = 1;
+			break;
 		default:
 			b_addchr(dest, '$');
 		}
-- 
1.7.10.4


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

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

* Re: [PATCH 1/2] hush: expand variables before globbing in for loops
  2013-02-21 14:17 [PATCH 1/2] hush: expand variables before globbing in for loops Sascha Hauer
  2013-02-21 14:17 ` [PATCH 2/2] hush: implement $* Sascha Hauer
@ 2013-02-22  8:13 ` Sascha Hauer
  1 sibling, 0 replies; 3+ messages in thread
From: Sascha Hauer @ 2013-02-22  8:13 UTC (permalink / raw)
  To: barebox

On Thu, Feb 21, 2013 at 03:17:13PM +0100, Sascha Hauer wrote:
> The following does work:
> 
> for i in *; do echo $i; done
> 
> but the following does not:
> 
> a="*"; for i in $a; do echo $i; done
> 
> This is because globbing in for loops takes place before the variable
> is expanded. Fix this by explicitly expanding the variables before globbing.
> 
> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>

I'll drop this one. While it makes the situation better in some cases,
it does not entirely fix this issue. I think hush is just too buggy to
fix this properly.

Sascha


-- 
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

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

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

end of thread, other threads:[~2013-02-22  8:14 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-02-21 14:17 [PATCH 1/2] hush: expand variables before globbing in for loops Sascha Hauer
2013-02-21 14:17 ` [PATCH 2/2] hush: implement $* Sascha Hauer
2013-02-22  8:13 ` [PATCH 1/2] hush: expand variables before globbing in for loops Sascha Hauer

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