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>
12 ebb4c247 2004-04-16 devnull #include <fcntl.h>
13 ebb4c247 2004-04-16 devnull #include <libc.h>
14 ebb4c247 2004-04-16 devnull #include "term.h"
16 ebb4c247 2004-04-16 devnull #define debug 0
18 ebb4c247 2004-04-16 devnull static char *abc =
19 ebb4c247 2004-04-16 devnull "abcdefghijklmnopqrstuvwxyz"
20 ebb4c247 2004-04-16 devnull "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
21 ebb4c247 2004-04-16 devnull "0123456789";
22 ebb4c247 2004-04-16 devnull static char *_123 =
23 ebb4c247 2004-04-16 devnull "0123456789"
24 ebb4c247 2004-04-16 devnull "abcdefghijklmnopqrstuvwxyz"
25 ebb4c247 2004-04-16 devnull "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
28 ebb4c247 2004-04-16 devnull getpts(int fd[], char *slave)
30 ebb4c247 2004-04-16 devnull char *a, *z;
31 ebb4c247 2004-04-16 devnull char pty[] = "/dev/ptyXX";
33 ebb4c247 2004-04-16 devnull for(a=abc; *a; a++)
34 ebb4c247 2004-04-16 devnull for(z=_123; *z; z++){
35 ebb4c247 2004-04-16 devnull pty[8] = *a;
36 ebb4c247 2004-04-16 devnull pty[9] = *z;
37 ebb4c247 2004-04-16 devnull if((fd[1] = open(pty, ORDWR)) < 0){
38 ebb4c247 2004-04-16 devnull if(errno == ENOENT)
41 ebb4c247 2004-04-16 devnull fchmod(fd[1], 0620);
42 ebb4c247 2004-04-16 devnull strcpy(slave, pty);
43 ebb4c247 2004-04-16 devnull slave[5] = 't';
44 ebb4c247 2004-04-16 devnull if((fd[0] = open(slave, ORDWR)) >= 0)
45 ebb4c247 2004-04-16 devnull return 0;
46 ebb4c247 2004-04-16 devnull close(fd[1]);
49 ebb4c247 2004-04-16 devnull sysfatal("no ptys");
50 ebb4c247 2004-04-16 devnull return 0;
54 ebb4c247 2004-04-16 devnull childpty(int fd[], char *slave)
58 ebb4c247 2004-04-16 devnull close(fd[1]); /* drop master */
59 ebb4c247 2004-04-16 devnull setsid();
60 ebb4c247 2004-04-16 devnull sfd = open(slave, ORDWR);
61 ebb4c247 2004-04-16 devnull if(sfd < 0)
62 ebb4c247 2004-04-16 devnull sysfatal("child open %s: %r\n", slave);
63 ebb4c247 2004-04-16 devnull if(ioctl(sfd, TIOCSCTTY, 0) < 0)
64 ebb4c247 2004-04-16 devnull fprint(2, "ioctl TIOCSCTTY: %r\n");
65 ebb4c247 2004-04-16 devnull return sfd;
68 ebb4c247 2004-04-16 devnull struct winsize ows;
71 ebb4c247 2004-04-16 devnull updatewinsize(int row, int col, int dx, int dy)
73 ebb4c247 2004-04-16 devnull struct winsize ws;
75 ebb4c247 2004-04-16 devnull ws.ws_row = row;
76 ebb4c247 2004-04-16 devnull ws.ws_col = col;
77 ebb4c247 2004-04-16 devnull ws.ws_xpixel = dx;
78 ebb4c247 2004-04-16 devnull ws.ws_ypixel = dy;
79 ebb4c247 2004-04-16 devnull if(ws.ws_row != ows.ws_row || ws.ws_col != ows.ws_col)
80 ebb4c247 2004-04-16 devnull if(ioctl(rcfd, TIOCSWINSZ, &ws) < 0)
81 ebb4c247 2004-04-16 devnull fprint(2, "ioctl: %r\n");
82 ebb4c247 2004-04-16 devnull ows = ws;
85 ebb4c247 2004-04-16 devnull static struct termios ttmode;
88 ebb4c247 2004-04-16 devnull isecho(int fd)
90 ebb4c247 2004-04-16 devnull if(tcgetattr(fd, &ttmode) < 0)
91 ebb4c247 2004-04-16 devnull fprint(2, "tcgetattr: %r\n");
92 ebb4c247 2004-04-16 devnull if(debug) fprint(2, "israw %c%c\n",
93 ebb4c247 2004-04-16 devnull ttmode.c_lflag&ICANON ? 'c' : '-',
94 ebb4c247 2004-04-16 devnull ttmode.c_lflag&ECHO ? 'e' : '-');
95 ebb4c247 2004-04-16 devnull return ttmode.c_lflag&ECHO;
99 ebb4c247 2004-04-16 devnull setecho(int fd, int newe)
101 ebb4c247 2004-04-16 devnull int old;
103 ebb4c247 2004-04-16 devnull if(tcgetattr(fd, &ttmode) < 0)
104 ebb4c247 2004-04-16 devnull fprint(2, "tcgetattr: %r\n");
105 ebb4c247 2004-04-16 devnull old = ttmode.c_lflag & ECHO;
106 ebb4c247 2004-04-16 devnull if(old != newe){
107 ebb4c247 2004-04-16 devnull ttmode.c_lflag &= ~ECHO;
108 ebb4c247 2004-04-16 devnull ttmode.c_lflag |= newe;
110 99c75f3c 2004-04-20 devnull * I tried using TCSADRAIN here, but that causes
111 99c75f3c 2004-04-20 devnull * hangs if there is any output waiting for us.
112 99c75f3c 2004-04-20 devnull * I guess TCSADRAIN is intended for use by our
113 99c75f3c 2004-04-20 devnull * clients, not by us.
115 99c75f3c 2004-04-20 devnull if(tcsetattr(fd, 0, &ttmode) < 0)
116 ebb4c247 2004-04-16 devnull fprint(2, "tcsetattr: %r\n");
118 ebb4c247 2004-04-16 devnull return old;