Blob


1 #include <u.h>
2 #include <termios.h>
3 #include <stropts.h>
4 #include <libc.h>
5 #include "term.h"
7 #define debug 0
9 int
10 getpts(int fd[], char *slave)
11 {
12 fd[1] = open("/dev/ptmx", ORDWR);
13 if ((grantpt(fd[1]) < 0) || (unlockpt(fd[1]) < 0))
14 return -1;
15 fchmod(fd[1], 0622);
17 strcpy(slave, ptsname(fd[1]));
19 fd[0] = open(slave, ORDWR);
20 if(fd[0] < 0)
21 sysfatal("open %s: %r\n", slave);
23 /* set up the right streams modules for a tty */
24 ioctl(fd[0], I_PUSH, "ptem"); /* push ptem */
25 ioctl(fd[0], I_PUSH, "ldterm"); /* push ldterm */
27 return 0;
28 }
30 int
31 childpty(int fd[], char *slave)
32 {
33 int sfd;
35 close(fd[1]);
36 setsid();
37 sfd = open(slave, ORDWR);
38 if(sfd < 0)
39 sysfatal("open %s: %r\n", slave);
40 return sfd;
41 }
43 struct winsize ows;
45 void
46 updatewinsize(int row, int col, int dx, int dy)
47 {
48 struct winsize ws;
50 ws.ws_row = row;
51 ws.ws_col = col;
52 ws.ws_xpixel = dx;
53 ws.ws_ypixel = dy;
54 if(ws.ws_row != ows.ws_row || ws.ws_col != ows.ws_col)
55 if(ioctl(rcfd, TIOCSWINSZ, &ws) < 0)
56 fprint(2, "ioctl TIOCSWINSZ: %r\n");
57 ows = ws;
58 }
60 static struct termios ttmode;
62 int
63 isecho(int fd)
64 {
65 if(tcgetattr(fd, &ttmode) < 0)
66 fprint(2, "tcgetattr: %r\n");
67 if(debug) fprint(2, "israw %c%c\n",
68 ttmode.c_lflag&ICANON ? 'c' : '-',
69 ttmode.c_lflag&ECHO ? 'e' : '-');
70 return (ttmode.c_lflag&ICANON && ttmode.c_lflag&ECHO);
71 }
73 int
74 setecho(int fd, int newe)
75 {
76 int old;
78 if(tcgetattr(fd, &ttmode) < 0)
79 fprint(2, "tcgetattr: %r\n");
80 old = (ttmode.c_lflag&ECHO)==ECHO;
81 if(old != newe){
82 if(newe)
83 ttmode.c_lflag |= ECHO;
84 else
85 ttmode.c_lflag &= ~ECHO;
86 if(tcsetattr(fd, TCSANOW, &ttmode) < 0)
87 fprint(2, "tcsetattr: %r\n");
88 }
89 return old;
90 }