1 /* $NetBSD: keyboard.c,v 1.1.1.1 2009/12/13 16:54:41 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.7 2007/06/19 23:47:19 tbox Exp */ 21 22 #include <config.h> 23 24 #include <sys/types.h> 25 26 #include <windows.h> 27 #include <errno.h> 28 #include <stdlib.h> 29 #include <string.h> 30 #include <stdio.h> 31 #include <unistd.h> 32 #include <fcntl.h> 33 34 #include <io.h> 35 36 #include <isc/keyboard.h> 37 #include <isc/util.h> 38 39 isc_result_t 40 isc_keyboard_open(isc_keyboard_t *keyboard) { 41 int fd; 42 43 REQUIRE(keyboard != NULL); 44 45 fd = _fileno(stdin); 46 if (fd < 0) 47 return (ISC_R_IOERROR); 48 49 keyboard->fd = fd; 50 51 keyboard->result = ISC_R_SUCCESS; 52 53 return (ISC_R_SUCCESS); 54 } 55 56 isc_result_t 57 isc_keyboard_close(isc_keyboard_t *keyboard, unsigned int sleeptime) { 58 REQUIRE(keyboard != NULL); 59 60 if (sleeptime > 0 && keyboard->result != ISC_R_CANCELED) 61 (void)Sleep(sleeptime*1000); 62 63 keyboard->fd = -1; 64 65 return (ISC_R_SUCCESS); 66 } 67 68 isc_result_t 69 isc_keyboard_getchar(isc_keyboard_t *keyboard, unsigned char *cp) { 70 ssize_t cc; 71 unsigned char c; 72 73 REQUIRE(keyboard != NULL); 74 REQUIRE(cp != NULL); 75 76 cc = read(keyboard->fd, &c, 1); 77 if (cc < 0) { 78 keyboard->result = ISC_R_IOERROR; 79 return (keyboard->result); 80 } 81 82 *cp = c; 83 84 return (ISC_R_SUCCESS); 85 } 86 87 isc_boolean_t 88 isc_keyboard_canceled(isc_keyboard_t *keyboard) { 89 return (ISC_TF(keyboard->result == ISC_R_CANCELED)); 90 } 91 92