[MODE] Reset the current thread UI language and streams codepage after changing the console codepage. Add extra error handling.

CORE-17601
This commit is contained in:
Hermès Bélusca-Maïto 2021-05-25 20:17:43 +02:00
parent a8ef85ad71
commit 0483063b69
No known key found for this signature in database
GPG key ID: 3B2539C65E7B93D0
10 changed files with 58 additions and 16 deletions

View 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

View 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

View 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

View 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

View 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

View 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

View 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

View 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

View 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
*
@ -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;
}

View 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 */