Skip to content

Commit

Permalink
Add sys$ttyname_r and ttyname_r() + ttyname().
Browse files Browse the repository at this point in the history
And print a greeting when sh starts up so we know which TTY we're on.
  • Loading branch information
awesomekling committed Oct 30, 2018
1 parent 7a85384 commit 00c21d1
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Kernel/Syscall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ DWORD handle(DWORD function, DWORD arg1, DWORD arg2, DWORD arg3)
return current->sys$set_mmap_name((void*)arg1, (size_t)arg2, (const char*)arg3);
case Syscall::PosixReadlink:
return current->sys$readlink((const char*)arg1, (char*)arg2, (size_t)arg3);
case Syscall::PosixTtynameR:
return current->sys$ttyname_r((int)arg1, (char*)arg2, (size_t)arg3);
default:
kprintf("int0x80: Unknown function %x requested {%x, %x, %x}\n", function, arg1, arg2, arg3);
break;
Expand Down
1 change: 1 addition & 0 deletions Kernel/Syscall.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ enum Function {
SetMmapName = 0x2005,
PosixReadlink = 0x2006,
PosixWrite = 0x2007,
PosixTtynameR = 0x2008,
};

void initialize();
Expand Down
2 changes: 2 additions & 0 deletions Kernel/TTY.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class TTY : public CharacterDevice {
virtual String ttyName() const = 0;

protected:
virtual bool isTTY() const final { return true; }

TTY(unsigned major, unsigned minor);
void emit(byte);

Expand Down
21 changes: 18 additions & 3 deletions Kernel/Task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -741,18 +741,33 @@ ssize_t Task::sys$get_dir_entries(int fd, void* buffer, size_t size)
VALIDATE_USER_BUFFER(buffer, size);
auto* handle = fileHandleIfExists(fd);
if (!handle)
return -1;
return -EBADF;
return handle->get_dir_entries((byte*)buffer, size);
}

int Task::sys$seek(int fd, int offset)
{
auto* handle = fileHandleIfExists(fd);
if (!handle)
return -1;
return -EBADF;
return handle->seek(offset, SEEK_SET);
}

int Task::sys$ttyname_r(int fd, char* buffer, size_t size)
{
VALIDATE_USER_BUFFER(buffer, size);
auto* handle = fileHandleIfExists(fd);
if (!handle)
return -EBADF;
if (!handle->isTTY())
return -ENOTTY;
auto ttyName = handle->tty()->ttyName();
if (size < ttyName.length() + 1)
return -ERANGE;
strcpy(buffer, ttyName.characters());
return 0;
}

