1 /* vi: set sw=4 ts=4: */
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
11 /* do nothing signal handler */
12 static void askpass_timeout(int UNUSED_PARAM ignore)
16 char* FAST_FUNC bb_ask_noecho(int fd, int timeout, const char *prompt)
18 #define MAX_LINE 0xfff
21 struct sigaction sa, oldsa;
22 struct termios tio, oldtio;
24 tcflush(fd, TCIFLUSH);
25 /* Was buggy: was printing prompt *before* flushing input,
26 * which was upsetting "expect" based scripts of some users.
28 fputs(prompt, stdout);
31 tcgetattr(fd, &oldtio);
33 /* Switch off echo. ECHOxyz meaning:
34 * ECHO echo input chars
35 * ECHOE echo BS-SP-BS on erase character
36 * ECHOK echo kill char specially, not as ^c (ECHOKE controls how exactly)
37 * ECHOKE erase all input via BS-SP-BS on kill char (else go to next line)
38 * ECHOCTL Echo ctrl chars as ^c (else echo verbatim:
39 * e.g. up arrow emits "ESC-something" and thus moves cursor up!)
40 * ECHONL Echo NL even if ECHO is not set
41 * ECHOPRT On erase, echo erased chars
42 * [qwe<BS><BS><BS> input looks like "qwe\ewq/" on screen]
44 tio.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL);
45 tcsetattr(fd, TCSANOW, &tio);
47 memset(&sa, 0, sizeof(sa));
48 /* sa.sa_flags = 0; - no SA_RESTART! */
49 /* SIGINT and SIGALRM will interrupt reads below */
50 sa.sa_handler = askpass_timeout;
51 sigaction(SIGINT, &sa, &oldsa);
53 sigaction_set(SIGALRM, &sa);
62 /* User input is uber-slow, no need to optimize reallocs.
63 * Grow it on every char.
65 ret = xrealloc(ret, i + 2);
66 r = read(fd, &ret[i], 1);
68 if ((i == 0 && r == 0) /* EOF (^D) with no password */
69 || r < 0 /* read is interrupted by timeout or ^C */
71 ret[i] = '\0'; /* paranoia */
72 nuke_str(ret); /* paranoia */
79 || ret[i] == '\r' || ret[i] == '\n' /* EOL */
80 || ++i == MAX_LINE /* line limit */
90 sigaction_set(SIGINT, &oldsa);
91 tcsetattr(fd, TCSANOW, &oldtio);
96 char* FAST_FUNC bb_ask_noecho_stdin(const char *prompt)
98 return bb_ask_noecho(STDIN_FILENO, 0, prompt);