2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Edward Wang at The University of California, Berkeley.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 static char sccsid[] = "@(#)wwiomux.c 8.1 (Berkeley) 6/6/93";
43 #include <sys/types.h>
44 #if !defined(OLD_TTY) && !defined(TIOCPKT_DATA)
45 #include <sys/ioctl.h>
50 * Multiple window output handler.
51 * The idea is to copy window outputs to the terminal, via the
52 * display package. We try to give wwcurwin highest priority.
53 * The only return conditions are when there is keyboard input
54 * and when a child process dies, which are serviced by signal
55 * catchers (wwrint() and wwchild()).
56 * When there's nothing to do, we sleep in a select().
57 * This can be done better with interrupt driven io. But that's
58 * not supported on ptys, yet.
59 * The history of this routine is interesting.
63 register struct ww *w;
79 for (w = wwhead.ww_forw; w != &wwhead; w = w->ww_forw) {
82 if (w->ww_obq < w->ww_obe) {
85 FD_SET(w->ww_pty, &imask);
87 if (w->ww_obq > w->ww_obp && !w->ww_stopped)
96 (void) setjmp(wwjmpbuf);
104 * Defensive code. If somebody else (for example,
105 * wall) clears the ASYNC flag on us, we will block
106 * forever. So we need a finite timeout and set
107 * the flag again. Anything more clever will probably
108 * need even more system calls. (This is a bug
110 * I don't like this one bit.
112 (void) fcntl(0, F_SETFL, wwnewtty.ww_fflags);
120 n = select(n + 1, &imask, (fd_set *)0, (fd_set *)0, &tv);
129 for (w = wwhead.ww_forw; w != &wwhead; w = w->ww_forw) {
131 !FD_ISSET(w->ww_pty, &imask))
143 n = read(w->ww_pty, p, w->ww_obe - p);
146 (void) close(w->ww_pty);
150 (void) close(w->ww_pty);
152 } else if (!w->ww_ispty) {
156 } else if (*p == TIOCPKT_DATA) {
163 if (*p & TIOCPKT_STOP)
165 if (*p & TIOCPKT_START)
167 if (*p & TIOCPKT_FLUSHWRITE) {
169 w->ww_obq = w->ww_obp =
177 * Try the current window first, if there is output
178 * then process it and go back to the top to try again.
179 * This can lead to starvation of the other windows,
180 * but presumably that what we want.
181 * Update will eventually happen when output from wwcurwin
184 if ((w = wwcurwin) != 0 && w->ww_pty >= 0 &&
185 w->ww_obq > w->ww_obp && !w->ww_stopped) {
186 n = wwwrite(w, w->ww_obp, w->ww_obq - w->ww_obp);
187 if ((w->ww_obp += n) == w->ww_obq)
188 w->ww_obq = w->ww_obp = w->ww_ob;
192 for (w = wwhead.ww_forw; w != &wwhead; w = w->ww_forw)
193 if (w->ww_pty >= 0 && w->ww_obq > w->ww_obp &&
195 n = wwwrite(w, w->ww_obp,
196 w->ww_obq - w->ww_obp);
197 if ((w->ww_obp += n) == w->ww_obq)
198 w->ww_obq = w->ww_obp = w->ww_ob;