[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.3

1.3     ! niklas      1: /*     $NetBSD: wwiomux.c,v 1.5 1996/02/08 20:45:09 mycroft Exp $      */
1.1       deraadt     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
1.3     ! niklas     43: static char rcsid[] = "$NetBSD: wwiomux.c,v 1.5 1996/02/08 20:45:09 mycroft Exp $";
1.1       deraadt    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
1.2       deraadt    60:  * and when a child process dies.
1.1       deraadt    61:  * When there's nothing to do, we sleep in a select().
                     62:  * The history of this routine is interesting.
                     63:  */
                     64: wwiomux()
                     65: {
                     66:        register struct ww *w;
                     67:        fd_set imask;
                     68:        register n;
                     69:        register char *p;
                     70:        char c;
                     71:        struct timeval tv;
                     72:        char noblock = 0;
                     73:
                     74:        for (;;) {
                     75:                if (wwinterrupt()) {
                     76:                        wwclrintr();
                     77:                        return;
                     78:                }
                     79:
                     80:                FD_ZERO(&imask);
                     81:                n = -1;
                     82:                for (w = wwhead.ww_forw; w != &wwhead; w = w->ww_forw) {
                     83:                        if (w->ww_pty < 0)
                     84:                                continue;
                     85:                        if (w->ww_obq < w->ww_obe) {
                     86:                                if (w->ww_pty > n)
1.2       deraadt    87:                                        n = w->ww_pty + 1;
1.1       deraadt    88:                                FD_SET(w->ww_pty, &imask);
                     89:                        }
1.3     ! niklas     90:                        if (w->ww_obq > w->ww_obp &&
        !            91:                            !ISSET(w->ww_pflags, WWP_STOPPED))
1.1       deraadt    92:                                noblock = 1;
                     93:                }
1.2       deraadt    94:                if (wwibq < wwibe) {
                     95:                        if (0 > n)
                     96:                                n = 0 + 1;
                     97:                        FD_SET(0, &imask);
                     98:                }
1.1       deraadt    99:
                    100:                if (!noblock) {
                    101:                        if (wwcurwin != 0)
                    102:                                wwcurtowin(wwcurwin);
                    103:                        wwupdate();
                    104:                        wwflush();
                    105:                        (void) setjmp(wwjmpbuf);
                    106:                        wwsetjmp = 1;
                    107:                        if (wwinterrupt()) {
                    108:                                wwsetjmp = 0;
                    109:                                wwclrintr();
                    110:                                return;
                    111:                        }
1.2       deraadt   112:                        /* XXXX */
1.1       deraadt   113:                        tv.tv_sec = 30;
                    114:                        tv.tv_usec = 0;
                    115:                } else {
                    116:                        tv.tv_sec = 0;
                    117:                        tv.tv_usec = 10000;
                    118:                }
                    119:                wwnselect++;
                    120:                n = select(n + 1, &imask, (fd_set *)0, (fd_set *)0, &tv);
                    121:                wwsetjmp = 0;
                    122:                noblock = 0;
                    123:
                    124:                if (n < 0)
                    125:                        wwnselecte++;
                    126:                else if (n == 0)
                    127:                        wwnselectz++;
1.2       deraadt   128:                else {
                    129:                        if (FD_ISSET(0, &imask))
                    130:                                wwrint();
1.1       deraadt   131:                        for (w = wwhead.ww_forw; w != &wwhead; w = w->ww_forw) {
                    132:                                if (w->ww_pty < 0 ||
                    133:                                    !FD_ISSET(w->ww_pty, &imask))
                    134:                                        continue;
                    135:                                wwnwread++;
                    136:                                p = w->ww_obq;
1.2       deraadt   137:                                if (w->ww_type == WWT_PTY) {
1.1       deraadt   138:                                        if (p == w->ww_ob) {
                    139:                                                w->ww_obp++;
                    140:                                                w->ww_obq++;
                    141:                                        } else
                    142:                                                p--;
                    143:                                        c = *p;
                    144:                                }
                    145:                                n = read(w->ww_pty, p, w->ww_obe - p);
                    146:                                if (n < 0) {
                    147:                                        wwnwreade++;
                    148:                                        (void) close(w->ww_pty);
                    149:                                        w->ww_pty = -1;
                    150:                                } else if (n == 0) {
                    151:                                        wwnwreadz++;
                    152:                                        (void) close(w->ww_pty);
                    153:                                        w->ww_pty = -1;
1.2       deraadt   154:                                } else if (w->ww_type != WWT_PTY) {
1.1       deraadt   155:                                        wwnwreadd++;
                    156:                                        wwnwreadc += n;
                    157:                                        w->ww_obq += n;
                    158:                                } else if (*p == TIOCPKT_DATA) {
                    159:                                        n--;
                    160:                                        wwnwreadd++;
                    161:                                        wwnwreadc += n;
                    162:                                        w->ww_obq += n;
                    163:                                } else {
                    164:                                        wwnwreadp++;
                    165:                                        if (*p & TIOCPKT_STOP)
1.3     ! niklas    166:                                                SET(w->ww_pflags, WWP_STOPPED);
1.1       deraadt   167:                                        if (*p & TIOCPKT_START)
1.3     ! niklas    168:                                                CLR(w->ww_pflags, WWP_STOPPED);
1.1       deraadt   169:                                        if (*p & TIOCPKT_FLUSHWRITE) {
1.3     ! niklas    170:                                                CLR(w->ww_pflags, WWP_STOPPED);
1.1       deraadt   171:                                                w->ww_obq = w->ww_obp =
                    172:                                                        w->ww_ob;
                    173:                                        }
                    174:                                }
                    175:                        }
1.2       deraadt   176:                }
1.1       deraadt   177:                /*
                    178:                 * Try the current window first, if there is output
                    179:                 * then process it and go back to the top to try again.
                    180:                 * This can lead to starvation of the other windows,
                    181:                 * but presumably that what we want.
                    182:                 * Update will eventually happen when output from wwcurwin
                    183:                 * dies down.
                    184:                 */
                    185:                if ((w = wwcurwin) != 0 && w->ww_pty >= 0 &&
1.3     ! niklas    186:                    w->ww_obq > w->ww_obp &&
        !           187:                    !ISSET(w->ww_pflags, WWP_STOPPED)) {
1.1       deraadt   188:                        n = wwwrite(w, w->ww_obp, w->ww_obq - w->ww_obp);
                    189:                        if ((w->ww_obp += n) == w->ww_obq)
                    190:                                w->ww_obq = w->ww_obp = w->ww_ob;
                    191:                        noblock = 1;
                    192:                        continue;
                    193:                }
                    194:                for (w = wwhead.ww_forw; w != &wwhead; w = w->ww_forw)
                    195:                        if (w->ww_pty >= 0 && w->ww_obq > w->ww_obp &&
1.3     ! niklas    196:                            !ISSET(w->ww_pflags, WWP_STOPPED)) {
1.1       deraadt   197:                                n = wwwrite(w, w->ww_obp,
                    198:                                        w->ww_obq - w->ww_obp);
                    199:                                if ((w->ww_obp += n) == w->ww_obq)
                    200:                                        w->ww_obq = w->ww_obp = w->ww_ob;
                    201:                                if (wwinterrupt())
                    202:                                        break;
                    203:                        }
                    204:        }
                    205: }