[Git][ghc/ghc][master] winio: restore console cp on exit

Marge Bot gitlab at gitlab.haskell.org
Fri Jul 24 22:12:30 UTC 2020



 Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC


Commits:
cdd0ff16 by Tamar Christina at 2020-07-24T18:12:23-04:00
winio: restore console cp on exit

- - - - -


3 changed files:

- includes/HsFFI.h
- rts/RtsStartup.c
- rts/win32/veh_excn.c


Changes:

=====================================
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);
 


=====================================
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();
 


=====================================
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);



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/cdd0ff16f20ce920c74f9128a1067cbe1bd378c2

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/cdd0ff16f20ce920c74f9128a1067cbe1bd378c2
You're receiving this email because of your account on gitlab.haskell.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20200724/39852f87/attachment-0001.html>


More information about the ghc-commits mailing list