2 * msvcrt.dll console functions
4 * Copyright 2000 Jon Griffiths
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Note: init and free don't need MT locking since they are called at DLL
21 * (de)attachment time, which is syncronised for us
26 #include "msvcrt/conio.h"
27 #include "msvcrt/malloc.h"
28 #include "msvcrt/stdio.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
38 #define LOCK_CONSOLE _mlock(_CONIO_LOCK)
39 #define UNLOCK_CONSOLE _munlock(_CONIO_LOCK)
41 static HANDLE MSVCRT_console_in = INVALID_HANDLE_VALUE;
42 static HANDLE MSVCRT_console_out= INVALID_HANDLE_VALUE;
43 static int __MSVCRT_console_buffer = MSVCRT_EOF;
45 /* INTERNAL: Initialise console handles */
46 void msvcrt_init_console(void)
48 TRACE(":Opening console handles\n");
50 MSVCRT_console_in = GetStdHandle(STD_INPUT_HANDLE);
52 /* FIXME: Should be initialised with:
53 * CreateFileA("CONIN$", GENERIC_READ, FILE_SHARE_READ,
54 * NULL, OPEN_EXISTING, 0, (HANDLE)NULL);
57 MSVCRT_console_out= CreateFileA("CONOUT$", GENERIC_WRITE, FILE_SHARE_WRITE,
58 NULL, OPEN_EXISTING, 0, (HANDLE)NULL);
60 if ((MSVCRT_console_in == INVALID_HANDLE_VALUE) ||
61 (MSVCRT_console_out== INVALID_HANDLE_VALUE))
62 WARN(":Console handle Initialisation FAILED!\n");
65 /* INTERNAL: Free console handles */
66 void msvcrt_free_console(void)
68 TRACE(":Closing console handles\n");
69 CloseHandle(MSVCRT_console_in);
70 CloseHandle(MSVCRT_console_out);
73 /*********************************************************************
76 int _cputs(const char* str)
79 int retval = MSVCRT_EOF;
82 if (WriteConsoleA(MSVCRT_console_out, str, strlen(str), &count, NULL)
89 /*********************************************************************
94 int retval = MSVCRT_EOF;
97 if (__MSVCRT_console_buffer != MSVCRT_EOF)
99 retval = __MSVCRT_console_buffer;
100 __MSVCRT_console_buffer = MSVCRT_EOF;
108 GetConsoleMode(MSVCRT_console_in, &mode);
110 SetConsoleMode(MSVCRT_console_in, 0);
113 if (ReadConsoleInputA(MSVCRT_console_in, &ir, 1, &count))
115 /* Only interested in ASCII chars */
116 if (ir.EventType == KEY_EVENT &&
117 ir.Event.KeyEvent.bKeyDown &&
118 ir.Event.KeyEvent.uChar.AsciiChar)
120 retval = ir.Event.KeyEvent.uChar.AsciiChar;
128 SetConsoleMode(MSVCRT_console_in, mode);
134 /*********************************************************************
139 int retval = MSVCRT_EOF;
142 if (WriteConsoleA(MSVCRT_console_out, &c, 1, &count, NULL) && count == 1)
148 /*********************************************************************
156 if (retval != MSVCRT_EOF)
157 retval = _putch(retval);
162 /*********************************************************************
165 char* _cgets(char* str)
169 str[1] = 0; /* Length */
170 /* FIXME: No editing of string supported */
174 if (str[1] >= str[0] || (str[1]++, c = _getche()) == MSVCRT_EOF || c == '\n')
183 /*********************************************************************
184 * _ungetch (MSVCRT.@)
188 int retval = MSVCRT_EOF;
190 if (c != MSVCRT_EOF && __MSVCRT_console_buffer == MSVCRT_EOF)
191 retval = __MSVCRT_console_buffer = c;
196 /* helper function for _cscanf. Returns the value of character c in the
197 * given base, or -1 if the given character is not a digit of the base.
199 static int char2digit(char c, int base) {
200 if ((c>='0') && (c<='9') && (c<='0'+base-1)) return (c-'0');
201 if (base<=10) return -1;
202 if ((c>='A') && (c<='Z') && (c<='A'+base-11)) return (c-'A'+10);
203 if ((c>='a') && (c<='z') && (c<='a'+base-11)) return (c-'a'+10);
207 /*********************************************************************
210 int _cscanf(const char* format, ...)
212 /* NOTE: If you extend this function, extend MSVCRT_fscanf in file.c too */
216 if (!*format) return 0;
217 WARN("\"%s\": semi-stub\n", format);
218 va_start(ap, format);
222 /* a whitespace character in the format string causes scanf to read,
223 * but not store, all consecutive white-space characters in the input
224 * up to the next non-white-space character. One white space character
225 * in the input matches any number (including zero) and combination of
226 * white-space characters in the input. */
227 if (isspace(*format)) {
228 /* skip whitespace */
229 while ((nch!=MSVCRT_EOF) && isspace(nch))
232 /* a format specification causes scanf to read and convert characters
233 * in the input into values of a specified type. The value is assigned
234 * to an argument in the argument list. Format specifications have
235 * the form %[*][width][{h | l | I64 | L}]type */
236 /* FIXME: unimplemented: h/l/I64/L modifiers and some type specs. */
237 else if (*format == '%') {
238 int st = 0; int suppress = 0; int width = 0;
239 int base, number_signed;
241 /* look for leading asterisk, which means 'suppress assignment of
247 /* look for width specification */
248 while (isdigit(*format)) {
250 width+=*format++ - '0';
252 if (width==0) width=-1; /* no width spec seen */
254 case '%': /* read a percent symbol */
259 case 'X': /* hexadecimal integer. */
260 base = 16; number_signed = 0;
262 case 'o': /* octal integer */
263 base = 8; number_signed = 0;
265 case 'u': /* unsigned decimal integer */
266 base = 10; number_signed = 0;
268 case 'd': /* signed decimal integer */
269 base = 10; number_signed = 1;
271 case 'i': /* generic integer */
272 base = 0; number_signed = 1;
274 /* read an integer */
275 int*val = suppress ? NULL : va_arg(ap, int*);
276 int cur = 0; int negative = 0; int seendigit=0;
277 /* skip initial whitespace */
278 while ((nch!=MSVCRT_EOF) && isspace(nch))
281 if (number_signed && (nch == '-' || nch == '+')) {
282 negative = (nch=='-');
284 if (width>0) width--;
286 /* look for leading indication of base */
287 if (width!=0 && nch == '0') {
289 if (width>0) width--;
291 if (width!=0 && (nch=='x' || nch=='X')) {
296 if (width>0) width--;
304 /* throw away leading zeros */
305 while (width!=0 && nch=='0') {
307 if (width>0) width--;
310 /* get first digit. Keep working copy negative, as the
311 * range of negative numbers in two's complement notation
312 * is one larger than the range of positive numbers. */
313 if (width!=0 && char2digit(nch, base)!=-1) {
314 cur = -char2digit(nch, base);
316 if (width>0) width--;
319 /* read until no more digits */
320 while (width!=0 && (nch!=MSVCRT_EOF) && isdigit(nch)) {
321 cur = cur*base + char2digit(nch, base);
323 if (width>0) width--;
326 /* negate parsed number if non-negative */
327 if (!negative) cur=-cur;
329 if (!seendigit) break; /* not a valid number */
331 if (!suppress) *val = cur;
338 case 'G': { /* read a float */
339 float*val = suppress ? NULL : va_arg(ap, float*);
342 /* skip initial whitespace */
343 while ((nch!=MSVCRT_EOF) && isspace(nch))
346 if (nch == '-' || nch == '+') {
347 negative = (nch=='-');
348 if (width>0) width--;
352 /* get first digit. */
353 if (!isdigit(nch)) break;
354 cur = (nch - '0') * (negative ? -1 : 1);
356 if (width>0) width--;
357 /* read until no more digits */
358 while (width!=0 && (nch!=MSVCRT_EOF) && isdigit(nch)) {
359 cur = cur*10 + (nch - '0');
361 if (width>0) width--;
363 /* handle decimals */
364 if (width!=0 && nch == '.') {
367 if (width>0) width--;
368 while (width!=0 && (nch!=MSVCRT_EOF) && isdigit(nch)) {
370 cur += dec * (nch - '0');
372 if (width>0) width--;
375 /* handle exponent */
376 if (width!=0 && (nch == 'e' || nch == 'E')) {
377 int exponent = 0, negexp = 0;
380 if (width>0) width--;
381 /* possible sign on the exponent */
382 if (width!=0 && (nch=='+' || nch=='-')) {
385 if (width>0) width--;
387 /* exponent digits */
388 while (width!=0 && (nch!=MSVCRT_EOF) && isdigit(nch)) {
390 exponent += (nch - '0');
392 if (width>0) width--;
394 /* update 'cur' with this exponent. */
395 expcnt = negexp ? .1 : 10;
396 while (exponent!=0) {
400 expcnt=expcnt*expcnt;
404 if (!suppress) *val = cur;
407 case 's': { /* read a word */
408 char*str = suppress ? NULL : va_arg(ap, char*);
410 /* skip initial whitespace */
411 while ((nch!=MSVCRT_EOF) && isspace(nch))
413 /* read until whitespace */
414 while (width!=0 && (nch!=MSVCRT_EOF) && !isspace(nch)) {
415 if (!suppress) *sptr++ = nch;
418 if (width>0) width--;
421 if (!suppress) *sptr = 0;
422 TRACE("read word: %s\n", str);
425 default: FIXME("unhandled: %%%c\n", *format);
426 /* From spec: "if a percent sign is followed by a character
427 * that has no meaning as a format-control character, that
428 * character and the following characters are treated as
429 * an ordinary sequence of characters, that is, a sequence
430 * of characters that must match the input. For example,
431 * to specify that a percent-sign character is to be input,
433 * LEAVING AS-IS because we catch bugs better that way. */
435 if (st && !suppress) rd++;
438 /* a non-white-space character causes scanf to read, but not store,
439 * a matching non-white-space character. */
441 /* check for character match */
448 if (nch != MSVCRT_EOF)
452 TRACE("returning %d\n", rd);
456 /*********************************************************************
464 if (__MSVCRT_console_buffer != MSVCRT_EOF)
468 /* FIXME: There has to be a faster way than this in Win32.. */
469 INPUT_RECORD *ir = NULL;
472 GetNumberOfConsoleInputEvents(MSVCRT_console_in, &count);
474 if (count && (ir = MSVCRT_malloc(count * sizeof(INPUT_RECORD))) &&
475 PeekConsoleInputA(MSVCRT_console_in, ir, count, &count))
476 for(i = 0; i < count - 1; i++)
478 if (ir[i].EventType == KEY_EVENT &&
479 ir[i].Event.KeyEvent.bKeyDown &&
480 ir[i].Event.KeyEvent.uChar.AsciiChar)
494 /*********************************************************************
495 * _cprintf (MSVCRT.@)
497 int _cprintf(const char* format, ...)
499 char buf[2048], *mem = buf;
500 int written, resize = sizeof(buf), retval;
503 va_start( valist, format );
504 /* There are two conventions for snprintf failing:
505 * Return -1 if we truncated, or
506 * Return the number of bytes that would have been written
507 * The code below handles both cases
509 while ((written = _snprintf( mem, resize, format, valist )) == -1 ||
512 resize = (written == -1 ? resize * 2 : written + 1);
515 if (!(mem = (char *)MSVCRT_malloc(resize)))
517 va_start( valist, format );
521 retval = _cputs( mem );