keyboard.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004, 2007  Internet Systems Consortium, Inc. ("ISC")
00003  * Copyright (C) 2000, 2001  Internet Software Consortium.
00004  *
00005  * Permission to use, copy, modify, and/or distribute this software for any
00006  * purpose with or without fee is hereby granted, provided that the above
00007  * copyright notice and this permission notice appear in all copies.
00008  *
00009  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
00010  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00011  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
00012  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00013  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00014  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00015  * PERFORMANCE OF THIS SOFTWARE.
00016  */
00017 
00018 /* $Id: keyboard.c,v 1.13 2007/06/19 23:47:18 tbox Exp $ */
00019 
00020 #include <config.h>
00021 
00022 #include <sys/param.h>
00023 #include <sys/types.h>
00024 #include <sys/time.h>
00025 #include <sys/uio.h>
00026 
00027 #include <errno.h>
00028 #include <stdlib.h>
00029 #include <string.h>
00030 #include <termios.h>
00031 #include <unistd.h>
00032 #include <fcntl.h>
00033 
00034 #include <isc/keyboard.h>
00035 #include <isc/util.h>
00036 
00037 isc_result_t
00038 isc_keyboard_open(isc_keyboard_t *keyboard) {
00039         int fd;
00040         isc_result_t ret;
00041         struct termios current_mode;
00042 
00043         REQUIRE(keyboard != NULL);
00044 
00045         fd = open("/dev/tty", O_RDONLY, 0);
00046         if (fd < 0)
00047                 return (ISC_R_IOERROR);
00048 
00049         keyboard->fd = fd;
00050 
00051         if (tcgetattr(fd, &keyboard->saved_mode) < 0) {
00052                 ret = ISC_R_IOERROR;
00053                 goto errout;
00054         }
00055 
00056         current_mode = keyboard->saved_mode;
00057 
00058         current_mode.c_iflag &=
00059                         ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
00060         current_mode.c_oflag &= ~OPOST;
00061         current_mode.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
00062         current_mode.c_cflag &= ~(CSIZE|PARENB);
00063         current_mode.c_cflag |= CS8;
00064 
00065         current_mode.c_cc[VMIN] = 1;
00066         current_mode.c_cc[VTIME] = 0;
00067         if (tcsetattr(fd, TCSAFLUSH, &current_mode) < 0) {
00068                 ret = ISC_R_IOERROR;
00069                 goto errout;
00070         }
00071 
00072         keyboard->result = ISC_R_SUCCESS;
00073 
00074         return (ISC_R_SUCCESS);
00075 
00076  errout:
00077         close (fd);
00078 
00079         return (ret);
00080 }
00081 
00082 isc_result_t
00083 isc_keyboard_close(isc_keyboard_t *keyboard, unsigned int sleeptime) {
00084         REQUIRE(keyboard != NULL);
00085 
00086         if (sleeptime > 0 && keyboard->result != ISC_R_CANCELED)
00087                 (void)sleep(sleeptime);
00088 
00089         (void)tcsetattr(keyboard->fd, TCSAFLUSH, &keyboard->saved_mode);
00090         (void)close(keyboard->fd);
00091 
00092         keyboard->fd = -1;
00093 
00094         return (ISC_R_SUCCESS);
00095 }
00096 
00097 isc_result_t
00098 isc_keyboard_getchar(isc_keyboard_t *keyboard, unsigned char *cp) {
00099         ssize_t cc;
00100         unsigned char c;
00101         cc_t *controlchars;
00102 
00103         REQUIRE(keyboard != NULL);
00104         REQUIRE(cp != NULL);
00105 
00106         cc = read(keyboard->fd, &c, 1);
00107         if (cc < 0) {
00108                 keyboard->result = ISC_R_IOERROR;
00109                 return (keyboard->result);
00110         }
00111 
00112         controlchars = keyboard->saved_mode.c_cc;
00113         if (c == controlchars[VINTR] || c == controlchars[VQUIT]) {
00114                 keyboard->result = ISC_R_CANCELED;
00115                 return (keyboard->result);
00116         }
00117 
00118         *cp = c;
00119 
00120         return (ISC_R_SUCCESS);
00121 }
00122 
00123 isc_boolean_t
00124 isc_keyboard_canceled(isc_keyboard_t *keyboard) {
00125         return (ISC_TF(keyboard->result == ISC_R_CANCELED));
00126 }

Generated on Tue Apr 28 17:41:05 2015 by Doxygen 1.5.4 for BIND9 Internals 9.11.0pre-alpha