1 /*-------------------------------------------------------------------------
4 * simple_prompt() routine
6 * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/common/sprompt.c
13 *-------------------------------------------------------------------------
17 #include "common/fe_memutils.h"
18 #include "common/string.h"
28 * Generalized function especially intended for reading in usernames and
29 * passwords interactively. Reads from /dev/tty or stdin/stderr.
31 * prompt: The prompt to print, or NULL if none (automatically localized)
32 * echo: Set to false if you want to hide what is entered (for passwords)
34 * The input (without trailing newline) is returned as a malloc'd string.
35 * Caller is responsible for freeing it when done.
38 simple_prompt(const char *prompt
, bool echo
)
40 return simple_prompt_extended(prompt
, echo
, NULL
);
44 * simple_prompt_extended
46 * This is the same as simple_prompt(), except that prompt_ctx can
47 * optionally be provided to allow this function to be canceled via an
48 * existing SIGINT signal handler that will longjmp to the specified place
49 * only when *(prompt_ctx->enabled) is true. If canceled, this function
50 * returns an empty string, and prompt_ctx->canceled is set to true.
53 simple_prompt_extended(const char *prompt
, bool echo
,
54 PromptInterruptContext
*prompt_ctx
)
59 #if defined(HAVE_TERMIOS_H)
60 struct termios t_orig
,
70 * A Windows console has an "input code page" and an "output code page";
71 * these usually match each other, but they rarely match the "Windows ANSI
72 * code page" defined at system boot and expected of "char *" arguments to
73 * Windows API functions. The Microsoft CRT write() implementation
74 * automatically converts text between these code pages when writing to a
75 * console. To identify such file descriptors, it calls GetConsoleMode()
76 * on the underlying HANDLE, which in turn requires GENERIC_READ access on
77 * the HANDLE. Opening termout in mode "w+" allows that detection to
78 * succeed. Otherwise, write() would not recognize the descriptor as a
79 * console, and non-ASCII characters would display incorrectly.
81 * XXX fgets() still receives text in the console's input code page. This
82 * makes non-ASCII credentials unportable.
84 * Unintuitively, we also open termin in mode "w+", even though we only
85 * read it; that's needed for SetConsoleMode() to succeed.
87 termin
= fopen("CONIN$", "w+");
88 termout
= fopen("CONOUT$", "w+");
92 * Do not try to collapse these into one "w+" mode file. Doesn't work on
93 * some platforms (eg, HPUX 10.20).
95 termin
= fopen("/dev/tty", "r");
96 termout
= fopen("/dev/tty", "w");
98 if (!termin
|| !termout
102 * Direct console I/O does not work from the MSYS 1.0.10 console. Writes
103 * reach nowhere user-visible; reads block indefinitely. XXX This affects
104 * most Windows terminal environments, including rxvt, mintty, Cygwin
105 * xterm, Cygwin sshd, and PowerShell ISE. Switch to a more-generic test.
107 || (getenv("OSTYPE") && strcmp(getenv("OSTYPE"), "msys") == 0)
121 #if defined(HAVE_TERMIOS_H)
122 /* disable echo via tcgetattr/tcsetattr */
123 tcgetattr(fileno(termin
), &t
);
126 tcsetattr(fileno(termin
), TCSAFLUSH
, &t
);
128 /* need the file's HANDLE to turn echo off */
129 t
= (HANDLE
) _get_osfhandle(_fileno(termin
));
131 /* save the old configuration first */
132 GetConsoleMode(t
, &t_orig
);
134 /* set to the new mode */
135 SetConsoleMode(t
, ENABLE_LINE_INPUT
| ENABLE_PROCESSED_INPUT
);
141 fputs(_(prompt
), termout
);
145 result
= pg_get_line(termin
, prompt_ctx
);
147 /* If we failed to read anything, just return an empty string */
149 result
= pg_strdup("");
151 /* strip trailing newline, including \r in case we're on Windows */
152 (void) pg_strip_crlf(result
);
156 /* restore previous echo behavior, then echo \n */
157 #if defined(HAVE_TERMIOS_H)
158 tcsetattr(fileno(termin
), TCSAFLUSH
, &t_orig
);
159 fputs("\n", termout
);
162 SetConsoleMode(t
, t_orig
);
163 fputs("\n", termout
);
167 else if (prompt_ctx
&& prompt_ctx
->canceled
)
169 /* also echo \n if prompt was canceled */
170 fputs("\n", termout
);