return 128+SIGALRM if read with timeout timeouts, inspired by GNU bash

This commit is contained in:
tg 2016-11-11 22:17:09 +00:00
parent 945ef46af1
commit 3aac3c30b5
2 changed files with 17 additions and 13 deletions

19
funcs.c
View File

@ -38,7 +38,7 @@
#endif
#endif
__RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.317 2016/11/11 21:37:35 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.318 2016/11/11 22:17:08 tg Exp $");
#if HAVE_KILLPG
/*
@ -2085,7 +2085,7 @@ c_read(const char **wp)
timersub(&tvlim, &tv, &tv);
if (tv.tv_sec < 0) {
/* timeout expired globally */
rv = 1;
rv = 3;
goto c_read_out;
}
@ -2095,8 +2095,8 @@ c_read(const char **wp)
case 0:
/* timeout expired for this call */
bytesread = 0;
/* fake EOF read; all cases return 1 */
goto c_read_didread;
rv = 3;
goto c_read_readdone;
default:
bi_errorf(Tf_sD_s, Tselect, cstrerror(errno));
rv = 2;
@ -2121,7 +2121,6 @@ c_read(const char **wp)
goto c_read_out;
}
c_read_didread:
switch (readmode) {
case READALL:
if (bytesread == 0) {
@ -2195,13 +2194,13 @@ c_read(const char **wp)
/*-
* state: we finished reading the input and NUL terminated it
* Xstring(xs, xp) -> xp-1 = input string without trailing delim
* rv = 1 if EOF, 0 otherwise (errors handled already)
* rv = 3 if timeout, 1 if EOF, 0 otherwise (errors handled already)
*/
if (rv == 1) {
/* clean up coprocess if needed, on EOF */
if (rv) {
/* clean up coprocess if needed, on EOF/error/timeout */
coproc_read_close(fd);
if (readmode == READALL)
if (readmode == READALL && (rv == 1 || (rv == 3 && bytesread)))
/* EOF is no error here */
rv = 0;
}
@ -2367,7 +2366,7 @@ c_read(const char **wp)
Xfree(xs, xp);
if (restore_tios)
mksh_tcset(fd, &tios);
return (rv);
return (rv == 3 ? ksh_sigmask(SIGALRM) : rv);
#undef is_ifsws
}

11
mksh.1
View File

@ -1,4 +1,4 @@
.\" $MirOS: src/bin/mksh/mksh.1,v 1.418 2016/11/11 21:37:44 tg Exp $
.\" $MirOS: src/bin/mksh/mksh.1,v 1.419 2016/11/11 22:17:09 tg Exp $
.\" $OpenBSD: ksh.1,v 1.160 2015/07/04 13:27:04 feinerer Exp $
.\"-
.\" Copyright © 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
@ -3912,7 +3912,10 @@ if empty, instead of the ASCII newline character as input line delimiter.
Instead of reading till end-of-line, read exactly
.Ar z
bytes.
If EOF or a timeout occurs, a partial read is returned with exit status 1.
Upon EOF, a partial read is returned with exit status 1.
After timeout, a partial read is returned with an exit status as if
.Dv SIGALRM
were caught.
.It Fl n Ar z
Instead of reading till end-of-line, read up to
.Ar z
@ -3933,7 +3936,9 @@ Interrupt reading after
seconds (specified as positive decimal value with an optional fractional part).
The exit status of
.Nm read
is 1 if the timeout occurred, but partial reads may still be returned.
is the same as if
.Dv SIGALRM
were caught if the timeout occurred, but partial reads may still be returned.
.It Fl r
Normally, the ASCII backslash character escapes the special
meaning of the following character and is stripped from the input;