1 /* $NetBSD: wwiomux.c,v 1.14 2009/04/14 08:50:06 lukem Exp $ */
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Edward Wang at The University of California, Berkeley.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
38 static char sccsid[] = "@(#)wwiomux.c 8.1 (Berkeley) 6/6/93";
40 __RCSID("$NetBSD: wwiomux.c,v 1.14 2009/04/14 08:50:06 lukem Exp $");
44 #include <sys/types.h>
45 #if !defined(OLD_TTY) && !defined(TIOCPKT_DATA)
46 #include <sys/ioctl.h>
58 * Multiple window output handler.
59 * The idea is to copy window outputs to the terminal, via the
60 * display package. We try to give wwcurwin highest priority.
61 * The only return conditions are when there is keyboard input
62 * and when a child process dies.
63 * When there's nothing to do, we sleep in a select().
64 * The history of this routine is interesting.
72 int volatile dostdin; /* avoid longjmp clobbering */
73 char volatile c; /* avoid longjmp clobbering */
77 static struct pollfd *pfd = NULL;
78 static nfds_t maxfds = 0;
80 c = 0; /* XXXGCC -Wuninitialized */
89 for (w = wwhead.ww_forw; w != &wwhead; w = w->ww_forw) {
90 if (w->ww_pty < 0 || w->ww_obq >= w->ww_obe)
95 if (maxfds <= ++nfd) { /* One more for the fd=0 case below */
96 struct pollfd *npfd = pfd == NULL ?
97 malloc(sizeof(*pfd) * nfd) :
98 realloc(pfd, sizeof(*pfd) * nfd);
112 for (w = wwhead.ww_forw; w != &wwhead; w = w->ww_forw) {
115 if (w->ww_obq < w->ww_obe) {
116 pfd[nfd].fd = w->ww_pty;
117 pfd[nfd++].events = POLLIN;
119 if (w->ww_obq > w->ww_obp &&
120 !ISSET(w->ww_pflags, WWP_STOPPED))
126 pfd[nfd++].events = POLLIN;
133 wwcurtowin(wwcurwin);
136 (void) setjmp(wwjmpbuf);
149 i = poll(pfd, nfd, millis);
158 if (dostdin != -1 && (pfd[dostdin].revents & POLLIN) != 0)
162 for (w = wwhead.ww_forw; w != &wwhead; w = w->ww_forw) {
167 if (w->ww_pty != pfd[nfd].fd)
169 if ((pfd[nfd++].revents & POLLIN) == 0)
173 if (w->ww_type == WWT_PTY) {
181 n = read(w->ww_pty, p, w->ww_obe - p);
184 (void) close(w->ww_pty);
188 (void) close(w->ww_pty);
190 } else if (w->ww_type != WWT_PTY) {
194 } else if (*p == TIOCPKT_DATA) {
201 if (*p & TIOCPKT_STOP)
202 SET(w->ww_pflags, WWP_STOPPED);
203 if (*p & TIOCPKT_START)
204 CLR(w->ww_pflags, WWP_STOPPED);
205 if (*p & TIOCPKT_FLUSHWRITE) {
206 CLR(w->ww_pflags, WWP_STOPPED);
207 w->ww_obq = w->ww_obp =
211 if (w->ww_type == WWT_PTY)
216 * Try the current window first, if there is output
217 * then process it and go back to the top to try again.
218 * This can lead to starvation of the other windows,
219 * but presumably that what we want.
220 * Update will eventually happen when output from wwcurwin
223 if ((w = wwcurwin) != 0 && w->ww_pty >= 0 &&
224 w->ww_obq > w->ww_obp &&
225 !ISSET(w->ww_pflags, WWP_STOPPED)) {
226 int n = wwwrite(w, w->ww_obp, w->ww_obq - w->ww_obp);
227 if ((w->ww_obp += n) == w->ww_obq)
228 w->ww_obq = w->ww_obp = w->ww_ob;
232 for (w = wwhead.ww_forw; w != &wwhead; w = w->ww_forw)
233 if (w->ww_pty >= 0 && w->ww_obq > w->ww_obp &&
234 !ISSET(w->ww_pflags, WWP_STOPPED)) {
235 int n = wwwrite(w, w->ww_obp,
236 w->ww_obq - w->ww_obp);
237 if ((w->ww_obp += n) == w->ww_obq)
238 w->ww_obq = w->ww_obp = w->ww_ob;