2 * Copyright (C) 2004, 2007 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2000, 2001 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: keyboard.c,v 1.13 2007/06/19 23:47:18 tbox Exp $ */
22 #include <sys/param.h>
23 #include <sys/types.h>
34 #include <isc/keyboard.h>
38 isc_keyboard_open(isc_keyboard_t *keyboard) {
41 struct termios current_mode;
43 REQUIRE(keyboard != NULL);
45 fd = open("/dev/tty", O_RDONLY, 0);
47 return (ISC_R_IOERROR);
51 if (tcgetattr(fd, &keyboard->saved_mode) < 0) {
56 current_mode = keyboard->saved_mode;
58 current_mode.c_iflag &=
59 ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
60 current_mode.c_oflag &= ~OPOST;
61 current_mode.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
62 current_mode.c_cflag &= ~(CSIZE|PARENB);
63 current_mode.c_cflag |= CS8;
65 current_mode.c_cc[VMIN] = 1;
66 current_mode.c_cc[VTIME] = 0;
67 if (tcsetattr(fd, TCSAFLUSH, ¤t_mode) < 0) {
72 keyboard->result = ISC_R_SUCCESS;
74 return (ISC_R_SUCCESS);
83 isc_keyboard_close(isc_keyboard_t *keyboard, unsigned int sleeptime) {
84 REQUIRE(keyboard != NULL);
86 if (sleeptime > 0 && keyboard->result != ISC_R_CANCELED)
87 (void)sleep(sleeptime);
89 (void)tcsetattr(keyboard->fd, TCSAFLUSH, &keyboard->saved_mode);
90 (void)close(keyboard->fd);
94 return (ISC_R_SUCCESS);
98 isc_keyboard_getchar(isc_keyboard_t *keyboard, unsigned char *cp) {
103 REQUIRE(keyboard != NULL);
106 cc = read(keyboard->fd, &c, 1);
108 keyboard->result = ISC_R_IOERROR;
109 return (keyboard->result);
112 controlchars = keyboard->saved_mode.c_cc;
113 if (c == controlchars[VINTR] || c == controlchars[VQUIT]) {
114 keyboard->result = ISC_R_CANCELED;
115 return (keyboard->result);
120 return (ISC_R_SUCCESS);
124 isc_keyboard_canceled(isc_keyboard_t *keyboard) {
125 return (ISC_TF(keyboard->result == ISC_R_CANCELED));