Reactos

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

+58 -16
+1
base/applications/cmdutils/mode/lang/de-DE.rc
··· 68 68 IDS_ERROR_INVALID_STOP_BITS "ERROR: Invalid value for Stop Bits %d:\n" 69 69 IDS_ERROR_NO_MEMORY "ERROR: Not enough memory.\n" 70 70 IDS_ERROR_SCREEN_LINES_COL "The screen cannot be set to the number of lines and columns specified.\n" 71 + IDS_ERROR_INVALID_CODEPAGE "The code page specified is not valid.\n" 71 72 END
+1
base/applications/cmdutils/mode/lang/en-US.rc
··· 68 68 IDS_ERROR_INVALID_STOP_BITS "ERROR: Invalid value for Stop Bits %d:\n" 69 69 IDS_ERROR_NO_MEMORY "ERROR: Not enough memory.\n" 70 70 IDS_ERROR_SCREEN_LINES_COL "The screen cannot be set to the number of lines and columns specified.\n" 71 + IDS_ERROR_INVALID_CODEPAGE "The code page specified is not valid.\n" 71 72 END
+1
base/applications/cmdutils/mode/lang/it-IT.rc
··· 68 68 IDS_ERROR_INVALID_STOP_BITS "ERRORE: valore non valido per i bit di stop %d:\n" 69 69 IDS_ERROR_NO_MEMORY "ERRORE: memoria insufficiente.\n" 70 70 IDS_ERROR_SCREEN_LINES_COL "Lo schermo non può essere impostato con il numero di righe e colonne specificato.\n" 71 + IDS_ERROR_INVALID_CODEPAGE "The code page specified is not valid.\n" 71 72 END
+1
base/applications/cmdutils/mode/lang/pl-PL.rc
··· 68 68 IDS_ERROR_INVALID_STOP_BITS "BŁĄD: Nieprawidłowa wartość dla bitów separatora %d:\n" 69 69 IDS_ERROR_NO_MEMORY "BŁĄD: Za mało pamięci.\n" 70 70 IDS_ERROR_SCREEN_LINES_COL "BŁĄD: Nie można ustawić ekranu na określoną liczbę wierszy i kolumn.\n" 71 + IDS_ERROR_INVALID_CODEPAGE "The code page specified is not valid.\n" 71 72 END
+1
base/applications/cmdutils/mode/lang/ro-RO.rc
··· 77 77 IDS_ERROR_INVALID_STOP_BITS "EROARE: Valoare eronată pentru biții delimitori %d:\n" 78 78 IDS_ERROR_NO_MEMORY "ERAORE: Nu există suficientă memorie.\n" 79 79 IDS_ERROR_SCREEN_LINES_COL "Ecranul nu poate fi stabilit cu numărul de linii sau coloane specificate.\n" 80 + IDS_ERROR_INVALID_CODEPAGE "The code page specified is not valid.\n" 80 81 END
+1
base/applications/cmdutils/mode/lang/ru-RU.rc
··· 68 68 IDS_ERROR_INVALID_STOP_BITS "ОШИБКА: Неверное значение стоповых битов %d:\n" 69 69 IDS_ERROR_NO_MEMORY "ОШИБКА: Недостаточно памяти.\n" 70 70 IDS_ERROR_SCREEN_LINES_COL "The screen cannot be set to the number of lines and columns specified.\n" 71 + IDS_ERROR_INVALID_CODEPAGE "The code page specified is not valid.\n" 71 72 END
+1
base/applications/cmdutils/mode/lang/tr-TR.rc
··· 70 70 IDS_ERROR_INVALID_STOP_BITS "YANLIŞLIK: %d Durma İkilleri için geçersiz değer:\n" 71 71 IDS_ERROR_NO_MEMORY "YANLIŞLIK: Yeterli bellek yok.\n" 72 72 IDS_ERROR_SCREEN_LINES_COL "Görüntülük, belirtilen yataç ve dikeç sayısına ayarlanamıyor.\n" 73 + IDS_ERROR_INVALID_CODEPAGE "The code page specified is not valid.\n" 73 74 END
+1
base/applications/cmdutils/mode/lang/zh-CN.rc
··· 68 68 IDS_ERROR_INVALID_STOP_BITS "错误: 终止位 %d 值无效:\n" 69 69 IDS_ERROR_NO_MEMORY "错误: 内存不足。\n" 70 70 IDS_ERROR_SCREEN_LINES_COL "屏幕无法被设置成指定的行数和列数。\n" 71 + IDS_ERROR_INVALID_CODEPAGE "The code page specified is not valid.\n" 71 72 END
+49 -16
base/applications/cmdutils/mode/mode.c
··· 1 + /* 2 + * PROJECT: ReactOS Mode Utility 3 + * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+) 4 + * PURPOSE: Provides fast mode setup for DOS devices. 5 + * COPYRIGHT: Copyright 2002 Robert Dickenson 6 + * Copyright 2016-2021 Hermes Belusca-Maito 7 + */ 1 8 /* 2 9 * ReactOS mode console command 3 10 * ··· 18 25 * You should have received a copy of the GNU General Public License 19 26 * along with this program; if not, write to the Free Software 20 27 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 21 - */ 22 - /* 23 - * COPYRIGHT: See COPYING in the top level directory 24 - * PROJECT: ReactOS Mode Utility 25 - * FILE: base/applications/cmdutils/mode/mode.c 26 - * PURPOSE: Provides fast mode setup for DOS devices. 27 - * PROGRAMMERS: Robert Dickenson 28 - * Hermes Belusca-Maito 29 28 */ 30 29 31 30 #include <stdio.h> ··· 453 452 int SetConsoleCPState(IN PCWSTR ArgStr) 454 453 { 455 454 PCWSTR argStr = ArgStr; 456 - DWORD CodePage = 0; 455 + DWORD value = 0; 456 + UINT uOldCodePage, uNewCodePage; 457 457 458 458 if ( (_wcsnicmp(argStr, L"SELECT=", 7) == 0 && (argStr += 7)) || 459 459 (_wcsnicmp(argStr, L"SEL=", 4) == 0 && (argStr += 4)) ) 460 460 { 461 - argStr = ParseNumber(argStr, &CodePage); 461 + argStr = ParseNumber(argStr, &value); 462 462 if (!argStr) goto invalid_parameter; 463 463 464 464 /* This should be the end of the string */ 465 465 while (*argStr == L' ') argStr++; 466 466 if (*argStr) goto invalid_parameter; 467 - 468 - SetConsoleCP(CodePage); 469 - SetConsoleOutputCP(CodePage); 470 - // "The code page specified is not valid." 471 - ShowConsoleCPStatus(); 472 467 } 473 468 else 474 469 { ··· 477 472 return 1; 478 473 } 479 474 480 - return 0; 475 + uNewCodePage = value; 476 + 477 + /** 478 + ** IMPORTANT NOTE: This code must be kept synchronized with CHCP.COM 479 + **/ 480 + 481 + /* 482 + * Save the original console code page to be restored 483 + * in case SetConsoleCP() or SetConsoleOutputCP() fails. 484 + */ 485 + uOldCodePage = GetConsoleCP(); 486 + 487 + /* 488 + * Try changing the console input and output code pages. 489 + * If it succeeds, refresh the local code page information. 490 + */ 491 + if (SetConsoleCP(uNewCodePage)) 492 + { 493 + if (SetConsoleOutputCP(uNewCodePage)) 494 + { 495 + /* Success, reset the current thread UI language 496 + * and update the streams cached code page. */ 497 + ConSetThreadUILanguage(0); 498 + ConStdStreamsSetCacheCodePage(uNewCodePage, uNewCodePage); 499 + 500 + /* Display the current console status */ 501 + ShowConsoleStatus(); 502 + return 0; 503 + } 504 + else 505 + { 506 + /* Failure, restore the original console code page */ 507 + SetConsoleCP(uOldCodePage); 508 + } 509 + } 510 + 511 + /* An error happened, display an error and bail out */ 512 + ConResPuts(StdErr, IDS_ERROR_INVALID_CODEPAGE); 513 + return 1; 481 514 } 482 515 483 516
+1
base/applications/cmdutils/mode/resource.h
··· 47 47 #define IDS_ERROR_INVALID_STOP_BITS 35 48 48 #define IDS_ERROR_NO_MEMORY 36 49 49 #define IDS_ERROR_SCREEN_LINES_COL 37 50 + #define IDS_ERROR_INVALID_CODEPAGE 38 50 51 51 52 #endif /* RESOURCE_H */