From b80c77e931cf89e8e1428715595f28d1e20ea8b7 Mon Sep 17 00:00:00 2001 From: pancake Date: Mon, 13 Sep 2021 08:09:09 -0700 Subject: [PATCH] Fix arrow keys regression on windows prompt ##cons --- libr/cons/dietline.c | 54 ++++++++++++++++++++++++------------------- libr/cons/input.c | 2 +- libr/include/r_cons.h | 2 +- 3 files changed, 32 insertions(+), 26 deletions(-) diff --git a/libr/cons/dietline.c b/libr/cons/dietline.c index 14fc82987f..05323d34a1 100644 --- a/libr/cons/dietline.c +++ b/libr/cons/dietline.c @@ -261,14 +261,14 @@ static int r_line_readchar_utf8(ut8 *s, int slen) { #if __WINDOWS__ static int r_line_readchar_win(ut8 *s, int slen) { // this function handle the input in console mode + r_sys_backtrace(); INPUT_RECORD irInBuf = { { 0 } }; - BOOL ret, bCtrl = FALSE; + BOOL ret; DWORD mode, out; char buf[5] = {0}; - HANDLE h; void *bed; - h = GetStdHandle (STD_INPUT_HANDLE); + HANDLE h = GetStdHandle (STD_INPUT_HANDLE); DWORD new_mode = I.vtmode == 2 ? ENABLE_VIRTUAL_TERMINAL_INPUT : 0; GetConsoleMode (h, &mode); SetConsoleMode (h, new_mode); @@ -298,18 +298,16 @@ do_it_again: if (irInBuf.Event.KeyEvent.bKeyDown) { if (irInBuf.Event.KeyEvent.uChar.UnicodeChar) { char *tmp = r_sys_conv_win_to_utf8_l ((PTCHAR)&irInBuf.Event.KeyEvent.uChar, 1); - if (!tmp) { - return 0; + if (tmp) { + r_str_ncpy (buf, tmp, sizeof (buf)); + free (tmp); } - strncpy_s (buf, sizeof (buf), tmp, strlen (tmp)); - free (tmp); } else { int idx = 0; buf[idx++] = 27; buf[idx++] = '['; // Simulate escaping - bCtrl = irInBuf.Event.KeyEvent.dwControlKeyState & 8; - if (bCtrl) { - buf[idx++] = 0x31; + if (irInBuf.Event.KeyEvent.dwControlKeyState & 8) { + buf[idx++] = '1'; // control key } switch (irInBuf.Event.KeyEvent.wVirtualKeyCode) { case VK_UP: buf[idx++] = 'A'; break; @@ -329,7 +327,7 @@ do_it_again: if (!buf[0]) { goto do_it_again; } - strncpy_s ((char *)s, slen, buf, sizeof (buf)); + r_str_ncpy (s, buf, slen); SetConsoleMode (h, mode); return strlen ((char *)s); } @@ -535,25 +533,23 @@ R_API int r_line_hist_load(const char *file) { return true; } -R_API int r_line_hist_save(const char *file) { - FILE *fd; - int i, ret = false; - if (!file || !*file) { - return false; - } +R_API bool r_line_hist_save(const char *file) { + r_return_val_if_fail (file && *file, false); + int i; + bool ret = false; char *p, *path = r_str_home (file); - if (path != NULL) { + if (path) { p = (char *) r_str_lastbut (path, R_SYS_DIR[0], NULL); // TODO: use fs if (p) { *p = 0; if (!r_sys_mkdirp (path)) { - eprintf ("could not save history into %s\n", path); + eprintf ("Could not save history into %s\n", path); goto end; } *p = R_SYS_DIR[0]; } - fd = r_sandbox_fopen (path, "w"); - if (fd != NULL) { + FILE *fd = r_sandbox_fopen (path, "w"); + if (fd) { if (I.history.data) { for (i = 0; i < I.history.index; i++) { fputs (I.history.data[i], fd); @@ -1630,16 +1626,24 @@ R_API const char *r_line_readline_cb(RLineReadCallback cb, void *user) { break; case 27: // esc-5b-41-00-00 alt/meta key #if __WINDOWS__ + // always skip escape + memmove (buf, buf + 1, strlen (buf)); +#if 0 if (I.vtmode != 2) { - memmove (buf, buf + 1, strlen (buf)); + if (buf[1] == '[') { + memmove (buf, buf + 2, strlen (buf)); + } else { + memmove (buf, buf + 1, strlen (buf)); + } if (!buf[0]) { buf[0] = -1; } } else { -#endif buf[0] = r_cons_readchar_timeout (50); -#if __WINDOWS__ } +#endif +#else + buf[0] = r_cons_readchar_timeout (50); #endif switch (buf[0]) { case 127: // alt+bkspace @@ -1694,6 +1698,7 @@ R_API const char *r_line_readline_cb(RLineReadCallback cb, void *user) { } break; default: +#if !__WINDOWS__ if (I.vtmode == 2) { buf[1] = r_cons_readchar_timeout (50); if (buf[1] == -1) { // alt+e @@ -1704,6 +1709,7 @@ R_API const char *r_line_readline_cb(RLineReadCallback cb, void *user) { } else { buf[1] = r_cons_readchar_timeout (50); } +#endif if (buf[0] == '[') { // [ switch (buf[1]) { case '3': // supr or mouse click diff --git a/libr/cons/input.c b/libr/cons/input.c index df057c9e05..5543cb776d 100644 --- a/libr/cons/input.c +++ b/libr/cons/input.c @@ -1,4 +1,4 @@ -/* radare - LGPL - Copyright 2009-2019 - pancake */ +/* radare - LGPL - Copyright 2009-2021 - pancake */ #include #include diff --git a/libr/include/r_cons.h b/libr/include/r_cons.h index 7b277e81e6..85635bfec6 100644 --- a/libr/include/r_cons.h +++ b/libr/include/r_cons.h @@ -1099,7 +1099,7 @@ R_API const char *r_line_readline_cb(RLineReadCallback cb, void *user); R_API int r_line_hist_load(const char *file); R_API int r_line_hist_add(const char *line); -R_API int r_line_hist_save(const char *file); +R_API bool r_line_hist_save(const char *file); R_API int r_line_hist_label(const char *label, void (*cb)(const char*)); R_API void r_line_label_show(void); R_API int r_line_hist_list(void);