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 */
{
ConOutPrintf(_T("%s=%s\r\n"), extension, fileType);
ConOutPrintf(_T("%s=%s\n"), extension, fileType);
}
if (fileTypeLength)

View file

@ -96,15 +96,15 @@ PrintAttribute (LPTSTR pszPath, LPTSTR pszFile, BOOL bRecurse)
_tcscpy (pszFileName, findData.cFileName);
ConOutPrintf (_T("%c %c%c%c %s\n"),
(findData.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) ? _T('A') : _T(' '),
(findData.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) ? _T('S') : _T(' '),
(findData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) ? _T('H') : _T(' '),
(findData.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? _T('R') : _T(' '),
szFullName);
ConOutPrintf(_T("%c %c%c%c %s\n"),
(findData.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) ? _T('A') : _T(' '),
(findData.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) ? _T('S') : _T(' '),
(findData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) ? _T('H') : _T(' '),
(findData.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? _T('R') : _T(' '),
szFullName);
}
while (FindNextFile (hFind, &findData));
FindClose (hFind);
while (FindNextFile(hFind, &findData));
FindClose(hFind);
}

View file

@ -296,7 +296,7 @@ loop:
GetTickCount () - clk);
val = IsKeyInString (lpOptions, cDefault, bCaseSensitive);
ConOutPrintf (_T("%c\n"), lpOptions[val]);
ConOutPrintf(_T("%c\n"), lpOptions[val]);
nErrorLevel = val + 1;

View file

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

View file

@ -438,7 +438,7 @@ BOOL ReadCommand(LPTSTR str, INT maxlen)
#endif
str[charcount++] = _T('\n');
str[charcount] = _T('\0');
ConOutChar (_T('\n'));
ConOutChar(_T('\n'));
bReturn = TRUE;
break;

View file

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

View file

