Skip to content

Commit 5c02287

Browse files
committed
Fix to UNICODE issues.
1 parent 37477b6 commit 5c02287

File tree

3 files changed

+43
-43
lines changed

3 files changed

+43
-43
lines changed

contrib/win32/win32compat/console.c

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,9 @@ void ConSetAttribute(int *iParam, int iParamCount)
315315
case ANSI_ATTR_RESET:
316316
iAttr |= FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
317317

318+
iAttr = iAttr & ~BACKGROUND_RED;
319+
iAttr = iAttr & ~BACKGROUND_BLUE;
320+
iAttr = iAttr & ~BACKGROUND_GREEN;
318321
iAttr = iAttr & ~BACKGROUND_INTENSITY;
319322
iAttr = iAttr & ~FOREGROUND_INTENSITY;
320323
iAttr = iAttr & ~COMMON_LVB_UNDERSCORE;
@@ -530,12 +533,24 @@ void ConFillToEndOfLine()
530533
int ConWriteString(char* pszString, int cbString)
531534
{
532535
DWORD Result = 0;
536+
int needed = 0;
537+
int cnt = 0;
538+
wchar_t* utf16 = NULL;
539+
540+
if ((needed = MultiByteToWideChar(CP_UTF8, 0, pszString, cbString, NULL, 0)) == 0 ||
541+
(utf16 = malloc(needed * sizeof(wchar_t))) == NULL ||
542+
(cnt = MultiByteToWideChar(CP_UTF8, 0, pszString, cbString, utf16, needed)) == 0) {
543+
Result = (DWORD)printf(pszString);
544+
} else {
545+
if (hOutputConsole)
546+
WriteConsoleW(hOutputConsole, utf16, cnt, &Result, 0);
547+
else
548+
Result = (DWORD)wprintf(utf16);
549+
}
550+
551+
if(utf16)
552+
free(utf16);
533553

534-
if (hOutputConsole)
535-
WriteConsole(hOutputConsole, pszString, cbString, &Result, 0);
536-
else
537-
Result = (DWORD) printf(pszString);
538-
539554
return cbString;
540555
}
541556

contrib/win32/win32compat/shell-host.c

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ void SendHorizontalScroll(HANDLE hInput, int cells) {
250250
WriteFile(hInput, formatted_output, out, &wr, NULL);
251251
}
252252

253-
void SendCharacter(HANDLE hInput, WORD attributes, char character) {
253+
void SendCharacter(HANDLE hInput, WORD attributes, wchar_t character) {
254254

255255
DWORD wr = 0;
256256
DWORD out = 0;
@@ -266,6 +266,9 @@ void SendCharacter(HANDLE hInput, WORD attributes, char character) {
266266
PSTR Next;
267267
size_t SizeLeft;
268268

269+
if (!character)
270+
return;
271+
269272
Next = formatted_output;
270273
SizeLeft = sizeof formatted_output;
271274

@@ -339,52 +342,33 @@ void SendCharacter(HANDLE hInput, WORD attributes, char character) {
339342
if (bUseAnsiEmulation && attributes != pattributes)
340343
WriteFile(hInput, formatted_output, (Next - formatted_output), &wr, NULL);
341344

342-
WriteFile(hInput, &character, 1, &wr, NULL);
345+
// East asian languages have 2 bytes for each character, only use the first
346+
if (!(attributes & COMMON_LVB_TRAILING_BYTE))
347+
{
348+
int nSize = WideCharToMultiByte(CP_UTF8,
349+
0,
350+
&character,
351+
1,
352+
Next,
353+
10,
354+
NULL,
355+
NULL);
356+
357+
if(nSize > 0)
358+
WriteFile(hInput, Next, nSize, &wr, NULL);
359+
}
343360

344361
pattributes = attributes;
345362
}
346363

347364
void SendBuffer(HANDLE hInput, CHAR_INFO *buffer, DWORD bufferSize) {
348-
349-
DWORD wr = 0;
350-
DWORD out = 0;
351-
DWORD current = 0;
352-
353-
char* formatted_output = NULL;
354-
355-
USHORT Color = 0;
356-
ULONG Status = 0;
357365

358366
if (bufferSize <= 0)
359367
return;
360368

361-
formatted_output = (char *)malloc(bufferSize);
362-
363-
PSTR Next;
364-
Next = formatted_output;
365-
366369
for (DWORD i = 0; i < bufferSize; i++)
367370
{
368-
// East asian languages have 2 bytes for each character, only use the first
369-
if (!(buffer[i].Attributes & COMMON_LVB_TRAILING_BYTE))
370-
{
371-
WideCharToMultiByte(cp,
372-
0,
373-
&buffer[i].Char.UnicodeChar,
374-
1,
375-
Next,
376-
1,
377-
NULL,
378-
NULL);
379-
380-
SendCharacter(hInput, buffer[i].Attributes, *Next);
381-
382-
Next++;
383-
}
384-
}
385-
386-
if (formatted_output) {
387-
free(formatted_output);
371+
SendCharacter(hInput, buffer[i].Attributes, buffer[i].Char.UnicodeChar);
388372
}
389373
}
390374

@@ -465,7 +449,8 @@ DWORD WINAPI MonitorChild(_In_ LPVOID lpParameter) {
465449
DWORD ProcessEvent(void *p) {
466450

467451
char f[255];
468-
char chUpdate;
452+
wchar_t chUpdate;
453+
469454
WORD wAttributes;
470455
WORD wX;
471456
WORD wY;

contrib/win32/win32compat/tncon.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ int ReadConsoleForTermEmul(HANDLE hInput, char *destin, int destinlen)
166166
if (InputRecord.Event.KeyEvent.bKeyDown)
167167
{
168168
int n = WideCharToMultiByte(
169-
GetConsoleCP(),
169+
CP_UTF8,
170170
0,
171171
&(InputRecord.Event.KeyEvent.uChar.UnicodeChar),
172172
1,

0 commit comments

Comments
 (0)