Commit 890fb609 authored by Michael Graff's avatar Michael Graff
Browse files

add keyboard API

parent 1fb5269e
......@@ -17,6 +17,7 @@ gxbn_test
headerdep_test.sh
hash_test
inter_test
keyboard_test
lex_test
lfsr_test
log_test
......
......@@ -56,6 +56,7 @@ XTARGETS = adb_test \
hash_test \
fsaccess_test \
inter_test \
keyboard_test \
lex_test \
lfsr_test \
log_test \
......@@ -97,6 +98,7 @@ SRCS = adb_test.c \
hash_test.c \
fsaccess_test.c \
inter_test.c \
keyboard_test.c \
lex_test.c \
lfsr_test.c \
log_test.c \
......@@ -273,6 +275,10 @@ inter_test: inter_test.@O@ ${ISCDEPLIBS}
${LIBTOOL} ${CC} ${CFLAGS} -o $@ inter_test.@O@ \
${ISCLIBS} ${LIBS}
keyboard_test: keyboard_test.@O@ ${ISCDEPLIBS}
${LIBTOOL} ${CC} ${CFLAGS} -o $@ keyboard_test.@O@ \
${ISCLIBS} ${LIBS}
lwresconf_test: lwresconf_test.@O@ ${ISCDEPLIBS} ${LWRESDEPLIBS}
${LIBTOOL} ${CC} ${CFLAGS} -o $@ lwresconf_test.@O@ \
${LWRESLIBS} ${ISCLIBS} ${LIBS}
......
/*
* Copyright (C) 2000 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
* SOFTWARE.
*/
#include <isc/keyboard.h>
#include <isc/util.h>
#include <stdio.h>
static void
CHECK(const char *msg, isc_result_t result) {
if (result != ISC_R_SUCCESS) {
printf("FAILURE: %s: %s\n", msg, isc_result_totext(result));
exit(1);
}
}
int
main(int argc, char **argv) {
isc_keyboard_t kbd;
unsigned char c;
isc_result_t res;
unsigned int count;
UNUSED(argc);
UNUSED(argv);
printf("Type Q to exit.\n");
res = isc_keyboard_open(&kbd);
CHECK("isc_keyboard_open()", res);
c = 'x';
count = 0;
while (res == ISC_R_SUCCESS && c != 'Q') {
res = isc_keyboard_getchar(&kbd, &c);
printf(".");
fflush(stdout);
count++;
if (count % 64 == 0)
printf("\r\n");
}
printf("\r\n");
if (res != ISC_R_SUCCESS) {
printf("FAILURE: keyboard close failed: %s\r\n",
isc_result_totext(res));
goto errout;
}
errout:
res = isc_keyboard_close(&kbd);
CHECK("isc_keyboard_close()", res);
return (0);
}
......@@ -32,8 +32,8 @@ CWARNINGS =
UNIXOBJS = @ISC_ISCIPV6_O@ \
unix/app.@O@ unix/dir.@O@ unix/entropy.@O@ \
unix/errno2result.@O@ unix/file.@O@ unix/fsaccess.@O@ \
unix/interfaceiter.@O@ unix/net.@O@ unix/socket.@O@ \
unix/time.@O@ unix/stdio.@O@ unix/stdtime.@O@
unix/interfaceiter.@O@ unix/keyboard.@O@ unix/net.@O@ \
unix/socket.@O@ unix/time.@O@ unix/stdio.@O@ unix/stdtime.@O@
NLSOBJS = nls/msgcat.@O@
......
......@@ -29,14 +29,14 @@ CWARNINGS =
# Alphabetically
OBJS = @ISC_IPV6_O@ \
app.@O@ dir.@O@ entropy.@O@ errno2result.@O@ file.@O@ \
fsaccess.@O@ interfaceiter.@O@ net.@O@ socket.@O@ stdio.@O@ \
stdtime.@O@ time.@O@
fsaccess.@O@ interfaceiter.@O@ keyboard.@O@ net.@O@ \
socket.@O@ stdio.@O@ stdtime.@O@ time.@O@
# Alphabetically
SRCS = @ISC_IPV6_C@ \
app.c dir.c entropy.c errno2result.c file.c \
fsaccess.c interfaceiter.c net.c socket.c stdio.c \
stdtime.c time.c
fsaccess.c interfaceiter.c keyboard.c net.c \
socket.c stdio.c stdtime.c time.c
SUBDIRS = include
TARGETS = ${OBJS}
......
/*
* Copyright (C) 2000 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
* SOFTWARE.
*/
/* $Id: keyboard.h,v 1.1 2000/06/21 01:45:21 explorer Exp $ */
#ifndef ISC_KEYBOARD_H
#define ISC_KEYBOARD_H 1
#include <termios.h>
#include <isc/lang.h>
#include <isc/result.h>
ISC_LANG_BEGINDECLS
typedef struct {
int fd;
struct termios saved_mode;
} isc_keyboard_t;
isc_result_t
isc_keyboard_open(isc_keyboard_t *keyboard);
isc_result_t
isc_keyboard_close(isc_keyboard_t *keyboard);
isc_result_t
isc_keyboard_getchar(isc_keyboard_t *keyboard, unsigned char *cp);
ISC_LANG_ENDDECLS
#endif /* ISC_KEYBOARD_H */
/*
* Copyright (C) 2000 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
* SOFTWARE.
*/
#include <config.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/uio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <isc/keyboard.h>
#include <isc/util.h>
isc_result_t
isc_keyboard_open(isc_keyboard_t *keyboard) {
int fd;
isc_result_t ret;
struct termios current_mode;
REQUIRE(keyboard != NULL);
fd = open("/dev/tty", O_RDONLY, 0);
if (fd < 0)
return (ISC_R_IOERROR);
keyboard->fd = fd;
if (tcgetattr(fd, &keyboard->saved_mode) < 0) {
ret = ISC_R_IOERROR;
goto errout;
}
current_mode = keyboard->saved_mode;
cfmakeraw(&current_mode);
current_mode.c_cc[VMIN] = 1;
current_mode.c_cc[VTIME] = 0;
if (tcsetattr(fd, TCSAFLUSH, &current_mode) < 0) {
ret = ISC_R_IOERROR;
goto errout;
}
return (ISC_R_SUCCESS);
errout:
close (fd);
return (ret);
}
isc_result_t
isc_keyboard_close(isc_keyboard_t *keyboard) {
REQUIRE(keyboard != NULL);
(void)tcsetattr(keyboard->fd, TCSAFLUSH, &keyboard->saved_mode);
close(keyboard->fd);
keyboard->fd = -1;
return (ISC_R_SUCCESS);
}
isc_result_t
isc_keyboard_getchar(isc_keyboard_t *keyboard, unsigned char *cp) {
ssize_t cc;
unsigned char c;
REQUIRE(keyboard != NULL);
REQUIRE(cp != NULL);
cc = read(keyboard->fd, &c, 1);
if (cc < 0)
return (ISC_R_IOERROR);
*cp = c;
return (ISC_R_SUCCESS);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment