diff options
| -rw-r--r-- | .gitignore | 54 | ||||
| -rw-r--r-- | commandHandler.c | 13 | ||||
| -rw-r--r-- | commandHandler.h | 13 | ||||
| -rw-r--r-- | commandRouter.c | 20 | ||||
| -rw-r--r-- | defaultCommands.c | 26 | ||||
| -rw-r--r-- | main.c | 35 |
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); +} @@ -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; +} |
