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