2 * psql - the PostgreSQL interactive terminal
4 * Copyright 2000 by PostgreSQL Global Development Group
6 * $Header: /cvsroot/pgsql/src/port/sprompt.c,v 1.2 2003/10/26 04:29:15 momjian Exp $
13 * Generalized function especially intended for reading in usernames and
14 * password interactively. Reads from /dev/tty or stdin/stderr.
16 * prompt: The prompt to print
17 * maxlen: How many characters to accept
18 * echo: Set to false if you want to hide what is entered (for passwords)
20 * Returns a malloc()'ed string with the input (w/o trailing newline).
33 bool prompt_state = false;
34 extern char *simple_prompt(const char *prompt, int maxlen, bool echo);
37 simple_prompt(const char *prompt, int maxlen, bool echo)
45 struct termios t_orig,
55 destination = (char *) malloc(maxlen + 1);
59 prompt_state = true; /* disable SIGINT */
62 * Do not try to collapse these into one "w+" mode file. Doesn't work
63 * on some platforms (eg, HPUX 10.20).
65 termin = fopen("/dev/tty", "r");
66 termout = fopen("/dev/tty", "w");
67 if (!termin || !termout)
80 tcgetattr(fileno(termin), &t);
83 tcsetattr(fileno(termin), TCSAFLUSH, &t);
89 /* get a new handle to turn echo off */
90 t_orig = (LPDWORD) malloc(sizeof(DWORD));
91 t = GetStdHandle(STD_INPUT_HANDLE);
93 /* save the old configuration first */
94 GetConsoleMode(t, t_orig);
96 /* set to the new mode */
97 SetConsoleMode(t, ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT);
104 fputs(gettext(prompt), termout);
108 if (fgets(destination, maxlen + 1, termin) == NULL)
109 destination[0] = '\0';
111 length = strlen(destination);
112 if (length > 0 && destination[length - 1] != '\n')
114 /* eat rest of the line */
120 if (fgets(buf, sizeof(buf), termin) == NULL)
122 buflen = strlen(buf);
123 } while (buflen > 0 && buf[buflen - 1] != '\n');
126 if (length > 0 && destination[length - 1] == '\n')
127 /* remove trailing newline */
128 destination[length - 1] = '\0';
130 #ifdef HAVE_TERMIOS_H
133 tcsetattr(fileno(termin), TCSAFLUSH, &t_orig);
134 fputs("\n", termout);
141 /* reset to the original console mode */
142 SetConsoleMode(t, *t_orig);
143 fputs("\n", termout);
156 prompt_state = false; /* SIGINT okay again */