2001-01-11 00:59:25 +01:00
|
|
|
/*
|
|
|
|
* msvcrt.dll console functions
|
|
|
|
*
|
|
|
|
* Copyright 2000 Jon Griffiths
|
|
|
|
*
|
2002-03-10 00:29:33 +01:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*
|
2001-01-11 00:59:25 +01:00
|
|
|
* Note: init and free don't need MT locking since they are called at DLL
|
|
|
|
* (de)attachment time, which is syncronised for us
|
|
|
|
*/
|
2004-06-25 03:19:15 +02:00
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
#include "msvcrt.h"
|
|
|
|
#include "wincon.h"
|
2002-02-21 21:22:00 +01:00
|
|
|
#include "mtdll.h"
|
2002-01-22 01:57:16 +01:00
|
|
|
#include "wine/debug.h"
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2001-04-11 01:25:25 +02:00
|
|
|
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/* MT */
|
2002-02-21 21:22:00 +01:00
|
|
|
#define LOCK_CONSOLE _mlock(_CONIO_LOCK)
|
|
|
|
#define UNLOCK_CONSOLE _munlock(_CONIO_LOCK)
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
static HANDLE MSVCRT_console_in = INVALID_HANDLE_VALUE;
|
|
|
|
static HANDLE MSVCRT_console_out= INVALID_HANDLE_VALUE;
|
|
|
|
static int __MSVCRT_console_buffer = MSVCRT_EOF;
|
|
|
|
|
|
|
|
/* INTERNAL: Initialise console handles */
|
2001-04-10 23:16:07 +02:00
|
|
|
void msvcrt_init_console(void)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
TRACE(":Opening console handles\n");
|
|
|
|
|
2005-06-27 11:54:09 +02:00
|
|
|
MSVCRT_console_in = CreateFileA("CONIN$", GENERIC_READ, FILE_SHARE_READ,
|
|
|
|
NULL, OPEN_EXISTING, 0, NULL);
|
2001-01-11 00:59:25 +01:00
|
|
|
MSVCRT_console_out= CreateFileA("CONOUT$", GENERIC_WRITE, FILE_SHARE_WRITE,
|
2003-01-15 00:43:41 +01:00
|
|
|
NULL, OPEN_EXISTING, 0, NULL);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
if ((MSVCRT_console_in == INVALID_HANDLE_VALUE) ||
|
|
|
|
(MSVCRT_console_out== INVALID_HANDLE_VALUE))
|
|
|
|
WARN(":Console handle Initialisation FAILED!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* INTERNAL: Free console handles */
|
2001-04-10 23:16:07 +02:00
|
|
|
void msvcrt_free_console(void)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
TRACE(":Closing console handles\n");
|
|
|
|
CloseHandle(MSVCRT_console_in);
|
|
|
|
CloseHandle(MSVCRT_console_out);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _cputs (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int _cputs(const char* str)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
DWORD count;
|
|
|
|
int retval = MSVCRT_EOF;
|
|
|
|
|
|
|
|
LOCK_CONSOLE;
|
|
|
|
if (WriteConsoleA(MSVCRT_console_out, str, strlen(str), &count, NULL)
|
|
|
|
&& count == 1)
|
|
|
|
retval = 0;
|
|
|
|
UNLOCK_CONSOLE;
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _getch (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int _getch(void)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
int retval = MSVCRT_EOF;
|
|
|
|
|
|
|
|
LOCK_CONSOLE;
|
|
|
|
if (__MSVCRT_console_buffer != MSVCRT_EOF)
|
|
|
|
{
|
|
|
|
retval = __MSVCRT_console_buffer;
|
|
|
|
__MSVCRT_console_buffer = MSVCRT_EOF;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
INPUT_RECORD ir;
|
|
|
|
DWORD count;
|
|
|
|
DWORD mode = 0;
|
|
|
|
|
|
|
|
GetConsoleMode(MSVCRT_console_in, &mode);
|
|
|
|
if(mode)
|
|
|
|
SetConsoleMode(MSVCRT_console_in, 0);
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (ReadConsoleInputA(MSVCRT_console_in, &ir, 1, &count))
|
|
|
|
{
|
|
|
|
/* Only interested in ASCII chars */
|
|
|
|
if (ir.EventType == KEY_EVENT &&
|
|
|
|
ir.Event.KeyEvent.bKeyDown &&
|
|
|
|
ir.Event.KeyEvent.uChar.AsciiChar)
|
|
|
|
{
|
|
|
|
retval = ir.Event.KeyEvent.uChar.AsciiChar;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
} while(1);
|
|
|
|
if (mode)
|
|
|
|
SetConsoleMode(MSVCRT_console_in, mode);
|
|
|
|
}
|
|
|
|
UNLOCK_CONSOLE;
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _putch (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int _putch(int c)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
int retval = MSVCRT_EOF;
|
|
|
|
DWORD count;
|
|
|
|
LOCK_CONSOLE;
|
|
|
|
if (WriteConsoleA(MSVCRT_console_out, &c, 1, &count, NULL) && count == 1)
|
|
|
|
retval = c;
|
|
|
|
UNLOCK_CONSOLE;
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _getche (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int _getche(void)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
int retval;
|
|
|
|
LOCK_CONSOLE;
|
2001-04-10 23:16:07 +02:00
|
|
|
retval = _getch();
|
2001-01-11 00:59:25 +01:00
|
|
|
if (retval != MSVCRT_EOF)
|
2001-04-10 23:16:07 +02:00
|
|
|
retval = _putch(retval);
|
2001-01-11 00:59:25 +01:00
|
|
|
UNLOCK_CONSOLE;
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _cgets (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
char* _cgets(char* str)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
char *buf = str + 2;
|
2005-10-29 12:29:24 +02:00
|
|
|
DWORD got;
|
|
|
|
DWORD conmode = 0;
|
|
|
|
|
|
|
|
TRACE("(%p)\n", str);
|
2001-01-11 00:59:25 +01:00
|
|
|
str[1] = 0; /* Length */
|
|
|
|
LOCK_CONSOLE;
|
2005-10-29 12:29:24 +02:00
|
|
|
GetConsoleMode(MSVCRT_console_in, &conmode);
|
|
|
|
SetConsoleMode(MSVCRT_console_in, ENABLE_LINE_INPUT|ENABLE_ECHO_INPUT|ENABLE_PROCESSED_INPUT);
|
|
|
|
|
|
|
|
if(ReadConsoleA(MSVCRT_console_in, buf, str[0], &got, NULL)) {
|
|
|
|
if(buf[got-2] == '\r') {
|
|
|
|
buf[got-2] = 0;
|
|
|
|
str[1] = got-2;
|
|
|
|
}
|
|
|
|
else if(got == 1 && buf[got-1] == '\n') {
|
|
|
|
buf[0] = 0;
|
|
|
|
str[1] = 0;
|
|
|
|
}
|
|
|
|
else if(got == str[0] && buf[got-1] == '\r') {
|
|
|
|
buf[got-1] = 0;
|
|
|
|
str[1] = got-1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
str[1] = got;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
buf = NULL;
|
|
|
|
SetConsoleMode(MSVCRT_console_in, conmode);
|
2001-01-11 00:59:25 +01:00
|
|
|
UNLOCK_CONSOLE;
|
2005-10-29 12:29:24 +02:00
|
|
|
return buf;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _ungetch (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int _ungetch(int c)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
int retval = MSVCRT_EOF;
|
|
|
|
LOCK_CONSOLE;
|
|
|
|
if (c != MSVCRT_EOF && __MSVCRT_console_buffer == MSVCRT_EOF)
|
|
|
|
retval = __MSVCRT_console_buffer = c;
|
|
|
|
UNLOCK_CONSOLE;
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _kbhit (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int _kbhit(void)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
int retval = 0;
|
|
|
|
|
|
|
|
LOCK_CONSOLE;
|
|
|
|
if (__MSVCRT_console_buffer != MSVCRT_EOF)
|
|
|
|
retval = 1;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* FIXME: There has to be a faster way than this in Win32.. */
|
|
|
|
INPUT_RECORD *ir = NULL;
|
|
|
|
DWORD count = 0, i;
|
|
|
|
|
|
|
|
GetNumberOfConsoleInputEvents(MSVCRT_console_in, &count);
|
|
|
|
|
|
|
|
if (count && (ir = MSVCRT_malloc(count * sizeof(INPUT_RECORD))) &&
|
|
|
|
PeekConsoleInputA(MSVCRT_console_in, ir, count, &count))
|
|
|
|
for(i = 0; i < count - 1; i++)
|
|
|
|
{
|
|
|
|
if (ir[i].EventType == KEY_EVENT &&
|
|
|
|
ir[i].Event.KeyEvent.bKeyDown &&
|
|
|
|
ir[i].Event.KeyEvent.uChar.AsciiChar)
|
|
|
|
{
|
|
|
|
retval = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ir)
|
|
|
|
MSVCRT_free(ir);
|
|
|
|
}
|
|
|
|
UNLOCK_CONSOLE;
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _cprintf (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int _cprintf(const char* format, ...)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
char buf[2048], *mem = buf;
|
|
|
|
int written, resize = sizeof(buf), retval;
|
|
|
|
va_list valist;
|
|
|
|
|
|
|
|
va_start( valist, format );
|
|
|
|
/* There are two conventions for snprintf failing:
|
|
|
|
* Return -1 if we truncated, or
|
|
|
|
* Return the number of bytes that would have been written
|
|
|
|
* The code below handles both cases
|
|
|
|
*/
|
2001-04-11 01:25:25 +02:00
|
|
|
while ((written = _snprintf( mem, resize, format, valist )) == -1 ||
|
2001-01-11 00:59:25 +01:00
|
|
|
written > resize)
|
|
|
|
{
|
|
|
|
resize = (written == -1 ? resize * 2 : written + 1);
|
|
|
|
if (mem != buf)
|
|
|
|
MSVCRT_free (mem);
|
|
|
|
if (!(mem = (char *)MSVCRT_malloc(resize)))
|
|
|
|
return MSVCRT_EOF;
|
|
|
|
va_start( valist, format );
|
|
|
|
}
|
|
|
|
va_end(valist);
|
|
|
|
LOCK_CONSOLE;
|
2001-04-10 23:16:07 +02:00
|
|
|
retval = _cputs( mem );
|
2001-01-11 00:59:25 +01:00
|
|
|
UNLOCK_CONSOLE;
|
|
|
|
if (mem != buf)
|
|
|
|
MSVCRT_free (mem);
|
|
|
|
return retval;
|
|
|
|
}
|