[BACK]Return to wwiomux.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / window

Annotation of src/usr.bin/window/wwiomux.c, Revision 1.1.1.1

1.1       deraadt     1: /*     $NetBSD: wwiomux.c,v 1.3 1995/09/28 10:35:37 tls 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: #ifndef lint
                     40: #if 0
                     41: static char sccsid[] = "@(#)wwiomux.c  8.1 (Berkeley) 6/6/93";
                     42: #else
                     43: static char rcsid[] = "$NetBSD: wwiomux.c,v 1.3 1995/09/28 10:35:37 tls Exp $";
                     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
                     60:  * and when a child process dies, which are serviced by signal
                     61:  * catchers (wwrint() and wwchild()).
                     62:  * When there's nothing to do, we sleep in a select().
                     63:  * This can be done better with interrupt driven io.  But that's
                     64:  * not supported on ptys, yet.
                     65:  * The history of this routine is interesting.
                     66:  */
                     67: wwiomux()
                     68: {
                     69:        register struct ww *w;
                     70:        fd_set imask;
                     71:        register n;
                     72:        register char *p;
                     73:        char c;
                     74:        struct timeval tv;
                     75:        char noblock = 0;
                     76:
                     77:        for (;;) {
                     78:                if (wwinterrupt()) {
                     79:                        wwclrintr();
                     80:                        return;
                     81:                }
                     82:
                     83:                FD_ZERO(&imask);
                     84:                n = -1;
                     85:                for (w = wwhead.ww_forw; w != &wwhead; w = w->ww_forw) {
                     86:                        if (w->ww_pty < 0)
                     87:                                continue;
                     88:                        if (w->ww_obq < w->ww_obe) {
                     89:                                if (w->ww_pty > n)
                     90:                                        n = w->ww_pty;
                     91:                                FD_SET(w->ww_pty, &imask);
                     92:                        }
                     93:                        if (w->ww_obq > w->ww_obp && !w->ww_stopped)
                     94:                                noblock = 1;
                     95:                }
                     96:
                     97:                if (!noblock) {
                     98:                        if (wwcurwin != 0)
                     99:                                wwcurtowin(wwcurwin);
                    100:                        wwupdate();
                    101:                        wwflush();
                    102:                        (void) setjmp(wwjmpbuf);
                    103:                        wwsetjmp = 1;
                    104:                        if (wwinterrupt()) {
                    105:                                wwsetjmp = 0;
                    106:                                wwclrintr();
                    107:                                return;
                    108:                        }
                    109:                        /*
                    110:                         * Defensive code.  If somebody else (for example,
                    111:                         * wall) clears the ASYNC flag on us, we will block
                    112:                         * forever.  So we need a finite timeout and set
                    113:                         * the flag again.  Anything more clever will probably
                    114:                         * need even more system calls.  (This is a bug
                    115:                         * in the kernel.)
                    116:                         * I don't like this one bit.
                    117:                         */
                    118:                        (void) fcntl(0, F_SETFL, wwnewtty.ww_fflags);
                    119:                        tv.tv_sec = 30;
                    120:                        tv.tv_usec = 0;
                    121:                } else {
                    122:                        tv.tv_sec = 0;
                    123:                        tv.tv_usec = 10000;
                    124:                }
                    125:                wwnselect++;
                    126:                n = select(n + 1, &imask, (fd_set *)0, (fd_set *)0, &tv);
                    127:                wwsetjmp = 0;
                    128:                noblock = 0;
                    129:
                    130:                if (n < 0)
                    131:                        wwnselecte++;
                    132:                else if (n == 0)
                    133:                        wwnselectz++;
                    134:                else
                    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_ispty) {
                    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_ispty) {
                    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:                                                w->ww_stopped = 1;
                    171:                                        if (*p & TIOCPKT_START)
                    172:                                                w->ww_stopped = 0;
                    173:                                        if (*p & TIOCPKT_FLUSHWRITE) {
                    174:                                                w->ww_stopped = 0;
                    175:                                                w->ww_obq = w->ww_obp =
                    176:                                                        w->ww_ob;
                    177:                                        }
                    178:                                }
                    179:                                if (w->ww_ispty)
                    180:                                        *p = c;
                    181:                        }
                    182:                /*
                    183:                 * Try the current window first, if there is output
                    184:                 * then process it and go back to the top to try again.
                    185:                 * This can lead to starvation of the other windows,
                    186:                 * but presumably that what we want.
                    187:                 * Update will eventually happen when output from wwcurwin
                    188:                 * dies down.
                    189:                 */
                    190:                if ((w = wwcurwin) != 0 && w->ww_pty >= 0 &&
                    191:                    w->ww_obq > w->ww_obp && !w->ww_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:                            !w->ww_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: }