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/stdio.h"
14 #include "msvcrt/stdlib.h"
16 DEFAULT_DEBUG_CHANNEL(msvcrt);
21 extern CRITICAL_SECTION MSVCRT_console_cs;
22 #define LOCK_CONSOLE EnterCriticalSection(&MSVCRT_console_cs)
23 #define UNLOCK_CONSOLE LeaveCriticalSection(&MSVCRT_console_cs)
25 static HANDLE MSVCRT_console_in = INVALID_HANDLE_VALUE;
26 static HANDLE MSVCRT_console_out= INVALID_HANDLE_VALUE;
27 static int __MSVCRT_console_buffer = MSVCRT_EOF;
29 /* INTERNAL: Initialise console handles */
30 void msvcrt_init_console(void)
32 TRACE(":Opening console handles\n");
34 MSVCRT_console_in = GetStdHandle(STD_INPUT_HANDLE);
36 /* FIXME: Should be initialised with:
37 * CreateFileA("CONIN$", GENERIC_READ, FILE_SHARE_READ,
38 * NULL, OPEN_EXISTING, 0, (HANDLE)NULL);
41 MSVCRT_console_out= CreateFileA("CONOUT$", GENERIC_WRITE, FILE_SHARE_WRITE,
42 NULL, OPEN_EXISTING, 0, (HANDLE)NULL);
44 if ((MSVCRT_console_in == INVALID_HANDLE_VALUE) ||
45 (MSVCRT_console_out== INVALID_HANDLE_VALUE))
46 WARN(":Console handle Initialisation FAILED!\n");
49 /* INTERNAL: Free console handles */
50 void msvcrt_free_console(void)
52 TRACE(":Closing console handles\n");
53 CloseHandle(MSVCRT_console_in);
54 CloseHandle(MSVCRT_console_out);
57 /*********************************************************************
60 int _cputs(const char* str)
63 int retval = MSVCRT_EOF;
66 if (WriteConsoleA(MSVCRT_console_out, str, strlen(str), &count, NULL)
73 /*********************************************************************
78 int retval = MSVCRT_EOF;
81 if (__MSVCRT_console_buffer != MSVCRT_EOF)
83 retval = __MSVCRT_console_buffer;
84 __MSVCRT_console_buffer = MSVCRT_EOF;
92 GetConsoleMode(MSVCRT_console_in, &mode);
94 SetConsoleMode(MSVCRT_console_in, 0);
97 if (ReadConsoleInputA(MSVCRT_console_in, &ir, 1, &count))
99 /* Only interested in ASCII chars */
100 if (ir.EventType == KEY_EVENT &&
101 ir.Event.KeyEvent.bKeyDown &&
102 ir.Event.KeyEvent.uChar.AsciiChar)
104 retval = ir.Event.KeyEvent.uChar.AsciiChar;
112 SetConsoleMode(MSVCRT_console_in, mode);
118 /*********************************************************************
123 int retval = MSVCRT_EOF;
126 if (WriteConsoleA(MSVCRT_console_out, &c, 1, &count, NULL) && count == 1)
132 /*********************************************************************
140 if (retval != MSVCRT_EOF)
141 retval = _putch(retval);
146 /*********************************************************************
149 char* _cgets(char* str)
153 str[1] = 0; /* Length */
154 /* FIXME: No editing of string supported */
158 if (str[1] >= str[0] || (str[1]++, c = _getche()) == MSVCRT_EOF || c == '\n')
167 /*********************************************************************
168 * _ungetch (MSVCRT.@)
172 int retval = MSVCRT_EOF;
174 if (c != MSVCRT_EOF && __MSVCRT_console_buffer == MSVCRT_EOF)
175 retval = __MSVCRT_console_buffer = c;
180 /*********************************************************************
183 int _cscanf(const char* format, ...)
185 /* NOTE: If you extend this function, extend MSVCRT_fscanf in file.c too */
189 if (!*format) return 0;
190 WARN("\"%s\": semi-stub\n", format);
191 va_start(ap, format);
195 if (*format == ' ') {
196 /* skip whitespace */
197 while ((nch!=MSVCRT_EOF) && isspace(nch))
200 else if (*format == '%') {
204 case 'd': { /* read an integer */
205 int*val = va_arg(ap, int*);
207 /* skip initial whitespace */
208 while ((nch!=MSVCRT_EOF) && isspace(nch))
210 /* get sign and first digit */
222 /* read until no more digits */
223 while ((nch!=MSVCRT_EOF) && isdigit(nch)) {
224 cur = cur*10 + (nch - '0');
231 case 'f': { /* read a float */
232 float*val = va_arg(ap, float*);
234 /* skip initial whitespace */
235 while ((nch!=MSVCRT_EOF) && isspace(nch))
237 /* get sign and first digit */
248 /* read until no more digits */
249 while ((nch!=MSVCRT_EOF) && isdigit(nch)) {
250 cur = cur*10 + (nch - '0');
254 /* handle decimals */
257 while ((nch!=MSVCRT_EOF) && isdigit(nch)) {
259 cur += dec * (nch - '0');
267 case 's': { /* read a word */
268 char*str = va_arg(ap, char*);
270 /* skip initial whitespace */
271 while ((nch!=MSVCRT_EOF) && isspace(nch))
273 /* read until whitespace */
274 while ((nch!=MSVCRT_EOF) && !isspace(nch)) {
280 TRACE("read word: %s\n", str);
283 default: FIXME("unhandled: %%%c\n", *format);
289 /* check for character match */
296 if (nch != MSVCRT_EOF)
300 TRACE("returning %d\n", rd);
304 /*********************************************************************
312 if (__MSVCRT_console_buffer != MSVCRT_EOF)
316 /* FIXME: There has to be a faster way than this in Win32.. */
317 INPUT_RECORD *ir = NULL;
320 GetNumberOfConsoleInputEvents(MSVCRT_console_in, &count);
322 if (count && (ir = MSVCRT_malloc(count * sizeof(INPUT_RECORD))) &&
323 PeekConsoleInputA(MSVCRT_console_in, ir, count, &count))
324 for(i = 0; i < count - 1; i++)
326 if (ir[i].EventType == KEY_EVENT &&
327 ir[i].Event.KeyEvent.bKeyDown &&
328 ir[i].Event.KeyEvent.uChar.AsciiChar)
342 /*********************************************************************
343 * _cprintf (MSVCRT.@)
345 int _cprintf(const char* format, ...)
347 char buf[2048], *mem = buf;
348 int written, resize = sizeof(buf), retval;
351 va_start( valist, format );
352 /* There are two conventions for snprintf failing:
353 * Return -1 if we truncated, or
354 * Return the number of bytes that would have been written
355 * The code below handles both cases
357 while ((written = _snprintf( mem, resize, format, valist )) == -1 ||
360 resize = (written == -1 ? resize * 2 : written + 1);
363 if (!(mem = (char *)MSVCRT_malloc(resize)))
365 va_start( valist, format );
369 retval = _cputs( mem );