summaryrefslogtreecommitdiffstats
path: root/src/cmd/9term/SunOS.c
blob: 252283443dd7af6c2691501c9c1ce08976d042cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <u.h>
#include <termios.h>
#include <stropts.h>
#include <libc.h>
#include "term.h"

int
getpts(int fd[], char *slave)
{
	fd[1] = open("/dev/ptmx", ORDWR);
	if ((grantpt(fd[1]) < 0) || (unlockpt(fd[1]) < 0))
		return -1;
	fchmod(fd[1], 0622);

	strcpy(slave, ptsname(fd[1]));

	fd[0] = open(slave, ORDWR);
	if(fd[0] < 0)
		sysfatal("open %s: %r\n", slave);

	/* set up the right streams modules for a tty */
	ioctl(fd[0], I_PUSH, "ptem");        /* push ptem */
	ioctl(fd[0], I_PUSH, "ldterm");      /* push ldterm */

	return 0;
}

int
childpty(int fd[], char *slave)
{
	int sfd;

	close(fd[1]);
	setsid();
	sfd = open(slave, ORDWR);
	if(sfd < 0)
		sysfatal("open %s: %r\n", slave);
	return sfd;
}

struct winsize ows;

void
updatewinsize(int row, int col, int dx, int dy)
{
	struct winsize ws;

	ws.ws_row = row;
	ws.ws_col = col;
	ws.ws_xpixel = dx;
	ws.ws_ypixel = dy;
	if(ws.ws_row != ows.ws_row || ws.ws_col != ows.ws_col)
	if(ioctl(rcfd, TIOCSWINSZ, &ws) < 0)
		fprint(2, "ioctl TIOCSWINSZ: %r\n");
	ows = ws;
}

/*
 * israw has been inspired by Matty Farrow's 9term.
 * The code below is probably a gross simplification --
 * for the few cases tested it seems to be enough.
 * However, for example, Matty's code also looks at ISIG,
 * whereas, we do not (yet?). Axel.
 *
 *Note: I guess only the get/set terminal mode attribute
 * code needs to be here; the logic around it could be
 * elswhere (9term.c) - but if the code below is split,
 * the question is what a nice interface would be. Axel.
 */

static struct termios ttmode;

int
israw(int fd)
{
	int e, c, i;

	tcgetattr(fd, &ttmode);
	c = (ttmode.c_lflag & ICANON) ? 1 : 0;
	e = (ttmode.c_lflag & ECHO) ? 1 : 0;
	i = (ttmode.c_lflag & ISIG) ? 1 : 0;

	if(0) fprint(2, "israw: icanon=%d echo=%d isig=%d\n", c, e, i);

	return !c || !e ;
}


int
setecho(int fd, int on)
{
	int e, c, i;
	int oldecho;

	tcgetattr(fd, &ttmode);
	c = (ttmode.c_lflag & ICANON) ? 1 : 0;
	e = (ttmode.c_lflag & ECHO) ? 1 : 0;
	i = (ttmode.c_lflag & ISIG) ? 1 : 0;

	if(0) fprint(2, "setecho(%d) pre: icanon=%d echo=%d isig=%d\n", on, c, e, i);

	oldecho = e;

	if (oldecho == on)
		return  oldecho;

	if (on) {
		ttmode.c_lflag |= ECHO;
		tcsetattr(fd, TCSANOW, &ttmode);
	} else {
		ttmode.c_lflag &= ~ECHO;
		tcsetattr(fd, TCSANOW, &ttmode);
	}

	if (0){
		tcgetattr(fd, &ttmode);
		c = (ttmode.c_lflag & ICANON) ? 1 : 0;
		e =  (ttmode.c_lflag & ECHO) ? 1 : 0;
		i = (ttmode.c_lflag & ISIG) ? 1 : 0;

		fprint(2, "setecho(%d) post: icanon=%d echo=%d isig=%d\n", on, c, e, i);
	}

	return oldecho;
}