1 /* @(#)wwiomux.c 8.1 (Berkeley) 6/6/93 */
2 /* $NetBSD: wwiomux.c,v 1.14 2009/04/14 08:50:06 lukem Exp $ */
5 * Copyright (c) 1983, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * Edward Wang at The University of California, Berkeley.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/types.h>
37 #if !defined(OLD_TTY) && !defined(TIOCPKT_DATA)
38 #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.
55 * When there's nothing to do, we sleep in a select().
56 * The history of this routine is interesting.
64 int volatile dostdin; /* avoid longjmp clobbering */
65 char volatile c; /* avoid longjmp clobbering */
69 static struct pollfd *pfd = NULL;
70 static nfds_t maxfds = 0;
72 c = 0; /* XXXGCC -Wuninitialized */
81 for (w = wwhead.ww_forw; w != &wwhead; w = w->ww_forw) {
82 if (w->ww_pty < 0 || w->ww_obq >= w->ww_obe)
87 if (maxfds <= ++nfd) { /* One more for the fd=0 case below */
88 struct pollfd *npfd = pfd == NULL ?
89 malloc(sizeof(*pfd) * nfd) :
90 realloc(pfd, sizeof(*pfd) * nfd);
104 for (w = wwhead.ww_forw; w != &wwhead; w = w->ww_forw) {
107 if (w->ww_obq < w->ww_obe) {
108 pfd[nfd].fd = w->ww_pty;
109 pfd[nfd++].events = POLLIN;
111 if (w->ww_obq > w->ww_obp &&
112 !ISSET(w->ww_pflags, WWP_STOPPED))
118 pfd[nfd++].events = POLLIN;
125 wwcurtowin(wwcurwin);
128 (void) setjmp(wwjmpbuf);
141 i = poll(pfd, nfd, millis);
150 if (dostdin != -1 && (pfd[dostdin].revents & POLLIN) != 0)
154 for (w = wwhead.ww_forw; w != &wwhead; w = w->ww_forw) {
159 if (w->ww_pty != pfd[nfd].fd)
161 if ((pfd[nfd++].revents & POLLIN) == 0)
165 if (w->ww_type == WWT_PTY) {
173 n = read(w->ww_pty, p, w->ww_obe - p);
176 (void) close(w->ww_pty);
180 (void) close(w->ww_pty);
182 } else if (w->ww_type != WWT_PTY) {
186 } else if (*p == TIOCPKT_DATA) {
193 if (*p & TIOCPKT_STOP)
194 SET(w->ww_pflags, WWP_STOPPED);
195 if (*p & TIOCPKT_START)
196 CLR(w->ww_pflags, WWP_STOPPED);
197 if (*p & TIOCPKT_FLUSHWRITE) {
198 CLR(w->ww_pflags, WWP_STOPPED);
199 w->ww_obq = w->ww_obp =
203 if (w->ww_type == WWT_PTY)
208 * Try the current window first, if there is output
209 * then process it and go back to the top to try again.
210 * This can lead to starvation of the other windows,
211 * but presumably that what we want.
212 * Update will eventually happen when output from wwcurwin
215 if ((w = wwcurwin) != NULL && w->ww_pty >= 0 &&
216 w->ww_obq > w->ww_obp &&
217 !ISSET(w->ww_pflags, WWP_STOPPED)) {
218 int n = wwwrite(w, w->ww_obp, w->ww_obq - w->ww_obp);
219 if ((w->ww_obp += n) == w->ww_obq)
220 w->ww_obq = w->ww_obp = w->ww_ob;
224 for (w = wwhead.ww_forw; w != &wwhead; w = w->ww_forw)
225 if (w->ww_pty >= 0 && w->ww_obq > w->ww_obp &&
226 !ISSET(w->ww_pflags, WWP_STOPPED)) {
227 int n = wwwrite(w, w->ww_obp,
228 w->ww_obq - w->ww_obp);
229 if ((w->ww_obp += n) == w->ww_obq)
230 w->ww_obq = w->ww_obp = w->ww_ob;