Commit | Line | Data |
---|---|---|
60de5df9 | 1 | /* |
46e9ea25 KB |
2 | * Copyright (c) 1983 Regents of the University of California. |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms are permitted | |
5e8b0e60 KB |
6 | * provided that the above copyright notice and this paragraph are |
7 | * duplicated in all such forms and that any documentation, | |
8 | * advertising materials, and other materials related to such | |
9 | * distribution and use acknowledge that the software was developed | |
10 | * by the University of California, Berkeley. The name of the | |
11 | * University may not be used to endorse or promote products derived | |
12 | * from this software without specific prior written permission. | |
13 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR | |
14 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED | |
15 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
60de5df9 EW |
16 | */ |
17 | ||
46e9ea25 | 18 | #ifndef lint |
acb7c762 | 19 | static char sccsid[] = "@(#)wwiomux.c 3.22 (Berkeley) %G%"; |
46e9ea25 KB |
20 | #endif /* not lint */ |
21 | ||
abbfd2f2 | 22 | #include "ww.h" |
8fa6d94c | 23 | #include <sys/time.h> |
629d3ed6 | 24 | #include <sys/types.h> |
77d19ca2 | 25 | #include <fcntl.h> |
abbfd2f2 | 26 | |
8fa6d94c | 27 | /* |
b1189050 EW |
28 | * Multiple window output handler. |
29 | * The idea is to copy window outputs to the terminal, via the | |
24735e52 EW |
30 | * display package. We try to give wwcurwin highest priority. |
31 | * The only return conditions are when there is keyboard input | |
32 | * and when a child process dies, which are serviced by signal | |
16078900 | 33 | * catchers (wwrint() and wwchild()). |
b1189050 EW |
34 | * When there's nothing to do, we sleep in a select(). |
35 | * This can be done better with interrupt driven io. But that's | |
36 | * not supported on ptys, yet. | |
37 | * The history of this routine is interesting. | |
8fa6d94c EW |
38 | */ |
39 | wwiomux() | |
abbfd2f2 | 40 | { |
8fa6d94c | 41 | register struct ww *w; |
629d3ed6 | 42 | fd_set imask; |
8fa6d94c | 43 | register n; |
b1189050 | 44 | register char *p; |
8fa6d94c | 45 | char c; |
77d19ca2 | 46 | struct timeval tv; |
0c9b9663 | 47 | char noblock = 0; |
abbfd2f2 | 48 | |
16078900 | 49 | for (;;) { |
b1189050 | 50 | if (wwinterrupt()) { |
16078900 | 51 | wwclrintr(); |
b1189050 EW |
52 | return; |
53 | } | |
b1189050 | 54 | |
16078900 | 55 | FD_ZERO(&imask); |
7ecf4dca | 56 | for (w = wwhead.ww_forw; w != &wwhead; w = w->ww_forw) { |
16078900 | 57 | if (w->ww_pty < 0) |
7ecf4dca | 58 | continue; |
16078900 EW |
59 | if (w->ww_obq < w->ww_obe) |
60 | FD_SET(w->ww_pty, &imask); | |
61 | if (w->ww_obq > w->ww_obp && !w->ww_stopped) | |
62 | noblock = 1; | |
63 | } | |
64 | ||
65 | if (!noblock) { | |
66 | if (wwcurwin != 0) | |
67 | wwcurtowin(wwcurwin); | |
68 | wwupdate(); | |
69 | wwflush(); | |
acb7c762 | 70 | (void) setjmp(wwjmpbuf); |
16078900 EW |
71 | wwsetjmp = 1; |
72 | if (wwinterrupt()) { | |
73 | wwsetjmp = 0; | |
74 | wwclrintr(); | |
75 | return; | |
7ecf4dca | 76 | } |
77d19ca2 EW |
77 | /* |
78 | * Defensive code. If somebody else (for example, | |
79 | * wall) clears the ASYNC flag on us, we will block | |
80 | * forever. So we need a finite timeout and set | |
81 | * the flag again. Anything more clever will probably | |
82 | * need even more system calls. (This is a bug | |
83 | * in the kernel.) | |
84 | * I don't like this one bit. | |
85 | */ | |
acb7c762 | 86 | (void) fcntl(0, F_SETFL, wwnewtty.ww_fflags); |
77d19ca2 | 87 | tv.tv_sec = 30; |
0c9b9663 EW |
88 | tv.tv_usec = 0; |
89 | } else { | |
77d19ca2 | 90 | tv.tv_sec = 0; |
0c9b9663 EW |
91 | tv.tv_usec = 10000; |
92 | } | |
16078900 | 93 | wwnselect++; |
77d19ca2 | 94 | n = select(wwdtablesize, &imask, (fd_set *)0, (fd_set *)0, &tv); |
16078900 | 95 | wwsetjmp = 0; |
0c9b9663 | 96 | noblock = 0; |
16078900 EW |
97 | |
98 | if (n < 0) | |
99 | wwnselecte++; | |
100 | else if (n == 0) | |
101 | wwnselectz++; | |
102 | else | |
103 | for (w = wwhead.ww_forw; w != &wwhead; w = w->ww_forw) { | |
104 | if (w->ww_pty < 0 || | |
105 | !FD_ISSET(w->ww_pty, &imask)) | |
106 | continue; | |
107 | wwnwread++; | |
108 | p = w->ww_obq; | |
109 | if (w->ww_ispty) { | |
110 | if (p == w->ww_ob) { | |
111 | w->ww_obp++; | |
112 | w->ww_obq++; | |
113 | } else | |
114 | p--; | |
115 | c = *p; | |
116 | } | |
117 | n = read(w->ww_pty, p, w->ww_obe - p); | |
118 | if (n < 0) { | |
119 | wwnwreade++; | |
120 | (void) close(w->ww_pty); | |
121 | w->ww_pty = -1; | |
122 | } else if (n == 0) { | |
123 | wwnwreadz++; | |
124 | (void) close(w->ww_pty); | |
125 | w->ww_pty = -1; | |
126 | } else if (!w->ww_ispty) { | |
127 | wwnwreadd++; | |
128 | wwnwreadc += n; | |
129 | w->ww_obq += n; | |
130 | } else if (*p == TIOCPKT_DATA) { | |
131 | n--; | |
132 | wwnwreadd++; | |
133 | wwnwreadc += n; | |
134 | w->ww_obq += n; | |
135 | } else { | |
136 | wwnwreadp++; | |
137 | if (*p & TIOCPKT_STOP) | |
138 | w->ww_stopped = 1; | |
139 | if (*p & TIOCPKT_START) | |
140 | w->ww_stopped = 0; | |
141 | if (*p & TIOCPKT_FLUSHWRITE) { | |
142 | w->ww_stopped = 0; | |
143 | w->ww_obq = w->ww_obp = | |
144 | w->ww_ob; | |
145 | } | |
146 | } | |
147 | if (w->ww_ispty) | |
148 | *p = c; | |
149 | } | |
24735e52 EW |
150 | /* |
151 | * Try the current window first, if there is output | |
152 | * then process it and go back to the top to try again. | |
153 | * This can lead to starvation of the other windows, | |
154 | * but presumably that what we want. | |
155 | * Update will eventually happen when output from wwcurwin | |
156 | * dies down. | |
157 | */ | |
158 | if ((w = wwcurwin) != 0 && w->ww_pty >= 0 && | |
159 | w->ww_obq > w->ww_obp && !w->ww_stopped) { | |
160 | n = wwwrite(w, w->ww_obp, w->ww_obq - w->ww_obp); | |
161 | if ((w->ww_obp += n) == w->ww_obq) | |
162 | w->ww_obq = w->ww_obp = w->ww_ob; | |
0c9b9663 | 163 | noblock = 1; |
24735e52 EW |
164 | continue; |
165 | } | |
16078900 EW |
166 | for (w = wwhead.ww_forw; w != &wwhead; w = w->ww_forw) |
167 | if (w->ww_pty >= 0 && w->ww_obq > w->ww_obp && | |
168 | !w->ww_stopped) { | |
169 | n = wwwrite(w, w->ww_obp, | |
170 | w->ww_obq - w->ww_obp); | |
171 | if ((w->ww_obp += n) == w->ww_obq) | |
7ecf4dca | 172 | w->ww_obq = w->ww_obp = w->ww_ob; |
24735e52 EW |
173 | if (wwinterrupt()) |
174 | break; | |
8fa6d94c | 175 | } |
16078900 | 176 | } |
abbfd2f2 | 177 | } |