2 * CONSOLE.C - console input/output functions.
7 * 20-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
10 * 03-Apr-2005 (Magnus Olsen) <magnus@greatlord.com>)
11 * Remove all hardcode string to En.rc
13 * 01-Jul-2005 (Brandon Turner) <turnerb7@msu.edu>)
14 * Added ConPrintfPaging and ConOutPrintfPaging
23 #define OUTPUT_BUFFER_SIZE 4096
30 VOID
ConInDisable (VOID
)
32 HANDLE hInput
= GetStdHandle (STD_INPUT_HANDLE
);
35 GetConsoleMode (hInput
, &dwMode
);
36 dwMode
&= ~ENABLE_PROCESSED_INPUT
;
37 SetConsoleMode (hInput
, dwMode
);
41 VOID
ConInEnable (VOID
)
43 HANDLE hInput
= GetStdHandle (STD_INPUT_HANDLE
);
46 GetConsoleMode (hInput
, &dwMode
);
47 dwMode
|= ENABLE_PROCESSED_INPUT
;
48 SetConsoleMode (hInput
, dwMode
);
52 VOID
ConInDummy (VOID
)
54 HANDLE hInput
= GetStdHandle (STD_INPUT_HANDLE
);
59 if (hInput
== INVALID_HANDLE_VALUE
)
60 DebugPrintf (_T("Invalid input handle!!!\n"));
62 ReadConsoleInput (hInput
, &dummy
, 1, &dwRead
);
65 VOID
ConInFlush (VOID
)
67 FlushConsoleInputBuffer (GetStdHandle (STD_INPUT_HANDLE
));
71 VOID
ConInKey (PINPUT_RECORD lpBuffer
)
73 HANDLE hInput
= GetStdHandle (STD_INPUT_HANDLE
);
77 if (hInput
== INVALID_HANDLE_VALUE
)
78 DebugPrintf (_T("Invalid input handle!!!\n"));
83 ReadConsoleInput (hInput
, lpBuffer
, 1, &dwRead
);
84 if ((lpBuffer
->EventType
== KEY_EVENT
) &&
85 (lpBuffer
->Event
.KeyEvent
.bKeyDown
== TRUE
))
93 VOID
ConInString (LPTSTR lpInput
, DWORD dwLength
)
104 pBuf
= (PCHAR
)malloc(dwLength
);
108 ZeroMemory (lpInput
, dwLength
* sizeof(TCHAR
));
109 hFile
= GetStdHandle (STD_INPUT_HANDLE
);
110 GetConsoleMode (hFile
, &dwOldMode
);
112 SetConsoleMode (hFile
, ENABLE_LINE_INPUT
| ENABLE_ECHO_INPUT
);
114 ReadFile (hFile
, (PVOID
)pBuf
, dwLength
, &dwRead
, NULL
);
117 MultiByteToWideChar( InputCodePage
, 0, pBuf
, dwLength
+ 1, lpInput
, dwLength
+ 1);
120 for (i
= 0; i
< dwRead
; i
++, p
++)
122 if (*p
== _T('\x0d'))
133 SetConsoleMode (hFile
, dwOldMode
);
136 static VOID
ConChar(TCHAR c
, DWORD nStdHandle
)
145 WideCharToMultiByte( OutputCodePage
, 0, ws
, 2, as
, 2, NULL
, NULL
);
150 WriteFile (GetStdHandle (nStdHandle
),
157 VOID
ConOutChar (TCHAR c
)
159 ConChar(c
, STD_OUTPUT_HANDLE
);
162 VOID
ConPuts(LPTSTR szText
, DWORD nStdHandle
)
168 len
= _tcslen(szText
);
170 pBuf
= malloc(len
+ 1);
171 len
= WideCharToMultiByte( OutputCodePage
, 0, szText
, len
+ 1, pBuf
, len
+ 1, NULL
, NULL
) - 1;
175 WriteFile (GetStdHandle (nStdHandle
),
180 WriteFile (GetStdHandle (nStdHandle
),
190 VOID
ConOutResPaging(BOOL NewPage
, UINT resID
)
192 TCHAR szMsg
[RC_STRING_MAX_SIZE
];
193 LoadString(CMD_ModuleHandle
, resID
, szMsg
, RC_STRING_MAX_SIZE
);
194 ConOutPrintfPaging(NewPage
, szMsg
);
197 VOID
ConOutResPuts (UINT resID
)
199 TCHAR szMsg
[RC_STRING_MAX_SIZE
];
200 LoadString(CMD_ModuleHandle
, resID
, szMsg
, RC_STRING_MAX_SIZE
);
202 ConPuts(szMsg
, STD_OUTPUT_HANDLE
);
205 VOID
ConOutPuts (LPTSTR szText
)
207 ConPuts(szText
, STD_OUTPUT_HANDLE
);
211 VOID
ConPrintf(LPTSTR szFormat
, va_list arg_ptr
, DWORD nStdHandle
)
215 TCHAR szOut
[OUTPUT_BUFFER_SIZE
];
218 len
= _vstprintf (szOut
, szFormat
, arg_ptr
);
220 pBuf
= malloc(len
+ 1);
221 len
= WideCharToMultiByte( OutputCodePage
, 0, szOut
, len
+ 1, pBuf
, len
+ 1, NULL
, NULL
) - 1;
226 WriteFile (GetStdHandle (nStdHandle
),
238 INT
ConPrintfPaging(BOOL NewPage
, LPTSTR szFormat
, va_list arg_ptr
, DWORD nStdHandle
)
242 CONSOLE_SCREEN_BUFFER_INFO csbi
;
243 TCHAR szOut
[OUTPUT_BUFFER_SIZE
];
246 /* used to count number of lines since last pause */
247 static int LineCount
= 0;
249 /* used to see how big the screen is */
252 /* the number of chars in a roow */
255 /* chars since end of line */
263 /* rest LineCount and return if no string have been given */
264 if (szFormat
== NULL
)
268 //get the size of the visual screen that can be printed too
269 if (!GetConsoleScreenBufferInfo(hConsole
, &csbi
))
271 // we assuming its a file handle
272 ConPrintf(szFormat
, arg_ptr
, nStdHandle
);
275 //subtract 2 to account for "press any key..." and for the blank line at the end of PagePrompt()
276 ScreenLines
= (csbi
.srWindow
.Bottom
- csbi
.srWindow
.Top
) - 4;
277 ScreenCol
= (csbi
.srWindow
.Right
- csbi
.srWindow
.Left
) + 1;
279 //make sure they didnt make the screen to small
282 ConPrintf(szFormat
, arg_ptr
, nStdHandle
);
286 len
= _vstprintf (szOut
, szFormat
, arg_ptr
);
288 pBuf
= malloc(len
+ 1);
289 len
= WideCharToMultiByte( OutputCodePage
, 0, szOut
, len
+ 1, pBuf
, len
+ 1, NULL
, NULL
) - 1;
294 for(i
= 0; i
< len
; i
++)
297 if(pBuf
[i
] == _T('\n'))
305 if (CharEL
>=ScreenCol
)
309 if(pBuf
[i
+1] != _T('\n')) LineCount
++;
315 /* FIXME : write more that one char at time */
316 WriteFile (GetStdHandle (nStdHandle
),&pBuf
[i
],sizeof(CHAR
),&dwWritten
,NULL
);
317 if(LineCount
>= ScreenLines
)
319 if(_tcsnicmp(&pBuf
[i
], _T("\n"), 2)!=0)
320 WriteFile (GetStdHandle (nStdHandle
),_T("\n"),sizeof(CHAR
),&dwWritten
,NULL
);
322 if(PagePrompt() != PROMPT_YES
)
326 //reset the number of lines being printed
339 VOID
ConErrFormatMessage (DWORD MessageId
, ...)
341 TCHAR szMsg
[RC_STRING_MAX_SIZE
];
346 va_start (arg_ptr
, MessageId
);
347 ret
= FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
,
350 MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
),
363 LoadString(CMD_ModuleHandle
, STRING_CONSOLE_ERROR
, szMsg
, RC_STRING_MAX_SIZE
);
368 VOID
ConOutFormatMessage (DWORD MessageId
, ...)
370 TCHAR szMsg
[RC_STRING_MAX_SIZE
];
375 va_start (arg_ptr
, MessageId
);
376 ret
= FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
,
379 MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
),
392 LoadString(CMD_ModuleHandle
, STRING_CONSOLE_ERROR
, szMsg
, RC_STRING_MAX_SIZE
);
397 VOID
ConOutPrintf (LPTSTR szFormat
, ...)
401 va_start (arg_ptr
, szFormat
);
402 ConPrintf(szFormat
, arg_ptr
, STD_OUTPUT_HANDLE
);
406 INT
ConOutPrintfPaging (BOOL NewPage
, LPTSTR szFormat
, ...)
411 va_start (arg_ptr
, szFormat
);
412 iReturn
= ConPrintfPaging(NewPage
, szFormat
, arg_ptr
, STD_OUTPUT_HANDLE
);
417 VOID
ConErrChar (TCHAR c
)
419 ConChar(c
, STD_ERROR_HANDLE
);
423 VOID
ConErrResPuts (UINT resID
)
425 TCHAR szMsg
[RC_STRING_MAX_SIZE
];
426 LoadString(CMD_ModuleHandle
, resID
, szMsg
, RC_STRING_MAX_SIZE
);
427 ConPuts(szMsg
, STD_ERROR_HANDLE
);
430 VOID
ConErrPuts (LPTSTR szText
)
432 ConPuts(szText
, STD_ERROR_HANDLE
);
436 VOID
ConErrPrintf (LPTSTR szFormat
, ...)
440 va_start (arg_ptr
, szFormat
);
441 ConPrintf(szFormat
, arg_ptr
, STD_ERROR_HANDLE
);
446 VOID
DebugPrintf (LPTSTR szFormat
, ...)
450 va_start (arg_ptr
, szFormat
);
451 ConPrintf(szFormat
, arg_ptr
, STD_ERROR_HANDLE
);
454 TCHAR szOut
[OUTPUT_BUFFER_SIZE
];
455 va_start (arg_ptr
, szFormat
);
456 _vstprintf (szOut
, szFormat
, arg_ptr
);
457 OutputDebugString (szOut
);
463 VOID
SetCursorXY (SHORT x
, SHORT y
)
469 SetConsoleCursorPosition (GetStdHandle (STD_OUTPUT_HANDLE
), coPos
);
473 VOID
GetCursorXY (PSHORT x
, PSHORT y
)
475 CONSOLE_SCREEN_BUFFER_INFO csbi
;
477 GetConsoleScreenBufferInfo (hConsole
, &csbi
);
479 *x
= csbi
.dwCursorPosition
.X
;
480 *y
= csbi
.dwCursorPosition
.Y
;
484 SHORT
GetCursorX (VOID
)
486 CONSOLE_SCREEN_BUFFER_INFO csbi
;
488 GetConsoleScreenBufferInfo (hConsole
, &csbi
);
490 return csbi
.dwCursorPosition
.X
;
494 SHORT
GetCursorY (VOID
)
496 CONSOLE_SCREEN_BUFFER_INFO csbi
;
498 GetConsoleScreenBufferInfo (hConsole
, &csbi
);
500 return csbi
.dwCursorPosition
.Y
;
504 VOID
GetScreenSize (PSHORT maxx
, PSHORT maxy
)
506 CONSOLE_SCREEN_BUFFER_INFO csbi
;
508 GetConsoleScreenBufferInfo (hConsole
, &csbi
);
511 *maxx
= csbi
.dwSize
.X
;
513 *maxy
= csbi
.dwSize
.Y
;
517 VOID
SetCursorType (BOOL bInsert
, BOOL bVisible
)
519 CONSOLE_CURSOR_INFO cci
;
521 cci
.dwSize
= bInsert
? 10 : 99;
522 cci
.bVisible
= bVisible
;
524 SetConsoleCursorInfo (GetStdHandle (STD_OUTPUT_HANDLE
), &cci
);