Fix unused parameter warnings
[cu.git] / input.c
blob9f9126cba12806c3195e713694a747b98981665f
1 /* $OpenBSD: input.c,v 1.2 2012/07/10 10:28:05 nicm Exp $ */
3 /*
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>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <signal.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <termios.h>
27 #include <unistd.h>
29 #include "cu.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);
40 void
41 input_signal(int sig __attribute__((unused)))
43 input_stop = 1;
46 const char *
47 get_input(const char *prompt)
49 static char s[BUFSIZ];
50 struct sigaction act, oact;
51 char c, *cp, *out = NULL;
52 ssize_t n;
54 memset(&act, 0, sizeof(act));
55 sigemptyset(&act.sa_mask);
56 act.sa_flags = 0;
57 act.sa_handler = input_signal;
58 if (sigaction(SIGINT, &act, &oact) != 0)
59 cu_err(1, "sigaction");
60 input_stop = 0;
62 restore_termios();
64 printf("%s ", prompt);
65 fflush(stdout);
67 cp = s;
68 while (cp != s + sizeof(s) - 1) {
69 n = read(STDIN_FILENO, &c, 1);
70 if (n == -1 && errno != EINTR)
71 cu_err(1, "read");
72 if (n != 1 || input_stop)
73 break;
74 if (c == '\n') {
75 out = s;
76 break;
78 if (!iscntrl((u_char)c))
79 *cp++ = c;
81 *cp = '\0';
83 set_termios();
85 sigaction(SIGINT, &oact, NULL);
87 return (out);