Re: BUG #6510: A simple prompt is displayed using wrong charset

From: Alexander Law <exclusion(at)gmail(dot)com>
To: Noah Misch <noah(at)leadboat(dot)com>
Cc: Alvaro Herrera <alvherre(at)commandprompt(dot)com>, Pg Bugs <pgsql-bugs(at)postgresql(dot)org>
Subject: Re: BUG #6510: A simple prompt is displayed using wrong charset
Date: 2012-10-14 06:35:04
Message-ID: 507A5D18.3080107@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs pgsql-general pgsql-hackers

Hi Noah,

Thank you for your review.
I agree with you, CONOUT$ way is much simpler. Please look at the patch.
Regarding msys - yes, that check was not correct.
In fact you can use "con" with msys, if you run sh.exe, not a graphical
terminal.
So the issue with con not related to msys, but to some terminal
implementations.
Namely, I see that con is not supported by rxvt, mintty and xterm (from
x.cygwin project).
(rxvt was default terminal for msys 1.0.10, so I think such behavior was
considered as msys feature because of this)
Your solution to use IsWindowVisible(GetConsoleWindow()) works for these
terminals (I've made simple test and it returns false for all of them),
but this check will not work for telnet (console app running through
telnet can use con/conout).
Maybe this should be considered as a distinct bug with another patch
required? (I see no ideal solution for it yet. Probably it's possible to
detect not "ostype", but these terminals, though it would not be generic
too.)
And there is another issue with a console charset. When writing string
to a console CRT converts it to console encoding, but when reading input
back it doesn't. So it seems, there should be conversion from
ConsoleCP() to ACP() and then probably to UTF-8 to make postgres
utilities support national chars in passwords or usernames (with
createuser --interactive).
I think it can be fixed as another bug too.

Best regards,
Alexander

10.10.2012 15:05, Noah Misch wrote:
> Hi Alexander,
>
>
> The console output code page will usually match the OEM code page, but this is
> not guaranteed. For example, one can change it with chcp.exe before starting
> psql. The conversion should be to the actual console output code page. After
> "chcp 869", notice how printing to stdout yields question marks while your
> code yields unrelated characters.
>
> It would be nicer still to find a way to make the output routines treat this
> explicitly-opened console like stdout to a console. I could not find any
> documentation around this. Digging into the CRT source code, I see that the
> automatic code page conversion happens in write(). One of tests write() uses
> to determine whether the destination is a console is to call GetConsoleMode()
> on the HANDLE underlying the CRT file descriptor. If that call fails, write()
> assumes the target is not a console. GetConsoleMode() requires GENERIC_READ
> access on its subject HANDLE, but the HANDLE resulting from our fopen("con",
> "w") has only GENERIC_WRITE. Therefore, write() wrongly concludes that it's
> writing to a non-console. fopen("con", "w+") fails, but fopen("CONOUT$",
> "w+") seems to give the outcome we need. write() recognizes that it's writing
> to a console and applies the code page conversion. Let's use that.
>
> This gave me occasion to look at the special case for MSYS that you mentioned.
> I observe the same behavior when running a native psql in a Cygwin xterm;
> writes to the console succeed but do not appear anywhere. Instead of guessing
> at console visibility based on an environment variable witnessing a particular
> platform, let's check IsWindowVisible(GetConsoleWindow()).
>
> What do you think of taking that approach?
>
> Thanks,
> nm

Attachment Content-Type Size
sprompt.diff text/x-patch 482 bytes

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Hiroshi Saito 2012-10-14 06:52:04 Re: [BUGS] Sorry to bother you. Please tell me exactly how the compiler was built ODBC
Previous Message John R Pierce 2012-10-14 06:19:31 Re: BUG #7592: Installation of PostGresql at c:\Progam files folder does not create right permission on data folder

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2012-10-14 16:10:42 Re: BUG #6510: A simple prompt is displayed using wrong charset
Previous Message Arvind Singh 2012-10-14 06:14:32 Re: Improve MMO Game Performance

Browse pgsql-hackers by date

  From Date Subject
Next Message Simon Riggs 2012-10-14 08:19:27 Re: tuplesort memory usage: grow_memtuples
Previous Message Jeff Janes 2012-10-14 05:22:54 Re: tuplesort memory usage: grow_memtuples