1 /*-------------------------------------------------------------------------
4 * simple_prompt() routine
6 * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $PostgreSQL: pgsql/src/port/sprompt.c,v 1.18 2006/10/04 00:30:14 momjian Exp $
13 *-------------------------------------------------------------------------
15 * PostgreSQL Database Management System
16 * (formerly known as Postgres, then as Postgres95)
18 * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
20 * Portions Copyright (c) 1994, The Regents of the University of California
22 * Permission to use, copy, modify, and distribute this software and its
23 * documentation for any purpose, without fee, and without a written agreement
24 * is hereby granted, provided that the above copyright notice and this
25 * paragraph and the following two paragraphs appear in all copies.
27 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
28 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
29 * LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
30 * DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
33 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
34 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
35 * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
36 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATIONS TO
37 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
45 * Generalized function especially intended for reading in usernames and
46 * password interactively. Reads from /dev/tty or stdin/stderr.
48 * prompt: The prompt to print
49 * maxlen: How many characters to accept
50 * echo: Set to false if you want to hide what is entered (for passwords)
52 * Returns a malloc()'ed string with the input (w/o trailing newline).
55 #define DEVTTY "/dev/tty"
68 # define HAVE_TERMIOS_H
73 extern char *simple_prompt(const char *prompt, int maxlen, int echo);
77 simple_prompt(const char *prompt, int maxlen, int echo)
85 struct termios t_orig,
90 LPDWORD t_orig = NULL;
94 destination = (char *) malloc(maxlen + 1);
99 * Do not try to collapse these into one "w+" mode file. Doesn't work on
100 * some platforms (eg, HPUX 10.20).
102 termin = fopen(DEVTTY, "r");
103 termout = fopen(DEVTTY, "w");
104 if (!termin || !termout
106 /* See DEVTTY comment for msys */
107 || (getenv("OSTYPE") && strcmp(getenv("OSTYPE"), "msys") == 0)
119 #ifdef HAVE_TERMIOS_H
122 tcgetattr(fileno(termin), &t);
125 tcsetattr(fileno(termin), TCSAFLUSH, &t);
131 /* get a new handle to turn echo off */
132 t_orig = (LPDWORD) malloc(sizeof(DWORD));
133 t = GetStdHandle(STD_INPUT_HANDLE);
135 /* save the old configuration first */
136 GetConsoleMode(t, t_orig);
138 /* set to the new mode */
139 SetConsoleMode(t, ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT);
146 fputs(prompt, termout);
150 if (fgets(destination, maxlen + 1, termin) == NULL)
151 destination[0] = '\0';
153 length = strlen(destination);
154 if (length > 0 && destination[length - 1] != '\n')
156 /* eat rest of the line */
162 if (fgets(buf, sizeof(buf), termin) == NULL)
164 buflen = strlen(buf);
166 while (buflen > 0 && buf[buflen - 1] != '\n');
169 if (length > 0 && destination[length - 1] == '\n')
170 /* remove trailing newline */
171 destination[length - 1] = '\0';
173 #ifdef HAVE_TERMIOS_H
176 tcsetattr(fileno(termin), TCSAFLUSH, &t_orig);
177 fputs("\n", termout);
184 /* reset to the original console mode */
185 SetConsoleMode(t, *t_orig);
186 fputs("\n", termout);