windows: read console using ReadFile/ReadConsole (#3387)

pull/3394/head
vitalyster 2020-01-10 16:06:40 +03:00 committed by Alexander Medvednikov
parent 8412c6f03f
commit 66a6fa163e
4 changed files with 24 additions and 38 deletions

View File

@ -8,15 +8,10 @@ __global g_m2_ptr byteptr
fn init() {
$if windows {
if is_atty(0) > 0 {
C._setmode(C._fileno(C.stdin), C._O_U16TEXT)
if is_atty(1) > 0 {
C.SetConsoleMode(C.GetStdHandle(C.STD_OUTPUT_HANDLE), C.ENABLE_PROCESSED_OUTPUT | 0x0004) // ENABLE_VIRTUAL_TERMINAL_PROCESSING
C.setbuf(C.stdout, 0)
}
else {
C._setmode(C._fileno(C.stdin), C._O_U8TEXT)
}
C._setmode(C._fileno(C.stdout), C._O_U8TEXT)
C.SetConsoleMode(C.GetStdHandle(C.STD_OUTPUT_HANDLE), C.ENABLE_PROCESSED_OUTPUT | 0x0004) // ENABLE_VIRTUAL_TERMINAL_PROCESSING
C.setbuf(C.stdout, 0)
}
}

View File

@ -197,8 +197,6 @@ fn C.syscall() int
fn C.sysctl() int
// Windows
fn C._setmode(int, int) int
fn C._fileno(int) int
@ -252,9 +250,6 @@ fn C.SetConsoleMode()
fn C.GetConsoleMode() int
fn C._putws()
fn C.wprintf()
@ -312,9 +307,6 @@ fn C.WriteConsole() voidptr
fn C.WriteFile() voidptr
fn C.fgetws() voidptr
fn C.GetModuleFileName() int

View File

@ -60,13 +60,6 @@ const (
#include <locale.h> // tolower
#include <sys/time.h>
#include <unistd.h> // sleep
#else
#if defined(_MSC_VER)
#pragma comment(lib, "Dbghelp.lib")
#endif
#if defined(__MSVCRT_VERSION__) && __MSVCRT_VERSION__ < __MSVCR90_DLL
#error Please upgrade your MinGW distribution to use msvcr90.dll or later.
#endif
#endif
#if defined(__CYGWIN__) && !defined(_WIN32)
@ -108,13 +101,7 @@ $c_common_macros
#define UNICODE
#include <windows.h>
// must be included after <windows.h>
#ifndef __TINYC__
#include <shellapi.h>
#endif
#include <io.h> // _waccess
#include <fcntl.h> // _O_U8TEXT
#include <direct.h> // _wgetcwd
//#include <WinSock2.h>
#ifdef _MSC_VER
@ -127,6 +114,10 @@ $c_common_macros
#define EMPTY_STRUCT_DECLARATION int ____dummy_variable
#define OPTION_CAST(x)
#include <dbghelp.h>
#pragma comment(lib, "Dbghelp.lib")
#endif
#else

View File

@ -646,18 +646,26 @@ pub fn get_raw_line() string {
$if windows {
max_line_chars := 256
buf := malloc(max_line_chars * 2)
h_input := C.GetStdHandle(STD_INPUT_HANDLE)
mut bytes_read := 0
if is_atty(0) > 0 {
h_input := C.GetStdHandle(STD_INPUT_HANDLE)
mut nr_chars := u32(0)
C.ReadConsole(h_input, buf, max_line_chars * 2, voidptr(&nr_chars), 0)
return string_from_wide2(&u16(buf), int(nr_chars))
C.ReadConsole(h_input, buf, max_line_chars * 2, &bytes_read, 0)
return string_from_wide2(&u16(buf), bytes_read)
}
res := C.fgetws(&u16(buf), max_line_chars, C.stdin)
len := C.wcslen(&u16(buf))
if !isnil(res) {
return string_from_wide2(&u16(buf), len)
mut offset := 0
for {
pos := buf + offset
res := C.ReadFile(h_input, pos, 1, &bytes_read, 0)
if !res || bytes_read == 0 {
break
}
if *pos == `\n` || *pos == `\r` {
offset++
break
}
offset++
}
return ''
return string(buf, offset)
} $else {
max := size_t(256)
buf := charptr(malloc(int(max)))