aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/9term/SunOS.c
blob: 467f0d232b49d183549a3f196b70403497c0e761 (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
#include <u.h>
#include <termios.h>
#include <stropts.h>
#include <signal.h>
#include <libc.h>
#include "term.h"

#define debug 0

int
getpts(int fd[], char *slave)
{
	void (*f)(int);
	int r;

	fd[1] = open("/dev/ptmx", ORDWR);
	f = signal(SIGCLD, SIG_DFL);
	r = grantpt(fd[1]);
	signal(SIGCLD, f);
	if(r < 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;
}

static struct termios ttmode;

int
isecho(int fd)
{
	if(tcgetattr(fd, &ttmode) < 0)
		fprint(2, "tcgetattr: %r\n");
	if(debug) fprint(2, "israw %c%c\n",
		ttmode.c_lflag&ICANON ? 'c' : '-',
		ttmode.c_lflag&ECHO ? 'e' : '-');
	return (ttmode.c_lflag&ICANON && ttmode.c_lflag&ECHO);
}

int
setecho(int fd, int newe)
{
	int old;

	if(tcgetattr(fd, &ttmode) < 0)
		fprint(2, "tcgetattr: %r\n");
	old = (ttmode.c_lflag&ECHO)==ECHO;
	if(old != newe){
		if(newe)
			ttmode.c_lflag |= ECHO;
		else
			ttmode.c_lflag &= ~ECHO;
		if(tcsetattr(fd, TCSANOW, &ttmode) < 0)
			fprint(2, "tcsetattr: %r\n");
	}
	return old;
}