'read -t' support, forward-ported from Gentoo Busybox 1.1.3.
Used during the LiveCD boot when prompting for a keymap.

Signed-off-by: Robin H. Johnson <robbat2@gentoo.org>

diff -Nuar --exclude '*.orig' busybox-1.7.4/shell/ash.c busybox-1.7.4+gentoo/shell/ash.c
--- busybox-1.7.4/shell/ash.c	2007-11-03 16:06:35.000000000 -0700
+++ busybox-1.7.4+gentoo/shell/ash.c	2008-03-11 10:21:28.000000000 -0700
@@ -11485,11 +11485,13 @@
 	int startword;
 	int status;
 	int i;
+#if ENABLE_ASH_READ_NCHARS || ENABLE_ASH_READ_TIMEOUT
+	struct termios tty, old_tty;
+#endif
 #if ENABLE_ASH_READ_NCHARS
 	int nch_flag = 0;
 	int nchars = 0;
 	int silent = 0;
-	struct termios tty, old_tty;
 #endif
 #if ENABLE_ASH_READ_TIMEOUT
 	fd_set set;
@@ -11566,44 +11568,74 @@
 	ifs = bltinlookup("IFS");
 	if (ifs == NULL)
 		ifs = defifs;
-#if ENABLE_ASH_READ_NCHARS
+#if ENABLE_ASH_READ_NCHARS || ENABLE_ASH_READ_TIMEOUT
+#if ENABLE_ASH_READ_NCHARS && ENABLE_ASH_READ_TIMEOUT
+	if (nch_flag || silent || ts.tv_sec || ts.tv_usec) {
+#elif ENABLE_ASH_READ_TIMEOUT
+	if (ts.tv_sec || ts.tv_usec) {
+#elif ENABLE_ASH_READ_NCHARS
 	if (nch_flag || silent) {
+#endif
 		tcgetattr(0, &tty);
 		old_tty = tty;
-		if (nch_flag) {
+#if ENABLE_ASH_READ_NCHARS && ENABLE_ASH_READ_TIMEOUT
+		if (nch_flag || ts.tv_sec || ts.tv_usec)
+#elif ENABLE_ASH_READ_TIMEOUT
+		if (ts.tv_sec || ts.tv_usec)
+#elif ENABLE_ASH_READ_NCHARS
+		if (nch_flag)
+#endif
 			tty.c_lflag &= ~ICANON;
-			tty.c_cc[VMIN] = nchars;
-		}
-		if (silent) {
-			tty.c_lflag &= ~(ECHO|ECHOK|ECHONL);
 
-		}
+
+#if ENABLE_ASH_READ_NCHARS
+		if (silent)
+		    tty.c_lflag &= ~(ECHO|ECHOK|ECHONL);
+#endif
 		tcsetattr(0, TCSANOW, &tty);
 	}
 #endif
+	i = 1;
+	STARTSTACKSTR(p);
 #if ENABLE_ASH_READ_TIMEOUT
 	if (ts.tv_sec || ts.tv_usec) {
 		FD_ZERO(&set);
 		FD_SET(0, &set);
 
 		i = select(FD_SETSIZE, &set, NULL, NULL, &ts);
-		if (!i) {
+		if (i == 1)
+		{
+			read(0, &c, 1);
+			if(c == '\n' || c == 4) /* Handle newlines and EOF */
+				i = 0; /* Don't read further... */
+			else
+				STPUTC(c, p); /* Ok, keep reading... */
+		}
 #if ENABLE_ASH_READ_NCHARS
-			if (nch_flag)
-				tcsetattr(0, TCSANOW, &old_tty);
+		if (!silent && !nch_flag)
 #endif
-			return 1;
+			tcsetattr(0, TCSANOW, &old_tty);
+
+#if ENABLE_ASH_READ_NCHARS
+		if(i == 0)
+		{
+			nchars = 0;
+			nch_flag = 1;
+		} else
+		{
+			if (nch_flag)
+				nchars--;
 		}
+#endif
 	}
 #endif
 	status = 0;
 	startword = 1;
 	backslash = 0;
-	STARTSTACKSTR(p);
 #if ENABLE_ASH_READ_NCHARS
 	while (!nch_flag || nchars--)
 #else
-	for (;;)
+	for (;i > 0;)
 #endif
 	{
 		if (read(0, &c, 1) != 1) {
@@ -11640,8 +11672,10 @@
 		}
 	}
 #if ENABLE_ASH_READ_NCHARS
-	if (nch_flag || silent)
+	if (silent || nch_flag)
 		tcsetattr(0, TCSANOW, &old_tty);
+	if (!silent && nch_flag)
+		printf("\n");
 #endif
 
 	STACKSTRNUL(p);