xref: /titanic_44/usr/src/lib/libcurses/screen/chkinput.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 1997 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate /*
31*7c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
32*7c478bd9Sstevel@tonic-gate  * The Regents of the University of California
33*7c478bd9Sstevel@tonic-gate  * All Rights Reserved
34*7c478bd9Sstevel@tonic-gate  *
35*7c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
36*7c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
37*7c478bd9Sstevel@tonic-gate  * contributors.
38*7c478bd9Sstevel@tonic-gate  */
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate /*
45*7c478bd9Sstevel@tonic-gate  * chkinput()
46*7c478bd9Sstevel@tonic-gate  *
47*7c478bd9Sstevel@tonic-gate  * Routine to check to see if any input is waiting.
48*7c478bd9Sstevel@tonic-gate  * It returns a 1 if there is input waiting, and a zero if
49*7c478bd9Sstevel@tonic-gate  * no input is waiting.
50*7c478bd9Sstevel@tonic-gate  *
51*7c478bd9Sstevel@tonic-gate  * This function replaces system calls to ioctl() with the FIONREAD
52*7c478bd9Sstevel@tonic-gate  * parameter. It enables curses to stop a screen refresh whenever
53*7c478bd9Sstevel@tonic-gate  * a character is input.
54*7c478bd9Sstevel@tonic-gate  * Standard BTL UNIX 4.0 or 5.0 does not handle FIONREAD.
55*7c478bd9Sstevel@tonic-gate  * Changes have been made to 'wrefresh.c' to
56*7c478bd9Sstevel@tonic-gate  * call this routine as "_inputpending = chkinput()".
57*7c478bd9Sstevel@tonic-gate  * (delay.c and getch.c also use FIONREAD for nodelay, select and fast peek,
58*7c478bd9Sstevel@tonic-gate  * but these routines have not been changed).
59*7c478bd9Sstevel@tonic-gate  *
60*7c478bd9Sstevel@tonic-gate  * Philip N. Crusius - July 20, 1983
61*7c478bd9Sstevel@tonic-gate  * Modified to handle various systems March 9, 1984 by Mark Horton.
62*7c478bd9Sstevel@tonic-gate  */
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate #include	<unistd.h>
65*7c478bd9Sstevel@tonic-gate #include	<sys/types.h>
66*7c478bd9Sstevel@tonic-gate #include	"curses_inc.h"
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate #ifdef	FIONREAD
69*7c478bd9Sstevel@tonic-gate #define	HAVE_CHK
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate int
_chkinput(void)72*7c478bd9Sstevel@tonic-gate _chkinput(void)
73*7c478bd9Sstevel@tonic-gate {
74*7c478bd9Sstevel@tonic-gate 	int	i;
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate 	ioctl(SP->check_fd, FIONREAD, &i);
77*7c478bd9Sstevel@tonic-gate 	return (i > 0);
78*7c478bd9Sstevel@tonic-gate }
79*7c478bd9Sstevel@tonic-gate #endif	/* FIONREAD */
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate #ifdef	SELECT
82*7c478bd9Sstevel@tonic-gate #ifndef	HAVE_CHK
83*7c478bd9Sstevel@tonic-gate #define	HAVE_CHK
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate int
_chkinput(void)86*7c478bd9Sstevel@tonic-gate _chkinput(void)
87*7c478bd9Sstevel@tonic-gate {
88*7c478bd9Sstevel@tonic-gate 	int ifds, ofds, efds, n;
89*7c478bd9Sstevel@tonic-gate 	struct timeval tv;
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate 	ifds = 1 << SP->check_fd;
92*7c478bd9Sstevel@tonic-gate 	ofds = efds = 0;
93*7c478bd9Sstevel@tonic-gate 	tv.tv_sec = tv.t_usec = 0;
94*7c478bd9Sstevel@tonic-gate 	n = select(20, &ifds, &ofds, &efds, &tv);
95*7c478bd9Sstevel@tonic-gate 	return (n > 0);
96*7c478bd9Sstevel@tonic-gate }
97*7c478bd9Sstevel@tonic-gate #endif	/* HAVE_CHK */
98*7c478bd9Sstevel@tonic-gate #endif	/* SELECT */
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate #ifndef	HAVE_CHK
101*7c478bd9Sstevel@tonic-gate #ifdef	SYSV
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate int
_chkinput(void)104*7c478bd9Sstevel@tonic-gate _chkinput(void)
105*7c478bd9Sstevel@tonic-gate {
106*7c478bd9Sstevel@tonic-gate 	unsigned	char	c;	/* character input */
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate 	/*
109*7c478bd9Sstevel@tonic-gate 	 * Only check typeahead if the user is using our input
110*7c478bd9Sstevel@tonic-gate 	 * routines. This is because the read below will put
111*7c478bd9Sstevel@tonic-gate 	 * stuff into the inputQ that will never be read and the
112*7c478bd9Sstevel@tonic-gate 	 * screen will never get updated from now on.
113*7c478bd9Sstevel@tonic-gate 	 * This code should GO AWAY when a poll() or FIONREAD can
114*7c478bd9Sstevel@tonic-gate 	 * be done on the file descriptor as then the check
115*7c478bd9Sstevel@tonic-gate 	 * will be non-destructive.
116*7c478bd9Sstevel@tonic-gate 	 */
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate 	if (!cur_term->fl_typeahdok ||
119*7c478bd9Sstevel@tonic-gate 	    (cur_term->_chars_on_queue == INP_QSIZE) ||
120*7c478bd9Sstevel@tonic-gate 	    (cur_term->_check_fd < 0)) {
121*7c478bd9Sstevel@tonic-gate 		goto bad;
122*7c478bd9Sstevel@tonic-gate 	}
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate 	/* If input is waiting in curses queue, return (TRUE). */
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate 	if ((int)cur_term->_chars_on_queue > 0) {
127*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
128*7c478bd9Sstevel@tonic-gate 		if (outf) {
129*7c478bd9Sstevel@tonic-gate 			(void) fprintf(outf, "Found a character on the input "
130*7c478bd9Sstevel@tonic-gate 			    "queue\n");
131*7c478bd9Sstevel@tonic-gate 			_print_queue();
132*7c478bd9Sstevel@tonic-gate 		}
133*7c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
134*7c478bd9Sstevel@tonic-gate 		goto good;
135*7c478bd9Sstevel@tonic-gate 	}
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate 	if (read(cur_term->_check_fd, (char *)&c, 1) > 0) {
138*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
139*7c478bd9Sstevel@tonic-gate 		if (outf) {
140*7c478bd9Sstevel@tonic-gate 			(void) fprintf(outf, "Reading ahead\n");
141*7c478bd9Sstevel@tonic-gate 		}
142*7c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
143*7c478bd9Sstevel@tonic-gate 		/*
144*7c478bd9Sstevel@tonic-gate 		 * A character was waiting.  Put it at the end
145*7c478bd9Sstevel@tonic-gate 		 * of the curses queue and return 1 to show that
146*7c478bd9Sstevel@tonic-gate 		 * input is waiting.
147*7c478bd9Sstevel@tonic-gate 		 */
148*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
149*7c478bd9Sstevel@tonic-gate 		if (outf)
150*7c478bd9Sstevel@tonic-gate 			_print_queue();
151*7c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
152*7c478bd9Sstevel@tonic-gate 		cur_term->_input_queue[cur_term->_chars_on_queue++] = c;
153*7c478bd9Sstevel@tonic-gate good:
154*7c478bd9Sstevel@tonic-gate 		return (TRUE);
155*7c478bd9Sstevel@tonic-gate 	} else {
156*7c478bd9Sstevel@tonic-gate 		/* No input was waiting so return 0. */
157*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
158*7c478bd9Sstevel@tonic-gate 		if (outf)
159*7c478bd9Sstevel@tonic-gate 			(void) fprintf(outf, "No input waiting\n");
160*7c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
161*7c478bd9Sstevel@tonic-gate bad:
162*7c478bd9Sstevel@tonic-gate 		return (FALSE);
163*7c478bd9Sstevel@tonic-gate 	}
164*7c478bd9Sstevel@tonic-gate }
165*7c478bd9Sstevel@tonic-gate #else	/* SYSV */
166*7c478bd9Sstevel@tonic-gate int
_chkinput()167*7c478bd9Sstevel@tonic-gate _chkinput()
168*7c478bd9Sstevel@tonic-gate {
169*7c478bd9Sstevel@tonic-gate 	return (FALSE);
170*7c478bd9Sstevel@tonic-gate }
171*7c478bd9Sstevel@tonic-gate #endif	/* SYSV */
172*7c478bd9Sstevel@tonic-gate #endif	/* HAVE_CHK */
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
175*7c478bd9Sstevel@tonic-gate void
_print_queue()176*7c478bd9Sstevel@tonic-gate _print_queue()	/* FOR DEBUG ONLY */
177*7c478bd9Sstevel@tonic-gate {
178*7c478bd9Sstevel@tonic-gate 	int		i, j = cur_term->_chars_on_queue;
179*7c478bd9Sstevel@tonic-gate 	chtype		*inputQ = cur_term->_input_queue;
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate 	if (outf)
182*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < j; i++)
183*7c478bd9Sstevel@tonic-gate 			(void) fprintf(outf, "inputQ[%d] = %c\n", i, inputQ[i]);
184*7c478bd9Sstevel@tonic-gate }
185*7c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
186