From 16988ce0cb0ec3000564a9d40d138316a2d65f35 Mon Sep 17 00:00:00 2001 From: Baptiste Wicht Date: Mon, 28 Oct 2013 20:24:15 +0100 Subject: [PATCH] Refactorings --- Makefile | 5 +- kernel/include/console.hpp | 2 + kernel/include/shell.hpp | 6 ++ kernel/src/console.cpp | 3 + kernel/src/kernel.cpp | 106 +------------------------------ kernel/src/shell.cpp | 126 +++++++++++++++++++++++++++++++++++++ 6 files changed, 144 insertions(+), 104 deletions(-) create mode 100644 kernel/include/shell.hpp create mode 100644 kernel/src/shell.cpp diff --git a/Makefile b/Makefile index 678bd9d5..bffb661f 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ micro_kernel.bin: $(MICRO_KERNEL_SRC) $(MICRO_KERNEL_UTILS_SRC) KERNEL_FLAGS=-masm=intel -Ikernel/include/ -O1 -std=c++11 -Wall -Wextra -fno-exceptions -fno-rtti -ffreestanding KERNEL_LINK_FLAGS=-std=c++11 -T linker.ld -ffreestanding -O1 -nostdlib -KERNEL_O_FILES=kernel.o keyboard.o console.o kernel_utils.o timer.o +KERNEL_O_FILES=kernel.o keyboard.o console.o kernel_utils.o timer.o shell.o kernel.o: kernel/src/kernel.cpp g++ $(KERNEL_FLAGS) -c kernel/src/kernel.cpp -o kernel.o @@ -29,6 +29,9 @@ kernel_utils.o: kernel/src/kernel_utils.cpp timer.o: kernel/src/timer.cpp g++ $(KERNEL_FLAGS) -c kernel/src/timer.cpp -o timer.o +shell.o: kernel/src/shell.cpp + g++ $(KERNEL_FLAGS) -c kernel/src/shell.cpp -o shell.o + kernel.bin: $(KERNEL_O_FILES) g++ $(KERNEL_LINK_FLAGS) -o kernel.bin.o $(KERNEL_O_FILES) objcopy -R .note -R .comment -S -O binary kernel.bin.o kernel.bin diff --git a/kernel/include/console.hpp b/kernel/include/console.hpp index f26f506e..3d0af954 100644 --- a/kernel/include/console.hpp +++ b/kernel/include/console.hpp @@ -1,6 +1,8 @@ #ifndef CONSOLE_H #define CONSOLE_H +#include + void set_column(long column); long get_column(); diff --git a/kernel/include/shell.hpp b/kernel/include/shell.hpp new file mode 100644 index 00000000..5ac9f09d --- /dev/null +++ b/kernel/include/shell.hpp @@ -0,0 +1,6 @@ +#ifndef SHELL_H +#define SHELL_ + +void init_shell(); + +#endif diff --git a/kernel/src/console.cpp b/kernel/src/console.cpp index 819d237e..6b5ca775 100644 --- a/kernel/src/console.cpp +++ b/kernel/src/console.cpp @@ -94,6 +94,9 @@ void k_print(const char* string){ } void wipeout(){ + current_line = 0; + current_column = 0; + for(int line = 0; line < 25; ++line){ for(std::size_t column = 0; column < 80; ++column){ k_print(' '); diff --git a/kernel/src/kernel.cpp b/kernel/src/kernel.cpp index 52c2a075..9c219f65 100644 --- a/kernel/src/kernel.cpp +++ b/kernel/src/kernel.cpp @@ -1,118 +1,18 @@ -#include -#include - #include "types.hpp" #include "keyboard.hpp" #include "kernel_utils.hpp" #include "console.hpp" #include "timer.hpp" +#include "shell.hpp" void keyboard_handler(); +void clear_command(); extern "C" { void __attribute__ ((section ("main_section"))) kernel_main(){ - wipeout(); - - k_print("thor> "); - - register_irq_handler<1>(keyboard_handler); + init_shell(); install_timer(); return; } } - -std::size_t current_input_length = 0; -char current_input[50]; - -void exec_command(); - -void keyboard_handler(){ - uint8_t key = in_byte(0x60); - - if(key & 0x80){ - //TODO Handle shift - } else { - if(key == 0x1C){ - current_input[current_input_length] = '\0'; - - k_print_line(); - - exec_command(); - - current_input_length = 0; - - k_print("thor> "); - } else if(key == 0x0E){ - set_column(get_column() - 1); - k_print(' '); - set_column(get_column() - 1); - - --current_input_length; - } else { - auto qwertz_key = key_to_ascii(key); - - if(qwertz_key > 0){ - current_input[current_input_length++] = qwertz_key; - k_print(qwertz_key); - } - } - } -} - -bool str_equals(const char* a, const char* b){ - while(*a && *a == *b){ - ++a; - ++b; - } - - return *a == *b; -} - -struct command_definition { - const char* name; - void (*function)(); -}; - -void reboot_command(); -void help_command(); -void uptime_command(); - -std::array commands = {{ - {"reboot", reboot_command}, - {"help", help_command}, - {"uptime", uptime_command} -}}; - -void reboot_command(){ - interrupt<60>(); -} - -void help_command(){ - k_print_line("Available commands:"); - - for(auto& command : commands){ - k_print(" "); - k_print_line(command.name); - } -} - -void uptime_command(){ - k_print("Uptime: "); - k_print(timer_seconds()); - k_print_line("s"); -} - -void exec_command(){ - for(auto& command : commands){ - if(str_equals(current_input, command.name)){ - command.function(); - - return; - } - } - - k_print("The command \""); - k_print(current_input); - k_print_line("\" does not exist"); -} diff --git a/kernel/src/shell.cpp b/kernel/src/shell.cpp new file mode 100644 index 00000000..0dc2110c --- /dev/null +++ b/kernel/src/shell.cpp @@ -0,0 +1,126 @@ +#include +#include + +#include "types.hpp" +#include "keyboard.hpp" +#include "kernel_utils.hpp" +#include "console.hpp" +#include "shell.hpp" +#include "timer.hpp" + +namespace { + +//Declarations of the different functions + +void reboot_command(); +void help_command(); +void uptime_command(); +void clear_command(); + +std::size_t current_input_length = 0; +char current_input[50]; + +void exec_command(); + +void keyboard_handler(){ + uint8_t key = in_byte(0x60); + + if(key & 0x80){ + //TODO Handle shift + } else { + if(key == 0x1C){ + current_input[current_input_length] = '\0'; + + k_print_line(); + + exec_command(); + + current_input_length = 0; + + k_print("thor> "); + } else if(key == 0x0E){ + set_column(get_column() - 1); + k_print(' '); + set_column(get_column() - 1); + + --current_input_length; + } else { + auto qwertz_key = key_to_ascii(key); + + if(qwertz_key > 0){ + current_input[current_input_length++] = qwertz_key; + k_print(qwertz_key); + } + } + } +} + +bool str_equals(const char* a, const char* b){ + while(*a && *a == *b){ + ++a; + ++b; + } + + return *a == *b; +} + +struct command_definition { + const char* name; + void (*function)(); +}; + +std::array commands = {{ + {"reboot", reboot_command}, + {"help", help_command}, + {"uptime", uptime_command}, + {"clear", clear_command} +}}; + +void reboot_command(){ + interrupt<60>(); +} + +void help_command(){ + k_print_line("Available commands:"); + + for(auto& command : commands){ + k_print(" "); + k_print_line(command.name); + } +} + +void uptime_command(){ + k_print("Uptime: "); + k_print(timer_seconds()); + k_print_line("s"); +} + +void exec_command(){ + for(auto& command : commands){ + if(str_equals(current_input, command.name)){ + command.function(); + + return; + } + } + + k_print("The command \""); + k_print(current_input); + k_print_line("\" does not exist"); +} + +void clear_command(){ + wipeout(); +} + +} //end of anonymous namespace + +void init_shell(){ + current_input_length = 0; + + clear_command(); + + k_print("thor> "); + + register_irq_handler<1>(keyboard_handler); +}