1 /* prompt.c -- prompt routines for vlock,
2 * the VT locking program for linux
4 * This program is copyright (C) 2007 Frank Benkstein, and is free
5 * software which is freely distributable under the terms of the
6 * GNU General Public License version 2, included as the file COPYING in this
7 * distribution. It is NOT public domain software, and any
8 * redistribution not permitted by the GNU General Public License is
9 * expressly forbidden without prior written permission from
13 * The prompt functions (prompt and prompt_echo_off) were
14 * inspired by/copied from openpam's openpam_ttyconv.c:
16 * Copyright (c) 2002-2003 Networks Associates Technology, Inc.
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 * 3. The name of the author may not be used to endorse or promote
27 * products derived from this software without specific prior written
30 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49 #include <sys/select.h>
54 #define PROMPT_BUFFER_SIZE 512
56 /* Prompt with the given string for a single line of input. The read string is
57 * returned in a new buffer that should be freed by the caller. If reading
58 * fails or the timeout (if given) occurs NULL is retured. */
59 char *prompt(const char *msg
, const struct timespec
*timeout
)
61 char buffer
[PROMPT_BUFFER_SIZE
];
65 struct timeval
*timeout_val
= NULL
;
70 if (timeout
!= NULL
) {
71 timeout_val
= malloc(sizeof *timeout_val
);
73 if (timeout_val
== NULL
)
76 timeout_val
->tv_sec
= timeout
->tv_sec
;
77 timeout_val
->tv_usec
= timeout
->tv_nsec
/ 1000;
81 /* Write out the prompt. */
82 (void) fputs(msg
, stderr
);
86 /* Get the current terminal attributes. */
87 (void) tcgetattr(STDIN_FILENO
, &term
);
88 /* Save the lflag value. */
90 /* Enable canonical mode. We're only interested in line buffering. */
91 term
.c_lflag
|= ICANON
;
92 /* Disable terminal signals. */
93 term
.c_lflag
&= ~ISIG
;
94 /* Set the terminal attributes. */
95 (void) tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &term
);
96 /* Discard all unread input characters. */
97 (void) tcflush(STDIN_FILENO
, TCIFLUSH
);
99 /* Initialize file descriptor set. */
101 FD_SET(STDIN_FILENO
, &readfds
);
106 /* Wait until a string was entered. */
107 if (select(STDIN_FILENO
+ 1, &readfds
, NULL
, NULL
, timeout_val
) != 1) {
109 perror("vlock-auth: select() on stdin failed");
111 fprintf(stderr
, "timeout!\n");
117 /* Read the string from stdin. At most buffer length - 1 bytes, to
118 * leave room for the terminating zero byte. */
119 if ((len
= read(STDIN_FILENO
, buffer
, sizeof buffer
- 1)) < 0) {
124 /* Terminate the string. */
127 /* Strip trailing newline characters. */
128 for (len
= strlen(buffer
); len
> 0; --len
) {
129 if (buffer
[len
- 1] != '\r' && buffer
[len
- 1] != '\n')
133 /* Terminate the string, again. */
136 /* Copy the string. */
137 result
= strdup(buffer
);
139 /* Clear our buffer. */
140 memset(buffer
, 0, sizeof buffer
);
145 /* Restore original terminal attributes. */
146 term
.c_lflag
= lflag
;
147 (void) tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &term
);
152 /* Same as prompt except that the characters entered are not echoed. */
153 char *prompt_echo_off(const char *msg
, const struct timespec
*timeout
)
159 (void) tcgetattr(STDIN_FILENO
, &term
);
160 lflag
= term
.c_lflag
;
161 term
.c_lflag
&= ~ECHO
;
162 (void) tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &term
);
164 result
= prompt(msg
, timeout
);
166 term
.c_lflag
= lflag
;
167 (void) tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &term
);
175 /* Read a single character from the stdin. If the timeout is reached
177 char read_character(struct timespec
*timeout
)
180 struct timeval
*timeout_val
= NULL
;
183 if (timeout
!= NULL
) {
184 timeout_val
= calloc(sizeof *timeout_val
, 1);
186 if (timeout_val
!= NULL
) {
187 timeout_val
->tv_sec
= timeout
->tv_sec
;
188 timeout_val
->tv_usec
= timeout
->tv_nsec
/ 1000;
192 /* Initialize file descriptor set. */
194 FD_SET(STDIN_FILENO
, &readfds
);
196 /* Wait for a character. */
197 if (select(STDIN_FILENO
+ 1, &readfds
, NULL
, NULL
, timeout_val
) != 1)
200 /* Read the character. */
201 (void) read(STDIN_FILENO
, &c
, 1);
208 /* Wait for any of the characters in the given character set to be read from
209 * stdin. If charset is NULL wait for any character. Returns 0 when the
211 char wait_for_character(const char *charset
, struct timespec
*timeout
)
217 /* switch off line buffering */
218 (void) tcgetattr(STDIN_FILENO
, &term
);
219 lflag
= term
.c_lflag
;
220 term
.c_lflag
&= ~ICANON
;
221 (void) tcsetattr(STDIN_FILENO
, TCSANOW
, &term
);
224 c
= read_character(timeout
);
226 if (c
== 0 || charset
== NULL
)
228 else if (strchr(charset
, c
) != NULL
)
232 /* restore line buffering */
233 term
.c_lflag
= lflag
;
234 (void) tcsetattr(STDIN_FILENO
, TCSANOW
, &term
);