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