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