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 ebb4c247 2004-04-16 devnull #include "term.h"
15 ebb4c247 2004-04-16 devnull
16 ebb4c247 2004-04-16 devnull #define debug 0
17 ebb4c247 2004-04-16 devnull
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";
26 ebb4c247 2004-04-16 devnull
27 ebb4c247 2004-04-16 devnull int
28 ebb4c247 2004-04-16 devnull getpts(int fd[], char *slave)
29 ebb4c247 2004-04-16 devnull {
30 ebb4c247 2004-04-16 devnull char *a, *z;
31 ebb4c247 2004-04-16 devnull char pty[] = "/dev/ptyXX";
32 ebb4c247 2004-04-16 devnull
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)
39 ebb4c247 2004-04-16 devnull break;
40 ebb4c247 2004-04-16 devnull }else{
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]);
47 ebb4c247 2004-04-16 devnull }
48 ebb4c247 2004-04-16 devnull }
49 ebb4c247 2004-04-16 devnull sysfatal("no ptys");
50 ebb4c247 2004-04-16 devnull return 0;
51 ebb4c247 2004-04-16 devnull }
52 ebb4c247 2004-04-16 devnull
53 ebb4c247 2004-04-16 devnull int
54 ebb4c247 2004-04-16 devnull childpty(int fd[], char *slave)
55 ebb4c247 2004-04-16 devnull {
56 ebb4c247 2004-04-16 devnull int sfd;
57 ebb4c247 2004-04-16 devnull
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;
66 ebb4c247 2004-04-16 devnull }
67 ebb4c247 2004-04-16 devnull
68 ebb4c247 2004-04-16 devnull struct winsize ows;
69 ebb4c247 2004-04-16 devnull
70 ebb4c247 2004-04-16 devnull void
71 ebb4c247 2004-04-16 devnull updatewinsize(int row, int col, int dx, int dy)
72 ebb4c247 2004-04-16 devnull {
73 ebb4c247 2004-04-16 devnull struct winsize ws;
74 ebb4c247 2004-04-16 devnull
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;
83 ebb4c247 2004-04-16 devnull }
84 ebb4c247 2004-04-16 devnull
85 ebb4c247 2004-04-16 devnull static struct termios ttmode;
86 ebb4c247 2004-04-16 devnull
87 ebb4c247 2004-04-16 devnull int
88 ebb4c247 2004-04-16 devnull isecho(int fd)
89 ebb4c247 2004-04-16 devnull {
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;
96 ebb4c247 2004-04-16 devnull }
97 ebb4c247 2004-04-16 devnull
98 ebb4c247 2004-04-16 devnull int
99 ebb4c247 2004-04-16 devnull setecho(int fd, int newe)
100 ebb4c247 2004-04-16 devnull {
101 ebb4c247 2004-04-16 devnull int old;
102 ebb4c247 2004-04-16 devnull
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;
109 99c75f3c 2004-04-20 devnull /*
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.
114 99c75f3c 2004-04-20 devnull */
115 99c75f3c 2004-04-20 devnull if(tcsetattr(fd, 0, &ttmode) < 0)
116 ebb4c247 2004-04-16 devnull fprint(2, "tcsetattr: %r\n");
117 ebb4c247 2004-04-16 devnull }
118 ebb4c247 2004-04-16 devnull return old;
119 ebb4c247 2004-04-16 devnull }