LibC/Kernel: Cleanup termios code
This is still not correct, but much better than it used to be
This commit is contained in:
@@ -68,8 +68,8 @@ namespace Kernel
|
||||
|
||||
BAN::ErrorOr<long> sys_exit(int status);
|
||||
|
||||
BAN::ErrorOr<long> sys_gettermios(::termios*);
|
||||
BAN::ErrorOr<long> sys_settermios(const ::termios*);
|
||||
BAN::ErrorOr<long> sys_tcgetattr(int fildes, termios*);
|
||||
BAN::ErrorOr<long> sys_tcsetattr(int fildes, int optional_actions, const termios*);
|
||||
|
||||
BAN::ErrorOr<long> sys_fork(uintptr_t rsp, uintptr_t rip);
|
||||
BAN::ErrorOr<long> sys_exec(const char* path, const char* const* argv, const char* const* envp);
|
||||
|
||||
@@ -4,18 +4,17 @@
|
||||
#include <kernel/Device/Device.h>
|
||||
#include <kernel/Lock/SpinLock.h>
|
||||
#include <kernel/Terminal/TerminalDriver.h>
|
||||
#include <kernel/Terminal/termios.h>
|
||||
#include <kernel/ThreadBlocker.h>
|
||||
#include <LibInput/KeyEvent.h>
|
||||
|
||||
#include <termios.h>
|
||||
|
||||
namespace Kernel
|
||||
{
|
||||
|
||||
class TTY : public CharacterDevice
|
||||
{
|
||||
public:
|
||||
void set_termios(const termios& termios) { m_termios = termios; }
|
||||
termios get_termios() const { return m_termios; }
|
||||
virtual void set_font(const LibFont::Font&) {};
|
||||
|
||||
void set_foreground_pgrp(pid_t pgrp) { m_foreground_pgrp = pgrp; }
|
||||
@@ -33,6 +32,10 @@ namespace Kernel
|
||||
void on_key_event(LibInput::KeyEvent);
|
||||
void handle_input_byte(uint8_t);
|
||||
|
||||
void get_termios(termios* termios) { *termios = m_termios; }
|
||||
// FIXME: validate termios
|
||||
BAN::ErrorOr<void> set_termios(const termios* termios) { m_termios = *termios; return {}; }
|
||||
|
||||
virtual bool is_tty() const override { return true; }
|
||||
|
||||
virtual uint32_t height() const = 0;
|
||||
@@ -53,7 +56,15 @@ namespace Kernel
|
||||
protected:
|
||||
TTY(mode_t mode, uid_t uid, gid_t gid)
|
||||
: CharacterDevice(mode, uid, gid)
|
||||
{ }
|
||||
{
|
||||
// FIXME: add correct baud and flags
|
||||
m_termios.c_iflag = 0;
|
||||
m_termios.c_oflag = 0;
|
||||
m_termios.c_cflag = CS8;
|
||||
m_termios.c_lflag = ECHO | ICANON;
|
||||
m_termios.c_ospeed = B38400;
|
||||
m_termios.c_ispeed = B38400;
|
||||
}
|
||||
|
||||
virtual void putchar_impl(uint8_t ch) = 0;
|
||||
virtual BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) override;
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
#include <BAN/Array.h>
|
||||
#include <kernel/Device/Device.h>
|
||||
#include <kernel/Terminal/TerminalDriver.h>
|
||||
#include <kernel/Terminal/termios.h>
|
||||
#include <kernel/Terminal/TTY.h>
|
||||
#include <kernel/ThreadBlocker.h>
|
||||
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
namespace Kernel
|
||||
{
|
||||
|
||||
struct termios
|
||||
{
|
||||
bool canonical { true };
|
||||
bool echo { true };
|
||||
};
|
||||
|
||||
}
|
||||
@@ -332,39 +332,38 @@ namespace Kernel
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
|
||||
BAN::ErrorOr<long> Process::sys_gettermios(::termios* termios)
|
||||
BAN::ErrorOr<long> Process::sys_tcgetattr(int fildes, termios* termios)
|
||||
{
|
||||
LockGuard _(m_process_lock);
|
||||
|
||||
TRY(validate_pointer_access(termios, sizeof(::termios)));
|
||||
TRY(validate_pointer_access(termios, sizeof(termios)));
|
||||
|
||||
if (!m_controlling_terminal)
|
||||
auto inode = TRY(m_open_file_descriptors.inode_of(fildes));
|
||||
if (!inode->is_tty())
|
||||
return BAN::Error::from_errno(ENOTTY);
|
||||
|
||||
Kernel::termios ktermios = m_controlling_terminal->get_termios();
|
||||
termios->c_lflag = 0;
|
||||
if (ktermios.canonical)
|
||||
termios->c_lflag |= ICANON;
|
||||
if (ktermios.echo)
|
||||
termios->c_lflag |= ECHO;
|
||||
static_cast<TTY*>(inode.ptr())->get_termios(termios);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
BAN::ErrorOr<long> Process::sys_settermios(const ::termios* termios)
|
||||
BAN::ErrorOr<long> Process::sys_tcsetattr(int fildes, int optional_actions, const termios* termios)
|
||||
{
|
||||
if (optional_actions != TCSANOW)
|
||||
return BAN::Error::from_errno(EINVAL);
|
||||
|
||||
LockGuard _(m_process_lock);
|
||||
|
||||
TRY(validate_pointer_access(termios, sizeof(::termios)));
|
||||
TRY(validate_pointer_access(termios, sizeof(termios)));
|
||||
|
||||
if (!m_controlling_terminal)
|
||||
auto inode = TRY(m_open_file_descriptors.inode_of(fildes));
|
||||
if (!inode->is_tty())
|
||||
return BAN::Error::from_errno(ENOTTY);
|
||||
|
||||
Kernel::termios ktermios;
|
||||
ktermios.echo = termios->c_lflag & ECHO;
|
||||
ktermios.canonical = termios->c_lflag & ICANON;
|
||||
TRY(static_cast<TTY*>(inode.ptr())->set_termios(termios));
|
||||
|
||||
// FIXME: SIGTTOU
|
||||
|
||||
m_controlling_terminal->set_termios(ktermios);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -234,7 +234,7 @@ namespace Kernel
|
||||
}
|
||||
|
||||
// ^D + canonical
|
||||
if (ch == '\x04' && m_termios.canonical)
|
||||
if (ch == '\x04' && (m_termios.c_lflag & ICANON))
|
||||
{
|
||||
m_output.flush = true;
|
||||
m_output.thread_blocker.unblock();
|
||||
@@ -242,7 +242,7 @@ namespace Kernel
|
||||
}
|
||||
|
||||
// backspace + canonical
|
||||
if (ch == '\b' && m_termios.canonical)
|
||||
if (ch == '\b' && (m_termios.c_lflag & ICANON))
|
||||
{
|
||||
do_backspace();
|
||||
return;
|
||||
@@ -250,7 +250,7 @@ namespace Kernel
|
||||
|
||||
m_output.buffer[m_output.bytes++] = ch;
|
||||
|
||||
if (m_termios.echo)
|
||||
if (m_termios.c_lflag & ECHO)
|
||||
{
|
||||
if ((ch <= 31 || ch == 127) && ch != '\n')
|
||||
{
|
||||
@@ -276,7 +276,7 @@ namespace Kernel
|
||||
}
|
||||
}
|
||||
|
||||
if (ch == '\n' || !m_termios.canonical)
|
||||
if (ch == '\n' || !(m_termios.c_lflag & ICANON))
|
||||
{
|
||||
m_output.flush = true;
|
||||
m_output.thread_blocker.unblock();
|
||||
|
||||
Reference in New Issue
Block a user