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