summaryrefslogtreecommitdiff
path: root/src/cmd/init/driver/termcook.c
blob: e0edcf51914a95a5a4621a8cb9bad9a716f09b4c (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
#include "driver.h"
#include <camellia.h>
#include <camellia/compat.h>
#include <camellia/flags.h>
#include <camellia/fs/dir.h>
#include <camellia/fs/misc.h>
#include <camellia/syscalls.h>
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <thread.h>
#include <unistd.h>

enum tstate {
	Normal,
	Esc,
	CSI,
};

enum handles {
	Hroot,
	Htokill,
};

typedef struct Tokill Tokill;
struct Tokill {
	Tokill *next;
	char path[]; /* NUL-terminated */
};
static Tokill *tokill = NULL;
static hid_t stdin_pipe[2];

static void w_output(hid_t output, const char *buf, size_t len) {
	size_t pos = 0;
	while (pos < len) {
		int ret = _sys_write(output, buf + pos, len - pos, pos, 0);
		if (ret < 0) break;
		pos += ret;
	}
}

static void send_intr(char *intr) {
	for (Tokill *it = tokill; it; it = it->next) {
		int fd = camellia_open(it->path, OPEN_WRITE);
		// TODO remove dead from list
		if (fd < 0) continue;
		_sys_write(fd, intr, strlen(intr), -1, 0);
		close(fd);
	}
}

static void line_editor(void *) {
	char readbuf[16], linebuf[256];
	size_t linepos = 0;
	enum tstate state = Normal;
	hid_t input = 0;
	hid_t output = stdin_pipe[1];
	for (;;) {
		int readlen = _sys_read(input, readbuf, sizeof readbuf, -1);
		if (readlen < 0) {
			errno = -readlen;
			err(1, "read");
			return;
		}
		for (int i = 0; i < readlen; i++) {
			char c = readbuf[i];
			switch (state) {
			case Normal:
				switch (c) {
				case '\b':
				case 0x7f:
					if (linepos != 0) {
						printf("\b \b");
						linepos--;
					}
					break;
				case 3: /* C-c */
					send_intr("");
					break;
				case 0x1c: /* C-\ */
					send_intr("kill");
					break;
				case 4: /* EOT, C-d */
					if (linepos > 0) {
						w_output(output, linebuf, linepos);
						linepos = 0;
					} else {
						_sys_write(output, NULL, 0, 0, 0); /* EOF */
					}
					break;
				case '\n':
				case '\r':
					printf("\n");
					if (linepos < sizeof linebuf)
						linebuf[linepos++] = '\n';
					w_output(output, linebuf, linepos);
					linepos = 0;
					break;
				case '\e':
					state = Esc;
					break;
				case '\t':
					break;
				default:
					if (linepos < sizeof linebuf) {
						linebuf[linepos++] = c;
						printf("%c", c);
					}
					break;
				}
				break;
			case Esc:
				if (c == '[') state = CSI;
				else state = Normal;
				break;
			case CSI:
				if (0x40 <= c && c <= 0x7E) state = Normal;
				break;
			}
		}
	}
}

static void fs(void *) {
	const size_t buflen = 1024;
	char *buf = malloc(buflen);
	int pipefd = stdin_pipe[0];
	if (!buf) err(1, "malloc");
	for (;;) {
		struct ufs_request req;
		hid_t reqh = ufs_wait(buf, buflen, &req);
		int id = (int)req.id;
		if (reqh < 0) errx(1, "ufs_wait error");

		if (req.op == VFSOP_OPEN) {
			if (strcmp(buf, "/") == 0) {
				_sys_fs_respond(reqh, (void*)Hroot, 0, 0);
			} else if (strcmp(buf, "/tokill") == 0) {
				_sys_fs_respond(reqh, (void*)Htokill, 0, 0);
			} else if (strcmp(buf, "/stdin") == 0) {
				_sys_fs_respond(reqh, NULL, pipefd, FSR_DELEGATE);
			} else {
				_sys_fs_respond(reqh, NULL, -ENOENT, 0);
			}
		} else if (id == Hroot && (req.op == VFSOP_READ || req.op == VFSOP_GETSIZE)) {
			struct dirbuild db;
			char *target = req.op == VFSOP_READ ? buf : NULL;
			dir_start(&db, req.offset, target, buflen);
			dir_append(&db, "stdin");
			_sys_fs_respond(reqh, target, dir_finish(&db), 0);
		} else if (id == Htokill && req.op == VFSOP_WRITE) {
			// alternatively, pass fd?
			Tokill *head = calloc(sizeof(Tokill) + req.len + 1, 1);
			if (!head) {
				_sys_fs_respond(reqh, NULL, -ENOMEM, 0);
				continue;
			}
			memcpy(head->path, buf, req.len);
			head->next = tokill;
			tokill = head;
			_sys_fs_respond(reqh, NULL, req.len, 0);
		} else {
			_sys_fs_respond(reqh, NULL, -ENOSYS, 0);
		}
	}
}

// TODO turn into a separate binary
void termcook(void) {
	if (_sys_pipe(stdin_pipe, 0) < 0)
		err(1, "pipe");

	thread_create(0, fs, NULL);
	thread_create(0, line_editor, NULL);
	_sys_await();
	exit(1);
}