From f680d078b6f31d01cda1227641e94defd9c799b3 Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Tue, 18 Feb 2020 22:31:23 +0000 Subject: [PATCH] abufs should have a capacity --- ke/config.sh | 2 +- ke/keypress.c | 10 ++++++---- ke/main.c | 20 ++++++++++++++++---- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/ke/config.sh b/ke/config.sh index 47005f7..8da5afe 100755 --- a/ke/config.sh +++ b/ke/config.sh @@ -21,7 +21,7 @@ then fi if [ -e "/usr/lib/libefence.a" ] then - OS_LIBS="${OS_LIBS} -lefence" + OS_LIBS="${OS_LIBS}" # -lefence" fi else OS_CFLAGS="" diff --git a/ke/keypress.c b/ke/keypress.c index 8586fb2..e531322 100644 --- a/ke/keypress.c +++ b/ke/keypress.c @@ -3,6 +3,7 @@ #include #include #include +#include struct termios orig_termios; @@ -33,15 +34,16 @@ main() { enableRawMode(); - char c; - while (read(STDIN_FILENO, &c, 1) == 1 && c != 'q') { - if (iscntrl(c)) { + wchar_t c; + while (read(STDIN_FILENO, &c, sizeof(c)) > 0 && c != 'q') { + if (iswcntrl(c)) { printf("$%02x\n", c); } else { - printf("$%02x ('%c')\n", c, c); + printf("$%02x ('%lc')\n", c, c); } } + perror("read"); return 0; } diff --git a/ke/main.c b/ke/main.c index ed5687d..87f3069 100644 --- a/ke/main.c +++ b/ke/main.c @@ -51,8 +51,9 @@ struct abuf { char *b; int len; + int cap; }; -#define ABUF_INIT {NULL, 0} +#define ABUF_INIT {NULL, 0, 0} void ab_append(struct abuf *buf, const char *s, int len); void ab_free(struct abuf *buf); @@ -157,10 +158,19 @@ struct editor_t { void ab_append(struct abuf *buf, const char *s, int len) { - char *nc = realloc(buf->b, buf->len + len); + char *nc = buf->b; + int sz = buf->len + len; - if (nc == NULL) { - abort(); + if (sz >= buf->cap) { + while (sz > buf->cap) { + if (buf->cap == 0) { + buf->cap = 1; + } else { + buf->cap *= 2; + } + } + nc = realloc(nc, buf->cap); + assert(nc != NULL); } memcpy(&nc[buf->len], s, len); @@ -174,6 +184,8 @@ ab_free(struct abuf *buf) { free(buf->b); buf->b = NULL; + buf->len = 0; + buf->cap = 0; } char