2018-10-30 13:59:29 +01:00
|
|
|
#include "TTY.h"
|
2018-11-02 14:06:48 +01:00
|
|
|
#include "Process.h"
|
2018-11-06 15:45:16 +01:00
|
|
|
#include <LibC/signal_numbers.h>
|
2018-10-30 13:59:29 +01:00
|
|
|
|
|
|
|
TTY::TTY(unsigned major, unsigned minor)
|
|
|
|
: CharacterDevice(major, minor)
|
|
|
|
{
|
2018-11-11 15:36:40 +01:00
|
|
|
memset(&m_termios, 0, sizeof(m_termios));
|
|
|
|
m_termios.c_lflag |= ISIG | ECHO;
|
2018-10-30 13:59:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TTY::~TTY()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t TTY::read(byte* buffer, size_t size)
|
|
|
|
{
|
2018-10-30 15:33:37 +01:00
|
|
|
ssize_t nread = min(m_buffer.size(), size);
|
|
|
|
memcpy(buffer, m_buffer.data(), nread);
|
2018-11-09 10:03:21 +01:00
|
|
|
if (nread == (ssize_t)m_buffer.size())
|
2018-10-30 15:33:37 +01:00
|
|
|
m_buffer.clear();
|
|
|
|
else {
|
|
|
|
dbgprintf("had %u, read %u\n", m_buffer.size(), nread);
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
return nread;
|
2018-10-30 13:59:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t TTY::write(const byte* buffer, size_t size)
|
|
|
|
{
|
2018-11-08 01:23:23 +01:00
|
|
|
onTTYWrite(buffer, size);
|
2018-10-30 13:59:29 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TTY::hasDataAvailableForRead() const
|
|
|
|
{
|
2018-10-30 15:33:37 +01:00
|
|
|
return !m_buffer.isEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TTY::emit(byte ch)
|
|
|
|
{
|
|
|
|
m_buffer.append(ch);
|
2018-10-30 13:59:29 +01:00
|
|
|
}
|
2018-11-02 14:06:48 +01:00
|
|
|
|
|
|
|
void TTY::interrupt()
|
|
|
|
{
|
2018-11-11 15:36:40 +01:00
|
|
|
if (!should_generate_signals())
|
|
|
|
return;
|
2018-11-02 14:06:48 +01:00
|
|
|
dbgprintf("%s: Interrupt ^C pressed!\n", ttyName().characters());
|
|
|
|
if (pgid()) {
|
|
|
|
dbgprintf("%s: Send SIGINT to everyone in pgrp %d\n", ttyName().characters(), pgid());
|
|
|
|
InterruptDisabler disabler;
|
|
|
|
Process::for_each_in_pgrp(pgid(), [this] (auto& process) {
|
|
|
|
dbgprintf("%s: Send SIGINT to %d\n", ttyName().characters(), process.pid());
|
|
|
|
process.send_signal(SIGINT, nullptr);
|
2018-11-07 21:38:18 +01:00
|
|
|
return true;
|
2018-11-02 14:06:48 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|