aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSyndamia <kamen.d.mladenov@protonmail.com>2021-09-02 08:44:13 +0300
committerSyndamia <kamen.d.mladenov@protonmail.com>2021-09-02 08:44:13 +0300
commit740efa6658c17158058f161b0230b7718b6bd051 (patch)
treebab8e9c1e8e65dad2428e51a5fa3a410daba1acf
parentf5e2da80d26c7ed8381cb31c2fde5aae67269952 (diff)
downloadyou86-740efa6658c17158058f161b0230b7718b6bd051.tar
you86-740efa6658c17158058f161b0230b7718b6bd051.tar.gz
you86-740efa6658c17158058f161b0230b7718b6bd051.zip
(1) Added current implementations: shell input handling
-rw-r--r--.gitignore54
-rw-r--r--commandHandler.c13
-rw-r--r--commandHandler.h13
-rw-r--r--commandRouter.c20
-rw-r--r--defaultCommands.c26
-rw-r--r--main.c35
6 files changed, 161 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..a782c01
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,54 @@
+# Taken from: https://github.com/github/gitignore/blob/master/C.gitignore
+
+# Prerequisites
+*.d
+
+# Object files
+*.o
+*.ko
+*.obj
+*.elf
+
+# Linker output
+*.ilk
+*.map
+*.exp
+
+# Precompiled Headers
+*.gch
+*.pch
+
+# Libraries
+*.lib
+*.a
+*.la
+*.lo
+
+# Shared objects (inc. Windows DLLs)
+*.dll
+*.so
+*.so.*
+*.dylib
+
+# Executables
+*.exe
+*.out
+*.app
+*.i*86
+*.x86_64
+*.hex
+
+# Debug files
+*.dSYM/
+*.su
+*.idb
+*.pdb
+
+# Kernel Module Compile Results
+*.mod*
+*.cmd
+.tmp_versions/
+modules.order
+Module.symvers
+Mkfile.old
+dkms.conf
diff --git a/commandHandler.c b/commandHandler.c
new file mode 100644
index 0000000..9d158f3
--- /dev/null
+++ b/commandHandler.c
@@ -0,0 +1,13 @@
+#include "commandHandler.h"
+
+struct CommandHandler handlers[MAX_HANDLERS];
+
+int handlerCount = 0;
+
+void registerHandler(char *name, F_EXECUTOR executor) {
+ struct CommandHandler newCH;
+ newCH.p_name = name;
+ newCH.p_executor = executor;
+
+ handlers[handlerCount++] = newCH;
+}
diff --git a/commandHandler.h b/commandHandler.h
new file mode 100644
index 0000000..d344abb
--- /dev/null
+++ b/commandHandler.h
@@ -0,0 +1,13 @@
+#ifndef COMMAND_HANDLER_HEADER
+#define COMMAND_HANDLER_HEADER
+
+#define MAX_HANDLERS 32
+
+typedef void (*F_EXECUTOR)(char *);
+
+struct CommandHandler {
+ char *p_name;
+ F_EXECUTOR p_executor;
+};
+
+#endif
diff --git a/commandRouter.c b/commandRouter.c
new file mode 100644
index 0000000..3d41ddd
--- /dev/null
+++ b/commandRouter.c
@@ -0,0 +1,20 @@
+#include <stdio.h>
+#include <string.h>
+#include <stdbool.h>
+#include <strings.h>
+#include "commandHandler.h"
+
+extern struct CommandHandler handlers[MAX_HANDLERS];
+extern int handlerCount;
+
+bool route(char *command) {
+ command = strtok(command, " ");
+
+ for (int i = 0; i < handlerCount; i++) {
+ if (strcasecmp(handlers[i].p_name, command) == 0) {
+ handlers[i].p_executor(command);
+ return true;
+ }
+ }
+ return false;
+}
diff --git a/defaultCommands.c b/defaultCommands.c
new file mode 100644
index 0000000..cd3442e
--- /dev/null
+++ b/defaultCommands.c
@@ -0,0 +1,26 @@
+#include "commandHandler.h"
+#include <stdio.h>
+
+void help(char *args) {
+ printf("This is the help message");
+}
+
+void about(char *args) {
+ printf("This is the about message");
+}
+
+void version(char *args) {
+ printf("This is the version message");
+}
+
+extern void registerHandler(char *name, F_EXECUTOR executor);
+
+void registerDefaultHandlers() {
+ registerHandler("help", &help);
+ registerHandler("h", &help);
+ registerHandler("?", &help);
+ registerHandler("about", &about);
+ registerHandler("a", &about);
+ registerHandler("version", &version);
+ registerHandler("v", &version);
+}
diff --git a/main.c b/main.c
new file mode 100644
index 0000000..3f2bba6
--- /dev/null
+++ b/main.c
@@ -0,0 +1,35 @@
+#include <bits/posix2_lim.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <limits.h>
+#include <string.h>
+
+#define SHELL_STRING "> "
+
+extern void registerDefaultHandlers();
+void printDefault();
+extern bool route(char *command);
+
+int main() {
+ char buffer[LINE_MAX];
+ buffer[0] = 0;
+
+ registerDefaultHandlers();
+
+ printf("Welcome!\n\n");
+ do {
+ // Removes the Line Feed character at the "end", if it exists
+ char *toChange = strchr(buffer, 0x0A);
+ if (toChange != NULL)
+ toChange[0] = 0;
+
+ if (buffer[0] > 0)
+ if (route(buffer))
+ printf("\n");
+
+ printf(SHELL_STRING);
+ } while (fgets(buffer, LINE_MAX, stdin));
+
+ return 0;
+}