m5term: assume localhost if host name not provided.

util/term/term.c:
    Reindent.
util/term/term.c:
    Assume localhost if only port number is given on command line.

--HG--
extra : convert_revision : 768e61a56339a0795ca258cca788e9a2c20cbaae
This commit is contained in:
Steve Reinhardt 2006-10-19 21:42:30 -07:00
parent 780aa0a0eb
commit 3772e4fc97

View file

@ -60,46 +60,49 @@ void usage(int);
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int ch, s, ret; int ch, s, ret;
char *host, *port, *endp; char *host, *port, *endp;
struct addrinfo hints; struct addrinfo hints;
socklen_t len; socklen_t len;
ret = 1; ret = 1;
s = 0; s = 0;
host = NULL; host = NULL;
port = NULL; port = NULL;
endp = NULL; endp = NULL;
strncpy(progname, argv[0], sizeof progname); strncpy(progname, argv[0], sizeof progname);
/* 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) {
host = argv[1]; host = argv[1];
port = argv[2]; port = argv[2];
} else {
usage(1);
}
if (!isatty(STDIN_FILENO))
errx(1, "not attached to a terminal");
if (!isatty(STDIN_FILENO)) raw_term();
errx(1, "not attached to a terminal");
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;
/* Initialize addrinfo structure */ s = remote_connect(host, port, hints);
memset(&hints, 0, sizeof(struct addrinfo)); ret = 0;
hints.ai_family = AF_UNSPEC; readwrite(s);
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
s = remote_connect(host, port, hints); if (s)
ret = 0; close(s);
readwrite(s);
if (s) exit(ret);
close(s);
exit(ret);
} }
/* /*
@ -110,28 +113,28 @@ main(int argc, char *argv[])
int int
remote_connect(char *host, char *port, struct addrinfo hints) remote_connect(char *host, char *port, struct addrinfo hints)
{ {
struct addrinfo *res, *res0; struct addrinfo *res, *res0;
int s, error; int s, error;
if ((error = getaddrinfo(host, port, &hints, &res))) if ((error = getaddrinfo(host, port, &hints, &res)))
errx(1, "getaddrinfo: %s", gai_strerror(error)); errx(1, "getaddrinfo: %s", gai_strerror(error));
res0 = res; res0 = res;
do { do {
if ((s = socket(res0->ai_family, res0->ai_socktype, if ((s = socket(res0->ai_family, res0->ai_socktype,
res0->ai_protocol)) < 0) res0->ai_protocol)) < 0)
continue; continue;
if (connect(s, res0->ai_addr, res0->ai_addrlen) == 0) if (connect(s, res0->ai_addr, res0->ai_addrlen) == 0)
break; break;
close(s); close(s);
s = -1; s = -1;
} while ((res0 = res0->ai_next) != NULL); } while ((res0 = res0->ai_next) != NULL);
freeaddrinfo(res); freeaddrinfo(res);
return (s); return (s);
} }
/* /*
@ -141,79 +144,79 @@ remote_connect(char *host, char *port, struct addrinfo hints)
void void
readwrite(int nfd) readwrite(int nfd)
{ {
struct pollfd pfd[2]; struct pollfd pfd[2];
char buf[BUFSIZ]; char buf[BUFSIZ];
int wfd = fileno(stdin), n, ret; int wfd = fileno(stdin), n, ret;
int lfd = fileno(stdout); int lfd = fileno(stdout);
int escape = 0; int escape = 0;
/* Setup Network FD */ /* Setup Network FD */
pfd[0].fd = nfd; pfd[0].fd = nfd;
pfd[0].events = POLLIN; pfd[0].events = POLLIN;
/* Setup STDIN FD */ /* Setup STDIN FD */
pfd[1].fd = wfd; pfd[1].fd = wfd;
pfd[1].events = POLLIN; pfd[1].events = POLLIN;
while (pfd[0].fd != -1) { while (pfd[0].fd != -1) {
if ((n = poll(pfd, 2, -1)) < 0) { if ((n = poll(pfd, 2, -1)) < 0) {
close(nfd); close(nfd);
err(1, "Polling Error"); err(1, "Polling Error");
}
if (n == 0)
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;
}
}
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 (n == 0)
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;
}
}
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;
}
}
}
} }
void void
usage(int ret) usage(int ret)
{ {
fprintf(stderr, "usage: %s hostname port\n", progname); fprintf(stderr, "usage: %s hostname port\n", progname);
if (ret) if (ret)
exit(1); exit(1);
} }
/* /*
@ -247,22 +250,22 @@ usage(int ret)
ssize_t ssize_t
atomicio(ssize_t (*f) (), int fd, void *_s, size_t n) atomicio(ssize_t (*f) (), int fd, void *_s, size_t n)
{ {
char *s = _s; char *s = _s;
ssize_t res, pos = 0; ssize_t res, pos = 0;
while (n > pos) { while (n > pos) {
res = (f) (fd, s + pos, n - pos); res = (f) (fd, s + pos, n - pos);
switch (res) { switch (res) {
case -1: case -1:
if (errno == EINTR || errno == EAGAIN) if (errno == EINTR || errno == EAGAIN)
continue; continue;
case 0: case 0:
return (res); return (res);
default: default:
pos += res; pos += res;
}
} }
return (pos); }
return (pos);
} }
/* /*
@ -284,28 +287,28 @@ atomicio(ssize_t (*f) (), int fd, void *_s, size_t n)
void void
raw_term() raw_term()
{ {
struct termios ios; struct termios ios;
if (tcgetattr(STDIN_FILENO, &ios) < 0) if (tcgetattr(STDIN_FILENO, &ios) < 0)
errx(1, "tcgetagttr\n"); errx(1, "tcgetagttr\n");
memcpy(&saved_ios, &ios, sizeof(struct termios)); memcpy(&saved_ios, &ios, sizeof(struct termios));
ios.c_iflag &= ~(ISTRIP|ICRNL|IGNCR|ICRNL|IXOFF|IXON); ios.c_iflag &= ~(ISTRIP|ICRNL|IGNCR|ICRNL|IXOFF|IXON);
ios.c_oflag &= ~(OPOST); ios.c_oflag &= ~(OPOST);
ios.c_oflag &= (ONLCR); ios.c_oflag &= (ONLCR);
ios.c_lflag &= ~(ISIG|ICANON|ECHO); ios.c_lflag &= ~(ISIG|ICANON|ECHO);
ios.c_cc[VMIN] = 1; ios.c_cc[VMIN] = 1;
ios.c_cc[VTIME] = 0; ios.c_cc[VTIME] = 0;
if (tcsetattr(STDIN_FILENO, TCSANOW, &ios) < 0) if (tcsetattr(STDIN_FILENO, TCSANOW, &ios) < 0)
errx(1, "tcsetattr\n"); errx(1, "tcsetattr\n");
atexit(restore_term); atexit(restore_term);
} }
void void
restore_term() restore_term()
{ {
tcsetattr(STDIN_FILENO, TCSANOW, &saved_ios); tcsetattr(STDIN_FILENO, TCSANOW, &saved_ios);
} }