Remove extra \r put by hand in some console output functions, so that we use there only \n.
But modify the ConWrite function so that, if we redirect output to something else than a console
(e.g. redirect to a file or to a serial console via ... > AUX), newline characters \n get converted
into \r\n automatically.

What you get, for instance, is: https://imageshack.com/a/img853/5834/l34.png

svn path=/trunk/; revision=59411
This commit is contained in:
Hermès Bélusca-Maïto 2013-07-02 23:07:15 +00:00
parent 803c51b6ba
commit 1965b2bf85
15 changed files with 173 additions and 128 deletions

View file

@ -80,7 +80,7 @@ PrintAssociation(LPTSTR extension)
if (fileTypeLength != 0) /* if there is a default key, display relevant information */ if (fileTypeLength != 0) /* if there is a default key, display relevant information */
{ {
ConOutPrintf(_T("%s=%s\r\n"), extension, fileType); ConOutPrintf(_T("%s=%s\n"), extension, fileType);
} }
if (fileTypeLength) if (fileTypeLength)

View file

@ -1378,7 +1378,7 @@ ReadLine(TCHAR *commandline, BOOL bMore)
if (bEcho) if (bEcho)
{ {
if (!bIgnoreEcho) if (!bIgnoreEcho)
ConOutChar('\n'); ConOutChar(_T('\n'));
PrintPrompt(); PrintPrompt();
} }
} }
@ -1391,7 +1391,7 @@ ReadLine(TCHAR *commandline, BOOL bMore)
if (CheckCtrlBreak(BREAK_INPUT)) if (CheckCtrlBreak(BREAK_INPUT))
{ {
ConOutPuts(_T("\n")); ConOutChar(_T('\n'));
return FALSE; return FALSE;
} }
ip = readline; ip = readline;

View file

@ -271,7 +271,7 @@ VOID PrintCommandList(VOID)
} }
if (y != 0) if (y != 0)
ConOutChar('\n'); ConOutChar(_T('\n'));
} }
VOID PrintCommandListDetail(VOID) VOID PrintCommandListDetail(VOID)

View file

