Annotation of src/usr.bin/window/wwiomux.c, Revision 1.5
1.5 ! downsj 1: /* $OpenBSD$ */
1.3 niklas 2: /* $NetBSD: wwiomux.c,v 1.5 1996/02/08 20:45:09 mycroft Exp $ */
1.1 deraadt 3:
4: /*
5: * Copyright (c) 1983, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * This code is derived from software contributed to Berkeley by
9: * Edward Wang at The University of California, Berkeley.
10: *
11: * Redistribution and use in source and binary forms, with or without
12: * modification, are permitted provided that the following conditions
13: * are met:
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. All advertising materials mentioning features or use of this software
20: * must display the following acknowledgement:
21: * This product includes software developed by the University of
22: * California, Berkeley and its contributors.
23: * 4. Neither the name of the University nor the names of its contributors
24: * may be used to endorse or promote products derived from this software
25: * without specific prior written permission.
26: *
27: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37: * SUCH DAMAGE.
38: */
39:
40: #ifndef lint
41: #if 0
42: static char sccsid[] = "@(#)wwiomux.c 8.1 (Berkeley) 6/6/93";
43: #else
1.5 ! downsj 44: static char rcsid[] = "$OpenBSD$";
1.1 deraadt 45: #endif
46: #endif /* not lint */
47:
48: #include "ww.h"
49: #include <sys/time.h>
50: #include <sys/types.h>
51: #if !defined(OLD_TTY) && !defined(TIOCPKT_DATA)
52: #include <sys/ioctl.h>
53: #endif
54: #include <fcntl.h>
55:
56: /*
57: * Multiple window output handler.
58: * The idea is to copy window outputs to the terminal, via the
59: * display package. We try to give wwcurwin highest priority.
60: * The only return conditions are when there is keyboard input
1.2 deraadt 61: * and when a child process dies.
1.1 deraadt 62: * When there's nothing to do, we sleep in a select().
63: * The history of this routine is interesting.
64: */
65: wwiomux()
66: {
67: register struct ww *w;
68: fd_set imask;
69: register n;
70: register char *p;
71: char c;
72: struct timeval tv;
73: char noblock = 0;
74:
75: for (;;) {
76: if (wwinterrupt()) {
77: wwclrintr();
78: return;
79: }
80:
81: FD_ZERO(&imask);
82: n = -1;
83: for (w = wwhead.ww_forw; w != &wwhead; w = w->ww_forw) {
84: if (w->ww_pty < 0)
85: continue;
86: if (w->ww_obq < w->ww_obe) {
87: if (w->ww_pty > n)
1.2 deraadt 88: n = w->ww_pty + 1;
1.1 deraadt 89: FD_SET(w->ww_pty, &imask);
90: }
1.3 niklas 91: if (w->ww_obq > w->ww_obp &&
92: !ISSET(w->ww_pflags, WWP_STOPPED))
1.1 deraadt 93: noblock = 1;
94: }
1.2 deraadt 95: if (wwibq < wwibe) {
96: if (0 > n)
97: n = 0 + 1;
98: FD_SET(0, &imask);
99: }
1.1 deraadt 100:
101: if (!noblock) {
102: if (wwcurwin != 0)
103: wwcurtowin(wwcurwin);
104: wwupdate();
105: wwflush();
106: (void) setjmp(wwjmpbuf);
107: wwsetjmp = 1;
108: if (wwinterrupt()) {
109: wwsetjmp = 0;
110: wwclrintr();
111: return;
112: }
1.2 deraadt 113: /* XXXX */
1.1 deraadt 114: tv.tv_sec = 30;
115: tv.tv_usec = 0;
116: } else {
117: tv.tv_sec = 0;
118: tv.tv_usec = 10000;
119: }
120: wwnselect++;
121: n = select(n + 1, &imask, (fd_set *)0, (fd_set *)0, &tv);
122: wwsetjmp = 0;
123: noblock = 0;
124:
125: if (n < 0)
126: wwnselecte++;
127: else if (n == 0)
128: wwnselectz++;
1.2 deraadt 129: else {
130: if (FD_ISSET(0, &imask))
131: wwrint();
1.1 deraadt 132: for (w = wwhead.ww_forw; w != &wwhead; w = w->ww_forw) {
133: if (w->ww_pty < 0 ||
134: !FD_ISSET(w->ww_pty, &imask))
135: continue;
136: wwnwread++;
137: p = w->ww_obq;
1.2 deraadt 138: if (w->ww_type == WWT_PTY) {
1.1 deraadt 139: if (p == w->ww_ob) {
140: w->ww_obp++;
141: w->ww_obq++;
142: } else
143: p--;
144: c = *p;
145: }
146: n = read(w->ww_pty, p, w->ww_obe - p);
147: if (n < 0) {
148: wwnwreade++;
149: (void) close(w->ww_pty);
150: w->ww_pty = -1;
151: } else if (n == 0) {
152: wwnwreadz++;
153: (void) close(w->ww_pty);
154: w->ww_pty = -1;
1.2 deraadt 155: } else if (w->ww_type != WWT_PTY) {
1.1 deraadt 156: wwnwreadd++;
157: wwnwreadc += n;
158: w->ww_obq += n;
159: } else if (*p == TIOCPKT_DATA) {
160: n--;
161: wwnwreadd++;
162: wwnwreadc += n;
163: w->ww_obq += n;
164: } else {
165: wwnwreadp++;
166: if (*p & TIOCPKT_STOP)
1.3 niklas 167: SET(w->ww_pflags, WWP_STOPPED);
1.1 deraadt 168: if (*p & TIOCPKT_START)
1.3 niklas 169: CLR(w->ww_pflags, WWP_STOPPED);
1.1 deraadt 170: if (*p & TIOCPKT_FLUSHWRITE) {
1.3 niklas 171: CLR(w->ww_pflags, WWP_STOPPED);
1.1 deraadt 172: w->ww_obq = w->ww_obp =
173: w->ww_ob;
174: }
175: }
176: }
1.2 deraadt 177: }
1.1 deraadt 178: /*
179: * Try the current window first, if there is output
180: * then process it and go back to the top to try again.
181: * This can lead to starvation of the other windows,
182: * but presumably that what we want.
183: * Update will eventually happen when output from wwcurwin
184: * dies down.
185: */
186: if ((w = wwcurwin) != 0 && w->ww_pty >= 0 &&
1.3 niklas 187: w->ww_obq > w->ww_obp &&
188: !ISSET(w->ww_pflags, WWP_STOPPED)) {
1.1 deraadt 189: n = wwwrite(w, w->ww_obp, w->ww_obq - w->ww_obp);
190: if ((w->ww_obp += n) == w->ww_obq)
191: w->ww_obq = w->ww_obp = w->ww_ob;
192: noblock = 1;
193: continue;
194: }
195: for (w = wwhead.ww_forw; w != &wwhead; w = w->ww_forw)
196: if (w->ww_pty >= 0 && w->ww_obq > w->ww_obp &&
1.3 niklas 197: !ISSET(w->ww_pflags, WWP_STOPPED)) {
1.1 deraadt 198: n = wwwrite(w, w->ww_obp,
199: w->ww_obq - w->ww_obp);
200: if ((w->ww_obp += n) == w->ww_obq)
201: w->ww_obq = w->ww_obp = w->ww_ob;
202: if (wwinterrupt())
203: break;
204: }
205: }
206: }