xref: /titanic_44/usr/src/lib/libcurses/screen/typeahead.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 1997 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1988 AT&T	*/
28 /*	  All Rights Reserved	*/
29 
30 /*
31  * University Copyright- Copyright (c) 1982, 1986, 1988
32  * The Regents of the University of California
33  * All Rights Reserved
34  *
35  * University Acknowledgment- Portions of this document are derived from
36  * software developed by the University of California, Berkeley, and its
37  * contributors.
38  */
39 
40 #pragma ident	"%Z%%M%	%I%	%E% SMI"
41 
42 /*LINTLIBRARY*/
43 
44 #include	<unistd.h>
45 #include	<sys/types.h>
46 #include	"curses_inc.h"
47 
48 /*
49  * Set the file descriptor for typeahead checks to fd.  fd can be -1
50  * to disable the checking.
51  */
52 
53 int
typeahead(int fd)54 typeahead(int fd)
55 {
56 #ifdef	SYSV
57 	/*
58 	 * Doing fcntls before and after each typeahead check
59 	 * read is a serious problem on the 3b2. Profiling
60 	 * results indicated that a simple program(edit.c from
61 	 * "The New Curses and Terminfo Package") was spending
62 	 * 9.2% of the time in fcntl().
63 	 */
64 
65 #include	<fcntl.h>
66 	int	savefd = cur_term->_check_fd;
67 
68 	/* Close the previous duped file descriptor. */
69 	if (savefd >= 0)
70 		(void) close(savefd);
71 
72 	/*
73 	 * Duplicate the file descriptor so we have one to play with.
74 	 * We cannot use dup(2), unfortunately, so we do typeahead checking
75 	 * on terminals only. Besides, if typeahead is done when input is
76 	 * coming from a file, nothing would EVER get drawn on the screen!
77 	 */
78 
79 	/* MODIFIED: DRS 3/26/89 */
80 
81 	/*
82 	 * a couple of notes by DRS:  first, a new file descriptor is
83 	 * required, since O_NDELAY must be set.  calling fcntl() or dup()
84 	 * would provide a new file descriptor, but NOT a new file pointer
85 	 * for the open file (by file pointer, i mean a unix kernal file
86 	 * ptr).  if a new underlying unix file ptr. is not allocated,
87 	 * setting O_NDELAY will cause all normal terminal i/o to return
88 	 * prematurely without blocking.
89 	 *
90 	 * second, the call to ttyname is NOT necessary, /dev/tty can be
91 	 * used instead.  calling ttyname is quite expensive -- especially
92 	 * for large /dev directories.
93 	 *
94 	 * note also that the code for the '#else' clause will not work
95 	 * since the new file descriptor MUST have O_NDELAY set for the
96 	 * rest of libcurses code to function properly.
97 	 *
98 	 * 4/24/89:  modified to set the close on exec() flag of the newly
99 	 * 		opened file descriptor
100 	 */
101 
102 	/*
103 	 *	cur_term->_check_fd = (tty = ttyname(fd)) ?
104 	 *	    open(tty, O_RDONLY | O_NDELAY) : -1;
105 	 */
106 	if (isatty(fd)) {
107 		if ((cur_term->_check_fd = open("/dev/tty", O_RDONLY |
108 		    O_NDELAY)) >= 0)
109 			(void) fcntl(cur_term->_check_fd, F_SETFD, 1);
110 	} else
111 		cur_term->_check_fd = -1;
112 
113 #else	/* SYSV */
114 	int savefd = cur_term->_check_fd;
115 	/* Only do typeahead checking if the input is a tty. */
116 	if (isatty(fd))
117 		cur_term->_check_fd = fd;
118 	else
119 		cur_term->_check_fd = -1;
120 #endif	/* SYSV */
121 	return (savefd);
122 }
123