svn commit: trunk/busybox/shell: ash_test ash_test/ash-read

vda at busybox.net vda at busybox.net
Tue Mar 25 00:07:14 UTC 2008


Author: vda
Date: 2008-03-24 17:07:12 -0700 (Mon, 24 Mar 2008)
New Revision: 21480

Log:
ash: add read -u; fix read -t and read -n; add testsuite entries.



Added:
   trunk/busybox/shell/ash_test/ash-read/
   trunk/busybox/shell/ash_test/ash-read/read_n.right
   trunk/busybox/shell/ash_test/ash-read/read_n.tests
   trunk/busybox/shell/ash_test/ash-read/read_r.right
   trunk/busybox/shell/ash_test/ash-read/read_r.tests
   trunk/busybox/shell/ash_test/ash-read/read_t.right
   trunk/busybox/shell/ash_test/ash-read/read_t.tests

Modified:
   trunk/busybox/shell/ash.c


Changeset:
Modified: trunk/busybox/shell/ash.c
===================================================================
--- trunk/busybox/shell/ash.c	2008-03-24 21:19:51 UTC (rev 21479)
+++ trunk/busybox/shell/ash.c	2008-03-25 00:07:12 UTC (rev 21480)
@@ -11672,10 +11672,18 @@
 #endif
 
 /*
- * The read builtin.  The -e option causes backslashes to escape the
- * following character.
- *
+ * The read builtin. Options:
+ *      -r              Do not interpret '\' specially
+ *      -s              Turn off echo (tty only)
+ *      -n NCHARS       Read NCHARS max
+ *      -p PROMPT       Display PROMPT on stderr (if input is from tty)
+ *      -t SECONDS      Timeout after SECONDS (tty or pipe only)
+ *      -u FD           Read from given FD instead of fd 0
  * This uses unbuffered input, which may be avoidable in some cases.
+ * TODO: bash also has:
+ *      -a ARRAY        Read into array[0],[1],etc
+ *      -d DELIM        End on DELIM char, not newline
+ *      -e              Use line editing (tty only)
  */
 static int
 readcmd(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED)
@@ -11690,31 +11698,23 @@
 	int startword;
 	int status;
 	int i;
+	int fd = 0;
 #if ENABLE_ASH_READ_NCHARS
-	int n_flag = 0;
-	int nchars = 0;
+	int nchars = 0; /* if != 0, -n is in effect */
 	int silent = 0;
 	struct termios tty, old_tty;
 #endif
 #if ENABLE_ASH_READ_TIMEOUT
-	fd_set set;
-	struct timeval ts;
-
-	ts.tv_sec = ts.tv_usec = 0;
+	unsigned end_ms = 0;
+	unsigned timeout = 0;
 #endif
 
 	rflag = 0;
 	prompt = NULL;
