Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / ntp / dist / lib / isc / unix / keyboard.c
blob36ef61cc3e4b62a15a83346388836c28de063bb5
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2004, 2007 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 2000, 2001 Internet Software Consortium.
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.
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.
20 /* Id: keyboard.c,v 1.13 2007/06/19 23:47:18 tbox Exp */
22 #include <config.h>
24 #include <sys/param.h>
25 #include <sys/types.h>
26 #include <sys/time.h>
27 #include <sys/uio.h>
29 #include <errno.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <termios.h>
33 #include <unistd.h>
34 #include <fcntl.h>
36 #include <isc/keyboard.h>
37 #include <isc/util.h>
39 isc_result_t
40 isc_keyboard_open(isc_keyboard_t *keyboard) {
41 int fd;
42 isc_result_t ret;
43 struct termios current_mode;
45 REQUIRE(keyboard != NULL);
47 fd = open("/dev/tty", O_RDONLY, 0);
48 if (fd < 0)
49 return (ISC_R_IOERROR);
51 keyboard->fd = fd;
53 if (tcgetattr(fd, &keyboard->saved_mode) < 0) {
54 ret = ISC_R_IOERROR;
55 goto errout;
58 current_mode = keyboard->saved_mode;
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;
67 current_mode.c_cc[VMIN] = 1;
68 current_mode.c_cc[VTIME] = 0;
69 if (tcsetattr(fd, TCSAFLUSH, &current_mode) < 0) {
70 ret = ISC_R_IOERROR;
71 goto errout;
74 keyboard->result = ISC_R_SUCCESS;
76 return (ISC_R_SUCCESS);
78 errout:
79 close (fd);
81 return (ret);
84 isc_result_t
85 isc_keyboard_close(isc_keyboard_t *keyboard, unsigned int sleeptime) {
86 REQUIRE(keyboard != NULL);
88 if (sleeptime > 0 && keyboard->result != ISC_R_CANCELED)
89 (void)sleep(sleeptime);
91 (void)tcsetattr(keyboard->fd, TCSAFLUSH, &keyboard->saved_mode);
92 (void)close(keyboard->fd);
94 keyboard->fd = -1;
96 return (ISC_R_SUCCESS);
99 isc_result_t
100 isc_keyboard_getchar(isc_keyboard_t *keyboard, unsigned char *cp) {
101 ssize_t cc;
102 unsigned char c;
103 cc_t *controlchars;
105 REQUIRE(keyboard != NULL);
106 REQUIRE(cp != NULL);
108 cc = read(keyboard->fd, &c, 1);
109 if (cc < 0) {
110 keyboard->result = ISC_R_IOERROR;
111 return (keyboard->result);
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);
120 *cp = c;
122 return (ISC_R_SUCCESS);
125 isc_boolean_t
126 isc_keyboard_canceled(isc_keyboard_t *keyboard) {
127 return (ISC_TF(keyboard->result == ISC_R_CANCELED));