1 /*-------------------------------------------------------------------------
4 * simple_prompt() routine
6 * Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $PostgreSQL: pgsql/src/port/sprompt.c,v 1.9 2004/09/27 19:16:02 momjian 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).
35 bool prompt_state = false;
36 extern char *simple_prompt(const char *prompt, int maxlen, bool echo);
39 simple_prompt(const char *prompt, int maxlen, bool echo)
47 struct termios t_orig,
53 LPDWORD t_orig = NULL;
57 destination = (char *) malloc(maxlen + 1);
61 prompt_state = true; /* disable SIGINT */
64 * Do not try to collapse these into one "w+" mode file. Doesn't work
65 * on some platforms (eg, HPUX 10.20).
67 termin = fopen("/dev/tty", "r");
68 termout = fopen("/dev/tty", "w");
69 if (!termin || !termout)
82 tcgetattr(fileno(termin), &t);
85 tcsetattr(fileno(termin), TCSAFLUSH, &t);
91 /* get a new handle to turn echo off */
92 t_orig = (LPDWORD) malloc(sizeof(DWORD));
93 t = GetStdHandle(STD_INPUT_HANDLE);
95 /* save the old configuration first */
96 GetConsoleMode(t, t_orig);
98 /* set to the new mode */
99 SetConsoleMode(t, ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT);
106 fputs(gettext(prompt), termout);
110 if (fgets(destination, maxlen + 1, termin) == NULL)
111 destination[0] = '\0';
113 length = strlen(destination);
114 if (length > 0 && destination[length - 1] != '\n')
116 /* eat rest of the line */
122 if (fgets(buf, sizeof(buf), termin) == NULL)
124 buflen = strlen(buf);
125 } while (buflen > 0 && buf[buflen - 1] != '\n');
128 if (length > 0 && destination[length - 1] == '\n')
129 /* remove trailing newline */
130 destination[length - 1] = '\0';
132 #ifdef HAVE_TERMIOS_H
135 tcsetattr(fileno(termin), TCSAFLUSH, &t_orig);
136 fputs("\n", termout);
143 /* reset to the original console mode */
144 SetConsoleMode(t, *t_orig);
145 fputs("\n", termout);
158 prompt_state = false; /* SIGINT okay again */