[MODE] Reset the current thread UI language and streams codepage after changing the...
authorHermès Bélusca-Maïto <hermes.belusca-maito@reactos.org>
Tue, 25 May 2021 18:17:43 +0000 (20:17 +0200)
committerHermès Bélusca-Maïto <hermes.belusca-maito@reactos.org>
Tue, 25 May 2021 23:42:11 +0000 (01:42 +0200)
CORE-17601

base/applications/cmdutils/mode/lang/de-DE.rc
base/applications/cmdutils/mode/lang/en-US.rc
base/applications/cmdutils/mode/lang/it-IT.rc
base/applications/cmdutils/mode/lang/pl-PL.rc
base/applications/cmdutils/mode/lang/ro-RO.rc
base/applications/cmdutils/mode/lang/ru-RU.rc
base/applications/cmdutils/mode/lang/tr-TR.rc
base/applications/cmdutils/mode/lang/zh-CN.rc
base/applications/cmdutils/mode/mode.c
base/applications/cmdutils/mode/resource.h

index 5355ac8..a2ccfcc 100644 (file)
@@ -68,4 +68,5 @@ BEGIN
     IDS_ERROR_INVALID_STOP_BITS "ERROR: Invalid value for Stop Bits %d:\n"
     IDS_ERROR_NO_MEMORY "ERROR: Not enough memory.\n"
     IDS_ERROR_SCREEN_LINES_COL "The screen cannot be set to the number of lines and columns specified.\n"
+    IDS_ERROR_INVALID_CODEPAGE "The code page specified is not valid.\n"
 END
index ce84d5d..c0fc805 100644 (file)
@@ -68,4 +68,5 @@ BEGIN
     IDS_ERROR_INVALID_STOP_BITS "ERROR: Invalid value for Stop Bits %d:\n"
     IDS_ERROR_NO_MEMORY "ERROR: Not enough memory.\n"
     IDS_ERROR_SCREEN_LINES_COL "The screen cannot be set to the number of lines and columns specified.\n"
+    IDS_ERROR_INVALID_CODEPAGE "The code page specified is not valid.\n"
 END
index 77a7b63..e0d3010 100644 (file)
@@ -68,4 +68,5 @@ BEGIN
     IDS_ERROR_INVALID_STOP_BITS "ERRORE: valore non valido per i bit di stop %d:\n"
     IDS_ERROR_NO_MEMORY "ERRORE: memoria insufficiente.\n"
     IDS_ERROR_SCREEN_LINES_COL "Lo schermo non può essere impostato con il numero di righe e colonne specificato.\n"
+    IDS_ERROR_INVALID_CODEPAGE "The code page specified is not valid.\n"
 END
index 0cd8d44..96e4796 100644 (file)
@@ -68,4 +68,5 @@ BEGIN
     IDS_ERROR_INVALID_STOP_BITS "BŁĄD: Nieprawidłowa wartość dla bitów separatora %d:\n"
     IDS_ERROR_NO_MEMORY "BŁĄD: Za mało pamięci.\n"
     IDS_ERROR_SCREEN_LINES_COL "BŁĄD: Nie można ustawić ekranu na określoną liczbę wierszy i kolumn.\n"
+    IDS_ERROR_INVALID_CODEPAGE "The code page specified is not valid.\n"
 END
index 01c83e0..8c8c38e 100644 (file)
@@ -77,4 +77,5 @@ BEGIN
     IDS_ERROR_INVALID_STOP_BITS "EROARE: Valoare eronată pentru biții delimitori %d:\n"
     IDS_ERROR_NO_MEMORY "ERAORE: Nu există suficientă memorie.\n"
     IDS_ERROR_SCREEN_LINES_COL "Ecranul nu poate fi stabilit cu numărul de linii sau coloane specificate.\n"
+    IDS_ERROR_INVALID_CODEPAGE "The code page specified is not valid.\n"
 END
index 81d2190..d79c223 100644 (file)
@@ -68,4 +68,5 @@ BEGIN
     IDS_ERROR_INVALID_STOP_BITS "ОШИБКА: Неверное значение стоповых битов %d:\n"
     IDS_ERROR_NO_MEMORY "ОШИБКА: Недостаточно памяти.\n"
     IDS_ERROR_SCREEN_LINES_COL "The screen cannot be set to the number of lines and columns specified.\n"
+    IDS_ERROR_INVALID_CODEPAGE "The code page specified is not valid.\n"
 END