@ -138,6 +138,7 @@ static VOID ConWrite(TCHAR *str, DWORD len, DWORD nStdHandle)
{
DWORD dwWritten;
HANDLE hOutput = GetStdHandle(nStdHandle);
PVOID p;
/* Check whether we are writing to a console and if so, write to it */
if (IsConsoleHandle(hOutput))
@ -157,10 +158,35 @@ static VOID ConWrite(TCHAR *str, DWORD len, DWORD nStdHandle)
error_out_of_memory();
return;
}
len = MultiByteToWideChar(OutputCodePage, 0, str, len, buffer, len);
len = (DWORD)MultiByteToWideChar(OutputCodePage, 0, str, (INT)len, buffer, (INT)len);
str = (PVOID)buffer;
#endif
WriteFile(hOutput, str, len * sizeof(WCHAR), &dwWritten, NULL);
/*
* 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);
break;
}
}
// WriteFile(hOutput, str, len * sizeof(WCHAR), &dwWritten, NULL);
#ifndef _UNICODE
cmd_free(buffer);
#endif
@ -177,21 +203,46 @@ static VOID ConWrite(TCHAR *str, DWORD len, DWORD nStdHandle)
len = WideCharToMultiByte(OutputCodePage, 0, str, len, buffer, len * MB_LEN_MAX, NULL, NULL);
str = (PVOID)buffer;
#endif
WriteFile(hOutput, str, len, &dwWritten, NULL);
/*
* 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);
break;
}
}
// WriteFile(hOutput, str, len, &dwWritten, NULL);
#ifdef _UNICODE
cmd_free(buffer);
#endif
}
}
VOID ConOutChar (TCHAR c)
VOID ConOutChar(TCHAR c)
{
ConWrite(&c, 1, STD_OUTPUT_HANDLE);
}
VOID ConPuts(LPTSTR szText, DWORD nStdHandle)
{
ConWrite(szText, _tcslen(szText), nStdHandle);
ConWrite(szText, (DWORD)_tcslen(szText), nStdHandle);
}
VOID ConOutResPaging(BOOL NewPage, UINT resID)
@ -201,15 +252,14 @@ VOID ConOutResPaging(BOOL NewPage, UINT resID)
ConOutPrintfPaging(NewPage, szMsg);
}
VOID ConOutResPuts (UINT resID)
VOID ConOutResPuts(UINT resID)
{
TCHAR szMsg[RC_STRING_MAX_SIZE];
LoadString(CMD_ModuleHandle, resID, szMsg, RC_STRING_MAX_SIZE);
ConPuts(szMsg, STD_OUTPUT_HANDLE);
}
VOID ConOutPuts (LPTSTR szText)
VOID ConOutPuts(LPTSTR szText)
{
ConPuts(szText, STD_OUTPUT_HANDLE);
}
@ -218,7 +268,10 @@ VOID ConOutPuts (LPTSTR szText)
VOID ConPrintf(LPTSTR szFormat, va_list arg_ptr, DWORD nStdHandle)
{
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)
@ -247,29 +300,29 @@ INT ConPrintfPaging(BOOL NewPage, LPTSTR szFormat, va_list arg_ptr, DWORD nStdHa
if (szFormat == NULL)
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))
{
// we assuming its a file handle
/* We assume it's a file handle */
ConPrintf(szFormat, arg_ptr, nStdHandle);
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;
CharSL = csbi.dwCursorPosition.X;
//make sure they didnt make the screen to small
if (ScreenLines<4)
/* Make sure they didn't make the screen to small */
if (ScreenLines < 4)
{
ConPrintf(szFormat, arg_ptr, nStdHandle);
return 0;
}
len = _vstprintf (szOut, szFormat, arg_ptr);
len = _vstprintf(szOut, szFormat, arg_ptr);
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)
continue;
@ -285,7 +338,7 @@ INT ConPrintfPaging(BOOL NewPage, LPTSTR szFormat, va_list arg_ptr, DWORD nStdHa
{
return 1;
}
//reset the number of lines being printed
/* Reset the number of lines being printed */
LineCount = 0;
}
}
@ -295,43 +348,14 @@ INT ConPrintfPaging(BOOL NewPage, LPTSTR szFormat, va_list arg_ptr, DWORD nStdHa
return 0;
}
VOID ConErrFormatMessage (DWORD MessageId, ...)
VOID ConErrFormatMessage(DWORD MessageId, ...)
{
TCHAR szMsg[RC_STRING_MAX_SIZE];
DWORD ret;
LPTSTR text;
va_list arg_ptr;
va_start (arg_ptr, MessageId);
ret = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
MessageId,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &text,
0,
&arg_ptr);
va_end (arg_ptr);
if (ret > 0)
{
ConErrPuts (text);
LocalFree(text);
}
else
{
LoadString(CMD_ModuleHandle, STRING_CONSOLE_ERROR, szMsg, RC_STRING_MAX_SIZE);
ConErrPrintf(szMsg);
}
}
VOID ConOutFormatMessage (DWORD MessageId, ...)
{
TCHAR szMsg[RC_STRING_MAX_SIZE];
DWORD ret;
LPTSTR text;
va_list arg_ptr;
va_start (arg_ptr, MessageId);
va_start(arg_ptr, MessageId);
ret = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
MessageId,
@ -340,10 +364,10 @@ VOID ConOutFormatMessage (DWORD MessageId, ...)
0,
&arg_ptr);
va_end (arg_ptr);
va_end(arg_ptr);
if (ret > 0)
{
ConErrPuts (text);
ConErrPuts(text);
LocalFree(text);
}
else
@ -353,118 +377,152 @@ VOID ConOutFormatMessage (DWORD MessageId, ...)
}
}
VOID ConOutResPrintf (UINT resID, ...)
VOID ConOutFormatMessage(DWORD MessageId, ...)
{
TCHAR szMsg[RC_STRING_MAX_SIZE];
DWORD ret;
LPTSTR text;
va_list arg_ptr;
va_start(arg_ptr, MessageId);
ret = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
MessageId,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &text,
0,
&arg_ptr);
va_end(arg_ptr);
if (ret > 0)
{
ConErrPuts(text);
LocalFree(text);
}
else
{
LoadString(CMD_ModuleHandle, STRING_CONSOLE_ERROR, szMsg, RC_STRING_MAX_SIZE);
ConErrPrintf(szMsg);
}
}
VOID ConOutResPrintf(UINT resID, ...)
{
TCHAR szMsg[RC_STRING_MAX_SIZE];
va_list arg_ptr;
va_start (arg_ptr, resID);
va_start(arg_ptr, resID);
LoadString(CMD_ModuleHandle, resID, szMsg, RC_STRING_MAX_SIZE);
ConPrintf(szMsg, arg_ptr, STD_OUTPUT_HANDLE);
va_end (arg_ptr);
va_end(arg_ptr);
}
VOID ConOutPrintf (LPTSTR szFormat, ...)
VOID ConOutPrintf(LPTSTR szFormat, ...)
{
va_list arg_ptr;
va_start (arg_ptr, szFormat);
va_start(arg_ptr, szFormat);
ConPrintf(szFormat, arg_ptr, STD_OUTPUT_HANDLE);
va_end (arg_ptr);
va_end(arg_ptr);
}
INT ConOutPrintfPaging (BOOL NewPage, LPTSTR szFormat, ...)
INT ConOutPrintfPaging(BOOL NewPage, LPTSTR szFormat, ...)
{
INT iReturn;
va_list arg_ptr;
va_start (arg_ptr, szFormat);
va_start(arg_ptr, szFormat);
iReturn = ConPrintfPaging(NewPage, szFormat, arg_ptr, STD_OUTPUT_HANDLE);
va_end (arg_ptr);
va_end(arg_ptr);
return iReturn;
}
VOID ConErrChar (TCHAR c)
VOID ConErrChar(TCHAR c)
{
ConWrite(&c, 1, STD_ERROR_HANDLE);
}
VOID ConErrResPuts (UINT resID)
VOID ConErrResPuts(UINT resID)
{
TCHAR szMsg[RC_STRING_MAX_SIZE];
LoadString(CMD_ModuleHandle, resID, szMsg, RC_STRING_MAX_SIZE);
ConPuts(szMsg, STD_ERROR_HANDLE);
}
VOID ConErrPuts (LPTSTR szText)
VOID ConErrPuts(LPTSTR szText)
{
ConPuts(szText, STD_ERROR_HANDLE);
}
VOID ConErrResPrintf (UINT resID, ...)
VOID ConErrResPrintf(UINT resID, ...)
{
TCHAR szMsg[RC_STRING_MAX_SIZE];
va_list arg_ptr;
va_start (arg_ptr, resID);
va_start(arg_ptr, resID);
LoadString(CMD_ModuleHandle, resID, szMsg, RC_STRING_MAX_SIZE);
ConPrintf(szMsg, arg_ptr, STD_ERROR_HANDLE);
va_end (arg_ptr);
va_end(arg_ptr);
}
VOID ConErrPrintf (LPTSTR szFormat, ...)
VOID ConErrPrintf(LPTSTR szFormat, ...)
{
va_list arg_ptr;
va_start (arg_ptr, szFormat);
va_start(arg_ptr, szFormat);
ConPrintf(szFormat, arg_ptr, STD_ERROR_HANDLE);
va_end (arg_ptr);
va_end(arg_ptr);
}
VOID SetCursorXY (SHORT x, SHORT y)
VOID SetCursorXY(SHORT x, SHORT y)
{
COORD coPos;
coPos.X = x;
coPos.Y = 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;
GetConsoleScreenBufferInfo (GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
*x = csbi.dwCursorPosition.X;
*y = csbi.dwCursorPosition.Y;
}
SHORT GetCursorX (VOID)
SHORT GetCursorX(VOID)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo (GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
return csbi.dwCursorPosition.X;
}
SHORT GetCursorY (VOID)
SHORT GetCursorY(VOID)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo (GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
return csbi.dwCursorPosition.Y;
}
VOID SetCursorType(BOOL bInsert, BOOL bVisible)
{
CONSOLE_CURSOR_INFO cci;
VOID GetScreenSize (PSHORT maxx, PSHORT maxy)
cci.dwSize = bInsert ? 10 : 99;
cci.bVisible = bVisible;
SetConsoleCursorInfo(GetStdHandle (STD_OUTPUT_HANDLE), &cci);
}
VOID GetScreenSize(PSHORT maxx, PSHORT maxy)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
@ -474,21 +532,8 @@ VOID GetScreenSize (PSHORT maxx, PSHORT maxy)
csbi.dwSize.Y = 25;
}
if (maxx)
*maxx = csbi.dwSize.X;
if (maxy)
*maxy = csbi.dwSize.Y;
}
VOID SetCursorType (BOOL bInsert, BOOL bVisible)
{
CONSOLE_CURSOR_INFO cci;
cci.dwSize = bInsert ? 10 : 99;
cci.bVisible = bVisible;
SetConsoleCursorInfo (GetStdHandle (STD_OUTPUT_HANDLE), &cci);
if (maxx) *maxx = csbi.dwSize.X;
if (maxy) *maxy = csbi.dwSize.Y;
}
/* EOF */

