From 4afc3d8e57a92e52117bb59cb4bd59d6786aa495 Mon Sep 17 00:00:00 2001 From: David Allsopp Date: Fri, 20 Oct 2017 12:14:23 +0100 Subject: Restore Windows Console CodePage on exit The chcp command is erroneously implemented in the Windows Command Prompt and merely returns the value which the Command Processor expects to be active. The OCaml runtime should not leave the Console in CP_UTF8, but rather restore the CodePage when the runtime terminates. --- byterun/caml/osdeps.h | 1 + byterun/sys.c | 3 +++ byterun/win32.c | 15 +++++++++++++-- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/byterun/caml/osdeps.h b/byterun/caml/osdeps.h index 620ca6b5f5..bc75a7deda 100644 --- a/byterun/caml/osdeps.h +++ b/byterun/caml/osdeps.h @@ -104,6 +104,7 @@ extern int caml_win32_rename(const wchar_t *, const wchar_t *); extern void caml_probe_win32_version(void); extern void caml_setup_win32_terminal(void); +extern void caml_restore_win32_terminal(void); /* Windows Unicode support */ diff --git a/byterun/sys.c b/byterun/sys.c index 603f3501ce..a46b6be446 100644 --- a/byterun/sys.c +++ b/byterun/sys.c @@ -154,6 +154,9 @@ CAMLprim value caml_sys_exit(value retcode_v) CAML_INSTR_ATEXIT (); if (caml_cleanup_on_exit) caml_shutdown(); +#ifdef _WIN32 + caml_restore_win32_terminal(); +#endif CAML_SYS_EXIT(retcode); return Val_unit; } diff --git a/byterun/win32.c b/byterun/win32.c index b11728e849..264ee20160 100644 --- a/byterun/win32.c +++ b/byterun/win32.c @@ -898,8 +898,19 @@ void caml_probe_win32_version(void) free(versionInfo); } +static UINT startup_codepage = 0; + void caml_setup_win32_terminal(void) { - if (caml_win32_major >= 10) - SetConsoleOutputCP(CP_UTF8); + if (caml_win32_major >= 10) { + startup_codepage = GetConsoleOutputCP(); + if (startup_codepage != CP_UTF8) + SetConsoleOutputCP(CP_UTF8); + } +} + +void caml_restore_win32_terminal(void) +{ + if (startup_codepage != 0) + SetConsoleOutputCP(startup_codepage); } -- cgit v1.2.1