ssize_t Task::sys$write(int fd, const void* data, size_t size)
{
VALIDATE_USER_BUFFER(data, size);
Expand Down Expand Up @@ -802,7 +817,7 @@ int Task::sys$close(int fd)
{
auto* handle = fileHandleIfExists(fd);
if (!handle)
return -1;
return -EBADF;
// FIXME: Implement.
return 0;
}
Expand Down
1 change: 1 addition & 0 deletions Kernel/Task.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ class Task : public InlineLinkedListNode<Task> {
int sys$get_arguments(int* argc, char*** argv);
int sys$uname(utsname*);
int sys$readlink(const char*, char*, size_t);
int sys$ttyname_r(int fd, char*, size_t);

static void initialize();

Expand Down
14 changes: 14 additions & 0 deletions LibC/unistd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ ssize_t write(int fd, const void* buf, size_t count)
__RETURN_WITH_ERRNO(rc, rc, -1);
}

int ttyname_r(int fd, char* buffer, size_t size)
{
int rc = Syscall::invoke(Syscall::PosixTtynameR, (dword)fd, (dword)buffer, (dword)size);
__RETURN_WITH_ERRNO(rc, rc, -1);
}

static char ttyname_buf[32];
char* ttyname(int fd)
{
if (ttyname_r(fd, ttyname_buf, sizeof(ttyname_buf)) < 0)
return nullptr;
return ttyname_buf;
}

int close(int fd)
{
int rc = Syscall::invoke(Syscall::PosixClose, fd);
Expand Down
2 changes: 2 additions & 0 deletions LibC/unistd.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ int lstat(const char* path, stat* statbuf);
int sleep(unsigned seconds);
int gethostname(char*, size_t);
ssize_t readlink(const char* path, char* buffer, size_t);
char* ttyname(int fd);
int ttyname_r(int fd, char* buffer, size_t);

#define WEXITSTATUS(status) (((status) & 0xff00) >> 8)
#define WTERMSIG(status) ((status) & 0x7f)
Expand Down
14 changes: 14 additions & 0 deletions Userland/sh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <LibC/errno.h>
#include <LibC/string.h>
#include <LibC/stdlib.h>
#include <LibC/utsname.h>
#include <AK/FileSystemPath.h>

struct GlobalState {
Expand Down Expand Up @@ -148,13 +149,26 @@ static int runcmd(char* cmd)
return retval;
}

static void greeting()
{
utsname uts;
int rc = uname(&uts);
if (rc < 0) {
perror("uname");
return;
}
printf("\n%s/%s on %s\n\n", uts.sysname, uts.machine, ttyname(0));
}

int main(int, char**)
{
g = new GlobalState;
int rc = gethostname(g->hostname, sizeof(g->hostname));
if (rc < 0)
perror("gethostname");

greeting();

char linebuf[128];
int linedx = 0;
linebuf[0] = '\0';
Expand Down
2 changes: 2 additions & 0 deletions VirtualFileSystem/CharacterDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class CharacterDevice {
unsigned major() const { return m_major; }
unsigned minor() const { return m_minor; }

virtual bool isTTY() const { return false; }

protected:
CharacterDevice(unsigned major, unsigned minor) : m_major(major), m_minor(minor) { }

Expand Down
16 changes: 16 additions & 0 deletions VirtualFileSystem/FileHandle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "CharacterDevice.h"
#include "sys-errno.h"
#include "UnixTypes.h"
#include "TTY.h"
#include <AK/BufferStream.h>

FileHandle::FileHandle(RetainPtr<VirtualFileSystem::Node>&& vnode)
Expand Down Expand Up @@ -109,6 +110,7 @@ Unix::ssize_t FileHandle::write(const byte* data, Unix::size_t size)
}
// FIXME: Implement non-device writes.
ASSERT_NOT_REACHED();
return -1;
}

bool FileHandle::hasDataAvailableForRead()
Expand Down Expand Up @@ -160,3 +162,17 @@ ssize_t FileHandle::get_dir_entries(byte* buffer, Unix::size_t size)
memcpy(buffer, tempBuffer.pointer(), stream.offset());
return stream.offset();
}

bool FileHandle::isTTY() const
{
if (auto* device = m_vnode->characterDevice())
return device->isTTY();
return false;
}

const TTY* FileHandle::tty() const
{
if (auto* device = m_vnode->characterDevice())
return static_cast<const TTY*>(device);
return nullptr;
}
5 changes: 5 additions & 0 deletions VirtualFileSystem/FileHandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "InodeMetadata.h"
#include <AK/ByteBuffer.h>

class TTY;

class FileHandle {
public:
explicit FileHandle(RetainPtr<VirtualFileSystem::Node>&&);
Expand All @@ -24,6 +26,9 @@ class FileHandle {

bool isDirectory() const;

bool isTTY() const;
const TTY* tty() const;

InodeMetadata metadata() const { return m_vnode->metadata(); }

VirtualFileSystem::Node* vnode() { return m_vnode.ptr(); }
Expand Down
1 change: 1 addition & 0 deletions VirtualFileSystem/VirtualFileSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class VirtualFileSystem {

bool isCharacterDevice() const { return m_characterDevice; }
CharacterDevice* characterDevice() { return m_characterDevice; }
const CharacterDevice* characterDevice() const { return m_characterDevice; }

void retain();
void release();
Expand Down

0 comments on commit 00c21d1

Please sign in to comment.