From 6152d11ae205d4b4a9f03574cfcb0c24cb54b4b5 Mon Sep 17 00:00:00 2001
From: dzwdz
Date: Thu, 7 Apr 2022 23:13:00 +0200
Subject: init: two concurrent shells - serial & vga/ps2

---
 src/init/driver/ansiterm.c | 27 ++++++++++-----------------
 src/init/fs/misc.c         | 17 +++++++----------
 src/init/main.c            | 18 ++++++++++++------
 src/init/shell.c           |  6 +++---
 src/init/stdlib.c          |  5 +++--
 src/init/stdlib.h          |  2 +-
 6 files changed, 36 insertions(+), 39 deletions(-)

(limited to 'src')

diff --git a/src/init/driver/ansiterm.c b/src/init/driver/ansiterm.c
index 5b05af1..115744b 100644
--- a/src/init/driver/ansiterm.c
+++ b/src/init/driver/ansiterm.c
@@ -8,17 +8,18 @@ struct vga_cell {
 } __attribute__((__packed__));
 static struct vga_cell vga[80 * 25];
 
-static handle_t tty_fd;
 static handle_t vga_fd;
 
 static struct {int x, y;} cursor = {0};
 static bool dirty = false;
 static bool pendingFlush = false;
 
-/* TODO unflushed writes mix with the kernel's vga driver */
-
 static void flush(void) {
-	_syscall_write(vga_fd, vga, sizeof vga, 0);
+	size_t off = 0;
+	/* we have to do multiple write() calls if we're behind a shitty passthrough fs
+	 * i don't like this either */
+	while (off < sizeof(vga))
+		off += _syscall_write(vga_fd, (void*)vga + off, sizeof(vga) - off, off);
 	dirty = false;
 	pendingFlush = false;
 }
