mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/2] hush: Fix handling '\ '
@ 2022-10-27  8:47 Sascha Hauer
  2022-10-27  8:47 ` [PATCH 2/2] readline: Complete strings containing whitespaces correctly Sascha Hauer
  0 siblings, 1 reply; 2+ messages in thread
From: Sascha Hauer @ 2022-10-27  8:47 UTC (permalink / raw)
  To: Barebox List; +Cc: afa

Currently when doing:

echo foo\ bar

we will get argv[1] = "foo\" and argv[2] = "bar".

An unquoted escaped whitespace should be replaced by a whitespace.
With this the above will correctly result in argv[1] = "foo bar"

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

diff --git a/common/hush.c b/common/hush.c
index 6a089fabf1..5138a1a45a 100644
--- a/common/hush.c
+++ b/common/hush.c
@@ -617,6 +617,7 @@ static int builtin_exit(struct p_context *ctx, struct child_prog *child,
 static void remove_quotes_in_str(char *src)
 {
 	char *trg = src;
+	bool in_double_quotes = false;
 
 	while (*src) {
 		if (*src == '\'') {
@@ -629,6 +630,7 @@ static void remove_quotes_in_str(char *src)
 
 		/* drop quotes */
 		if (*src == '"') {
+			in_double_quotes = !in_double_quotes;
 			src++;
 			continue;
 		}
@@ -654,6 +656,13 @@ static void remove_quotes_in_str(char *src)
 			continue;
 		}
 
+		/* replace '\ ' with ' ' */
+		if (!in_double_quotes && *src == '\\' && *(src + 1) == ' ') {
+			*trg++ = ' ';
+			src += 2;
+			continue;
+		}
+
 		*trg++ = *src++;
 	}
 	*trg = 0;
-- 
2.30.2




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

* [PATCH 2/2] readline: Complete strings containing whitespaces correctly
  2022-10-27  8:47 [PATCH 1/2] hush: Fix handling '\ ' Sascha Hauer
@ 2022-10-27  8:47 ` Sascha Hauer
  0 siblings, 0 replies; 2+ messages in thread
From: Sascha Hauer @ 2022-10-27  8:47 UTC (permalink / raw)
  To: Barebox List; +Cc: afa

Completion strings containing whitespaces have to end up as a single
argv[] argument, thus the whitespaces have to be escaped. Fix this.

Ideally the whitespaces should only be escaped when we are outside of
double or single quotes, but our completion currently doesn't trigger
at all when invokes inside quotes, so we can ignore this case for now.

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

diff --git a/lib/readline.c b/lib/readline.c
index 37d5b0a343..92bec3d1d8 100644
--- a/lib/readline.c
+++ b/lib/readline.c
@@ -230,9 +230,14 @@ int readline(const char *prompt, char *buf, int len)
 			}
 
 			i = 0;
-			while (completestr[i])
+			while (completestr[i]) {
+				if (completestr[i] == ' ' && completestr[i + 1])
+					cread_add_char('\\', insert, &num,
+						&eol_num, buf, len);
+
 				cread_add_char(completestr[i++], insert, &num,
 						&eol_num, buf, len);
+			}
 #endif
 			break;
 
-- 
2.30.2




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

end of thread, other threads:[~2022-10-27  8:49 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-27  8:47 [PATCH 1/2] hush: Fix handling '\ ' Sascha Hauer
2022-10-27  8:47 ` [PATCH 2/2] readline: Complete strings containing whitespaces correctly Sascha Hauer

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