summaryrefslogtreecommitdiff
path: root/src/init/shell.c
diff options
context:
space:
mode:
authordzwdz2021-10-10 15:31:18 +0000
committerdzwdz2021-10-10 15:31:18 +0000
commitba69471edd732efa8a9b435299b82bfc482e0756 (patch)
treea5f51bcd98bfdf52e9545b7a5620a9dd5899195a /src/init/shell.c
parentd9463f6e977ce686ac7a55f22b0b25b0ce67b025 (diff)
init/shell: basic readline()
Diffstat (limited to 'src/init/shell.c')
-rw-r--r--src/init/shell.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/init/shell.c b/src/init/shell.c
new file mode 100644
index 0000000..d61f46f
--- /dev/null
+++ b/src/init/shell.c
@@ -0,0 +1,44 @@
+#include <init/shell.h>
+#include <init/stdlib.h>
+#include <shared/syscalls.h>
+
+#define PROMPT "$ "
+
+static int tty_fd = 0; // TODO put in stdlib
+
+static int readline(char *buf, size_t max) {
+ char c;
+ size_t pos = 0;
+ while (_syscall_read(tty_fd, &c, 1, 0)) {
+ switch (c) {
+ case '\b':
+ case 0x7f:
+ /* for some reason backspace outputs 0x7f (DEL) */
+ if (pos != 0) {
+ printf("\b \b");
+ pos--;
+ }
+ break;
+ case '\r':
+ printf("\n");
+ buf[pos++] = '\0';
+ return pos;
+ default:
+ if (pos < max) {
+ _syscall_write(tty_fd, &c, 1, 0);
+ buf[pos] = c;
+ pos++;
+ }
+ }
+ }
+ return -1; // error
+}
+
+void shell_loop(void) {
+ static char cmd[256];
+ for (;;) {
+ printf(PROMPT);
+ readline(cmd, 256);
+ printf(" %s\n", cmd);
+ }
+}