Blob


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