Skip to content

Commit

Permalink
Merge pull request #5356 from maron2000/fix_chcp
Browse files Browse the repository at this point in the history
Fix codepage didn't change when loading language files in some cases
  • Loading branch information
joncampbell123 authored Dec 30, 2024
2 parents 24e0d70 + 5d40409 commit d93d37c
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 34 deletions.
6 changes: 4 additions & 2 deletions src/dos/dos_keyboard_layout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1391,7 +1391,8 @@ Bitu DOS_LoadKeyboardLayout(const char * layoutname, int32_t codepage, const cha
return kerrcode;
}
// Everything went fine, switch to new layout
loaded_layout=temp_layout;
delete loaded_layout;
loaded_layout=temp_layout;
return KEYB_NOERROR;
}

Expand All @@ -1405,7 +1406,7 @@ Bitu DOS_SwitchKeyboardLayout(const char* new_layout, int32_t& tried_cp) {
loaded_layout=changed_layout;
}
return ret_code;
} else return 0xff;
} else return 0xFF;
}


Expand All @@ -1418,6 +1419,7 @@ Bitu DOS_ChangeKeyboardLayout(const char* layoutname, int32_t codepage) {
return kerrcode;
}
// Everything went fine, switch to new layout
delete loaded_layout;
loaded_layout = temp_layout;
return KEYB_NOERROR;
}
Expand Down
14 changes: 10 additions & 4 deletions src/misc/messages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ bool InitCodePage() {
return true;
}
else dos.loaded_codepage = msgcodepage;
return true;
}
}
if (msgcodepage>0) {
Expand Down Expand Up @@ -217,6 +218,7 @@ bool CheckDBCSCP(int32_t codepage) {
else return false;
}

#if 0
void SetKEYBCP() {
if (IS_PC98_ARCH || IS_JEGA_ARCH || IS_DOSV || dos_kernel_disabled || !strcmp(RunningProgram, "LOADLIN")) return;
Bitu return_code;
Expand All @@ -236,6 +238,7 @@ void SetKEYBCP() {
}
runRescan("-A -Q");
}
#endif

FILE *testLoadLangFile(const char *fname) {
std::string config_path, res_path, exepath=GetDOSBoxXPath();
Expand Down Expand Up @@ -279,8 +282,7 @@ void LoadMessageFile(const char * fname) {
//LOG_MSG("Message file %s already loaded.",fname);
return;
}
strcpy(loaded_fname, fname);
LOG(LOG_MISC,LOG_DEBUG)("Loading message file %s",loaded_fname);
LOG(LOG_MISC,LOG_DEBUG)("Loading message file %s",fname);

FILE * mfile=testLoadLangFile(fname);
/* This should never happen and since other modules depend on this use a normal printf */
Expand All @@ -292,6 +294,7 @@ void LoadMessageFile(const char * fname) {
control->opt_lang = "";
return;
}
strcpy(loaded_fname, fname);
langname = langnote = "";
char linein[LINE_IN_MAXLEN+1024];
char name[LINE_IN_MAXLEN+1024], menu_name[LINE_IN_MAXLEN], mapper_name[LINE_IN_MAXLEN];
Expand Down Expand Up @@ -337,7 +340,10 @@ void LoadMessageFile(const char * fname) {
}
else {
std::string msg = "The specified language file uses code page " + std::to_string(c) + ". Do you want to change to this code page accordingly?";
if(c == dos.loaded_codepage) msgcodepage = c;
if(c == dos.loaded_codepage) {
msgcodepage = c;
lastmsgcp = msgcodepage;
}
if(c != dos.loaded_codepage && (control->opt_langcp || uselangcp || !CHCP_changed || CheckDBCSCP(c) || !loadlang || (loadlang && systemmessagebox("DOSBox-X language file", msg.c_str(), "yesno", "question", 1)))) {
loadlangcp = true;
if(c == 950 && dos.loaded_codepage == 951) msgcodepage = 951; // zh_tw defaults to CP950, but CP951 is acceptable as well so keep it
Expand Down Expand Up @@ -404,7 +410,7 @@ void LoadMessageFile(const char * fname) {
dos.loaded_codepage=cp;
if (loadlangcp && msgcodepage>0) {
const char* layoutname = DOS_GetLoadedLayout();
if(!IS_DOSV && !IS_JEGA_ARCH && layoutname != NULL) {
if(!IS_DOSV && !IS_JEGA_ARCH && !IS_PC98_ARCH && layoutname != NULL) {
toSetCodePage(NULL, msgcodepage, -1);
}
}
Expand Down
39 changes: 20 additions & 19 deletions src/shell/shell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ extern bool shell_keyboard_flush;
extern bool dos_shell_running_program, mountwarning, winautorun;
extern bool startcmd, startwait, startquiet, internal_program;
extern bool addovl, addipx, addne2k, enableime, showdbcs;
extern bool halfwidthkana, force_conversion, gbk, uselangcp;
extern bool halfwidthkana, force_conversion, gbk, uselangcp, chinasea;
extern const char* RunningProgram;
extern int enablelfn, msgcodepage, lastmsgcp;
extern uint16_t countryNo;
Expand All @@ -69,6 +69,8 @@ Bitu call_int23 = 0;

std::string GetDOSBoxXPath(bool withexe=false);
const char* DOS_GetLoadedLayout(void);
Bitu DOS_ChangeKeyboardLayout(const char* layoutname, int32_t codepage), DOS_SwitchKeyboardLayout(const char* new_layout, int32_t& tried_cp);
Bitu DOS_LoadKeyboardLayout(const char* layoutname, int32_t codepage, const char* codepagefile);
int Reflect_Menu(void);
void SetIMPosition(void);
void SetKEYBCP();
Expand All @@ -81,6 +83,7 @@ bool SwitchLanguage(int oldcp, int newcp, bool confirm);
void CALLBACK_DeAllocate(Bitu in), DOSBox_ConsolePauseWait();
void GFX_SetTitle(int32_t cycles, int frameskip, Bits timing, bool paused);
bool isDBCSCP(), InitCodePage(), isKanji1(uint8_t chr), shiftjis_lead_byte(int c), sdl_wait_on_error(), CheckDBCSCP(int32_t codepage), TTF_using(void);
void makestdcp950table(), makeseacp951table();

Bitu call_shellstop = 0;
/* Larger scope so shell_del autoexec can use it to
Expand Down Expand Up @@ -819,6 +822,12 @@ void showWelcome(Program *shell) {
bool finish_prepare = false;
void DOS_Shell::Prepare(void) {
if (this == first_shell) {
const char* layoutname = DOS_GetLoadedLayout();
if(layoutname == NULL) {
int32_t cp = dos.loaded_codepage;
Bitu keyb_error = DOS_LoadKeyboardLayout("us", 437, "auto");
toSetCodePage(NULL, cp, -1);
}
Section_prop *section = static_cast<Section_prop *>(control->GetSection("dosbox"));
if (section->Get_bool("startbanner")&&!control->opt_fastlaunch)
showWelcome(this);
Expand Down Expand Up @@ -857,33 +866,25 @@ void DOS_Shell::Prepare(void) {
if ((section!=NULL&&!control->opt_noconfig)||control->opt_langcp) {
char *countrystr = (char *)section->Get_string("country"), *r=strchr(countrystr, ',');
int country = 0;
if ((r==NULL || !*(r+1)) && !control->opt_langcp)
int32_t newCP = dos.loaded_codepage;
if((control->opt_langcp && msgcodepage > 0) || CheckDBCSCP(msgcodepage) || msgcodepage == dos.loaded_codepage) newCP = msgcodepage;
if ((r==NULL || !*(r+1)) && !control->opt_langcp)
country = atoi(trim(countrystr));
else {
else if(!msgcodepage){
if (r!=NULL) *r=0;
country = atoi(trim(countrystr));
int32_t newCP = r==NULL||IS_PC98_ARCH||IS_JEGA_ARCH||IS_DOSV?dos.loaded_codepage:atoi(trim(r+1));
newCP = r==NULL||IS_PC98_ARCH||IS_JEGA_ARCH||IS_DOSV?dos.loaded_codepage:atoi(trim(r+1));
if (r!=NULL) *r=',';
if (!IS_PC98_ARCH&&!IS_JEGA_ARCH) {
if(!newCP && IS_DOSV) {
if(IS_JDOSV) newCP = 932;
else if(IS_PDOSV) newCP = 936;
else if(IS_KDOSV) newCP = 949;
else if(IS_TDOSV) newCP = 950;
}
if((control->opt_langcp && msgcodepage > 0 ) || CheckDBCSCP(msgcodepage)|| msgcodepage == dos.loaded_codepage) newCP = msgcodepage;
if (newCP != dos.loaded_codepage && (!TTF_using() || (TTF_using() && isSupportedCP(newCP)))) {
int missing = toSetCodePage(this, newCP, msgcodepage?-1:control->opt_fastlaunch?1:-2);
//WriteOut(MSG_Get("SHELL_CMD_CHCP_ACTIVE"), dos.loaded_codepage);
if (missing > 0) WriteOut(MSG_Get("SHELL_CMD_CHCP_MISSING"), missing);
else if (missing < 0) WriteOut(MSG_Get("SHELL_CMD_CHCP_INVALID"), newCP);
}
}
}
if (newCP != dos.loaded_codepage && (!TTF_using() || (TTF_using() && isSupportedCP(newCP)))) {
int missing = toSetCodePage(this, newCP, -1);
}
if (country>0&&!control->opt_noconfig) {
countryNo = country;
DOS_SetCountry(countryNo);
}
if(!chinasea)makestdcp950table();
if(chinasea) makeseacp951table();
runRescan("-A -Q");
const char * extra = section->data.c_str();
if (extra&&!control->opt_securemode&&!control->SecureMode()&&!control->opt_noconfig) {
Expand Down
34 changes: 25 additions & 9 deletions src/shell/shell_cmds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ const char *GetCmdName(int i) {
}

extern int enablelfn, lfn_filefind_handle, file_access_tries, lastmsgcp;
extern bool date_host_forced, usecon, outcon, rsize, autoboxdraw, dbcs_sbcs, sync_time, manualtime, inshell, noassoc, dotype, loadlang;
extern bool date_host_forced, usecon, outcon, rsize, autoboxdraw, dbcs_sbcs, sync_time, manualtime, inshell, noassoc, dotype, loadlang, chinasea;
extern unsigned long freec;
extern uint8_t DOS_GetAnsiAttr(void);
extern uint16_t countryNo, altcp_to_unicode[256];
Expand All @@ -143,6 +143,12 @@ std::string GetDOSBoxXPath(bool withexe=false);
FILE *testLoadLangFile(const char *fname);
Bitu DOS_ChangeCodepage(int32_t codepage, const char* codepagefile);
bool CheckDBCSCP(int32_t codepage), SwitchLanguage(int oldcp, int newcp, bool confirm);
void makestdcp950table(), makeseacp951table();
#if C_OPENGL
bool OpenGL_using(void);
#endif
void UpdateSDLDrawTexture();

static int32_t lastsetcp = 0;
bool CHCP_changed = false;

Expand Down Expand Up @@ -4532,25 +4538,29 @@ extern Bitu DOS_LoadKeyboardLayout(const char * layoutname, int32_t codepage, co
void runRescan(const char *str), MSG_Init(), JFONT_Init(), InitFontHandle(), ShutFontHandle(), initcodepagefont(), DOSBox_SetSysMenu();
int toSetCodePage(DOS_Shell *shell, int newCP, int opt) {
if((TTF_using() && isSupportedCP(newCP)) || !TTF_using()) {
int32_t oldcp = dos.loaded_codepage;
int32_t oldcp = dos.loaded_codepage, cpbak = newCP;
Bitu keyb_error;
if (!CheckDBCSCP(newCP)){
if(IS_PC98_ARCH || IS_JEGA_ARCH) newCP = 932;
else if(IS_DOSV) {
if(IS_JDOSV) newCP = 932;
else if(IS_PDOSV) newCP = 936;
else if(IS_KDOSV) newCP = 949;
else if(IS_TDOSV) newCP = 950;
}
else if (!CheckDBCSCP(newCP)){
keyb_error = DOS_ChangeCodepage(newCP, "auto");
if (keyb_error != KEYB_NOERROR) {
dos.loaded_codepage = oldcp;
dos.loaded_codepage = oldcp;
return -1;
}
}
else {
dos.loaded_codepage = newCP;
}
if(newCP != cpbak) LOG_MSG("SHELL: Invalid codepage %d, set to %d.", cpbak, newCP);
dos.loaded_codepage = newCP;
int missing = 0;
#if defined(USE_TTF)
missing = TTF_using() ? setTTFCodePage() : 0;
#endif
if (!TTF_using()) initcodepagefont();
//if (dos.loaded_codepage==437) DOS_LoadKeyboardLayout("us", 437, "auto");
//LOG_MSG("toSetCodePage opt=%d, loadlangnew=%d", opt, loadlangnew?1:0);
if (opt==-1) {
MSG_Init();
#if DOSBOXMENU_TYPE == DOSBOXMENU_HMENU
Expand All @@ -4568,8 +4578,14 @@ int toSetCodePage(DOS_Shell *shell, int newCP, int opt) {
JFONT_Init();
SetupDBCSTable();
clearFontCache();
if(newCP == 950 && !chinasea) makestdcp950table();
if(newCP == 951 && chinasea) makeseacp951table();
}
if (finish_prepare) runRescan("-A -Q");
#if C_OPENGL && DOSBOXMENU_TYPE == DOSBOXMENU_SDLDRAW
if(OpenGL_using() && control->opt_lang.size())
UpdateSDLDrawTexture();
#endif
#if defined(USE_TTF)
if ((opt==-1||opt==-2)&&TTF_using()) {
Section_prop * ttf_section = static_cast<Section_prop *>(control->GetSection("ttf"));
Expand Down

0 comments on commit d93d37c

Please sign in to comment.