View file

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

View file

@ -46,7 +46,7 @@ VOID ErrorMessage (DWORD dwErrorCode, LPTSTR szFormat, ...)
NULL, dwErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&szError, 0, NULL))
{
ConErrPrintf (_T("%s %s\n"), szError, szMessage);
ConErrPrintf(_T("%s %s\n"), szError, szMessage);
if (szError)
LocalFree (szError);
return;
@ -54,9 +54,9 @@ VOID ErrorMessage (DWORD dwErrorCode, LPTSTR szFormat, ...)
/* Fall back just in case the error is not defined */
if (szFormat)
ConErrPrintf (_T("%s -- %s\n"), szMsg, szMessage);
ConErrPrintf(_T("%s -- %s\n"), szMsg, szMessage);
else
ConErrPrintf (_T("%s\n"), szMsg);
ConErrPrintf(_T("%s\n"), szMsg);
}
VOID error_parameter_format(TCHAR ch)

View file

@ -302,7 +302,7 @@ BOOL ShowCompletionMatches (LPTSTR str, INT charcount)
longestfname += 3;
/* find anything */
ConOutChar (_T('\n'));
ConOutChar(_T('\n'));
do
{
/* ignore . and .. */
@ -323,7 +323,7 @@ BOOL ShowCompletionMatches (LPTSTR str, INT charcount)
/* print the new line only if we aren't on the
* last column, in this case it wraps anyway */
if (count * longestfname != (UINT)screenwidth)
ConOutPrintf (_T("\n"));
ConOutChar(_T('\n'));
count = 0;
}
}
@ -332,7 +332,7 @@ BOOL ShowCompletionMatches (LPTSTR str, INT charcount)
FindClose (hFile);
if (count)
ConOutChar (_T('\n'));
ConOutChar(_T('\n'));
}
else
{

View file

@ -66,7 +66,7 @@ PrintDiskInfo (LPTSTR szDisk)
&dwBytPerSec, &dwFreeCl, &dwTotCl))
{
LoadString(CMD_ModuleHandle, STRING_FREE_ERROR1, szMsg, RC_STRING_MAX_SIZE);
ConErrPrintf (_T("%s %s:\n"), szMsg, szDrive);
ConErrPrintf(_T("%s %s:\n"), szMsg, szDrive);
return;
}

View file

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

View file

@ -63,7 +63,7 @@ INT cmd_path (LPTSTR param)
GetEnvironmentVariable (_T("PATH"), pszBuffer, dwBuffer);
}
ConOutPrintf (_T("PATH=%s\n"), pszBuffer);
ConOutPrintf(_T("PATH=%s\n"), pszBuffer);
cmd_free (pszBuffer);
return 0;

View file

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

View file

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