1 /*-------------------------------------------------------------------------
4 * simple_prompt() routine
6 * Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
13 *-------------------------------------------------------------------------
20 * Generalized function especially intended for reading in usernames and
21 * password interactively. Reads from /dev/tty or stdin/stderr.
23 * prompt: The prompt to print
24 * maxlen: How many characters to accept
25 * echo: Set to false if you want to hide what is entered (for passwords)
27 * Returns a malloc()'ed string with the input (w/o trailing newline).
35 extern char *simple_prompt(const char *prompt, int maxlen, bool echo);
38 simple_prompt(const char *prompt, int maxlen, bool echo)
46 struct termios t_orig,
51 LPDWORD t_orig = NULL;
55 destination = (char *) malloc(maxlen + 1);
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.
73 * XXX fgets() still receives text in the console's input code page. This
74 * makes non-ASCII credentials unportable.
76 termin = fopen("CONIN$", "r");
77 termout = fopen("CONOUT$", "w+");
81 * Do not try to collapse these into one "w+" mode file. Doesn't work on
82 * some platforms (eg, HPUX 10.20).
84 termin = fopen("/dev/tty", "r");
85 termout = fopen("/dev/tty", "w");
87 if (!termin || !termout
91 * Direct console I/O does not work from the MSYS 1.0.10 console. Writes
92 * reach nowhere user-visible; reads block indefinitely. XXX This affects
93 * most Windows terminal environments, including rxvt, mintty, Cygwin
94 * xterm, Cygwin sshd, and PowerShell ISE. Switch to a more-generic test.
96 || (getenv("OSTYPE") && strcmp(getenv("OSTYPE"), "msys") == 0)
108 #ifdef HAVE_TERMIOS_H
111 tcgetattr(fileno(termin), &t);
114 tcsetattr(fileno(termin), TCSAFLUSH, &t);
120 /* get a new handle to turn echo off */
121 t_orig = (LPDWORD) malloc(sizeof(DWORD));
122 t = GetStdHandle(STD_INPUT_HANDLE);
124 /* save the old configuration first */
125 GetConsoleMode(t, t_orig);
127 /* set to the new mode */
128 SetConsoleMode(t, ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT);
135 fputs(_(prompt), termout);
139 if (fgets(destination, maxlen + 1, termin) == NULL)
140 destination[0] = '\0';
142 length = strlen(destination);
143 if (length > 0 && destination[length - 1] != '\n')
145 /* eat rest of the line */
151 if (fgets(buf, sizeof(buf), termin) == NULL)
153 buflen = strlen(buf);
154 } while (buflen > 0 && buf[buflen - 1] != '\n');
157 if (length > 0 && destination[length - 1] == '\n')
158 /* remove trailing newline */
159 destination[length - 1] = '\0';
161 #ifdef HAVE_TERMIOS_H
164 tcsetattr(fileno(termin), TCSAFLUSH, &t_orig);
165 fputs("\n", termout);
172 /* reset to the original console mode */
173 SetConsoleMode(t, *t_orig);
174 fputs("\n", termout);