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