From: Steve Reinhardt Date: Fri, 20 Oct 2006 04:42:30 +0000 (-0700) Subject: m5term: assume localhost if host name not provided. X-Git-Tag: m5_2.0_beta2~83^2~10 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=3772e4fc97c352138b1262b1708632b3b7fa48b7;p=gem5.git 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 --- diff --git a/util/term/term.c b/util/term/term.c index 8a95480b1..597966159 100644 --- a/util/term/term.c +++ b/util/term/term.c @@ -60,46 +60,49 @@ void usage(int); int main(int argc, char *argv[]) { - int ch, s, ret; - char *host, *port, *endp; - struct addrinfo hints; - socklen_t len; - - ret = 1; - s = 0; - host = NULL; - port = NULL; - endp = NULL; - - 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); - + int ch, s, ret; + char *host, *port, *endp; + struct addrinfo hints; + socklen_t len; + + ret = 1; + s = 0; + host = NULL; + port = NULL; + endp = NULL; + + strncpy(progname, argv[0], sizeof progname); + + /* 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]; port = argv[2]; + } else { + usage(1); + } + if (!isatty(STDIN_FILENO)) + errx(1, "not attached to a terminal"); - if (!isatty(STDIN_FILENO)) - errx(1, "not attached to a terminal"); + raw_term(); - 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 */ - 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); - s = remote_connect(host, port, hints); - ret = 0; - readwrite(s); + if (s) + close(s); - if (s) - close(s); - - exit(ret); + exit(ret); } /* @@ -110,28 +113,28 @@ main(int argc, char *argv[]) int remote_connect(char *host, char *port, struct addrinfo hints) { - struct addrinfo *res, *res0; - int s, error; + struct addrinfo *res, *res0; + int s, error; - 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)); - 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; - if (connect(s, res0->ai_addr, res0->ai_addrlen) == 0) - break; + if (connect(s, res0->ai_addr, res0->ai_addrlen) == 0) + break; - close(s); - s = -1; - } while ((res0 = res0->ai_next) != NULL); + close(s); + s = -1; + } 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 readwrite(int nfd) { - struct pollfd pfd[2]; - char buf[BUFSIZ]; - int wfd = fileno(stdin), n, ret; - int lfd = fileno(stdout); - int escape = 0; - - /* Setup Network FD */ - pfd[0].fd = nfd; - pfd[0].events = POLLIN; - - /* Setup STDIN FD */ - pfd[1].fd = wfd; - pfd[1].events = POLLIN; - - while (pfd[0].fd != -1) { - if ((n = poll(pfd, 2, -1)) < 0) { - close(nfd); - err(1, "Polling Error"); - } + struct pollfd pfd[2]; + char buf[BUFSIZ]; + int wfd = fileno(stdin), n, ret; + int lfd = fileno(stdout); + int escape = 0; + + /* Setup Network FD */ + pfd[0].fd = nfd; + pfd[0].events = POLLIN; + + /* Setup STDIN FD */ + pfd[1].fd = wfd; + pfd[1].events = POLLIN; + + while (pfd[0].fd != -1) { + if ((n = poll(pfd, 2, -1)) < 0) { + close(nfd); + err(1, "Polling Error"); + } - if (n == 0) - 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[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 (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 ((ret = atomicio(write, nfd, buf, n)) != n) + return; + } } + } } void usage(int ret) { - fprintf(stderr, "usage: %s hostname port\n", progname); - if (ret) - exit(1); + fprintf(stderr, "usage: %s hostname port\n", progname); + if (ret) + exit(1); } /* @@ -247,22 +250,22 @@ usage(int ret) ssize_t atomicio(ssize_t (*f) (), int fd, void *_s, size_t n) { - char *s = _s; - ssize_t res, pos = 0; - - 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; - } + char *s = _s; + ssize_t res, pos = 0; + + 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; } - return (pos); + } + return (pos); } /* @@ -284,28 +287,28 @@ atomicio(ssize_t (*f) (), int fd, void *_s, size_t n) void raw_term() { - struct termios ios; + struct termios ios; - if (tcgetattr(STDIN_FILENO, &ios) < 0) - errx(1, "tcgetagttr\n"); + if (tcgetattr(STDIN_FILENO, &ios) < 0) + 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_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; - if (tcsetattr(STDIN_FILENO, TCSANOW, &ios) < 0) - errx(1, "tcsetattr\n"); + if (tcsetattr(STDIN_FILENO, TCSANOW, &ios) < 0) + errx(1, "tcsetattr\n"); - atexit(restore_term); + atexit(restore_term); } void restore_term() { - tcsetattr(STDIN_FILENO, TCSANOW, &saved_ios); + tcsetattr(STDIN_FILENO, TCSANOW, &saved_ios); }