From f7d32ebb01ba900361640c9a63157319af714241 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Tue, 8 Nov 2016 20:10:17 +0100 Subject: 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. but not Key. --- util/keys.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/util/keys.c b/util/keys.c index 4940400..2026bad 100644 --- a/util/keys.c +++ b/util/keys.c @@ -1,9 +1,13 @@ #include #include +#include #include #include #include +/* 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); } } } -- cgit v1.2.3