blob: 5b4f48c019211dde80498734b4a128194904d692 [file] [log] [blame]
rsc8ad51792004-03-25 23:03:57 +00001#include <u.h>
rsc4dcd9af2004-04-15 02:04:30 +00002#include <signal.h>
rscb4a659b2004-04-19 23:03:46 +00003#include <libc.h>
rsc8ad51792004-03-25 23:03:57 +00004#include "term.h"
5
rscc6687d42004-09-21 01:11:28 +00006static void
rsc60535a52004-12-26 21:37:31 +00007sys(char *buf, int devnull)
rscc6687d42004-09-21 01:11:28 +00008{
9 char buf2[100];
10 char *f[20];
11 int nf, pid;
12
rsc60535a52004-12-26 21:37:31 +000013 notedisable("sys: child");
rscc6687d42004-09-21 01:11:28 +000014 strcpy(buf2, buf);
15 nf = tokenize(buf2, f, nelem(f));
16 f[nf] = nil;
17 switch(pid = fork()){
18 case 0:
rsc60535a52004-12-26 21:37:31 +000019 close(1);
20 open("/dev/null", OREAD);
21 close(2);
22 open("/dev/null", OREAD);
rscc6687d42004-09-21 01:11:28 +000023 execvp(f[0], f);
rsc60535a52004-12-26 21:37:31 +000024 _exit(2);
rscc6687d42004-09-21 01:11:28 +000025 default:
26 waitpid();
27 }
28}
29
rsc8ad51792004-03-25 23:03:57 +000030int
rsc4dcd9af2004-04-15 02:04:30 +000031rcstart(int argc, char **argv, int *pfd, int *tfd)
rsc8ad51792004-03-25 23:03:57 +000032{
rsca2705f22004-04-16 15:27:29 +000033 int fd[2], i, pid;
rsc8ad51792004-03-25 23:03:57 +000034 char *xargv[3];
35 char slave[256];
36 int sfd;
37
38 if(argc == 0){
39 argc = 2;
40 argv = xargv;
41 argv[0] = getenv("SHELL");
42 if(argv[0] == 0)
43 argv[0] = "rc";
44 argv[1] = "-i";
45 argv[2] = 0;
46 }
47 /*
48 * fd0 is slave (tty), fd1 is master (pty)
49 */
50 fd[0] = fd[1] = -1;
rsc60535a52004-12-26 21:37:31 +000051 if(getpts(fd, slave) < 0){
52 exit(3);
rsc75024f02004-03-26 00:09:27 +000053 sysfatal("getpts: %r\n");
rsc60535a52004-12-26 21:37:31 +000054 }
rsc55d360f2005-07-13 03:53:17 +000055 /*
56 * notedisable("sys: window size change");
57 *
58 * Can't disable because will be inherited by other programs
59 * like if you run an xterm from the prompt, and then xterm's
rsc1a24aac2005-07-21 15:43:51 +000060 * resizes won't get handled right. Sigh.
61 *
62 * Can't not disable because when we stty below we'll get a
63 * signal, which will drop us into the thread library note handler,
64 * which will get all confused because we just forked and thus
65 * have an unknown pid.
66 *
67 * So disable it internally. ARGH!
rsc55d360f2005-07-13 03:53:17 +000068 */
rsc1a24aac2005-07-21 15:43:51 +000069 notifyoff("sys: window size change");
70
rsc60535a52004-12-26 21:37:31 +000071 pid = fork();
72 switch(pid){
rsc8ad51792004-03-25 23:03:57 +000073 case 0:
74 putenv("TERM", "9term");
75 sfd = childpty(fd, slave);
76 dup(sfd, 0);
77 dup(sfd, 1);
78 dup(sfd, 2);
rsc60535a52004-12-26 21:37:31 +000079 sys("stty tabs -onlcr icanon echo erase '^h' intr '^?'", 0);
80 sys("stty onocr", 1); /* not available on mac */
rscb4a659b2004-04-19 23:03:46 +000081 if(noecho)
rsc60535a52004-12-26 21:37:31 +000082 sys("stty -echo", 0);
rsca2705f22004-04-16 15:27:29 +000083 for(i=3; i<100; i++)
84 close(i);
rscc6687d42004-09-21 01:11:28 +000085 signal(SIGINT, SIG_DFL);
86 signal(SIGHUP, SIG_DFL);
87 signal(SIGTERM, SIG_DFL);
rsc8ad51792004-03-25 23:03:57 +000088 execvp(argv[0], argv);
89 fprint(2, "exec %s failed: %r\n", argv[0]);
rsc60535a52004-12-26 21:37:31 +000090 _exit(2);
rsc8ad51792004-03-25 23:03:57 +000091 break;
92 case -1:
93 sysfatal("proc failed: %r");
94 break;
95 }
rsc8ad51792004-03-25 23:03:57 +000096 *pfd = fd[1];
rsca2705f22004-04-16 15:27:29 +000097 close(fd[0]);
98 if(tfd){
99 if((*tfd = open(slave, OREAD)) < 0)
100 sysfatal("parent open %s: %r", slave);
101 }
rsc8ad51792004-03-25 23:03:57 +0000102 return pid;
103}
104