[ros-diffs] [jmorlan] 40272: - Implement CMD /A and /U switches. - CLS: Fill console with current color rather than original; if standard output is not a console, print a form-feed character. - COLOR: If standard output is not a console, do nothing.

jmorlan at svn.reactos.org jmorlan at svn.reactos.org
Sat Mar 28 20:36:23 CET 2009


Author: jmorlan
Date: Sat Mar 28 22:36:22 2009
New Revision: 40272

URL: http://svn.reactos.org/svn/reactos?rev=40272&view=rev
Log:
- Implement CMD /A and /U switches.
- CLS: Fill console with current color rather than original; if standard output is not a console, print a form-feed character.
- COLOR: If standard output is not a console, do nothing.

Modified:
    trunk/reactos/base/shell/cmd/cls.c
    trunk/reactos/base/shell/cmd/cmd.c
    trunk/reactos/base/shell/cmd/color.c
    trunk/reactos/base/shell/cmd/console.c

Modified: trunk/reactos/base/shell/cmd/cls.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/base/shell/cmd/cls.c?rev=40272&r1=40271&r2=40272&view=diff
==============================================================================
--- trunk/reactos/base/shell/cmd/cls.c [iso-8859-1] (original)
+++ trunk/reactos/base/shell/cmd/cls.c [iso-8859-1] Sat Mar 28 22:36:22 2009
@@ -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 @@
 		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;
 }

Modified: trunk/reactos/base/shell/cmd/cmd.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/base/shell/cmd/cmd.c?rev=40272&r1=40271&r2=40272&view=diff
==============================================================================
--- trunk/reactos/base/shell/cmd/cmd.c [iso-8859-1] (original)
+++ trunk/reactos/base/shell/cmd/cmd.c [iso-8859-1] Sat Mar 28 22:36:22 2009
@@ -157,6 +157,7 @@
 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 NtReadVirtualMemoryProc       NtReadVirtualMemoryPtr = NULL;
 
 #ifdef INCLUDE_CMD_COLOR
-WORD wColor;              /* current color */
 WORD wDefColor;           /* default color */
 #endif
 
@@ -1818,6 +1818,10 @@
 				}
 				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 @@
 			{
 				/* 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 @@
 		ConErrFormatMessage(GetLastError());
 		return(1);
 	}
-	wColor = Info.wAttributes;
-	wDefColor = wColor;
+	wDefColor = Info.wAttributes;
 
 	InputCodePage= GetConsoleCP();
 	OutputCodePage = GetConsoleOutputCP();

Modified: trunk/reactos/base/shell/cmd/color.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/base/shell/cmd/color.c?rev=40272&r1=40271&r2=40272&view=diff
==============================================================================
--- trunk/reactos/base/shell/cmd/color.c [iso-8859-1] (original)
+++ trunk/reactos/base/shell/cmd/color.c [iso-8859-1] Sat Mar 28 22:36:22 2009
@@ -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 @@
  */
 INT CommandColor (LPTSTR rest)
 {
+	WORD wColor;
+
 	if (_tcsncmp (rest, _T("/?"), 2) == 0)
 	{
 		ConOutResPaging(TRUE,STRING_COLOR_HELP1);
@@ -84,6 +87,7 @@
 
 	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));

Modified: trunk/reactos/base/shell/cmd/console.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/base/shell/cmd/console.c?rev=40272&r1=40271&r2=40272&view=diff
==============================================================================
--- trunk/reactos/base/shell/cmd/console.c [iso-8859-1] (original)
+++ trunk/reactos/base/shell/cmd/console.c [iso-8859-1] Sat Mar 28 22:36:22 2009
@@ -128,61 +128,45 @@
 	SetConsoleMode (hFile, dwOldMode);
 }
 
-static VOID ConChar(TCHAR c, DWORD nStdHandle)
+static VOID ConWrite(TCHAR *str, DWORD len, DWORD nStdHandle)
 {
 	DWORD dwWritten;
-	CHAR cc;
+	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(hOutput, str, len * sizeof(WCHAR), &dwWritten, NULL);
+	}
+	else
+	{
 #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;
+		CHAR buffer[len * MB_LEN_MAX];
+		len = WideCharToMultiByte(OutputCodePage, 0, str, len, buffer, len * MB_LEN_MAX, NULL, NULL);
+		str = (PVOID)buffer;
 #endif
-	WriteFile (GetStdHandle (nStdHandle),
-	           &cc,
-	           1,
-	           &dwWritten,
-	           NULL);
+		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 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 @@
 
 VOID ConErrChar (TCHAR c)
 {
-	ConChar(c, STD_ERROR_HANDLE);
+	ConWrite(&c, 1, STD_ERROR_HANDLE);
 }
 
 



More information about the Ros-diffs mailing list