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