[PATCH] read enhancements for ash

James Simmons jsimmons at infradead.org
Mon Mar 24 08:40:53 PDT 2008


Some info about what the command does. Allow the use of any file 
descriptor for the tty.

diff -urwN busybox-1.10.0.orig/shell/ash.c busybox-1.10.0/shell/ash.c
--- busybox-1.10.0.orig/shell/ash.c	2008-03-21 21:31:45.000000000 -0400
+++ busybox-1.10.0/shell/ash.c	2008-03-24 11:48:49.000000000 -0400
@@ -11672,8 +11672,14 @@
 #endif
 
 /*
- * The read builtin.  The -e option causes backslashes to escape the
- * following character.
+ * The read builtin. Options:
+ *		-r			Cause backslashes to escape the following character.
+ *		-s			Turn off echo on the terminal
+ *		-n NCHARS		Read until we get a specified number of characters, or
+ *					a newline, whichever comes first.
+ *		-p PROMPT		Display a prompt.
+ *		-t SECONDS		Allow user only a specified number of seconds to enter text.
+ *		-u FD			Read from an alternate file descriptor.
  *
  * This uses unbuffered input, which may be avoidable in some cases.
  */
@@ -11690,6 +11696,7 @@
 	int startword;
 	int status;
 	int i;
+	int fd = 0;
 #if ENABLE_ASH_READ_NCHARS
 	int n_flag = 0;
 	int nchars = 0;
@@ -11706,13 +11713,13 @@
 	rflag = 0;
 	prompt = NULL;
 #if ENABLE_ASH_READ_NCHARS && ENABLE_ASH_READ_TIMEOUT
-	while ((i = nextopt("p:rt:n:s")) != '\0')
+	while ((i = nextopt("p:u:rt:n:s")) != '\0')
 #elif ENABLE_ASH_READ_NCHARS
-	while ((i = nextopt("p:rn:s")) != '\0')
+	while ((i = nextopt("p:u:rn:s")) != '\0')
 #elif ENABLE_ASH_READ_TIMEOUT
-	while ((i = nextopt("p:rt:")) != '\0')
+	while ((i = nextopt("p:u:rt:")) != '\0')
 #else
-	while ((i = nextopt("p:r")) != '\0')
+	while ((i = nextopt("p:u:r")) != '\0')
 #endif
 	{
 		switch (i) {
@@ -11760,11 +11767,16 @@
 		case 'r':
 			rflag = 1;
 			break;
+		case 'u':
+			fd = strtol(optionarg, &p, 10);
+			if (*p)
+				ash_msg_and_raise_error("invalid file descriptor");
+			break;
 		default:
 			break;
 		}
 	}
-	if (prompt && isatty(0)) {
+	if (prompt && isatty(fd)) {
 		out2str(prompt);
 	}
 	ap = argptr;
@@ -11775,7 +11787,7 @@
 		ifs = defifs;
 #if ENABLE_ASH_READ_NCHARS
 	if (n_flag || silent) {
-		if (tcgetattr(0, &tty) != 0) {
+		if (tcgetattr(fd, &tty) != 0) {
 			/* Not a tty */
 			n_flag = 0;
 			silent = 0;
@@ -11788,21 +11800,21 @@
 			if (silent) {
 				tty.c_lflag &= ~(ECHO | ECHOK | ECHONL);
 			}
-			tcsetattr(0, TCSANOW, &tty);
+			tcsetattr(fd, TCSANOW, &tty);
 		}
 	}
 #endif
 #if ENABLE_ASH_READ_TIMEOUT
 	if (ts.tv_sec || ts.tv_usec) {
 		FD_ZERO(&set);
-		FD_SET(0, &set);
+		FD_SET(fd, &set);
 
 		/* poll-based wait produces bigger code, using select */
 		i = select(1, &set, NULL, NULL, &ts);
 		if (!i) { /* timed out! */
 #if ENABLE_ASH_READ_NCHARS
 			if (n_flag)
-				tcsetattr(0, TCSANOW, &old_tty);
+				tcsetattr(fd, TCSANOW, &old_tty);
 #endif
 			return 1;
 		}
@@ -11813,7 +11825,7 @@
 	backslash = 0;
 	STARTSTACKSTR(p);
 	do {
-		if (nonblock_safe_read(0, &c, 1) != 1) {
+		if (nonblock_safe_read(fd, &c, 1) != 1) {
 			status = 1;
 			break;
 		}
@@ -11855,7 +11867,7 @@
 
 #if ENABLE_ASH_READ_NCHARS
 	if (n_flag || silent)
-		tcsetattr(0, TCSANOW, &old_tty);
+		tcsetattr(fd, TCSANOW, &old_tty);
 #endif
 
 	STACKSTRNUL(p);


More information about the busybox mailing list