Skip to content

Commit

Permalink
MDEV-27090 Windows client - ReadConsoleA does not work correctly with…
Browse files Browse the repository at this point in the history
… 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 Jan 18, 2022
1 parent 55a5443 commit 2e174dc
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 264 deletions.
128 changes: 95 additions & 33 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,98 @@ extern "C" {
#define USE_POPEN
}

static CHARSET_INFO *charset_info= &my_charset_latin1;

#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, chomp EOL*/
static char *win_readline()
{
static wchar_t wstrbuf[MAX_CGETS_LINE_LEN];
static char strbuf[MAX_CGETS_LINE_LEN * 4];

DWORD nchars= 0;
uint len= 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;

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

if (nchars > 0)
{
uint errors;
len= my_convert(strbuf, sizeof(strbuf), charset_info,
(const char *) wstrbuf, nchars * sizeof(wchar_t),
&my_charset_utf16le_bin, &errors);
}
strbuf[len]= 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 @@ -208,7 +298,6 @@ unsigned short terminal_width= 80;

static uint opt_protocol=0;
static const char *opt_protocol_type= "";
static CHARSET_INFO *charset_info= &my_charset_latin1;

static uint protocol_to_force= MYSQL_PROTOCOL_DEFAULT;

Expand Down Expand Up @@ -2033,11 +2122,6 @@ static int get_options(int argc, char **argv)

static int read_and_execute(bool interactive)
{
#if defined(_WIN32)
String tmpbuf;
String buffer;
#endif

char *line= NULL;
char in_string=0;
ulong line_number=0;
Expand Down Expand Up @@ -2115,26 +2199,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 Expand Up @@ -2201,10 +2266,7 @@ static int read_and_execute(bool interactive)
}
}

#if defined(_WIN32)
buffer.free();
tmpbuf.free();
#else
#if !defined(_WIN32)
if (interactive)
/*
free the last entered line.
Expand Down
7 changes: 0 additions & 7 deletions include/my_sys.h
Original file line number Diff line number Diff line change
Expand Up @@ -1098,13 +1098,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
Loading

0 comments on commit 2e174dc

Please sign in to comment.