diff options
author | June McEnroe <programble@gmail.com> | 2018-02-21 20:29:45 -0500 |
---|---|---|
committer | June McEnroe <programble@gmail.com> | 2018-02-21 20:29:45 -0500 |
commit | 51ddce4e388bdbc87e480bf47bcbe429f10564d5 (patch) | |
tree | a87c0596e5f3cbefc15a5f88b891059b3622a024 | |
parent | Add view with just window size matching (diff) | |
download | stream-51ddce4e388bdbc87e480bf47bcbe429f10564d5.tar.gz stream-51ddce4e388bdbc87e480bf47bcbe429f10564d5.zip |
Read input and remote in view
Diffstat (limited to '')
-rw-r--r-- | view.c | 87 |
1 files changed, 67 insertions, 20 deletions
diff --git a/view.c b/view.c index ca7f206..d7ca456 100644 --- a/view.c +++ b/view.c @@ -16,13 +16,20 @@ #include <err.h> #include <fcntl.h> +#include <poll.h> #include <signal.h> #include <stdio.h> #include <stdlib.h> #include <sys/ioctl.h> #include <sysexits.h> +#include <termios.h> #include <unistd.h> +static struct termios saveTerm; +static void restoreTerm(void) { + tcsetattr(STDOUT_FILENO, TCSADRAIN, &saveTerm); +} + static struct { int read; int write; @@ -41,39 +48,79 @@ static void sigwinch() { } int main(int argc, char *argv[]) { + int error; + if (argc < 2) return EX_USAGE; const char *path = argv[1]; - int fd = open(path, O_RDONLY); - if (fd < 0) err(EX_NOINPUT, "%s", path); + int input = STDIN_FILENO; + int local = STDOUT_FILENO; + + error = tcgetattr(local, &saveTerm); + if (error) err(EX_IOERR, "tcgetattr"); + atexit(restoreTerm); + + struct termios term = saveTerm; + term.c_lflag &= ~(ICANON | ECHO); + error = tcsetattr(local, TCSADRAIN, &term); + if (error) err(EX_IOERR, "tcsetattr"); + + int remote = open(path, O_RDONLY); + if (remote < 0) err(EX_NOINPUT, "%s", path); struct winsize remoteWindow; - ssize_t size = read(fd, &remoteWindow, sizeof(remoteWindow)); + ssize_t size = read(remote, &remoteWindow, sizeof(remoteWindow)); if (size < 0) err(EX_IOERR, "%s", path); if ((size_t)size < sizeof(remoteWindow)) errx(EX_IOERR, "%s: short read", path); - int error = pipe((int *)&winch); + error = pipe((int *)&winch); if (error) err(EX_OSERR, "pipe"); - signal(SIGWINCH, sigwinch); sigwinch(); - for (;;) { - struct winsize localWindow; - ssize_t size = read(winch.read, &localWindow, sizeof(localWindow)); - if (size < 0) err(EX_IOERR, "read(%d)", winch.read); - if ((size_t)size < sizeof(localWindow)) { - errx(EX_IOERR, "short read(%d)", winch.read); + + char buf[4096]; + struct pollfd fds[3] = { + { .fd = winch.read, .events = POLLIN }, + { .fd = input, .events = POLLIN }, + { .fd = remote, .events = POLLIN }, + }; + while (0 < poll(fds, 3, -1)) { + if (fds[0].revents) { + for (;;) { + struct winsize localWindow; + ssize_t size = read(winch.read, &localWindow, sizeof(localWindow)); + if (size < 0) err(EX_IOERR, "read(%d)", winch.read); + if ((size_t)size < sizeof(localWindow)) { + errx(EX_IOERR, "short read(%d)", winch.read); + } + + if ( + localWindow.ws_col == remoteWindow.ws_col + && localWindow.ws_row == remoteWindow.ws_row + ) break; + + warnx( + "Please resize your terminal %hux%hu -> %hux%hu", + localWindow.ws_col, localWindow.ws_row, + remoteWindow.ws_col, remoteWindow.ws_row + ); + } } - if ( - localWindow.ws_col == remoteWindow.ws_col - && localWindow.ws_row == remoteWindow.ws_row - ) break; + if (fds[1].revents) { + ssize_t size = read(input, buf, sizeof(buf)); + if (size < 0) err(EX_IOERR, "read(%d)", input); + if (size == 1 && buf[0] == 'q') return EX_OK; + } + + if (fds[2].revents) { + ssize_t readSize = read(remote, buf, sizeof(buf)); + if (readSize < 0) err(EX_IOERR, "read(%d)", remote); - printf( - "%hux%hu -> %hux%hu\n", - localWindow.ws_col, localWindow.ws_row, - remoteWindow.ws_col, remoteWindow.ws_row - ); + ssize_t writeSize = write(local, buf, readSize); + if (writeSize < 0) err(EX_IOERR, "write(%d)", local); + if (writeSize < readSize) errx(EX_IOERR, "short write(%d)", local); + } } + err(EX_IOERR, "poll"); } |