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 "term.h"
16 #define debug 0
18 static char *abc =
19 "abcdefghijklmnopqrstuvwxyz"
20 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
21 "0123456789";
22 static char *_123 =
23 "0123456789"
24 "abcdefghijklmnopqrstuvwxyz"
25 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
27 int
28 getpts(int fd[], char *slave)
29 {
30 char *a, *z;
31 char pty[] = "/dev/ptyXX";
33 for(a=abc; *a; a++)
34 for(z=_123; *z; z++){
35 pty[8] = *a;
36 pty[9] = *z;
37 if((fd[1] = open(pty, ORDWR)) < 0){
38 if(errno == ENOENT)
39 break;
40 }else{
41 fchmod(fd[1], 0620);
42 strcpy(slave, pty);
43 slave[5] = 't';
44 if((fd[0] = open(slave, ORDWR)) >= 0)
45 return 0;
46 close(fd[1]);
47 }
48 }
49 sysfatal("no ptys");
50 return 0;
51 }
53 int
54 childpty(int fd[], char *slave)
55 {
56 int sfd;
58 close(fd[1]); /* drop master */
59 setsid();
60 sfd = open(slave, ORDWR);
61 if(sfd < 0)
62 sysfatal("child open %s: %r\n", slave);
63 if(ioctl(sfd, TIOCSCTTY, 0) < 0)
64 fprint(2, "ioctl TIOCSCTTY: %r\n");
65 return sfd;
66 }
68 struct winsize ows;
70 void
71 updatewinsize(int row, int col, int dx, int dy)
72 {
73 struct winsize ws;
75 ws.ws_row = row;
76 ws.ws_col = col;
77 ws.ws_xpixel = dx;
78 ws.ws_ypixel = dy;
79 if(ws.ws_row != ows.ws_row || ws.ws_col != ows.ws_col){
80 if(ioctl(rcfd, TIOCSWINSZ, &ws) < 0)
81 fprint(2, "ioctl: %r\n");
82 }
83 ows = ws;
84 }
86 static struct termios ttmode;
88 int
89 isecho(int fd)
90 {
91 if(tcgetattr(fd, &ttmode) < 0)
92 fprint(2, "tcgetattr: %r\n");
93 if(debug) fprint(2, "israw %c%c\n",
94 ttmode.c_lflag&ICANON ? 'c' : '-',
95 ttmode.c_lflag&ECHO ? 'e' : '-');
96 return ttmode.c_lflag&ECHO;
97 }
99 int
100 setecho(int fd, int newe)
102 int old;
104 if(tcgetattr(fd, &ttmode) < 0)
105 fprint(2, "tcgetattr: %r\n");
106 old = ttmode.c_lflag & ECHO;
107 if(old != newe){
108 ttmode.c_lflag &= ~ECHO;
109 ttmode.c_lflag |= newe;
110 /*
111 * I tried using TCSADRAIN here, but that causes
112 * hangs if there is any output waiting for us.
113 * I guess TCSADRAIN is intended for use by our
114 * clients, not by us.
115 */
116 if(tcsetattr(fd, 0, &ttmode) < 0)
117 fprint(2, "tcsetattr: %r\n");
119 return old;
122 int
123 getintr(int fd)
125 if(tcgetattr(fd, &ttmode) < 0)
126 return 0x7F;
127 return ttmode.c_cc[VINTR];