1 /*-------------------------------------------------------------------------
4 * simple_prompt() routine
6 * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $PostgreSQL: pgsql/src/port/sprompt.c,v 1.6 2004/06/24 18:53:48 tgl Exp $
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).
40 bool prompt_state = false;
41 extern char *simple_prompt(const char *prompt, int maxlen, bool echo);
44 simple_prompt(const char *prompt, int maxlen, bool echo)
52 struct termios t_orig,
58 LPDWORD t_orig = NULL;
62 destination = (char *) malloc(maxlen + 1);
66 prompt_state = true; /* disable SIGINT */
69 * Do not try to collapse these into one "w+" mode file. Doesn't work
70 * on some platforms (eg, HPUX 10.20).
72 termin = fopen("/dev/tty", "r");
73 termout = fopen("/dev/tty", "w");
74 if (!termin || !termout)
87 tcgetattr(fileno(termin), &t);
90 tcsetattr(fileno(termin), TCSAFLUSH, &t);
96 /* get a new handle to turn echo off */
97 t_orig = (LPDWORD) malloc(sizeof(DWORD));
98 t = GetStdHandle(STD_INPUT_HANDLE);
100 /* save the old configuration first */
101 GetConsoleMode(t, t_orig);
103 /* set to the new mode */
104 SetConsoleMode(t, ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT);
111 fputs(gettext(prompt), termout);
115 if (fgets(destination, maxlen + 1, termin) == NULL)
116 destination[0] = '\0';
118 length = strlen(destination);
119 if (length > 0 && destination[length - 1] != '\n')
121 /* eat rest of the line */
127 if (fgets(buf, sizeof(buf), termin) == NULL)
129 buflen = strlen(buf);
130 } while (buflen > 0 && buf[buflen - 1] != '\n');
133 if (length > 0 && destination[length - 1] == '\n')
134 /* remove trailing newline */
135 destination[length - 1] = '\0';
137 #ifdef HAVE_TERMIOS_H
140 tcsetattr(fileno(termin), TCSAFLUSH, &t_orig);
141 fputs("\n", termout);
148 /* reset to the original console mode */
149 SetConsoleMode(t, *t_orig);
150 fputs("\n", termout);
163 prompt_state = false; /* SIGINT okay again */