From 8ef0249d88a07a42075cef387bb8e3def9f20cf0 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 24 Jun 2020 20:28:18 +0200 Subject: Shell: added some basic commands Added simple temp command manager. Added clear, halt, shutdown and reboot. --- src/pOS/arch/x86/kernel/shell.cpp | 46 ------------------------- src/pOS/arch/x86/kernel/shell/cmd.cpp | 33 ++++++++++++++++++ src/pOS/arch/x86/kernel/shell/cmds/clear.cpp | 9 +++++ src/pOS/arch/x86/kernel/shell/cmds/halt.cpp | 9 +++++ src/pOS/arch/x86/kernel/shell/cmds/reboot.cpp | 9 +++++ src/pOS/arch/x86/kernel/shell/cmds/shutdown.cpp | 9 +++++ src/pOS/arch/x86/kernel/shell/shell.cpp | 43 +++++++++++++++++++++++ src/pOS/include/kernel/kernel.h | 2 +- src/pOS/include/kernel/shell.h | 18 ---------- src/pOS/include/kernel/shell/cmd.h | 29 ++++++++++++++++ src/pOS/include/kernel/shell/shell.h | 19 ++++++++++ 11 files changed, 161 insertions(+), 65 deletions(-) delete mode 100644 src/pOS/arch/x86/kernel/shell.cpp create mode 100644 src/pOS/arch/x86/kernel/shell/cmd.cpp create mode 100644 src/pOS/arch/x86/kernel/shell/cmds/clear.cpp create mode 100644 src/pOS/arch/x86/kernel/shell/cmds/halt.cpp create mode 100644 src/pOS/arch/x86/kernel/shell/cmds/reboot.cpp create mode 100644 src/pOS/arch/x86/kernel/shell/cmds/shutdown.cpp create mode 100644 src/pOS/arch/x86/kernel/shell/shell.cpp delete mode 100644 src/pOS/include/kernel/shell.h create mode 100644 src/pOS/include/kernel/shell/cmd.h create mode 100644 src/pOS/include/kernel/shell/shell.h diff --git a/src/pOS/arch/x86/kernel/shell.cpp b/src/pOS/arch/x86/kernel/shell.cpp deleted file mode 100644 index b6e74be..0000000 --- a/src/pOS/arch/x86/kernel/shell.cpp +++ /dev/null @@ -1,46 +0,0 @@ -#include - -char Shell::prefix[10]; - -int Shell::init(void) -{ - Shell::set_prefix("pOS# "); - return run(); -} - -int Shell::run(void) /* TODO: implement args */ -{ - char c; - char cmd[20]; /* Mem management */ - int indx = 0; - printf("pOS# "); - while((c = getc()) && c != EOF) - { - putc(c); - - if(c == '\n') - { - cmd[indx] = '\0'; - indx = 0; - - handle_cmd(cmd); - printf(Shell::prefix); - } - else - cmd[indx++] = c; - } - - ASSERT(c == EOF); - return 0; -} - -int Shell::handle_cmd(const char* cmd) -{ - printf("Unknown cmd: %s\n", cmd); - return 0; -} - -void Shell::set_prefix(const char* new_prefix) -{ - strncpy(prefix, new_prefix, sizeof(prefix)); -} diff --git a/src/pOS/arch/x86/kernel/shell/cmd.cpp b/src/pOS/arch/x86/kernel/shell/cmd.cpp new file mode 100644 index 0000000..cc34f6e --- /dev/null +++ b/src/pOS/arch/x86/kernel/shell/cmd.cpp @@ -0,0 +1,33 @@ +#include +#include +#include + +#include + +int CMD_MANAGER::execute(char* cmd) +{ + //char* arg = strtok(cmd, " "); + + dbgprintf("cmd: %s\n", cmd); + + switch(str2int(cmd)) + { + case str2int("clear"): + case str2int("cls"): + case str2int("c"): + return CMD_Clear().execute(cmd); + + case str2int("halt"): + case str2int("hlt"): + return CMD_Halt().execute(cmd); + + case str2int("shutdown"): + return CMD_Shutdown().execute(cmd); + + case str2int("reboot"): + return CMD_Reboot().execute(cmd); + } + + printf("Unknown cmd: %s\n", cmd); + return -1; +} diff --git a/src/pOS/arch/x86/kernel/shell/cmds/clear.cpp b/src/pOS/arch/x86/kernel/shell/cmds/clear.cpp new file mode 100644 index 0000000..1919a3a --- /dev/null +++ b/src/pOS/arch/x86/kernel/shell/cmds/clear.cpp @@ -0,0 +1,9 @@ +#include +#include + +int CMD_Clear::execute(const char* args) +{ + UNUSED_VARIABLE(args); + TTY::tty_initialize(); + return 0; +} diff --git a/src/pOS/arch/x86/kernel/shell/cmds/halt.cpp b/src/pOS/arch/x86/kernel/shell/cmds/halt.cpp new file mode 100644 index 0000000..2993af3 --- /dev/null +++ b/src/pOS/arch/x86/kernel/shell/cmds/halt.cpp @@ -0,0 +1,9 @@ +#include +#include + +int CMD_Halt::execute(const char* args) +{ + UNUSED_VARIABLE(args); + ACPI::halt(); + return 0; +} diff --git a/src/pOS/arch/x86/kernel/shell/cmds/reboot.cpp b/src/pOS/arch/x86/kernel/shell/cmds/reboot.cpp new file mode 100644 index 0000000..bf2dac3 --- /dev/null +++ b/src/pOS/arch/x86/kernel/shell/cmds/reboot.cpp @@ -0,0 +1,9 @@ +#include +#include + +int CMD_Reboot::execute(const char* args) +{ + UNUSED_VARIABLE(args); + ACPI::reboot(); + return 0; +} diff --git a/src/pOS/arch/x86/kernel/shell/cmds/shutdown.cpp b/src/pOS/arch/x86/kernel/shell/cmds/shutdown.cpp new file mode 100644 index 0000000..e0939d9 --- /dev/null +++ b/src/pOS/arch/x86/kernel/shell/cmds/shutdown.cpp @@ -0,0 +1,9 @@ +#include +#include + +int CMD_Shutdown::execute(const char* args) +{ + UNUSED_VARIABLE(args); + ACPI::shutdown(); + return 0; +} diff --git a/src/pOS/arch/x86/kernel/shell/shell.cpp b/src/pOS/arch/x86/kernel/shell/shell.cpp new file mode 100644 index 0000000..9dc1fcd --- /dev/null +++ b/src/pOS/arch/x86/kernel/shell/shell.cpp @@ -0,0 +1,43 @@ +#include + +char Shell::prefix[10]; + +int Shell::init(void) +{ + Shell::set_prefix("pOS# "); + return run(); +} + +int Shell::run(void) +{ + char c; + char cmd[20]; /* Mem management */ + int indx = 0; + printf("pOS# "); + while((c = getc()) && c != EOF) + { + putc(c); + /* TODO: Handle signals */ + if(c == '\b') + indx--; + else if(c == '\n') + { + cmd[indx] = '\0'; + indx = 0; + + CMD_MANAGER::execute(cmd); + + printf(Shell::prefix); + } + else + cmd[indx++] = c; + } + + ASSERT(c == EOF); + return 0; +} + +void Shell::set_prefix(const char* new_prefix) +{ + strncpy(prefix, new_prefix, sizeof(prefix)); +} diff --git a/src/pOS/include/kernel/kernel.h b/src/pOS/include/kernel/kernel.h index 671bee6..cbe5f3d 100644 --- a/src/pOS/include/kernel/kernel.h +++ b/src/pOS/include/kernel/kernel.h @@ -5,7 +5,7 @@ #include #include -#include +#include #include #include #include diff --git a/src/pOS/include/kernel/shell.h b/src/pOS/include/kernel/shell.h deleted file mode 100644 index cb1cc4b..0000000 --- a/src/pOS/include/kernel/shell.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef _SHELL_H_ -#define _SHELL_H_ - -#include -#include - -class Shell -{ -public: - static int init(void); - static void set_prefix(const char* prefix); -private: - static int run(void); - static int handle_cmd(const char* cmd); - static char prefix[10]; -}; - -#endif diff --git a/src/pOS/include/kernel/shell/cmd.h b/src/pOS/include/kernel/shell/cmd.h new file mode 100644 index 0000000..48c3d58 --- /dev/null +++ b/src/pOS/include/kernel/shell/cmd.h @@ -0,0 +1,29 @@ +#ifndef _CMD_H_ +#define _CMD_H_ + +#define ADD_CMD(name) \ + class name : public CMD \ + { \ + public: \ + int execute(const char* args); \ + }; + +class CMD_MANAGER +{ +public: + static int execute(char* cmd); +}; + +class CMD +{ +public: + virtual int execute(const char* args); +}; + +/* SHELL CMDS */ +ADD_CMD(CMD_Clear); +ADD_CMD(CMD_Shutdown); +ADD_CMD(CMD_Reboot); +ADD_CMD(CMD_Halt); + +#endif diff --git a/src/pOS/include/kernel/shell/shell.h b/src/pOS/include/kernel/shell/shell.h new file mode 100644 index 0000000..ced7ca6 --- /dev/null +++ b/src/pOS/include/kernel/shell/shell.h @@ -0,0 +1,19 @@ +#ifndef _SHELL_H_ +#define _SHELL_H_ + +#include +#include +#include + +class Shell +{ +public: + static int init(void); + static void set_prefix(const char* prefix); +private: + static int run(void); + static int handle_cmd(const char* cmd); + static char prefix[10]; +}; + +#endif -- cgit 1.4.1