about summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Stephenson <pws@users.sourceforge.net>2003-02-05 11:56:55 +0000
committerPeter Stephenson <pws@users.sourceforge.net>2003-02-05 11:56:55 +0000
commit590a26709a67abf13eeec66f161b0ec085637459 (patch)
treeea78dc37f3be5fc553682c81a876f27c25cd6d6e
parentf87232c120696b6e33b241e814d4e9b48efc13d7 (diff)
downloadzsh-590a26709a67abf13eeec66f161b0ec085637459.tar.gz
zsh-590a26709a67abf13eeec66f161b0ec085637459.tar.xz
zsh-590a26709a67abf13eeec66f161b0ec085637459.zip
18195: Timeouts for read builtin.
-rw-r--r--ChangeLog6
-rw-r--r--Doc/Zsh/builtins.yo31
-rw-r--r--Src/Modules/zpty.c2
-rw-r--r--Src/builtin.c34
-rw-r--r--Src/utils.c8
5 files changed, 56 insertions, 25 deletions
diff --git a/ChangeLog b/ChangeLog
index f4666473d..970eba1c4 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2003-02-05  Peter Stephenson  <pws@csr.com>
+
+	* 18195: Doc/Zsh/builtins.yo Src/builtin.c Src/utils.c
+	Src/Modules/zpty.c: Timeouts with `read' via optional numeric
+	argument to -t option.
+
 2003-02-04  Peter Stephenson  <pws@csr.com>
 
 	* unposted: 4.1.0-dev-7.
diff --git a/Doc/Zsh/builtins.yo b/Doc/Zsh/builtins.yo
index 6bd0084df..7512e9acf 100644
--- a/Doc/Zsh/builtins.yo
+++ b/Doc/Zsh/builtins.yo
@@ -824,8 +824,8 @@ contain symbolic links.
 alias(r)(fc -e -)
 findex(read)
 vindex(IFS, use of)
-ifzman(xitem(tt(read) [ tt(-rszpqAclneEt) ] [ tt(-k) [ var(num) ] ]))
-item(ifnzman(tt(read) [ tt(-rszpqAclneEt) ] [ tt(-k) [ var(num) ] ]) [ tt(-u)var(n) ] [ var(name)[tt(?)var(prompt)] ] [ var(name) ...  ])(
+ifzman(xitem(tt(read) [ tt(-rszpqAclneE) ] [ tt(-t) [ var(num) ] ] [ tt(-k) [ var(num) ] ]))
+item(ifnzman(tt(read) [ tt(-rszpqAclneE) ] [ tt(-t) [ var(num) ] ] [ tt(-k) [ var(num) ] ]) [ tt(-u)var(n) ] [ var(name)[tt(?)var(prompt)] ] [ var(name) ...  ])(
 vindex(REPLY, use of)
 vindex(reply, use of)
 Read one line and break it into fields using the characters
@@ -899,21 +899,28 @@ Input is read from file descriptor var(n).
 item(tt(-p))(
 Input is read from the coprocess.
 )
-item(tt(-t))(
-Test if input is available before attempting to read; if none is, return
-status 1 and do not set any variables.  This is not available when reading
-from the editor buffer with tt(-z), when called from within completion
-with tt(-c) or tt(-l), with tt(-q) which clears the input queue before
-reading, or within zle where other mechanisms should be used to test for
-input.
+item(tt(-t) [ var(num) ])(
+Test if input is available before attempting to read.  If var(num)
+is present, it must begin with a digit and will be evaluated
+to give a number of seconds, which may be a floating point number;
+in this case the read times out if input is not available within this
+time.  If var(num) is not present, it is taken to be zero, so that
+tt(read) returns immediately if no input is available.
+If no input is available, return status 1 and do not set any variables.
+ifzman( )
+This option is not available when reading from the editor buffer with
+tt(-z), when called from within completion with tt(-c) or tt(-l), with
+tt(-q) which clears the input queue before reading, or within zle where
+other mechanisms should be used to test for input.
 ifzman( )
 Note that read does not attempt to alter the input processing mode.  The
 default mode is canonical input, in which an entire line is read at a time,
 so usually `tt(read -t)' will not read anything until an entire line has
 been typed.  However, when reading from the terminal with tt(-k)
-this is automatically handled; note that only availability of the first
-character is tested, so that e.g. `tt(read -t -k 2)' can still block on the
-second character.
+input is processed one key at a time; in this case, only availability of
+the first character is tested, so that e.g. `tt(read -t -k 2)' can still
+block on the second character.  Use two instances of `tt(read -t -k)' if
+this is not what is wanted.
 )
 enditem()
 If the first argument contains a `tt(?)', the remainder of this
