uncrustify all source files
[vlock.git] / src / prompt.c
blobbe8e27cb81fc0efb9036bce106d2ba92a846dbef
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
10 * the author.
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
20 * are met:
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
28 * permission.
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
40 * SUCH DAMAGE.
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <termios.h>
48 #include <unistd.h>
49 #include <sys/select.h>
50 #include <errno.h>
52 #include <glib.h>
54 #include "prompt.h"
56 #define PROMPT_BUFFER_SIZE 512
58 GQuark vlock_prompt_error_quark(void)
60 return g_quark_from_static_string("vlock-prompt-error-quark");
63 /* Prompt with the given string for a single line of input. The read string is
64 * returned in a new buffer that should be freed by the caller. If reading
65 * fails or the timeout (if given) occurs NULL is retured. */
66 char *prompt(const char *msg, const struct timespec *timeout, GError **error)
68 char buffer[PROMPT_BUFFER_SIZE];
69 char *result = NULL;
70 ssize_t len;
71 struct termios term;
72 struct timeval *timeout_val = NULL;
73 tcflag_t lflag;
74 fd_set readfds;
76 if (msg != NULL) {
77 /* Write out the prompt. */
78 (void) fputs(msg, stderr);
79 fflush(stderr);
82 /* Get the current terminal attributes. */
83 (void) tcgetattr(STDIN_FILENO, &term);
84 /* Save the lflag value. */
85 lflag = term.c_lflag;
86 /* Enable canonical mode. We're only interested in line buffering. */
87 term.c_lflag |= ICANON;
88 /* Disable terminal signals. */
89 term.c_lflag &= ~ISIG;
90 /* Set the terminal attributes. */
91 (void) tcsetattr(STDIN_FILENO, TCSAFLUSH, &term);
92 /* Discard all unread input characters. */
93 (void) tcflush(STDIN_FILENO, TCIFLUSH);
95 /* Initialize file descriptor set. */
96 FD_ZERO(&readfds);
97 FD_SET(STDIN_FILENO, &readfds);
100 before_select:
101 /* copy timeout */
102 if (timeout != NULL) {
103 timeout_val = malloc(sizeof *timeout_val);
105 if (timeout_val == NULL) {
106 g_propagate_error(error,
107 g_error_new_literal(
108 VLOCK_PROMPT_ERROR,
109 VLOCK_PROMPT_ERROR_FAILED,
110 g_strerror(errno)));
111 return NULL;
114 timeout_val->tv_sec = timeout->tv_sec;
115 timeout_val->tv_usec = timeout->tv_nsec / 1000;
118 /* Reset errno. */
119 errno = 0;
121 /* Wait until a string was entered. */
122 if (select(STDIN_FILENO + 1, &readfds, NULL, NULL, timeout_val) != 1) {
123 switch (errno) {
124 case 0:
125 g_propagate_error(error,
126 g_error_new_literal(
127 VLOCK_PROMPT_ERROR, VLOCK_PROMPT_ERROR_TIMEOUT, ""));
128 goto out;
129 case EINTR:
130 /* A signal was caught. Restart. */
131 free(timeout_val);
132 goto before_select;
133 default:
134 g_propagate_error(error,
135 g_error_new_literal(
136 VLOCK_PROMPT_ERROR,
137 VLOCK_PROMPT_ERROR_FAILED,
138 g_strerror(errno)));
139 goto out;
143 /* Read the string from stdin. At most buffer length - 1 bytes, to
144 * leave room for the terminating zero byte. */
145 if ((len = read(STDIN_FILENO, buffer, sizeof buffer - 1)) < 0)
146 goto out;
148 /* Terminate the string. */
149 buffer[len] = '\0';
151 /* Strip trailing newline characters. */
152 for (len = strlen(buffer); len > 0; --len)
153 if (buffer[len - 1] != '\r' && buffer[len - 1] != '\n')
154 break;
156 /* Terminate the string, again. */
157 buffer[len] = '\0';
159 /* Copy the string. */
160 if ((result = strdup(buffer)) == NULL)
161 g_propagate_error(error,
162 g_error_new_literal(
163 VLOCK_PROMPT_ERROR,
164 VLOCK_PROMPT_ERROR_FAILED,
165 g_strerror(errno)));
167 /* Clear our buffer. */
168 memset(buffer, 0, sizeof buffer);
170 out:
171 free(timeout_val);
173 /* Restore original terminal attributes. */
174 term.c_lflag = lflag;
175 (void) tcsetattr(STDIN_FILENO, TCSAFLUSH, &term);
177 return result;
180 /* Same as prompt except that the characters entered are not echoed. */
181 char *prompt_echo_off(const char *msg,
182 const struct timespec *timeout,
183 GError **error)
185 struct termios term;
186 tcflag_t lflag;
187 char *result;
189 (void) tcgetattr(STDIN_FILENO, &term);
190 lflag = term.c_lflag;
191 term.c_lflag &= ~ECHO;
192 (void) tcsetattr(STDIN_FILENO, TCSAFLUSH, &term);
194 result = prompt(msg, timeout, error);
196 term.c_lflag = lflag;
197 (void) tcsetattr(STDIN_FILENO, TCSAFLUSH, &term);
199 if (result != NULL)
200 fputc('\n', stderr);
202 return result;
205 /* Read a single character from the stdin. If the timeout is reached
206 * 0 is returned. */
207 char read_character(struct timespec *timeout)
209 char c = 0;
210 struct timeval *timeout_val = NULL;
211 fd_set readfds;
213 if (timeout != NULL) {
214 timeout_val = calloc(sizeof *timeout_val, 1);
216 if (timeout_val != NULL) {
217 timeout_val->tv_sec = timeout->tv_sec;
218 timeout_val->tv_usec = timeout->tv_nsec / 1000;
222 /* Initialize file descriptor set. */
223 FD_ZERO(&readfds);
224 FD_SET(STDIN_FILENO, &readfds);
226 /* Wait for a character. */
227 if (select(STDIN_FILENO + 1, &readfds, NULL, NULL, timeout_val) != 1)
228 goto out;
230 /* Read the character. */
231 (void) read(STDIN_FILENO, &c, 1);
233 out:
234 free(timeout_val);
235 return c;
238 /* Wait for any of the characters in the given character set to be read from
239 * stdin. If charset is NULL wait for any character. Returns 0 when the
240 * timeout occurs. */
241 char wait_for_character(const char *charset, struct timespec *timeout)
243 struct termios term;
244 tcflag_t lflag;
245 char c;
247 /* switch off line buffering */
248 (void) tcgetattr(STDIN_FILENO, &term);
249 lflag = term.c_lflag;
250 term.c_lflag &= ~ICANON;
251 (void) tcsetattr(STDIN_FILENO, TCSANOW, &term);
253 for (;;) {
254 c = read_character(timeout);
256 if (c == 0 || charset == NULL)
257 break;
258 else if (strchr(charset, c) != NULL)
259 break;
262 /* restore line buffering */
263 term.c_lflag = lflag;
264 (void) tcsetattr(STDIN_FILENO, TCSANOW, &term);
266 return c;