aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/commandHandler.c13
-rw-r--r--src/commandHandler.h13
-rw-r--r--src/commandRouter.c20
-rw-r--r--src/defaultCommands.c26
-rw-r--r--src/main.c35
5 files changed, 107 insertions, 0 deletions
diff --git a/src/commandHandler.c b/src/commandHandler.c
new file mode 100644
index 0000000..9d158f3
--- /dev/null
+++ b/src/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/src/commandHandler.h b/src/commandHandler.h
new file mode 100644
index 0000000..d344abb
--- /dev/null
+++ b/src/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/src/commandRouter.c b/src/commandRouter.c
new file mode 100644
index 0000000..3d41ddd
--- /dev/null
+++ b/src/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/src/defaultCommands.c b/src/defaultCommands.c
new file mode 100644
index 0000000..cd3442e
--- /dev/null
+++ b/src/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/src/main.c b/src/main.c
new file mode 100644
index 0000000..3f2bba6
--- /dev/null
+++ b/src/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;
+}