summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTamar Christina <tamar@zhox.com>2020-07-12 16:33:47 +0100
committerMarge Bot <ben+marge-bot@smart-cactus.org>2020-07-24 18:12:23 -0400
commitcdd0ff16f20ce920c74f9128a1067cbe1bd378c2 (patch)
tree9a62e2e84dbbc4b54aee3fccd65523fb1247a920
parentdff1cb3d9c111808fec60190747272b973547c52 (diff)
downloadhaskell-cdd0ff16f20ce920c74f9128a1067cbe1bd378c2.tar.gz
winio: restore console cp on exit
-rw-r--r--includes/HsFFI.h1
-rw-r--r--rts/RtsStartup.c39
-rw-r--r--rts/win32/veh_excn.c1
3 files changed, 39 insertions, 2 deletions
diff --git a/includes/HsFFI.h b/includes/HsFFI.h
index d9812604d4..7c25599f83 100644
--- a/includes/HsFFI.h
+++ b/includes/HsFFI.h
@@ -102,6 +102,7 @@ extern void hs_exit (void);
extern void hs_exit_nowait(void);
extern void hs_set_argv (int argc, char *argv[]);
extern void hs_thread_done (void);
+extern void hs_restoreConsoleCP (void);
extern void hs_perform_gc (void);
diff --git a/rts/RtsStartup.c b/rts/RtsStartup.c
index a52c02190e..497add5e92 100644
--- a/rts/RtsStartup.c
+++ b/rts/RtsStartup.c
@@ -68,6 +68,11 @@
static int hs_init_count = 0;
static bool rts_shutdown = false;
+#if defined(mingw32_HOST_OS)
+/* Indicates CodePage to set program to after exit. */
+static int64_t __codePage = 0;
+#endif
+
static void flushStdHandles(void);
/* -----------------------------------------------------------------------------
@@ -128,13 +133,38 @@ void fpreset(void) {
static void
initConsoleCP (void)
{
+ /* Set the initial codepage to automatic. */
+ __codePage = -1;
+
/* Check if the codepage is still the system default ANSI codepage. */
- if (GetConsoleCP () == GetOEMCP ()) {
- if (! SetConsoleCP (CP_UTF8))
+ if (GetConsoleCP () == GetOEMCP ()
+ && GetConsoleOutputCP () == GetOEMCP ()) {
+ if (!SetConsoleCP (CP_UTF8) || !SetConsoleOutputCP (CP_UTF8))
errorBelch ("Unable to set console CodePage, Unicode output may be "
"garbled.\n");
else
IF_DEBUG (scheduler, debugBelch ("Codepage set to UTF-8.\n"));
+
+ /* Assign the codepage so we can restore it on exit. */
+ __codePage = (int64_t)GetOEMCP ();
+ }
+}
+
+/* Restore the CodePage to what it was before we started. If the CodePage was
+ already set then this call is a no-op. */
+void
+hs_restoreConsoleCP (void)
+{
+ /* If we set the CP at startup, we should set it on exit. */
+ if (__codePage == -1)
+ return;
+
+ UINT cp = (UINT)__codePage;
+ __codePage = -1;
+ if (SetConsoleCP (cp) && SetConsoleOutputCP (cp)) {
+ IF_DEBUG (scheduler, debugBelch ("Codepage restored to OEM.\n"));
+ } else {
+ IF_DEBUG (scheduler, debugBelch ("Unable to restore CodePage to OEM.\n"));
}
}
#endif
@@ -533,6 +563,11 @@ hs_exit_(bool wait_foreign)
shutdownAsyncIO(wait_foreign);
#endif
+ /* Restore the console Codepage. */
+#if defined(mingw32_HOST_OS)
+ if (is_io_mng_native_p())
+ hs_restoreConsoleCP();
+#endif
/* free hash table storage */
exitHashTable();
diff --git a/rts/win32/veh_excn.c b/rts/win32/veh_excn.c
index b86abe64b0..141806997a 100644
--- a/rts/win32/veh_excn.c
+++ b/rts/win32/veh_excn.c
@@ -153,6 +153,7 @@ long WINAPI __hs_exception_handler(struct _EXCEPTION_POINTERS *exception_data)
if (EXCEPTION_CONTINUE_EXECUTION == action)
{
fflush(stderr);
+ hs_restoreConsoleCP ();
generateStack (exception_data);
generateDump (exception_data);
stg_exit(exit_code);