1 /*-------------------------------------------------------------------------
4 * simple_prompt() routine
6 * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
13 *-------------------------------------------------------------------------
25 * Generalized function especially intended for reading in usernames and
26 * passwords interactively. Reads from /dev/tty or stdin/stderr.
28 * prompt: The prompt to print, or NULL if none (automatically localized)
29 * destination: buffer in which to store result
30 * destlen: allocated length of destination
31 * echo: Set to false if you want to hide what is entered (for passwords)
33 * The input (without trailing newline) is returned in the destination buffer,
34 * with a '\0' appended.
37 simple_prompt(const char *prompt, char *destination, size_t destlen, bool echo)
44 struct termios t_orig,
56 * A Windows console has an "input code page" and an "output code page";
57 * these usually match each other, but they rarely match the "Windows ANSI
58 * code page" defined at system boot and expected of "char *" arguments to
59 * Windows API functions. The Microsoft CRT write() implementation
60 * automatically converts text between these code pages when writing to a
61 * console. To identify such file descriptors, it calls GetConsoleMode()
62 * on the underlying HANDLE, which in turn requires GENERIC_READ access on
63 * the HANDLE. Opening termout in mode "w+" allows that detection to
64 * succeed. Otherwise, write() would not recognize the descriptor as a
65 * console, and non-ASCII characters would display incorrectly.
67 * XXX fgets() still receives text in the console's input code page. This
68 * makes non-ASCII credentials unportable.
70 termin = fopen("CONIN$", "r");
71 termout = fopen("CONOUT$", "w+");
75 * Do not try to collapse these into one "w+" mode file. Doesn't work on
76 * some platforms (eg, HPUX 10.20).
78 termin = fopen("/dev/tty", "r");
79 termout = fopen("/dev/tty", "w");
81 if (!termin || !termout
85 * Direct console I/O does not work from the MSYS 1.0.10 console. Writes
86 * reach nowhere user-visible; reads block indefinitely. XXX This affects
87 * most Windows terminal environments, including rxvt, mintty, Cygwin
88 * xterm, Cygwin sshd, and PowerShell ISE. Switch to a more-generic test.
90 || (getenv("OSTYPE") && strcmp(getenv("OSTYPE"), "msys") == 0)
102 #ifdef HAVE_TERMIOS_H
105 tcgetattr(fileno(termin), &t);
108 tcsetattr(fileno(termin), TCSAFLUSH, &t);
114 /* get a new handle to turn echo off */
115 t = GetStdHandle(STD_INPUT_HANDLE);
117 /* save the old configuration first */
118 GetConsoleMode(t, &t_orig);
120 /* set to the new mode */
121 SetConsoleMode(t, ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT);
128 fputs(_(prompt), termout);
132 if (fgets(destination, destlen, termin) == NULL)
133 destination[0] = '\0';
135 length = strlen(destination);
136 if (length > 0 && destination[length - 1] != '\n')
138 /* eat rest of the line */
144 if (fgets(buf, sizeof(buf), termin) == NULL)
146 buflen = strlen(buf);
147 } while (buflen > 0 && buf[buflen - 1] != '\n');
150 if (length > 0 && destination[length - 1] == '\n')
151 /* remove trailing newline */
152 destination[length - 1] = '\0';
154 #ifdef HAVE_TERMIOS_H
157 tcsetattr(fileno(termin), TCSAFLUSH, &t_orig);
158 fputs("\n", termout);
165 /* reset to the original console mode */
166 SetConsoleMode(t, t_orig);
167 fputs("\n", termout);