From 397d9448eb9afa0eeb2eebb531ff23ded0eb728d Mon Sep 17 00:00:00 2001
From: Tim Buchner <twometer@outlook.de>
Date: Wed, 27 Jan 2021 02:04:51 +0100
Subject: [PATCH] Crappy but working interactive shell :3

---
 kernel/device/keyboard.cpp | 10 +++----
 kernel/kernel.cpp          |  2 +-
 libc/string/string.c       |  4 +--
 userland/shell/main.cpp    | 57 +++++++++++++-------------------------
 4 files changed, 27 insertions(+), 46 deletions(-)

diff --git a/kernel/device/keyboard.cpp b/kernel/device/keyboard.cpp
index f6549a0..8eee641 100644
--- a/kernel/device/keyboard.cpp
+++ b/kernel/device/keyboard.cpp
@@ -18,7 +18,7 @@ void Keyboard::Initialize()
     NewScancode(5, '4');
     NewScancode(6, '5');
     NewScancode(7, '6');
-    NewScancode(8, '7');
+    NewScancode(8, '/');
     NewScancode(9, '8');
     NewScancode(10, '9');
     NewScancode(11, '0');
@@ -92,15 +92,15 @@ size_t Keyboard::ReadUntil(char *dst, size_t maxSize, char delim)
     if (idx < 0)
         return 0;
 
-    idx++; // include the delim itself
-    
-    auto bufSize = buf.Size();
-    auto size = bufSize > maxSize ? maxSize : bufSize;
+    auto size = idx > maxSize ? maxSize : idx;
 
     for (size_t i = 0; i < size; i++)
         dst[i] = buf[i];
 
     buf.RemoveBlock(0, size);
+
+    if (buf.Size() > 0 && buf[0] == '\n')
+        buf.Remove(0);
     return size;
 }
 
diff --git a/kernel/kernel.cpp b/kernel/kernel.cpp
index ab5aca8..0a9441e 100644
--- a/kernel/kernel.cpp
+++ b/kernel/kernel.cpp
@@ -67,7 +67,7 @@ extern "C"
 		// Banner
 		TTY::Clear();
 		TTY::SetColor(0x9f);
-		printf("nekosys 0.04\n");
+		printf("nekosys 0.05\n");
 		kdbg("Booting koneko kernel...\n");
 		TTY::SetColor(0x07);
 
diff --git a/libc/string/string.c b/libc/string/string.c
index d05a543..ae478aa 100644
--- a/libc/string/string.c
+++ b/libc/string/string.c
@@ -13,7 +13,7 @@ int streq(const char *a, const char *b)
 	size_t alen = strlen(a);
 	size_t blen = strlen(b);
 	if (alen != blen)
-		return 0;
+		return 1;
 
-	return memcmp(a, b, alen) != 0;
+	return memcmp(a, b, alen);
 }
\ No newline at end of file
diff --git a/userland/shell/main.cpp b/userland/shell/main.cpp
index 729d3c8..e764677 100644
--- a/userland/shell/main.cpp
+++ b/userland/shell/main.cpp
@@ -1,51 +1,32 @@
 #include <stdio.h>
 #include <stdint.h>
 #include <unistd.h>
+#include <string.h>
 #include <nekosys.h>
 
 int main(int argc, char **argv)
 {
-	printf("Opening file...\n");
-	FILE *file = fopen("/home/neko/test.txt", "r");
-	printf("opened\n");
+	char *buf = new char[512];
 
-	fseek(file, 0, SEEK_END);
-	size_t filesize = ftell(file);
-	fseek(file, 0, SEEK_SET);
-	printf("Test file has size %d.\n", filesize);
-
-	uint8_t *data = new uint8_t[filesize + 1];
-	data[filesize] = 0;
-
-	fread(data, 1, filesize, file);
-
-	printf("File test contents: %s\n", data);
-
-	fclose(file);
-	delete[] data;
-
-	printf("sleeping for 5 secs...\n");
-	//sleep(5);
-	printf("yay, we're back\n");
-
-	printf("Testing spawning\n");
-	pid_t p = 0;
-	int result = spawnp(&p, "/bin/hlwrld.app", nullptr, nullptr);
-	if (result)
-	{
-		printf("spawn failed\n");
-	}
-	else
+	for (;;)
 	{
-		printf("spawned process as %d\n", p);
-
-		if (waitp(p))
-			printf("wait failed\n");
+		printf("neko:/ $ "); // Prompt
+
+		size_t read = readln(buf, 512); // Command
+		buf[read] = 0x00;
+
+		if (streq(buf, "exit") == 0)
+		{
+			break;
+		}
+
+		pid_t pid = 0;
+		if (spawnp(&pid, buf, nullptr, nullptr))
+		{
+			printf("nsh: error: Cannot spawn\n");
+		}
+		waitp(pid);
 	}
 
-	printf("reading from console...\n");
-	char *buf = new char[256];
-	readln(buf, 256);
-	printf("read: %s\n", buf);
 	return 0;
 }
\ No newline at end of file