@ -138,6 +138,7 @@ static VOID ConWrite(TCHAR *str, DWORD len, DWORD nStdHandle)
{ {
DWORD dwWritten; DWORD dwWritten;
HANDLE hOutput = GetStdHandle(nStdHandle); HANDLE hOutput = GetStdHandle(nStdHandle);
PVOID p;
/* Check whether we are writing to a console and if so, write to it */ /* Check whether we are writing to a console and if so, write to it */
if (IsConsoleHandle(hOutput)) if (IsConsoleHandle(hOutput))
@ -157,10 +158,35 @@ static VOID ConWrite(TCHAR *str, DWORD len, DWORD nStdHandle)
error_out_of_memory(); error_out_of_memory();
return; return;
} }
len = MultiByteToWideChar(OutputCodePage, 0, str, len, buffer, len); len = (DWORD)MultiByteToWideChar(OutputCodePage, 0, str, (INT)len, buffer, (INT)len);
str = (PVOID)buffer; str = (PVOID)buffer;
#endif #endif
/*
* Find any newline character in the buffer,
* send the part BEFORE the newline, then send
* a carriage-return + newline, and then send
* the remaining part of the buffer.
*
* This fixes output in files and serial console.
*/
while (str && *(PWCHAR)str && len > 0)
{
p = wcspbrk((PWCHAR)str, L"\r\n");
if (p)
{
len -= ((PWCHAR)p - (PWCHAR)str) + 1;
WriteFile(hOutput, str, ((PWCHAR)p - (PWCHAR)str) * sizeof(WCHAR), &dwWritten, NULL);
WriteFile(hOutput, L"\r\n", 2 * sizeof(WCHAR), &dwWritten, NULL);
str = (PVOID)((PWCHAR)p + 1);
}
else
{
WriteFile(hOutput, str, len * sizeof(WCHAR), &dwWritten, NULL); WriteFile(hOutput, str, len * sizeof(WCHAR), &dwWritten, NULL);
break;
}
}
// WriteFile(hOutput, str, len * sizeof(WCHAR), &dwWritten, NULL);
#ifndef _UNICODE #ifndef _UNICODE
cmd_free(buffer); cmd_free(buffer);
#endif #endif
@ -177,7 +203,32 @@ static VOID ConWrite(TCHAR *str, DWORD len, DWORD nStdHandle)
len = WideCharToMultiByte(OutputCodePage, 0, str, len, buffer, len * MB_LEN_MAX, NULL, NULL); len = WideCharToMultiByte(OutputCodePage, 0, str, len, buffer, len * MB_LEN_MAX, NULL, NULL);
str = (PVOID)buffer; str = (PVOID)buffer;
#endif #endif
/*
* Find any newline character in the buffer,
* send the part BEFORE the newline, then send
* a carriage-return + newline, and then send
* the remaining part of the buffer.
*
* This fixes output in files and serial console.
*/
while (str && *(PCHAR)str && len > 0)
{
p = strpbrk((PCHAR)str, "\r\n");
if (p)
{
len -= ((PCHAR)p - (PCHAR)str) + 1;
WriteFile(hOutput, str, ((PCHAR)p - (PCHAR)str), &dwWritten, NULL);
WriteFile(hOutput, "\r\n", 2, &dwWritten, NULL);
str = (PVOID)((PCHAR)p + 1);
}
else
{
WriteFile(hOutput, str, len, &dwWritten, NULL); WriteFile(hOutput, str, len, &dwWritten, NULL);
break;
}
}
// WriteFile(hOutput, str, len, &dwWritten, NULL);
#ifdef _UNICODE #ifdef _UNICODE
cmd_free(buffer); cmd_free(buffer);
#endif #endif
@ -191,7 +242,7 @@ VOID ConOutChar (TCHAR c)
VOID ConPuts(LPTSTR szText, DWORD nStdHandle) VOID ConPuts(LPTSTR szText, DWORD nStdHandle)
{ {
ConWrite(szText, _tcslen(szText), nStdHandle); ConWrite(szText, (DWORD)_tcslen(szText), nStdHandle);
} }
VOID ConOutResPaging(BOOL NewPage, UINT resID) VOID ConOutResPaging(BOOL NewPage, UINT resID)
@ -205,7 +256,6 @@ VOID ConOutResPuts (UINT resID)
{ {
TCHAR szMsg[RC_STRING_MAX_SIZE]; TCHAR szMsg[RC_STRING_MAX_SIZE];
LoadString(CMD_ModuleHandle, resID, szMsg, RC_STRING_MAX_SIZE); LoadString(CMD_ModuleHandle, resID, szMsg, RC_STRING_MAX_SIZE);
ConPuts(szMsg, STD_OUTPUT_HANDLE); ConPuts(szMsg, STD_OUTPUT_HANDLE);
} }
@ -218,7 +268,10 @@ VOID ConOutPuts (LPTSTR szText)
VOID ConPrintf(LPTSTR szFormat, va_list arg_ptr, DWORD nStdHandle) VOID ConPrintf(LPTSTR szFormat, va_list arg_ptr, DWORD nStdHandle)
{ {
TCHAR szOut[OUTPUT_BUFFER_SIZE]; TCHAR szOut[OUTPUT_BUFFER_SIZE];
ConWrite(szOut, _vstprintf(szOut, szFormat, arg_ptr), nStdHandle); DWORD len;
len = (DWORD)_vstprintf(szOut, szFormat, arg_ptr);
ConWrite(szOut, len, nStdHandle);
} }
INT ConPrintfPaging(BOOL NewPage, LPTSTR szFormat, va_list arg_ptr, DWORD nStdHandle) INT ConPrintfPaging(BOOL NewPage, LPTSTR szFormat, va_list arg_ptr, DWORD nStdHandle)
@ -247,18 +300,18 @@ INT ConPrintfPaging(BOOL NewPage, LPTSTR szFormat, va_list arg_ptr, DWORD nStdHa
if (szFormat == NULL) if (szFormat == NULL)
return 0; return 0;
//get the size of the visual screen that can be printed too /* Get the size of the visual screen that can be printed too */
if (!IsConsoleHandle(hOutput) || !GetConsoleScreenBufferInfo(hOutput, &csbi)) if (!IsConsoleHandle(hOutput) || !GetConsoleScreenBufferInfo(hOutput, &csbi))
{ {
// we assuming its a file handle /* We assume it's a file handle */
ConPrintf(szFormat, arg_ptr, nStdHandle); ConPrintf(szFormat, arg_ptr, nStdHandle);
return 0; return 0;
} }
//subtract 2 to account for "press any key..." and for the blank line at the end of PagePrompt() /* Subtract 2 to account for "press any key..." and for the blank line at the end of PagePrompt() */
ScreenLines = (csbi.srWindow.Bottom - csbi.srWindow.Top) - 4; ScreenLines = (csbi.srWindow.Bottom - csbi.srWindow.Top) - 4;
CharSL = csbi.dwCursorPosition.X; CharSL = csbi.dwCursorPosition.X;
//make sure they didnt make the screen to small /* Make sure they didn't make the screen to small */
if (ScreenLines < 4) if (ScreenLines < 4)
{ {
ConPrintf(szFormat, arg_ptr, nStdHandle); ConPrintf(szFormat, arg_ptr, nStdHandle);
@ -269,7 +322,7 @@ INT ConPrintfPaging(BOOL NewPage, LPTSTR szFormat, va_list arg_ptr, DWORD nStdHa
while (i < len) while (i < len)
{ {
// Search until the end of a line is reached /* Search until the end of a line is reached */
if (szOut[i++] != _T('\n') && ++CharSL < csbi.dwSize.X) if (szOut[i++] != _T('\n') && ++CharSL < csbi.dwSize.X)
continue; continue;
@ -285,7 +338,7 @@ INT ConPrintfPaging(BOOL NewPage, LPTSTR szFormat, va_list arg_ptr, DWORD nStdHa
{ {
return 1; return 1;
} }
//reset the number of lines being printed /* Reset the number of lines being printed */
LineCount = 0; LineCount = 0;
} }
} }
@ -423,6 +476,7 @@ VOID ConErrPrintf (LPTSTR szFormat, ...)
va_end(arg_ptr); va_end(arg_ptr);
} }
VOID SetCursorXY(SHORT x, SHORT y) VOID SetCursorXY(SHORT x, SHORT y)
{ {
COORD coPos; COORD coPos;
@ -432,7 +486,6 @@ VOID SetCursorXY (SHORT x, SHORT y)
SetConsoleCursorPosition(GetStdHandle (STD_OUTPUT_HANDLE), coPos); SetConsoleCursorPosition(GetStdHandle (STD_OUTPUT_HANDLE), coPos);
} }
VOID GetCursorXY(PSHORT x, PSHORT y) VOID GetCursorXY(PSHORT x, PSHORT y)
{ {
CONSOLE_SCREEN_BUFFER_INFO csbi; CONSOLE_SCREEN_BUFFER_INFO csbi;
@ -443,44 +496,22 @@ VOID GetCursorXY (PSHORT x, PSHORT y)
*y = csbi.dwCursorPosition.Y; *y = csbi.dwCursorPosition.Y;
} }
SHORT GetCursorX(VOID) SHORT GetCursorX(VOID)
{ {
CONSOLE_SCREEN_BUFFER_INFO csbi; CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi); GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
return csbi.dwCursorPosition.X; return csbi.dwCursorPosition.X;
} }
SHORT GetCursorY(VOID) SHORT GetCursorY(VOID)
{ {
CONSOLE_SCREEN_BUFFER_INFO csbi; CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi); GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
return csbi.dwCursorPosition.Y; return csbi.dwCursorPosition.Y;
} }
VOID GetScreenSize (PSHORT maxx, PSHORT maxy)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
if (!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
{
csbi.dwSize.X = 80;
csbi.dwSize.Y = 25;
}
if (maxx)
*maxx = csbi.dwSize.X;
if (maxy)
*maxy = csbi.dwSize.Y;
}
VOID SetCursorType(BOOL bInsert, BOOL bVisible) VOID SetCursorType(BOOL bInsert, BOOL bVisible)
{ {
CONSOLE_CURSOR_INFO cci; CONSOLE_CURSOR_INFO cci;
@ -491,4 +522,18 @@ VOID SetCursorType (BOOL bInsert, BOOL bVisible)
SetConsoleCursorInfo(GetStdHandle (STD_OUTPUT_HANDLE), &cci); SetConsoleCursorInfo(GetStdHandle (STD_OUTPUT_HANDLE), &cci);
} }
VOID GetScreenSize(PSHORT maxx, PSHORT maxy)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
if (!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
{
csbi.dwSize.X = 80;
csbi.dwSize.Y = 25;
}
if (maxx) *maxx = csbi.dwSize.X;
if (maxy) *maxy = csbi.dwSize.Y;
}
/* EOF */ /* EOF */

