]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/window/wwtty.c
This commit was generated by cvs2svn to compensate for changes in r68651,
[FreeBSD/FreeBSD.git] / usr.bin / window / wwtty.c
1 /*
2  * Copyright (c) 1983, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Edward Wang at The University of California, Berkeley.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by the University of
19  *      California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36
37 #ifndef lint
38 static char sccsid[] = "@(#)wwtty.c     8.1 (Berkeley) 6/6/93";
39 static char rcsid[] = "@(#)$FreeBSD$";
40 #endif /* not lint */
41
42 #include "ww.h"
43 #include <sys/types.h>
44 #include <fcntl.h>
45 #if !defined(OLD_TTY) && !defined(TIOCGWINSZ)
46 #include <sys/ioctl.h>
47 #endif
48
49 wwgettty(d, t)
50 register struct ww_tty *t;
51 {
52 #ifdef OLD_TTY
53         if (ioctl(d, TIOCGETP, (char *)&t->ww_sgttyb) < 0)
54                 goto bad;
55         if (ioctl(d, TIOCGETC, (char *)&t->ww_tchars) < 0)
56                 goto bad;
57         if (ioctl(d, TIOCGLTC, (char *)&t->ww_ltchars) < 0)
58                 goto bad;
59         if (ioctl(d, TIOCLGET, (char *)&t->ww_lmode) < 0)
60                 goto bad;
61         if (ioctl(d, TIOCGETD, (char *)&t->ww_ldisc) < 0)
62                 goto bad;
63 #else
64         if (tcgetattr(d, &t->ww_termios) < 0)
65                 goto bad;
66 #endif
67         if ((t->ww_fflags = fcntl(d, F_GETFL, 0)) < 0)
68                 goto bad;
69         return 0;
70 bad:
71         wwerrno = WWE_SYS;
72         return -1;
73 }
74
75 /*
76  * Set the modes of tty 'd' to 't'
77  * 'o' is the current modes.  We set the line discipline only if
78  * it changes, to avoid unnecessary flushing of typeahead.
79  */
80 wwsettty(d, t)
81 register struct ww_tty *t;
82 {
83 #ifdef OLD_TTY
84         int i;
85
86         /* XXX, for buggy tty drivers that don't wait for output to drain */
87         while (ioctl(d, TIOCOUTQ, &i) >= 0 && i > 0)
88                 usleep(100000);
89         if (ioctl(d, TIOCSETN, (char *)&t->ww_sgttyb) < 0)
90                 goto bad;
91         if (ioctl(d, TIOCSETC, (char *)&t->ww_tchars) < 0)
92                 goto bad;
93         if (ioctl(d, TIOCSLTC, (char *)&t->ww_ltchars) < 0)
94                 goto bad;
95         if (ioctl(d, TIOCLSET, (char *)&t->ww_lmode) < 0)
96                 goto bad;
97         if (ioctl(d, TIOCGETD, (char *)&i) < 0)
98                 goto bad;
99         if (t->ww_ldisc != i &&
100             ioctl(d, TIOCSETD, (char *)&t->ww_ldisc) < 0)
101                 goto bad;
102 #else
103 #ifdef sun
104         /* XXX, for buggy tty drivers that don't wait for output to drain */
105         (void) tcdrain(d);
106 #endif
107         if (tcsetattr(d, TCSADRAIN, &t->ww_termios) < 0)
108                 goto bad;
109 #endif
110         if (fcntl(d, F_SETFL, t->ww_fflags) < 0)
111                 goto bad;
112         return 0;
113 bad:
114         wwerrno = WWE_SYS;
115         return -1;
116 }
117
118 /*
119  * The ttysize and stop-start routines must also work
120  * on the control side of pseudoterminals.
121  */
122
123 wwgetttysize(d, r, c)
124         int *r, *c;
125 {
126         struct winsize winsize;
127
128         if (ioctl(d, TIOCGWINSZ, (char *)&winsize) < 0) {
129                 wwerrno = WWE_SYS;
130                 return -1;
131         }
132         if (winsize.ws_row != 0)
133                 *r = winsize.ws_row;
134         if (winsize.ws_col != 0)
135                 *c = winsize.ws_col;
136         return 0;
137 }
138
139 wwsetttysize(d, r, c)
140 {
141         struct winsize winsize;
142
143         winsize.ws_row = r;
144         winsize.ws_col = c;
145         winsize.ws_xpixel = winsize.ws_ypixel = 0;
146         if (ioctl(d, TIOCSWINSZ, (char *)&winsize) < 0) {
147                 wwerrno = WWE_SYS;
148                 return -1;
149         }
150         return 0;
151 }
152
153 wwstoptty(d)
154 {
155 #if !defined(OLD_TTY) && defined(TCOOFF)
156         /* not guaranteed to work on the pty side */
157         if (tcflow(d, TCOOFF) < 0)
158 #else
159         if (ioctl(d, TIOCSTOP, (char *)0) < 0)
160 #endif
161         {
162                 wwerrno = WWE_SYS;
163                 return -1;
164         }
165         return 0;
166 }
167
168 wwstarttty(d)
169 {
170 #if !defined(OLD_TTY) && defined(TCOON)
171         /* not guaranteed to work on the pty side */
172         if (tcflow(d, TCOON) < 0)
173 #else
174         if (ioctl(d, TIOCSTART, (char *)0) < 0)
175 #endif
176         {
177                 wwerrno = WWE_SYS;
178                 return -1;
179         }
180         return 0;
181 }