diff options
Diffstat (limited to 'lib/tty/key.c')
-rw-r--r-- | lib/tty/key.c | 36 |
1 files changed, 14 insertions, 22 deletions
diff --git a/lib/tty/key.c b/lib/tty/key.c index 5671666..8e7770b 100644 --- a/lib/tty/key.c +++ b/lib/tty/key.c @@ -1,7 +1,7 @@ /* Keyboard support routines. - Copyright (C) 1994-2023 + Copyright (C) 1994-2024 Free Software Foundation, Inc. Written by: @@ -1173,28 +1173,19 @@ getch_with_timeout (unsigned int delay_us) /* --------------------------------------------------------------------------------------------- */ static void -learn_store_key (char *buffer, char **p, int c) +learn_store_key (GString * buffer, int c) { - if (*p - buffer > 253) - return; - if (c == ESC_CHAR) - { - *(*p)++ = '\\'; - *(*p)++ = 'e'; - } + g_string_append (buffer, "\\e"); else if (c < ' ') { - *(*p)++ = '^'; - *(*p)++ = c + 'a' - 1; + g_string_append_c (buffer, '^'); + g_string_append_c (buffer, c + 'a' - 1); } else if (c == '^') - { - *(*p)++ = '^'; - *(*p)++ = '^'; - } + g_string_append (buffer, "^^"); else - *(*p)++ = (char) c; + g_string_append_c (buffer, (char) c); } /* --------------------------------------------------------------------------------------------- */ @@ -2163,14 +2154,15 @@ learn_key (void) fd_set Read_FD_Set; gint64 end_time; int c; - char buffer[256]; - char *p = buffer; + GString *buffer; + + buffer = g_string_sized_new (16); tty_keypad (FALSE); /* disable interpreting keys by ncurses */ c = tty_lowlevel_getch (); while (c == -1) c = tty_lowlevel_getch (); /* Sanity check, should be unnecessary */ - learn_store_key (buffer, &p, c); + learn_store_key (buffer, c); end_time = g_get_monotonic_time () + LEARN_TIMEOUT * MC_USEC_PER_MSEC; @@ -2194,12 +2186,12 @@ learn_key (void) } if (c == -1) break; - learn_store_key (buffer, &p, c); + learn_store_key (buffer, c); } tty_keypad (TRUE); tty_nodelay (FALSE); - *p = '\0'; - return (buffer[0] != '\0' ? g_strdup (buffer) : NULL); + + return g_string_free (buffer, buffer->len == 0); #undef LEARN_TIMEOUT } |