scroll

scrollbackbuffer program for st
git clone git://git.suckless.org//gitrepos
Log | Files | Refs

commit c242b24cfb682c23f3484e40921667ffa8e6c166
parent 3a664c24cc4008ef1dea102c8d82f5f149e154e4
Author: Jan Klemkow <j.klemkow@wemelug.de>
Date:   Tue, 31 Mar 2020 21:23:55 +0200

buffered i/o for reading from shell

Diffstat:
Mscroll.c | 37++++++++++++++++++++++---------------
1 file changed, 22 insertions(+), 15 deletions(-)

diff --git a/scroll.c b/scroll.c @@ -372,12 +372,12 @@ main(int argc, char *argv[]) }; for (;;) { - char c; - if (poll(pfd, 2, -1) == -1 && errno != EINTR) die("poll:"); if (pfd[0].revents & POLLIN) { + char c; + if (read(STDIN_FILENO, &c, 1) <= 0 && errno != EINTR) die("read:"); if (c == 17) /* ^Q */ @@ -391,23 +391,30 @@ main(int argc, char *argv[]) } } if (pfd[1].revents & POLLIN) { - ssize_t n = read(mfd, &c, 1); + char input[BUFSIZ]; + ssize_t n = read(mfd, input, sizeof input); + if (n == -1 && errno != EINTR) die("read:"); - if (!isaltscreen(c)) { - if (c == '\r') { - addline(buf, pos); - memset(buf, 0, size); - pos = 0; - } - buf[pos++] = c; - if (pos == size) { - size *= 2; - buf = earealloc(buf, size); + + /* iterate over the input buffer */ + for (char *c = input; n-- > 0; c++) { + if (!isaltscreen(*c)) { + if (*c == '\r') { + addline(buf, pos); + memset(buf, 0, size); + pos = 0; + } + buf[pos++] = *c; + if (pos == size) { + size *= 2; + buf = earealloc(buf, size); + } } + + if (write(STDOUT_FILENO, c, 1) == -1) + die("write:"); } - if (write(STDOUT_FILENO, &c, 1) == -1) - die("write:"); } }