index 31cfe37..9b93bf8 100644 (file)
@@ -70,4 +70,5 @@ BEGIN
     IDS_ERROR_INVALID_STOP_BITS "YANLIŞLIK: %d Durma İkilleri için geçersiz değer:\n"
     IDS_ERROR_NO_MEMORY "YANLIŞLIK: Yeterli bellek yok.\n"
     IDS_ERROR_SCREEN_LINES_COL "Görüntülük, belirtilen yataç ve dikeç sayısına ayarlanamıyor.\n"
+    IDS_ERROR_INVALID_CODEPAGE "The code page specified is not valid.\n"
 END
index 7e8e841..3a6ca30 100644 (file)
@@ -68,4 +68,5 @@ BEGIN
     IDS_ERROR_INVALID_STOP_BITS "错误: 终止位 %d 值无效:\n"
     IDS_ERROR_NO_MEMORY "错误: 内存不足。\n"
     IDS_ERROR_SCREEN_LINES_COL "屏幕无法被设置成指定的行数和列数。\n"
+    IDS_ERROR_INVALID_CODEPAGE "The code page specified is not valid.\n"
 END
index 054564e..8cb834b 100644 (file)
@@ -1,3 +1,10 @@
+/*
+ * PROJECT:     ReactOS Mode Utility
+ * LICENSE:     GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
+ * PURPOSE:     Provides fast mode setup for DOS devices.
+ * COPYRIGHT:   Copyright 2002 Robert Dickenson
+ *              Copyright 2016-2021 Hermes Belusca-Maito
+ */
 /*
  *  ReactOS mode console command
  *
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
-/*
- * COPYRIGHT:       See COPYING in the top level directory
- * PROJECT:         ReactOS Mode Utility
- * FILE:            base/applications/cmdutils/mode/mode.c
- * PURPOSE:         Provides fast mode setup for DOS devices.
- * PROGRAMMERS:     Robert Dickenson
- *                  Hermes Belusca-Maito
- */
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -453,22 +452,18 @@ invalid_parameter:
 int SetConsoleCPState(IN PCWSTR ArgStr)
 {
     PCWSTR argStr = ArgStr;
-    DWORD CodePage = 0;
+    DWORD value = 0;
+    UINT uOldCodePage, uNewCodePage;
 
     if ( (_wcsnicmp(argStr, L"SELECT=", 7) == 0 && (argStr += 7)) ||
          (_wcsnicmp(argStr, L"SEL=", 4) == 0 && (argStr += 4)) )
     {
-        argStr = ParseNumber(argStr, &CodePage);
+        argStr = ParseNumber(argStr, &value);
         if (!argStr) goto invalid_parameter;
 
         /* This should be the end of the string */
         while (*argStr == L' ') argStr++;
         if (*argStr) goto invalid_parameter;
-
-        SetConsoleCP(CodePage);
-        SetConsoleOutputCP(CodePage);
-        // "The code page specified is not valid."
-        ShowConsoleCPStatus();
     }
     else
     {
@@ -477,7 +472,45 @@ invalid_parameter:
         return 1;
     }
 
-    return 0;
+    uNewCodePage = value;
+
+/**
+ ** IMPORTANT NOTE: This code must be kept synchronized with CHCP.COM
+ **/
+
+    /*
+     * Save the original console code page to be restored
+     * in case SetConsoleCP() or SetConsoleOutputCP() fails.
+     */
+    uOldCodePage = GetConsoleCP();
+
+    /*
+     * Try changing the console input and output code pages.
+     * If it succeeds, refresh the local code page information.
+     */
+    if (SetConsoleCP(uNewCodePage))
+    {
+        if (SetConsoleOutputCP(uNewCodePage))
+        {
+            /* Success, reset the current thread UI language
+             * and update the streams cached code page. */
+            ConSetThreadUILanguage(0);
+            ConStdStreamsSetCacheCodePage(uNewCodePage, uNewCodePage);
+
+            /* Display the current console status */
+            ShowConsoleStatus();
+            return 0;
+        }
+        else
+        {
+            /* Failure, restore the original console code page */
+            SetConsoleCP(uOldCodePage);
+        }
+    }
+
+    /* An error happened, display an error and bail out */
+    ConResPuts(StdErr, IDS_ERROR_INVALID_CODEPAGE);
+    return 1;
 }
 
 
index 2916e47..65b05fa 100644 (file)
@@ -47,5 +47,6 @@
 #define IDS_ERROR_INVALID_STOP_BITS     35
 #define IDS_ERROR_NO_MEMORY             36
 #define IDS_ERROR_SCREEN_LINES_COL      37
+#define IDS_ERROR_INVALID_CODEPAGE      38
 
 #endif /* RESOURCE_H */