kernel+sh: Allow using Ctrl+D to send EOF
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
parent
a18e50ff34
commit
f084b57f39
14
apps/sh.c
14
apps/sh.c
@ -82,10 +82,15 @@ int main()
|
|||||||
fputs("sh$ ", stdout);
|
fputs("sh$ ", stdout);
|
||||||
|
|
||||||
char command[4096];
|
char command[4096];
|
||||||
fgets(command, sizeof(command), stdin);
|
char* rc = fgets(command, sizeof(command), stdin);
|
||||||
|
if (!rc) return 0;
|
||||||
|
|
||||||
pid_t child = fork();
|
pid_t child = fork();
|
||||||
if (child < 0) { perror("fork"); }
|
if (child < 0)
|
||||||
|
{
|
||||||
|
perror("fork");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
if (child == 0)
|
if (child == 0)
|
||||||
{
|
{
|
||||||
@ -96,6 +101,9 @@ int main()
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (waitpid(child, NULL, 0) < 0) perror("waitpid");
|
if (waitpid(child, NULL, 0) < 0)
|
||||||
|
{
|
||||||
|
perror("waitpid");
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,5 +3,13 @@
|
|||||||
|
|
||||||
namespace Keyboard
|
namespace Keyboard
|
||||||
{
|
{
|
||||||
|
enum Modifiers
|
||||||
|
{
|
||||||
|
LeftControl = 1,
|
||||||
|
LeftAlt = 2,
|
||||||
|
};
|
||||||
|
|
||||||
Option<char> decode_scancode(u8 scancode);
|
Option<char> decode_scancode(u8 scancode);
|
||||||
|
|
||||||
|
int modifiers();
|
||||||
}
|
}
|
||||||
|
@ -25,8 +25,7 @@ constexpr u8 F12 = 0x58;
|
|||||||
|
|
||||||
static bool should_ignore_key(u8 scancode)
|
static bool should_ignore_key(u8 scancode)
|
||||||
{
|
{
|
||||||
return (scancode > 0x3A && scancode < 0x47) || scancode == LEFT_CONTROL || scancode == TAB ||
|
return (scancode > 0x3A && scancode < 0x47) || scancode == TAB || scancode == F11 || scancode == F12;
|
||||||
scancode == LEFT_ALT || scancode == F11 || scancode == F12;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool g_ignore_next { false };
|
static bool g_ignore_next { false };
|
||||||
@ -124,6 +123,8 @@ static bool is_shifted()
|
|||||||
return g_left_shift || g_right_shift;
|
return g_left_shift || g_right_shift;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int g_modifiers = 0;
|
||||||
|
|
||||||
namespace Keyboard
|
namespace Keyboard
|
||||||
{
|
{
|
||||||
Option<char> decode_scancode(u8 scancode)
|
Option<char> decode_scancode(u8 scancode)
|
||||||
@ -161,6 +162,22 @@ namespace Keyboard
|
|||||||
return {};
|
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 (should_ignore_key(scancode)) return {};
|
||||||
|
|
||||||
if (released) return {};
|
if (released) return {};
|
||||||
@ -168,4 +185,9 @@ namespace Keyboard
|
|||||||
if (is_shifted()) return shifted_key_table[scancode];
|
if (is_shifted()) return shifted_key_table[scancode];
|
||||||
return key_table[scancode];
|
return key_table[scancode];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int modifiers()
|
||||||
|
{
|
||||||
|
return g_modifiers;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
#include "fs/devices/ConsoleDevice.h"
|
#include "fs/devices/ConsoleDevice.h"
|
||||||
|
#include "arch/Keyboard.h"
|
||||||
#include "video/TextConsole.h"
|
#include "video/TextConsole.h"
|
||||||
#include <luna/Buffer.h>
|
#include <luna/Buffer.h>
|
||||||
#include <luna/CString.h>
|
#include <luna/CString.h>
|
||||||
#include <luna/Vector.h>
|
#include <luna/Vector.h>
|
||||||
|
|
||||||
static Buffer g_console_input;
|
static Buffer g_console_input;
|
||||||
|
static bool g_eof { false };
|
||||||
|
|
||||||
Result<SharedPtr<Device>> ConsoleDevice::create()
|
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();
|
g_console_input.try_resize(g_console_input.size() - length).release_value();
|
||||||
|
|
||||||
|
if (!length && g_eof) g_eof = false;
|
||||||
|
|
||||||
return length;
|
return length;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -32,7 +36,7 @@ Result<usize> ConsoleDevice::write(const u8* buf, usize, usize length)
|
|||||||
|
|
||||||
bool ConsoleDevice::blocking() const
|
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;
|
static Vector<u8> g_temp_input;
|
||||||
@ -46,6 +50,13 @@ void ConsoleDevice::did_press_key(char key)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ctrl+D
|
||||||
|
if (key == 'd' && (Keyboard::modifiers() & Keyboard::LeftControl))
|
||||||
|
{
|
||||||
|
g_eof = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
g_temp_input.try_append((u8)key).value();
|
g_temp_input.try_append((u8)key).value();
|
||||||
|
|
||||||
if (key == '\n')
|
if (key == '\n')
|
||||||
|
Loading…
Reference in New Issue
Block a user