diff --git a/Src/Modules/zpty.c b/Src/Modules/zpty.c
index 1be615610..7d33bd064 100644
--- a/Src/Modules/zpty.c
+++ b/Src/Modules/zpty.c
@@ -632,7 +632,7 @@ bin_zpty(char *nam, char **args, Options ops, int func)
 	if (p->fin)
 	    return 2;
 	if (OPT_ISSET(ops,'t') && p->read == -1 &&
-	    !read_poll(p->fd, &p->read, 0))
+	    !read_poll(p->fd, &p->read, 0, 0))
 	    return 1;
 
 	return (OPT_ISSET(ops,'r') ?
diff --git a/Src/builtin.c b/Src/builtin.c
index 530bba8c8..a8871b9a6 100644
--- a/Src/builtin.c
+++ b/Src/builtin.c
@@ -106,7 +106,7 @@ static struct builtin builtins[] =
     BUILTIN("pushln", BINF_PRINTOPTS, bin_print, 0, -1, BIN_PRINT, NULL, "-nz"),
     BUILTIN("pwd", 0, bin_pwd, 0, 0, 0, "rLP", NULL),
     BUILTIN("r", 0, bin_fc, 0, -1, BIN_R, "nrl", NULL),
-    BUILTIN("read", 0, bin_read, 0, -1, 0, "cek:%lnpqrstzu:AE", NULL),
+    BUILTIN("read", 0, bin_read, 0, -1, 0, "cek:%lnpqrst:%zu:AE", NULL),
     BUILTIN("readonly", BINF_PLUSOPTS | BINF_MAGICEQUALS | BINF_PSPECIAL, bin_typeset, 0, -1, 0, "AE:%F:%HL:%R:%TUZ:%afghi:%lptux", "r"),
     BUILTIN("rehash", 0, bin_hash, 0, 0, 0, "df", "r"),
     BUILTIN("return", BINF_PSPECIAL, bin_break, 0, 1, BIN_RETURN, NULL, NULL),
@@ -4189,16 +4189,30 @@ bin_read(char *name, char **args, Options ops, int func)
     } else
 	readfd = izle = 0;
 
-    if (OPT_ISSET(ops,'t') && 
-	!read_poll(readfd, &readchar, keys && !zleactive)) {
-	if (OPT_ISSET(ops,'k') && !zleactive && !isem)
-	    settyinfo(&shttyinfo);
-	if (haso) {
-	    fclose(shout);
-	    shout = oshout;
-	    SHTTY = -1;
+    if (OPT_ISSET(ops,'t')) {
+	zlong timeout = 0;
+	if (OPT_HASARG(ops,'t')) {
+	    mnumber mn = zero_mnumber;
+	    mn = matheval(OPT_ARG(ops,'t'));
+	    if (errflag)
+		return 1;
+	    if (mn.type == MN_FLOAT) {
+		mn.u.d *= 1e6;
+		timeout = (zlong)mn.u.d;
+	    } else {
+		timeout = (zlong)mn.u.l * (zlong)1000000;
+	    }
+	}
+	if (!read_poll(readfd, &readchar, keys && !zleactive, timeout)) {
+	    if (OPT_ISSET(ops,'k') && !zleactive && !isem)
+		settyinfo(&shttyinfo);
+	    if (haso) {
+		fclose(shout);
+		shout = oshout;
+		SHTTY = -1;
+	    }
+	    return 1;
 	}
-	return 1;
     }
     if (OPT_ISSET(ops,'s') && SHTTY != -1) {
 	struct ttyinfo ti;
diff --git a/Src/utils.c b/Src/utils.c
index 8534a64cc..64a6a722e 100644
--- a/Src/utils.c
+++ b/Src/utils.c
@@ -1332,7 +1332,7 @@ setblock_stdin(void)
 
 /**/
 mod_export int
-read_poll(int fd, int *readchar, int polltty)
+read_poll(int fd, int *readchar, int polltty, zlong microseconds)
 {
     int ret = -1;
     long mode = -1;
@@ -1374,6 +1374,8 @@ read_poll(int fd, int *readchar, int polltty)
 	gettyinfo(&ti);
 	if ((polltty = ti.tio.c_cc[VMIN])) {
 	    ti.tio.c_cc[VMIN] = 0;
+	    /* termios timeout is 10ths of a second */
+	    ti.tio.c_cc[VTIME] = (int) (microseconds / (zlong)100000);
 	    settyinfo(&ti);
 	}
     }
@@ -1381,7 +1383,8 @@ read_poll(int fd, int *readchar, int polltty)
     polltty = 0;
 #endif
 #ifdef HAVE_SELECT
-    expire_tv.tv_sec = expire_tv.tv_usec = 0;
+    expire_tv.tv_sec = (int) (microseconds / (zlong)1000000);
+    expire_tv.tv_usec = microseconds % (zlong)1000000;
     FD_ZERO(&foofd);
     FD_SET(fd, &foofd);
     ret = select(fd+1, (SELECT_ARG_2_T) &foofd, NULL, NULL, &expire_tv);
@@ -1407,6 +1410,7 @@ read_poll(int fd, int *readchar, int polltty)
 #ifdef HAS_TIO
     if (polltty) {
 	ti.tio.c_cc[VMIN] = 1;
+	ti.tio.c_cc[VTIME] = 0;
 	settyinfo(&ti);
     }
 #endif