Annotation of src/usr.bin/window/wwiomux.c, Revision 1.7
1.7 ! mpech 1: /* $OpenBSD: wwiomux.c,v 1.6 1998/04/26 22:49:01 millert Exp $ */
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.7 ! mpech 44: static char rcsid[] = "$OpenBSD: wwiomux.c,v 1.6 1998/04/26 22:49:01 millert Exp $";
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>
1.6 millert 55: #include <string.h>
1.1 deraadt 56:
57: /*
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
1.2 deraadt 62: * and when a child process dies.
1.1 deraadt 63: * When there's nothing to do, we sleep in a select().
64: * The history of this routine is interesting.
65: */
66: wwiomux()
67: {
1.7 ! mpech 68: struct ww *w;
1.1 deraadt 69: fd_set imask;
1.7 ! mpech 70: int n;
! 71: char *p;
1.1 deraadt 72: char c;
73: struct timeval tv;
74: char noblock = 0;
75:
76: for (;;) {
77: if (wwinterrupt()) {
78: wwclrintr();
79: return;
80: }
81:
82: FD_ZERO(&imask);
83: n = -1;
84: for (w = wwhead.ww_forw; w != &wwhead; w = w->ww_forw) {
85: if (w->ww_pty < 0)
86: continue;
87: if (w->ww_obq < w->ww_obe) {
88: if (w->ww_pty > n)
1.2 deraadt 89: n = w->ww_pty + 1;
1.1 deraadt 90: FD_SET(w->ww_pty, &imask);
91: }
1.3 niklas 92: if (w->ww_obq > w->ww_obp &&
93: !ISSET(w->ww_pflags, WWP_STOPPED))
1.1 deraadt 94: noblock = 1;
95: }
1.2 deraadt 96: if (wwibq < wwibe) {
97: if (0 > n)
98: n = 0 + 1;
99: FD_SET(0, &imask);
100: }
1.1 deraadt 101:
102: if (!noblock) {
103: if (wwcurwin != 0)
104: wwcurtowin(wwcurwin);
105: wwupdate();
106: wwflush();
107: (void) setjmp(wwjmpbuf);
108: wwsetjmp = 1;
109: if (wwinterrupt()) {
110: wwsetjmp = 0;
111: wwclrintr();
112: return;
113: }
1.2 deraadt 114: /* XXXX */
1.1 deraadt 115: tv.tv_sec = 30;
116: tv.tv_usec = 0;
117: } else {
118: tv.tv_sec = 0;
119: tv.tv_usec = 10000;
120: }
121: wwnselect++;
122: n = select(n + 1, &imask, (fd_set *)0, (fd_set *)0, &tv);
123: wwsetjmp = 0;
124: noblock = 0;
125:
126: if (n < 0)
127: wwnselecte++;
128: else if (n == 0)
129: wwnselectz++;
1.2 deraadt 130: else {
131: if (FD_ISSET(0, &imask))
132: wwrint();
1.1 deraadt 133: for (w = wwhead.ww_forw; w != &wwhead; w = w->ww_forw) {
134: if (w->ww_pty < 0 ||
135: !FD_ISSET(w->ww_pty, &imask))
136: continue;
137: wwnwread++;
138: p = w->ww_obq;
1.2 deraadt 139: if (w->ww_type == WWT_PTY) {
1.1 deraadt 140: if (p == w->ww_ob) {
141: w->ww_obp++;
142: w->ww_obq++;
143: } else
144: p--;
145: c = *p;
146: }
147: n = read(w->ww_pty, p, w->ww_obe - p);
148: if (n < 0) {
149: wwnwreade++;
150: (void) close(w->ww_pty);
151: w->ww_pty = -1;
152: } else if (n == 0) {
153: wwnwreadz++;
154: (void) close(w->ww_pty);
155: w->ww_pty = -1;
1.2 deraadt 156: } else if (w->ww_type != WWT_PTY) {
1.1 deraadt 157: wwnwreadd++;
158: wwnwreadc += n;
159: w->ww_obq += n;
160: } else if (*p == TIOCPKT_DATA) {
161: n--;
162: wwnwreadd++;
163: wwnwreadc += n;
164: w->ww_obq += n;
165: } else {
166: wwnwreadp++;
167: if (*p & TIOCPKT_STOP)
1.3 niklas 168: SET(w->ww_pflags, WWP_STOPPED);
1.1 deraadt 169: if (*p & TIOCPKT_START)
1.3 niklas 170: CLR(w->ww_pflags, WWP_STOPPED);
1.1 deraadt 171: if (*p & TIOCPKT_FLUSHWRITE) {
1.3 niklas 172: CLR(w->ww_pflags, WWP_STOPPED);
1.1 deraadt 173: w->ww_obq = w->ww_obp =
174: w->ww_ob;
175: }
176: }
177: }
1.2 deraadt 178: }
1.1 deraadt 179: /*
180: * Try the current window first, if there is output
181: * then process it and go back to the top to try again.
182: * This can lead to starvation of the other windows,
183: * but presumably that what we want.
184: * Update will eventually happen when output from wwcurwin
185: * dies down.
186: */
187: if ((w = wwcurwin) != 0 && w->ww_pty >= 0 &&
1.3 niklas 188: w->ww_obq > w->ww_obp &&
189: !ISSET(w->ww_pflags, WWP_STOPPED)) {
1.1 deraadt 190: n = wwwrite(w, w->ww_obp, w->ww_obq - w->ww_obp);
191: if ((w->ww_obp += n) == w->ww_obq)
192: w->ww_obq = w->ww_obp = w->ww_ob;
193: noblock = 1;
194: continue;
195: }
196: for (w = wwhead.ww_forw; w != &wwhead; w = w->ww_forw)
197: if (w->ww_pty >= 0 && w->ww_obq > w->ww_obp &&
1.3 niklas 198: !ISSET(w->ww_pflags, WWP_STOPPED)) {
1.1 deraadt 199: n = wwwrite(w, w->ww_obp,
200: w->ww_obq - w->ww_obp);
201: if ((w->ww_obp += n) == w->ww_obq)
202: w->ww_obq = w->ww_obp = w->ww_ob;
203: if (wwinterrupt())
204: break;
205: }
206: }
207: }