2 * CONSOLE.C - console input/output functions.
7 * 20-Jan-1999 (Eric Kohl)
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
16 * 02-Feb-2007 (Paolo Devoti) <devotip at gmail.com>)
17 * Fixed ConPrintfPaging
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
))
92 VOID
ConInString (LPTSTR lpInput
, DWORD dwLength
)
103 pBuf
= (PCHAR
)cmd_alloc(dwLength
);
107 ZeroMemory (lpInput
, dwLength
* sizeof(TCHAR
));
108 hFile
= GetStdHandle (STD_INPUT_HANDLE
);
109 GetConsoleMode (hFile
, &dwOldMode
);
111 SetConsoleMode (hFile
, ENABLE_LINE_INPUT
| ENABLE_ECHO_INPUT
);
113 ReadFile (hFile
, (PVOID
)pBuf
, dwLength
, &dwRead
, NULL
);
116 MultiByteToWideChar( InputCodePage
, 0, pBuf
, dwLength
+ 1, lpInput
, dwLength
+ 1);
119 for (i
= 0; i
< dwRead
; i
++, p
++)
121 if (*p
== _T('\x0d'))
132 SetConsoleMode (hFile
, dwOldMode
);
135 static VOID
ConChar(TCHAR c
, DWORD nStdHandle
)
144 WideCharToMultiByte( OutputCodePage
, 0, ws
, 2, as
, 2, NULL
, NULL
);
149 WriteFile (GetStdHandle (nStdHandle
),
156 VOID
ConOutChar (TCHAR c
)
158 ConChar(c
, STD_OUTPUT_HANDLE
);
161 VOID
ConPuts(LPTSTR szText
, DWORD nStdHandle
)
167 len
= _tcslen(szText
);
169 pBuf
= cmd_alloc(len
+ 1);
170 len
= WideCharToMultiByte( OutputCodePage
, 0, szText
, len
+ 1, pBuf
, len
+ 1, NULL
, NULL
) - 1;
174 WriteFile (GetStdHandle (nStdHandle
),
179 WriteFile (GetStdHandle (nStdHandle
),
189 VOID
ConOutResPaging(BOOL NewPage
, UINT resID
)
191 TCHAR szMsg
[RC_STRING_MAX_SIZE
];
192 LoadString(CMD_ModuleHandle
, resID
, szMsg
, RC_STRING_MAX_SIZE
);
193 ConOutPrintfPaging(NewPage
, szMsg
);
196 VOID
ConOutResPuts (UINT resID
)
198 TCHAR szMsg
[RC_STRING_MAX_SIZE
];
199 LoadString(CMD_ModuleHandle
, resID
, szMsg
, RC_STRING_MAX_SIZE
);
201 ConPuts(szMsg
, STD_OUTPUT_HANDLE
);
204 VOID
ConOutPuts (LPTSTR szText
)
206 ConPuts(szText
, STD_OUTPUT_HANDLE
);
210 VOID
ConPrintf(LPTSTR szFormat
, va_list arg_ptr
, DWORD nStdHandle
)
214 TCHAR szOut
[OUTPUT_BUFFER_SIZE
];
217 len
= _vstprintf (szOut
, szFormat
, arg_ptr
);
219 pBuf
= cmd_alloc(len
+ 1);
220 len
= WideCharToMultiByte( OutputCodePage
, 0, szOut
, len
+ 1, pBuf
, len
+ 1, NULL
, NULL
) - 1;
225 WriteFile (GetStdHandle (nStdHandle
),
237 INT
ConPrintfPaging(BOOL NewPage
, LPTSTR szFormat
, va_list arg_ptr
, DWORD nStdHandle
)
241 CONSOLE_SCREEN_BUFFER_INFO csbi
;
242 TCHAR szOut
[OUTPUT_BUFFER_SIZE
];
245 /* used to count number of lines since last pause */
246 static int LineCount
= 0;
248 /* used to see how big the screen is */
251 /* the number of chars in a roow */
254 /* chars since start of line */
262 /* rest LineCount and return if no string have been given */
263 if (szFormat
== NULL
)
267 //get the size of the visual screen that can be printed too
268 if (!GetConsoleScreenBufferInfo(hConsole
, &csbi
))
270 // we assuming its a file handle
271 ConPrintf(szFormat
, arg_ptr
, nStdHandle
);
274 //subtract 2 to account for "press any key..." and for the blank line at the end of PagePrompt()
275 ScreenLines
= (csbi
.srWindow
.Bottom
- csbi
.srWindow
.Top
) - 4;
276 ScreenCol
= (csbi
.srWindow
.Right
- csbi
.srWindow
.Left
) + 1;
278 //make sure they didnt make the screen to small
281 ConPrintf(szFormat
, arg_ptr
, nStdHandle
);
285 len
= _vstprintf (szOut
, szFormat
, arg_ptr
);
287 pBuf
= cmd_alloc(len
+ 1);
288 len
= WideCharToMultiByte( OutputCodePage
, 0, szOut
, len
+ 1, pBuf
, len
+ 1, NULL
, NULL
) - 1;
293 for(i
= 0; i
< len
; i
++)
295 // search 'end of string' '\n' or 'end of screen line'
296 for(; (i
< len
) && (pBuf
[i
] != _T('\n') && (CharSL
<ScreenCol
)) ; i
++)
299 WriteFile (GetStdHandle (nStdHandle
),&pBuf
[i
-CharSL
],sizeof(CHAR
)*(CharSL
+1),&dwWritten
,NULL
);
303 if(LineCount
>= ScreenLines
)
305 if(_strnicmp(&pBuf
[i
], "\n", 2)!=0)
306 WriteFile (GetStdHandle (nStdHandle
),_T("\n"),sizeof(CHAR
),&dwWritten
,NULL
);
308 if(PagePrompt() != PROMPT_YES
)
315 //reset the number of lines being printed
328 VOID
ConErrFormatMessage (DWORD MessageId
, ...)
330 TCHAR szMsg
[RC_STRING_MAX_SIZE
];
335 va_start (arg_ptr
, MessageId
);
336 ret
= FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
,
339 MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
),
352 LoadString(CMD_ModuleHandle
, STRING_CONSOLE_ERROR
, szMsg
, RC_STRING_MAX_SIZE
);
357 VOID
ConOutFormatMessage (DWORD MessageId
, ...)
359 TCHAR szMsg
[RC_STRING_MAX_SIZE
];
364 va_start (arg_ptr
, MessageId
);
365 ret
= FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
,
368 MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
),
381 LoadString(CMD_ModuleHandle
, STRING_CONSOLE_ERROR
, szMsg
, RC_STRING_MAX_SIZE
);
386 VOID
ConOutPrintf (LPTSTR szFormat
, ...)
390 va_start (arg_ptr
, szFormat
);
391 ConPrintf(szFormat
, arg_ptr
, STD_OUTPUT_HANDLE
);
395 INT
ConOutPrintfPaging (BOOL NewPage
, LPTSTR szFormat
, ...)
400 va_start (arg_ptr
, szFormat
);
401 iReturn
= ConPrintfPaging(NewPage
, szFormat
, arg_ptr
, STD_OUTPUT_HANDLE
);
406 VOID
ConErrChar (TCHAR c
)
408 ConChar(c
, STD_ERROR_HANDLE
);
412 VOID
ConErrResPuts (UINT resID
)
414 TCHAR szMsg
[RC_STRING_MAX_SIZE
];
415 LoadString(CMD_ModuleHandle
, resID
, szMsg
, RC_STRING_MAX_SIZE
);
416 ConPuts(szMsg
, STD_ERROR_HANDLE
);
419 VOID
ConErrPuts (LPTSTR szText
)
421 ConPuts(szText
, STD_ERROR_HANDLE
);
425 VOID
ConErrPrintf (LPTSTR szFormat
, ...)
429 va_start (arg_ptr
, szFormat
);
430 ConPrintf(szFormat
, arg_ptr
, STD_ERROR_HANDLE
);
435 VOID
DebugPrintf (LPTSTR szFormat
, ...)
439 va_start (arg_ptr
, szFormat
);
440 ConPrintf(szFormat
, arg_ptr
, STD_ERROR_HANDLE
);
443 TCHAR szOut
[OUTPUT_BUFFER_SIZE
];
444 va_start (arg_ptr
, szFormat
);
445 _vstprintf (szOut
, szFormat
, arg_ptr
);
446 OutputDebugString (szOut
);
452 VOID
SetCursorXY (SHORT x
, SHORT y
)
458 SetConsoleCursorPosition (GetStdHandle (STD_OUTPUT_HANDLE
), coPos
);
462 VOID
GetCursorXY (PSHORT x
, PSHORT y
)
464 CONSOLE_SCREEN_BUFFER_INFO csbi
;
466 GetConsoleScreenBufferInfo (hConsole
, &csbi
);
468 *x
= csbi
.dwCursorPosition
.X
;
469 *y
= csbi
.dwCursorPosition
.Y
;
473 SHORT
GetCursorX (VOID
)
475 CONSOLE_SCREEN_BUFFER_INFO csbi
;
477 GetConsoleScreenBufferInfo (hConsole
, &csbi
);
479 return csbi
.dwCursorPosition
.X
;
483 SHORT
GetCursorY (VOID
)
485 CONSOLE_SCREEN_BUFFER_INFO csbi
;
487 GetConsoleScreenBufferInfo (hConsole
, &csbi
);
489 return csbi
.dwCursorPosition
.Y
;
493 VOID
GetScreenSize (PSHORT maxx
, PSHORT maxy
)
495 CONSOLE_SCREEN_BUFFER_INFO csbi
;
497 GetConsoleScreenBufferInfo (hConsole
, &csbi
);
500 *maxx
= csbi
.dwSize
.X
;
502 *maxy
= csbi
.dwSize
.Y
;
506 VOID
SetCursorType (BOOL bInsert
, BOOL bVisible
)
508 CONSOLE_CURSOR_INFO cci
;
510 cci
.dwSize
= bInsert
? 10 : 99;
511 cci
.bVisible
= bVisible
;
513 SetConsoleCursorInfo (GetStdHandle (STD_OUTPUT_HANDLE
), &cci
);