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