summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <programble@gmail.com>2017-07-23 00:23:47 -0400
committerJune McEnroe <programble@gmail.com>2017-07-23 00:23:47 -0400
commit4a8a186bc0b8ef0caa187c478241822abe33472c (patch)
treeaf6d0d765ecbc3d1f0cda870e5457309065a1c08
parentSet window size in hnel (diff)
downloadsrc-4a8a186bc0b8ef0caa187c478241822abe33472c.tar.gz
src-4a8a186bc0b8ef0caa187c478241822abe33472c.zip
Clean up hnel a tiny bit
-rwxr-xr-xcurtis/.bin/hnel.c29
1 files changed, 15 insertions, 14 deletions
diff --git a/curtis/.bin/hnel.c b/curtis/.bin/hnel.c
index 2f00711a..bb087df1 100755
--- a/curtis/.bin/hnel.c
+++ b/curtis/.bin/hnel.c
@@ -2,7 +2,7 @@
 exec cc -Wall -Wextra -pedantic $@ -lutil -o $(dirname $0)/hnel $0
 #endif
 
-// Wrapper for preserving HJKL in Tarmak layouts.
+// PTY wrapper for preserving HJKL in Tarmak layouts.
 
 #include <err.h>
 #include <poll.h>
@@ -38,6 +38,7 @@ static void restoreTerm(void) {
 
 int main(int argc, char *argv[]) {
     int error;
+
     if (argc < 2) return EX_USAGE;
 
     char table[256] = {0};
@@ -46,9 +47,18 @@ int main(int argc, char *argv[]) {
     table['j'] = 'e'; table['J'] = 'E'; table[CTRL('J')] = CTRL('E');
     table['k'] = 'n'; table['K'] = 'N'; table[CTRL('K')] = CTRL('N');
 
+    error = tcgetattr(STDERR_FILENO, &saveTerm);
+    if (error) err(EX_IOERR, "tcgetattr");
+    atexit(restoreTerm);
+
+    struct termios raw;
+    cfmakeraw(&raw);
+    error = tcsetattr(STDERR_FILENO, TCSADRAIN, &raw);
+    if (error) err(EX_IOERR, "tcsetattr");
+
     struct winsize window;
     error = ioctl(STDERR_FILENO, TIOCGWINSZ, &window);
-    if (error) err(EX_IOERR, "ioctl");
+    if (error) err(EX_IOERR, "ioctl(%d, TIOCGWINSZ)", STDERR_FILENO);
 
     int master;
     pid_t pid = forkpty(&master, NULL, NULL, &window);
@@ -59,23 +69,14 @@ int main(int argc, char *argv[]) {
         err(EX_OSERR, "%s", argv[1]);
     }
 
-    error = tcgetattr(STDERR_FILENO, &saveTerm);
-    if (error) err(EX_IOERR, "tcgetattr");
-    atexit(restoreTerm);
-
-    struct termios raw;
-    cfmakeraw(&raw);
-    error = tcsetattr(STDERR_FILENO, TCSADRAIN, &raw);
-    if (error) err(EX_IOERR, "tcsetattr");
-
     struct pollfd fds[2] = {
         { .fd = STDIN_FILENO, .events = POLLIN },
         { .fd = master, .events = POLLIN },
     };
-
-    char buf[4096];
-    ssize_t len;
     while (0 < poll(fds, 2, -1)) {
+        char buf[4096];
+        ssize_t len;
+
         if (fds[0].revents) {
             len = read(STDIN_FILENO, buf, sizeof(buf));
             if (len < 0) err(EX_IOERR, "read(%d)", STDIN_FILENO);