Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TLu70-0004En-T2 for pgsql-bugs@postgresql.org; Wed, 10 Oct 2012 11:06:03 +0000 Received: from mail-qa0-f46.google.com ([209.85.216.46]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TLu6x-0001Dw-Qh for pgsql-bugs@postgresql.org; Wed, 10 Oct 2012 11:06:02 +0000 Received: by mail-qa0-f46.google.com with SMTP id c26so4358362qad.19 for ; Wed, 10 Oct 2012 04:05:58 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=date:from:to:cc:subject:message-id:references:mime-version :content-type:content-disposition:in-reply-to:user-agent :x-gm-message-state; bh=Fv4TC18ncsfwinSiKXn5JSuucyszRYs+Di0sqhi5BdI=; b=cxWggRfhc4WF/hLI915i3vxVoy7nqlY8H86C6G5UGtw6or4JxN8czxDbNrQmVP6TSw RoDk6OD55Ub9FXerHR7Axs8XRlQQVPKNhaQKJtGGUUKZ8ykB8lkqaBanRdp9DDdQBEZB 4U6UTpXmBh0BHEONjtgvONk7rJaFDifG44WgjJWrLoaeWFvw+qxpdlx+SzK1roqPQvrs 08TcWp3Xg1eP3E33ngQ36SY0QbLmSV87qHLrSL3xiE5Qr9BRYpBWIPb6qqiZNY87sW5b 4gW6VYhuKCPAFYENwreD4VE5SA8i5JuBDvV6Cuv19a8jIJPFW4w29f77AO71E94jZ8Xr Do/w== Received: by 10.224.53.18 with SMTP id k18mr40067275qag.1.1349867158379; Wed, 10 Oct 2012 04:05:58 -0700 (PDT) Received: from tornado.leadboat.com (ip68-105-236-221.hr.hr.cox.net. [68.105.236.221]) by mx.google.com with ESMTPS id g18sm1351197qan.1.2012.10.10.04.05.56 (version=SSLv3 cipher=OTHER); Wed, 10 Oct 2012 04:05:57 -0700 (PDT) Date: Wed, 10 Oct 2012 07:05:55 -0400 From: Noah Misch To: Alexander LAW Cc: Alvaro Herrera , Pg Bugs Subject: Re: BUG #6510: A simple prompt is displayed using wrong charset Message-ID: <20121010110555.GA21405@tornado.leadboat.com> References: <1331925178-sup-9261@alvh.no-ip.org> <4F65A533.7090208@gmail.com> <1332187409-sup-4631@alvh.no-ip.org> <4F68DF76.1030609@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <4F68DF76.1030609@gmail.com> User-Agent: Mutt/1.5.18 (2008-05-17) X-Gm-Message-State: ALoCoQnQCl/ea/deErvh/4W++Suz6N8RMdtriLn0v7Cjqg3IsMhnHUxiaKhRL/sapO4MsfY0ON7l X-Pg-Spam-Score: -2.6 (--) X-Archive-Number: 201210/74 X-Sequence-Number: 34374 Hi Alexander, I was able to reproduce the problem based on your description and test case, and your change does resolve it for me. On Tue, Mar 20, 2012 at 11:50:14PM +0400, Alexander LAW wrote: > Thanks, I've understood your point. > Please look at the patch. It implements the first way and it makes psql > work too. > 20.03.2012 00:05, Alvaro Herrera ??????????: >> Excerpts from Alexander LAW's message of dom mar 18 06:04:51 -0300 2012: >>> I see two ways to resolve the issue. >>> First is to use CharToOemBuff when writing a string to the "con" and >>> OemToCharBuff when reading an input from it. >>> The other is to always use stderr/stdin for Win32 as it was done for >>> msys before. I think it's more straightforward. >> Using console directly instead of stdin/out/err is more appropriate when >> asking for passwords and reading them back, because you can redirect the >> rest of the output to/from files or pipes, without the prompt >> interfering with that. This also explains why stderr is used instead of >> stdout. 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