From 2493b45d9f8220507eaf13277bb95ff7f3655e3f Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Thu, 2 Jun 2022 20:53:17 -0400 Subject: Use stderr instead of /dev/tty, realloc buffer if lines too long For some reason I haven't been able to figure out, trying to poll /dev/tty returns POLLNVAL (and this was using 100% CPU looping), but using stderr instead works fine. --- bin/qf.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'bin') diff --git a/bin/qf.c b/bin/qf.c index 6cf415a3..3b4f5851 100644 --- a/bin/qf.c +++ b/bin/qf.c @@ -17,11 +17,11 @@ #include #include #include -#include #include #include #include #include +#include #include #include @@ -96,14 +96,8 @@ enum { Highlight = 3, }; -static int tty = -1; - static void curse(void) { - tty = open(_PATH_TTY, O_RDWR); - if (tty < 0) err(EX_IOERR, "%s", _PATH_TTY); - FILE *ttyin = fdopen(tty, "r"); - if (!ttyin) err(EX_OSERR, "fdopen"); - set_term(newterm(NULL, stdout, ttyin)); + set_term(newterm(NULL, stdout, stderr)); cbreak(); noecho(); nodelay(stdscr, true); @@ -159,8 +153,7 @@ static void edit(struct Line line) { pid_t pid = fork(); if (pid < 0) err(EX_OSERR, "fork"); if (!pid) { - dup2(tty, STDIN_FILENO); - close(tty); + dup2(STDERR_FILENO, STDIN_FILENO); execlp(editor, editor, cmd, line.path, NULL); err(EX_CONFIG, "%s", editor); } @@ -222,13 +215,15 @@ int main(void) { curse(); struct pollfd fds[2] = { { .fd = STDIN_FILENO, .events = POLLIN }, - { .fd = tty, .events = POLLIN }, + { .fd = STDERR_FILENO, .events = POLLIN }, }; - char buf[4096]; size_t len = 0; + size_t cap = 4096; + char *buf = malloc(cap); + if (!buf) err(EX_OSERR, "malloc"); while (poll(fds, 2, -1)) { if (fds[0].revents) { - ssize_t n = read(fds[0].fd, &buf[len], sizeof(buf) - len); + ssize_t n = read(fds[0].fd, &buf[len], cap - len); if (n < 0) err(EX_IOERR, "read"); if (!n) fds[0].events = 0; len += n; @@ -244,6 +239,11 @@ int main(void) { } len -= ptr - buf; memmove(buf, ptr, len); + if (len == cap) { + cap *= 2; + buf = realloc(buf, cap); + if (!buf) err(EX_OSERR, "realloc"); + } } if (fds[1].revents) { input(); -- cgit 1.4.1