Hi,

Attatched is a patch that fixes the annoying bug in ash prmpt expansion.

Currently the default PS1='\w \$ ' will always show a '$' as prompt
while PS1='\w \\$ ' will show a '#' if effective user is root and '$'
otherwise.

The problem is that the prompt string is expanded as a normal double
quote expansion (readtoken1()) before its sent to the
libbb/lineedit.c:parse_prompt() function. The function readtoken1() will
correctly convert '\$' to '$' before its send to parse_prompt() which is
looking for '\$'.

The attatched patch adds a PSSYNTAX expansion style which is identical
as the DQSYNTAX, except that it keep '\$' as '\$'.

The patch is my contribution to the mess. Please let me know if there
are better ways to handle this.

-nc
Index: shell/ash.c
===================================================================
--- shell/ash.c	(revision 19828)
+++ shell/ash.c	(working copy)
@@ -2494,6 +2494,7 @@
 #define DQSYNTAX   1    /* in double quotes */
 #define SQSYNTAX   2    /* in single quotes */
 #define ARISYNTAX  3    /* in arithmetic */
+#define PSSYNTAX   4    /* prompt */
 
 #if ENABLE_ASH_OPTIMIZE_FOR_SIZE
 #define USE_SIT_FUNCTION
@@ -9891,6 +9892,7 @@
 	int dqvarnest = 0;  /* levels of variables expansion within double quotes */
 	int oldstyle = 0;
 	int prevsyntax = 0; /* syntax before arithmetic */
+	int pssyntax = 0;   /* we are expanding a prompt string */
 #if __GNUC__
 	/* Avoid longjmp clobbering */
 	(void) &out;
@@ -9907,6 +9909,10 @@
 
 	startlinno = plinno;
 	dblquote = 0;
+	if (syntax == PSSYNTAX) {
+		pssyntax = 1;
+		syntax = DQSYNTAX;
+	}
 	if (syntax == DQSYNTAX)
 		dblquote = 1;
 	quotef = 0;
@@ -9949,11 +9955,15 @@
 					if (doprompt)
 						setprompt(2);
 				} else {
+					if (c == '$' && pssyntax) {
+						USTPUTC(CTLESC, out);
+						USTPUTC('\\', out);
+					}
 					if (dblquote &&
 						c != '\\' && c != '`' &&
 						c != '$' && (
 							c != '"' ||
-							eofmark != NULL)
+							eofmark != NULL) 
 					) {
 						USTPUTC(CTLESC, out);
 						USTPUTC('\\', out);
@@ -10784,7 +10794,7 @@
 
 	/* XXX Fix (char *) cast. */
 	setinputstring((char *)ps);
-	readtoken1(pgetc(), DQSYNTAX, nullstr, 0);
+	readtoken1(pgetc(), PSSYNTAX, nullstr, 0);
 	popfile();
 
 	n.narg.type = NARG;
_______________________________________________
busybox mailing list
busybox@busybox.net
http://busybox.net/cgi-bin/mailman/listinfo/busybox

Reply via email to