diff --git a/reactos/base/shell/cmd/cls.c b/reactos/base/shell/cmd/cls.c index b821ad2a845..2cb13d6ac31 100644 --- a/reactos/base/shell/cmd/cls.c +++ b/reactos/base/shell/cmd/cls.c @@ -32,6 +32,7 @@ INT cmd_cls (LPTSTR param) { + HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_SCREEN_BUFFER_INFO csbi; COORD coPos; DWORD dwWritten; @@ -42,17 +43,22 @@ INT cmd_cls (LPTSTR param) return 0; } - GetConsoleScreenBufferInfo(hConsole, &csbi); - - coPos.X = 0; - coPos.Y = 0; - FillConsoleOutputAttribute(hConsole, wColor, - csbi.dwSize.X * csbi.dwSize.Y, - coPos, &dwWritten); - FillConsoleOutputCharacter(hConsole, _T(' '), - csbi.dwSize.X * csbi.dwSize.Y, - coPos, &dwWritten); - SetConsoleCursorPosition(hConsole, coPos); + if (GetConsoleScreenBufferInfo(hOutput, &csbi)) + { + coPos.X = 0; + coPos.Y = 0; + FillConsoleOutputAttribute(hOutput, csbi.wAttributes, + csbi.dwSize.X * csbi.dwSize.Y, + coPos, &dwWritten); + FillConsoleOutputCharacter(hOutput, _T(' '), + csbi.dwSize.X * csbi.dwSize.Y, + coPos, &dwWritten); + SetConsoleCursorPosition(hOutput, coPos); + } + else + { + ConOutChar(_T('\f')); + } return 0; } diff --git a/reactos/base/shell/cmd/cmd.c b/reactos/base/shell/cmd/cmd.c index d9445a42d86..6ff028124e3 100644 --- a/reactos/base/shell/cmd/cmd.c +++ b/reactos/base/shell/cmd/cmd.c @@ -157,6 +157,7 @@ BOOL bCtrlBreak = FALSE; /* Ctrl-Break or Ctrl-C hit */ BOOL bIgnoreEcho = FALSE; /* Set this to TRUE to prevent a newline, when executing a command */ INT nErrorLevel = 0; /* Errorlevel of last launched external program */ BOOL bChildProcessRunning = FALSE; +BOOL bUnicodeOutput = FALSE; BOOL bDisableBatchEcho = FALSE; BOOL bDelayedExpansion = FALSE; DWORD dwChildProcessId = 0; @@ -171,7 +172,6 @@ static NtQueryInformationProcessProc NtQueryInformationProcessPtr = NULL; static NtReadVirtualMemoryProc NtReadVirtualMemoryPtr = NULL; #ifdef INCLUDE_CMD_COLOR -WORD wColor; /* current color */ WORD wDefColor; /* default color */ #endif @@ -1818,6 +1818,10 @@ Initialize() } bCanExit = FALSE; } + else if (option == _T('A')) + { + bUnicodeOutput = FALSE; + } else if (option == _T('C') || option == _T('K') || option == _T('R')) { /* Remainder of command line is a command to be run */ @@ -1840,10 +1844,13 @@ Initialize() { /* process /t (color) argument */ wDefColor = (WORD)_tcstoul(&ptr[3], &ptr, 16); - wColor = wDefColor; - SetScreenColor (wColor, TRUE); + SetScreenColor(wDefColor, TRUE); } #endif + else if (option == _T('U')) + { + bUnicodeOutput = TRUE; + } else if (option == _T('V')) { bDelayedExpansion = _tcsnicmp(&ptr[2], _T(":OFF"), 4); @@ -1956,8 +1963,7 @@ int cmd_main (int argc, const TCHAR *argv[]) ConErrFormatMessage(GetLastError()); return(1); } - wColor = Info.wAttributes; - wDefColor = wColor; + wDefColor = Info.wAttributes; InputCodePage= GetConsoleCP(); OutputCodePage = GetConsoleOutputCP(); diff --git a/reactos/base/shell/cmd/color.c b/reactos/base/shell/cmd/color.c index 7b6cea9e250..b30fafc57f0 100644 --- a/reactos/base/shell/cmd/color.c +++ b/reactos/base/shell/cmd/color.c @@ -30,6 +30,7 @@ VOID SetScreenColor (WORD wColor, BOOL bNoFill) { + HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); DWORD dwWritten; CONSOLE_SCREEN_BUFFER_INFO csbi; COORD coPos; @@ -65,6 +66,8 @@ VOID SetScreenColor (WORD wColor, BOOL bNoFill) */ INT CommandColor (LPTSTR rest) { + WORD wColor; + if (_tcsncmp (rest, _T("/?"), 2) == 0) { ConOutResPaging(TRUE,STRING_COLOR_HELP1); @@ -84,6 +87,7 @@ INT CommandColor (LPTSTR rest) if ( _tcslen(&rest[0])==1) { + HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); if ( (_tcscmp(&rest[0], _T("0")) >=0 ) && (_tcscmp(&rest[0], _T("9")) <=0 ) ) { SetConsoleTextAttribute (hConsole, (WORD)_ttoi(rest)); diff --git a/reactos/base/shell/cmd/console.c b/reactos/base/shell/cmd/console.c index a488807a323..bcaf388eb86 100644 --- a/reactos/base/shell/cmd/console.c +++ b/reactos/base/shell/cmd/console.c @@ -128,61 +128,45 @@ VOID ConInString (LPTSTR lpInput, DWORD dwLength) SetConsoleMode (hFile, dwOldMode); } -static VOID ConChar(TCHAR c, DWORD nStdHandle) +static VOID ConWrite(TCHAR *str, DWORD len, DWORD nStdHandle) { DWORD dwWritten; - CHAR cc; -#ifdef _UNICODE - CHAR as[2]; - WCHAR ws[2]; - ws[0] = c; - ws[1] = 0; - WideCharToMultiByte( OutputCodePage, 0, ws, 2, as, 2, NULL, NULL); - cc = as[0]; -#else - cc = c; + HANDLE hOutput = GetStdHandle(nStdHandle); + + if (WriteConsole(hOutput, str, len, &dwWritten, NULL)) + return; + + /* We're writing to a file or pipe instead of the console. Convert the + * string from TCHARs to the desired output format, if the two differ */ + if (bUnicodeOutput) + { +#ifndef _UNICODE + WCHAR buffer[len]; + len = MultiByteToWideChar(OutputCodePage, 0, str, len, buffer, len, NULL, NULL); + str = (PVOID)buffer; #endif - WriteFile (GetStdHandle (nStdHandle), - &cc, - 1, - &dwWritten, - NULL); + WriteFile(hOutput, str, len * sizeof(WCHAR), &dwWritten, NULL); + } + else + { +#ifdef _UNICODE + CHAR buffer[len * MB_LEN_MAX]; + len = WideCharToMultiByte(OutputCodePage, 0, str, len, buffer, len * MB_LEN_MAX, NULL, NULL); + str = (PVOID)buffer; +#endif + WriteFile(hOutput, str, len, &dwWritten, NULL); + } } VOID ConOutChar (TCHAR c) { - ConChar(c, STD_OUTPUT_HANDLE); + ConWrite(&c, 1, STD_OUTPUT_HANDLE); } VOID ConPuts(LPTSTR szText, DWORD nStdHandle) { - DWORD dwWritten; - HANDLE hStdHandle; - PCHAR pBuf; - INT len; - - len = _tcslen(szText); -#ifdef _UNICODE - pBuf = cmd_alloc(len * 2 + 1); - len = WideCharToMultiByte(OutputCodePage, 0, szText, len + 1, pBuf, len * 2 + 1, NULL, NULL) - 1; -#else - pBuf = szText; -#endif - hStdHandle = GetStdHandle(nStdHandle); - - WriteFile (hStdHandle, - pBuf, - len, - &dwWritten, - NULL); - WriteFile (hStdHandle, - _T("\n"), - 1, - &dwWritten, - NULL); -#ifdef _UNICODE - cmd_free(pBuf); -#endif + ConWrite(szText, _tcslen(szText), nStdHandle); + ConWrite(_T("\n"), 1, nStdHandle); } VOID ConOutResPaging(BOOL NewPage, UINT resID) @@ -208,28 +192,8 @@ VOID ConOutPuts (LPTSTR szText) VOID ConPrintf(LPTSTR szFormat, va_list arg_ptr, DWORD nStdHandle) { - INT len; - PCHAR pBuf; TCHAR szOut[OUTPUT_BUFFER_SIZE]; - DWORD dwWritten; - - len = _vstprintf(szOut, szFormat, arg_ptr); -#ifdef _UNICODE - pBuf = cmd_alloc(len * 2 + 1); - len = WideCharToMultiByte(OutputCodePage, 0, szOut, len + 1, pBuf, len * 2 + 1, NULL, NULL) - 1; -#else - pBuf = szOut; -#endif - - WriteFile (GetStdHandle (nStdHandle), - pBuf, - len, - &dwWritten, - NULL); - -#ifdef _UNICODE - cmd_free(pBuf); -#endif + ConWrite(szOut, _vstprintf(szOut, szFormat, arg_ptr), nStdHandle); } INT ConPrintfPaging(BOOL NewPage, LPTSTR szFormat, va_list arg_ptr, DWORD nStdHandle) @@ -398,7 +362,7 @@ INT ConOutPrintfPaging (BOOL NewPage, LPTSTR szFormat, ...) VOID ConErrChar (TCHAR c) { - ConChar(c, STD_ERROR_HANDLE); + ConWrite(&c, 1, STD_ERROR_HANDLE); }