xref: /illumos-gate/usr/src/lib/libcurses/screen/draino.c (revision 22028508fd28d36ff74dc02c5774a8ba1f0db045)
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  * Code for various kinds of delays.  Most of this is nonportable and
50  * requires various enhancements to the operating system, so it won't
51  * work on all systems.  It is included in curses to provide a portable
52  * interface, and so curses itself can use it for function keys.
53  */
54 
55 #define	NAPINTERVAL	100
56 /*
57  * Wait until the output has drained enough that it will only take
58  * ms more milliseconds to drain completely.
59  * Needs Berkeley TIOCOUTQ ioctl.  Returns ERR if impossible.
60  */
61 
62 int
63 draino(int ms)
64 {
65 #ifdef	TIOCOUTQ
66 #define	_DRAINO
67 	/* number of chars = that many ms */
68 	long	ncneeded;
69 
70 	/* 10 bits/char, 1000 ms/sec, baudrate in bits/sec */
71 	ncneeded = SP->baud * ms / (10 * 1000);
72 	/*CONSTCOND*/
73 	while (TRUE) {
74 		int	rv;		/* ioctl return value */
75 		int	ncthere = 0;	/* number of chars actually in */
76 					/* output queue */
77 
78 		rv = ioctl(cur_term->Filedes, TIOCOUTQ, &ncthere);
79 #ifdef	DEBUG
80 		if (outf)
81 			fprintf(outf, "draino: rv %d, ncneeded %d, "
82 			    "ncthere %d\n", rv, ncneeded, ncthere);
83 #endif	/* DEBUG */
84 		if (rv < 0)
85 			return (ERR);	/* ioctl didn't work */
86 		if (ncthere <= ncneeded)
87 			return (OK);
88 		(void) napms(NAPINTERVAL);
89 	}
90 #else	/* TIOCOUTQ */
91 
92 #ifdef	TCSETAW
93 #define	_DRAINO
94 	/*
95 	 * SYSV simulation - waits until the entire queue is empty,
96 	 * then sets the state to what it already is (e.g. no-op).
97 	 * Unfortunately this only works if ms is zero.
98 	 */
99 	if (ms <= 0) {
100 #ifdef SYSV
101 		if (prog_istermios < 0) {
102 			int i;
103 
104 			PROGTTY.c_lflag = PROGTTYS.c_lflag;
105 			PROGTTY.c_oflag = PROGTTYS.c_oflag;
106 			PROGTTY.c_iflag = PROGTTYS.c_iflag;
107 			PROGTTY.c_cflag = PROGTTYS.c_cflag;
108 			for (i = 0; i < NCC; i++)
109 				PROGTTY.c_cc[i] = PROGTTYS.c_cc[i];
110 			(void) ioctl(cur_term->Filedes, TCSETAW, &PROGTTY);
111 		} else
112 			(void) ioctl(cur_term->Filedes, TCSETSW, &PROGTTYS);
113 #else	/* SYSV */
114 			(void) ioctl(cur_term->Filedes, TCSETAW, &PROGTTY);
115 #endif	/* SYSV */
116 		return (OK);
117 	} else
118 		return (ERR);
119 #endif	/* TCSETAW */
120 #endif	/* TIOCOUTQ */
121 
122 #ifndef	_DRAINO
123 	/*
124 	 * No way to fake it, so we return failure.
125 	 * Used #else to avoid warning from compiler about unreached stmt
126 	 */
127 	return (ERR);
128 #endif	/* _DRAINO */
129 /*NOTREACHED*/
130 }
131