1/* $NetBSD: keyboard.c,v 1.1.1.1 2009/12/13 16:54:33 kardel Exp $ */ 2 3/* 4 * Copyright (C) 2004, 2007 Internet Systems Consortium, Inc. ("ISC") 5 * Copyright (C) 2000, 2001 Internet Software Consortium. 6 * 7 * Permission to use, copy, modify, and/or distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20/* Id: keyboard.c,v 1.13 2007/06/19 23:47:18 tbox Exp */ 21 22#include <config.h> 23 24#include <sys/param.h> 25#include <sys/types.h> 26#include <sys/time.h> 27#include <sys/uio.h> 28 29#include <errno.h> 30#include <stdlib.h> 31#include <string.h> 32#include <termios.h> 33#include <unistd.h> 34#include <fcntl.h> 35 36#include <isc/keyboard.h> 37#include <isc/util.h> 38 39isc_result_t 40isc_keyboard_open(isc_keyboard_t *keyboard) { 41 int fd; 42 isc_result_t ret; 43 struct termios current_mode; 44 45 REQUIRE(keyboard != NULL); 46 47 fd = open("/dev/tty", O_RDONLY, 0); 48 if (fd < 0) 49 return (ISC_R_IOERROR); 50 51 keyboard->fd = fd; 52 53 if (tcgetattr(fd, &keyboard->saved_mode) < 0) { 54 ret = ISC_R_IOERROR; 55 goto errout; 56 } 57 58 current_mode = keyboard->saved_mode; 59 60 current_mode.c_iflag &= 61 ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON); 62 current_mode.c_oflag &= ~OPOST; 63 current_mode.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); 64 current_mode.c_cflag &= ~(CSIZE|PARENB); 65 current_mode.c_cflag |= CS8; 66 67 current_mode.c_cc[VMIN] = 1; 68 current_mode.c_cc[VTIME] = 0; 69 if (tcsetattr(fd, TCSAFLUSH, ¤t_mode) < 0) { 70 ret = ISC_R_IOERROR; 71 goto errout; 72 } 73 74 keyboard->result = ISC_R_SUCCESS; 75 76 return (ISC_R_SUCCESS); 77 78 errout: 79 close (fd); 80 81 return (ret); 82} 83 84isc_result_t 85isc_keyboard_close(isc_keyboard_t *keyboard, unsigned int sleeptime) { 86 REQUIRE(keyboard != NULL); 87 88 if (sleeptime > 0 && keyboard->result != ISC_R_CANCELED) 89 (void)sleep(sleeptime); 90 91 (void)tcsetattr(keyboard->fd, TCSAFLUSH, &keyboard->saved_mode); 92 (void)close(keyboard->fd); 93 94 keyboard->fd = -1; 95 96 return (ISC_R_SUCCESS); 97} 98 99isc_result_t 100isc_keyboard_getchar(isc_keyboard_t *keyboard, unsigned char *cp) { 101 ssize_t cc; 102 unsigned char c; 103 cc_t *controlchars; 104 105 REQUIRE(keyboard != NULL); 106 REQUIRE(cp != NULL); 107 108 cc = read(keyboard->fd, &c, 1); 109 if (cc < 0) { 110 keyboard->result = ISC_R_IOERROR; 111 return (keyboard->result); 112 } 113 114 controlchars = keyboard->saved_mode.c_cc; 115 if (c == controlchars[VINTR] || c == controlchars[VQUIT]) { 116 keyboard->result = ISC_R_CANCELED; 117 return (keyboard->result); 118 } 119 120 *cp = c; 121 122 return (ISC_R_SUCCESS); 123} 124 125isc_boolean_t 126isc_keyboard_canceled(isc_keyboard_t *keyboard) { 127 return (ISC_TF(keyboard->result == ISC_R_CANCELED)); 128} 129