1 /*	$OpenBSD: wwtty.c,v 1.7 2003/08/01 22:01:38 david Exp $	*/
2 /*	$NetBSD: wwtty.c,v 1.4 1995/12/21 11:06:50 mycroft Exp $	*/
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. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)wwtty.c	8.1 (Berkeley) 6/6/93";
39 #else
40 static char rcsid[] = "$OpenBSD: wwtty.c,v 1.7 2003/08/01 22:01:38 david Exp $";
41 #endif
42 #endif /* not lint */
43 
44 #include "ww.h"
45 #include <sys/types.h>
46 #include <fcntl.h>
47 #if !defined(OLD_TTY)
48 #include <sys/ioctl.h>
49 #endif
50 
51 wwgettty(d, t)
52 struct ww_tty *t;
53 {
54 #ifdef OLD_TTY
55 	if (ioctl(d, TIOCGETP, (char *)&t->ww_sgttyb) < 0)
56 		goto bad;
57 	if (ioctl(d, TIOCGETC, (char *)&t->ww_tchars) < 0)
58 		goto bad;
59 	if (ioctl(d, TIOCGLTC, (char *)&t->ww_ltchars) < 0)
60 		goto bad;
61 	if (ioctl(d, TIOCLGET, (char *)&t->ww_lmode) < 0)
62 		goto bad;
63 	if (ioctl(d, TIOCGETD, (char *)&t->ww_ldisc) < 0)
64 		goto bad;
65 #else
66 	if (tcgetattr(d, &t->ww_termios) < 0)
67 		goto bad;
68 #endif
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 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 	return 0;
111 bad:
112 	wwerrno = WWE_SYS;
113 	return -1;
114 }
115 
116 /*
117  * The ttysize and stop-start routines must also work
118  * on the control side of pseudoterminals.
119  */
120 
wwgetttysize(d,r,c)121 wwgetttysize(d, r, c)
122 	int *r, *c;
123 {
124 	struct winsize winsize;
125 
126 	if (ioctl(d, TIOCGWINSZ, (char *)&winsize) < 0) {
127 		wwerrno = WWE_SYS;
128 		return -1;
129 	}
130 	if (winsize.ws_row != 0)
131 		*r = winsize.ws_row;
132 	if (winsize.ws_col != 0)
133 		*c = winsize.ws_col;
134 	return 0;
135 }
136 
wwsetttysize(d,r,c)137 wwsetttysize(d, r, c)
138 {
139 	struct winsize winsize;
140 
141 	winsize.ws_row = r;
142 	winsize.ws_col = c;
143 	winsize.ws_xpixel = winsize.ws_ypixel = 0;
144 	if (ioctl(d, TIOCSWINSZ, (char *)&winsize) < 0) {
145 		wwerrno = WWE_SYS;
146 		return -1;
147 	}
148 	return 0;
149 }
150 
wwstoptty(d)151 wwstoptty(d)
152 {
153 #if !defined(OLD_TTY) && defined(TCOOFF)
154 	/* not guaranteed to work on the pty side */
155 	if (tcflow(d, TCOOFF) < 0)
156 #else
157 	if (ioctl(d, TIOCSTOP, (char *)0) < 0)
158 #endif
159 	{
160 		wwerrno = WWE_SYS;
161 		return -1;
162 	}
163 	return 0;
164 }
165 
wwstarttty(d)166 wwstarttty(d)
167 {
168 #if !defined(OLD_TTY) && defined(TCOON)
169 	/* not guaranteed to work on the pty side */
170 	if (tcflow(d, TCOON) < 0)
171 #else
172 	if (ioctl(d, TIOCSTART, (char *)0) < 0)
173 #endif
174 	{
175 		wwerrno = WWE_SYS;
176 		return -1;
177 	}
178 	return 0;
179 }
180