vis

a vi-like editor based on Plan 9's structural regular expressions

git clone https://9o.is/git/vis.git

commit d9b893f8402dfd29a866d4aeedabfa5dda742077
parent 50d615e27f6dd4135d75fcbc492391972c698d1e
Author: Marc André Tanner <mat@brain-dump.org>
Date:   Tue,  8 Nov 2016 20:10:17 +0100

test/util: fix key parsing in keys utility

We should only attempt to parse special keys if they are
delimited by angle brackets i.e. <Key> but not Key.

Diffstat:
Mutil/keys.c | 25++++++++++++++++++++-----
1 file changed, 20 insertions(+), 5 deletions(-)

diff --git a/util/keys.c b/util/keys.c @@ -1,9 +1,13 @@ #include <stdio.h> #include <stdarg.h> +#include <string.h> #include <stdlib.h> #include <unistd.h> #include <termkey.h> +/* is c the start of a utf8 sequence? */ +#define ISUTF8(c) (((c)&0xC0)!=0x80) + static TermKey *termkey; static void die(const char *errstr, ...) { @@ -140,19 +144,30 @@ int main(int argc, char *argv[]) { if (!(termkey = termkey_new_abstract(term, TERMKEY_FLAG_UTF8))) die("Failed to initialize libtermkey\n"); while (fgets(buf, sizeof buf, file)) { - TermKeyKey key; const char *keys = buf, *next; while (*keys) { + TermKeyKey key = { 0 }; if (*keys == '\n') { keys++; } else if (*keys == '<' && (next = termkey_strpkey(termkey, keys+1, &key, TERMKEY_FORMAT_VIM)) && *next == '>') { printkey(&key); keys = next+1; - } else if ((next = termkey_strpkey(termkey, keys, &key, TERMKEY_FORMAT_VIM))) { - printkey(&key); - keys = next; } else { - die("Failed to parse keys: %s\n", keys); + const char *start = keys; + if (ISUTF8(*keys)) + keys++; + while (!ISUTF8(*keys)) + keys++; + size_t len = keys - start; + if (len >= sizeof(key.utf8)) + die("Too long UTF-8 sequence: %s\n", start); + // FIXME: not really correct, bug good enough for now + key.type = TERMKEY_TYPE_UNICODE; + key.modifiers = 0; + if (len > 0) + memcpy(key.utf8, start, len); + key.utf8[len] = '\0'; + printkey(&key); } } }