diff options
Diffstat (limited to 'src/user/driver')
-rw-r--r-- | src/user/driver/driver.h | 2 | ||||
-rw-r--r-- | src/user/driver/termcook.c | 67 |
2 files changed, 69 insertions, 0 deletions
diff --git a/src/user/driver/driver.h b/src/user/driver/driver.h index 69c4529..d6e7343 100644 --- a/src/user/driver/driver.h +++ b/src/user/driver/driver.h @@ -3,3 +3,5 @@ void ansiterm_drv(void); void ps2_drv(void); void tmpfs_drv(void); + +void termcook(void); diff --git a/src/user/driver/termcook.c b/src/user/driver/termcook.c new file mode 100644 index 0000000..6ed8223 --- /dev/null +++ b/src/user/driver/termcook.c @@ -0,0 +1,67 @@ +#include <shared/syscalls.h> +#include <user/lib/stdlib.h> +#include <user/driver/driver.h> + +static void w_output(handle_t output, const char *buf, size_t len) { + size_t pos = 0; + while (pos < len) { + int ret = _syscall_write(output, buf + pos, len - pos, pos); + if (ret < 0) break; + pos += ret; + } +} + +static void line_editor(handle_t input, handle_t output) { + char readbuf[16], linebuf[256]; + size_t linepos = 0; + for (;;) { + int readlen = _syscall_read(input, readbuf, sizeof readbuf, -1); + if (readlen < 0) return; + for (int i = 0; i < readlen; i++) { + char c = readbuf[i]; + switch (c) { + case '\b': + case 0x7f: + if (linepos != 0) { + printf("\b \b"); + linepos--; + } + break; + case 4: /* EOT, C-d */ + w_output(output, linebuf, linepos); + linepos = 0; + break; + case '\n': + case '\r': + printf("\n"); + if (linepos < sizeof linebuf) + linebuf[linepos++] = '\n'; + w_output(output, linebuf, linepos); + linepos = 0; + break; + default: + if (linepos < sizeof linebuf) { + linebuf[linepos++] = c; + printf("%c", c); + } + break; + } + } + } +} + +void termcook(void) { + handle_t stdin_pipe[2] = {-1, -1}; + if (_syscall_pipe(stdin_pipe, 0) < 0) + return; + + if (!fork()) { + close(stdin_pipe[0]); + line_editor(0, stdin_pipe[1]); + _syscall_exit(0); + } + /* 0 is stdin, like in unix */ + _syscall_dup(stdin_pipe[0], 0, 0); + close(stdin_pipe[0]); + close(stdin_pipe[1]); +} |