Blame


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