aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/9term/bsdpty.c
blob: d64e4c2fcb9a88d397890adf5e9336e14ada2a83 (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
#include <u.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <errno.h>
#include <grp.h>
#include <termios.h>
#include <sys/termios.h>
#ifdef __linux__
#include <pty.h>
#endif
#include <fcntl.h>
#include <libc.h>
#include <draw.h>
#include "term.h"

#define debug 0

static char *abc =
	"abcdefghijklmnopqrstuvwxyz"
	"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	"0123456789";
static char *_123 =
	"0123456789"
	"abcdefghijklmnopqrstuvwxyz"
	"ABCDEFGHIJKLMNOPQRSTUVWXYZ";

int
getpts(int fd[], char *slave)
{
	char *a, *z;
	char pty[] = "/dev/ptyXX";

	for(a=abc; *a; a++)
	for(z=_123; *z; z++){
		pty[8] = *a;
		pty[9] = *z;
		if((fd[1] = open(pty, ORDWR)) < 0){
			if(errno == ENOENT)
				break;
		}else{
			fchmod(fd[1], 0620);
			strcpy(slave, pty);
			slave[5] = 't';
			if((fd[0] = open(slave, ORDWR)) >= 0)
				return 0;
			close(fd[1]);
		}
	}
	sysfatal("no ptys");
	return 0;
}

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

	close(fd[1]);	/* drop master */
	setsid();
	sfd = open(slave, ORDWR);
	if(sfd < 0)
		sysfatal("child open %s: %r\n", slave);
	if(ioctl(sfd, TIOCSCTTY, 0) < 0)
		fprint(2, "ioctl TIOCSCTTY: %r\n");
	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;

	needdisplay(); // in case this is 'win' and not 9term
	// Leave "is this a hidpi display" in the low bit of the ypixel height for mc.
	dy &= ~1;
	if(display != nil && display->dpi >= DefaultDPI*3/2)
		dy |= 1;
	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: %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&ECHO;
}

int
getintr(int fd)
{
	if(tcgetattr(fd, &ttmode) < 0)
		return 0x7F;
	return ttmode.c_cc[VINTR];
}