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
36 * @(#)wwiomux.c 8.1 (Berkeley) 6/6/93
37 * $FreeBSD: src/usr.bin/window/wwiomux.c,v 1.1.1.1.14.1 2001/05/17 09:45:01 obrien Exp $
38 * $DragonFly: src/usr.bin/window/wwiomux.c,v 1.3 2008/05/19 10:19:49 corecode Exp $
43 #include <sys/types.h>
44 #include <sys/select.h>
45 #if !defined(OLD_TTY) && !defined(TIOCPKT_DATA)
46 #include <sys/ioctl.h>
51 * Multiple window output handler.
52 * The idea is to copy window outputs to the terminal, via the
53 * display package. We try to give wwcurwin highest priority.
54 * The only return conditions are when there is keyboard input
55 * and when a child process dies, which are serviced by signal
56 * catchers (wwrint() and wwchild()).
57 * When there's nothing to do, we sleep in a select().
58 * This can be done better with interrupt driven io. But that's
59 * not supported on ptys, yet.
60 * The history of this routine is interesting.
64 register struct ww *w;
80 for (w = wwhead.ww_forw; w != &wwhead; w = w->ww_forw) {
83 if (w->ww_obq < w->ww_obe) {
86 FD_SET(w->ww_pty, &imask);
88 if (w->ww_obq > w->ww_obp && !w->ww_stopped)
97 (void) setjmp(wwjmpbuf);
105 * Defensive code. If somebody else (for example,
106 * wall) clears the ASYNC flag on us, we will block
107 * forever. So we need a finite timeout and set
108 * the flag again. Anything more clever will probably
109 * need even more system calls. (This is a bug
111 * I don't like this one bit.
113 (void) fcntl(0, F_SETFL, wwnewtty.ww_fflags);
121 n = select(n + 1, &imask, (fd_set *)0, (fd_set *)0, &tv);
130 for (w = wwhead.ww_forw; w != &wwhead; w = w->ww_forw) {
132 !FD_ISSET(w->ww_pty, &imask))
144 n = read(w->ww_pty, p, w->ww_obe - p);
147 (void) close(w->ww_pty);
151 (void) close(w->ww_pty);
153 } else if (!w->ww_ispty) {
157 } else if (*p == TIOCPKT_DATA) {
164 if (*p & TIOCPKT_STOP)
166 if (*p & TIOCPKT_START)
168 if (*p & TIOCPKT_FLUSHWRITE) {
170 w->ww_obq = w->ww_obp =
178 * Try the current window first, if there is output
179 * then process it and go back to the top to try again.
180 * This can lead to starvation of the other windows,
181 * but presumably that what we want.
182 * Update will eventually happen when output from wwcurwin
185 if ((w = wwcurwin) != 0 && w->ww_pty >= 0 &&
186 w->ww_obq > w->ww_obp && !w->ww_stopped) {
187 n = wwwrite(w, w->ww_obp, w->ww_obq - w->ww_obp);
188 if ((w->ww_obp += n) == w->ww_obq)
189 w->ww_obq = w->ww_obp = w->ww_ob;
193 for (w = wwhead.ww_forw; w != &wwhead; w = w->ww_forw)
194 if (w->ww_pty >= 0 && w->ww_obq > w->ww_obp &&
196 n = wwwrite(w, w->ww_obp,
197 w->ww_obq - w->ww_obp);
198 if ((w->ww_obp += n) == w->ww_obq)
199 w->ww_obq = w->ww_obp = w->ww_ob;