@@ -55,9 +56,6 @@ static void in_char(char c) {
 }
 
 void ansiterm_drv(void) {
-	tty_fd = _syscall_open("/tty", 4);
-	_syscall_write(tty_fd, "ansiterm...\n", 12, 0);
-
 	vga_fd = _syscall_open("/vga", 4);
 	_syscall_read(vga_fd, vga, sizeof vga, 0);
 
@@ -71,6 +69,10 @@ void ansiterm_drv(void) {
 	}
 	cursor.x = 0;
 
+	for (int i = 0; i < 80 * 25; i++)
+		vga[i].style = 0x70;
+	flush();
+
 	static char buf[512];
 	struct fs_wait_response res;
 	while (!_syscall_fs_wait(buf, sizeof buf, &res)) {
@@ -81,21 +83,12 @@ void ansiterm_drv(void) {
 				break;
 
 			case VFSOP_WRITE:
-				_syscall_write(tty_fd, buf, res.len, 0);
 				for (int i = 0; i < res.len; i++)
 					in_char(buf[i]);
-				if (pendingFlush) flush();
+				/* if (pendingFlush) */ flush();
 				_syscall_fs_respond(NULL, res.len);
 				break;
 
-			case VFSOP_READ:
-				if (res.capacity > sizeof buf)
-					res.capacity = sizeof buf;
-				if (dirty) flush();
-				size_t len = _syscall_read(tty_fd, buf, res.capacity, 0);
-				_syscall_fs_respond(buf, len);
-				break;
-
 			default:
 				_syscall_fs_respond(NULL, -1);
 				break;
diff --git a/src/init/fs/misc.c b/src/init/fs/misc.c
index ef064a2..2f02e09 100644
--- a/src/init/fs/misc.c
+++ b/src/init/fs/misc.c
@@ -47,14 +47,13 @@ static void fs_respond_delegate(struct fs_wait_response *res, handle_t delegate,
 	 *          ```
 	 */
 	static char buf[1024];
-	int buf_size  = 1024;
 	int size;
 	int ret;
 
 	switch (res->op) {
 		case VFSOP_READ:
 			// TODO instead of truncating the size, allocate a bigger buffer
-			size = res->capacity < buf_size ? res->capacity : buf_size;
+			size = res->capacity < sizeof(buf) ? res->capacity : sizeof(buf);
 			ret = _syscall_read(delegate, buf, size, res->offset);
 			_syscall_fs_respond(buf, ret);
 			break;
@@ -74,17 +73,16 @@ static void fs_respond_delegate(struct fs_wait_response *res, handle_t delegate,
 
 void fs_passthru(const char *prefix) {
 	struct fs_wait_response res;
-	int buf_size = 64; // TODO just use sizeof...
-	char buf[      64];
+	static char buf[1024];
 	int ret, prefix_len;
 	if (prefix) prefix_len = strlen(prefix);
 
-	while (!_syscall_fs_wait(buf, buf_size, &res)) {
+	while (!_syscall_fs_wait(buf, sizeof(buf), &res)) {
 		switch (res.op) {
 			case VFSOP_OPEN:
 				if (prefix) {
 					/* special case: rewriting the path */
-					if (prefix_len + res.len <= buf_size) {
+					if (prefix_len + res.len <= sizeof(buf)) {
 						// TODO memmove
 						char tmp[64];
 						memcpy(tmp, buf, res.len);
@@ -117,11 +115,10 @@ void fs_dir_inject(const char *path) {
 	struct fs_wait_response res;
 	struct fs_dir_handle handles[16]; // TODO hardcoded FD_MAX - use malloc instead
 	int handle_next = 0;
-	int buf_size = 64;
-	char buf[      64];
+	static char buf[1024];
 	int ret;
 
-	while (!_syscall_fs_wait(buf, buf_size, &res)) {
+	while (!_syscall_fs_wait(buf, sizeof buf, &res)) {
 		switch (res.op) {
 			case VFSOP_OPEN:
 				if (handle_next > 15) _syscall_fs_respond(NULL, -2); // we ran out of handles, which is entirely our fault.
@@ -153,7 +150,7 @@ void fs_dir_inject(const char *path) {
 					buf[out_len++] = '\0';
 
 					if (h.delegate >= 0) {
-						int to_read = res.capacity < buf_size ? res.capacity : buf_size;
+						int to_read = res.capacity < sizeof(buf) ? res.capacity : sizeof(buf);
 						to_read -= out_len;
 						ret = _syscall_read(h.delegate, buf + out_len, to_read, 0);
 						if (ret > 0) out_len += ret;
diff --git a/src/init/main.c b/src/init/main.c
index 661ee64..5b106df 100644
--- a/src/init/main.c
+++ b/src/init/main.c
@@ -30,24 +30,30 @@ int main(void) {
 	MOUNT("/bind", fs_passthru(NULL));
 
 	if (!_syscall_fork()) {
-		__tty_fd = _syscall_open(argify("/com1"));
-		if (__tty_fd < 0) _syscall_exit(1);
+		__stdin = __stdout = _syscall_open(argify("/com1"));
+		if (__stdout < 0) _syscall_exit(1);
 
 		shell_loop();
 		_syscall_exit(1);
 	}
 
 	if (!_syscall_fork()) {
-		__tty_fd = _syscall_open(argify("/vga_tty"));
-		if (__tty_fd < 0) _syscall_exit(1);
+		__stdout = _syscall_open(argify("/vga_tty"));
+		if (__stdout < 0) _syscall_exit(1);
+
+		__stdin = _syscall_open(argify("/keyboard"));
+		if (__stdin < 0) {
+			printf("couldn't open /keyboard\n");
+			_syscall_exit(1);
+		}
 
 		shell_loop();
 		_syscall_exit(1);
 	}
 
 	// try to find any working output
-	__tty_fd = _syscall_open(argify("/com1"));
-	if (__tty_fd < 0) __tty_fd = _syscall_open(argify("/vga_tty"));
+	__stdout = _syscall_open(argify("/com1"));
+	if (__stdout < 0) __stdout = _syscall_open(argify("/vga_tty"));
 
 	for (;;) {
 		_syscall_await();
diff --git a/src/init/shell.c b/src/init/shell.c
index 39db99f..6314721 100644
--- a/src/init/shell.c
+++ b/src/init/shell.c
@@ -18,7 +18,7 @@ static char *split(char *base) {
 static int readline(char *buf, size_t max) {
 	char c;
 	size_t pos = 0;
-	while (_syscall_read(__tty_fd, &c, 1, 0)) {
+	while (_syscall_read(__stdin, &c, 1, 0)) {
 		switch (c) {
 			case '\b':
 			case 0x7f:
@@ -35,7 +35,7 @@ static int readline(char *buf, size_t max) {
 				return pos;
 			default:
 				if (pos < max) {
-					_syscall_write(__tty_fd, &c, 1, 0);
+					_syscall_write(__stdout, &c, 1, 0);
 					buf[pos] = c;
 					pos++;
 				}
@@ -74,7 +74,7 @@ static void cmd_cat_ls(const char *args, bool ls) {
 		for (int i = 0; i < len; i++)
 			if (buf[i] == '\0') buf[i] = '\n';
 
-	_syscall_write(__tty_fd, buf, len, 0);
+	_syscall_write(__stdout, buf, len, 0);
 	_syscall_close(fd);
 }
 
diff --git a/src/init/stdlib.c b/src/init/stdlib.c
index 839b8f5..095be2e 100644
--- a/src/init/stdlib.c
+++ b/src/init/stdlib.c
@@ -2,7 +2,8 @@
 #include <shared/syscalls.h>
 #include <stdarg.h>
 
-int __tty_fd;
+int __stdin  = -1;
+int __stdout = -1;
 
 
 int printf(const char *fmt, ...) {
@@ -10,7 +11,7 @@ int printf(const char *fmt, ...) {
 	int total = 0;
 	va_list argp;
 	va_start(argp, fmt);
-	if (__tty_fd < 0) return 0;
+	if (__stdout < 0) return 0;
 
 	for (;;) {
 		char c = *fmt++;
diff --git a/src/init/stdlib.h b/src/init/stdlib.h
index 17ec0b9..1d7efdf 100644
--- a/src/init/stdlib.h
+++ b/src/init/stdlib.h
@@ -2,6 +2,6 @@
 #include <shared/mem.h>
 #include <stddef.h>
 
-extern int __tty_fd;
+extern int __stdin, __stdout;
 
 int printf(const char *fmt, ...);
-- 
cgit v1.2.3