diff --git a/bin/tests/.cvsignore b/bin/tests/.cvsignore index cf2e86e384..41d088d136 100644 --- a/bin/tests/.cvsignore +++ b/bin/tests/.cvsignore @@ -17,6 +17,7 @@ gxbn_test headerdep_test.sh hash_test inter_test +keyboard_test lex_test lfsr_test log_test diff --git a/bin/tests/Makefile.in b/bin/tests/Makefile.in index 27d3f8e37b..243615ec56 100644 --- a/bin/tests/Makefile.in +++ b/bin/tests/Makefile.in @@ -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} diff --git a/bin/tests/keyboard_test.c b/bin/tests/keyboard_test.c new file mode 100644 index 0000000000..8011788611 --- /dev/null +++ b/bin/tests/keyboard_test.c @@ -0,0 +1,69 @@ +/* + * 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 +#include + +#include + +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); +} + diff --git a/lib/isc/Makefile.in b/lib/isc/Makefile.in index 9eb45901b8..54eb673522 100644 --- a/lib/isc/Makefile.in +++ b/lib/isc/Makefile.in @@ -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@ diff --git a/lib/isc/unix/Makefile.in b/lib/isc/unix/Makefile.in index e22ad58f73..55b9f0defe 100644 --- a/lib/isc/unix/Makefile.in +++ b/lib/isc/unix/Makefile.in @@ -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} diff --git a/lib/isc/unix/include/isc/keyboard.h b/lib/isc/unix/include/isc/keyboard.h new file mode 100644 index 0000000000..0c620fdc47 --- /dev/null +++ b/lib/isc/unix/include/isc/keyboard.h @@ -0,0 +1,46 @@ +/* + * 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 + +#include +#include + +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 */ diff --git a/lib/isc/unix/keyboard.c b/lib/isc/unix/keyboard.c new file mode 100644 index 0000000000..35f50bbb16 --- /dev/null +++ b/lib/isc/unix/keyboard.c @@ -0,0 +1,99 @@ +/* + * 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 + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include +#include + +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(¤t_mode); + current_mode.c_cc[VMIN] = 1; + current_mode.c_cc[VTIME] = 0; + if (tcsetattr(fd, TCSAFLUSH, ¤t_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); +}