-#if ENABLE_ASH_READ_NCHARS && ENABLE_ASH_READ_TIMEOUT
-	while ((i = nextopt("p:rt:n:s")) != '\0')
-#elif ENABLE_ASH_READ_NCHARS
-	while ((i = nextopt("p:rn:s")) != '\0')
-#elif ENABLE_ASH_READ_TIMEOUT
-	while ((i = nextopt("p:rt:")) != '\0')
-#else
-	while ((i = nextopt("p:r")) != '\0')
-#endif
-	{
+	while ((i = nextopt("p:u:r"
+		USE_ASH_READ_TIMEOUT("t:")
+		USE_ASH_READ_NCHARS("n:s")
+	)) != '\0') {
 		switch (i) {
 		case 'p':
 			prompt = optionarg;
@@ -11724,7 +11724,7 @@
 			nchars = bb_strtou(optionarg, NULL, 10);
 			if (nchars < 0 || errno)
 				ash_msg_and_raise_error("invalid count");
-			n_flag = nchars; /* just a flag "nchars is nonzero" */
+			/* nchars == 0: off (bash 3.2 does this too) */
 			break;
 		case 's':
 			silent = 1;
@@ -11732,6 +11732,11 @@
 #endif
 #if ENABLE_ASH_READ_TIMEOUT
 		case 't':
+			timeout = bb_strtou(optionarg, NULL, 10);
+			if (errno || timeout > UINT_MAX / 2048)
+				ash_msg_and_raise_error("invalid timeout");
+			timeout *= 1000;
+#if 0 /* even bash have no -t N.NNN support */
 			ts.tv_sec = bb_strtou(optionarg, &p, 10);
 			ts.tv_usec = 0;
 			/* EINVAL means number is ok, but not terminated by NUL */
@@ -11755,16 +11760,22 @@
 			if (!(ts.tv_sec | ts.tv_usec)) { /* both are 0? */
 				ash_msg_and_raise_error("invalid timeout");
 			}
+#endif /* if 0 */
 			break;
 #endif
 		case 'r':
 			rflag = 1;
 			break;
+		case 'u':
+			fd = bb_strtou(optionarg, NULL, 10);
+			if (fd < 0 || errno)
+				ash_msg_and_raise_error("invalid file descriptor");
+			break;
 		default:
 			break;
 		}
 	}
-	if (prompt && isatty(0)) {
+	if (prompt && isatty(fd)) {
 		out2str(prompt);
 	}
 	ap = argptr;
@@ -11774,46 +11785,48 @@
 	if (ifs == NULL)
 		ifs = defifs;
 #if ENABLE_ASH_READ_NCHARS
-	if (n_flag || silent) {
-		if (tcgetattr(0, &tty) != 0) {
-			/* Not a tty */
-			n_flag = 0;
-			silent = 0;
-		} else {
-			old_tty = tty;
-			if (n_flag) {
-				tty.c_lflag &= ~ICANON;
-				tty.c_cc[VMIN] = nchars < 256 ? nchars : 255;
-			}
-			if (silent) {
-				tty.c_lflag &= ~(ECHO | ECHOK | ECHONL);
-			}
-			tcsetattr(0, TCSANOW, &tty);
+	tcgetattr(fd, &tty);
+	old_tty = tty;
+	if (nchars || silent) {
+		if (nchars) {
+			tty.c_lflag &= ~ICANON;
+			tty.c_cc[VMIN] = nchars < 256 ? nchars : 255;
 		}
+		if (silent) {
+			tty.c_lflag &= ~(ECHO | ECHOK | ECHONL);
+		}
+		/* if tcgetattr failed, tcsetattr will fail too.
+		 * Ignoring, it's harmless. */
+		tcsetattr(fd, TCSANOW, &tty);
 	}
 #endif
-#if ENABLE_ASH_READ_TIMEOUT
-	if (ts.tv_sec || ts.tv_usec) {
-		FD_ZERO(&set);
-		FD_SET(0, &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);
-#endif
-			return 1;
-		}
-	}
-#endif
 	status = 0;
 	startword = 1;
 	backslash = 0;
+#if ENABLE_ASH_READ_TIMEOUT
+	if (timeout) /* NB: ensuring end_ms is nonzero */
+		end_ms = ((unsigned)(monotonic_us() / 1000) + timeout) | 1;
+#endif
 	STARTSTACKSTR(p);
 	do {
-		if (nonblock_safe_read(0, &c, 1) != 1) {
+#if ENABLE_ASH_READ_TIMEOUT
+		if (end_ms) {
+			struct pollfd pfd[1];
+			pfd[0].fd = fd;
+			pfd[0].events = POLLIN;
+			timeout = end_ms - (unsigned)(monotonic_us() / 1000);
+			if ((int)timeout <= 0 /* already late? */
+			 || safe_poll(pfd, 1, timeout) != 1 /* no? wait... */
+			) { /* timed out! */
+#if ENABLE_ASH_READ_NCHARS
+				tcsetattr(fd, TCSANOW, &old_tty);
+#endif
+				return 1;
+			}
+		}
+#endif
+		if (nonblock_safe_read(fd, &c, 1) != 1) {
 			status = 1;
 			break;
 		}
@@ -11848,14 +11861,13 @@
 	}
 /* end of do {} while: */
 #if ENABLE_ASH_READ_NCHARS
-	while (!n_flag || --nchars);
+	while (--nchars);
 #else
 	while (1);
 #endif
 
 #if ENABLE_ASH_READ_NCHARS
-	if (n_flag || silent)
-		tcsetattr(0, TCSANOW, &old_tty);
+	tcsetattr(fd, TCSANOW, &old_tty);
 #endif
 
 	STACKSTRNUL(p);

Added: trunk/busybox/shell/ash_test/ash-read/read_n.right
===================================================================
--- trunk/busybox/shell/ash_test/ash-read/read_n.right	                        (rev 0)
+++ trunk/busybox/shell/ash_test/ash-read/read_n.right	2008-03-25 00:07:12 UTC (rev 21480)
@@ -0,0 +1,3 @@
+test
+tes
+tes

Added: trunk/busybox/shell/ash_test/ash-read/read_n.tests
===================================================================
--- trunk/busybox/shell/ash_test/ash-read/read_n.tests	                        (rev 0)
+++ trunk/busybox/shell/ash_test/ash-read/read_n.tests	2008-03-25 00:07:12 UTC (rev 21480)
@@ -0,0 +1,3 @@
+echo 'test' | (read reply; echo "$reply")
+echo 'test' | (read -n 3 reply; echo "$reply")
+echo 'test' | (read -n3 reply; echo "$reply")


Property changes on: trunk/busybox/shell/ash_test/ash-read/read_n.tests
___________________________________________________________________
Name: svn:executable
   + *

Added: trunk/busybox/shell/ash_test/ash-read/read_r.right
===================================================================
--- trunk/busybox/shell/ash_test/ash-read/read_r.right	                        (rev 0)
+++ trunk/busybox/shell/ash_test/ash-read/read_r.right	2008-03-25 00:07:12 UTC (rev 21480)
@@ -0,0 +1,2 @@
+testbest
+test\

Added: trunk/busybox/shell/ash_test/ash-read/read_r.tests
===================================================================
--- trunk/busybox/shell/ash_test/ash-read/read_r.tests	                        (rev 0)
+++ trunk/busybox/shell/ash_test/ash-read/read_r.tests	2008-03-25 00:07:12 UTC (rev 21480)
@@ -0,0 +1,2 @@
+echo -e 'test\\\nbest' | (read reply; echo "$reply")
+echo -e 'test\\\nbest' | (read -r reply; echo "$reply")


Property changes on: trunk/busybox/shell/ash_test/ash-read/read_r.tests
___________________________________________________________________
Name: svn:executable
   + *

Added: trunk/busybox/shell/ash_test/ash-read/read_t.right
===================================================================
--- trunk/busybox/shell/ash_test/ash-read/read_t.right	                        (rev 0)
+++ trunk/busybox/shell/ash_test/ash-read/read_t.right	2008-03-25 00:07:12 UTC (rev 21480)
@@ -0,0 +1,4 @@
+><
+><
+>test<
+>test<

Added: trunk/busybox/shell/ash_test/ash-read/read_t.tests
===================================================================
--- trunk/busybox/shell/ash_test/ash-read/read_t.tests	                        (rev 0)
+++ trunk/busybox/shell/ash_test/ash-read/read_t.tests	2008-03-25 00:07:12 UTC (rev 21480)
@@ -0,0 +1,10 @@
+# bash 3.2 outputs:
+
+# ><
+{ echo -n 'te'; sleep 2; echo 'st'; }   | (read -t 1 reply; echo ">$reply<")
+# ><
+{               sleep 2; echo 'test'; } | (read -t 1 reply; echo ">$reply<")
+# >test<
+{ echo -n 'te'; sleep 1; echo 'st'; }   | (read -t 2 reply; echo ">$reply<")
+# >test<
+{               sleep 1; echo 'test'; } | (read -t 2 reply; echo ">$reply<")


Property changes on: trunk/busybox/shell/ash_test/ash-read/read_t.tests
___________________________________________________________________
Name: svn:executable
   + *




More information about the busybox-cvs mailing list