xref: /original-bsd/usr.bin/window/wwtty.c (revision 2622b709)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * 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  * %sccs.include.redist.c%
9  */
10 
11 #ifndef lint
12 static char sccsid[] = "@(#)wwtty.c	3.16 (Berkeley) 06/06/90";
13 #endif /* not lint */
14 
15 #include "ww.h"
16 #include <fcntl.h>
17 
18 wwgettty(d, t)
19 register struct ww_tty *t;
20 {
21 #ifndef POSIX_TTY
22 	if (ioctl(d, TIOCGETP, (char *)&t->ww_sgttyb) < 0)
23 		goto bad;
24 	if (ioctl(d, TIOCGETC, (char *)&t->ww_tchars) < 0)
25 		goto bad;
26 	if (ioctl(d, TIOCGLTC, (char *)&t->ww_ltchars) < 0)
27 		goto bad;
28 	if (ioctl(d, TIOCLGET, (char *)&t->ww_lmode) < 0)
29 		goto bad;
30 	if (ioctl(d, TIOCGETD, (char *)&t->ww_ldisc) < 0)
31 		goto bad;
32 #else
33 	if (tcgetattr(d, &t->ww_termios) < 0)
34 		goto bad;
35 #endif
36 	if ((t->ww_fflags = fcntl(d, F_GETFL, 0)) < 0)
37 		goto bad;
38 	return 0;
39 bad:
40 	wwerrno = WWE_SYS;
41 	return -1;
42 }
43 
44 /*
45  * Set the modes of tty 'd' to 't'
46  * 'o' is the current modes.  We set the line discipline only if
47  * it changes, to avoid unnecessary flushing of typeahead.
48  */
49 wwsettty(d, t, o)
50 register struct ww_tty *t, *o;
51 {
52 #ifndef POSIX_TTY
53 	/* for buggy tty driver that doesn't wait for output to drain */
54 	int i;
55 	while (ioctl(d, TIOCOUTQ, &i) >= 0 && i > 0)
56 		usleep(100000);
57 	if (ioctl(d, TIOCSETN, (char *)&t->ww_sgttyb) < 0)
58 		goto bad;
59 	if (ioctl(d, TIOCSETC, (char *)&t->ww_tchars) < 0)
60 		goto bad;
61 	if (ioctl(d, TIOCSLTC, (char *)&t->ww_ltchars) < 0)
62 		goto bad;
63 	if (ioctl(d, TIOCLSET, (char *)&t->ww_lmode) < 0)
64 		goto bad;
65 	if ((o == 0 || t->ww_ldisc != o->ww_ldisc) &&
66 	    ioctl(d, TIOCSETD, (char *)&t->ww_ldisc) < 0)
67 		goto bad;
68 #else
69 	if (tcsetattr(d, TCSADRAIN, &t->ww_termios) < 0)
70 		goto bad;
71 #endif
72 	if (fcntl(d, F_SETFL, t->ww_fflags) < 0)
73 		goto bad;
74 	return 0;
75 bad:
76 	wwerrno = WWE_SYS;
77 	return -1;
78 }
79