Fix to UNICODE issues.

This commit is contained in:
Ray Hayes 2016-11-04 10:09:08 -07:00
parent 37477b654c
commit 5c02287fd8
3 changed files with 43 additions and 43 deletions

View File

@ -315,6 +315,9 @@ void ConSetAttribute(int *iParam, int iParamCount)
case ANSI_ATTR_RESET: case ANSI_ATTR_RESET:
iAttr |= FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; iAttr |= FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
iAttr = iAttr & ~BACKGROUND_RED;
iAttr = iAttr & ~BACKGROUND_BLUE;
iAttr = iAttr & ~BACKGROUND_GREEN;
iAttr = iAttr & ~BACKGROUND_INTENSITY; iAttr = iAttr & ~BACKGROUND_INTENSITY;
iAttr = iAttr & ~FOREGROUND_INTENSITY; iAttr = iAttr & ~FOREGROUND_INTENSITY;
iAttr = iAttr & ~COMMON_LVB_UNDERSCORE; iAttr = iAttr & ~COMMON_LVB_UNDERSCORE;
@ -530,11 +533,23 @@ void ConFillToEndOfLine()
int ConWriteString(char* pszString, int cbString) int ConWriteString(char* pszString, int cbString)
{ {
DWORD Result = 0; DWORD Result = 0;
int needed = 0;
int cnt = 0;
wchar_t* utf16 = NULL;
if (hOutputConsole) if ((needed = MultiByteToWideChar(CP_UTF8, 0, pszString, cbString, NULL, 0)) == 0 ||
WriteConsole(hOutputConsole, pszString, cbString, &Result, 0); (utf16 = malloc(needed * sizeof(wchar_t))) == NULL ||
else (cnt = MultiByteToWideChar(CP_UTF8, 0, pszString, cbString, utf16, needed)) == 0) {
Result = (DWORD) printf(pszString); Result = (DWORD)printf(pszString);
} else {
if (hOutputConsole)
WriteConsoleW(hOutputConsole, utf16, cnt, &Result, 0);
else
Result = (DWORD)wprintf(utf16);
}
if(utf16)
free(utf16);
return cbString; return cbString;
} }

View File

@ -250,7 +250,7 @@ void SendHorizontalScroll(HANDLE hInput, int cells) {
WriteFile(hInput, formatted_output, out, &wr, NULL); WriteFile(hInput, formatted_output, out, &wr, NULL);
} }
void SendCharacter(HANDLE hInput, WORD attributes, char character) { void SendCharacter(HANDLE hInput, WORD attributes, wchar_t character) {
DWORD wr = 0; DWORD wr = 0;
DWORD out = 0; DWORD out = 0;
@ -266,6 +266,9 @@ void SendCharacter(HANDLE hInput, WORD attributes, char character) {
PSTR Next; PSTR Next;
size_t SizeLeft; size_t SizeLeft;
if (!character)
return;
Next = formatted_output; Next = formatted_output;
SizeLeft = sizeof formatted_output; SizeLeft = sizeof formatted_output;
@ -339,52 +342,33 @@ void SendCharacter(HANDLE hInput, WORD attributes, char character) {
if (bUseAnsiEmulation && attributes != pattributes) if (bUseAnsiEmulation && attributes != pattributes)
WriteFile(hInput, formatted_output, (Next - formatted_output), &wr, NULL); WriteFile(hInput, formatted_output, (Next - formatted_output), &wr, NULL);
WriteFile(hInput, &character, 1, &wr, NULL); // East asian languages have 2 bytes for each character, only use the first
if (!(attributes & COMMON_LVB_TRAILING_BYTE))
{
int nSize = WideCharToMultiByte(CP_UTF8,
0,
&character,
1,
Next,
10,
NULL,
NULL);
if(nSize > 0)
WriteFile(hInput, Next, nSize, &wr, NULL);
}
pattributes = attributes; pattributes = attributes;
} }
void SendBuffer(HANDLE hInput, CHAR_INFO *buffer, DWORD bufferSize) { void SendBuffer(HANDLE hInput, CHAR_INFO *buffer, DWORD bufferSize) {
DWORD wr = 0;
DWORD out = 0;
DWORD current = 0;
char* formatted_output = NULL;
USHORT Color = 0;
ULONG Status = 0;
if (bufferSize <= 0) if (bufferSize <= 0)
return; return;
formatted_output = (char *)malloc(bufferSize);
PSTR Next;
Next = formatted_output;
for (DWORD i = 0; i < bufferSize; i++) for (DWORD i = 0; i < bufferSize; i++)
{ {
// East asian languages have 2 bytes for each character, only use the first SendCharacter(hInput, buffer[i].Attributes, buffer[i].Char.UnicodeChar);
if (!(buffer[i].Attributes & COMMON_LVB_TRAILING_BYTE))
{
WideCharToMultiByte(cp,
0,
&buffer[i].Char.UnicodeChar,
1,
Next,
1,
NULL,
NULL);
SendCharacter(hInput, buffer[i].Attributes, *Next);
Next++;
}
}
if (formatted_output) {
free(formatted_output);
} }
} }
@ -465,7 +449,8 @@ DWORD WINAPI MonitorChild(_In_ LPVOID lpParameter) {
DWORD ProcessEvent(void *p) { DWORD ProcessEvent(void *p) {
char f[255]; char f[255];
char chUpdate; wchar_t chUpdate;
WORD wAttributes; WORD wAttributes;
WORD wX; WORD wX;
WORD wY; WORD wY;

View File

@ -166,7 +166,7 @@ int ReadConsoleForTermEmul(HANDLE hInput, char *destin, int destinlen)
if (InputRecord.Event.KeyEvent.bKeyDown) if (InputRecord.Event.KeyEvent.bKeyDown)
{ {
int n = WideCharToMultiByte( int n = WideCharToMultiByte(
GetConsoleCP(), CP_UTF8,
0, 0,
&(InputRecord.Event.KeyEvent.uChar.UnicodeChar), &(InputRecord.Event.KeyEvent.uChar.UnicodeChar),
1, 1,