xref: /csrg-svn/usr.bin/window/wwiomux.c (revision 38497)
118744Sedward /*
233514Sbostic  * Copyright (c) 1983 Regents of the University of California.
333514Sbostic  * All rights reserved.
433514Sbostic  *
533514Sbostic  * Redistribution and use in source and binary forms are permitted
634909Sbostic  * provided that the above copyright notice and this paragraph are
734909Sbostic  * duplicated in all such forms and that any documentation,
834909Sbostic  * advertising materials, and other materials related to such
934909Sbostic  * distribution and use acknowledge that the software was developed
1034909Sbostic  * by the University of California, Berkeley.  The name of the
1134909Sbostic  * University may not be used to endorse or promote products derived
1234909Sbostic  * from this software without specific prior written permission.
1334909Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1434909Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1534909Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1618744Sedward  */
1718744Sedward 
1833514Sbostic #ifndef lint
19*38497Sedward static char sccsid[] = "@(#)wwiomux.c	3.22 (Berkeley) 07/28/89";
2033514Sbostic #endif /* not lint */
2133514Sbostic 
2213923Sedward #include "ww.h"
2315872Sedward #include <sys/time.h>
2424958Sedward #include <sys/types.h>
2533751Sedward #include <fcntl.h>
2613923Sedward 
2715872Sedward /*
2816124Sedward  * Multiple window output handler.
2916124Sedward  * The idea is to copy window outputs to the terminal, via the
3035342Sedward  * display package.  We try to give wwcurwin highest priority.
3135342Sedward  * The only return conditions are when there is keyboard input
3235342Sedward  * and when a child process dies, which are serviced by signal
3331443Sedward  * catchers (wwrint() and wwchild()).
3416124Sedward  * When there's nothing to do, we sleep in a select().
3516124Sedward  * This can be done better with interrupt driven io.  But that's
3616124Sedward  * not supported on ptys, yet.
3716124Sedward  * The history of this routine is interesting.
3815872Sedward  */
3915872Sedward wwiomux()
4013923Sedward {
4115872Sedward 	register struct ww *w;
4224958Sedward 	fd_set imask;
4316124Sedward 	register n;
4415872Sedward 	register char *p;
4515872Sedward 	char c;
4633751Sedward 	struct timeval tv;
4736800Sedward 	char noblock = 0;
4813923Sedward 
4931443Sedward 	for (;;) {
5016124Sedward 		if (wwinterrupt()) {
5131443Sedward 			wwclrintr();
5216124Sedward 			return;
5316124Sedward 		}
5416124Sedward 
5531443Sedward 		FD_ZERO(&imask);
5616313Sedward 		for (w = wwhead.ww_forw; w != &wwhead; w = w->ww_forw) {
5731443Sedward 			if (w->ww_pty < 0)
5816313Sedward 				continue;
5931443Sedward 			if (w->ww_obq < w->ww_obe)
6031443Sedward 				FD_SET(w->ww_pty, &imask);
6131443Sedward 			if (w->ww_obq > w->ww_obp && !w->ww_stopped)
6231443Sedward 				noblock = 1;
6331443Sedward 		}
6431443Sedward 
6531443Sedward 		if (!noblock) {
6631443Sedward 			if (wwcurwin != 0)
6731443Sedward 				wwcurtowin(wwcurwin);
6831443Sedward 			wwupdate();
6931443Sedward 			wwflush();
70*38497Sedward 			(void) setjmp(wwjmpbuf);
7131443Sedward 			wwsetjmp = 1;
7231443Sedward 			if (wwinterrupt()) {
7331443Sedward 				wwsetjmp = 0;
7431443Sedward 				wwclrintr();
7531443Sedward 				return;
7616313Sedward 			}
7733751Sedward 			/*
7833751Sedward 			 * Defensive code.  If somebody else (for example,
7933751Sedward 			 * wall) clears the ASYNC flag on us, we will block
8033751Sedward 			 * forever.  So we need a finite timeout and set
8133751Sedward 			 * the flag again.  Anything more clever will probably
8233751Sedward 			 * need even more system calls.  (This is a bug
8333751Sedward 			 * in the kernel.)
8433751Sedward 			 * I don't like this one bit.
8533751Sedward 			 */
86*38497Sedward 			(void) fcntl(0, F_SETFL, wwnewtty.ww_fflags);
8733751Sedward 			tv.tv_sec = 30;
8836800Sedward 			tv.tv_usec = 0;
8936800Sedward 		} else {
9033751Sedward 			tv.tv_sec = 0;
9136800Sedward 			tv.tv_usec = 10000;
9236800Sedward 		}
9331443Sedward 		wwnselect++;
9433751Sedward 		n = select(wwdtablesize, &imask, (fd_set *)0, (fd_set *)0, &tv);
9531443Sedward 		wwsetjmp = 0;
9636800Sedward 		noblock = 0;
9731443Sedward 
9831443Sedward 		if (n < 0)
9931443Sedward 			wwnselecte++;
10031443Sedward 		else if (n == 0)
10131443Sedward 			wwnselectz++;
10231443Sedward 		else
10331443Sedward 			for (w = wwhead.ww_forw; w != &wwhead; w = w->ww_forw) {
10431443Sedward 				if (w->ww_pty < 0 ||
10531443Sedward 				    !FD_ISSET(w->ww_pty, &imask))
10631443Sedward 					continue;
10731443Sedward 				wwnwread++;
10831443Sedward 				p = w->ww_obq;
10931443Sedward 				if (w->ww_ispty) {
11031443Sedward 					if (p == w->ww_ob) {
11131443Sedward 						w->ww_obp++;
11231443Sedward 						w->ww_obq++;
11331443Sedward 					} else
11431443Sedward 						p--;
11531443Sedward 					c = *p;
11631443Sedward 				}
11731443Sedward 				n = read(w->ww_pty, p, w->ww_obe - p);
11831443Sedward 				if (n < 0) {
11931443Sedward 					wwnwreade++;
12031443Sedward 					(void) close(w->ww_pty);
12131443Sedward 					w->ww_pty = -1;
12231443Sedward 				} else if (n == 0) {
12331443Sedward 					wwnwreadz++;
12431443Sedward 					(void) close(w->ww_pty);
12531443Sedward 					w->ww_pty = -1;
12631443Sedward 				} else if (!w->ww_ispty) {
12731443Sedward 					wwnwreadd++;
12831443Sedward 					wwnwreadc += n;
12931443Sedward 					w->ww_obq += n;
13031443Sedward 				} else if (*p == TIOCPKT_DATA) {
13131443Sedward 					n--;
13231443Sedward 					wwnwreadd++;
13331443Sedward 					wwnwreadc += n;
13431443Sedward 					w->ww_obq += n;
13531443Sedward 				} else {
13631443Sedward 					wwnwreadp++;
13731443Sedward 					if (*p & TIOCPKT_STOP)
13831443Sedward 						w->ww_stopped = 1;
13931443Sedward 					if (*p & TIOCPKT_START)
14031443Sedward 						w->ww_stopped = 0;
14131443Sedward 					if (*p & TIOCPKT_FLUSHWRITE) {
14231443Sedward 						w->ww_stopped = 0;
14331443Sedward 						w->ww_obq = w->ww_obp =
14431443Sedward 							w->ww_ob;
14531443Sedward 					}
14631443Sedward 				}
14731443Sedward 				if (w->ww_ispty)
14831443Sedward 					*p = c;
14931443Sedward 			}
15035342Sedward 		/*
15135342Sedward 		 * Try the current window first, if there is output
15235342Sedward 		 * then process it and go back to the top to try again.
15335342Sedward 		 * This can lead to starvation of the other windows,
15435342Sedward 		 * but presumably that what we want.
15535342Sedward 		 * Update will eventually happen when output from wwcurwin
15635342Sedward 		 * dies down.
15735342Sedward 		 */
15835342Sedward 		if ((w = wwcurwin) != 0 && w->ww_pty >= 0 &&
15935342Sedward 		    w->ww_obq > w->ww_obp && !w->ww_stopped) {
16035342Sedward 			n = wwwrite(w, w->ww_obp, w->ww_obq - w->ww_obp);
16135342Sedward 			if ((w->ww_obp += n) == w->ww_obq)
16235342Sedward 				w->ww_obq = w->ww_obp = w->ww_ob;
16336800Sedward 			noblock = 1;
16435342Sedward 			continue;
16535342Sedward 		}
16631443Sedward 		for (w = wwhead.ww_forw; w != &wwhead; w = w->ww_forw)
16731443Sedward 			if (w->ww_pty >= 0 && w->ww_obq > w->ww_obp &&
16831443Sedward 			    !w->ww_stopped) {
16931443Sedward 				n = wwwrite(w, w->ww_obp,
17031443Sedward 					w->ww_obq - w->ww_obp);
17131443Sedward 				if ((w->ww_obp += n) == w->ww_obq)
17216313Sedward 					w->ww_obq = w->ww_obp = w->ww_ob;
17335342Sedward 				if (wwinterrupt())
17435342Sedward 					break;
17516313Sedward 			}
17631443Sedward 	}
17713923Sedward }
178