diff --git a/kernel/Makefile b/kernel/Makefile index 94517a5a..fc419f62 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -3,9 +3,10 @@ default: kernel.bin CC=x86_64-elf-g++ AS=x86_64-elf-as +THOR_FLAGS=-DCONFIG_HISTORY=y WARNING_FLAGS=-Wall -Wextra -pedantic -Wold-style-cast -Wshadow CPP_FLAGS=-masm=intel -Iinclude/ -nostdlib -Os -std=c++11 -fno-exceptions -fno-rtti -ffreestanding -mno-red-zone -mno-mmx -mno-sse -mno-sse2 -mno-sse3 -mno-3dnow $(WARNING_FLAGS) -KERNEL_FLAGS=$(CPP_FLAGS) +KERNEL_FLAGS=$(CPP_FLAGS) $(THOR_FLAGS) KERNEL_LINK_FLAGS=-lgcc -T linker.ld $(CPP_FLAGS) KERNEL_CPP_FILES=$(wildcard src/*.cpp) diff --git a/kernel/src/shell.cpp b/kernel/src/shell.cpp index 8cb9d96f..048dc1c2 100644 --- a/kernel/src/shell.cpp +++ b/kernel/src/shell.cpp @@ -23,8 +23,14 @@ namespace { +#ifdef CONFIG_HISTORY +static constexpr bool History = true; +#else +static constexpr bool History = false; +#endif + vector history; -uint64_t history_index; +uint64_t history_index = 0; bool shift = false; @@ -79,6 +85,59 @@ char current_input[50]; void exec_command(); +template +void history_key(char key){ + if(history.size() > 0){ + if(key == keyboard::KEY_UP){ + if(history_index == 0){ + return; + } + + --history_index; + } else { //KEY_DOWN + if(history_index == history.size()){ + return; + } + + ++history_index; + } + + set_column(6); + + for(uint64_t i = 0; i < current_input_length; ++i){ + k_print(' '); + } + + set_column(6); + + current_input_length = 0; + + if(history_index < history.size()){ + auto saved = history[history_index]; + while(*saved){ + current_input[current_input_length++] = *saved; + k_print(*saved); + + ++saved; + } + } + } +} + +template<> void history_key(char){} + +template +void history_save(){ + auto saved = new char[current_input_length + 1]; + memcopy(saved, current_input, current_input_length); + saved[current_input_length] = '\0'; + + history.push_back(saved); + history_index = history.size(); +} + +template<> void history_save(){} + void start_shell(){ while(true){ auto key = keyboard::get_char(); @@ -112,41 +171,7 @@ void start_shell(){ } else if(key == keyboard::KEY_LEFT_SHIFT || key == keyboard::KEY_RIGHT_SHIFT){ shift = true; } else if(key == keyboard::KEY_UP || key == keyboard::KEY_DOWN){ - if(history.size() > 0){ - if(key == keyboard::KEY_UP){ - if(history_index == 0){ - continue; - } - - --history_index; - } else { //KEY_DOWN - if(history_index == history.size()){ - continue; - } - - ++history_index; - } - - set_column(6); - - for(uint64_t i = 0; i < current_input_length; ++i){ - k_print(' '); - } - - set_column(6); - - current_input_length = 0; - - if(history_index < history.size()){ - auto saved = history[history_index]; - while(*saved){ - current_input[current_input_length++] = *saved; - k_print(*saved); - - ++saved; - } - } - } + history_key(key); } else if(key == keyboard::KEY_BACKSPACE){ if(current_input_length > 0){ k_print('\b'); @@ -172,12 +197,7 @@ void start_shell(){ void exec_command(){ char buffer[50]; - auto saved = new char[current_input_length + 1]; - memcopy(saved, current_input, current_input_length); - saved[current_input_length] = '\0'; - - history.push_back(saved); - history_index = history.size(); + history_save(); for(auto& command : commands){ const char* input_command = current_input; @@ -495,7 +515,6 @@ void cd_command(const vector& params){ void init_shell(){ current_input_length = 0; - history_index = 0; wipeout();