]> git.tuebingen.mpg.de Git - paraslash.git/commitdiff
play: Print hex representation of key sequence in help.
authorAndre Noll <maan@tuebingen.mpg.de>
Sun, 27 Mar 2016 03:39:47 +0000 (03:39 +0000)
committerAndre Noll <maan@tuebingen.mpg.de>
Thu, 2 Jun 2016 20:16:23 +0000 (22:16 +0200)
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

diff --git a/play.c b/play.c
index 59a4cec85ce26bb5d5d57c7926ad70245a4c656b..e484427c87c1ed44d0585b67ef813b1c4da48521 100644 (file)
--- 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",