aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/9term/rcstart.c
blob: 9bfefdaa08291b41cf939bebb2d620a234f2944f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include <u.h>
#include <signal.h>
#include <libc.h>
#include "term.h"

int loginshell;

static void
sys(char *buf, int devnull)
{
	char buf2[100];
	char *f[20];
	int nf, pid;

	notedisable("sys: child");
	strcpy(buf2, buf);
	nf = tokenize(buf2, f, nelem(f));
	f[nf] = nil;
	switch(pid = fork()){
	case 0:
		close(1);
		open("/dev/null", OREAD);
		close(2);
		open("/dev/null", OREAD);
		execvp(f[0], f);
		_exit(2);
	default:
		waitpid();
	}
}

int
rcstart(int argc, char **argv, int *pfd, int *tfd)
{
	int fd[2], i, pid;
	char *cmd, *xargv[3];
	char slave[256];
	int sfd;

	if(argc == 0){
		argc = 2;
		argv = xargv;
		argv[0] = getenv("SHELL");
		if(argv[0] == 0)
			argv[0] = "rc";
		argv[1] = "-i";
		argv[2] = 0;
	}
	cmd = argv[0];
	if(loginshell){
		argv[0] = malloc(strlen(cmd)+2);
		strcpy(argv[0]+1, cmd);
		argv[0][0] = '-';
	}

	/*
	 * fd0 is slave (tty), fd1 is master (pty)
	 */
	fd[0] = fd[1] = -1;
	if(getpts(fd, slave) < 0){
		exit(3);
		sysfatal("getpts: %r\n");
	}
	/*
	 * notedisable("sys: window size change");
	 * 
	 * Can't disable because will be inherited by other programs
	 * like if you run an xterm from the prompt, and then xterm's
	 * resizes won't get handled right.  Sigh.  
	 *
	 * Can't not disable because when we stty below we'll get a
	 * signal, which will drop us into the thread library note handler,
	 * which will get all confused because we just forked and thus
	 * have an unknown pid. 
	 *
	 * So disable it internally.  ARGH!
	 */
	notifyoff("sys: window size change");

	// This used to be TERM=9term but we don't know of anything that cares.
	// Worse, various cc have started assuming that TERM != dumb implies
	// the ability to understand ANSI escape codes. 9term will squelch them
	// but acme win does not.
	putenv("TERM", "dumb");

	// Set $termprog to 9term or win for those who care about what kind of
	// dumb terminal this is.
	putenv("termprog", (char*)termprog);

	pid = fork();
	switch(pid){
	case 0:
		sfd = childpty(fd, slave);
		dup(sfd, 0);
		dup(sfd, 1);
		dup(sfd, 2);
		sys("stty tabs -onlcr icanon echo erase '^h' intr '^?'", 0);
		sys("stty onocr", 1);	/* not available on mac */
		for(i=3; i<100; i++)
			close(i);
		signal(SIGINT, SIG_DFL);
		signal(SIGHUP, SIG_DFL);
		signal(SIGTERM, SIG_DFL);
		execvp(cmd, argv);
		fprint(2, "exec %s failed: %r\n", argv[0]);
		_exit(2);
		break;
	case -1:
		sysfatal("proc failed: %r");
		break;
	}
	*pfd = fd[1];
	close(fd[0]);
	if(tfd){
		if((*tfd = open(slave, OREAD)) < 0)
			sysfatal("parent open %s: %r", slave);
	}
	return pid;
}

struct {
	Lock l;
	char buf[1<<20];
	int r, w;
} echo;

void
echoed(char *p, int n)
{
	lock(&echo.l);
	if(echo.r > 0) {
		memmove(echo.buf, echo.buf+echo.r, echo.w-echo.r);
		echo.w -= echo.r;
		echo.r = 0;
	}
	if(echo.w+n > sizeof echo.buf)
		echo.r = echo.w = 0;
	if(echo.w+n > sizeof echo.buf)
		n = 0;
	memmove(echo.buf+echo.w, p, n);
	echo.w += n;	
	unlock(&echo.l);
}

int
echocancel(char *p, int n)
{
	int i;

	lock(&echo.l);
	for(i=0; i<n; i++) {
		if(echo.r < echo.w) {
			if(echo.buf[echo.r] == p[i]) {
				echo.r++;
				continue;
			}
			if(echo.buf[echo.r] == '\n' && p[i] == '\r')
				continue;
			if(p[i] == 0x08) {
				if(i+2 <= n && p[i+1] == ' ' && p[i+2] == 0x08)
					i += 2;
				continue;
			}
		}
		echo.r = echo.w;
		break;
	}
	unlock(&echo.l);
	if(i > 0)
		memmove(p, p+i, n-i);
	return n-i;
}

int
dropcrnl(char *p, int n)
{
	char *r, *w;

	for(r=w=p; r<p+n; r++) {
		if(r+1<p+n && *r == '\r' && *(r+1) == '\n')
			continue;
		if(*r == 0x08) {
			if(r+2<=p+n && *(r+1) == ' ' && *(r+2) == 0x08)
				r += 2;
			continue;
		}
		*w++ = *r;
	}
	return w-p;
}