Blob


1 #include <u.h>
2 #include <signal.h>
3 #include <libc.h>
4 #include "term.h"
6 int loginshell;
8 static void
9 sys(char *buf, int devnull)
10 {
11 char buf2[100];
12 char *f[20];
13 int nf, pid;
15 notedisable("sys: child");
16 strcpy(buf2, buf);
17 nf = tokenize(buf2, f, nelem(f));
18 f[nf] = nil;
19 switch(pid = fork()){
20 case 0:
21 close(1);
22 open("/dev/null", OREAD);
23 close(2);
24 open("/dev/null", OREAD);
25 execvp(f[0], f);
26 _exit(2);
27 default:
28 waitpid();
29 }
30 }
32 int
33 rcstart(int argc, char **argv, int *pfd, int *tfd)
34 {
35 int fd[2], i, pid;
36 char *cmd, *xargv[3];
37 char slave[256];
38 int sfd;
40 if(argc == 0){
41 argc = 2;
42 argv = xargv;
43 argv[0] = getenv("SHELL");
44 if(argv[0] == 0)
45 argv[0] = "rc";
46 argv[1] = "-i";
47 argv[2] = 0;
48 }
49 cmd = argv[0];
50 if(loginshell){
51 argv[0] = malloc(strlen(cmd)+2);
52 strcpy(argv[0]+1, cmd);
53 argv[0][0] = '-';
54 }
56 /*
57 * fd0 is slave (tty), fd1 is master (pty)
58 */
59 fd[0] = fd[1] = -1;
60 if(getpts(fd, slave) < 0){
61 exit(3);
62 sysfatal("getpts: %r\n");
63 }
64 /*
65 * notedisable("sys: window size change");
66 *
67 * Can't disable because will be inherited by other programs
68 * like if you run an xterm from the prompt, and then xterm's
69 * resizes won't get handled right. Sigh.
70 *
71 * Can't not disable because when we stty below we'll get a
72 * signal, which will drop us into the thread library note handler,
73 * which will get all confused because we just forked and thus
74 * have an unknown pid.
75 *
76 * So disable it internally. ARGH!
77 */
78 notifyoff("sys: window size change");
80 // This used to be TERM=9term but we don't know of anything that cares.
81 // Worse, various cc have started assuming that TERM != dumb implies
82 // the ability to understand ANSI escape codes. 9term will squelch them
83 // but acme win does not.
84 putenv("TERM", "dumb");
86 // Set $termprog to 9term or win for those who care about what kind of
87 // dumb terminal this is.
88 putenv("termprog", (char*)termprog);
90 pid = fork();
91 switch(pid){
92 case 0:
93 sfd = childpty(fd, slave);
94 dup(sfd, 0);
95 dup(sfd, 1);
96 dup(sfd, 2);
97 sys("stty tabs -onlcr icanon echo erase '^h' intr '^?'", 0);
98 sys("stty onocr", 1); /* not available on mac */
99 for(i=3; i<100; i++)
100 close(i);
101 signal(SIGINT, SIG_DFL);
102 signal(SIGHUP, SIG_DFL);
103 signal(SIGTERM, SIG_DFL);
104 execvp(cmd, argv);
105 fprint(2, "exec %s failed: %r\n", argv[0]);
106 _exit(2);
107 break;
108 case -1:
109 sysfatal("proc failed: %r");
110 break;
112 *pfd = fd[1];
113 close(fd[0]);
114 if(tfd){
115 if((*tfd = open(slave, OREAD)) < 0)
116 sysfatal("parent open %s: %r", slave);
118 return pid;
121 struct {
122 Lock l;
123 char buf[1<<20];
124 int r, w;
125 } echo;
127 void
128 echoed(char *p, int n)
130 lock(&echo.l);
131 if(echo.r > 0) {
132 memmove(echo.buf, echo.buf+echo.r, echo.w-echo.r);
133 echo.w -= echo.r;
134 echo.r = 0;
136 if(echo.w+n > sizeof echo.buf)
137 echo.r = echo.w = 0;
138 if(echo.w+n > sizeof echo.buf)
139 n = 0;
140 memmove(echo.buf+echo.w, p, n);
141 echo.w += n;
142 unlock(&echo.l);
145 int
146 echocancel(char *p, int n)
148 int i;
150 lock(&echo.l);
151 for(i=0; i<n; i++) {
152 if(echo.r < echo.w) {
153 if(echo.buf[echo.r] == p[i]) {
154 echo.r++;
155 continue;
157 if(echo.buf[echo.r] == '\n' && p[i] == '\r')
158 continue;
159 if(p[i] == 0x08) {
160 if(i+2 <= n && p[i+1] == ' ' && p[i+2] == 0x08)
161 i += 2;
162 continue;
165 echo.r = echo.w;
166 break;
168 unlock(&echo.l);
169 if(i > 0)
170 memmove(p, p+i, n-i);
171 return n-i;
174 int
175 dropcrnl(char *p, int n)
177 char *r, *w;
179 for(r=w=p; r<p+n; r++) {
180 if(r+1<p+n && *r == '\r' && *(r+1) == '\n')
181 continue;
182 if(*r == 0x08) {
183 if(r+2<=p+n && *(r+1) == ' ' && *(r+2) == 0x08)
184 r += 2;
185 continue;
187 *w++ = *r;
189 return w-p;