17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate * Copyright (c) 1995, by Sun Microsystems, Inc.
247c478bd9Sstevel@tonic-gate * All rights reserved.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate * wgetch.c
297c478bd9Sstevel@tonic-gate *
307c478bd9Sstevel@tonic-gate * XCurses Library
317c478bd9Sstevel@tonic-gate *
327c478bd9Sstevel@tonic-gate * Copyright 1990, 1995 by Mortice Kern Systems Inc. All rights reserved.
337c478bd9Sstevel@tonic-gate *
347c478bd9Sstevel@tonic-gate */
357c478bd9Sstevel@tonic-gate
367c478bd9Sstevel@tonic-gate #ifdef M_RCSID
377c478bd9Sstevel@tonic-gate #ifndef lint
387c478bd9Sstevel@tonic-gate static char rcsID[] = "$Header: /rd/src/libc/xcurses/rcs/wgetch.c 1.5 1995/06/19 16:12:13 ant Exp $";
397c478bd9Sstevel@tonic-gate #endif
407c478bd9Sstevel@tonic-gate #endif
417c478bd9Sstevel@tonic-gate
427c478bd9Sstevel@tonic-gate #include <private.h>
437c478bd9Sstevel@tonic-gate
447c478bd9Sstevel@tonic-gate /*
457c478bd9Sstevel@tonic-gate * Push single-byte character back onto the input queue.
467c478bd9Sstevel@tonic-gate *
47*1da57d55SToomas Soome * MKS EXTENSION permits the return value of wgetch(), which
487c478bd9Sstevel@tonic-gate * can be a KEY_ value, to be pushed back.
497c478bd9Sstevel@tonic-gate */
507c478bd9Sstevel@tonic-gate int
ungetch(ch)517c478bd9Sstevel@tonic-gate ungetch(ch)
527c478bd9Sstevel@tonic-gate int ch;
537c478bd9Sstevel@tonic-gate {
547c478bd9Sstevel@tonic-gate int code;
557c478bd9Sstevel@tonic-gate
567c478bd9Sstevel@tonic-gate #ifdef M_CURSES_TRACE
577c478bd9Sstevel@tonic-gate __m_trace("ungetch(%d)", ch);
587c478bd9Sstevel@tonic-gate #endif
597c478bd9Sstevel@tonic-gate code = __xc_ungetc(ch, (WINDOW *) 0) == EOF ? ERR : OK;
607c478bd9Sstevel@tonic-gate
617c478bd9Sstevel@tonic-gate return __m_return_code("ungetch", code);
627c478bd9Sstevel@tonic-gate }
637c478bd9Sstevel@tonic-gate
647c478bd9Sstevel@tonic-gate /*
657c478bd9Sstevel@tonic-gate * Push a single-byte character or KEY_ value but onto the
667c478bd9Sstevel@tonic-gate * input queue. Ignore the window parameter.
677c478bd9Sstevel@tonic-gate */
687c478bd9Sstevel@tonic-gate int
__xc_ungetc(int ch,void * w)697c478bd9Sstevel@tonic-gate __xc_ungetc(int ch, void *w)
707c478bd9Sstevel@tonic-gate {
717c478bd9Sstevel@tonic-gate if (ISFULL())
727c478bd9Sstevel@tonic-gate return EOF;
737c478bd9Sstevel@tonic-gate
747c478bd9Sstevel@tonic-gate PUSH(ch);
757c478bd9Sstevel@tonic-gate
767c478bd9Sstevel@tonic-gate return 0;
777c478bd9Sstevel@tonic-gate }
787c478bd9Sstevel@tonic-gate
797c478bd9Sstevel@tonic-gate /*
807c478bd9Sstevel@tonic-gate * Return true if the SCREEN's stream has an I/O error.
817c478bd9Sstevel@tonic-gate * Ignore the window parameter.
827c478bd9Sstevel@tonic-gate */
837c478bd9Sstevel@tonic-gate int
__xc_ferror(void * w)847c478bd9Sstevel@tonic-gate __xc_ferror(void *w)
857c478bd9Sstevel@tonic-gate {
867c478bd9Sstevel@tonic-gate return ferror(__m_screen->_if);
877c478bd9Sstevel@tonic-gate }
887c478bd9Sstevel@tonic-gate
897c478bd9Sstevel@tonic-gate /*
907c478bd9Sstevel@tonic-gate * Return true if the SCREEN's stream has seen EOF.
917c478bd9Sstevel@tonic-gate * Ignore the window parameter.
927c478bd9Sstevel@tonic-gate */
937c478bd9Sstevel@tonic-gate int
__xc_feof(void * w)947c478bd9Sstevel@tonic-gate __xc_feof(void *w)
957c478bd9Sstevel@tonic-gate {
967c478bd9Sstevel@tonic-gate return feof(__m_screen->_if);
977c478bd9Sstevel@tonic-gate }
987c478bd9Sstevel@tonic-gate
997c478bd9Sstevel@tonic-gate /*
1007c478bd9Sstevel@tonic-gate * Clear the error and eof flags of the SCREEN's stream.
1017c478bd9Sstevel@tonic-gate * Ignore the window parameter.
1027c478bd9Sstevel@tonic-gate */
1037c478bd9Sstevel@tonic-gate void
__xc_clearerr(void * w)1047c478bd9Sstevel@tonic-gate __xc_clearerr(void *w)
1057c478bd9Sstevel@tonic-gate {
1067c478bd9Sstevel@tonic-gate clearerr(__m_screen->_if);
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate
109*1da57d55SToomas Soome int
wgetch(w)1107c478bd9Sstevel@tonic-gate wgetch(w)
1117c478bd9Sstevel@tonic-gate WINDOW *w;
1127c478bd9Sstevel@tonic-gate {
1137c478bd9Sstevel@tonic-gate t_decode *node;
1147c478bd9Sstevel@tonic-gate int ch, i, j, timeout;
1157c478bd9Sstevel@tonic-gate
1167c478bd9Sstevel@tonic-gate #ifdef M_CURSES_TRACE
1177c478bd9Sstevel@tonic-gate __m_trace("wgetch(%p) at (%d, %d).", w, w->_cury, w->_curx);
1187c478bd9Sstevel@tonic-gate #endif
1197c478bd9Sstevel@tonic-gate
1207c478bd9Sstevel@tonic-gate (void) wrefresh(w);
1217c478bd9Sstevel@tonic-gate
1227c478bd9Sstevel@tonic-gate if (!ISEMPTY())
1237c478bd9Sstevel@tonic-gate return __m_return_int("wgetch", POP());
1247c478bd9Sstevel@tonic-gate
125*1da57d55SToomas Soome /* Only change the terminal's input method if the window
1267c478bd9Sstevel@tonic-gate * requires different settings from what is currently set.
1277c478bd9Sstevel@tonic-gate * We do this because tcsetattr() on some systems can be
128*1da57d55SToomas Soome * _really_ slow to do for each character.
1297c478bd9Sstevel@tonic-gate *
1307c478bd9Sstevel@tonic-gate * NOTE that halfdelay() overrides nodelay() and wtimeout().
131*1da57d55SToomas Soome */
132*1da57d55SToomas Soome if (!(cur_term->_flags & __TERM_HALF_DELAY)
1337c478bd9Sstevel@tonic-gate && (cur_term->_prog.c_cc[VMIN] != w->_vmin
1347c478bd9Sstevel@tonic-gate || cur_term->_prog.c_cc[VTIME] != w->_vtime)) {
135*1da57d55SToomas Soome cur_term->_prog.c_cc[VMIN] = w->_vmin;
136*1da57d55SToomas Soome cur_term->_prog.c_cc[VTIME] = w->_vtime;
1377c478bd9Sstevel@tonic-gate
1387c478bd9Sstevel@tonic-gate if (__m_tty_set(&cur_term->_prog) == ERR)
1397c478bd9Sstevel@tonic-gate return __m_return_int("wgetch", EOF);
1407c478bd9Sstevel@tonic-gate }
1417c478bd9Sstevel@tonic-gate
1427c478bd9Sstevel@tonic-gate if (req_for_input != (char *) 0)
1437c478bd9Sstevel@tonic-gate (void) tputs(req_for_input, 1, __m_outc);
1447c478bd9Sstevel@tonic-gate
1457c478bd9Sstevel@tonic-gate clearerr(__m_screen->_if);
1467c478bd9Sstevel@tonic-gate ch = fgetc(__m_screen->_if);
1477c478bd9Sstevel@tonic-gate
148*1da57d55SToomas Soome /* Only check for function keys if keypad is true and we
1497c478bd9Sstevel@tonic-gate * did not read a KEY_ value (which are < 0), nor EOF.
150*1da57d55SToomas Soome * It is conceivable that a KEY_ was pushed back with
1517c478bd9Sstevel@tonic-gate * ungetch().
1527c478bd9Sstevel@tonic-gate */
1537c478bd9Sstevel@tonic-gate if ((w->_flags & W_USE_KEYPAD) && 0 <= ch && ch != EOF) {
154*1da57d55SToomas Soome /* Treat the termios ERASE key the same as key_backspace.
1557c478bd9Sstevel@tonic-gate *
1567c478bd9Sstevel@tonic-gate * We used to change the key_backspace entry to be a string
1577c478bd9Sstevel@tonic-gate * containing the ERASE key in setupterm(), but this would
1587c478bd9Sstevel@tonic-gate * then disable the real terminfo entry for the backspace key.
1597c478bd9Sstevel@tonic-gate * Apparently VT300 terminals change the key code/sequence
160*1da57d55SToomas Soome * of the backspace key in application keypad mode.
1617c478bd9Sstevel@tonic-gate * See SR 6014.
1627c478bd9Sstevel@tonic-gate *
163*1da57d55SToomas Soome * Refer to _shell instead of _prog, since _shell will
164*1da57d55SToomas Soome * correctly reflect the user's prefered settings, whereas
165*1da57d55SToomas Soome * _prog may not have been initialised if both input and
1667c478bd9Sstevel@tonic-gate * output have been redirected.
1677c478bd9Sstevel@tonic-gate */
1687c478bd9Sstevel@tonic-gate #ifdef _POSIX_VDISABLE
1697c478bd9Sstevel@tonic-gate if (cur_term->_shell.c_cc[VERASE] != _POSIX_VDISABLE)
1707c478bd9Sstevel@tonic-gate #endif
1717c478bd9Sstevel@tonic-gate if (ch == cur_term->_shell.c_cc[VERASE])
1727c478bd9Sstevel@tonic-gate return __m_return_int("wgetch", KEY_BACKSPACE);
1737c478bd9Sstevel@tonic-gate
1747c478bd9Sstevel@tonic-gate /* Begin check for function key. */
1757c478bd9Sstevel@tonic-gate node = (t_decode *) __m_screen->_decode;
1767c478bd9Sstevel@tonic-gate
1777c478bd9Sstevel@tonic-gate /* Use input stack as a queue. */
1787c478bd9Sstevel@tonic-gate timeout = w->_flags & W_USE_TIMEOUT;
1797c478bd9Sstevel@tonic-gate for (RESET(); !ISFULL(); ) {
1807c478bd9Sstevel@tonic-gate PUSH(ch);
1817c478bd9Sstevel@tonic-gate
1827c478bd9Sstevel@tonic-gate while (node->ch != ch) {
1837c478bd9Sstevel@tonic-gate node = node->sibling;
1847c478bd9Sstevel@tonic-gate if (node == (t_decode *) 0)
1857c478bd9Sstevel@tonic-gate goto invalid;
1867c478bd9Sstevel@tonic-gate }
1877c478bd9Sstevel@tonic-gate
1887c478bd9Sstevel@tonic-gate /* Found funuction key? */
1897c478bd9Sstevel@tonic-gate if (node->key != 0) {
1907c478bd9Sstevel@tonic-gate RESET();
1917c478bd9Sstevel@tonic-gate return __m_return_int("wgetch", node->key);
1927c478bd9Sstevel@tonic-gate }
1937c478bd9Sstevel@tonic-gate
194*1da57d55SToomas Soome /* Setup interbyte timer (once only). fgetc() will
195*1da57d55SToomas Soome * return EOF if no input received, which may not be
1967c478bd9Sstevel@tonic-gate * a true EOF.
1977c478bd9Sstevel@tonic-gate */
1987c478bd9Sstevel@tonic-gate if (timeout) {
199*1da57d55SToomas Soome cur_term->_prog.c_cc[VMIN] = 0;
200*1da57d55SToomas Soome cur_term->_prog.c_cc[VTIME] =
2017c478bd9Sstevel@tonic-gate M_CURSES_INTERBYTE_TIME;
2027c478bd9Sstevel@tonic-gate (void) __m_tty_set(&cur_term->_prog);
2037c478bd9Sstevel@tonic-gate }
2047c478bd9Sstevel@tonic-gate timeout = 0;
2057c478bd9Sstevel@tonic-gate
2067c478bd9Sstevel@tonic-gate if ((ch = fgetc(__m_screen->_if)) == EOF)
2077c478bd9Sstevel@tonic-gate /* Timeout or real eof. */
2087c478bd9Sstevel@tonic-gate break;
2097c478bd9Sstevel@tonic-gate
2107c478bd9Sstevel@tonic-gate /* Incomplete sequence, continue. */
2117c478bd9Sstevel@tonic-gate node = node->child;
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate invalid:
2147c478bd9Sstevel@tonic-gate /* Reverse contents of the input queue to form a stack. */
2157c478bd9Sstevel@tonic-gate for (i = 0, j = __m_screen->_unget._count; i < --j; ++i) {
2167c478bd9Sstevel@tonic-gate ch = __m_screen->_unget._stack[i];
217*1da57d55SToomas Soome __m_screen->_unget._stack[i] =
2187c478bd9Sstevel@tonic-gate __m_screen->_unget._stack[j];
2197c478bd9Sstevel@tonic-gate __m_screen->_unget._stack[j] = ch;
2207c478bd9Sstevel@tonic-gate }
221*1da57d55SToomas Soome
2227c478bd9Sstevel@tonic-gate /* Return first byte received or EOF. */
2237c478bd9Sstevel@tonic-gate ch = POP();
2247c478bd9Sstevel@tonic-gate }
2257c478bd9Sstevel@tonic-gate
2267c478bd9Sstevel@tonic-gate if ((__m_screen->_flags & S_ECHO) && 0 <= ch && ch != EOF) {
2277c478bd9Sstevel@tonic-gate (void) waddch(w, ch);
2287c478bd9Sstevel@tonic-gate (void) wrefresh(w);
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate
2317c478bd9Sstevel@tonic-gate return __m_return_int("wgetch", ch);
2327c478bd9Sstevel@tonic-gate }
2337c478bd9Sstevel@tonic-gate
234