Blame


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