summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <programble@gmail.com>2018-02-21 20:29:45 -0500
committerJune McEnroe <programble@gmail.com>2018-02-21 20:29:45 -0500
commit51ddce4e388bdbc87e480bf47bcbe429f10564d5 (patch)
treea87c0596e5f3cbefc15a5f88b891059b3622a024
parentAdd view with just window size matching (diff)
downloadstream-51ddce4e388bdbc87e480bf47bcbe429f10564d5.tar.gz
stream-51ddce4e388bdbc87e480bf47bcbe429f10564d5.zip
Read input and remote in view
-rw-r--r--view.c87
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");
 }
r done by a post-processing tool such as cat -n or producing a two-column HTML <table>. 2019-02-18Add Tag class to hiJune McEnroe 2019-02-17Generate HTML with hi -n -f html -o anchorJune McEnroe Running hi twice to insert stuff between the head and the content is a bit of a hack but oh well. 2019-02-17Add hi -f html -o anchor for line number linksJune McEnroe 2019-02-17Simplify temp trap in upJune McEnroe 2019-02-17Add line numbers to hiJune McEnroe Renames previous -n option to -m to stay consistent with cat -n. Prefixing lines with line numbers affects where the first tab indent ends up relative to the text above it. Not sure if it's worth fixing somehow. 2019-02-17Always split spans after newlinesJune McEnroe Simplifies ANSI and IRC output code, and prepares for line numbered output. 2019-02-15Color format specifiers light cyan in vimJune McEnroe 2019-02-15Highlight Interp as yellowJune McEnroe 2019-02-15Highlight strings in sh command substitutionsJune McEnroe 2019-02-15Add nmap gpJune McEnroe 2019-02-14Avoid newline when copying URL to pasteboardJune McEnroe 2019-02-13Add forgotten "sixth" book of H2G2June McEnroe