View file

@ -83,7 +83,7 @@ INT CommandEcho (LPTSTR param)
{ {
/* skip the first character */ /* skip the first character */
ConOutPuts(param + 1); ConOutPuts(param + 1);
ConOutPuts(_T("\r\n")); ConOutChar(_T('\n'));
} }
return 0; return 0;
} }

View file

@ -323,7 +323,7 @@ BOOL ShowCompletionMatches (LPTSTR str, INT charcount)
/* print the new line only if we aren't on the /* print the new line only if we aren't on the
* last column, in this case it wraps anyway */ * last column, in this case it wraps anyway */
if (count * longestfname != (UINT)screenwidth) if (count * longestfname != (UINT)screenwidth)
ConOutPrintf (_T("\n")); ConOutChar(_T('\n'));
count = 0; count = 0;
} }
} }

View file

@ -157,7 +157,7 @@ BOOL CheckCtrlBreak (INT mode)
c = _totupper(cgetchar()); c = _totupper(cgetchar());
} while (!(_tcschr(options, c) || c == _T('\3')) || !c); } while (!(_tcschr(options, c) || c == _T('\3')) || !c);
ConOutPuts (_T("\r\n")); ConOutChar(_T('\n'));
if (c == options[1]) if (c == options[1])
return bCtrlBreak = FALSE; /* ignore */ return bCtrlBreak = FALSE; /* ignore */
@ -579,7 +579,7 @@ INT FilePromptYN (UINT resID)
/* preliminary fix */ /* preliminary fix */
ConInString(szIn, 10); ConInString(szIn, 10);
ConOutPrintf (_T("\n")); ConOutChar(_T('\n'));
_tcsupr (szIn); _tcsupr (szIn);
for (p = szIn; _istspace (*p); p++) for (p = szIn; _istspace (*p); p++)
@ -643,7 +643,7 @@ INT FilePromptYNA (UINT resID)
/* preliminary fix */ /* preliminary fix */
ConInString(szIn, 10); ConInString(szIn, 10);
ConOutPrintf (_T("\n")); ConOutChar(_T('\n'));
_tcsupr (szIn); _tcsupr (szIn);
for (p = szIn; _istspace (*p); p++) for (p = szIn; _istspace (*p); p++)

