ReactOS 0.4.15-dev-8002-gbbb3b00
console.c
Go to the documentation of this file.
1/*
2 * CONSOLE.C - console input/output functions.
3 *
4 *
5 * History:
6 *
7 * 20-Jan-1999 (Eric Kohl)
8 * started
9 *
10 * 03-Apr-2005 (Magnus Olsen <magnus@greatlord.com>)
11 * Remove all hardcoded strings in En.rc
12 *
13 * 01-Jul-2005 (Brandon Turner <turnerb7@msu.edu>)
14 * Added ConPrintfPaging and ConOutPrintfPaging
15 *
16 * 02-Feb-2007 (Paolo Devoti <devotip at gmail.com>)
17 * Fixed ConPrintfPaging
18 */
19
20#include "precomp.h"
21
22#define OUTPUT_BUFFER_SIZE 4096
23
24/* Cache codepage for text streams */
27
28/* Global console Screen and Pager */
31
32
33
34/********************* Console STREAM IN utility functions ********************/
35
37{
39 DWORD dwMode;
40
41 GetConsoleMode(hInput, &dwMode);
42 dwMode &= ~ENABLE_PROCESSED_INPUT;
43 SetConsoleMode(hInput, dwMode);
44}
45
47{
49 DWORD dwMode;
50
51 GetConsoleMode(hInput, &dwMode);
52 dwMode |= ENABLE_PROCESSED_INPUT;
53 SetConsoleMode(hInput, dwMode);
54}
55
57{
59}
60
62{
64 DWORD dwRead;
65
66 if (hInput == INVALID_HANDLE_VALUE)
67 WARN ("Invalid input handle!!!\n");
68
69 do
70 {
71 ReadConsoleInput(hInput, lpBuffer, 1, &dwRead);
72 if (lpBuffer->EventType == KEY_EVENT &&
73 lpBuffer->Event.KeyEvent.bKeyDown)
74 {
75 break;
76 }
77 }
78 while (TRUE);
79}
80
82{
83 DWORD dwOldMode;
84 DWORD dwRead = 0;
86
87 LPTSTR p;
88 PCHAR pBuf;
89
90#ifdef _UNICODE
91 pBuf = (PCHAR)cmd_alloc(dwLength - 1);
92#else
93 pBuf = lpInput;
94#endif
95 ZeroMemory(lpInput, dwLength * sizeof(TCHAR));
97 GetConsoleMode(hFile, &dwOldMode);
98
100
101 ReadFile(hFile, (PVOID)pBuf, dwLength - 1, &dwRead, NULL);
102
103#ifdef _UNICODE
104 MultiByteToWideChar(InputCodePage, 0, pBuf, dwRead, lpInput, dwLength - 1);
105 cmd_free(pBuf);
106#endif
107 for (p = lpInput; *p; p++)
108 {
109 if (*p == _T('\r')) // Terminate at the carriage-return.
110 {
111 *p = _T('\0');
112 break;
113 }
114 }
115
116 SetConsoleMode(hFile, dwOldMode);
117}
118
119
120
121/******************** Console STREAM OUT utility functions ********************/
122
124{
125 ConWrite(StdOut, &c, 1);
126}
127
129{
130 ConWrite(StdErr, &c, 1);
131}
132
134{
135 INT Len;
136 va_list arg_ptr;
137
138 va_start(arg_ptr, MessageId);
141 NULL,
142 MessageId,
144 &arg_ptr);
145 va_end(arg_ptr);
146
147 if (Len <= 0)
149}
150
151
152
153/************************** Console PAGER functions ***************************/
154
155BOOL ConPrintfVPaging(PCON_PAGER Pager, BOOL StartPaging, LPTSTR szFormat, va_list arg_ptr)
156{
157 // INT len;
159
160 /* Return if no string has been given */
161 if (szFormat == NULL)
162 return TRUE;
163
164 /*len =*/ _vstprintf(szOut, szFormat, arg_ptr);
165
166 // return ConPutsPaging(Pager, PagePrompt, StartPaging, szOut);
167 return ConWritePaging(Pager, PagePrompt, StartPaging,
168 szOut, wcslen(szOut));
169}
170
171BOOL __cdecl ConOutPrintfPaging(BOOL StartPaging, LPTSTR szFormat, ...)
172{
173 BOOL bRet;
174 va_list arg_ptr;
175
176 va_start(arg_ptr, szFormat);
177 bRet = ConPrintfVPaging(&StdOutPager, StartPaging, szFormat, arg_ptr);
178 va_end(arg_ptr);
179 return bRet;
180}
181
182VOID ConOutResPaging(BOOL StartPaging, UINT resID)
183{
184 ConResPaging(&StdOutPager, PagePrompt, StartPaging, resID);
185}
186
187
188
189/************************** Console SCREEN functions **************************/
190
192{
193 COORD coPos;
194
195 coPos.X = x;
196 coPos.Y = y;
198}
199
201{
203
205
206 *x = csbi.dwCursorPosition.X;
207 *y = csbi.dwCursorPosition.Y;
208}
209
211{
213
215 return csbi.dwCursorPosition.X;
216}
217
219{
221
223 return csbi.dwCursorPosition.Y;
224}
225
227{
229
230 cci.dwSize = bInsert ? 10 : 99;
231 cci.bVisible = bVisible;
232
234}
235
237{
239
241 {
242 csbi.dwSize.X = 80;
243 csbi.dwSize.Y = 25;
244 }
245
246 if (maxx) *maxx = csbi.dwSize.X;
247 if (maxy) *maxy = csbi.dwSize.Y;
248}
249
250
251
252
253#ifdef INCLUDE_CMD_COLOR
254
256{
258 HANDLE hConsole;
260
261 /* Do not modify *pwDefAttr if we fail, in which case use default attributes */
262
263 hConsole = CreateFile(_T("CONOUT$"), GENERIC_READ|GENERIC_WRITE,
265 OPEN_EXISTING, 0, NULL);
266 if (hConsole == INVALID_HANDLE_VALUE)
267 return FALSE; // No default console
268
269 Success = GetConsoleScreenBufferInfo(hConsole, &csbi);
270 if (Success)
271 *pwDefAttr = csbi.wAttributes;
272
273 CloseHandle(hConsole);
274 return Success;
275}
276
277#endif
278
279
280BOOL ConSetTitle(IN LPCTSTR lpConsoleTitle)
281{
282 /* Now really set the console title */
283 return SetConsoleTitle(lpConsoleTitle);
284}
285
286#ifdef INCLUDE_CMD_BEEP
288{
289#if 0
290 /* Emit an error beep sound */
291 if (IsConsoleHandle(hOutput))
292 Beep(800, 200);
293 else if (IsTTYHandle(hOutput))
294 ConOutPuts(_T("\a")); // BEL character 0x07
295 else
296#endif
297 MessageBeep(-1);
298}
299#endif
300
301#ifdef INCLUDE_CMD_COLOR
303{
304 DWORD dwWritten;
306 COORD coPos;
307
308 /* Foreground and Background colors can't be the same */
309 if ((wColor & 0x0F) == (wColor & 0xF0) >> 4)
310 return FALSE;
311
312 /* Fill the whole background if needed */
313 if (bFill)
314 {
315 GetConsoleScreenBufferInfo(hOutput, &csbi);
316
317 coPos.X = 0;
318 coPos.Y = 0;
320 wColor & 0x00FF,
321 csbi.dwSize.X * csbi.dwSize.Y,
322 coPos,
323 &dwWritten);
324 }
325
326 /* Set the text attribute */
327 SetConsoleTextAttribute(hOutput, wColor & 0x00FF);
328 return TRUE;
329}
330#endif
331
332#include <cjkcode.h>
333#include "wcwidth.c"
334
335// NOTE: The check against 0x80 is to avoid calling the helper function
336// for characters that we already know are not full-width.
337#define IS_FULL_WIDTH(wch) \
338 (((USHORT)(wch) >= 0x0080) && (mk_wcwidth_cjk(wch) == 2))
339
341{
342 SIZE_T ich, cxWidth;
343
345 return _tcslen(pszText);
346
347 for (ich = cxWidth = 0; pszText[ich]; ++ich)
348 {
349 if (IS_FULL_WIDTH(pszText[ich]))
350 cxWidth += 2;
351 else
352 ++cxWidth;
353 }
354
355 return cxWidth;
356}
357
359{
360 int cchMax;
361 PWSTR pszWide;
362 SIZE_T cxWidth;
363
364 cchMax = MultiByteToWideChar(OutputCodePage, 0, pszText, -1, NULL, 0);
365 pszWide = cmd_alloc(cchMax * sizeof(WCHAR));
366 MultiByteToWideChar(OutputCodePage, 0, pszText, -1, pszWide, cchMax);
367 cxWidth = ConGetTextWidthW(pszWide);
368 cmd_free(pszWide);
369 return cxWidth;
370}
371
372/* EOF */
UINT cchMax
#define __cdecl
Definition: accygwin.h:79
char * va_list
Definition: acmsvcex.h:78
#define va_end(ap)
Definition: acmsvcex.h:90
#define va_start(ap, A)
Definition: acmsvcex.h:91
#define STRING_CONSOLE_ERROR
Definition: resource.h:6
#define StdOut
Definition: fc.c:14
void ConResPrintf(FILE *fp, UINT nID,...)
Definition: fc.c:33
#define StdErr
Definition: fc.c:15
BOOL WINAPI FlushConsoleInputBuffer(IN HANDLE hConsoleInput)
Definition: console.c:220
BOOL WINAPI SetConsoleCursorPosition(IN HANDLE hConsoleOutput, IN COORD dwCursorPosition)
Definition: console.c:641
HANDLE WINAPI GetStdHandle(IN DWORD nStdHandle)
Definition: console.c:203
BOOL WINAPI SetConsoleTextAttribute(IN HANDLE hConsoleOutput, IN WORD wAttributes)
Definition: console.c:672
BOOL WINAPI SetConsoleCursorInfo(IN HANDLE hConsoleOutput, IN const CONSOLE_CURSOR_INFO *lpConsoleCursorInfo)
Definition: console.c:618
BOOL WINAPI FillConsoleOutputAttribute(IN HANDLE hConsoleOutput, IN WORD wAttribute, IN DWORD nLength, IN COORD dwWriteCoord, OUT LPDWORD lpNumberOfAttrsWritten)
Definition: console.c:525
BOOL WINAPI GetConsoleScreenBufferInfo(IN HANDLE hConsoleOutput, OUT PCONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo)
Definition: console.c:595
WORD wColor
VOID ConOutChar(TCHAR c)
Definition: console.c:123
VOID SetCursorXY(SHORT x, SHORT y)
Definition: console.c:191
VOID __cdecl ConFormatMessage(PCON_STREAM Stream, DWORD MessageId,...)
Definition: console.c:133
SHORT GetCursorY(VOID)
Definition: console.c:218
UINT InputCodePage
Definition: console.c:25
BOOL ConSetScreenColor(HANDLE hOutput, WORD wColor, BOOL bFill)
Definition: console.c:302
BOOL __cdecl ConOutPrintfPaging(BOOL StartPaging, LPTSTR szFormat,...)
Definition: console.c:171
VOID ConOutResPaging(BOOL StartPaging, UINT resID)
Definition: console.c:182
VOID ConInDisable(VOID)
Definition: console.c:36
SHORT GetCursorX(VOID)
Definition: console.c:210
#define OUTPUT_BUFFER_SIZE
Definition: console.c:22
VOID ConInEnable(VOID)
Definition: console.c:46
VOID GetCursorXY(PSHORT x, PSHORT y)
Definition: console.c:200
VOID ConRingBell(HANDLE hOutput)
Definition: console.c:287
SIZE_T ConGetTextWidthW(PCWSTR pszText)
Definition: console.c:340
UINT OutputCodePage
Definition: console.c:26
VOID ConInKey(PINPUT_RECORD lpBuffer)
Definition: console.c:61
BOOL ConSetTitle(IN LPCTSTR lpConsoleTitle)
Definition: console.c:280
CON_PAGER StdOutPager
Definition: console.c:30
VOID ConInString(LPTSTR lpInput, DWORD dwLength)
Definition: console.c:81
VOID GetScreenSize(PSHORT maxx, PSHORT maxy)
Definition: console.c:236
BOOL ConGetDefaultAttributes(PWORD pwDefAttr)
Definition: console.c:255
BOOL ConPrintfVPaging(PCON_PAGER Pager, BOOL StartPaging, LPTSTR szFormat, va_list arg_ptr)
Definition: console.c:155
CON_SCREEN StdOutScreen
Definition: console.c:29
#define IS_FULL_WIDTH(wch)
Definition: console.c:337
VOID ConErrChar(TCHAR c)
Definition: console.c:128
SIZE_T ConGetTextWidthA(PCSTR pszText)
Definition: console.c:358
VOID ConInFlush(VOID)
Definition: console.c:56
VOID SetCursorType(BOOL bInsert, BOOL bVisible)
Definition: console.c:226
#define ConOutPuts(szStr)
Definition: console.h:29
#define WARN(fmt,...)
Definition: debug.h:112
#define IsCJKCodePage(CodePage)
Definition: cjkcode.h:27
#define cmd_free(ptr)
Definition: cmddbg.h:31
#define cmd_alloc(size)
Definition: cmddbg.h:29
SHORT maxx
Definition: cmdinput.c:115
static BOOL bInsert
Definition: cmdinput.c:121
SHORT maxy
Definition: cmdinput.c:116
static TAGREF LPCWSTR LPDWORD LPVOID lpBuffer
Definition: db.cpp:175
#define Len
Definition: deflate.h:82
BOOL WINAPI Beep(IN DWORD dwFreq, IN DWORD dwDuration)
Definition: deviceio.c:48
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define CloseHandle
Definition: compat.h:739
#define OPEN_EXISTING
Definition: compat.h:775
#define ReadFile(a, b, c, d, e)
Definition: compat.h:742
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
#define GENERIC_READ
Definition: compat.h:135
#define MultiByteToWideChar
Definition: compat.h:110
#define FILE_SHARE_READ
Definition: compat.h:136
static DWORD DWORD * dwLength
Definition: fusion.c:86
BOOL WINAPI GetConsoleMode(HANDLE hConsoleHandle, LPDWORD lpMode)
Definition: console.c:1569
BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleMode(HANDLE hConsoleHandle, DWORD dwMode)
Definition: console.c:1606
@ Success
Definition: eventcreate.c:712
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
unsigned short WORD
Definition: ntddk_ex.h:93
GLint GLint GLint GLint GLint x
Definition: gl.h:1548
GLint GLint GLint GLint GLint GLint y
Definition: gl.h:1548
const GLubyte * c
Definition: glext.h:8905
GLfloat GLfloat p
Definition: glext.h:8902
#define _vstprintf
Definition: tchar.h:540
_CRTIMP size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
#define PCHAR
Definition: match.c:90
static IStream Stream
Definition: htmldoc.c:1115
static BOOL __stdcall PagePrompt(PCON_PAGER Pager, DWORD Done, DWORD Total)
Definition: more.c:160
_In_ HANDLE hFile
Definition: mswsock.h:90
unsigned int UINT
Definition: ndis.h:50
#define FILE_SHARE_WRITE
Definition: nt_native.h:681
#define GENERIC_WRITE
Definition: nt_native.h:90
INT __stdcall ConWrite(IN PCON_STREAM Stream, IN PCTCH szStr, IN DWORD len)
Definition: outstream.c:85
INT ConMsgPrintfV(IN PCON_STREAM Stream, IN DWORD dwFlags, IN LPCVOID lpSource OPTIONAL, IN DWORD dwMessageId, IN DWORD dwLanguageId, IN va_list *Arguments OPTIONAL)
Definition: outstream.c:1030
#define INIT_CON_PAGER(pScreen)
Definition: pager.h:73
WORD * PWORD
Definition: pedump.c:67
short SHORT
Definition: pedump.c:59
#define INIT_CON_SCREEN(pStream)
Definition: screen.h:49
#define IsConsoleHandle(h)
Definition: console.h:14
BOOL ConResPaging(IN PCON_PAGER Pager, IN PAGE_PROMPT PagePrompt, IN BOOL StartPaging, IN UINT uID)
Definition: pager.c:681
BOOL ConWritePaging(IN PCON_PAGER Pager, IN PAGE_PROMPT PagePrompt, IN BOOL StartPaging, IN PCTCH szStr, IN DWORD len)
Definition: pager.c:549
BOOL IsTTYHandle(IN HANDLE hHandle)
Definition: utils.c:393
Definition: bl.h:1338
ULONG Y
Definition: bl.h:1340
ULONG X
Definition: bl.h:1339
SHORT Y
Definition: blue.h:27
SHORT X
Definition: blue.h:26
#define LANG_USER_DEFAULT
Definition: tnerror.cpp:50
uint16_t * PWSTR
Definition: typedefs.h:56
const uint16_t * PCWSTR
Definition: typedefs.h:57
int16_t * PSHORT
Definition: typedefs.h:55
ULONG_PTR SIZE_T
Definition: typedefs.h:80
int32_t INT
Definition: typedefs.h:58
const char * PCSTR
Definition: typedefs.h:52
#define IN
Definition: typedefs.h:39
char * PCHAR
Definition: typedefs.h:51
#define _T(x)
Definition: vfdio.h:22
#define ZeroMemory
Definition: winbase.h:1712
#define STD_OUTPUT_HANDLE
Definition: winbase.h:268
#define STD_INPUT_HANDLE
Definition: winbase.h:267
#define FORMAT_MESSAGE_FROM_SYSTEM
Definition: winbase.h:423
#define CreateFile
Definition: winbase.h:3749
#define ReadConsoleInput
Definition: wincon.h:778
#define ENABLE_ECHO_INPUT
Definition: wincon.h:80
#define KEY_EVENT
Definition: wincon.h:128
#define SetConsoleTitle
Definition: wincon.h:783
#define ENABLE_LINE_INPUT
Definition: wincon.h:79
#define ENABLE_PROCESSED_INPUT
Definition: wincon.h:78
BOOL WINAPI MessageBeep(_In_ UINT uType)
char TCHAR
Definition: xmlstorage.h:189
__wchar_t WCHAR
Definition: xmlstorage.h:180
const CHAR * LPCTSTR
Definition: xmlstorage.h:193
CHAR * LPTSTR
Definition: xmlstorage.h:192
#define _tcslen
Definition: xmlstorage.h:198