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-1998, 2000 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /* LINTLIBRARY */
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[] =
41*7c478bd9Sstevel@tonic-gate "$Header: /team/ps/sun_xcurses/archive/local_changes/xcurses/src/lib/"
42*7c478bd9Sstevel@tonic-gate "libxcurses/src/libc/xcurses/rcs/wgetch.c 1.23 1998/06/05 16:38:43 "
43*7c478bd9Sstevel@tonic-gate "cbates Exp $";
44*7c478bd9Sstevel@tonic-gate #endif
45*7c478bd9Sstevel@tonic-gate #endif
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate #include <private.h>
48*7c478bd9Sstevel@tonic-gate #include <string.h>
49*7c478bd9Sstevel@tonic-gate #include <errno.h>
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate static struct termios	read_termios;
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate int
iqContainsFullLine(void)54*7c478bd9Sstevel@tonic-gate iqContainsFullLine(void)
55*7c478bd9Sstevel@tonic-gate {
56*7c478bd9Sstevel@tonic-gate 	int	i;
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate 	if (!(PTERMIOS(_prog)->c_lflag & ICANON)) {
59*7c478bd9Sstevel@tonic-gate 		/*
60*7c478bd9Sstevel@tonic-gate 		 * Non-canonical mode ...
61*7c478bd9Sstevel@tonic-gate 		 * Don't care about full lines ... continue
62*7c478bd9Sstevel@tonic-gate 		 */
63*7c478bd9Sstevel@tonic-gate 		return (1);
64*7c478bd9Sstevel@tonic-gate 	}
65*7c478bd9Sstevel@tonic-gate 	if (read_termios.c_lflag & ICANON) {
66*7c478bd9Sstevel@tonic-gate 		/*
67*7c478bd9Sstevel@tonic-gate 		 * Terminal subsystem worries about lines etc. ...
68*7c478bd9Sstevel@tonic-gate 		 * continue ...
69*7c478bd9Sstevel@tonic-gate 		 */
70*7c478bd9Sstevel@tonic-gate 		return (1);
71*7c478bd9Sstevel@tonic-gate 	}
72*7c478bd9Sstevel@tonic-gate 	/* We turned off ICANON so we have to do it ourselves */
73*7c478bd9Sstevel@tonic-gate 	if ((read_termios.c_cc[VMIN] == 0) &&
74*7c478bd9Sstevel@tonic-gate 		(read_termios.c_cc[VTIME] != 0)) {
75*7c478bd9Sstevel@tonic-gate 		/* We set delay mode. Only error if noting in the read */
76*7c478bd9Sstevel@tonic-gate 		return (!iqIsEmpty());
77*7c478bd9Sstevel@tonic-gate 	}
78*7c478bd9Sstevel@tonic-gate 	for (i = __m_screen->_unget._count - 1; i >= 0; i--) {
79*7c478bd9Sstevel@tonic-gate 		int	ch = __m_screen->_unget._stack[i];
80*7c478bd9Sstevel@tonic-gate 		if (PTERMIOS(_shell)->c_cc[VEOL] == ch)
81*7c478bd9Sstevel@tonic-gate 			return (1);
82*7c478bd9Sstevel@tonic-gate 		if ('\n' == ch)
83*7c478bd9Sstevel@tonic-gate 			return (1);
84*7c478bd9Sstevel@tonic-gate 	}
85*7c478bd9Sstevel@tonic-gate 	return (0);
86*7c478bd9Sstevel@tonic-gate }
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate void
iqPush(unsigned int ch)89*7c478bd9Sstevel@tonic-gate iqPush(unsigned int ch)
90*7c478bd9Sstevel@tonic-gate {
91*7c478bd9Sstevel@tonic-gate 	if (__m_screen->_unget._count >= __m_screen->_unget._size)
92*7c478bd9Sstevel@tonic-gate 		return;
93*7c478bd9Sstevel@tonic-gate 	__m_screen->_unget._stack[__m_screen->_unget._count++] =
94*7c478bd9Sstevel@tonic-gate 		(int) ch;
95*7c478bd9Sstevel@tonic-gate }
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate void
iqAdd(unsigned int ch)98*7c478bd9Sstevel@tonic-gate iqAdd(unsigned int ch)
99*7c478bd9Sstevel@tonic-gate {
100*7c478bd9Sstevel@tonic-gate 	int	count;
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate 	if (++(__m_screen->_unget._count) >= __m_screen->_unget._size)
103*7c478bd9Sstevel@tonic-gate 		__m_screen->_unget._count = __m_screen->_unget._size - 1;
104*7c478bd9Sstevel@tonic-gate 	count = __m_screen->_unget._count - 1;
105*7c478bd9Sstevel@tonic-gate 	if (count) {
106*7c478bd9Sstevel@tonic-gate 		(void) memmove(__m_screen->_unget._stack + 1,
107*7c478bd9Sstevel@tonic-gate 			__m_screen->_unget._stack, count * sizeof (int));
108*7c478bd9Sstevel@tonic-gate 	}
109*7c478bd9Sstevel@tonic-gate 	__m_screen->_unget._stack[0] = (int) ch;
110*7c478bd9Sstevel@tonic-gate }
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate int
iqIsEmpty(void)113*7c478bd9Sstevel@tonic-gate iqIsEmpty(void)
114*7c478bd9Sstevel@tonic-gate {
115*7c478bd9Sstevel@tonic-gate 	return (__m_screen->_unget._count == 0);
116*7c478bd9Sstevel@tonic-gate }
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate void
iqReset(void)119*7c478bd9Sstevel@tonic-gate iqReset(void)
120*7c478bd9Sstevel@tonic-gate {
121*7c478bd9Sstevel@tonic-gate 	__m_screen->_unget._count = 0;
122*7c478bd9Sstevel@tonic-gate }
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate /* Assumes count > 0 */
125*7c478bd9Sstevel@tonic-gate int
iqPull(void)126*7c478bd9Sstevel@tonic-gate iqPull(void)
127*7c478bd9Sstevel@tonic-gate {
128*7c478bd9Sstevel@tonic-gate 	int	ch;
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate 	ch = __m_screen->_unget._stack[--(__m_screen->_unget._count)];
131*7c478bd9Sstevel@tonic-gate 	return (ch);
132*7c478bd9Sstevel@tonic-gate }
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate /* Discard n characters from front of Q */
135*7c478bd9Sstevel@tonic-gate void
iqTrash(int n)136*7c478bd9Sstevel@tonic-gate iqTrash(int n)
137*7c478bd9Sstevel@tonic-gate {
138*7c478bd9Sstevel@tonic-gate 	__m_screen->_unget._count -= n;
139*7c478bd9Sstevel@tonic-gate 	if (__m_screen->_unget._count < 0) {
140*7c478bd9Sstevel@tonic-gate 		__m_screen->_unget._count = 0;
141*7c478bd9Sstevel@tonic-gate 	}
142*7c478bd9Sstevel@tonic-gate }
143*7c478bd9Sstevel@tonic-gate 
144*7c478bd9Sstevel@tonic-gate int
iqGetNth(int n)145*7c478bd9Sstevel@tonic-gate iqGetNth(int n)
146*7c478bd9Sstevel@tonic-gate {
147*7c478bd9Sstevel@tonic-gate 	int	ch;
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate 	if (__m_screen->_unget._count - n <= 0) {
150*7c478bd9Sstevel@tonic-gate 		return (EOF);
151*7c478bd9Sstevel@tonic-gate 	}
152*7c478bd9Sstevel@tonic-gate 	ch = __m_screen->_unget._stack[__m_screen->_unget._count - n - 1];
153*7c478bd9Sstevel@tonic-gate 	return (ch);
154*7c478bd9Sstevel@tonic-gate }
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate 
157*7c478bd9Sstevel@tonic-gate struct termios
__m_tty_override_mode(int vmin,int vtime)158*7c478bd9Sstevel@tonic-gate __m_tty_override_mode(int vmin, int vtime)
159*7c478bd9Sstevel@tonic-gate {
160*7c478bd9Sstevel@tonic-gate 	struct termios	rval;
161*7c478bd9Sstevel@tonic-gate 	struct termios	newstuff;
162*7c478bd9Sstevel@tonic-gate 
163*7c478bd9Sstevel@tonic-gate 	rval = newstuff = *PTERMIOS(_actual);
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate 	/* If halfdelay mode. Leave canonical mode intact */
166*7c478bd9Sstevel@tonic-gate 	if (!(vmin == 0 && vtime == 0) &&
167*7c478bd9Sstevel@tonic-gate 		(cur_term->_flags & __TERM_HALF_DELAY))
168*7c478bd9Sstevel@tonic-gate 		return (rval);
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate 	/* If blocking mode. Leave canonical mode intact */
171*7c478bd9Sstevel@tonic-gate 	if (vmin == 1)
172*7c478bd9Sstevel@tonic-gate 		return (rval);
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate 	/* VMIN and VTIME trash VEOL and VEOF so canonical cannot work */
175*7c478bd9Sstevel@tonic-gate 	newstuff.c_cc[VMIN] = (cc_t) vmin;
176*7c478bd9Sstevel@tonic-gate 	newstuff.c_cc[VTIME] = (cc_t) vtime;
177*7c478bd9Sstevel@tonic-gate 	newstuff.c_lflag &= ~ICANON;
178*7c478bd9Sstevel@tonic-gate 
179*7c478bd9Sstevel@tonic-gate 	(void) __m_tty_set(&newstuff);
180*7c478bd9Sstevel@tonic-gate 	return (rval);
181*7c478bd9Sstevel@tonic-gate }
182*7c478bd9Sstevel@tonic-gate 
183*7c478bd9Sstevel@tonic-gate int
__m_read_input_char(int * pChar)184*7c478bd9Sstevel@tonic-gate __m_read_input_char(int *pChar)
185*7c478bd9Sstevel@tonic-gate {
186*7c478bd9Sstevel@tonic-gate 	if (req_for_input != NULL) {
187*7c478bd9Sstevel@tonic-gate 		(void) TPUTS(req_for_input, 1, __m_outc);
188*7c478bd9Sstevel@tonic-gate 	}
189*7c478bd9Sstevel@tonic-gate 	clearerr(__m_screen->_if);
190*7c478bd9Sstevel@tonic-gate 	*pChar = 0;
191*7c478bd9Sstevel@tonic-gate 	/* save actual setting for later test */
192*7c478bd9Sstevel@tonic-gate 	read_termios = *PTERMIOS(_actual);
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate 	errno = 0;
195*7c478bd9Sstevel@tonic-gate 	if ((*pChar = fgetc(__m_screen->_if)) == EOF) {
196*7c478bd9Sstevel@tonic-gate 		return ((errno) ? ERR : OK);
197*7c478bd9Sstevel@tonic-gate 	}
198*7c478bd9Sstevel@tonic-gate 
199*7c478bd9Sstevel@tonic-gate 	if (((PTERMIOS(_prog)->c_cflag & CSIZE) != CS8) && (*pChar != EOF))
200*7c478bd9Sstevel@tonic-gate 		*pChar &= 0x7f;
201*7c478bd9Sstevel@tonic-gate 	return (OK);
202*7c478bd9Sstevel@tonic-gate }
203*7c478bd9Sstevel@tonic-gate 
204*7c478bd9Sstevel@tonic-gate int
__m_typeahead_read_input_char(int * pChar)205*7c478bd9Sstevel@tonic-gate __m_typeahead_read_input_char(int *pChar)
206*7c478bd9Sstevel@tonic-gate {
207*7c478bd9Sstevel@tonic-gate 	unsigned char	ch;
208*7c478bd9Sstevel@tonic-gate 	ssize_t	r;
209*7c478bd9Sstevel@tonic-gate 
210*7c478bd9Sstevel@tonic-gate 	if (req_for_input != NULL) {
211*7c478bd9Sstevel@tonic-gate 		(void) TPUTS(req_for_input, 1, __m_outc);
212*7c478bd9Sstevel@tonic-gate 	}
213*7c478bd9Sstevel@tonic-gate 
214*7c478bd9Sstevel@tonic-gate 	*pChar = 0;
215*7c478bd9Sstevel@tonic-gate 	/* save actual setting for later test */
216*7c478bd9Sstevel@tonic-gate 	read_termios = *PTERMIOS(_actual);
217*7c478bd9Sstevel@tonic-gate 
218*7c478bd9Sstevel@tonic-gate 	errno = 0;
219*7c478bd9Sstevel@tonic-gate 	if ((r = read(__m_screen->_kfd, (void *)&ch, 1)) > 0) {
220*7c478bd9Sstevel@tonic-gate 		if ((PTERMIOS(_prog)->c_cflag & CSIZE) != CS8) {
221*7c478bd9Sstevel@tonic-gate 			*pChar = ch & 0x7f;
222*7c478bd9Sstevel@tonic-gate 		} else {
223*7c478bd9Sstevel@tonic-gate 			*pChar = (int)ch;
224*7c478bd9Sstevel@tonic-gate 		}
225*7c478bd9Sstevel@tonic-gate 		return (OK);
226*7c478bd9Sstevel@tonic-gate 	} else if (r == 0) {
227*7c478bd9Sstevel@tonic-gate 		*pChar = EOF;
228*7c478bd9Sstevel@tonic-gate 		return (OK);
229*7c478bd9Sstevel@tonic-gate 	} else {
230*7c478bd9Sstevel@tonic-gate 		return (ERR);
231*7c478bd9Sstevel@tonic-gate 	}
232*7c478bd9Sstevel@tonic-gate }
233*7c478bd9Sstevel@tonic-gate 
234*7c478bd9Sstevel@tonic-gate 
235*7c478bd9Sstevel@tonic-gate static int	klugeTypeaheadInGetch = 0;
236*7c478bd9Sstevel@tonic-gate 
237*7c478bd9Sstevel@tonic-gate int
pollTypeahead(void)238*7c478bd9Sstevel@tonic-gate pollTypeahead(void)
239*7c478bd9Sstevel@tonic-gate {
240*7c478bd9Sstevel@tonic-gate 	struct termios	save;
241*7c478bd9Sstevel@tonic-gate 	int	ch;
242*7c478bd9Sstevel@tonic-gate 
243*7c478bd9Sstevel@tonic-gate 	if (!(__m_screen->_flags & S_ISATTY) ||
244*7c478bd9Sstevel@tonic-gate 		!(__m_screen->_flags & S_TYPEAHEAD_OK)) {
245*7c478bd9Sstevel@tonic-gate 		/* Typeahead disabled */
246*7c478bd9Sstevel@tonic-gate 		return (0);
247*7c478bd9Sstevel@tonic-gate 	}
248*7c478bd9Sstevel@tonic-gate 	save = __m_tty_override_mode(0, 0);
249*7c478bd9Sstevel@tonic-gate 	while (__m_typeahead_read_input_char(&ch) == OK) {
250*7c478bd9Sstevel@tonic-gate 		if (ch == EOF)
251*7c478bd9Sstevel@tonic-gate 			break;
252*7c478bd9Sstevel@tonic-gate 		iqAdd(ch);
253*7c478bd9Sstevel@tonic-gate 	}
254*7c478bd9Sstevel@tonic-gate 	(void) __m_tty_set(&save);
255*7c478bd9Sstevel@tonic-gate 	/* if in wgetch, always do refresh */
256*7c478bd9Sstevel@tonic-gate 	return ((klugeTypeaheadInGetch) ? 0 : !iqIsEmpty());
257*7c478bd9Sstevel@tonic-gate }
258*7c478bd9Sstevel@tonic-gate 
259*7c478bd9Sstevel@tonic-gate /*
260*7c478bd9Sstevel@tonic-gate  * Push single-byte character back onto the input queue.
261*7c478bd9Sstevel@tonic-gate  *
262*7c478bd9Sstevel@tonic-gate  * MKS EXTENSION permits the return value of wgetch(), which
263*7c478bd9Sstevel@tonic-gate  * can be a KEY_ value, to be pushed back.
264*7c478bd9Sstevel@tonic-gate  */
265*7c478bd9Sstevel@tonic-gate int
ungetch(int ch)266*7c478bd9Sstevel@tonic-gate ungetch(int ch)
267*7c478bd9Sstevel@tonic-gate {
268*7c478bd9Sstevel@tonic-gate 	iqPush(ch);
269*7c478bd9Sstevel@tonic-gate 	return (OK);
270*7c478bd9Sstevel@tonic-gate }
271*7c478bd9Sstevel@tonic-gate 
272*7c478bd9Sstevel@tonic-gate /*
273*7c478bd9Sstevel@tonic-gate  * Return true if the SCREEN's stream has an I/O error.
274*7c478bd9Sstevel@tonic-gate  * Ignore the window parameter.
275*7c478bd9Sstevel@tonic-gate  */
276*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
277*7c478bd9Sstevel@tonic-gate int
__xc_ferror(void * w)278*7c478bd9Sstevel@tonic-gate __xc_ferror(void *w)
279*7c478bd9Sstevel@tonic-gate {
280*7c478bd9Sstevel@tonic-gate 	return (ferror(__m_screen->_if));
281*7c478bd9Sstevel@tonic-gate }
282*7c478bd9Sstevel@tonic-gate 
283*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
284*7c478bd9Sstevel@tonic-gate int
__xc_ungetc(int ch,void * w)285*7c478bd9Sstevel@tonic-gate __xc_ungetc(int ch, void *w)
286*7c478bd9Sstevel@tonic-gate {
287*7c478bd9Sstevel@tonic-gate 	iqPush(ch);
288*7c478bd9Sstevel@tonic-gate 	return (1);
289*7c478bd9Sstevel@tonic-gate }
290*7c478bd9Sstevel@tonic-gate 
291*7c478bd9Sstevel@tonic-gate /*
292*7c478bd9Sstevel@tonic-gate  * Return true if the SCREEN's stream has seen EOF.
293*7c478bd9Sstevel@tonic-gate  * Ignore the window parameter.
294*7c478bd9Sstevel@tonic-gate  */
295*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
296*7c478bd9Sstevel@tonic-gate int
__xc_feof(void * w)297*7c478bd9Sstevel@tonic-gate __xc_feof(void *w)
298*7c478bd9Sstevel@tonic-gate {
299*7c478bd9Sstevel@tonic-gate 	return (feof(__m_screen->_if));
300*7c478bd9Sstevel@tonic-gate }
301*7c478bd9Sstevel@tonic-gate 
302*7c478bd9Sstevel@tonic-gate /*
303*7c478bd9Sstevel@tonic-gate  * Clear the error and eof flags of the SCREEN's stream.
304*7c478bd9Sstevel@tonic-gate  * Ignore the window parameter.
305*7c478bd9Sstevel@tonic-gate  */
306*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
307*7c478bd9Sstevel@tonic-gate void
__xc_clearerr(void * w)308*7c478bd9Sstevel@tonic-gate __xc_clearerr(void *w)
309*7c478bd9Sstevel@tonic-gate {
310*7c478bd9Sstevel@tonic-gate 	clearerr(__m_screen->_if);
311*7c478bd9Sstevel@tonic-gate }
312*7c478bd9Sstevel@tonic-gate 
313*7c478bd9Sstevel@tonic-gate int
__m_echo(WINDOW * w,int ch)314*7c478bd9Sstevel@tonic-gate __m_echo(WINDOW *w, int ch)
315*7c478bd9Sstevel@tonic-gate {
316*7c478bd9Sstevel@tonic-gate 	if (!(__m_screen->_flags & S_ECHO))
317*7c478bd9Sstevel@tonic-gate 		return (ch);
318*7c478bd9Sstevel@tonic-gate 	if (!(0 <= ch && ch != EOF)) {
319*7c478bd9Sstevel@tonic-gate 		(void) beep();
320*7c478bd9Sstevel@tonic-gate 		return (ERR);
321*7c478bd9Sstevel@tonic-gate 	}
322*7c478bd9Sstevel@tonic-gate 	if (ch == '\b') {
323*7c478bd9Sstevel@tonic-gate 		if (w->_curx <= 0) {
324*7c478bd9Sstevel@tonic-gate 			(void) beep();
325*7c478bd9Sstevel@tonic-gate 			return (ch);
326*7c478bd9Sstevel@tonic-gate 		}
327*7c478bd9Sstevel@tonic-gate 		w->_curx--;
328*7c478bd9Sstevel@tonic-gate 		(void) wdelch(w);
329*7c478bd9Sstevel@tonic-gate 	} else {
330*7c478bd9Sstevel@tonic-gate 		(void) waddch(w, ch);
331*7c478bd9Sstevel@tonic-gate 	}
332*7c478bd9Sstevel@tonic-gate 	(void) wrefresh(w);
333*7c478bd9Sstevel@tonic-gate 	return (ch);
334*7c478bd9Sstevel@tonic-gate }
335*7c478bd9Sstevel@tonic-gate 
336*7c478bd9Sstevel@tonic-gate int
wgetch(WINDOW * w)337*7c478bd9Sstevel@tonic-gate wgetch(WINDOW *w)
338*7c478bd9Sstevel@tonic-gate {
339*7c478bd9Sstevel@tonic-gate 	t_decode	*node;
340*7c478bd9Sstevel@tonic-gate 	int	ch, i, timeout;
341*7c478bd9Sstevel@tonic-gate 	struct termios	save;
342*7c478bd9Sstevel@tonic-gate 
343*7c478bd9Sstevel@tonic-gate 	__m_screen->_flags |= S_TYPEAHEAD_OK;
344*7c478bd9Sstevel@tonic-gate 
345*7c478bd9Sstevel@tonic-gate 	klugeTypeaheadInGetch = 1;
346*7c478bd9Sstevel@tonic-gate 	(void) wrefresh(w);
347*7c478bd9Sstevel@tonic-gate 	klugeTypeaheadInGetch = 0;
348*7c478bd9Sstevel@tonic-gate 
349*7c478bd9Sstevel@tonic-gate 	if (iqIsEmpty()) {
350*7c478bd9Sstevel@tonic-gate 	    save = __m_tty_override_mode(w->_vmin, w->_vtime);
351*7c478bd9Sstevel@tonic-gate 	    if (__m_read_input_char(&ch) == ERR) {
352*7c478bd9Sstevel@tonic-gate 			(void) __m_tty_set(&save);
353*7c478bd9Sstevel@tonic-gate 			return (ERR);
354*7c478bd9Sstevel@tonic-gate 	    }
355*7c478bd9Sstevel@tonic-gate 	    if (!((ch == EOF) && (PTERMIOS(_prog)->c_lflag & ICANON))) {
356*7c478bd9Sstevel@tonic-gate 			/* Put EOF on Q only in non-canonical mode */
357*7c478bd9Sstevel@tonic-gate 			iqAdd(ch);
358*7c478bd9Sstevel@tonic-gate 	    }
359*7c478bd9Sstevel@tonic-gate 		(void) __m_tty_set(&save);
360*7c478bd9Sstevel@tonic-gate 	}
361*7c478bd9Sstevel@tonic-gate 	ch = iqGetNth(0);
362*7c478bd9Sstevel@tonic-gate 	if (!iqContainsFullLine()) {
363*7c478bd9Sstevel@tonic-gate 	    return (ERR);
364*7c478bd9Sstevel@tonic-gate 	}
365*7c478bd9Sstevel@tonic-gate 
366*7c478bd9Sstevel@tonic-gate 	/*
367*7c478bd9Sstevel@tonic-gate 	 * Only check for function keys if keypad is true and we
368*7c478bd9Sstevel@tonic-gate 	 * did not read a KEY_ value (which are < 0), nor EOF.
369*7c478bd9Sstevel@tonic-gate 	 * It is conceivable that a KEY_ was pushed back with
370*7c478bd9Sstevel@tonic-gate 	 * ungetch().
371*7c478bd9Sstevel@tonic-gate 	 */
372*7c478bd9Sstevel@tonic-gate 	if ((w->_flags & W_USE_KEYPAD) && 0 <= ch && ch != EOF) {
373*7c478bd9Sstevel@tonic-gate 		/*
374*7c478bd9Sstevel@tonic-gate 		 * Treat the termios ERASE key the same as key_backspace.
375*7c478bd9Sstevel@tonic-gate 		 *
376*7c478bd9Sstevel@tonic-gate 		 * We used to change the key_backspace entry to be a string
377*7c478bd9Sstevel@tonic-gate 		 * containing the ERASE key in setupterm(), but this would
378*7c478bd9Sstevel@tonic-gate 		 * then disable the real terminfo entry for the backspace key.
379*7c478bd9Sstevel@tonic-gate 		 * Apparently VT300 terminals change the key code/sequence
380*7c478bd9Sstevel@tonic-gate 		 * of the backspace key in application keypad mode.
381*7c478bd9Sstevel@tonic-gate 		 * See SR 6014.
382*7c478bd9Sstevel@tonic-gate 		 *
383*7c478bd9Sstevel@tonic-gate 		 * Refer to _shell instead of _prog, since _shell will
384*7c478bd9Sstevel@tonic-gate 		 * correctly reflect the user's prefered settings, whereas
385*7c478bd9Sstevel@tonic-gate 		 * _prog may not have been initialised if both input and
386*7c478bd9Sstevel@tonic-gate 		 * output have been redirected.
387*7c478bd9Sstevel@tonic-gate 		 */
388*7c478bd9Sstevel@tonic-gate #ifdef _POSIX_VDISABLE
389*7c478bd9Sstevel@tonic-gate 		if (PTERMIOS(_shell)->c_cc[VERASE] != _POSIX_VDISABLE)
390*7c478bd9Sstevel@tonic-gate #endif
391*7c478bd9Sstevel@tonic-gate 			if (ch == PTERMIOS(_shell)->c_cc[VERASE]) {
392*7c478bd9Sstevel@tonic-gate 				/* Discard ch from Q */
393*7c478bd9Sstevel@tonic-gate 				(void) iqPull();
394*7c478bd9Sstevel@tonic-gate 				return (KEY_BACKSPACE);
395*7c478bd9Sstevel@tonic-gate 			}
396*7c478bd9Sstevel@tonic-gate 
397*7c478bd9Sstevel@tonic-gate 		/* Begin check for function key. */
398*7c478bd9Sstevel@tonic-gate 		node = (t_decode *) __m_screen->_decode;
399*7c478bd9Sstevel@tonic-gate 
400*7c478bd9Sstevel@tonic-gate 		/* Use input stack as a queue. */
401*7c478bd9Sstevel@tonic-gate 		timeout = w->_flags & W_USE_TIMEOUT;
402*7c478bd9Sstevel@tonic-gate 		for (i = 1; ; i++) {
403*7c478bd9Sstevel@tonic-gate 			while (node->ch != ch) {
404*7c478bd9Sstevel@tonic-gate 				node = node->sibling;
405*7c478bd9Sstevel@tonic-gate 				if (node == NULL)
406*7c478bd9Sstevel@tonic-gate 					goto uncoded;
407*7c478bd9Sstevel@tonic-gate 			}
408*7c478bd9Sstevel@tonic-gate 
409*7c478bd9Sstevel@tonic-gate 			/* Found funuction key? */
410*7c478bd9Sstevel@tonic-gate 			if (node->key != 0) {
411*7c478bd9Sstevel@tonic-gate 				/* Trash all input used to make the FKey */
412*7c478bd9Sstevel@tonic-gate 				iqTrash(i);
413*7c478bd9Sstevel@tonic-gate 				return (__m_echo(w, node->key));
414*7c478bd9Sstevel@tonic-gate 			}
415*7c478bd9Sstevel@tonic-gate 
416*7c478bd9Sstevel@tonic-gate 			/*
417*7c478bd9Sstevel@tonic-gate 			 * Get next candidate character -
418*7c478bd9Sstevel@tonic-gate 			 * either from Q or input
419*7c478bd9Sstevel@tonic-gate 			 */
420*7c478bd9Sstevel@tonic-gate 			if ((ch = iqGetNth(i)) == EOF) {
421*7c478bd9Sstevel@tonic-gate 				/*
422*7c478bd9Sstevel@tonic-gate 				 * Setup interbyte timer (once only).
423*7c478bd9Sstevel@tonic-gate 				 * fgetc() will return EOF if no input received,
424*7c478bd9Sstevel@tonic-gate 				 * which may not be a true EOF.
425*7c478bd9Sstevel@tonic-gate 				 */
426*7c478bd9Sstevel@tonic-gate 				if (timeout) {
427*7c478bd9Sstevel@tonic-gate 					(void) __m_tty_override_mode(0,
428*7c478bd9Sstevel@tonic-gate 						M_CURSES_INTERBYTE_TIME);
429*7c478bd9Sstevel@tonic-gate 				}
430*7c478bd9Sstevel@tonic-gate 				timeout = 0;
431*7c478bd9Sstevel@tonic-gate 				if (__m_read_input_char(&ch) == ERR)
432*7c478bd9Sstevel@tonic-gate 				    return (ERR);
433*7c478bd9Sstevel@tonic-gate 				/* Timeout or real eof. */
434*7c478bd9Sstevel@tonic-gate 				if (ch == EOF)
435*7c478bd9Sstevel@tonic-gate 					break;
436*7c478bd9Sstevel@tonic-gate 				iqAdd(ch);
437*7c478bd9Sstevel@tonic-gate 			}
438*7c478bd9Sstevel@tonic-gate 
439*7c478bd9Sstevel@tonic-gate 			/* Incomplete sequence, continue. */
440*7c478bd9Sstevel@tonic-gate 			node = node->child;
441*7c478bd9Sstevel@tonic-gate 		}
442*7c478bd9Sstevel@tonic-gate 	}
443*7c478bd9Sstevel@tonic-gate uncoded:
444*7c478bd9Sstevel@tonic-gate 	/* Return first byte received or EOF. */
445*7c478bd9Sstevel@tonic-gate 	ch = iqPull();
446*7c478bd9Sstevel@tonic-gate 	return (__m_echo(w, ch));
447*7c478bd9Sstevel@tonic-gate }
448