View file

@ -96,7 +96,7 @@ INT cmd_set (LPTSTR param)
if (*lpOutput != _T('=')) if (*lpOutput != _T('='))
ConOutPuts(lpOutput); ConOutPuts(lpOutput);
lpOutput += _tcslen(lpOutput) + 1; lpOutput += _tcslen(lpOutput) + 1;
ConOutPuts(_T("\r\n")); ConOutChar(_T('\n'));
} }
FreeEnvironmentStrings (lpEnv); FreeEnvironmentStrings (lpEnv);
} }
@ -185,7 +185,7 @@ INT cmd_set (LPTSTR param)
if (!_tcsnicmp(lpOutput, param, p - param)) if (!_tcsnicmp(lpOutput, param, p - param))
{ {
ConOutPuts(lpOutput); ConOutPuts(lpOutput);
ConOutPuts(_T("\r\n")); ConOutChar(_T('\n'));
bFound = TRUE; bFound = TRUE;
} }
lpOutput += _tcslen(lpOutput) + 1; lpOutput += _tcslen(lpOutput) + 1;

View file

@ -46,7 +46,7 @@ VOID ShortVersion (VOID)
ConOutResPrintf(STRING_VERSION_RUNVER, RosVersion); ConOutResPrintf(STRING_VERSION_RUNVER, RosVersion);
} }
} }
ConOutPuts (_T("\n")); ConOutChar(_T('\n'));
} }