2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License, Version 1.0 only
2N/A * (the "License"). You may not use this file except in compliance
2N/A * with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright (c) 1995, by Sun Microsystems, Inc.
2N/A * All rights reserved.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A/*
2N/A * wgetch.c
2N/A *
2N/A * XCurses Library
2N/A *
2N/A * Copyright 1990, 1995 by Mortice Kern Systems Inc. All rights reserved.
2N/A *
2N/A */
2N/A
2N/A#ifdef M_RCSID
2N/A#ifndef lint
2N/Astatic char rcsID[] = "$Header: /rd/src/libc/xcurses/rcs/wgetch.c 1.5 1995/06/19 16:12:13 ant Exp $";
2N/A#endif
2N/A#endif
2N/A
2N/A#include <private.h>
2N/A
2N/A/*
2N/A * Push single-byte character back onto the input queue.
2N/A *
2N/A * MKS EXTENSION permits the return value of wgetch(), which
2N/A * can be a KEY_ value, to be pushed back.
2N/A */
2N/Aint
2N/Aungetch(ch)
2N/Aint ch;
2N/A{
2N/A int code;
2N/A
2N/A#ifdef M_CURSES_TRACE
2N/A __m_trace("ungetch(%d)", ch);
2N/A#endif
2N/A code = __xc_ungetc(ch, (WINDOW *) 0) == EOF ? ERR : OK;
2N/A
2N/A return __m_return_code("ungetch", code);
2N/A}
2N/A
2N/A/*
2N/A * Push a single-byte character or KEY_ value but onto the
2N/A * input queue. Ignore the window parameter.
2N/A */
2N/Aint
2N/A__xc_ungetc(int ch, void *w)
2N/A{
2N/A if (ISFULL())
2N/A return EOF;
2N/A
2N/A PUSH(ch);
2N/A
2N/A return 0;
2N/A}
2N/A
2N/A/*
2N/A * Return true if the SCREEN's stream has an I/O error.
2N/A * Ignore the window parameter.
2N/A */
2N/Aint
2N/A__xc_ferror(void *w)
2N/A{
2N/A return ferror(__m_screen->_if);
2N/A}
2N/A
2N/A/*
2N/A * Return true if the SCREEN's stream has seen EOF.
2N/A * Ignore the window parameter.
2N/A */
2N/Aint
2N/A__xc_feof(void *w)
2N/A{
2N/A return feof(__m_screen->_if);
2N/A}
2N/A
2N/A/*
2N/A * Clear the error and eof flags of the SCREEN's stream.
2N/A * Ignore the window parameter.
2N/A */
2N/Avoid
2N/A__xc_clearerr(void *w)
2N/A{
2N/A clearerr(__m_screen->_if);
2N/A}
2N/A
2N/Aint
2N/Awgetch(w)
2N/AWINDOW *w;
2N/A{
2N/A t_decode *node;
2N/A int ch, i, j, timeout;
2N/A
2N/A#ifdef M_CURSES_TRACE
2N/A __m_trace("wgetch(%p) at (%d, %d).", w, w->_cury, w->_curx);
2N/A#endif
2N/A
2N/A (void) wrefresh(w);
2N/A
2N/A if (!ISEMPTY())
2N/A return __m_return_int("wgetch", POP());
2N/A
2N/A /* Only change the terminal's input method if the window
2N/A * requires different settings from what is currently set.
2N/A * We do this because tcsetattr() on some systems can be
2N/A * _really_ slow to do for each character.
2N/A *
2N/A * NOTE that halfdelay() overrides nodelay() and wtimeout().
2N/A */
2N/A if (!(cur_term->_flags & __TERM_HALF_DELAY)
2N/A && (cur_term->_prog.c_cc[VMIN] != w->_vmin
2N/A || cur_term->_prog.c_cc[VTIME] != w->_vtime)) {
2N/A cur_term->_prog.c_cc[VMIN] = w->_vmin;
2N/A cur_term->_prog.c_cc[VTIME] = w->_vtime;
2N/A
2N/A if (__m_tty_set(&cur_term->_prog) == ERR)
2N/A return __m_return_int("wgetch", EOF);
2N/A }
2N/A
2N/A if (req_for_input != (char *) 0)
2N/A (void) tputs(req_for_input, 1, __m_outc);
2N/A
2N/A clearerr(__m_screen->_if);
2N/A ch = fgetc(__m_screen->_if);
2N/A
2N/A /* Only check for function keys if keypad is true and we
2N/A * did not read a KEY_ value (which are < 0), nor EOF.
2N/A * It is conceivable that a KEY_ was pushed back with
2N/A * ungetch().
2N/A */
2N/A if ((w->_flags & W_USE_KEYPAD) && 0 <= ch && ch != EOF) {
2N/A /* Treat the termios ERASE key the same as key_backspace.
2N/A *
2N/A * We used to change the key_backspace entry to be a string
2N/A * containing the ERASE key in setupterm(), but this would
2N/A * then disable the real terminfo entry for the backspace key.
2N/A * Apparently VT300 terminals change the key code/sequence
2N/A * of the backspace key in application keypad mode.
2N/A * See SR 6014.
2N/A *
2N/A * Refer to _shell instead of _prog, since _shell will
2N/A * correctly reflect the user's prefered settings, whereas
2N/A * _prog may not have been initialised if both input and
2N/A * output have been redirected.
2N/A */
2N/A#ifdef _POSIX_VDISABLE
2N/A if (cur_term->_shell.c_cc[VERASE] != _POSIX_VDISABLE)
2N/A#endif
2N/A if (ch == cur_term->_shell.c_cc[VERASE])
2N/A return __m_return_int("wgetch", KEY_BACKSPACE);
2N/A
2N/A /* Begin check for function key. */
2N/A node = (t_decode *) __m_screen->_decode;
2N/A
2N/A /* Use input stack as a queue. */
2N/A timeout = w->_flags & W_USE_TIMEOUT;
2N/A for (RESET(); !ISFULL(); ) {
2N/A PUSH(ch);
2N/A
2N/A while (node->ch != ch) {
2N/A node = node->sibling;
2N/A if (node == (t_decode *) 0)
2N/A goto invalid;
2N/A }
2N/A
2N/A /* Found funuction key? */
2N/A if (node->key != 0) {
2N/A RESET();
2N/A return __m_return_int("wgetch", node->key);
2N/A }
2N/A
2N/A /* Setup interbyte timer (once only). fgetc() will
2N/A * return EOF if no input received, which may not be
2N/A * a true EOF.
2N/A */
2N/A if (timeout) {
2N/A cur_term->_prog.c_cc[VMIN] = 0;
2N/A cur_term->_prog.c_cc[VTIME] =
2N/A M_CURSES_INTERBYTE_TIME;
2N/A (void) __m_tty_set(&cur_term->_prog);
2N/A }
2N/A timeout = 0;
2N/A
2N/A if ((ch = fgetc(__m_screen->_if)) == EOF)
2N/A /* Timeout or real eof. */
2N/A break;
2N/A
2N/A /* Incomplete sequence, continue. */
2N/A node = node->child;
2N/A }
2N/Ainvalid:
2N/A /* Reverse contents of the input queue to form a stack. */
2N/A for (i = 0, j = __m_screen->_unget._count; i < --j; ++i) {
2N/A ch = __m_screen->_unget._stack[i];
2N/A __m_screen->_unget._stack[i] =
2N/A __m_screen->_unget._stack[j];
2N/A __m_screen->_unget._stack[j] = ch;
2N/A }
2N/A
2N/A /* Return first byte received or EOF. */
2N/A ch = POP();
2N/A }
2N/A
2N/A if ((__m_screen->_flags & S_ECHO) && 0 <= ch && ch != EOF) {
2N/A (void) waddch(w, ch);
2N/A (void) wrefresh(w);
2N/A }
2N/A
2N/A return __m_return_int("wgetch", ch);
2N/A}
2N/A