-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
214 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
#ifdef HAVE_CONFIG_H | ||
#include "config.h" | ||
#endif | ||
|
||
#include <sys/types.h> | ||
|
||
#include "compat.h" | ||
|
||
#ifdef HAVE_BROKEN_POLL | ||
|
||
#include <assert.h> | ||
#include <errno.h> | ||
#include <fcntl.h> | ||
#include <poll.h> | ||
#include <unistd.h> | ||
|
||
extern int tty_getc_peek; | ||
|
||
static int ttygetchar(int); | ||
|
||
int | ||
fdwait(int ttyfd, int infd, int timeout) | ||
{ | ||
static int state; | ||
struct pollfd pfd; | ||
int flags, nready; | ||
int res = 0; | ||
|
||
/* XXX filter_choices() poll disabled */ | ||
if (infd == -1 && timeout == 0) | ||
return 0; | ||
|
||
if (state == 0) { | ||
flags = fcntl(ttyfd, F_GETFL, 0); | ||
if (flags == -1) | ||
return -1; | ||
flags |= O_NONBLOCK; | ||
if (fcntl(ttyfd, F_SETFL, flags) == -1) | ||
return -1; | ||
state++; | ||
} | ||
|
||
if (state == 1) { | ||
if (timeout < 0) | ||
timeout = 100; | ||
pfd.fd = infd; | ||
pfd.events = POLLIN; | ||
while (res == 0) { | ||
if (infd == -1) { | ||
state++; | ||
break; | ||
} | ||
|
||
nready = poll(&pfd, 1, timeout); | ||
if (nready == -1) | ||
return -1; | ||
if (nready > 0) { | ||
if (pfd.revents & (POLLERR | POLLNVAL)) { | ||
errno = EBADF; | ||
return -1; | ||
} else if (pfd.revents & (POLLIN | POLLHUP)) { | ||
res |= FD_STDIN_READY; | ||
} | ||
} | ||
|
||
nready = ttygetchar(ttyfd); | ||
if (nready == -1) { | ||
if (errno == EAGAIN) | ||
continue; | ||
return -1; | ||
} else if (nready > 0) { | ||
res |= FD_TTY_READY; | ||
} | ||
} | ||
} | ||
|
||
if (state == 2) { | ||
flags = fcntl(ttyfd, F_GETFL, 0); | ||
if (flags == -1) | ||
return -1; | ||
flags &= ~O_NONBLOCK; | ||
if (fcntl(ttyfd, F_SETFL, flags) == -1) | ||
return -1; | ||
state++; | ||
} | ||
|
||
if (state == 3) { | ||
nready = ttygetchar(ttyfd); | ||
if (nready == -1) | ||
return -1; | ||
else if (nready > 0) | ||
res |= FD_TTY_READY; | ||
} | ||
|
||
return res; | ||
} | ||
|
||
static int | ||
ttygetchar(int fd) | ||
{ | ||
ssize_t n; | ||
unsigned char c; | ||
|
||
n = read(fd, &c, sizeof(c)); | ||
if (n > 0) { | ||
assert(tty_getc_peek == -1); | ||
tty_getc_peek = c; | ||
} | ||
return n; | ||
} | ||
|
||
#else | ||
|
||
#include <errno.h> | ||
#include <poll.h> | ||
|
||
int | ||
fdwait(int ttyfd, int infd, int timeout) | ||
{ | ||
struct pollfd fds[2]; | ||
int i, nready; | ||
int nfds = 0; | ||
int res = 0; | ||
|
||
if (ttyfd != -1) { | ||
fds[nfds].fd = ttyfd; | ||
fds[nfds].events = POLLIN; | ||
nfds++; | ||
} | ||
if (infd != -1) { | ||
fds[nfds].fd = infd; | ||
fds[nfds].events = POLLIN; | ||
nfds++; | ||
} | ||
nready = poll(fds, nfds, timeout); | ||
if (nready <= 0) | ||
return nready; | ||
for (i = 0; i < nfds; i++) { | ||
if (fds[i].revents & (POLLERR | POLLNVAL)) { | ||
errno = EBADF; | ||
return -1; | ||
} else if ((fds[i].revents & (POLLIN | POLLHUP)) == 0) { | ||
continue; | ||
} | ||
|
||
if (fds[i].fd == ttyfd) | ||
res |= FD_TTY_READY; | ||
else if (fds[i].fd == infd) | ||
res |= FD_STDIN_READY; | ||
} | ||
return res; | ||
} | ||
|
||
#endif /* HAVE_BROKEN_POLL */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters