]>
Commit | Line | Data |
---|---|---|
44bd5ea7 A |
1 | /* $NetBSD: wwiomux.c,v 1.6 1997/11/21 08:37:30 lukem Exp $ */ |
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 | #include <sys/cdefs.h> | |
40 | #ifndef lint | |
41 | #if 0 | |
42 | static char sccsid[] = "@(#)wwiomux.c 8.1 (Berkeley) 6/6/93"; | |
43 | #else | |
44 | __RCSID("$NetBSD: wwiomux.c,v 1.6 1997/11/21 08:37:30 lukem Exp $"); | |
45 | #endif | |
46 | #endif /* not lint */ | |
47 | ||
48 | #include <sys/types.h> | |
49 | #if !defined(OLD_TTY) && !defined(TIOCPKT_DATA) | |
50 | #include <sys/ioctl.h> | |
51 | #endif | |
52 | #include <sys/time.h> | |
53 | #include <fcntl.h> | |
54 | #include <string.h> | |
55 | #include <unistd.h> | |
56 | #include "ww.h" | |
57 | ||
58 | /* | |
59 | * Multiple window output handler. | |
60 | * The idea is to copy window outputs to the terminal, via the | |
61 | * display package. We try to give wwcurwin highest priority. | |
62 | * The only return conditions are when there is keyboard input | |
63 | * and when a child process dies. | |
64 | * When there's nothing to do, we sleep in a select(). | |
65 | * The history of this routine is interesting. | |
66 | */ | |
67 | void | |
68 | wwiomux() | |
69 | { | |
70 | struct ww *w; | |
71 | fd_set imask; | |
72 | volatile int n; | |
73 | char *p; | |
74 | char c; | |
75 | struct timeval tv; | |
76 | char noblock = 0; | |
77 | ||
78 | for (;;) { | |
79 | if (wwinterrupt()) { | |
80 | wwclrintr(); | |
81 | return; | |
82 | } | |
83 | ||
84 | FD_ZERO(&imask); | |
85 | n = -1; | |
86 | for (w = wwhead.ww_forw; w != &wwhead; w = w->ww_forw) { | |
87 | if (w->ww_pty < 0) | |
88 | continue; | |
89 | if (w->ww_obq < w->ww_obe) { | |
90 | if (w->ww_pty > n) | |
91 | n = w->ww_pty + 1; | |
92 | FD_SET(w->ww_pty, &imask); | |
93 | } | |
94 | if (w->ww_obq > w->ww_obp && | |
95 | !ISSET(w->ww_pflags, WWP_STOPPED)) | |
96 | noblock = 1; | |
97 | } | |
98 | if (wwibq < wwibe) { | |
99 | if (0 > n) | |
100 | n = 0 + 1; | |
101 | FD_SET(0, &imask); | |
102 | } | |
103 | ||
104 | if (!noblock) { | |
105 | if (wwcurwin != 0) | |
106 | wwcurtowin(wwcurwin); | |
107 | wwupdate(); | |
108 | wwflush(); | |
109 | (void) setjmp(wwjmpbuf); | |
110 | wwsetjmp = 1; | |
111 | if (wwinterrupt()) { | |
112 | wwsetjmp = 0; | |
113 | wwclrintr(); | |
114 | return; | |
115 | } | |
116 | /* XXXX */ | |
117 | tv.tv_sec = 30; | |
118 | tv.tv_usec = 0; | |
119 | } else { | |
120 | tv.tv_sec = 0; | |
121 | tv.tv_usec = 10000; | |
122 | } | |
123 | wwnselect++; | |
124 | n = select(n + 1, &imask, (fd_set *)0, (fd_set *)0, &tv); | |
125 | wwsetjmp = 0; | |
126 | noblock = 0; | |
127 | ||
128 | if (n < 0) | |
129 | wwnselecte++; | |
130 | else if (n == 0) | |
131 | wwnselectz++; | |
132 | else { | |
133 | if (FD_ISSET(0, &imask)) | |
134 | wwrint(); | |
135 | for (w = wwhead.ww_forw; w != &wwhead; w = w->ww_forw) { | |
136 | if (w->ww_pty < 0 || | |
137 | !FD_ISSET(w->ww_pty, &imask)) | |
138 | continue; | |
139 | wwnwread++; | |
140 | p = w->ww_obq; | |
141 | if (w->ww_type == WWT_PTY) { | |
142 | if (p == w->ww_ob) { | |
143 | w->ww_obp++; | |
144 | w->ww_obq++; | |
145 | } else | |
146 | p--; | |
147 | c = *p; | |
148 | } | |
149 | n = read(w->ww_pty, p, w->ww_obe - p); | |
150 | if (n < 0) { | |
151 | wwnwreade++; | |
152 | (void) close(w->ww_pty); | |
153 | w->ww_pty = -1; | |
154 | } else if (n == 0) { | |
155 | wwnwreadz++; | |
156 | (void) close(w->ww_pty); | |
157 | w->ww_pty = -1; | |
158 | } else if (w->ww_type != WWT_PTY) { | |
159 | wwnwreadd++; | |
160 | wwnwreadc += n; | |
161 | w->ww_obq += n; | |
162 | } else if (*p == TIOCPKT_DATA) { | |
163 | n--; | |
164 | wwnwreadd++; | |
165 | wwnwreadc += n; | |
166 | w->ww_obq += n; | |
167 | } else { | |
168 | wwnwreadp++; | |
169 | if (*p & TIOCPKT_STOP) | |
170 | SET(w->ww_pflags, WWP_STOPPED); | |
171 | if (*p & TIOCPKT_START) | |
172 | CLR(w->ww_pflags, WWP_STOPPED); | |
173 | if (*p & TIOCPKT_FLUSHWRITE) { | |
174 | CLR(w->ww_pflags, WWP_STOPPED); | |
175 | w->ww_obq = w->ww_obp = | |
176 | w->ww_ob; | |
177 | } | |
178 | } | |
179 | } | |
180 | } | |
181 | /* | |
182 | * Try the current window first, if there is output | |
183 | * then process it and go back to the top to try again. | |
184 | * This can lead to starvation of the other windows, | |
185 | * but presumably that what we want. | |
186 | * Update will eventually happen when output from wwcurwin | |
187 | * dies down. | |
188 | */ | |
189 | if ((w = wwcurwin) != 0 && w->ww_pty >= 0 && | |
190 | w->ww_obq > w->ww_obp && | |
191 | !ISSET(w->ww_pflags, WWP_STOPPED)) { | |
192 | n = wwwrite(w, w->ww_obp, w->ww_obq - w->ww_obp); | |
193 | if ((w->ww_obp += n) == w->ww_obq) | |
194 | w->ww_obq = w->ww_obp = w->ww_ob; | |
195 | noblock = 1; | |
196 | continue; | |
197 | } | |
198 | for (w = wwhead.ww_forw; w != &wwhead; w = w->ww_forw) | |
199 | if (w->ww_pty >= 0 && w->ww_obq > w->ww_obp && | |
200 | !ISSET(w->ww_pflags, WWP_STOPPED)) { | |
201 | n = wwwrite(w, w->ww_obp, | |
202 | w->ww_obq - w->ww_obp); | |
203 | if ((w->ww_obp += n) == w->ww_obq) | |
204 | w->ww_obq = w->ww_obp = w->ww_ob; | |
205 | if (wwinterrupt()) | |
206 | break; | |
207 | } | |
208 | } | |
209 | } |