1 /* $OpenBSD: input.c,v 1.2 2012/07/10 10:28:05 nicm Exp $ */
4 * Copyright (c) 2012 Nicholas Marriott <nicm@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
32 * Prompt and read a line of user input from stdin. We want to use the termios
33 * we were started with so restore and stick in a signal handler for ^C.
36 volatile sig_atomic_t input_stop
;
38 void input_signal(int);
41 input_signal(int sig
__attribute__((unused
)))
47 get_input(const char *prompt
)
49 static char s
[BUFSIZ
];
50 struct sigaction act
, oact
;
51 char c
, *cp
, *out
= NULL
;
54 memset(&act
, 0, sizeof(act
));
55 sigemptyset(&act
.sa_mask
);
57 act
.sa_handler
= input_signal
;
58 if (sigaction(SIGINT
, &act
, &oact
) != 0)
59 cu_err(1, "sigaction");
64 printf("%s ", prompt
);
68 while (cp
!= s
+ sizeof(s
) - 1) {
69 n
= read(STDIN_FILENO
, &c
, 1);
70 if (n
== -1 && errno
!= EINTR
)
72 if (n
!= 1 || input_stop
)
78 if (!iscntrl((u_char
)c
))
85 sigaction(SIGINT
, &oact
, NULL
);