kernel+sh: Allow using Ctrl+D to send EOF
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
apio 2023-03-24 17:21:21 +01:00
parent a18e50ff34
commit f084b57f39
Signed by: apio
GPG Key ID: B8A7D06E42258954
4 changed files with 56 additions and 7 deletions

View File

@ -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;
}
}
}

View File

@ -3,5 +3,13 @@
namespace Keyboard
{
enum Modifiers
{
LeftControl = 1,
LeftAlt = 2,
};
Option<char> decode_scancode(u8 scancode);
int modifiers();
}

View File

@ -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<char> 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;
}
}

View File

@ -1,10 +1,12 @@
#include "fs/devices/ConsoleDevice.h"
#include "arch/Keyboard.h"
#include "video/TextConsole.h"
#include <luna/Buffer.h>
#include <luna/CString.h>
#include <luna/Vector.h>
static Buffer g_console_input;
static bool g_eof { false };
Result<SharedPtr<Device>> ConsoleDevice::create()
{
@ -21,6 +23,8 @@ Result<usize> 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<usize> 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<u8> 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')