00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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, ¤t_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 }