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