summary refs log tree commit diff
path: root/bin
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2018-01-29 23:24:48 -0500
committerJune McEnroe <june@causal.agency>2018-01-29 23:24:48 -0500
commit12355bb4b09e7fde06ffaa1bde2b8670f6b541c8 (patch)
tree597da99ad1eacb4a9f3c4bd67f6520ebcda4bf96 /bin
parentRemove Tarmak remappings from vim (diff)
downloadsrc-12355bb4b09e7fde06ffaa1bde2b8670f6b541c8.tar.gz
src-12355bb4b09e7fde06ffaa1bde2b8670f6b541c8.zip
Generalize hnel
It's like tr for PTYs.
Diffstat (limited to 'bin')
-rw-r--r--bin/hnel.c20
1 files changed, 10 insertions, 10 deletions
diff --git a/bin/hnel.c b/bin/hnel.c
index 0a0e52fa..8a3a6502 100644
--- a/bin/hnel.c
+++ b/bin/hnel.c
@@ -14,12 +14,13 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-// PTY wrapper for preserving HJKL in Tarmak layouts.
+// PTY wrapper for remapping arbitrary keys.
 
 #include <err.h>
 #include <poll.h>
 #include <stdbool.h>
 #include <stdlib.h>
+#include <string.h>
 #include <sys/ioctl.h>
 #include <sys/wait.h>
 #include <sysexits.h>
@@ -34,13 +35,6 @@
 #include <util.h>
 #endif
 
-static char table[256] = {
-    ['n'] = 'j', ['N'] = 'J', [CTRL('N')] = CTRL('J'),
-    ['e'] = 'k', ['E'] = 'K', [CTRL('E')] = CTRL('K'),
-    ['j'] = 'e', ['J'] = 'E', [CTRL('J')] = CTRL('E'),
-    ['k'] = 'n', ['K'] = 'N', [CTRL('K')] = CTRL('N'),
-};
-
 static ssize_t writeAll(int fd, const char *buf, size_t len) {
     ssize_t writeLen;
     while (0 < (writeLen = write(fd, buf, len))) {
@@ -59,7 +53,13 @@ static void restoreTerm(void) {
 int main(int argc, char *argv[]) {
     int error;
 
-    if (argc < 2) return EX_USAGE;
+    if (argc < 4) return EX_USAGE;
+
+    char table[256] = {0};
+    if (strlen(argv[1]) != strlen(argv[2])) return EX_USAGE;
+    for (const char *from = argv[1], *to = argv[2]; *from; ++from, ++to) {
+        table[(unsigned)*from] = *to;
+    }
 
     error = tcgetattr(STDERR_FILENO, &saveTerm);
     if (error) err(EX_IOERR, "tcgetattr");
@@ -79,7 +79,7 @@ int main(int argc, char *argv[]) {
     if (pid < 0) err(EX_OSERR, "forkpty");
 
     if (!pid) {
-        execvp(argv[1], argv + 1);
+        execvp(argv[3], argv + 3);
         err(EX_OSERR, "%s", argv[1]);
     }