The attached is a patch that uses the _fileno/_get_osfhandle approach.
This doesn't address the stdin fallback, or error handling if opening
termin fails; however, it should be no worse than what's there now, and
it fixes the immediate problem.
I'm still thinking about the fallback in terms of a mintty-type
environment, but I wanted to get this patch out there in the meantime.
-Matt Stickney
From 734b979e688884947e1b820f5c1a40454bb35c03 Mon Sep 17 00:00:00 2001
From: Matthew Stickney <mtstick...@gmail.com>
Date: Mon, 21 May 2018 23:24:34 -0400
Subject: [PATCH] Disable input echo on the console, not stdin.
When data is piped to psql, a handle to stdin will no longer be a handle
to the console; SetConsoleMode will fail, and prompt input will be echoed
to the screen.
Instead, retrieve a handle to the console's input buffer fron termin, and
set the console mode there (this requires write permissions). The code is
now setting the console mode on the same handle it reads from, which should
always be consistent.
---
src/port/sprompt.c | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/src/port/sprompt.c b/src/port/sprompt.c
index 70dfa69d7b..340232a7a2 100644
--- a/src/port/sprompt.c
+++ b/src/port/sprompt.c
@@ -66,8 +66,12 @@ simple_prompt(const char *prompt, char *destination, size_t
destlen, bool echo)
*
* XXX fgets() still receives text in the console's input code page.
This
* makes non-ASCII credentials unportable.
+ *
+ * Note that termin must also be opened with write permissions in order
for
+ * SetConsoleMode() to succeed, even though it's only used for reading
here.
+ *
*/
- termin = fopen("CONIN$", "r");
+ termin = fopen("CONIN$", "w+");
termout = fopen("CONOUT$", "w+");
#else
@@ -112,7 +116,7 @@ simple_prompt(const char *prompt, char *destination, size_t
destlen, bool echo)
if (!echo)
{
/* get a new handle to turn echo off */
- t = GetStdHandle(STD_INPUT_HANDLE);
+ t = (HANDLE)_get_osfhandle(_fileno(termin));
/* save the old configuration first */
GetConsoleMode(t, &t_orig);
--
2.16.2.windows.1