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
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2002-03-10 00:29:33 +01:00
|
|
|
*
|
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.@)
|
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
int CDECL _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;
|
|
|
|
}
|
|
|
|
|
2008-05-23 20:04:36 +02:00
|
|
|
#define NORMAL_CHAR 0
|
|
|
|
#define ALT_CHAR 1
|
|
|
|
#define CTRL_CHAR 2
|
|
|
|
#define SHIFT_CHAR 3
|
|
|
|
|
2008-05-27 14:58:52 +02:00
|
|
|
static const struct {unsigned vk; unsigned ch[4][2];} enh_map[] = {
|
2008-05-23 20:04:36 +02:00
|
|
|
{0x47, {{0xE0, 0x47}, {0x00, 0x97}, {0xE0, 0x77}, {0xE0, 0x47}}},
|
|
|
|
{0x48, {{0xE0, 0x48}, {0x00, 0x98}, {0xE0, 0x8D}, {0xE0, 0x48}}},
|
|
|
|
{0x49, {{0xE0, 0x49}, {0x00, 0x99}, {0xE0, 0x86}, {0xE0, 0x49}}},
|
|
|
|
{0x4B, {{0xE0, 0x4B}, {0x00, 0x9B}, {0xE0, 0x73}, {0xE0, 0x4B}}},
|
|
|
|
{0x4D, {{0xE0, 0x4D}, {0x00, 0x9D}, {0xE0, 0x74}, {0xE0, 0x4D}}},
|
|
|
|
{0x4F, {{0xE0, 0x4F}, {0x00, 0x9F}, {0xE0, 0x75}, {0xE0, 0x4F}}},
|
|
|
|
{0x50, {{0xE0, 0x50}, {0x00, 0xA0}, {0xE0, 0x91}, {0xE0, 0x50}}},
|
|
|
|
{0x51, {{0xE0, 0x51}, {0x00, 0xA1}, {0xE0, 0x76}, {0xE0, 0x51}}},
|
|
|
|
{0x52, {{0xE0, 0x52}, {0x00, 0xA2}, {0xE0, 0x92}, {0xE0, 0x52}}},
|
|
|
|
{0x53, {{0xE0, 0x53}, {0x00, 0xA3}, {0xE0, 0x93}, {0xE0, 0x53}}},
|
|
|
|
};
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _getch (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
int CDECL _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))
|
|
|
|
{
|
2008-10-21 22:44:27 +02:00
|
|
|
unsigned int i;
|
2001-01-11 00:59:25 +01:00
|
|
|
/* Only interested in ASCII chars */
|
|
|
|
if (ir.EventType == KEY_EVENT &&
|
2008-05-23 20:04:36 +02:00
|
|
|
ir.Event.KeyEvent.bKeyDown)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2008-05-23 20:04:36 +02:00
|
|
|
if (ir.Event.KeyEvent.uChar.AsciiChar)
|
|
|
|
{
|
|
|
|
retval = ir.Event.KeyEvent.uChar.AsciiChar;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
for (i = 0; i < sizeof(enh_map) / sizeof(enh_map[0]); i++)
|
|
|
|
{
|
|
|
|
if (ir.Event.KeyEvent.wVirtualScanCode == enh_map[i].vk) break;
|
|
|
|
}
|
|
|
|
if (i < sizeof(enh_map) / sizeof(enh_map[0]))
|
|
|
|
{
|
|
|
|
unsigned idx;
|
|
|
|
|
2008-06-01 22:04:15 +02:00
|
|
|
if (ir.Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED))
|
2008-05-23 20:04:36 +02:00
|
|
|
idx = ALT_CHAR;
|
2008-06-01 22:04:15 +02:00
|
|
|
else if (ir.Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED) )
|
2008-05-23 20:04:36 +02:00
|
|
|
idx = CTRL_CHAR;
|
2008-06-01 22:04:15 +02:00
|
|
|
else if (ir.Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED)
|
2008-05-23 20:04:36 +02:00
|
|
|
idx = SHIFT_CHAR;
|
|
|
|
else
|
|
|
|
idx = NORMAL_CHAR;
|
|
|
|
|
|
|
|
retval = enh_map[i].ch[idx][0];
|
|
|
|
__MSVCRT_console_buffer = enh_map[i].ch[idx][1];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
WARN("Unmapped char keyState=%x vk=%x\n",
|
|
|
|
ir.Event.KeyEvent.dwControlKeyState, ir.Event.KeyEvent.wVirtualScanCode);
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
} while(1);
|
|
|
|
if (mode)
|
|
|
|
SetConsoleMode(MSVCRT_console_in, mode);
|
|
|
|
}
|
|
|
|
UNLOCK_CONSOLE;
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _putch (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
int CDECL _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.@)
|
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
int CDECL _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.@)
|
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
char* CDECL _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.@)
|
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
int CDECL _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.@)
|
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
int CDECL _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;
|
|
|
|
}
|
|
|
|
}
|
2006-08-09 01:17:41 +02:00
|
|
|
MSVCRT_free(ir);
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
UNLOCK_CONSOLE;
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _cprintf (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
int CDECL _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
|
|
|
|
*/
|
2006-01-24 12:21:58 +01:00
|
|
|
while ((written = MSVCRT_vsnprintf( 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);
|
2007-07-03 23:56:55 +02:00
|
|
|
if (!(mem = MSVCRT_malloc(resize)))
|
2001-01-11 00:59:25 +01:00
|
|
|
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;
|
|
|
|
}
|