17 #include "mysys_priv.h"
30 #define MAX_CONSOLE_LINE_SIZE 65535
36 #define MAX_NUM_OF_CHARS_TO_READ 24530
48 my_win_is_console(FILE *
file)
51 if (GetConsoleMode((HANDLE) _get_osfhandle(_fileno(file)), &mode))
73 my_win_console_readline(
const CHARSET_INFO *cs,
char *mbbuf,
size_t mbbufsize)
76 static wchar_t u16buf[MAX_CONSOLE_LINE_SIZE + 1];
82 HANDLE console= GetStdHandle(STD_INPUT_HANDLE);
84 DBUG_ASSERT(mbbufsize > 0);
85 GetConsoleMode(console, &console_mode);
86 SetConsoleMode(console, ENABLE_LINE_INPUT |
87 ENABLE_PROCESSED_INPUT | ENABLE_ECHO_INPUT);
89 if (!ReadConsoleW(console, u16buf, MAX_NUM_OF_CHARS_TO_READ, &nchars, NULL))
91 SetConsoleMode(console, console_mode);
96 if (nchars >= 2 && u16buf[nchars - 2] == L
'\r')
98 else if ((nchars == MAX_NUM_OF_CHARS_TO_READ) &&
99 (u16buf[nchars - 1] == L
'\r'))
102 else if ((nchars == 1) && (u16buf[0] == L
'\n'))
106 SetConsoleMode(console, console_mode);
110 mblen= my_convert(mbbuf, mbbufsize - 1, cs,
111 (
const char *) u16buf, nchars *
sizeof(
wchar_t),
112 &my_charset_utf16le_bin, &dummy_errors);
114 DBUG_ASSERT(mblen < mbbufsize);
133 my_mbstou16s(
const CHARSET_INFO *cs,
const uchar * from,
size_t from_length,
134 wchar_t *
to,
size_t to_chars)
137 const uchar *from_end= from + from_length;
138 wchar_t *to_orig=
to, *to_end= to + to_chars;
139 my_charset_conv_mb_wc mb_wc= cs->cset->mb_wc;
140 my_charset_conv_wc_mb wc_mb= to_cs->cset->wc_mb;
141 while (from < from_end)
145 if ((cnvres= (*mb_wc)(cs, &wc, from, from_end)) > 0)
151 else if (cnvres == MY_CS_ILSEQ)
153 wc= (my_wc_t) (uchar) *from;
156 else if (cnvres > MY_CS_TOOSMALL)
167 wc= (my_wc_t) (uchar) *from;
171 if ((cnvres= (*wc_mb)(to_cs, wc, (uchar *) to, (uchar *) to_end)) > 0)
174 DBUG_ASSERT((cnvres %
sizeof(
wchar_t)) == 0);
176 to+= cnvres /
sizeof(wchar_t);
178 else if (cnvres == MY_CS_ILUNI && wc !=
'?')
203 my_win_console_write(
const CHARSET_INFO *cs,
const char *data,
size_t datalen)
205 static wchar_t u16buf[MAX_CONSOLE_LINE_SIZE + 1];
206 size_t nchars= my_mbstou16s(cs, (
const uchar *) data, datalen,
207 u16buf,
sizeof(u16buf));
209 WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE),
210 u16buf, (DWORD) nchars, &nwritten, NULL);
227 my_win_console_write(cs, &ch, 1);
238 my_win_console_fputs(
const CHARSET_INFO *cs,
const char *data)
240 my_win_console_write(cs, data, strlen(data));
248 my_win_console_vfprintf(
const CHARSET_INFO *cs,
const char *
fmt, va_list args)
250 static char buff[MAX_CONSOLE_LINE_SIZE + 1];
251 size_t len= vsnprintf(buff,
sizeof(buff) - 1, fmt, args);
252 my_win_console_write(cs, buff, len);
256 #include <shellapi.h>
268 my_win_translate_command_line_args(
const CHARSET_INFO *cs,
int *argc,
char ***argv)
272 wchar_t *command_line= GetCommandLineW();
273 wchar_t **wargs= CommandLineToArgvW(command_line, &ac);
274 size_t nbytes= (ac + 1) *
sizeof(
char *);
277 av= (
char **) my_once_alloc(nbytes, MYF(MY_ZEROFILL));
279 for(i= 0; i < ac; i++)
282 size_t arg_len= wcslen(wargs[i]);
283 size_t len, alloced_len= arg_len * cs->mbmaxlen + 1;
284 av[
i]= (
char *) my_once_alloc(alloced_len, MYF(0));
285 len= my_convert(av[i], alloced_len, cs,
286 (
const char *) wargs[i], arg_len *
sizeof(
wchar_t),
287 &my_charset_utf16le_bin, &dummy_errors);
288 DBUG_ASSERT(len < alloced_len);
294 LocalFree((HLOCAL) wargs);