Make FIXMEs more grep-able.
[wine/multimedia.git] / dlls / msvcrt / console.c
blob980c69167d37a19e2a668059b5cd8a4a1c374356
1 /*
2 * msvcrt.dll console functions
4 * Copyright 2000 Jon Griffiths
6 * Note: init and free don't need MT locking since they are called at DLL
7 * (de)attachment time, which is syncronised for us
8 */
9 #include "msvcrt.h"
10 #include "wincon.h"
12 #include "msvcrt/conio.h"
13 #include "msvcrt/malloc.h"
14 #include "msvcrt/stdio.h"
16 #include "wine/debug.h"
18 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
22 /* MT */
23 extern CRITICAL_SECTION MSVCRT_console_cs;
24 #define LOCK_CONSOLE EnterCriticalSection(&MSVCRT_console_cs)
25 #define UNLOCK_CONSOLE LeaveCriticalSection(&MSVCRT_console_cs)
27 static HANDLE MSVCRT_console_in = INVALID_HANDLE_VALUE;
28 static HANDLE MSVCRT_console_out= INVALID_HANDLE_VALUE;
29 static int __MSVCRT_console_buffer = MSVCRT_EOF;
31 /* INTERNAL: Initialise console handles */
32 void msvcrt_init_console(void)
34 TRACE(":Opening console handles\n");
36 MSVCRT_console_in = GetStdHandle(STD_INPUT_HANDLE);
38 /* FIXME: Should be initialised with:
39 * CreateFileA("CONIN$", GENERIC_READ, FILE_SHARE_READ,
40 * NULL, OPEN_EXISTING, 0, (HANDLE)NULL);
43 MSVCRT_console_out= CreateFileA("CONOUT$", GENERIC_WRITE, FILE_SHARE_WRITE,
44 NULL, OPEN_EXISTING, 0, (HANDLE)NULL);
46 if ((MSVCRT_console_in == INVALID_HANDLE_VALUE) ||
47 (MSVCRT_console_out== INVALID_HANDLE_VALUE))
48 WARN(":Console handle Initialisation FAILED!\n");
51 /* INTERNAL: Free console handles */
52 void msvcrt_free_console(void)
54 TRACE(":Closing console handles\n");
55 CloseHandle(MSVCRT_console_in);
56 CloseHandle(MSVCRT_console_out);
59 /*********************************************************************
60 * _cputs (MSVCRT.@)
62 int _cputs(const char* str)
64 DWORD count;
65 int retval = MSVCRT_EOF;
67 LOCK_CONSOLE;
68 if (WriteConsoleA(MSVCRT_console_out, str, strlen(str), &count, NULL)
69 && count == 1)
70 retval = 0;
71 UNLOCK_CONSOLE;
72 return retval;
75 /*********************************************************************
76 * _getch (MSVCRT.@)
78 int _getch(void)
80 int retval = MSVCRT_EOF;
82 LOCK_CONSOLE;
83 if (__MSVCRT_console_buffer != MSVCRT_EOF)
85 retval = __MSVCRT_console_buffer;
86 __MSVCRT_console_buffer = MSVCRT_EOF;
88 else
90 INPUT_RECORD ir;
91 DWORD count;
92 DWORD mode = 0;
94 GetConsoleMode(MSVCRT_console_in, &mode);
95 if(mode)
96 SetConsoleMode(MSVCRT_console_in, 0);
98 do {
99 if (ReadConsoleInputA(MSVCRT_console_in, &ir, 1, &count))
101 /* Only interested in ASCII chars */
102 if (ir.EventType == KEY_EVENT &&
103 ir.Event.KeyEvent.bKeyDown &&
104 ir.Event.KeyEvent.uChar.AsciiChar)
106 retval = ir.Event.KeyEvent.uChar.AsciiChar;
107 break;
110 else
111 break;
112 } while(1);
113 if (mode)
114 SetConsoleMode(MSVCRT_console_in, mode);
116 UNLOCK_CONSOLE;
117 return retval;
120 /*********************************************************************
121 * _putch (MSVCRT.@)
123 int _putch(int c)
125 int retval = MSVCRT_EOF;
126 DWORD count;
127 LOCK_CONSOLE;
128 if (WriteConsoleA(MSVCRT_console_out, &c, 1, &count, NULL) && count == 1)
129 retval = c;
130 UNLOCK_CONSOLE;
131 return retval;
134 /*********************************************************************
135 * _getche (MSVCRT.@)
137 int _getche(void)
139 int retval;
140 LOCK_CONSOLE;
141 retval = _getch();
142 if (retval != MSVCRT_EOF)
143 retval = _putch(retval);
144 UNLOCK_CONSOLE;
145 return retval;
148 /*********************************************************************
149 * _cgets (MSVCRT.@)
151 char* _cgets(char* str)
153 char *buf = str + 2;
154 int c;
155 str[1] = 0; /* Length */
156 /* FIXME: No editing of string supported */
157 LOCK_CONSOLE;
160 if (str[1] >= str[0] || (str[1]++, c = _getche()) == MSVCRT_EOF || c == '\n')
161 break;
162 *buf++ = c & 0xff;
163 } while (1);
164 UNLOCK_CONSOLE;
165 *buf = '\0';
166 return str + 2;
169 /*********************************************************************
170 * _ungetch (MSVCRT.@)
172 int _ungetch(int c)
174 int retval = MSVCRT_EOF;
175 LOCK_CONSOLE;
176 if (c != MSVCRT_EOF && __MSVCRT_console_buffer == MSVCRT_EOF)
177 retval = __MSVCRT_console_buffer = c;
178 UNLOCK_CONSOLE;
179 return retval;
182 /*********************************************************************
183 * _cscanf (MSVCRT.@)
185 int _cscanf(const char* format, ...)
187 /* NOTE: If you extend this function, extend MSVCRT_fscanf in file.c too */
188 int rd = 0;
189 int nch;
190 va_list ap;
191 if (!*format) return 0;
192 WARN("\"%s\": semi-stub\n", format);
193 va_start(ap, format);
194 LOCK_CONSOLE;
195 nch = _getch();
196 while (*format) {
197 if (*format == ' ') {
198 /* skip whitespace */
199 while ((nch!=MSVCRT_EOF) && isspace(nch))
200 nch = _getch();
202 else if (*format == '%') {
203 int st = 0;
204 format++;
205 switch(*format) {
206 case 'd': { /* read an integer */
207 int*val = va_arg(ap, int*);
208 int cur = 0;
209 /* skip initial whitespace */
210 while ((nch!=MSVCRT_EOF) && isspace(nch))
211 nch = _getch();
212 /* get sign and first digit */
213 if (nch == '-') {
214 nch = _getch();
215 if (isdigit(nch))
216 cur = -(nch - '0');
217 else break;
218 } else {
219 if (isdigit(nch))
220 cur = nch - '0';
221 else break;
223 nch = _getch();
224 /* read until no more digits */
225 while ((nch!=MSVCRT_EOF) && isdigit(nch)) {
226 cur = cur*10 + (nch - '0');
227 nch = _getch();
229 st = 1;
230 *val = cur;
232 break;
233 case 'f': { /* read a float */
234 float*val = va_arg(ap, float*);
235 float cur = 0;
236 /* skip initial whitespace */
237 while ((nch!=MSVCRT_EOF) && isspace(nch))
238 nch = _getch();
239 /* get sign and first digit */
240 if (nch == '-') {
241 nch = _getch();
242 if (isdigit(nch))
243 cur = -(nch - '0');
244 else break;
245 } else {
246 if (isdigit(nch))
247 cur = nch - '0';
248 else break;
250 /* read until no more digits */
251 while ((nch!=MSVCRT_EOF) && isdigit(nch)) {
252 cur = cur*10 + (nch - '0');
253 nch = _getch();
255 if (nch == '.') {
256 /* handle decimals */
257 float dec = 1;
258 nch = _getch();
259 while ((nch!=MSVCRT_EOF) && isdigit(nch)) {
260 dec /= 10;
261 cur += dec * (nch - '0');
262 nch = _getch();
265 st = 1;
266 *val = cur;
268 break;
269 case 's': { /* read a word */
270 char*str = va_arg(ap, char*);
271 char*sptr = str;
272 /* skip initial whitespace */
273 while ((nch!=MSVCRT_EOF) && isspace(nch))
274 nch = _getch();
275 /* read until whitespace */
276 while ((nch!=MSVCRT_EOF) && !isspace(nch)) {
277 *sptr++ = nch; st++;
278 nch = _getch();
280 /* terminate */
281 *sptr = 0;
282 TRACE("read word: %s\n", str);
284 break;
285 default: FIXME("unhandled: %%%c\n", *format);
287 if (st) rd++;
288 else break;
290 else {
291 /* check for character match */
292 if (nch == *format)
293 nch = _getch();
294 else break;
296 format++;
298 if (nch != MSVCRT_EOF)
299 _ungetch(nch);
300 UNLOCK_CONSOLE;
301 va_end(ap);
302 TRACE("returning %d\n", rd);
303 return rd;
306 /*********************************************************************
307 * _kbhit (MSVCRT.@)
309 int _kbhit(void)
311 int retval = 0;
313 LOCK_CONSOLE;
314 if (__MSVCRT_console_buffer != MSVCRT_EOF)
315 retval = 1;
316 else
318 /* FIXME: There has to be a faster way than this in Win32.. */
319 INPUT_RECORD *ir = NULL;
320 DWORD count = 0, i;
322 GetNumberOfConsoleInputEvents(MSVCRT_console_in, &count);
324 if (count && (ir = MSVCRT_malloc(count * sizeof(INPUT_RECORD))) &&
325 PeekConsoleInputA(MSVCRT_console_in, ir, count, &count))
326 for(i = 0; i < count - 1; i++)
328 if (ir[i].EventType == KEY_EVENT &&
329 ir[i].Event.KeyEvent.bKeyDown &&
330 ir[i].Event.KeyEvent.uChar.AsciiChar)
332 retval = 1;
333 break;
336 if (ir)
337 MSVCRT_free(ir);
339 UNLOCK_CONSOLE;
340 return retval;
344 /*********************************************************************
345 * _cprintf (MSVCRT.@)
347 int _cprintf(const char* format, ...)
349 char buf[2048], *mem = buf;
350 int written, resize = sizeof(buf), retval;
351 va_list valist;
353 va_start( valist, format );
354 /* There are two conventions for snprintf failing:
355 * Return -1 if we truncated, or
356 * Return the number of bytes that would have been written
357 * The code below handles both cases
359 while ((written = _snprintf( mem, resize, format, valist )) == -1 ||
360 written > resize)
362 resize = (written == -1 ? resize * 2 : written + 1);
363 if (mem != buf)
364 MSVCRT_free (mem);
365 if (!(mem = (char *)MSVCRT_malloc(resize)))
366 return MSVCRT_EOF;
367 va_start( valist, format );
369 va_end(valist);
370 LOCK_CONSOLE;
371 retval = _cputs( mem );
372 UNLOCK_CONSOLE;
373 if (mem != buf)
374 MSVCRT_free (mem);
375 return retval;