[Patch] Fix incorrect code page when setting console title on Win10

Johannes Schindelin Johannes.Schindelin@gmx.de
Wed Aug 26 07:30:13 GMT 2020


Hi Corinna,

On Wed, 26 Aug 2020, Corinna Vinschen wrote:

> On Aug 26 16:43, 宫大汉 via Cygwin-patches wrote:
> > When Cygwin sets console titles on Win10 (has_con_24bit_colors && !con_is_legacy),
> > `WriteConsoleA` is used and causes an error if:
> > 1. the environment variable of `LANG` is `***.UTF-8`
> > 2. and the code page of console.exe is not UTF-8
> >   1. e.g. on my Computer, it's GB2312, for Chinese text
> >
> >
> > I've done some tests on msys2 and details are on https://github.com/git-for-windows/git/issues/2738,
> > and I filed a PR of https://github.com/git-for-windows/msys2-runtime/pull/25.

Just in case you want to have a look at it, you can download the patch via
https://github.com/git-for-windows/msys2-runtime/commit/334f52a53a2e6b7f560b0e8810b9f672ebb3ad24.patch

FWIW my original reviewer comment was: "why not fix wpbuf.send() in the
first place?" but after having a good look around, that method seemed to
be called from so many places that I "got cold feet" of that approach.

For one, I saw at least one caller that wants to send Escape sequences,
and I have no idea whether it is a good idea to do that in the `*W()`
version of the `WriteConsole()` function.

So the real question from my side is: how to address properly the many
uses of `WriteConsoleA()` (which breaks all kinds of encodings in many
situations because Windows' idea of the current code page and Cygwin's
idea of the current locale are pretty often at odds).

The patch discussed here circumvents one of those call sites.

However, even though there have not been any callers of `WriteConsoleA()`
in Cygwin v3.0.7 (but four callers of `WriteConsoleW()` which I suspect
were converted to `*A()` calls in v3.1.0 by the Pseudo Console patches),
there are now a whopping 15 callers of that `*A()` function in Cygwin
v3.1.7. See here:

	$ git grep WriteConsoleA
	winsup/cygwin/fhandler_console.cc:    WriteConsoleA (handle, buf, ixput, wn, 0);
	winsup/cygwin/fhandler_console.cc:          WriteConsoleA (get_output_handle (), "\033[?1h", 5, NULL, 0);
	winsup/cygwin/fhandler_console.cc:            WriteConsoleA (get_output_handle (), &last_char, 1, 0, 0);
	winsup/cygwin/fhandler_console.cc:                WriteConsoleA (get_output_handle (),
	winsup/cygwin/fhandler_console.cc:            WriteConsoleA (get_output_handle (), buf, strlen (buf), 0, 0);
	winsup/cygwin/fhandler_console.cc:            WriteConsoleA (get_output_handle (), buf, strlen (buf), 0, 0);
	winsup/cygwin/fhandler_console.cc:            WriteConsoleA (get_output_handle (), buf, strlen (buf), 0, 0);
	winsup/cygwin/fhandler_console.cc:            WriteConsoleA (get_output_handle (), buf, strlen (buf), 0, 0);
	winsup/cygwin/fhandler_console.cc:            WriteConsoleA (get_output_handle (), buf, strlen (buf), 0, 0);
	winsup/cygwin/fhandler_console.cc:            WriteConsoleA (get_output_handle (), buf, strlen (buf), 0, 0);
	winsup/cygwin/fhandler_console.cc:                    WriteConsoleA (get_output_handle (),
	winsup/cygwin/fhandler_tty.cc:DEF_HOOK (WriteConsoleA);
	winsup/cygwin/fhandler_tty.cc:WriteConsoleA_Hooked
	winsup/cygwin/fhandler_tty.cc:  return WriteConsoleA_Orig (h, p, l, n, o);
	winsup/cygwin/fhandler_tty.cc:      DO_HOOK (NULL, WriteConsoleA);

That cannot be intentional, can it? We should always thrive to use the
`*W()` functions so that we can be sure that the expected encoding is
used, right?

Ciao,
Dscho


More information about the Cygwin-patches mailing list