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 /*LINTLIBRARY*/
41 
42 #include	<sys/types.h>
43 #include	"curses_inc.h"
44 #include	<fcntl.h>
45 
46 /*
47  * Set the current time-out period for getting input.
48  *
49  * delay:	< 0 for infinite delay (blocking read)
50  * 		= 0 for no delay read
51  * 		> 0 to specify a period in millisecs to wait
52  *		for input, then return '\0' if none comes
53  */
54 
55 static void	_setblock(int), _settimeout(int);
56 
57 int
ttimeout(int delay)58 ttimeout(int delay)
59 {
60 	if (cur_term->_inputfd < 0)
61 		return (ERR);
62 
63 	if (delay < 0)
64 		delay = -1;
65 
66 	if (cur_term->_delay == delay)
67 		goto good;
68 
69 #ifdef	SYSV
70 	if (delay > 0) {
71 		if (cur_term->_delay < 0)
72 			_setblock(-delay);
73 		_settimeout(delay);
74 	} else
75 		if ((delay + cur_term->_delay) == -1)
76 			_setblock(delay);
77 		else {
78 			if (delay < 0)
79 				_setblock(delay);
80 			_settimeout(delay);
81 		}
82 #else	/* SYSV */
83 	cbreak();
84 #endif	/* SYSV */
85 
86 	cur_term->_delay = delay;
87 good:
88 	return (OK);
89 }
90 
91 #ifdef	SYSV
92 /* set the terminal to nodelay (==0) or block(<0) */
93 static	void
_setblock(int block)94 _setblock(int block)
95 {
96 	int	flags = fcntl(cur_term->_inputfd, F_GETFL, 0);
97 
98 	if (block < 0)
99 		flags &= ~O_NDELAY;
100 	else
101 		flags |= O_NDELAY;
102 
103 	(void) fcntl(cur_term->_inputfd, F_SETFL, flags);
104 }
105 
106 /*
107  * set the terminal to timeout in t milliseconds,
108  * rounded up to the nearest 10th of a second.
109  */
110 static	void
_settimeout(int num)111 _settimeout(int num)
112 {
113 	PROGTTYS.c_lflag &= ~ICANON;
114 	if (num > 0) {
115 		PROGTTYS.c_cc[VMIN] = 0;
116 		PROGTTYS.c_cc[VTIME] = (num > 25500) ? 255 : (num + 99) / 100;
117 		cur_term->_fl_rawmode = 3;
118 	} else {
119 		PROGTTYS.c_cc[VMIN] = 1;
120 		PROGTTYS.c_cc[VTIME] = 0;
121 		cur_term->_fl_rawmode = 1;
122 	}
123 	(void) reset_prog_mode();
124 }
125 #endif	/* SYSV */
126