summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/user/app/main.c2
-rw-r--r--src/user/app/shell.c31
-rw-r--r--src/user/driver/driver.h2
-rw-r--r--src/user/driver/termcook.c67
4 files changed, 77 insertions, 25 deletions
diff --git a/src/user/app/main.c b/src/user/app/main.c
index f87133e..ff47d4e 100644
--- a/src/user/app/main.c
+++ b/src/user/app/main.c
@@ -63,6 +63,7 @@ int main(void) {
printf("couldn't open /kdev/com1\n");
_syscall_exit(1);
}
+ termcook();
shell_loop();
_syscall_exit(1);
@@ -78,6 +79,7 @@ int main(void) {
printf("couldn't open /keyboard\n");
_syscall_exit(1);
}
+ termcook();
shell_loop();
_syscall_exit(1);
diff --git a/src/user/app/shell.c b/src/user/app/shell.c
index 4f062d9..3a08af4 100644
--- a/src/user/app/shell.c
+++ b/src/user/app/shell.c
@@ -31,33 +31,14 @@ static char *strtrim(char *s) {
}
+// TODO fgets
static int readline(char *buf, size_t max) {
- char c;
+ char c = '\0';
size_t pos = 0;
- while (file_read(stdin, &c, 1)) {
- switch (c) {
- case '\b':
- case 0x7f:
- /* for some reason backspace outputs 0x7f (DEL) */
- if (pos != 0) {
- printf("\b \b");
- pos--;
- }
- break;
- case '\n':
- case '\r':
- printf("\n");
- buf[pos++] = '\0';
- return pos;
- default:
- if (pos < max) {
- printf("%c", c);
- buf[pos] = c;
- pos++;
- }
- }
- }
- return -1; // error
+ while (pos < (max-1) && c != '\n' && file_read(stdin, &c, 1))
+ buf[pos++] = c;
+ buf[pos++] = '\0';
+ return pos;
}
static void cmd_cat_ls(const char *args, bool ls) {
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]);
+}