1 #include <u.h>
2 #include <sys/types.h>
3 #include <sys/ioctl.h>
4 #include <sys/stat.h>
5 #include <errno.h>
6 #include <grp.h>
7 #include <termios.h>
8 #ifdef HAS_SYS_TERMIOS
9 #endif
10 #ifdef __linux__
11 #include <pty.h>
12 #endif
13 #include <fcntl.h>
14 #include <libc.h>
15 #include <draw.h>
16 #include "term.h"
17 
18 #define debug 0
19 
20 static char *abc =
21 	"abcdefghijklmnopqrstuvwxyz"
22 	"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
23 	"0123456789";
24 static char *_123 =
25 	"0123456789"
26 	"abcdefghijklmnopqrstuvwxyz"
27 	"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
28 
29 int
getpts(int fd[],char * slave)30 getpts(int fd[], char *slave)
31 {
32 	char *a, *z;
33 	char pty[] = "/dev/ptyXX";
34 
35 	for(a=abc; *a; a++)
36 	for(z=_123; *z; z++){
37 		pty[8] = *a;
38 		pty[9] = *z;
39 		if((fd[1] = open(pty, ORDWR)) < 0){
40 			if(errno == ENOENT)
41 				break;
42 		}else{
43 			fchmod(fd[1], 0620);
44 			strcpy(slave, pty);
45 			slave[5] = 't';
46 			if((fd[0] = open(slave, ORDWR)) >= 0)
47 				return 0;
48 			close(fd[1]);
49 		}
50 	}
51 	sysfatal("no ptys");
52 	return 0;
53 }
54 
55 int
childpty(int fd[],char * slave)56 childpty(int fd[], char *slave)
57 {
58 	int sfd;
59 
60 	close(fd[1]);	/* drop master */
61 	setsid();
62 	sfd = open(slave, ORDWR);
63 	if(sfd < 0)
64 		sysfatal("child open %s: %r\n", slave);
65 	if(ioctl(sfd, TIOCSCTTY, 0) < 0)
66 		fprint(2, "ioctl TIOCSCTTY: %r\n");
67 	return sfd;
68 }
69 
70 struct winsize ows;
71 
72 void
updatewinsize(int row,int col,int dx,int dy)73 updatewinsize(int row, int col, int dx, int dy)
74 {
75 	struct winsize ws;
76 
77 	ws.ws_row = row;
78 	ws.ws_col = col;
79 	ws.ws_xpixel = dx;
80 
81 	needdisplay(); // in case this is 'win' and not 9term
82 	// Leave "is this a hidpi display" in the low bit of the ypixel height for mc.
83 	dy &= ~1;
84 	if(display != nil && display->dpi >= DefaultDPI*3/2)
85 		dy |= 1;
86 	ws.ws_ypixel = dy;
87 
88 	if(ws.ws_row != ows.ws_row || ws.ws_col != ows.ws_col){
89 		if(ioctl(rcfd, TIOCSWINSZ, &ws) < 0)
90 			fprint(2, "ioctl: %r\n");
91 	}
92 	ows = ws;
93 }
94 
95 static struct termios ttmode;
96 
97 int
isecho(int fd)98 isecho(int fd)
99 {
100 	if(tcgetattr(fd, &ttmode) < 0)
101 		fprint(2, "tcgetattr: %r\n");
102 	if(debug) fprint(2, "israw %c%c\n",
103 		ttmode.c_lflag&ICANON ? 'c' : '-',
104 		ttmode.c_lflag&ECHO ? 'e' : '-');
105 	return ttmode.c_lflag&ECHO;
106 }
107 
108 int
getintr(int fd)109 getintr(int fd)
110 {
111 	if(tcgetattr(fd, &ttmode) < 0)
112 		return 0x7F;
113 	return ttmode.c_cc[VINTR];
114 }
115