1258945Sroberto/*
2258945Sroberto * Copyright (C) 2004, 2007  Internet Systems Consortium, Inc. ("ISC")
3258945Sroberto * Copyright (C) 2000, 2001  Internet Software Consortium.
4258945Sroberto *
5258945Sroberto * Permission to use, copy, modify, and/or distribute this software for any
6258945Sroberto * purpose with or without fee is hereby granted, provided that the above
7258945Sroberto * copyright notice and this permission notice appear in all copies.
8258945Sroberto *
9258945Sroberto * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10258945Sroberto * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11258945Sroberto * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12258945Sroberto * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13258945Sroberto * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14258945Sroberto * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15258945Sroberto * PERFORMANCE OF THIS SOFTWARE.
16258945Sroberto */
17258945Sroberto
18258945Sroberto/* $Id: keyboard.c,v 1.13 2007/06/19 23:47:18 tbox Exp $ */
19258945Sroberto
20258945Sroberto#include <config.h>
21258945Sroberto
22258945Sroberto#include <sys/param.h>
23258945Sroberto#include <sys/types.h>
24258945Sroberto#include <sys/time.h>
25258945Sroberto#include <sys/uio.h>
26258945Sroberto
27258945Sroberto#include <errno.h>
28258945Sroberto#include <stdlib.h>
29258945Sroberto#include <string.h>
30258945Sroberto#include <termios.h>
31258945Sroberto#include <unistd.h>
32258945Sroberto#include <fcntl.h>
33258945Sroberto
34258945Sroberto#include <isc/keyboard.h>
35258945Sroberto#include <isc/util.h>
36258945Sroberto
37258945Srobertoisc_result_t
38258945Srobertoisc_keyboard_open(isc_keyboard_t *keyboard) {
39258945Sroberto	int fd;
40258945Sroberto	isc_result_t ret;
41258945Sroberto	struct termios current_mode;
42258945Sroberto
43258945Sroberto	REQUIRE(keyboard != NULL);
44258945Sroberto
45258945Sroberto	fd = open("/dev/tty", O_RDONLY, 0);
46258945Sroberto	if (fd < 0)
47258945Sroberto		return (ISC_R_IOERROR);
48258945Sroberto
49258945Sroberto	keyboard->fd = fd;
50258945Sroberto
51258945Sroberto	if (tcgetattr(fd, &keyboard->saved_mode) < 0) {
52258945Sroberto		ret = ISC_R_IOERROR;
53258945Sroberto		goto errout;
54258945Sroberto	}
55258945Sroberto
56258945Sroberto	current_mode = keyboard->saved_mode;
57258945Sroberto
58258945Sroberto	current_mode.c_iflag &=
59258945Sroberto			~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
60258945Sroberto	current_mode.c_oflag &= ~OPOST;
61258945Sroberto	current_mode.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
62258945Sroberto	current_mode.c_cflag &= ~(CSIZE|PARENB);
63258945Sroberto	current_mode.c_cflag |= CS8;
64258945Sroberto
65258945Sroberto	current_mode.c_cc[VMIN] = 1;
66258945Sroberto	current_mode.c_cc[VTIME] = 0;
67258945Sroberto	if (tcsetattr(fd, TCSAFLUSH, &current_mode) < 0) {
68258945Sroberto		ret = ISC_R_IOERROR;
69258945Sroberto		goto errout;
70258945Sroberto	}
71258945Sroberto
72258945Sroberto	keyboard->result = ISC_R_SUCCESS;
73258945Sroberto
74258945Sroberto	return (ISC_R_SUCCESS);
75258945Sroberto
76258945Sroberto errout:
77258945Sroberto	close (fd);
78258945Sroberto
79258945Sroberto	return (ret);
80258945Sroberto}
81258945Sroberto
82258945Srobertoisc_result_t
83258945Srobertoisc_keyboard_close(isc_keyboard_t *keyboard, unsigned int sleeptime) {
84258945Sroberto	REQUIRE(keyboard != NULL);
85258945Sroberto
86258945Sroberto	if (sleeptime > 0 && keyboard->result != ISC_R_CANCELED)
87258945Sroberto		(void)sleep(sleeptime);
88258945Sroberto
89258945Sroberto	(void)tcsetattr(keyboard->fd, TCSAFLUSH, &keyboard->saved_mode);
90258945Sroberto	(void)close(keyboard->fd);
91258945Sroberto
92258945Sroberto	keyboard->fd = -1;
93258945Sroberto
94258945Sroberto	return (ISC_R_SUCCESS);
95258945Sroberto}
96258945Sroberto
97258945Srobertoisc_result_t
98258945Srobertoisc_keyboard_getchar(isc_keyboard_t *keyboard, unsigned char *cp) {
99258945Sroberto	ssize_t cc;
100258945Sroberto	unsigned char c;
101258945Sroberto	cc_t *controlchars;
102258945Sroberto
103258945Sroberto	REQUIRE(keyboard != NULL);
104258945Sroberto	REQUIRE(cp != NULL);
105258945Sroberto
106258945Sroberto	cc = read(keyboard->fd, &c, 1);
107258945Sroberto	if (cc < 0) {
108258945Sroberto		keyboard->result = ISC_R_IOERROR;
109258945Sroberto		return (keyboard->result);
110258945Sroberto	}
111258945Sroberto
112258945Sroberto	controlchars = keyboard->saved_mode.c_cc;
113258945Sroberto	if (c == controlchars[VINTR] || c == controlchars[VQUIT]) {
114258945Sroberto		keyboard->result = ISC_R_CANCELED;
115258945Sroberto		return (keyboard->result);
116258945Sroberto	}
117258945Sroberto
118258945Sroberto	*cp = c;
119258945Sroberto
120258945Sroberto	return (ISC_R_SUCCESS);
121258945Sroberto}
122258945Sroberto
123258945Srobertoisc_boolean_t
124258945Srobertoisc_keyboard_canceled(isc_keyboard_t *keyboard) {
125258945Sroberto	return (ISC_TF(keyboard->result == ISC_R_CANCELED));
126258945Sroberto}
127