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

Annotation of src/usr.bin/window/wwtty.c, Revision 1.1

1.1     ! deraadt     1: /*     $NetBSD: wwtty.c,v 1.3 1995/09/28 10:35:58 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[] = "@(#)wwtty.c    8.1 (Berkeley) 6/6/93";
        !            42: #else
        !            43: static char rcsid[] = "$NetBSD: wwtty.c,v 1.3 1995/09/28 10:35:58 tls Exp $";
        !            44: #endif
        !            45: #endif /* not lint */
        !            46:
        !            47: #include "ww.h"
        !            48: #include <sys/types.h>
        !            49: #include <fcntl.h>
        !            50: #if !defined(OLD_TTY) && !defined(TIOCGWINSZ)
        !            51: #include <sys/ioctl.h>
        !            52: #endif
        !            53:
        !            54: wwgettty(d, t)
        !            55: register struct ww_tty *t;
        !            56: {
        !            57: #ifdef OLD_TTY
        !            58:        if (ioctl(d, TIOCGETP, (char *)&t->ww_sgttyb) < 0)
        !            59:                goto bad;
        !            60:        if (ioctl(d, TIOCGETC, (char *)&t->ww_tchars) < 0)
        !            61:                goto bad;
        !            62:        if (ioctl(d, TIOCGLTC, (char *)&t->ww_ltchars) < 0)
        !            63:                goto bad;
        !            64:        if (ioctl(d, TIOCLGET, (char *)&t->ww_lmode) < 0)
        !            65:                goto bad;
        !            66:        if (ioctl(d, TIOCGETD, (char *)&t->ww_ldisc) < 0)
        !            67:                goto bad;
        !            68: #else
        !            69:        if (tcgetattr(d, &t->ww_termios) < 0)
        !            70:                goto bad;
        !            71: #endif
        !            72:        if ((t->ww_fflags = fcntl(d, F_GETFL, 0)) < 0)
        !            73:                goto bad;
        !            74:        return 0;
        !            75: bad:
        !            76:        wwerrno = WWE_SYS;
        !            77:        return -1;
        !            78: }
        !            79:
        !            80: /*
        !            81:  * Set the modes of tty 'd' to 't'
        !            82:  * 'o' is the current modes.  We set the line discipline only if
        !            83:  * it changes, to avoid unnecessary flushing of typeahead.
        !            84:  */
        !            85: wwsettty(d, t)
        !            86: register struct ww_tty *t;
        !            87: {
        !            88: #ifdef OLD_TTY
        !            89:        int i;
        !            90:
        !            91:        /* XXX, for buggy tty drivers that don't wait for output to drain */
        !            92:        while (ioctl(d, TIOCOUTQ, &i) >= 0 && i > 0)
        !            93:                usleep(100000);
        !            94:        if (ioctl(d, TIOCSETN, (char *)&t->ww_sgttyb) < 0)
        !            95:                goto bad;
        !            96:        if (ioctl(d, TIOCSETC, (char *)&t->ww_tchars) < 0)
        !            97:                goto bad;
        !            98:        if (ioctl(d, TIOCSLTC, (char *)&t->ww_ltchars) < 0)
        !            99:                goto bad;
        !           100:        if (ioctl(d, TIOCLSET, (char *)&t->ww_lmode) < 0)
        !           101:                goto bad;
        !           102:        if (ioctl(d, TIOCGETD, (char *)&i) < 0)
        !           103:                goto bad;
        !           104:        if (t->ww_ldisc != i &&
        !           105:            ioctl(d, TIOCSETD, (char *)&t->ww_ldisc) < 0)
        !           106:                goto bad;
        !           107: #else
        !           108: #ifdef sun
        !           109:        /* XXX, for buggy tty drivers that don't wait for output to drain */
        !           110:        (void) tcdrain(d);
        !           111: #endif
        !           112:        if (tcsetattr(d, TCSADRAIN, &t->ww_termios) < 0)
        !           113:                goto bad;
        !           114: #endif
        !           115:        if (fcntl(d, F_SETFL, t->ww_fflags) < 0)
        !           116:                goto bad;
        !           117:        return 0;
        !           118: bad:
        !           119:        wwerrno = WWE_SYS;
        !           120:        return -1;
        !           121: }
        !           122:
        !           123: /*
        !           124:  * The ttysize and stop-start routines must also work
        !           125:  * on the control side of pseudoterminals.
        !           126:  */
        !           127:
        !           128: wwgetttysize(d, r, c)
        !           129:        int *r, *c;
        !           130: {
        !           131:        struct winsize winsize;
        !           132:
        !           133:        if (ioctl(d, TIOCGWINSZ, (char *)&winsize) < 0) {
        !           134:                wwerrno = WWE_SYS;
        !           135:                return -1;
        !           136:        }
        !           137:        if (winsize.ws_row != 0)
        !           138:                *r = winsize.ws_row;
        !           139:        if (winsize.ws_col != 0)
        !           140:                *c = winsize.ws_col;
        !           141:        return 0;
        !           142: }
        !           143:
        !           144: wwsetttysize(d, r, c)
        !           145: {
        !           146:        struct winsize winsize;
        !           147:
        !           148:        winsize.ws_row = r;
        !           149:        winsize.ws_col = c;
        !           150:        winsize.ws_xpixel = winsize.ws_ypixel = 0;
        !           151:        if (ioctl(d, TIOCSWINSZ, (char *)&winsize) < 0) {
        !           152:                wwerrno = WWE_SYS;
        !           153:                return -1;
        !           154:        }
        !           155:        return 0;
        !           156: }
        !           157:
        !           158: wwstoptty(d)
        !           159: {
        !           160: #if !defined(OLD_TTY) && defined(TCOOFF)
        !           161:        /* not guaranteed to work on the pty side */
        !           162:        if (tcflow(d, TCOOFF) < 0)
        !           163: #else
        !           164:        if (ioctl(d, TIOCSTOP, (char *)0) < 0)
        !           165: #endif
        !           166:        {
        !           167:                wwerrno = WWE_SYS;
        !           168:                return -1;
        !           169:        }
        !           170:        return 0;
        !           171: }
        !           172:
        !           173: wwstarttty(d)
        !           174: {
        !           175: #if !defined(OLD_TTY) && defined(TCOON)
        !           176:        /* not guaranteed to work on the pty side */
        !           177:        if (tcflow(d, TCOON) < 0)
        !           178: #else
        !           179:        if (ioctl(d, TIOCSTART, (char *)0) < 0)
        !           180: #endif
        !           181:        {
        !           182:                wwerrno = WWE_SYS;
        !           183:                return -1;
        !           184:        }
        !           185:        return 0;
        !           186: }