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