diff --git a/apps/sh.c b/apps/sh.c index 1b689f37..a61a4188 100644 --- a/apps/sh.c +++ b/apps/sh.c @@ -82,10 +82,15 @@ int main() fputs("sh$ ", stdout); char command[4096]; - fgets(command, sizeof(command), stdin); + char* rc = fgets(command, sizeof(command), stdin); + if (!rc) return 0; pid_t child = fork(); - if (child < 0) { perror("fork"); } + if (child < 0) + { + perror("fork"); + return 1; + } if (child == 0) { @@ -96,6 +101,9 @@ int main() return 1; } - if (waitpid(child, NULL, 0) < 0) perror("waitpid"); + if (waitpid(child, NULL, 0) < 0) + { + perror("waitpid"); + return 1; + } } -} diff --git a/kernel/src/arch/Keyboard.h b/kernel/src/arch/Keyboard.h index de9e9370..0fbdba67 100644 --- a/kernel/src/arch/Keyboard.h +++ b/kernel/src/arch/Keyboard.h @@ -3,5 +3,13 @@ namespace Keyboard { + enum Modifiers + { + LeftControl = 1, + LeftAlt = 2, + }; + Option decode_scancode(u8 scancode); + + int modifiers(); } diff --git a/kernel/src/arch/x86_64/Keyboard.cpp b/kernel/src/arch/x86_64/Keyboard.cpp index 8ce2723c..6488cd63 100644 --- a/kernel/src/arch/x86_64/Keyboard.cpp +++ b/kernel/src/arch/x86_64/Keyboard.cpp @@ -25,8 +25,7 @@ constexpr u8 F12 = 0x58; static bool should_ignore_key(u8 scancode) { - return (scancode > 0x3A && scancode < 0x47) || scancode == LEFT_CONTROL || scancode == TAB || - scancode == LEFT_ALT || scancode == F11 || scancode == F12; + return (scancode > 0x3A && scancode < 0x47) || scancode == TAB || scancode == F11 || scancode == F12; } static bool g_ignore_next { false }; @@ -124,6 +123,8 @@ static bool is_shifted() return g_left_shift || g_right_shift; } +int g_modifiers = 0; + namespace Keyboard { Option decode_scancode(u8 scancode) @@ -161,6 +162,22 @@ namespace Keyboard return {}; } + if (scancode == LEFT_ALT) + { + if (released) g_modifiers &= ~LeftAlt; + else + g_modifiers |= LeftAlt; + return {}; + } + + if (scancode == LEFT_CONTROL) + { + if (released) g_modifiers &= ~LeftControl; + else + g_modifiers |= LeftControl; + return {}; + } + if (should_ignore_key(scancode)) return {}; if (released) return {}; @@ -168,4 +185,9 @@ namespace Keyboard if (is_shifted()) return shifted_key_table[scancode]; return key_table[scancode]; } + + int modifiers() + { + return g_modifiers; + } } diff --git a/kernel/src/fs/devices/ConsoleDevice.cpp b/kernel/src/fs/devices/ConsoleDevice.cpp index 88dfe5df..cffb29f1 100644 --- a/kernel/src/fs/devices/ConsoleDevice.cpp +++ b/kernel/src/fs/devices/ConsoleDevice.cpp @@ -1,10 +1,12 @@ #include "fs/devices/ConsoleDevice.h" +#include "arch/Keyboard.h" #include "video/TextConsole.h" #include #include #include static Buffer g_console_input; +static bool g_eof { false }; Result> ConsoleDevice::create() { @@ -21,6 +23,8 @@ Result ConsoleDevice::read(u8* buf, usize, usize length) const g_console_input.try_resize(g_console_input.size() - length).release_value(); + if (!length && g_eof) g_eof = false; + return length; } @@ -32,7 +36,7 @@ Result ConsoleDevice::write(const u8* buf, usize, usize length) bool ConsoleDevice::blocking() const { - return g_console_input.size() == 0; + return g_eof ? false : g_console_input.size() == 0; } static Vector g_temp_input; @@ -46,6 +50,13 @@ void ConsoleDevice::did_press_key(char key) return; } + // Ctrl+D + if (key == 'd' && (Keyboard::modifiers() & Keyboard::LeftControl)) + { + g_eof = true; + return; + } + g_temp_input.try_append((u8)key).value(); if (key == '\n')