63,66c63,66
< int ch, s, ret;
< char *host, *port, *endp;
< struct addrinfo hints;
< socklen_t len;
---
> int ch, s, ret;
> char *host, *port, *endp;
> struct addrinfo hints;
> socklen_t len;
68,72c68,72
< ret = 1;
< s = 0;
< host = NULL;
< port = NULL;
< endp = NULL;
---
> ret = 1;
> s = 0;
> host = NULL;
> port = NULL;
> endp = NULL;
74c74
< strncpy(progname, argv[0], sizeof progname);
---
> strncpy(progname, argv[0], sizeof progname);
76,79c76,80
< /* Cruft to make sure options are clean, and used properly. */
< if (argc != 3 || !argv[1] || !argv[2])
< usage(1);
<
---
> /* Cruft to make sure options are clean, and used properly. */
> if (argc == 2) {
> host = "localhost";
> port = argv[1];
> } else if (argc == 3) {
81a83,85
> } else {
> usage(1);
> }
82a87,88
> if (!isatty(STDIN_FILENO))
> errx(1, "not attached to a terminal");
84,85c90
< if (!isatty(STDIN_FILENO))
< errx(1, "not attached to a terminal");
---
> raw_term();
87c92,96
< raw_term();
---
> /* Initialize addrinfo structure */
> memset(&hints, 0, sizeof(struct addrinfo));
> hints.ai_family = AF_UNSPEC;
> hints.ai_socktype = SOCK_STREAM;
> hints.ai_protocol = IPPROTO_TCP;
89,93c98,100
< /* Initialize addrinfo structure */
< memset(&hints, 0, sizeof(struct addrinfo));
< hints.ai_family = AF_UNSPEC;
< hints.ai_socktype = SOCK_STREAM;
< hints.ai_protocol = IPPROTO_TCP;
---
> s = remote_connect(host, port, hints);
> ret = 0;
> readwrite(s);
95,97c102,103
< s = remote_connect(host, port, hints);
< ret = 0;
< readwrite(s);
---
> if (s)
> close(s);
99,102c105
< if (s)
< close(s);
<
< exit(ret);
---
> exit(ret);
113,114c116,117
< struct addrinfo *res, *res0;
< int s, error;
---
> struct addrinfo *res, *res0;
> int s, error;
116,117c119,120
< if ((error = getaddrinfo(host, port, &hints, &res)))
< errx(1, "getaddrinfo: %s", gai_strerror(error));
---
> if ((error = getaddrinfo(host, port, &hints, &res)))
> errx(1, "getaddrinfo: %s", gai_strerror(error));
119,123c122,126
< res0 = res;
< do {
< if ((s = socket(res0->ai_family, res0->ai_socktype,
< res0->ai_protocol)) < 0)
< continue;
---
> res0 = res;
> do {
> if ((s = socket(res0->ai_family, res0->ai_socktype,
> res0->ai_protocol)) < 0)
> continue;
125,126c128,129
< if (connect(s, res0->ai_addr, res0->ai_addrlen) == 0)
< break;
---
> if (connect(s, res0->ai_addr, res0->ai_addrlen) == 0)
> break;
128,130c131,133
< close(s);
< s = -1;
< } while ((res0 = res0->ai_next) != NULL);
---
> close(s);
> s = -1;
> } while ((res0 = res0->ai_next) != NULL);
132c135
< freeaddrinfo(res);
---
> freeaddrinfo(res);
134c137
< return (s);
---
> return (s);
144,148c147,151
< struct pollfd pfd[2];
< char buf[BUFSIZ];
< int wfd = fileno(stdin), n, ret;
< int lfd = fileno(stdout);
< int escape = 0;
---
> struct pollfd pfd[2];
> char buf[BUFSIZ];
> int wfd = fileno(stdin), n, ret;
> int lfd = fileno(stdout);
> int escape = 0;
150,152c153,155
< /* Setup Network FD */
< pfd[0].fd = nfd;
< pfd[0].events = POLLIN;
---
> /* Setup Network FD */
> pfd[0].fd = nfd;
> pfd[0].events = POLLIN;
154,156c157,159
< /* Setup STDIN FD */
< pfd[1].fd = wfd;
< pfd[1].events = POLLIN;
---
> /* Setup STDIN FD */
> pfd[1].fd = wfd;
> pfd[1].events = POLLIN;
158,162c161,165
< while (pfd[0].fd != -1) {
< if ((n = poll(pfd, 2, -1)) < 0) {
< close(nfd);
< err(1, "Polling Error");
< }
---
> while (pfd[0].fd != -1) {
> if ((n = poll(pfd, 2, -1)) < 0) {
> close(nfd);
> err(1, "Polling Error");
> }
164,165c167,168
< if (n == 0)
< return;
---
> if (n == 0)
> return;
167,178c170,181
< if (pfd[0].revents & POLLIN) {
< if ((n = read(nfd, buf, sizeof(buf))) < 0)
< return;
< else if (n == 0) {
< shutdown(nfd, SHUT_RD);
< pfd[0].fd = -1;
< pfd[0].events = 0;
< } else {
< if ((ret = atomicio(write, lfd, buf, n)) != n)
< return;
< }
< }
---
> if (pfd[0].revents & POLLIN) {
> if ((n = read(nfd, buf, sizeof(buf))) < 0)
> return;
> else if (n == 0) {
> shutdown(nfd, SHUT_RD);
> pfd[0].fd = -1;
> pfd[0].events = 0;
> } else {
> if ((ret = atomicio(write, lfd, buf, n)) != n)
> return;
> }
> }
180,206c183,204
< if (pfd[1].revents & POLLIN) {
< if ((n = read(wfd, buf, sizeof(buf))) < 0)
< return;
< else if (n == 0) {
< shutdown(nfd, SHUT_WR);
< pfd[1].fd = -1;
< pfd[1].events = 0;
< } else {
< if (escape) {
< char buf2[] = "~";
< if (*buf == '.') {
< printf("quit!\n");
< return;
< }
< escape = 0;
< if (*buf != '~' &&
< (ret = atomicio(write, nfd, buf2, 1)) != n)
< return;
< } else {
< escape = (*buf == '~');
< if (escape)
< continue;
< }
<
< if ((ret = atomicio(write, nfd, buf, n)) != n)
< return;
< }
---
> if (pfd[1].revents & POLLIN) {
> if ((n = read(wfd, buf, sizeof(buf))) < 0)
> return;
> else if (n == 0) {
> shutdown(nfd, SHUT_WR);
> pfd[1].fd = -1;
> pfd[1].events = 0;
> } else {
> if (escape) {
> char buf2[] = "~";
> if (*buf == '.') {
> printf("quit!\n");
> return;
> }
> escape = 0;
> if (*buf != '~' &&
> (ret = atomicio(write, nfd, buf2, 1)) != n)
> return;
> } else {
> escape = (*buf == '~');
> if (escape)
> continue;
207a206,209
>
> if ((ret = atomicio(write, nfd, buf, n)) != n)
> return;
> }
208a211
> }
214,216c217,219
< fprintf(stderr, "usage: %s hostname port\n", progname);
< if (ret)
< exit(1);
---
> fprintf(stderr, "usage: %s hostname port\n", progname);
> if (ret)
> exit(1);
250,251c253,254
< char *s = _s;
< ssize_t res, pos = 0;
---
> char *s = _s;
> ssize_t res, pos = 0;
253,263c256,265
< while (n > pos) {
< res = (f) (fd, s + pos, n - pos);
< switch (res) {
< case -1:
< if (errno == EINTR || errno == EAGAIN)
< continue;
< case 0:
< return (res);
< default:
< pos += res;
< }
---
> while (n > pos) {
> res = (f) (fd, s + pos, n - pos);
> switch (res) {
> case -1:
> if (errno == EINTR || errno == EAGAIN)
> continue;
> case 0:
> return (res);
> default:
> pos += res;
265c267,268
< return (pos);
---
> }
> return (pos);
287c290
< struct termios ios;
---
> struct termios ios;
289,290c292,293
< if (tcgetattr(STDIN_FILENO, &ios) < 0)
< errx(1, "tcgetagttr\n");
---
> if (tcgetattr(STDIN_FILENO, &ios) < 0)
> errx(1, "tcgetagttr\n");
292c295
< memcpy(&saved_ios, &ios, sizeof(struct termios));
---
> memcpy(&saved_ios, &ios, sizeof(struct termios));
294,299c297,302
< ios.c_iflag &= ~(ISTRIP|ICRNL|IGNCR|ICRNL|IXOFF|IXON);
< ios.c_oflag &= ~(OPOST);
< ios.c_oflag &= (ONLCR);
< ios.c_lflag &= ~(ISIG|ICANON|ECHO);
< ios.c_cc[VMIN] = 1;
< ios.c_cc[VTIME] = 0;
---
> ios.c_iflag &= ~(ISTRIP|ICRNL|IGNCR|ICRNL|IXOFF|IXON);
> ios.c_oflag &= ~(OPOST);
> ios.c_oflag &= (ONLCR);
> ios.c_lflag &= ~(ISIG|ICANON|ECHO);
> ios.c_cc[VMIN] = 1;
> ios.c_cc[VTIME] = 0;
301,302c304,305
< if (tcsetattr(STDIN_FILENO, TCSANOW, &ios) < 0)
< errx(1, "tcsetattr\n");
---
> if (tcsetattr(STDIN_FILENO, TCSANOW, &ios) < 0)
> errx(1, "tcsetattr\n");
304c307
< atexit(restore_term);
---
> atexit(restore_term);
310c313
< tcsetattr(STDIN_FILENO, TCSANOW, &saved_ios);
---
> tcsetattr(STDIN_FILENO, TCSANOW, &saved_ios);