diff options
author | dzwdz | 2022-03-27 17:49:54 +0200 |
---|---|---|
committer | dzwdz | 2022-03-27 17:49:54 +0200 |
commit | f1cefa851c3bc8aa6a8c2e6b86d23d87db342a2d (patch) | |
tree | ba5a67cbf7a60f19bbe6a2ca83dedf2132b991ba | |
parent | 2cd4a60b2f8dd3fc5196fe97c6f1882e2e8ebe0d (diff) |
init: userland tty emulator ; also workaround a fork() bug
-rw-r--r-- | src/init/ansiterm.c | 95 | ||||
-rw-r--r-- | src/init/main.c | 10 | ||||
-rw-r--r-- | src/kernel/syscalls.c | 8 |
3 files changed, 106 insertions, 7 deletions
diff --git a/src/init/ansiterm.c b/src/init/ansiterm.c new file mode 100644 index 0000000..c66d39b --- /dev/null +++ b/src/init/ansiterm.c @@ -0,0 +1,95 @@ +#include <shared/syscalls.h> + +struct vga_cell { + unsigned char c; + unsigned char style; +} __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 void flush(void) { + _syscall_write(vga_fd, vga, sizeof vga, 0); +} + +static void scroll(void) { + for (size_t i = 0; i < 80 * 24; i++) + vga[i] = vga[i + 80]; + for (size_t i = 80 * 24; i < 80 * 25; i++) + vga[i].c = ' '; + cursor.y--; +} + +static void in_char(char c) { + switch (c) { + case '\n': + cursor.x = 0; + cursor.y++; + break; + case '\b': + if (--cursor.x < 0) cursor.x = 0; + break; + default: + vga[cursor.y * 80 + cursor.x++].c = c; + } + + if (cursor.x >= 80) { + cursor.x = 0; + cursor.y++; + } + while (cursor.y >= 25) scroll(); +} + +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); + + // find first empty line + for (cursor.y = 0; cursor.y < 25; cursor.y++) { + for (cursor.x = 0; cursor.x < 80; cursor.x++) { + char c = vga[cursor.y * 80 + cursor.x].c; + if (c != ' ' && c != '\0') break; + } + if (cursor.x == 80) break; + } + cursor.x = 0; + + static char buf[512]; + struct fs_wait_response res; + while (!_syscall_fs_wait(buf, sizeof buf, &res)) { + switch (res.op) { + case VFSOP_OPEN: + // TODO check path + _syscall_fs_respond(NULL, 1); + break; + + case VFSOP_WRITE: + _syscall_write(tty_fd, buf, res.len, 0); + for (int i = 0; i < res.len; i++) + in_char(buf[i]); + flush(); + _syscall_fs_respond(NULL, res.len); + break; + + case VFSOP_READ: + if (res.capacity > sizeof buf) + res.capacity = sizeof buf; + size_t len = _syscall_read(tty_fd, buf, res.capacity, 0); + _syscall_fs_respond(buf, len); + break; + + default: + _syscall_fs_respond(NULL, -1); + break; + } + } + + _syscall_exit(1); +} diff --git a/src/init/main.c b/src/init/main.c index ba5fdcd..1b9c7ed 100644 --- a/src/init/main.c +++ b/src/init/main.c @@ -17,21 +17,19 @@ extern char _initrd; void read_file(const char *path, size_t len); void fs_prep(void); +void ansiterm_drv(void); // TODO header file + __attribute__((section(".text.startup"))) int main(void) { // allocate bss _syscall_memflag(&_bss_start, &_bss_end - &_bss_start, MEMFLAG_PRESENT); + if (!fork2_n_mount("/tty")) ansiterm_drv(); + __tty_fd = _syscall_open("/tty", sizeof("/tty") - 1); if (__tty_fd < 0) _syscall_exit(1); - // change screen color - handle_t vga = _syscall_open(argify("/vga")); - for (int i = 1; i < 80*25*2; i += 2) - _syscall_write(vga, "\x4f", 1, i); - _syscall_close(vga); - fs_prep(); shell_loop(); diff --git a/src/kernel/syscalls.c b/src/kernel/syscalls.c index f41aabf..faa7ccc 100644 --- a/src/kernel/syscalls.c +++ b/src/kernel/syscalls.c @@ -179,7 +179,13 @@ handle_t _syscall_fs_fork2(void) { * also there's this whole thing with handling errors here properly and * errno * TODO figure this out */ - if (front == 0) panic_unimplemented(); + if (front == 0) { + // dumb hack + front = process_find_handle(process_current); + if (front < 0) return -1; + process_current->handles[front].type = HANDLE_FS_FRONT; + process_current->handles[0].type = HANDLE_EMPTY; + } backend = kmalloc(sizeof *backend); // TODO never freed backend->type = VFS_BACK_USER; |