xref: /titanic_52/usr/src/lib/libxcurses/src/libc/xcurses/wgetch.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1995, by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  * wgetch.c
31*7c478bd9Sstevel@tonic-gate  *
32*7c478bd9Sstevel@tonic-gate  * XCurses Library
33*7c478bd9Sstevel@tonic-gate  *
34*7c478bd9Sstevel@tonic-gate  * Copyright 1990, 1995 by Mortice Kern Systems Inc.  All rights reserved.
35*7c478bd9Sstevel@tonic-gate  *
36*7c478bd9Sstevel@tonic-gate  */
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate #ifdef M_RCSID
39*7c478bd9Sstevel@tonic-gate #ifndef lint
40*7c478bd9Sstevel@tonic-gate static char rcsID[] = "$Header: /rd/src/libc/xcurses/rcs/wgetch.c 1.5 1995/06/19 16:12:13 ant Exp $";
41*7c478bd9Sstevel@tonic-gate #endif
42*7c478bd9Sstevel@tonic-gate #endif
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate #include <private.h>
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate /*
47*7c478bd9Sstevel@tonic-gate  * Push single-byte character back onto the input queue.
48*7c478bd9Sstevel@tonic-gate  *
49*7c478bd9Sstevel@tonic-gate  * MKS EXTENSION permits the return value of wgetch(), which
50*7c478bd9Sstevel@tonic-gate  * can be a KEY_ value, to be pushed back.
51*7c478bd9Sstevel@tonic-gate  */
52*7c478bd9Sstevel@tonic-gate int
53*7c478bd9Sstevel@tonic-gate ungetch(ch)
54*7c478bd9Sstevel@tonic-gate int ch;
55*7c478bd9Sstevel@tonic-gate {
56*7c478bd9Sstevel@tonic-gate 	int code;
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate #ifdef M_CURSES_TRACE
59*7c478bd9Sstevel@tonic-gate 	__m_trace("ungetch(%d)", ch);
60*7c478bd9Sstevel@tonic-gate #endif
61*7c478bd9Sstevel@tonic-gate 	code = __xc_ungetc(ch, (WINDOW *) 0) == EOF ? ERR : OK;
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate 	return __m_return_code("ungetch", code);
64*7c478bd9Sstevel@tonic-gate }
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate /*
67*7c478bd9Sstevel@tonic-gate  * Push a single-byte character or KEY_ value but onto the
68*7c478bd9Sstevel@tonic-gate  * input queue.  Ignore the window parameter.
69*7c478bd9Sstevel@tonic-gate  */
70*7c478bd9Sstevel@tonic-gate int
71*7c478bd9Sstevel@tonic-gate __xc_ungetc(int ch, void *w)
72*7c478bd9Sstevel@tonic-gate {
73*7c478bd9Sstevel@tonic-gate 	if (ISFULL())
74*7c478bd9Sstevel@tonic-gate 		return EOF;
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate 	PUSH(ch);
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate 	return 0;
79*7c478bd9Sstevel@tonic-gate }
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate /*
82*7c478bd9Sstevel@tonic-gate  * Return true if the SCREEN's stream has an I/O error.
83*7c478bd9Sstevel@tonic-gate  * Ignore the window parameter.
84*7c478bd9Sstevel@tonic-gate  */
85*7c478bd9Sstevel@tonic-gate int
86*7c478bd9Sstevel@tonic-gate __xc_ferror(void *w)
87*7c478bd9Sstevel@tonic-gate {
88*7c478bd9Sstevel@tonic-gate 	return ferror(__m_screen->_if);
89*7c478bd9Sstevel@tonic-gate }
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate /*
92*7c478bd9Sstevel@tonic-gate  * Return true if the SCREEN's stream has seen EOF.
93*7c478bd9Sstevel@tonic-gate  * Ignore the window parameter.
94*7c478bd9Sstevel@tonic-gate  */
95*7c478bd9Sstevel@tonic-gate int
96*7c478bd9Sstevel@tonic-gate __xc_feof(void *w)
97*7c478bd9Sstevel@tonic-gate {
98*7c478bd9Sstevel@tonic-gate 	return feof(__m_screen->_if);
99*7c478bd9Sstevel@tonic-gate }
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate /*
102*7c478bd9Sstevel@tonic-gate  * Clear the error and eof flags of the SCREEN's stream.
103*7c478bd9Sstevel@tonic-gate  * Ignore the window parameter.
104*7c478bd9Sstevel@tonic-gate  */
105*7c478bd9Sstevel@tonic-gate void
106*7c478bd9Sstevel@tonic-gate __xc_clearerr(void *w)
107*7c478bd9Sstevel@tonic-gate {
108*7c478bd9Sstevel@tonic-gate 	clearerr(__m_screen->_if);
109*7c478bd9Sstevel@tonic-gate }
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate int
112*7c478bd9Sstevel@tonic-gate wgetch(w)
113*7c478bd9Sstevel@tonic-gate WINDOW *w;
114*7c478bd9Sstevel@tonic-gate {
115*7c478bd9Sstevel@tonic-gate 	t_decode *node;
116*7c478bd9Sstevel@tonic-gate 	int ch, i, j, timeout;
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate #ifdef M_CURSES_TRACE
119*7c478bd9Sstevel@tonic-gate 	__m_trace("wgetch(%p) at (%d, %d).", w, w->_cury, w->_curx);
120*7c478bd9Sstevel@tonic-gate #endif
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate 	(void) wrefresh(w);
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate 	if (!ISEMPTY())
125*7c478bd9Sstevel@tonic-gate 		return __m_return_int("wgetch", POP());
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate 	/* Only change the terminal's input method if the window
128*7c478bd9Sstevel@tonic-gate 	 * requires different settings from what is currently set.
129*7c478bd9Sstevel@tonic-gate 	 * We do this because tcsetattr() on some systems can be
130*7c478bd9Sstevel@tonic-gate 	 * _really_ slow to do for each character.
131*7c478bd9Sstevel@tonic-gate 	 *
132*7c478bd9Sstevel@tonic-gate 	 * NOTE that halfdelay() overrides nodelay() and wtimeout().
133*7c478bd9Sstevel@tonic-gate 	 */
134*7c478bd9Sstevel@tonic-gate 	if (!(cur_term->_flags & __TERM_HALF_DELAY)
135*7c478bd9Sstevel@tonic-gate 	&& (cur_term->_prog.c_cc[VMIN] != w->_vmin
136*7c478bd9Sstevel@tonic-gate 	|| cur_term->_prog.c_cc[VTIME] != w->_vtime)) {
137*7c478bd9Sstevel@tonic-gate 		cur_term->_prog.c_cc[VMIN] = w->_vmin;
138*7c478bd9Sstevel@tonic-gate 		cur_term->_prog.c_cc[VTIME] = w->_vtime;
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate 		if (__m_tty_set(&cur_term->_prog) == ERR)
141*7c478bd9Sstevel@tonic-gate 			return __m_return_int("wgetch", EOF);
142*7c478bd9Sstevel@tonic-gate 	}
143*7c478bd9Sstevel@tonic-gate 
144*7c478bd9Sstevel@tonic-gate 	if (req_for_input != (char *) 0)
145*7c478bd9Sstevel@tonic-gate 		(void) tputs(req_for_input, 1, __m_outc);
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate 	clearerr(__m_screen->_if);
148*7c478bd9Sstevel@tonic-gate 	ch = fgetc(__m_screen->_if);
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate 	/* Only check for function keys if keypad is true and we
151*7c478bd9Sstevel@tonic-gate 	 * did not read a KEY_ value (which are < 0), nor EOF.
152*7c478bd9Sstevel@tonic-gate 	 * It is conceivable that a KEY_ was pushed back with
153*7c478bd9Sstevel@tonic-gate 	 * ungetch().
154*7c478bd9Sstevel@tonic-gate 	 */
155*7c478bd9Sstevel@tonic-gate 	if ((w->_flags & W_USE_KEYPAD) && 0 <= ch && ch != EOF) {
156*7c478bd9Sstevel@tonic-gate 		/* Treat the termios ERASE key the same as key_backspace.
157*7c478bd9Sstevel@tonic-gate 		 *
158*7c478bd9Sstevel@tonic-gate 		 * We used to change the key_backspace entry to be a string
159*7c478bd9Sstevel@tonic-gate 		 * containing the ERASE key in setupterm(), but this would
160*7c478bd9Sstevel@tonic-gate 		 * then disable the real terminfo entry for the backspace key.
161*7c478bd9Sstevel@tonic-gate 		 * Apparently VT300 terminals change the key code/sequence
162*7c478bd9Sstevel@tonic-gate 		 * of the backspace key in application keypad mode.
163*7c478bd9Sstevel@tonic-gate 		 * See SR 6014.
164*7c478bd9Sstevel@tonic-gate 		 *
165*7c478bd9Sstevel@tonic-gate 		 * Refer to _shell instead of _prog, since _shell will
166*7c478bd9Sstevel@tonic-gate 		 * correctly reflect the user's prefered settings, whereas
167*7c478bd9Sstevel@tonic-gate 		 * _prog may not have been initialised if both input and
168*7c478bd9Sstevel@tonic-gate 		 * output have been redirected.
169*7c478bd9Sstevel@tonic-gate 		 */
170*7c478bd9Sstevel@tonic-gate #ifdef _POSIX_VDISABLE
171*7c478bd9Sstevel@tonic-gate 		if (cur_term->_shell.c_cc[VERASE] != _POSIX_VDISABLE)
172*7c478bd9Sstevel@tonic-gate #endif
173*7c478bd9Sstevel@tonic-gate 			if (ch == cur_term->_shell.c_cc[VERASE])
174*7c478bd9Sstevel@tonic-gate 				return __m_return_int("wgetch", KEY_BACKSPACE);
175*7c478bd9Sstevel@tonic-gate 
176*7c478bd9Sstevel@tonic-gate 		/* Begin check for function key. */
177*7c478bd9Sstevel@tonic-gate 		node = (t_decode *) __m_screen->_decode;
178*7c478bd9Sstevel@tonic-gate 
179*7c478bd9Sstevel@tonic-gate 		/* Use input stack as a queue. */
180*7c478bd9Sstevel@tonic-gate 		timeout = w->_flags & W_USE_TIMEOUT;
181*7c478bd9Sstevel@tonic-gate 		for (RESET(); !ISFULL(); ) {
182*7c478bd9Sstevel@tonic-gate 			PUSH(ch);
183*7c478bd9Sstevel@tonic-gate 
184*7c478bd9Sstevel@tonic-gate 			while (node->ch != ch) {
185*7c478bd9Sstevel@tonic-gate 				node = node->sibling;
186*7c478bd9Sstevel@tonic-gate 				if (node == (t_decode *) 0)
187*7c478bd9Sstevel@tonic-gate 					goto invalid;
188*7c478bd9Sstevel@tonic-gate 			}
189*7c478bd9Sstevel@tonic-gate 
190*7c478bd9Sstevel@tonic-gate 			/* Found funuction key? */
191*7c478bd9Sstevel@tonic-gate 			if (node->key != 0) {
192*7c478bd9Sstevel@tonic-gate 				RESET();
193*7c478bd9Sstevel@tonic-gate 				return __m_return_int("wgetch", node->key);
194*7c478bd9Sstevel@tonic-gate 			}
195*7c478bd9Sstevel@tonic-gate 
196*7c478bd9Sstevel@tonic-gate 			/* Setup interbyte timer (once only).  fgetc() will
197*7c478bd9Sstevel@tonic-gate 			 * return EOF if no input received, which may not be
198*7c478bd9Sstevel@tonic-gate 			 * a true EOF.
199*7c478bd9Sstevel@tonic-gate 			 */
200*7c478bd9Sstevel@tonic-gate 			if (timeout) {
201*7c478bd9Sstevel@tonic-gate 				cur_term->_prog.c_cc[VMIN] = 0;
202*7c478bd9Sstevel@tonic-gate 				cur_term->_prog.c_cc[VTIME] =
203*7c478bd9Sstevel@tonic-gate 					M_CURSES_INTERBYTE_TIME;
204*7c478bd9Sstevel@tonic-gate 				(void) __m_tty_set(&cur_term->_prog);
205*7c478bd9Sstevel@tonic-gate 			}
206*7c478bd9Sstevel@tonic-gate 			timeout = 0;
207*7c478bd9Sstevel@tonic-gate 
208*7c478bd9Sstevel@tonic-gate 			if ((ch = fgetc(__m_screen->_if)) == EOF)
209*7c478bd9Sstevel@tonic-gate 				/* Timeout or real eof. */
210*7c478bd9Sstevel@tonic-gate 				break;
211*7c478bd9Sstevel@tonic-gate 
212*7c478bd9Sstevel@tonic-gate 			/* Incomplete sequence, continue. */
213*7c478bd9Sstevel@tonic-gate 			node = node->child;
214*7c478bd9Sstevel@tonic-gate 		}
215*7c478bd9Sstevel@tonic-gate invalid:
216*7c478bd9Sstevel@tonic-gate 		/* Reverse contents of the input queue to form a stack. */
217*7c478bd9Sstevel@tonic-gate 		for (i = 0, j = __m_screen->_unget._count; i < --j; ++i) {
218*7c478bd9Sstevel@tonic-gate 			ch = __m_screen->_unget._stack[i];
219*7c478bd9Sstevel@tonic-gate 			__m_screen->_unget._stack[i] =
220*7c478bd9Sstevel@tonic-gate 				__m_screen->_unget._stack[j];
221*7c478bd9Sstevel@tonic-gate 			__m_screen->_unget._stack[j] = ch;
222*7c478bd9Sstevel@tonic-gate 		}
223*7c478bd9Sstevel@tonic-gate 
224*7c478bd9Sstevel@tonic-gate 		/* Return first byte received or EOF. */
225*7c478bd9Sstevel@tonic-gate 		ch = POP();
226*7c478bd9Sstevel@tonic-gate 	}
227*7c478bd9Sstevel@tonic-gate 
228*7c478bd9Sstevel@tonic-gate 	if ((__m_screen->_flags & S_ECHO) && 0 <= ch && ch != EOF)  {
229*7c478bd9Sstevel@tonic-gate 		(void) waddch(w, ch);
230*7c478bd9Sstevel@tonic-gate 		(void) wrefresh(w);
231*7c478bd9Sstevel@tonic-gate 	}
232*7c478bd9Sstevel@tonic-gate 
233*7c478bd9Sstevel@tonic-gate 	return __m_return_int("wgetch", ch);
234*7c478bd9Sstevel@tonic-gate }
235*7c478bd9Sstevel@tonic-gate 
236