Skip to content

Commit

Permalink
MDEV-27090: Windows client - ReadConsoleA does not work correctly wit…
Browse files Browse the repository at this point in the history
…h UTF8 codepage

Corresponding Windows bug  microsoft/terminal#4551

Use ReadConsoleW instead and convert to console's input codepage, to
workaround.

Also, disable VT sequences in the console output, as we do not knows what
type of data comes with SELECT, we do not want VT escapes there.

Remove my_cgets()
  • Loading branch information
vaintroub committed Nov 26, 2021
1 parent 1411d5f commit d8a13e2
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 254 deletions.
120 changes: 97 additions & 23 deletions client/mysql.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@ extern "C" {
#endif /* defined(HAVE_CURSES_H) && defined(HAVE_TERM_H) */

#undef bcmp // Fix problem with new readline
#if defined(_WIN32)
#include <conio.h>
#else
#if !defined(_WIN32)
# ifdef __APPLE__
# include <editline/readline.h>
# else
Expand All @@ -104,6 +102,101 @@ extern "C" {
#endif
}


#if defined(_WIN32)
/*
Set console mode for the whole duration of the client session.
We need for input
- line input (i.e read lines from console)
- echo typed characters
- "cooked" mode, i.e we do not want to handle all keystrokes,
like DEL etc ourselves, yet. We might want handle keystrokes
in the future, to implement tab completion, and better
(multiline) history.
Disable VT escapes for the output.We do not know what kind of escapes SELECT would return.
*/
struct Console_mode
{
HANDLE in= GetStdHandle(STD_INPUT_HANDLE);
HANDLE out= GetStdHandle(STD_OUTPUT_HANDLE);
DWORD mode_in=0;
DWORD mode_out=0;

enum {STDIN_CHANGED = 1, STDOUT_CHANGED = 2};
int changes=0;

Console_mode()
{
if (in && in != INVALID_HANDLE_VALUE && GetConsoleMode(in, &mode_in))
{
SetConsoleMode(in, ENABLE_ECHO_INPUT|ENABLE_LINE_INPUT|ENABLE_PROCESSED_INPUT);
changes |= STDIN_CHANGED;
}

if (out && out != INVALID_HANDLE_VALUE && GetConsoleMode(out, &mode_out))
{
#ifdef ENABLE_VIRTUAL_TERMINAL_INPUT
SetConsoleMode(out, mode_out & ~ENABLE_VIRTUAL_TERMINAL_INPUT);
changes |= STDOUT_CHANGED;
#endif
}
}

~Console_mode()
{
if (changes & STDIN_CHANGED)
SetConsoleMode(in, mode_in);

if(changes & STDOUT_CHANGED)
SetConsoleMode(out, mode_out);
}
};

static Console_mode my_conmode;

#define MAX_CGETS_LINE_LEN 65535
/** Read line from console in ANSI codepage, chomp EOL*/
static char *win_readline()
{
static wchar_t wstrbuf[MAX_CGETS_LINE_LEN];
static char strbuf[MAX_CGETS_LINE_LEN*2];

DWORD nchars= 0;
SetLastError(0);
if (!ReadConsoleW(GetStdHandle(STD_INPUT_HANDLE), wstrbuf, MAX_CGETS_LINE_LEN-1,
&nchars, NULL))
goto err;
if (nchars == 0 && GetLastError() == ERROR_OPERATION_ABORTED)
goto err;

while (nchars > 0)
{
if (wstrbuf[nchars - 1] != '\n' && wstrbuf[nchars - 1] != '\r')
break;
wstrbuf[--nchars]= 0;
}

wstrbuf[nchars]= 0;
if (nchars > 0)
{
int len = WideCharToMultiByte(GetConsoleCP(), 0, wstrbuf, nchars + 1,
strbuf, sizeof(strbuf),NULL, NULL);
if (len < 1)
strbuf[0]= 0;
}
else
{
strbuf[0]= 0;
}
return strbuf;
err:
return NULL;
}
#endif


#ifdef HAVE_VIDATTR
static int have_curses= 0;
static void my_vidattr(chtype attrs)
Expand Down Expand Up @@ -2115,26 +2208,7 @@ static int read_and_execute(bool interactive)

#if defined(_WIN32)
tee_fputs(prompt, stdout);
if (!tmpbuf.is_alloced())
tmpbuf.alloc(65535);
tmpbuf.length(0);
buffer.length(0);
size_t clen;
do
{
line= my_cgets((char*)tmpbuf.ptr(), tmpbuf.alloced_length()-1, &clen);
buffer.append(line, clen);
/*
if we got buffer fully filled than there is a chance that
something else is still in console input buffer
*/
} while (tmpbuf.alloced_length() <= clen);
/*
An empty line is returned from my_cgets when there's error reading :
Ctrl-c for example
*/
if (line)
line= buffer.c_ptr();
line= win_readline();
#else
if (opt_outfile)
fputs(prompt, OUTFILE);
Expand Down
7 changes: 0 additions & 7 deletions include/my_sys.h
Original file line number Diff line number Diff line change
Expand Up @@ -1097,13 +1097,6 @@ extern void thd_increment_bytes_sent(void *thd, size_t length);
extern void thd_increment_bytes_received(void *thd, size_t length);
extern void thd_increment_net_big_packet_count(void *thd, size_t length);

#ifdef _WIN32

/* implemented in my_conio.c */
char* my_cgets(char *string, size_t clen, size_t* plen);

#endif

#include <mysql/psi/psi.h>

#ifdef HAVE_PSI_INTERFACE
Expand Down
1 change: 0 additions & 1 deletion mysys/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ IF (WIN32)
my_wincond.c
my_winerr.c
my_winfile.c
my_conio.c
my_minidump.cc
my_win_popen.cc)
ENDIF()
Expand Down
223 changes: 0 additions & 223 deletions mysys/my_conio.c

This file was deleted.

0 comments on commit d8a13e2

Please sign in to comment.