aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc André Tanner <mat@brain-dump.org>2016-11-08 20:10:17 +0100
committerMarc André Tanner <mat@brain-dump.org>2016-11-08 22:28:02 +0100
commitf7d32ebb01ba900361640c9a63157319af714241 (patch)
treeac9ffe389def4f2e63a44a6b4f4d1961c28599f1
parent39605a281ac53277e77ab20ffd07b0d02fc6b272 (diff)
downloadvis-f7d32ebb01ba900361640c9a63157319af714241.tar.gz
vis-f7d32ebb01ba900361640c9a63157319af714241.tar.xz
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.
-rw-r--r--util/keys.c25
1 files 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 <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);
}
}
}