diff options
author | David Allsopp <david.allsopp@metastack.com> | 2017-10-20 12:14:23 +0100 |
---|---|---|
committer | David Allsopp <david.allsopp@metastack.com> | 2017-10-25 15:24:12 +0100 |
commit | 4afc3d8e57a92e52117bb59cb4bd59d6786aa495 (patch) | |
tree | 6bda41bdc2a372c2cd041650d8cdf3d641c594a8 | |
parent | 4fc24e6ebb1e81c50d41f645717356c17859b05b (diff) | |
download | ocaml-4afc3d8e57a92e52117bb59cb4bd59d6786aa495.tar.gz |
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.
-rw-r--r-- | byterun/caml/osdeps.h | 1 | ||||
-rw-r--r-- | byterun/sys.c | 3 | ||||
-rw-r--r-- | 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); } |