summary refs log tree commit diff
diff options
context:
space:
mode:
-rwxr-xr-xhome/.bin/hnel.c29
1 files changed, 15 insertions, 14 deletions
diff --git a/home/.bin/hnel.c b/home/.bin/hnel.c
index 2f00711a..bb087df1 100755
--- a/home/.bin/hnel.c
+++ b/home/.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);