scroll

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

commit 5e42d0b7dbe680c630912169d4fc93e23748c7aa
parent 0424c45b6df02ff69e112700be676623e53e2f6a
Author: Jan Klemkow <j.klemkow@wemelug.de>
Date:   Mon,  3 Feb 2020 22:35:52 +0100

add dynamic allocated input buffer

Diffstat:
MTODO | 1-
Mscroll.c | 32+++++++++++++++++++++-----------
2 files changed, 21 insertions(+), 12 deletions(-)

diff --git a/TODO b/TODO @@ -1,5 +1,4 @@ * strlen function which is aware of ESC sequences and unicode - * dynamic input buffer * Alternative Screen mode detection * scroll down function * scroll/jump to buttom on new input diff --git a/scroll.c b/scroll.c @@ -83,18 +83,18 @@ reset(void) } void -addline(char *str) +addline(char *str, size_t size) { struct line *line = malloc(sizeof *line); if (line == NULL) die("malloc:"); - line->len = strlen(str); - line->str = strdup(str); - + line->len = size; + line->str = malloc(size); if (line->str == NULL) - die("strdup"); + die("malloc:"); + memcpy(line->str, str, size); bottom = line; @@ -185,8 +185,12 @@ main(int argc, char *argv[]) die("tcsetattr:"); fd_set rd; - char buf[10000], *p = buf; - memset(buf, 0, sizeof buf); + size_t size = BUFSIZ, pos = 0; + char *buf = calloc(size, sizeof *buf); + if (buf == NULL) + die("calloc:"); + memset(buf, 0, size); + for (;;) { char c; @@ -208,11 +212,17 @@ main(int argc, char *argv[]) if (FD_ISSET(mfd, &rd)) { if (read(mfd, &c, 1) <= 0 && errno != EINTR) die("read:"); - *p++ = c; + buf[pos++] = c; + if (pos == size) { + size *= 2; + buf = realloc(buf, size); + if (buf == NULL) + die("realloc:"); + } if (c == '\n') { - p = buf; - addline(buf); - memset(buf, 0, sizeof buf); + addline(buf, pos); + memset(buf, 0, size); + pos = 0; } if (write(STDOUT_FILENO, &c, 1) == -1) die("write:");