Skip to content

Commit 1068771

Browse files
committed
Use correct output device for Windows prompts.
This ensures that mapping of non-ascii prompts to the correct code page occurs. Bug report and original patch from Alexander Law, reviewed and reworked by Noah Misch. Backpatch to all live branches.
1 parent a9ceaa5 commit 1068771

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

src/bin/psql/command.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,6 +1043,17 @@ exec_command(const char *cmd,
10431043
char *fname = psql_scan_slash_option(scan_state,
10441044
OT_NORMAL, NULL, true);
10451045

1046+
#if defined(WIN32) && !defined(__CYGWIN__)
1047+
1048+
/*
1049+
* XXX This does not work for all terminal environments or for output
1050+
* containing non-ASCII characters; see comments in simple_prompt().
1051+
*/
1052+
#define DEVTTY "con"
1053+
#else
1054+
#define DEVTTY "/dev/tty"
1055+
#endif
1056+
10461057
expand_tilde(&fname);
10471058
/* This scrolls off the screen when using /dev/tty */
10481059
success = saveHistory(fname ? fname : DEVTTY, -1, false, false);

src/include/port.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,8 @@ extern BOOL AddUserToTokenDacl(HANDLE hToken);
110110

111111
#if defined(WIN32) && !defined(__CYGWIN__)
112112
#define DEVNULL "nul"
113-
/* "con" does not work from the Msys 1.0.10 console (part of MinGW). */
114-
#define DEVTTY "con"
115113
#else
116114
#define DEVNULL "/dev/null"
117-
#define DEVTTY "/dev/tty"
118115
#endif
119116

120117
/*

src/port/sprompt.c

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,42 @@ simple_prompt(const char *prompt, int maxlen, bool echo)
5656
if (!destination)
5757
return NULL;
5858

59+
#ifdef WIN32
60+
61+
/*
62+
* A Windows console has an "input code page" and an "output code page";
63+
* these usually match each other, but they rarely match the "Windows ANSI
64+
* code page" defined at system boot and expected of "char *" arguments to
65+
* Windows API functions. The Microsoft CRT write() implementation
66+
* automatically converts text between these code pages when writing to a
67+
* console. To identify such file descriptors, it calls GetConsoleMode()
68+
* on the underlying HANDLE, which in turn requires GENERIC_READ access on
69+
* the HANDLE. Opening termout in mode "w+" allows that detection to
70+
* succeed. Otherwise, write() would not recognize the descriptor as a
71+
* console, and non-ASCII characters would display incorrectly.
72+
*
73+
* XXX fgets() still receives text in the console's input code page. This
74+
* makes non-ASCII credentials unportable.
75+
*/
76+
termin = fopen("CONIN$", "r");
77+
termout = fopen("CONOUT$", "w+");
78+
#else
79+
5980
/*
6081
* Do not try to collapse these into one "w+" mode file. Doesn't work on
6182
* some platforms (eg, HPUX 10.20).
6283
*/
63-
termin = fopen(DEVTTY, "r");
64-
termout = fopen(DEVTTY, "w");
84+
termin = fopen("/dev/tty", "r");
85+
termout = fopen("/dev/tty", "w");
86+
#endif
6587
if (!termin || !termout
6688
#ifdef WIN32
67-
/* See DEVTTY comment for msys */
89+
/*
90+
* Direct console I/O does not work from the MSYS 1.0.10 console. Writes
91+
* reach nowhere user-visible; reads block indefinitely. XXX This affects
92+
* most Windows terminal environments, including rxvt, mintty, Cygwin
93+
* xterm, Cygwin sshd, and PowerShell ISE. Switch to a more-generic test.
94+
*/
6895
|| (getenv("OSTYPE") && strcmp(getenv("OSTYPE"), "msys") == 0)
6996
#endif
7097
)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy