Make m5term use select() so OS X is happy.

This commit is contained in:
Ali Saidi 2008-10-01 16:27:52 -04:00
parent 7bf6a219db
commit 4ba87133bd

View file

@ -139,54 +139,64 @@ remote_connect(char *host, char *port, struct addrinfo hints)
/* /*
* readwrite() * readwrite()
* Loop that polls on the network file descriptor and stdin. * Loop that selects on the network file descriptor and stdin.
* Changed from poll() by Ali Saidi to make work on Mac OS X >= 10.4
*/ */
void void
readwrite(int nfd) readwrite(int nfd)
{ {
struct pollfd pfd[2]; fd_set read_fds;
char buf[BUFSIZ]; char buf[BUFSIZ];
int wfd = fileno(stdin), n, ret; int wfd = fileno(stdin), n, ret, max_fd;
int lfd = fileno(stdout); int lfd = fileno(stdout);
int escape = 0; int escape = 0;
struct timeval timeout;
/* Setup Network FD */ if (nfd == -1)
pfd[0].fd = nfd; return;
pfd[0].events = POLLIN;
/* Setup STDIN FD */ FD_ZERO(&read_fds);
pfd[1].fd = wfd;
pfd[1].events = POLLIN;
while (pfd[0].fd != -1) { FD_SET(wfd, &read_fds);
if ((n = poll(pfd, 2, -1)) < 0) { FD_SET(nfd, &read_fds);
max_fd = nfd + 1;
timeout.tv_sec = 1;
timeout.tv_usec = 0;
while (1) {
n = select(max_fd, &read_fds, NULL, NULL, &timeout);
if (n < 0) {
close(nfd); close(nfd);
err(1, "Polling Error"); perror("Select Error:");
} }
if (n == 0) if (n == 0) {
if (read(nfd, buf, 0) < 0)
return;
goto setup_select;
}
if (read(nfd, buf, 0) < 0)
return; return;
if (pfd[0].revents & POLLIN) { if (FD_ISSET(nfd, &read_fds)) {
if ((n = read(nfd, buf, sizeof(buf))) < 0) if ((n = read(nfd, buf, sizeof(buf))) < 0)
return; return;
else if (n == 0) { else if (n == 0) {
shutdown(nfd, SHUT_RD); shutdown(nfd, SHUT_RD);
pfd[0].fd = -1; return;
pfd[0].events = 0;
} else { } else {
if ((ret = atomicio(write, lfd, buf, n)) != n) if ((ret = atomicio(write, lfd, buf, n)) != n)
return; return;
} }
} }
if (pfd[1].revents & POLLIN) { if (FD_ISSET(wfd, &read_fds)) {
if ((n = read(wfd, buf, sizeof(buf))) < 0) if ((n = read(wfd, buf, sizeof(buf))) < 0)
return; return;
else if (n == 0) { else if (n == 0) {
shutdown(nfd, SHUT_WR); shutdown(nfd, SHUT_WR);
pfd[1].fd = -1;
pfd[1].events = 0;
} else { } else {
if (escape) { if (escape) {
char buf2[] = "~"; char buf2[] = "~";
@ -208,7 +218,13 @@ readwrite(int nfd)
return; return;
} }
} }
} setup_select:
FD_ZERO(&read_fds);
FD_SET(wfd, &read_fds);
FD_SET(nfd, &read_fds);
timeout.tv_sec = 1;
timeout.tv_usec = 0;
} // while
} }
void void