Some reformatting, no code change

svn path=/trunk/; revision=25991
This commit is contained in:
Hervé Poussineau 2007-03-04 23:18:28 +00:00
parent 55a11ded1b
commit abfa7fb417

View file

@ -16,71 +16,71 @@
* 02-Feb-2007 (Paolo Devoti) <devotip at gmail.com>) * 02-Feb-2007 (Paolo Devoti) <devotip at gmail.com>)
* Fixed ConPrintfPaging * Fixed ConPrintfPaging
*/ */
#include <precomp.h> #include <precomp.h>
#include "resource.h" #include "resource.h"
#define OUTPUT_BUFFER_SIZE 4096 #define OUTPUT_BUFFER_SIZE 4096
UINT InputCodePage; UINT InputCodePage;
UINT OutputCodePage; UINT OutputCodePage;
VOID ConInDisable (VOID) VOID ConInDisable (VOID)
{ {
HANDLE hInput = GetStdHandle (STD_INPUT_HANDLE); HANDLE hInput = GetStdHandle (STD_INPUT_HANDLE);
DWORD dwMode; DWORD dwMode;
GetConsoleMode (hInput, &dwMode); GetConsoleMode (hInput, &dwMode);
dwMode &= ~ENABLE_PROCESSED_INPUT; dwMode &= ~ENABLE_PROCESSED_INPUT;
SetConsoleMode (hInput, dwMode); SetConsoleMode (hInput, dwMode);
} }
VOID ConInEnable (VOID) VOID ConInEnable (VOID)
{ {
HANDLE hInput = GetStdHandle (STD_INPUT_HANDLE); HANDLE hInput = GetStdHandle (STD_INPUT_HANDLE);
DWORD dwMode; DWORD dwMode;
GetConsoleMode (hInput, &dwMode); GetConsoleMode (hInput, &dwMode);
dwMode |= ENABLE_PROCESSED_INPUT; dwMode |= ENABLE_PROCESSED_INPUT;
SetConsoleMode (hInput, dwMode); SetConsoleMode (hInput, dwMode);
} }
VOID ConInDummy (VOID) VOID ConInDummy (VOID)
{ {
HANDLE hInput = GetStdHandle (STD_INPUT_HANDLE); HANDLE hInput = GetStdHandle (STD_INPUT_HANDLE);
INPUT_RECORD dummy; INPUT_RECORD dummy;
DWORD dwRead; DWORD dwRead;
#ifdef _DEBUG #ifdef _DEBUG
if (hInput == INVALID_HANDLE_VALUE) if (hInput == INVALID_HANDLE_VALUE)
DebugPrintf (_T("Invalid input handle!!!\n")); DebugPrintf (_T("Invalid input handle!!!\n"));
#endif /* _DEBUG */ #endif /* _DEBUG */
ReadConsoleInput (hInput, &dummy, 1, &dwRead); ReadConsoleInput (hInput, &dummy, 1, &dwRead);
} }
VOID ConInFlush (VOID) VOID ConInFlush (VOID)
{ {
FlushConsoleInputBuffer (GetStdHandle (STD_INPUT_HANDLE)); FlushConsoleInputBuffer (GetStdHandle (STD_INPUT_HANDLE));
} }
VOID ConInKey (PINPUT_RECORD lpBuffer) VOID ConInKey (PINPUT_RECORD lpBuffer)
{ {
HANDLE hInput = GetStdHandle (STD_INPUT_HANDLE); HANDLE hInput = GetStdHandle (STD_INPUT_HANDLE);
DWORD dwRead; DWORD dwRead;
#ifdef _DEBUG #ifdef _DEBUG
if (hInput == INVALID_HANDLE_VALUE) if (hInput == INVALID_HANDLE_VALUE)
DebugPrintf (_T("Invalid input handle!!!\n")); DebugPrintf (_T("Invalid input handle!!!\n"));
#endif /* _DEBUG */ #endif /* _DEBUG */
do do
{ {
ReadConsoleInput (hInput, lpBuffer, 1, &dwRead); ReadConsoleInput (hInput, lpBuffer, 1, &dwRead);
@ -90,19 +90,18 @@ VOID ConInKey (PINPUT_RECORD lpBuffer)
} }
while (TRUE); while (TRUE);
} }
VOID ConInString (LPTSTR lpInput, DWORD dwLength) VOID ConInString (LPTSTR lpInput, DWORD dwLength)
{ {
DWORD dwOldMode; DWORD dwOldMode;
DWORD dwRead; DWORD dwRead;
HANDLE hFile; HANDLE hFile;
LPTSTR p; LPTSTR p;
DWORD i; DWORD i;
PCHAR pBuf; PCHAR pBuf;
#ifdef _UNICODE #ifdef _UNICODE
pBuf = (PCHAR)malloc(dwLength); pBuf = (PCHAR)malloc(dwLength);
#else #else
@ -111,11 +110,11 @@ VOID ConInString (LPTSTR lpInput, DWORD dwLength)
ZeroMemory (lpInput, dwLength * sizeof(TCHAR)); ZeroMemory (lpInput, dwLength * sizeof(TCHAR));
hFile = GetStdHandle (STD_INPUT_HANDLE); hFile = GetStdHandle (STD_INPUT_HANDLE);
GetConsoleMode (hFile, &dwOldMode); GetConsoleMode (hFile, &dwOldMode);
SetConsoleMode (hFile, ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT); SetConsoleMode (hFile, ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT);
ReadFile (hFile, (PVOID)pBuf, dwLength, &dwRead, NULL); ReadFile (hFile, (PVOID)pBuf, dwLength, &dwRead, NULL);
#ifdef _UNICODE #ifdef _UNICODE
MultiByteToWideChar( InputCodePage, 0, pBuf, dwLength + 1, lpInput, dwLength + 1); MultiByteToWideChar( InputCodePage, 0, pBuf, dwLength + 1, lpInput, dwLength + 1);
#endif #endif
@ -128,14 +127,14 @@ VOID ConInString (LPTSTR lpInput, DWORD dwLength)
break; break;
} }
} }
#ifdef _UNICODE #ifdef _UNICODE
free(pBuf); free(pBuf);
#endif #endif
SetConsoleMode (hFile, dwOldMode); SetConsoleMode (hFile, dwOldMode);
} }
static VOID ConChar(TCHAR c, DWORD nStdHandle) static VOID ConChar(TCHAR c, DWORD nStdHandle)
{ {
DWORD dwWritten; DWORD dwWritten;
@ -156,18 +155,18 @@ static VOID ConChar(TCHAR c, DWORD nStdHandle)
&dwWritten, &dwWritten,
NULL); NULL);
} }
VOID ConOutChar (TCHAR c) VOID ConOutChar (TCHAR c)
{ {
ConChar(c, STD_OUTPUT_HANDLE); ConChar(c, STD_OUTPUT_HANDLE);
} }
VOID ConPuts(LPTSTR szText, DWORD nStdHandle) VOID ConPuts(LPTSTR szText, DWORD nStdHandle)
{ {
DWORD dwWritten; DWORD dwWritten;
PCHAR pBuf; PCHAR pBuf;
INT len; INT len;
len = _tcslen(szText); len = _tcslen(szText);
#ifdef _UNICODE #ifdef _UNICODE
pBuf = malloc(len + 1); pBuf = malloc(len + 1);
@ -189,35 +188,35 @@ VOID ConPuts(LPTSTR szText, DWORD nStdHandle)
free(pBuf); free(pBuf);
#endif #endif
} }
VOID ConOutResPaging(BOOL NewPage, UINT resID) VOID ConOutResPaging(BOOL NewPage, 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);
ConOutPrintfPaging(NewPage, szMsg); ConOutPrintfPaging(NewPage, szMsg);
} }
VOID ConOutResPuts (UINT resID) 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);
} }
VOID ConOutPuts (LPTSTR szText) VOID ConOutPuts (LPTSTR szText)
{ {
ConPuts(szText, STD_OUTPUT_HANDLE); ConPuts(szText, STD_OUTPUT_HANDLE);
} }
VOID ConPrintf(LPTSTR szFormat, va_list arg_ptr, DWORD nStdHandle) VOID ConPrintf(LPTSTR szFormat, va_list arg_ptr, DWORD nStdHandle)
{ {
INT len; INT len;
PCHAR pBuf; PCHAR pBuf;
TCHAR szOut[OUTPUT_BUFFER_SIZE]; TCHAR szOut[OUTPUT_BUFFER_SIZE];
DWORD dwWritten; DWORD dwWritten;
len = _vstprintf (szOut, szFormat, arg_ptr); len = _vstprintf (szOut, szFormat, arg_ptr);
#ifdef _UNICODE #ifdef _UNICODE
pBuf = malloc(len + 1); pBuf = malloc(len + 1);
@ -225,19 +224,19 @@ VOID ConPrintf(LPTSTR szFormat, va_list arg_ptr, DWORD nStdHandle)
#else #else
pBuf = szOut; pBuf = szOut;
#endif #endif
WriteFile (GetStdHandle (nStdHandle), WriteFile (GetStdHandle (nStdHandle),
pBuf, pBuf,
len, len,
&dwWritten, &dwWritten,
NULL); NULL);
#ifdef _UNICODE #ifdef _UNICODE
free(pBuf); free(pBuf);
#endif #endif
} }
INT ConPrintfPaging(BOOL NewPage, LPTSTR szFormat, va_list arg_ptr, DWORD nStdHandle) INT ConPrintfPaging(BOOL NewPage, LPTSTR szFormat, va_list arg_ptr, DWORD nStdHandle)
{ {
INT len; INT len;
@ -245,89 +244,89 @@ INT ConPrintfPaging(BOOL NewPage, LPTSTR szFormat, va_list arg_ptr, DWORD nStdHa
CONSOLE_SCREEN_BUFFER_INFO csbi; CONSOLE_SCREEN_BUFFER_INFO csbi;
TCHAR szOut[OUTPUT_BUFFER_SIZE]; TCHAR szOut[OUTPUT_BUFFER_SIZE];
DWORD dwWritten; DWORD dwWritten;
/* used to count number of lines since last pause */ /* used to count number of lines since last pause */
static int LineCount = 0; static int LineCount = 0;
/* used to see how big the screen is */ /* used to see how big the screen is */
int ScreenLines = 0; int ScreenLines = 0;
/* the number of chars in a roow */ /* the number of chars in a roow */
int ScreenCol = 0; int ScreenCol = 0;
/* chars since start of line */ /* chars since start of line */
int CharSL = 0; int CharSL = 0;
int i = 0; int i = 0;
if(NewPage == TRUE) if(NewPage == TRUE)
LineCount = 0; LineCount = 0;
/* rest LineCount and return if no string have been given */ /* rest LineCount and return if no string have been given */
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 (!GetConsoleScreenBufferInfo(hConsole, &csbi)) if (!GetConsoleScreenBufferInfo(hConsole, &csbi))
{ {
// we assuming its a file handle // we assuming its 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;
ScreenCol = (csbi.srWindow.Right - csbi.srWindow.Left) + 1; ScreenCol = (csbi.srWindow.Right - csbi.srWindow.Left) + 1;
//make sure they didnt make the screen to small //make sure they didnt make the screen to small
if(ScreenLines<4) if(ScreenLines<4)
{ {
ConPrintf(szFormat, arg_ptr, nStdHandle); ConPrintf(szFormat, arg_ptr, nStdHandle);
return 0; return 0;
} }
len = _vstprintf (szOut, szFormat, arg_ptr); len = _vstprintf (szOut, szFormat, arg_ptr);
pBuf = szOut; pBuf = szOut;
for(i = 0; i < len; i++) for(i = 0; i < len; i++)
{ {
// search 'end of string' '\n' or 'end of screen line' // search 'end of string' '\n' or 'end of screen line'
for(; (i < len) && (pBuf[i] != _T('\n') && (CharSL<ScreenCol)) ; i++) for(; (i < len) && (pBuf[i] != _T('\n') && (CharSL<ScreenCol)) ; i++)
CharSL++; CharSL++;
WriteFile (GetStdHandle (nStdHandle),&pBuf[i-CharSL],sizeof(CHAR)*(CharSL+1),&dwWritten,NULL); WriteFile (GetStdHandle (nStdHandle),&pBuf[i-CharSL],sizeof(CHAR)*(CharSL+1),&dwWritten,NULL);
LineCount++; LineCount++;
CharSL=0; CharSL=0;
if(LineCount >= ScreenLines) if(LineCount >= ScreenLines)
{ {
if(_tcsnicmp(&pBuf[i], _T("\n"), 2)!=0) if(_tcsnicmp(&pBuf[i], _T("\n"), 2)!=0)
WriteFile (GetStdHandle (nStdHandle),_T("\n"),sizeof(CHAR),&dwWritten,NULL); WriteFile (GetStdHandle (nStdHandle),_T("\n"),sizeof(CHAR),&dwWritten,NULL);
if(PagePrompt() != PROMPT_YES) if(PagePrompt() != PROMPT_YES)
{ {
return 1; return 1;
} }
//reset the number of lines being printed //reset the number of lines being printed
LineCount = 0; LineCount = 0;
CharSL=0; CharSL=0;
} }
} }
#ifdef _UNICODE #ifdef _UNICODE
free(pBuf); free(pBuf);
#endif #endif
return 0; return 0;
} }
VOID ConErrFormatMessage (DWORD MessageId, ...) VOID ConErrFormatMessage (DWORD MessageId, ...)
{ {
TCHAR szMsg[RC_STRING_MAX_SIZE]; TCHAR szMsg[RC_STRING_MAX_SIZE];
DWORD ret; DWORD ret;
LPTSTR text; LPTSTR text;
va_list arg_ptr; va_list arg_ptr;
va_start (arg_ptr, MessageId); va_start (arg_ptr, MessageId);
ret = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, ret = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL, NULL,
@ -336,7 +335,7 @@ VOID ConErrFormatMessage (DWORD MessageId, ...)
(LPTSTR) &text, (LPTSTR) &text,
0, 0,
&arg_ptr); &arg_ptr);
va_end (arg_ptr); va_end (arg_ptr);
if(ret > 0) if(ret > 0)
{ {
@ -349,14 +348,14 @@ VOID ConErrFormatMessage (DWORD MessageId, ...)
ConErrPrintf(szMsg); ConErrPrintf(szMsg);
} }
} }
VOID ConOutFormatMessage (DWORD MessageId, ...) VOID ConOutFormatMessage (DWORD MessageId, ...)
{ {
TCHAR szMsg[RC_STRING_MAX_SIZE]; TCHAR szMsg[RC_STRING_MAX_SIZE];
DWORD ret; DWORD ret;
LPTSTR text; LPTSTR text;
va_list arg_ptr; va_list arg_ptr;
va_start (arg_ptr, MessageId); va_start (arg_ptr, MessageId);
ret = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, ret = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL, NULL,
@ -365,7 +364,7 @@ VOID ConOutFormatMessage (DWORD MessageId, ...)
(LPTSTR) &text, (LPTSTR) &text,
0, 0,
&arg_ptr); &arg_ptr);
va_end (arg_ptr); va_end (arg_ptr);
if(ret > 0) if(ret > 0)
{ {
@ -378,60 +377,60 @@ VOID ConOutFormatMessage (DWORD MessageId, ...)
ConErrPrintf(szMsg); ConErrPrintf(szMsg);
} }
} }
VOID ConOutPrintf (LPTSTR szFormat, ...) VOID ConOutPrintf (LPTSTR szFormat, ...)
{ {
va_list arg_ptr; va_list arg_ptr;
va_start (arg_ptr, szFormat); va_start (arg_ptr, szFormat);
ConPrintf(szFormat, arg_ptr, STD_OUTPUT_HANDLE); 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; INT iReturn;
va_list arg_ptr; va_list arg_ptr;
va_start (arg_ptr, szFormat); va_start (arg_ptr, szFormat);
iReturn = ConPrintfPaging(NewPage, szFormat, arg_ptr, STD_OUTPUT_HANDLE); iReturn = ConPrintfPaging(NewPage, szFormat, arg_ptr, STD_OUTPUT_HANDLE);
va_end (arg_ptr); va_end (arg_ptr);
return iReturn; return iReturn;
} }
VOID ConErrChar (TCHAR c) VOID ConErrChar (TCHAR c)
{ {
ConChar(c, STD_ERROR_HANDLE); ConChar(c, STD_ERROR_HANDLE);
} }
VOID ConErrResPuts (UINT resID) VOID ConErrResPuts (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_ERROR_HANDLE); ConPuts(szMsg, STD_ERROR_HANDLE);
} }
VOID ConErrPuts (LPTSTR szText) VOID ConErrPuts (LPTSTR szText)
{ {
ConPuts(szText, STD_ERROR_HANDLE); ConPuts(szText, STD_ERROR_HANDLE);
} }
VOID ConErrPrintf (LPTSTR szFormat, ...) VOID ConErrPrintf (LPTSTR szFormat, ...)
{ {
va_list arg_ptr; va_list arg_ptr;
va_start (arg_ptr, szFormat); va_start (arg_ptr, szFormat);
ConPrintf(szFormat, arg_ptr, STD_ERROR_HANDLE); ConPrintf(szFormat, arg_ptr, STD_ERROR_HANDLE);
va_end (arg_ptr); va_end (arg_ptr);
} }
#ifdef _DEBUG #ifdef _DEBUG
VOID DebugPrintf (LPTSTR szFormat, ...) VOID DebugPrintf (LPTSTR szFormat, ...)
{ {
va_list arg_ptr; va_list arg_ptr;
va_start (arg_ptr, szFormat); va_start (arg_ptr, szFormat);
ConPrintf(szFormat, arg_ptr, STD_ERROR_HANDLE); ConPrintf(szFormat, arg_ptr, STD_ERROR_HANDLE);
va_end (arg_ptr); va_end (arg_ptr);
@ -444,69 +443,69 @@ VOID DebugPrintf (LPTSTR szFormat, ...)
#endif #endif
} }
#endif /* _DEBUG */ #endif /* _DEBUG */
VOID SetCursorXY (SHORT x, SHORT y) VOID SetCursorXY (SHORT x, SHORT y)
{ {
COORD coPos; COORD coPos;
coPos.X = x; coPos.X = x;
coPos.Y = y; 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; CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo (hConsole, &csbi); GetConsoleScreenBufferInfo (hConsole, &csbi);
*x = csbi.dwCursorPosition.X; *x = csbi.dwCursorPosition.X;
*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 (hConsole, &csbi); GetConsoleScreenBufferInfo (hConsole, &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 (hConsole, &csbi); GetConsoleScreenBufferInfo (hConsole, &csbi);
return csbi.dwCursorPosition.Y; return csbi.dwCursorPosition.Y;
} }
VOID GetScreenSize (PSHORT maxx, PSHORT maxy) VOID GetScreenSize (PSHORT maxx, PSHORT maxy)
{ {
CONSOLE_SCREEN_BUFFER_INFO csbi; CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo (hConsole, &csbi); GetConsoleScreenBufferInfo (hConsole, &csbi);
if (maxx) if (maxx)
*maxx = csbi.dwSize.X; *maxx = csbi.dwSize.X;
if (maxy) if (maxy)
*maxy = csbi.dwSize.Y; *maxy = csbi.dwSize.Y;
} }
VOID SetCursorType (BOOL bInsert, BOOL bVisible) VOID SetCursorType (BOOL bInsert, BOOL bVisible)
{ {
CONSOLE_CURSOR_INFO cci; CONSOLE_CURSOR_INFO cci;
cci.dwSize = bInsert ? 10 : 99; cci.dwSize = bInsert ? 10 : 99;
cci.bVisible = bVisible; cci.bVisible = bVisible;
SetConsoleCursorInfo (GetStdHandle (STD_OUTPUT_HANDLE), &cci); SetConsoleCursorInfo (GetStdHandle (STD_OUTPUT_HANDLE), &cci);
} }
/* EOF */ /* EOF */