diff --git a/winsup/cygwin/cygheap.h b/winsup/cygwin/cygheap.h index 2b84f4252..8877cc358 100644 --- a/winsup/cygwin/cygheap.h +++ b/winsup/cygwin/cygheap.h @@ -341,7 +341,6 @@ struct cygheap_debug struct cygheap_locale { mbtowc_p mbtowc; - UINT term_code_page; }; struct user_heap_info diff --git a/winsup/cygwin/fhandler.h b/winsup/cygwin/fhandler.h index af619df5f..17e03785f 100644 --- a/winsup/cygwin/fhandler.h +++ b/winsup/cygwin/fhandler.h @@ -2336,6 +2336,7 @@ class fhandler_pty_slave: public fhandler_pty_common void set_switch_to_pcon (void); void reset_switch_to_pcon (void); void mask_switch_to_pcon_in (bool mask); + void setup_locale (const WCHAR *env); }; #define __ptsname(buf, unit) __small_sprintf ((buf), "/dev/pty%d", (unit)) diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc index feb014175..fc9f4c44d 100644 --- a/winsup/cygwin/fhandler_tty.cc +++ b/winsup/cygwin/fhandler_tty.cc @@ -39,7 +39,7 @@ extern "C" { VOID WINAPI ClosePseudoConsole (HPCON); } -extern UINT __eval_codepage_from_internal_charset (); +extern UINT __eval_codepage_from_internal_charset (const WCHAR *env); #define close_maybe(h) \ do { \ @@ -635,9 +635,9 @@ fhandler_pty_slave::open (int flags, mode_t) fhandler_console::need_invisible (); -#if 1 /* Let's try this first */ - if (!cygheap->locale.term_code_page) - cygheap->locale.term_code_page = __eval_codepage_from_internal_charset (); +#if 0 /* Let's try this first */ + if (!get_ttyp ()->term_code_page) + get_ttyp ()->term_code_page = __eval_codepage_from_internal_charset (NULL); #endif set_open_status (); @@ -1615,8 +1615,8 @@ fhandler_pty_master::write (const void *ptr, size_t len) return (ssize_t) bg; #if 0 /* Let's try this if setting codepage at pty open time is not enough */ - if (!cygheap->locale.term_code_page) - cygheap->locale.term_code_page = __eval_codepage_from_internal_charset (); + if (!get_ttyp ()->term_code_page) + get_ttyp ()->term_code_page = __eval_codepage_from_internal_charset (NULL); #endif push_process_state process_state (PID_TTYOU); @@ -1627,7 +1627,7 @@ fhandler_pty_master::write (const void *ptr, size_t len) { size_t nlen; char *buf = convert_mb_str (CP_UTF8, &nlen, - cygheap->locale.term_code_page, + get_ttyp ()->term_code_page, (const char *) ptr, len); WaitForSingleObject (input_mutex, INFINITE); @@ -1795,6 +1795,13 @@ fhandler_pty_common::set_close_on_exec (bool val) close_on_exec (val); } +void +fhandler_pty_slave::setup_locale (const WCHAR *env) +{ + if (!get_ttyp ()->term_code_page) + get_ttyp ()->term_code_page = __eval_codepage_from_internal_charset (env); +} + void fhandler_pty_slave::fixup_after_fork (HANDLE parent) { @@ -1811,6 +1818,9 @@ fhandler_pty_slave::fixup_after_exec () if (!close_on_exec ()) fixup_after_fork (NULL); /* No parent handle required. */ + /* Set locale */ + setup_locale (NULL); + /* Hook Console API */ #define DO_HOOK(module, name) \ if (!name##_Orig) \ @@ -1982,8 +1992,8 @@ fhandler_pty_master::pty_master_fwd_thread () char outbuf[65536]; #if 0 /* Let's try this if setting codepage at pty open time is not enough */ - if (!cygheap->locale.term_code_page) - cygheap->locale.term_code_page = __eval_codepage_from_internal_charset (); + if (!get_ttyp ()->term_code_page) + get_ttyp ()->term_code_page = __eval_codepage_from_internal_charset (NULL); #endif termios_printf ("Started."); @@ -2041,7 +2051,7 @@ fhandler_pty_master::pty_master_fwd_thread () state = 0; size_t nlen; - char *buf = convert_mb_str (cygheap->locale.term_code_page, + char *buf = convert_mb_str (get_ttyp ()->term_code_page, &nlen, CP_UTF8, ptr, wlen); ptr = buf; @@ -2064,7 +2074,7 @@ fhandler_pty_master::pty_master_fwd_thread () continue; } size_t nlen; - char *buf = convert_mb_str (cygheap->locale.term_code_page, &nlen, + char *buf = convert_mb_str (get_ttyp ()->term_code_page, &nlen, GetConsoleOutputCP (), ptr, wlen); ptr = buf; diff --git a/winsup/cygwin/nlsfuncs.cc b/winsup/cygwin/nlsfuncs.cc index 6bffc77c9..120b55c24 100644 --- a/winsup/cygwin/nlsfuncs.cc +++ b/winsup/cygwin/nlsfuncs.cc @@ -1453,9 +1453,42 @@ __set_charset_from_locale (const char *locale, char *charset) codepage connected to a locale by default, so we have to set this up explicitely. */ UINT -__eval_codepage_from_internal_charset () +__eval_codepage_from_internal_charset (const WCHAR *envblock) { - const char *charset = __locale_charset (__get_global_locale ()); + const char *charset; + __locale_t *loc = NULL; + if (__get_current_locale ()->lc_cat[LC_CTYPE].buf) + charset = __locale_charset (__get_current_locale ()); + else + { + char locale[ENCODING_LEN + 1] = {0, }; + if (envblock) + { + const WCHAR *lc_all = NULL, *lc_ctype = NULL, *lang = NULL; + for (const WCHAR *p = envblock; *p != L'\0'; p += wcslen (p) + 1) + if (wcsncmp (p, L"LC_ALL=", 7) == 0) + lc_all = p + 7; + else if (wcsncmp (p, L"LC_CTYPE=", 9) == 0) + lc_ctype = p + 9; + else if (wcsncmp (p, L"LANG=", 5) == 0) + lang = p + 5; + if (lc_all && *lc_all) + snprintf (locale, ENCODING_LEN + 1, "%ls", lc_all); + else if (lc_ctype && *lc_ctype) + snprintf (locale, ENCODING_LEN + 1, "%ls", lc_ctype); + else if (lang && *lang) + snprintf (locale, ENCODING_LEN + 1, "%ls", lang); + } + if (!*locale) + { + const char *env = __get_locale_env (_REENT, LC_CTYPE); + strncpy (locale, env, ENCODING_LEN); + locale[ENCODING_LEN] = '\0'; + } + loc = duplocale (__get_current_locale ()); + __loadlocale (loc, LC_CTYPE, locale); + charset = __locale_charset (loc); + } UINT codepage = CP_UTF8; /* Default UTF8 */ /* The internal charset names are well defined, so we can use shortcuts. */ @@ -1485,7 +1518,7 @@ __eval_codepage_from_internal_charset () codepage = 936; break; case 'I': /* ISO-8859-x */ - codepage = strtoul (charset + 9, NULL, 10); + codepage = strtoul (charset + 9, NULL, 10) + 28590; break; case 'S': /* SJIS */ codepage = 932; @@ -1493,6 +1526,8 @@ __eval_codepage_from_internal_charset () default: /* All set to UTF8 already */ break; } + if (loc) + freelocale (loc); return codepage; } diff --git a/winsup/cygwin/spawn.cc b/winsup/cygwin/spawn.cc index 02c4207ab..e9b808688 100644 --- a/winsup/cygwin/spawn.cc +++ b/winsup/cygwin/spawn.cc @@ -619,6 +619,18 @@ child_info_spawn::worker (const char *prog_arg, const char *const *argv, } } + if (!iscygwin ()) + { + cfd.rewind (); + while (cfd.next () >= 0) + if (cfd->get_major () == DEV_PTYS_MAJOR) + { + fhandler_pty_slave *ptys = + (fhandler_pty_slave *)(fhandler_base *) cfd; + ptys->setup_locale (envblock); + } + } + /* Set up needed handles for stdio */ si.dwFlags = STARTF_USESTDHANDLES; si.hStdInput = handle ((in__stdin < 0 ? 0 : in__stdin), false);