Annotation of src/usr.bin/window/wwiomux.c, Revision 1.8
1.8 ! millert 1: /* $OpenBSD: wwiomux.c,v 1.7 2001/11/19 19:02:18 mpech 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.
1.8 ! millert 19: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 20: * may be used to endorse or promote products derived from this software
21: * without specific prior written permission.
22: *
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
33: * SUCH DAMAGE.
34: */
35:
36: #ifndef lint
37: #if 0
38: static char sccsid[] = "@(#)wwiomux.c 8.1 (Berkeley) 6/6/93";
39: #else
1.8 ! millert 40: static char rcsid[] = "$OpenBSD: wwiomux.c,v 1.7 2001/11/19 19:02:18 mpech Exp $";
1.1 deraadt 41: #endif
42: #endif /* not lint */
43:
44: #include "ww.h"
45: #include <sys/time.h>
46: #include <sys/types.h>
47: #if !defined(OLD_TTY) && !defined(TIOCPKT_DATA)
48: #include <sys/ioctl.h>
49: #endif
50: #include <fcntl.h>
1.6 millert 51: #include <string.h>
1.1 deraadt 52:
53: /*
54: * Multiple window output handler.
55: * The idea is to copy window outputs to the terminal, via the
56: * display package. We try to give wwcurwin highest priority.
57: * The only return conditions are when there is keyboard input
1.2 deraadt 58: * and when a child process dies.
1.1 deraadt 59: * When there's nothing to do, we sleep in a select().
60: * The history of this routine is interesting.
61: */
62: wwiomux()
63: {
1.7 mpech 64: struct ww *w;
1.1 deraadt 65: fd_set imask;
1.7 mpech 66: int n;
67: char *p;
1.1 deraadt 68: char c;
69: struct timeval tv;
70: char noblock = 0;
71:
72: for (;;) {
73: if (wwinterrupt()) {
74: wwclrintr();
75: return;
76: }
77:
78: FD_ZERO(&imask);
79: n = -1;
80: for (w = wwhead.ww_forw; w != &wwhead; w = w->ww_forw) {
81: if (w->ww_pty < 0)
82: continue;
83: if (w->ww_obq < w->ww_obe) {
84: if (w->ww_pty > n)
1.2 deraadt 85: n = w->ww_pty + 1;
1.1 deraadt 86: FD_SET(w->ww_pty, &imask);
87: }
1.3 niklas 88: if (w->ww_obq > w->ww_obp &&
89: !ISSET(w->ww_pflags, WWP_STOPPED))
1.1 deraadt 90: noblock = 1;
91: }
1.2 deraadt 92: if (wwibq < wwibe) {
93: if (0 > n)
94: n = 0 + 1;
95: FD_SET(0, &imask);
96: }
1.1 deraadt 97:
98: if (!noblock) {
99: if (wwcurwin != 0)
100: wwcurtowin(wwcurwin);
101: wwupdate();
102: wwflush();
103: (void) setjmp(wwjmpbuf);
104: wwsetjmp = 1;
105: if (wwinterrupt()) {
106: wwsetjmp = 0;
107: wwclrintr();
108: return;
109: }
1.2 deraadt 110: /* XXXX */
1.1 deraadt 111: tv.tv_sec = 30;
112: tv.tv_usec = 0;
113: } else {
114: tv.tv_sec = 0;
115: tv.tv_usec = 10000;
116: }
117: wwnselect++;
118: n = select(n + 1, &imask, (fd_set *)0, (fd_set *)0, &tv);
119: wwsetjmp = 0;
120: noblock = 0;
121:
122: if (n < 0)
123: wwnselecte++;
124: else if (n == 0)
125: wwnselectz++;
1.2 deraadt 126: else {
127: if (FD_ISSET(0, &imask))
128: wwrint();
1.1 deraadt 129: for (w = wwhead.ww_forw; w != &wwhead; w = w->ww_forw) {
130: if (w->ww_pty < 0 ||
131: !FD_ISSET(w->ww_pty, &imask))
132: continue;
133: wwnwread++;
134: p = w->ww_obq;
1.2 deraadt 135: if (w->ww_type == WWT_PTY) {
1.1 deraadt 136: if (p == w->ww_ob) {
137: w->ww_obp++;
138: w->ww_obq++;
139: } else
140: p--;
141: c = *p;
142: }
143: n = read(w->ww_pty, p, w->ww_obe - p);
144: if (n < 0) {
145: wwnwreade++;
146: (void) close(w->ww_pty);
147: w->ww_pty = -1;
148: } else if (n == 0) {
149: wwnwreadz++;
150: (void) close(w->ww_pty);
151: w->ww_pty = -1;
1.2 deraadt 152: } else if (w->ww_type != WWT_PTY) {
1.1 deraadt 153: wwnwreadd++;
154: wwnwreadc += n;
155: w->ww_obq += n;
156: } else if (*p == TIOCPKT_DATA) {
157: n--;
158: wwnwreadd++;
159: wwnwreadc += n;
160: w->ww_obq += n;
161: } else {
162: wwnwreadp++;
163: if (*p & TIOCPKT_STOP)
1.3 niklas 164: SET(w->ww_pflags, WWP_STOPPED);
1.1 deraadt 165: if (*p & TIOCPKT_START)
1.3 niklas 166: CLR(w->ww_pflags, WWP_STOPPED);
1.1 deraadt 167: if (*p & TIOCPKT_FLUSHWRITE) {
1.3 niklas 168: CLR(w->ww_pflags, WWP_STOPPED);
1.1 deraadt 169: w->ww_obq = w->ww_obp =
170: w->ww_ob;
171: }
172: }
173: }
1.2 deraadt 174: }
1.1 deraadt 175: /*
176: * Try the current window first, if there is output
177: * then process it and go back to the top to try again.
178: * This can lead to starvation of the other windows,
179: * but presumably that what we want.
180: * Update will eventually happen when output from wwcurwin
181: * dies down.
182: */
183: if ((w = wwcurwin) != 0 && w->ww_pty >= 0 &&
1.3 niklas 184: w->ww_obq > w->ww_obp &&
185: !ISSET(w->ww_pflags, WWP_STOPPED)) {
1.1 deraadt 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 &&
1.3 niklas 194: !ISSET(w->ww_pflags, WWP_STOPPED)) {
1.1 deraadt 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: }