From 046359b034b67972a2a9d91cfd40476bb15f5785 Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Sun, 27 Mar 2016 03:39:47 +0000 Subject: [PATCH] play: Print hex representation of key sequence in help. Some predefined keys of para_play, for example the four cursor keys, are mapped to key sequences which should not be printed verbatim to the console in com_help(). This patch introduces get_key_map_seq_safe(), an alternative to get_key_map_seq() which returns the hexadecimal representation of the bytes in the sequence of the given key. Single character sequences, however, are printed verbatim if the character is printable. com_help() is changed to call get_key_map_seq_safe() instead of get_key_map_seq(). --- play.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/play.c b/play.c index 59a4cec8..e484427c 100644 --- a/play.c +++ b/play.c @@ -592,6 +592,28 @@ static char *get_key_map_seq(int key) get_internal_key_map_seq(key) : get_user_key_map_seq(key); } +static char *get_key_map_seq_safe(int key) +{ + const char hex[] = "0123456789abcdef"; + char *seq = get_key_map_seq(key), *sseq; + size_t n, len = strlen(seq); + + if (len == 1 && isprint(*seq)) + return seq; + sseq = para_malloc(2 + 2 * len + 1); + sseq[0] = '0'; + sseq[1] = 'x'; + for (n = 0; n < len; n++) { + uint8_t val = (seq[n] & 0xf0) >> 4; + sseq[2 + 2 * n] = hex[val]; + val = seq[n] & 0xf; + sseq[2 + 2 * n + 1] = hex[val]; + } + free(seq); + sseq[2 + 2 * n] = '\0'; + return sseq; +} + static inline char *get_internal_key_map_cmd(int key) { return para_strdup(default_commands[get_internal_key_map_idx(key)]); @@ -710,7 +732,7 @@ static int com_help(struct play_task *pt, int argc, char **argv) FOR_EACH_MAPPED_KEY(i) { bool internal = is_internal_key(i); int idx = get_key_map_idx(i); - char *seq = get_key_map_seq(i); + char *seq = get_key_map_seq_safe(i); char *cmd = get_key_map_cmd(i); sz = xasprintf(&buf, "%s key #%d: %s -> %s\n", -- 2.30.2