summaryrefslogtreecommitdiffstats
path: root/xterm-keys.c
diff options
context:
space:
mode:
authorNicholas Marriott <nicm@openbsd.org>2009-11-30 16:44:03 +0000
committerNicholas Marriott <nicm@openbsd.org>2009-11-30 16:44:03 +0000
commit29a5931c6a352f12fe4bcc1cb72cb96e073ddca6 (patch)
treeb7fef76523c8fc723b11e45c8cd89f18bf33568a /xterm-keys.c
parent2182e1badcc161c6e6f1ef6dec21a57cc510299e (diff)
Handle partial xterm function key sequences.
Diffstat (limited to 'xterm-keys.c')
-rw-r--r--xterm-keys.c37
1 files changed, 25 insertions, 12 deletions
diff --git a/xterm-keys.c b/xterm-keys.c
index ca94a1f6..f0fbf802 100644
--- a/xterm-keys.c
+++ b/xterm-keys.c
@@ -85,22 +85,28 @@ struct xterm_keys_entry xterm_keys_table[] = {
{ KEYC_DC, "\033[3;_~" },
};
-/* Match key against buffer, treating _ as a wildcard. */
+/*
+ * Match key against buffer, treating _ as a wildcard. Return -1 for no match,
+ * 0 for match, 1 if the end of the buffer is reached (need more data).
+ */
int
xterm_keys_match(const char *template, const char *buf, size_t len)
{
size_t pos;
- if (len == 0 || len < strlen(template))
+ if (len == 0)
return (0);
pos = 0;
do {
if (*template != '_' && buf[pos] != *template)
- return (0);
+ return (-1);
} while (pos++ != len && *++template != '\0');
- return (1);
+ if (*template != '\0') /* partial */
+ return (1);
+
+ return (0);
}
/* Find modifiers based on template. */
@@ -131,22 +137,29 @@ xterm_keys_modifiers(const char *template, const char *buf, size_t len)
return (0);
}
-/* Lookup key from buffer against table. */
+/*
+ * Lookup key from a buffer against the table. Returns 0 for found (and the
+ * key), -1 for not found, 1 for partial match.
+ */
int
-xterm_keys_find(const char *buf, size_t len, size_t *size)
+xterm_keys_find(const char *buf, size_t len, size_t *size, int *key)
{
struct xterm_keys_entry *entry;
u_int i;
for (i = 0; i < nitems(xterm_keys_table); i++) {
entry = &xterm_keys_table[i];
- if (xterm_keys_match(entry->template, buf, len))
- break;
+ switch (xterm_keys_match(entry->template, buf, len)) {
+ case 0:
+ *size = strlen(entry->template);
+ *key = entry->key;
+ *key |= xterm_keys_modifiers(entry->template, buf, len);
+ return (0);
+ case 1:
+ return (1);
+ }
}
- if (i == nitems(xterm_keys_table))
- return (KEYC_NONE);
- *size = strlen(entry->template);
- return (entry->key | xterm_keys_modifiers(entry->template, buf, len));
+ return (-1);
}
/* Lookup a key number from the table. */