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