Received: from magus.postgresql.org ([87.238.57.229]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TNHn0-0003RJ-F6 for pgsql-bugs@postgresql.org; Sun, 14 Oct 2012 06:35:06 +0000 Received: from mail-lb0-f174.google.com ([209.85.217.174]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TNHms-00087a-FU for pgsql-bugs@postgresql.org; Sun, 14 Oct 2012 06:35:05 +0000 Received: by mail-lb0-f174.google.com with SMTP id n3so2828633lbo.19 for ; Sat, 13 Oct 2012 23:34:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:content-type; bh=/flgRPtX6j8sM6HYvemmOUKxQD8fLs+qfjZbenY60EU=; b=Y39hebHNnMkDThgXJhtkGSsBiJWJy1I77NA1lqLzzVRC1U45QH7BuIjV2Al2itw4OI gyJV1Cw5JM5Gfo1gUhYhBaF5wi4DFY+amCKv7LIvPWTaudzJtYCFF/vm2W5SGowzrTKu cAgIMXS4qVUAr1eKHBVpCwFk2fmQwT24qZ8WGjW1BahtuQtm5ZBj6VQstrRaZQ9AWemY J+purlwhrgSCTbkzaK+EhAbhB6NNvx5jGZ0R4H1ggJ9pzmBi0G4KsbNX8lcx2ugFm0Jd ylQfziR5Cz/Ar7lBNVbAYluV5IUSMuIFyjX8L68gQux7TiKYU7AbQeQVS3lY2nQCEdZf nE8w== Received: by 10.112.43.34 with SMTP id t2mr3097718lbl.109.1350196497596; Sat, 13 Oct 2012 23:34:57 -0700 (PDT) Received: from [1.0.0.7] ([213.132.88.193]) by mx.google.com with ESMTPS id gt17sm285000lab.6.2012.10.13.23.34.55 (version=SSLv3 cipher=OTHER); Sat, 13 Oct 2012 23:34:56 -0700 (PDT) Message-ID: <507A5D18.3080107@gmail.com> Date: Sun, 14 Oct 2012 10:35:04 +0400 From: Alexander Law User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121011 Thunderbird/16.0.1 MIME-Version: 1.0 To: Noah Misch CC: Alvaro Herrera , Pg Bugs Subject: Re: BUG #6510: A simple prompt is displayed using wrong charset References: <1331925178-sup-9261@alvh.no-ip.org> <4F65A533.7090208@gmail.com> <1332187409-sup-4631@alvh.no-ip.org> <4F68DF76.1030609@gmail.com> <20121010110555.GA21405@tornado.leadboat.com> In-Reply-To: <20121010110555.GA21405@tornado.leadboat.com> Content-Type: multipart/mixed; boundary="------------030400040702080507010307" X-Pg-Spam-Score: -2.7 (--) X-Archive-Number: 201210/125 X-Sequence-Number: 34425 This is a multi-part message in MIME format. --------------030400040702080507010307 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit 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 --------------030400040702080507010307 Content-Type: text/x-patch; name="sprompt.diff" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="sprompt.diff" --- a/src/port/sprompt.c +++ b/src/port/sprompt.c @@ -60,8 +60,13 @@ simple_prompt(const char *prompt, int maxlen, bool echo) * Do not try to collapse these into one "w+" mode file. Doesn't work on * some platforms (eg, HPUX 10.20). */ +#ifdef WIN32 + termin = fopen("CONIN$", "r"); + termout = fopen("CONOUT$", "w+"); +#else termin = fopen(DEVTTY, "r"); termout = fopen(DEVTTY, "w"); +#endif if (!termin || !termout #ifdef WIN32 /* See DEVTTY comment for msys */ --------------030400040702080507010307--