summaryrefslogtreecommitdiff
path: root/Kernel/TTY.cpp
blob: 2ad8c50a729fb7c40a29bde62a54a3924f9c0b03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "TTY.h"
#include "Process.h"
#include <LibC/signal_numbers.h>

TTY::TTY(unsigned major, unsigned minor)
    : CharacterDevice(major, minor)
{
}

TTY::~TTY()
{
}

ssize_t TTY::read(byte* buffer, size_t size)
{
    ssize_t nread = min(m_buffer.size(), size);
    memcpy(buffer, m_buffer.data(), nread);
    if (nread == m_buffer.size())
        m_buffer.clear();
    else {
        dbgprintf("had %u, read %u\n", m_buffer.size(), nread);
        ASSERT_NOT_REACHED();
    }
    return nread;
}

ssize_t TTY::write(const byte* buffer, size_t size)
{
    onTTYWrite(buffer, size);
    return 0;
}

bool TTY::hasDataAvailableForRead() const
{
    return !m_buffer.isEmpty();
}

void TTY::emit(byte ch)
{
    m_buffer.append(ch);
}

void TTY::interrupt()
{
    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);
            return true;
        });
    }
}