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
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);
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 /*********************************************************************
62 int _cputs(const char* str)
65 int retval = MSVCRT_EOF;
68 if (WriteConsoleA(MSVCRT_console_out, str, strlen(str), &count, NULL)
75 /*********************************************************************
80 int retval = MSVCRT_EOF;
83 if (__MSVCRT_console_buffer != MSVCRT_EOF)
85 retval = __MSVCRT_console_buffer;
86 __MSVCRT_console_buffer = MSVCRT_EOF;
94 GetConsoleMode(MSVCRT_console_in, &mode);
96 SetConsoleMode(MSVCRT_console_in, 0);
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;
114 SetConsoleMode(MSVCRT_console_in, mode);
120 /*********************************************************************
125 int retval = MSVCRT_EOF;
128 if (WriteConsoleA(MSVCRT_console_out, &c, 1, &count, NULL) && count == 1)
134 /*********************************************************************
142 if (retval != MSVCRT_EOF)
143 retval = _putch(retval);
148 /*********************************************************************
151 char* _cgets(char* str)
155 str[1] = 0; /* Length */
156 /* FIXME: No editing of string supported */
160 if (str[1] >= str[0] || (str[1]++, c = _getche()) == MSVCRT_EOF || c == '\n')
169 /*********************************************************************
170 * _ungetch (MSVCRT.@)
174 int retval = MSVCRT_EOF;
176 if (c != MSVCRT_EOF && __MSVCRT_console_buffer == MSVCRT_EOF)
177 retval = __MSVCRT_console_buffer = c;
182 /* helper function for _cscanf. Returns the value of character c in the
183 * given base, or -1 if the given character is not a digit of the base.
185 static int char2digit(char c, int base) {
186 if ((c>='0') && (c<='9') && (c<='0'+base-1)) return (c-'0');
187 if (base<=10) return -1;
188 if ((c>='A') && (c<='Z') && (c<='A'+base-11)) return (c-'A'+10);
189 if ((c>='a') && (c<='z') && (c<='a'+base-11)) return (c-'a'+10);
193 /*********************************************************************
196 int _cscanf(const char* format, ...)
198 /* NOTE: If you extend this function, extend MSVCRT_fscanf in file.c too */
202 if (!*format) return 0;
203 WARN("\"%s\": semi-stub\n", format);
204 va_start(ap, format);
208 /* a whitespace character in the format string causes scanf to read,
209 * but not store, all consecutive white-space characters in the input
210 * up to the next non-white-space character. One white space character
211 * in the input matches any number (including zero) and combination of
212 * white-space characters in the input. */
213 if (isspace(*format)) {
214 /* skip whitespace */
215 while ((nch!=MSVCRT_EOF) && isspace(nch))
218 /* a format specification causes scanf to read and convert characters
219 * in the input into values of a specified type. The value is assigned
220 * to an argument in the argument list. Format specifications have
221 * the form %[*][width][{h | l | I64 | L}]type */
222 /* FIXME: unimplemented: h/l/I64/L modifiers and some type specs. */
223 else if (*format == '%') {
224 int st = 0; int suppress = 0; int width = 0;
225 int base, number_signed;
227 /* look for leading asterisk, which means 'suppress assignment of
233 /* look for width specification */
234 while (isdigit(*format)) {
236 width+=*format++ - '0';
238 if (width==0) width=-1; /* no width spec seen */
240 case '%': /* read a percent symbol */
245 case 'X': /* hexadecimal integer. */
246 base = 16; number_signed = 0;
248 case 'o': /* octal integer */
249 base = 8; number_signed = 0;
251 case 'u': /* unsigned decimal integer */
252 base = 10; number_signed = 0;
254 case 'd': /* signed decimal integer */
255 base = 10; number_signed = 1;
257 case 'i': /* generic integer */
258 base = 0; number_signed = 1;
260 /* read an integer */
261 int*val = suppress ? NULL : va_arg(ap, int*);
262 int cur = 0; int negative = 0; int seendigit=0;
263 /* skip initial whitespace */
264 while ((nch!=MSVCRT_EOF) && isspace(nch))
267 if (number_signed && (nch == '-' || nch == '+')) {
268 negative = (nch=='-');
270 if (width>0) width--;
272 /* look for leading indication of base */
273 if (width!=0 && nch == '0') {
275 if (width>0) width--;
277 if (width!=0 && (nch=='x' || nch=='X')) {
282 if (width>0) width--;
290 /* throw away leading zeros */
291 while (width!=0 && nch=='0') {
293 if (width>0) width--;
296 /* get first digit. Keep working copy negative, as the
297 * range of negative numbers in two's complement notation
298 * is one larger than the range of positive numbers. */
299 if (width!=0 && char2digit(nch, base)!=-1) {
300 cur = -char2digit(nch, base);
302 if (width>0) width--;
305 /* read until no more digits */
306 while (width!=0 && (nch!=MSVCRT_EOF) && isdigit(nch)) {
307 cur = cur*base + char2digit(nch, base);
309 if (width>0) width--;
312 /* negate parsed number if non-negative */
313 if (!negative) cur=-cur;
315 if (!seendigit) break; /* not a valid number */
317 if (!suppress) *val = cur;
324 case 'G': { /* read a float */
325 float*val = suppress ? NULL : va_arg(ap, float*);
328 /* skip initial whitespace */
329 while ((nch!=MSVCRT_EOF) && isspace(nch))
332 if (nch == '-' || nch == '+') {
333 negative = (nch=='-');
334 if (width>0) width--;
338 /* get first digit. */
339 if (!isdigit(nch)) break;
340 cur = (nch - '0') * (negative ? -1 : 1);
342 if (width>0) width--;
343 /* read until no more digits */
344 while (width!=0 && (nch!=MSVCRT_EOF) && isdigit(nch)) {
345 cur = cur*10 + (nch - '0');
347 if (width>0) width--;
349 /* handle decimals */
350 if (width!=0 && nch == '.') {
353 if (width>0) width--;
354 while (width!=0 && (nch!=MSVCRT_EOF) && isdigit(nch)) {
356 cur += dec * (nch - '0');
358 if (width>0) width--;
361 /* handle exponent */
362 if (width!=0 && (nch == 'e' || nch == 'E')) {
363 int exponent = 0, negexp = 0;
366 if (width>0) width--;
367 /* possible sign on the exponent */
368 if (width!=0 && (nch=='+' || nch=='-')) {
371 if (width>0) width--;
373 /* exponent digits */
374 while (width!=0 && (nch!=MSVCRT_EOF) && isdigit(nch)) {
376 exponent += (nch - '0');
378 if (width>0) width--;
380 /* update 'cur' with this exponent. */
381 expcnt = negexp ? .1 : 10;
382 while (exponent!=0) {
386 expcnt=expcnt*expcnt;
390 if (!suppress) *val = cur;
393 case 's': { /* read a word */
394 char*str = suppress ? NULL : va_arg(ap, char*);
396 /* skip initial whitespace */
397 while ((nch!=MSVCRT_EOF) && isspace(nch))
399 /* read until whitespace */
400 while (width!=0 && (nch!=MSVCRT_EOF) && !isspace(nch)) {
401 if (!suppress) *sptr++ = nch;
404 if (width>0) width--;
407 if (!suppress) *sptr = 0;
408 TRACE("read word: %s\n", str);
411 default: FIXME("unhandled: %%%c\n", *format);
412 /* From spec: "if a percent sign is followed by a character
413 * that has no meaning as a format-control character, that
414 * character and the following characters are treated as
415 * an ordinary sequence of characters, that is, a sequence
416 * of characters that must match the input. For example,
417 * to specify that a percent-sign character is to be input,
419 * LEAVING AS-IS because we catch bugs better that way. */
421 if (st && !suppress) rd++;
424 /* a non-white-space character causes scanf to read, but not store,
425 * a matching non-white-space character. */
427 /* check for character match */
434 if (nch != MSVCRT_EOF)
438 TRACE("returning %d\n", rd);
442 /*********************************************************************
450 if (__MSVCRT_console_buffer != MSVCRT_EOF)
454 /* FIXME: There has to be a faster way than this in Win32.. */
455 INPUT_RECORD *ir = NULL;
458 GetNumberOfConsoleInputEvents(MSVCRT_console_in, &count);
460 if (count && (ir = MSVCRT_malloc(count * sizeof(INPUT_RECORD))) &&
461 PeekConsoleInputA(MSVCRT_console_in, ir, count, &count))
462 for(i = 0; i < count - 1; i++)
464 if (ir[i].EventType == KEY_EVENT &&
465 ir[i].Event.KeyEvent.bKeyDown &&
466 ir[i].Event.KeyEvent.uChar.AsciiChar)
480 /*********************************************************************
481 * _cprintf (MSVCRT.@)
483 int _cprintf(const char* format, ...)
485 char buf[2048], *mem = buf;
486 int written, resize = sizeof(buf), retval;
489 va_start( valist, format );
490 /* There are two conventions for snprintf failing:
491 * Return -1 if we truncated, or
492 * Return the number of bytes that would have been written
493 * The code below handles both cases
495 while ((written = _snprintf( mem, resize, format, valist )) == -1 ||
498 resize = (written == -1 ? resize * 2 : written + 1);
501 if (!(mem = (char *)MSVCRT_malloc(resize)))
503 va_start( valist, format );
507 retval = _cputs( mem );