builtin: use C.write for print/eprint . Call C.SetConsoleOutputCP(C.CP_UTF8) on windows
parent
d46c1f0f20
commit
40ed2e1b3d
|
@ -6,6 +6,9 @@ module builtin
|
||||||
__global g_m2_buf byteptr
|
__global g_m2_buf byteptr
|
||||||
__global g_m2_ptr byteptr
|
__global g_m2_ptr byteptr
|
||||||
|
|
||||||
|
type FnExitCb fn()
|
||||||
|
fn C.atexit(f FnExitCb) int
|
||||||
|
|
||||||
pub fn exit(code int) {
|
pub fn exit(code int) {
|
||||||
C.exit(code)
|
C.exit(code)
|
||||||
}
|
}
|
||||||
|
@ -66,48 +69,25 @@ pub fn eprintln(s string) {
|
||||||
if s.str == 0 {
|
if s.str == 0 {
|
||||||
eprintln('eprintln(NIL)')
|
eprintln('eprintln(NIL)')
|
||||||
}
|
}
|
||||||
$if !windows {
|
|
||||||
C.fflush(C.stdout)
|
C.fflush(C.stdout)
|
||||||
C.fflush(C.stderr)
|
C.fflush(C.stderr)
|
||||||
C.fprintf(C.stderr, '%.*s\n', s.len, s.str)
|
C.write(2, s.str, s.len)
|
||||||
|
C.write(2, c'\n', 1)
|
||||||
C.fflush(C.stderr)
|
C.fflush(C.stderr)
|
||||||
return
|
|
||||||
}
|
|
||||||
// TODO issues with stderr and cross compiling for Linux
|
|
||||||
println(s)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn eprint(s string) {
|
pub fn eprint(s string) {
|
||||||
if s.str == 0 {
|
if s.str == 0 {
|
||||||
eprintln('eprint(NIL)')
|
eprintln('eprint(NIL)')
|
||||||
}
|
}
|
||||||
$if !windows {
|
|
||||||
C.fflush(C.stdout)
|
C.fflush(C.stdout)
|
||||||
C.fflush(C.stderr)
|
C.fflush(C.stderr)
|
||||||
C.fprintf(C.stderr, '%.*s', s.len, s.str)
|
C.write(2, s.str, s.len)
|
||||||
C.fflush(C.stderr)
|
C.fflush(C.stderr)
|
||||||
return
|
|
||||||
}
|
|
||||||
print(s)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn print(s string) {
|
pub fn print(s string) {
|
||||||
$if windows {
|
C.write(1, s.str, s.len)
|
||||||
output_handle := C.GetStdHandle(C.STD_OUTPUT_HANDLE)
|
|
||||||
mut bytes_written := 0
|
|
||||||
if is_atty(1) > 0 {
|
|
||||||
wide_str := s.to_wide()
|
|
||||||
wide_len := C.wcslen(wide_str)
|
|
||||||
C.WriteConsole(output_handle, wide_str, wide_len, &bytes_written, 0)
|
|
||||||
unsafe {
|
|
||||||
free(wide_str)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
C.WriteFile(output_handle, s.str, s.len, &bytes_written, 0)
|
|
||||||
}
|
|
||||||
} $else {
|
|
||||||
C.printf('%.*s', s.len, s.str)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
|
@ -59,11 +59,25 @@ const (
|
||||||
symopt_debug = 0x80000000
|
symopt_debug = 0x80000000
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// g_original_codepage - used to restore the original windows console code page when exiting
|
||||||
|
__global g_original_codepage u32=0
|
||||||
|
// utf8 to stdout needs C.SetConsoleOutputCP(C.CP_UTF8)
|
||||||
|
fn C.GetConsoleOutputCP() u32
|
||||||
|
fn C.SetConsoleOutputCP(wCodePageID u32) bool
|
||||||
|
fn restore_codepage() {
|
||||||
|
C.SetConsoleOutputCP( g_original_codepage )
|
||||||
|
}
|
||||||
|
|
||||||
fn builtin_init() {
|
fn builtin_init() {
|
||||||
|
g_original_codepage = C.GetConsoleOutputCP()
|
||||||
|
C.SetConsoleOutputCP(C.CP_UTF8)
|
||||||
|
C.atexit(restore_codepage)
|
||||||
if is_atty(1) > 0 {
|
if is_atty(1) > 0 {
|
||||||
C.SetConsoleMode(C.GetStdHandle(C.STD_OUTPUT_HANDLE), C.ENABLE_PROCESSED_OUTPUT | 0x0004) // enable_virtual_terminal_processing
|
C.SetConsoleMode(C.GetStdHandle(C.STD_OUTPUT_HANDLE), C.ENABLE_PROCESSED_OUTPUT | 0x0004) // enable_virtual_terminal_processing
|
||||||
|
C.SetConsoleMode(C.GetStdHandle(C.STD_ERROR_HANDLE), C.ENABLE_PROCESSED_OUTPUT | 0x0004) // enable_virtual_terminal_processing
|
||||||
unsafe {
|
unsafe {
|
||||||
C.setbuf(C.stdout, 0)
|
C.setbuf(C.stdout, 0)
|
||||||
|
C.setbuf(C.stderr, 0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
add_unhandled_exception_handler()
|
add_unhandled_exception_handler()
|
||||||
|
|
Loading…
Reference in New Issue