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 noteenable("sys: child");
30 }
31 }
33 int
34 rcstart(int argc, char **argv, int *pfd, int *tfd)
35 {
36 int fd[2], i, pid;
37 char *cmd, *xargv[3];
38 char slave[256];
39 int sfd;
41 if(argc == 0){
42 argc = 2;
43 argv = xargv;
44 argv[0] = getenv("SHELL");
45 if(argv[0] == 0)
46 argv[0] = "rc";
47 argv[1] = "-i";
48 argv[2] = 0;
49 }
50 cmd = argv[0];
51 if(loginshell){
52 argv[0] = malloc(strlen(cmd)+2);
53 strcpy(argv[0]+1, cmd);
54 argv[0][0] = '-';
55 }
57 /*
58 * fd0 is slave (tty), fd1 is master (pty)
59 */
60 fd[0] = fd[1] = -1;
61 if(getpts(fd, slave) < 0){
62 exit(3);
63 sysfatal("getpts: %r\n");
64 }
65 /*
66 * notedisable("sys: window size change");
67 *
68 * Can't disable because will be inherited by other programs
69 * like if you run an xterm from the prompt, and then xterm's
70 * resizes won't get handled right. Sigh.
71 *
72 * Can't not disable because when we stty below we'll get a
73 * signal, which will drop us into the thread library note handler,
74 * which will get all confused because we just forked and thus
75 * have an unknown pid.
76 *
77 * So disable it internally. ARGH!
78 */
79 notifyoff("sys: window size change");
81 // This used to be TERM=9term but we don't know of anything that cares.
82 // Worse, various cc have started assuming that TERM != dumb implies
83 // the ability to understand ANSI escape codes. 9term will squelch them
84 // but acme win does not.
85 putenv("TERM", "dumb");
87 // Set $termprog to 9term or win for those who care about what kind of
88 // dumb terminal this is.
89 putenv("termprog", (char*)termprog);
91 pid = fork();
92 switch(pid){
93 case 0:
94 sfd = childpty(fd, slave);
95 dup(sfd, 0);
96 dup(sfd, 1);
97 dup(sfd, 2);
98 sys("stty tabs -onlcr icanon echo erase '^h' intr '^?'", 0);
99 sys("stty onocr", 1); /* not available on mac */
100 for(i=3; i<100; i++)
101 close(i);
102 signal(SIGINT, SIG_DFL);
103 signal(SIGHUP, SIG_DFL);
104 signal(SIGTERM, SIG_DFL);
105 execvp(cmd, argv);
106 fprint(2, "exec %s failed: %r\n", argv[0]);
107 _exit(2);
108 break;
109 case -1:
110 sysfatal("proc failed: %r");
111 break;
113 *pfd = fd[1];
114 close(fd[0]);
115 if(tfd){
116 if((*tfd = open(slave, OREAD)) < 0)
117 sysfatal("parent open %s: %r", slave);
119 return pid;
122 struct {
123 Lock l;
124 char buf[1<<20];
125 int r, w;
126 } echo;
128 void
129 echoed(char *p, int n)
131 lock(&echo.l);
132 if(echo.r > 0) {
133 memmove(echo.buf, echo.buf+echo.r, echo.w-echo.r);
134 echo.w -= echo.r;
135 echo.r = 0;
137 if(echo.w+n > sizeof echo.buf)
138 echo.r = echo.w = 0;
139 if(echo.w+n > sizeof echo.buf)
140 n = 0;
141 memmove(echo.buf+echo.w, p, n);
142 echo.w += n;
143 unlock(&echo.l);
146 int
147 echocancel(char *p, int n)
149 int i;
151 lock(&echo.l);
152 for(i=0; i<n; i++) {
153 if(echo.r < echo.w) {
154 if(echo.buf[echo.r] == p[i]) {
155 echo.r++;
156 continue;
158 if(echo.buf[echo.r] == '\n' && p[i] == '\r')
159 continue;
160 if(p[i] == 0x08) {
161 if(i+2 <= n && p[i+1] == ' ' && p[i+2] == 0x08)
162 i += 2;
163 continue;
166 echo.r = echo.w;
167 break;
169 unlock(&echo.l);
170 if(i > 0)
171 memmove(p, p+i, n-i);
172 return n-i;
175 int
176 dropcrnl(char *p, int n)
178 char *r, *w;
180 for(r=w=p; r<p+n; r++) {
181 if(r+1<p+n && *r == '\r' && *(r+1) == '\n')
182 continue;
183 if(*r == 0x08) {
184 if(r+2<=p+n && *(r+1) == ' ' && *(r+2) == 0x08)
185 r += 2;
186 continue;
188 *w++ = *r;
190 return w-p;