Merge 14981:15268 from trunk
[reactos.git] / reactos / subsys / system / cmd / console.c
1 /*
2 * CONSOLE.C - console input/output functions.
3 *
4 *
5 * History:
6 *
7 * 20-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
8 * started
9 *
10 * 03-Apr-2005 (Magnus Olsen) <magnus@greatlord.com>)
11 * Remove all hardcode string to En.rc
12 */
13
14
15
16 #include "precomp.h"
17 #include "resource.h"
18
19
20 #define OUTPUT_BUFFER_SIZE 4096
21
22
23 UINT InputCodePage;
24 UINT OutputCodePage;
25
26
27 VOID ConInDisable (VOID)
28 {
29 HANDLE hInput = GetStdHandle (STD_INPUT_HANDLE);
30 DWORD dwMode;
31
32 GetConsoleMode (hInput, &dwMode);
33 dwMode &= ~ENABLE_PROCESSED_INPUT;
34 SetConsoleMode (hInput, dwMode);
35 }
36
37
38 VOID ConInEnable (VOID)
39 {
40 HANDLE hInput = GetStdHandle (STD_INPUT_HANDLE);
41 DWORD dwMode;
42
43 GetConsoleMode (hInput, &dwMode);
44 dwMode |= ENABLE_PROCESSED_INPUT;
45 SetConsoleMode (hInput, dwMode);
46 }
47
48
49 VOID ConInDummy (VOID)
50 {
51 HANDLE hInput = GetStdHandle (STD_INPUT_HANDLE);
52 INPUT_RECORD dummy;
53 DWORD dwRead;
54
55 #ifdef _DEBUG
56 if (hInput == INVALID_HANDLE_VALUE)
57 DebugPrintf (_T("Invalid input handle!!!\n"));
58 #endif /* _DEBUG */
59 ReadConsoleInput (hInput, &dummy, 1, &dwRead);
60 }
61
62 VOID ConInFlush (VOID)
63 {
64 FlushConsoleInputBuffer (GetStdHandle (STD_INPUT_HANDLE));
65 }
66
67
68 VOID ConInKey (PINPUT_RECORD lpBuffer)
69 {
70 HANDLE hInput = GetStdHandle (STD_INPUT_HANDLE);
71 DWORD dwRead;
72
73 #ifdef _DEBUG
74 if (hInput == INVALID_HANDLE_VALUE)
75 DebugPrintf (_T("Invalid input handle!!!\n"));
76 #endif /* _DEBUG */
77
78 do
79 {
80 ReadConsoleInput (hInput, lpBuffer, 1, &dwRead);
81 if ((lpBuffer->EventType == KEY_EVENT) &&
82 (lpBuffer->Event.KeyEvent.bKeyDown == TRUE))
83 break;
84 }
85 while (TRUE);
86 }
87
88
89
90 VOID ConInString (LPTSTR lpInput, DWORD dwLength)
91 {
92 DWORD dwOldMode;
93 DWORD dwRead;
94 HANDLE hFile;
95
96 LPTSTR p;
97 DWORD i;
98 PCHAR pBuf;
99
100 #ifdef _UNICODE
101 pBuf = (PCHAR)malloc(dwLength);
102 #else
103 pBuf = lpInput;
104 #endif
105 ZeroMemory (lpInput, dwLength * sizeof(TCHAR));
106 hFile = GetStdHandle (STD_INPUT_HANDLE);
107 GetConsoleMode (hFile, &dwOldMode);
108
109 SetConsoleMode (hFile, ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT);
110
111 ReadFile (hFile, (PVOID)pBuf, dwLength, &dwRead, NULL);
112
113 #ifdef _UNICODE
114 MultiByteToWideChar( InputCodePage, 0, pBuf, dwLength + 1, lpInput, dwLength + 1);
115 #endif
116 p = lpInput;
117 for (i = 0; i < dwRead; i++, p++)
118 {
119 if (*p == _T('\x0d'))
120 {
121 *p = _T('\0');
122 break;
123 }
124 }
125
126 #ifdef _UNICODE
127 free(pBuf);
128 #endif
129
130 SetConsoleMode (hFile, dwOldMode);
131 }
132
133 static VOID ConChar(TCHAR c, DWORD nStdHandle)
134 {
135 DWORD dwWritten;
136 CHAR cc;
137 #ifdef _UNICODE
138 CHAR as[2];
139 WCHAR ws[2];
140 ws[0] = c;
141 ws[1] = 0;
142 WideCharToMultiByte( OutputCodePage, 0, ws, 2, as, 2, NULL, NULL);
143 cc = as[0];
144 #else
145 cc = c;
146 #endif
147 WriteFile (GetStdHandle (nStdHandle),
148 &cc,
149 1,
150 &dwWritten,
151 NULL);
152 }
153
154 VOID ConOutChar (TCHAR c)
155 {
156 ConChar(c, STD_OUTPUT_HANDLE);
157 }
158
159 VOID ConPuts(LPTSTR szText, DWORD nStdHandle)
160 {
161 DWORD dwWritten;
162 PCHAR pBuf;
163 INT len;
164
165 len = _tcslen(szText);
166 #ifdef _UNICODE
167 pBuf = malloc(len + 1);
168 len = WideCharToMultiByte( OutputCodePage, 0, szText, len + 1, pBuf, len + 1, NULL, NULL) - 1;
169 #else
170 pBuf = szText;
171 #endif
172 WriteFile (GetStdHandle (nStdHandle),
173 pBuf,
174 len,
175 &dwWritten,
176 NULL);
177 WriteFile (GetStdHandle (nStdHandle),
178 "\n",
179 1,
180 &dwWritten,
181 NULL);
182 #ifdef UNICODE
183 free(pBuf);
184 #endif
185 }
186
187 VOID ConOutResPuts (UINT resID)
188 {
189 TCHAR szMsg[RC_STRING_MAX_SIZE];
190 LoadString(CMD_ModuleHandle, resID, szMsg, RC_STRING_MAX_SIZE);
191
192 ConPuts(szMsg, STD_OUTPUT_HANDLE);
193 }
194
195 VOID ConOutPuts (LPTSTR szText)
196 {
197 ConPuts(szText, STD_OUTPUT_HANDLE);
198 }
199
200
201 VOID ConPrintf(LPTSTR szFormat, va_list arg_ptr, DWORD nStdHandle)
202 {
203 INT len;
204 PCHAR pBuf;
205 TCHAR szOut[OUTPUT_BUFFER_SIZE];
206 DWORD dwWritten;
207
208 len = _vstprintf (szOut, szFormat, arg_ptr);
209 #ifdef _UNICODE
210 pBuf = malloc(len + 1);
211 len = WideCharToMultiByte( OutputCodePage, 0, szOut, len + 1, pBuf, len + 1, NULL, NULL) - 1;
212 #else
213 pBuf = szOut;
214 #endif
215 WriteFile (GetStdHandle (nStdHandle),
216 pBuf,
217 len,
218 &dwWritten,
219 NULL);
220 #ifdef UNICODE
221 free(pBuf);
222 #endif
223 }
224
225 VOID ConOutFormatMessage (DWORD MessageId, ...)
226 {
227 TCHAR szMsg[RC_STRING_MAX_SIZE];
228 DWORD ret;
229 LPTSTR text;
230 va_list arg_ptr;
231
232 va_start (arg_ptr, MessageId);
233 ret = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
234 NULL,
235 MessageId,
236 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
237 (LPTSTR) &text,
238 0,
239 &arg_ptr);
240
241 va_end (arg_ptr);
242 if(ret > 0)
243 {
244 ConErrPuts (text);
245 LocalFree(text);
246 }
247 else
248 {
249 LoadString(CMD_ModuleHandle, STRING_CONSOLE_ERROR, szMsg, RC_STRING_MAX_SIZE);
250 ConErrPrintf(szMsg);
251 }
252 }
253
254 VOID ConOutPrintf (LPTSTR szFormat, ...)
255 {
256 va_list arg_ptr;
257
258 va_start (arg_ptr, szFormat);
259 ConPrintf(szFormat, arg_ptr, STD_OUTPUT_HANDLE);
260 va_end (arg_ptr);
261 }
262
263 VOID ConErrChar (TCHAR c)
264 {
265 ConChar(c, STD_ERROR_HANDLE);
266 }
267
268
269 VOID ConErrResPuts (UINT resID)
270 {
271 TCHAR szMsg[RC_STRING_MAX_SIZE];
272 LoadString(CMD_ModuleHandle, resID, szMsg, RC_STRING_MAX_SIZE);
273 ConPuts(szMsg, STD_ERROR_HANDLE);
274 }
275
276 VOID ConErrPuts (LPTSTR szText)
277 {
278 ConPuts(szText, STD_ERROR_HANDLE);
279 }
280
281
282 VOID ConErrPrintf (LPTSTR szFormat, ...)
283 {
284 va_list arg_ptr;
285
286 va_start (arg_ptr, szFormat);
287 ConPrintf(szFormat, arg_ptr, STD_ERROR_HANDLE);
288 va_end (arg_ptr);
289 }
290
291 #ifdef _DEBUG
292 VOID DebugPrintf (LPTSTR szFormat, ...)
293 {
294 va_list arg_ptr;
295
296 va_start (arg_ptr, szFormat);
297 ConPrintf(szFormat, arg_ptr, STD_ERROR_HANDLE);
298 va_end (arg_ptr);
299 #if 0
300 TCHAR szOut[OUTPUT_BUFFER_SIZE];
301 va_start (arg_ptr, szFormat);
302 _vstprintf (szOut, szFormat, arg_ptr);
303 OutputDebugString (szOut);
304 va_end (arg_ptr);
305 #endif
306 }
307 #endif /* _DEBUG */
308
309 VOID SetCursorXY (SHORT x, SHORT y)
310 {
311 COORD coPos;
312
313 coPos.X = x;
314 coPos.Y = y;
315 SetConsoleCursorPosition (GetStdHandle (STD_OUTPUT_HANDLE), coPos);
316 }
317
318
319 VOID GetCursorXY (PSHORT x, PSHORT y)
320 {
321 CONSOLE_SCREEN_BUFFER_INFO csbi;
322
323 GetConsoleScreenBufferInfo (hConsole, &csbi);
324
325 *x = csbi.dwCursorPosition.X;
326 *y = csbi.dwCursorPosition.Y;
327 }
328
329
330 SHORT GetCursorX (VOID)
331 {
332 CONSOLE_SCREEN_BUFFER_INFO csbi;
333
334 GetConsoleScreenBufferInfo (hConsole, &csbi);
335
336 return csbi.dwCursorPosition.X;
337 }
338
339
340 SHORT GetCursorY (VOID)
341 {
342 CONSOLE_SCREEN_BUFFER_INFO csbi;
343
344 GetConsoleScreenBufferInfo (hConsole, &csbi);
345
346 return csbi.dwCursorPosition.Y;
347 }
348
349
350 VOID GetScreenSize (PSHORT maxx, PSHORT maxy)
351 {
352 CONSOLE_SCREEN_BUFFER_INFO csbi;
353
354 GetConsoleScreenBufferInfo (hConsole, &csbi);
355
356 if (maxx)
357 *maxx = csbi.dwSize.X;
358 if (maxy)
359 *maxy = csbi.dwSize.Y;
360 }
361
362
363 VOID SetCursorType (BOOL bInsert, BOOL bVisible)
364 {
365 CONSOLE_CURSOR_INFO cci;
366
367 cci.dwSize = bInsert ? 10 : 99;
368 cci.bVisible = bVisible;
369
370 SetConsoleCursorInfo (GetStdHandle (STD_OUTPUT_HANDLE), &cci);
371 }
372
373 /* EOF */