https://git.reactos.org/?p=reactos.git;a=commitdiff;h=0483063b6932c779bc114…
commit 0483063b6932c779bc114948a0f1f3a7dc58d24f
Author: Hermès Bélusca-Maïto <hermes.belusca-maito(a)reactos.org>
AuthorDate: Tue May 25 20:17:43 2021 +0200
Commit: Hermès Bélusca-Maïto <hermes.belusca-maito(a)reactos.org>
CommitDate: Wed May 26 01:42:11 2021 +0200
[MODE] Reset the current thread UI language and streams codepage after changing the
console codepage. Add extra error handling.
CORE-17601
---
base/applications/cmdutils/mode/lang/de-DE.rc | 1 +
base/applications/cmdutils/mode/lang/en-US.rc | 1 +
base/applications/cmdutils/mode/lang/it-IT.rc | 1 +
base/applications/cmdutils/mode/lang/pl-PL.rc | 1 +
base/applications/cmdutils/mode/lang/ro-RO.rc | 1 +
base/applications/cmdutils/mode/lang/ru-RU.rc | 1 +
base/applications/cmdutils/mode/lang/tr-TR.rc | 1 +
base/applications/cmdutils/mode/lang/zh-CN.rc | 1 +
base/applications/cmdutils/mode/mode.c | 65 ++++++++++++++++++++-------
base/applications/cmdutils/mode/resource.h | 1 +
10 files changed, 58 insertions(+), 16 deletions(-)
diff --git a/base/applications/cmdutils/mode/lang/de-DE.rc
b/base/applications/cmdutils/mode/lang/de-DE.rc
index 5355ac86e83..a2ccfcc4a06 100644
--- a/base/applications/cmdutils/mode/lang/de-DE.rc
+++ b/base/applications/cmdutils/mode/lang/de-DE.rc
@@ -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
diff --git a/base/applications/cmdutils/mode/lang/en-US.rc
b/base/applications/cmdutils/mode/lang/en-US.rc
index ce84d5d0e5f..c0fc8055e47 100644
--- a/base/applications/cmdutils/mode/lang/en-US.rc
+++ b/base/applications/cmdutils/mode/lang/en-US.rc
@@ -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
diff --git a/base/applications/cmdutils/mode/lang/it-IT.rc
b/base/applications/cmdutils/mode/lang/it-IT.rc
index 77a7b6360a3..e0d3010a7de 100644
--- a/base/applications/cmdutils/mode/lang/it-IT.rc
+++ b/base/applications/cmdutils/mode/lang/it-IT.rc
@@ -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
diff --git a/base/applications/cmdutils/mode/lang/pl-PL.rc
b/base/applications/cmdutils/mode/lang/pl-PL.rc
index 0cd8d44268d..96e47961e1e 100644
--- a/base/applications/cmdutils/mode/lang/pl-PL.rc
+++ b/base/applications/cmdutils/mode/lang/pl-PL.rc
@@ -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
diff --git a/base/applications/cmdutils/mode/lang/ro-RO.rc
b/base/applications/cmdutils/mode/lang/ro-RO.rc
index 01c83e098b2..8c8c38e47d9 100644
--- a/base/applications/cmdutils/mode/lang/ro-RO.rc
+++ b/base/applications/cmdutils/mode/lang/ro-RO.rc
@@ -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
diff --git a/base/applications/cmdutils/mode/lang/ru-RU.rc
b/base/applications/cmdutils/mode/lang/ru-RU.rc
index 81d2190c7d5..d79c2235757 100644
--- a/base/applications/cmdutils/mode/lang/ru-RU.rc
+++ b/base/applications/cmdutils/mode/lang/ru-RU.rc
@@ -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
diff --git a/base/applications/cmdutils/mode/lang/tr-TR.rc
b/base/applications/cmdutils/mode/lang/tr-TR.rc
index 31cfe37bfd9..9b93bf8c2d1 100644
--- a/base/applications/cmdutils/mode/lang/tr-TR.rc
+++ b/base/applications/cmdutils/mode/lang/tr-TR.rc
@@ -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
diff --git a/base/applications/cmdutils/mode/lang/zh-CN.rc
b/base/applications/cmdutils/mode/lang/zh-CN.rc
index 7e8e841dcdd..3a6ca30be63 100644
--- a/base/applications/cmdutils/mode/lang/zh-CN.rc
+++ b/base/applications/cmdutils/mode/lang/zh-CN.rc
@@ -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
diff --git a/base/applications/cmdutils/mode/mode.c
b/base/applications/cmdutils/mode/mode.c
index 054564e3ddb..8cb834b6c23 100644
--- a/base/applications/cmdutils/mode/mode.c
+++ b/base/applications/cmdutils/mode/mode.c
@@ -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
*
@@ -19,14 +26,6 @@
* 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;
}
diff --git a/base/applications/cmdutils/mode/resource.h
b/base/applications/cmdutils/mode/resource.h
index 2916e47c2c4..65b05fa8806 100644
--- a/base/applications/cmdutils/mode/resource.h
+++ b/base/applications/cmdutils/mode/resource.h
@@ -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 */