Compare commits

..

3 Commits

Author SHA1 Message Date
bab9600be5
libc: Add creat()
Some checks failed
continuous-integration/drone/push Build is failing
2023-03-19 19:21:36 +01:00
31ef96ebfc
libc: Add stdin 2023-03-19 19:19:20 +01:00
d33fccc66f
kernel: Implement reading from /dev/console 2023-03-19 19:15:19 +01:00
10 changed files with 49 additions and 11 deletions

View File

@ -24,18 +24,19 @@ int main()
{ {
if (getpid() != 1) if (getpid() != 1)
{ {
printf("error: init not running as PID 1.\n"); fprintf(stderr, "error: init not running as PID 1.\n");
return 1; return 1;
} }
populate_devfs(); populate_devfs();
// Before this point, we don't even have an stdout and stderr. Set it up now so that child processes (and us) can // Before this point, we don't even have an stdin, stdout and stderr. Set it up now so that child processes (and us)
// print stuff. // can print stuff.
stdin = fopen("/dev/console", "r");
stdout = fopen("/dev/console", "w"); stdout = fopen("/dev/console", "w");
stderr = fopen("/dev/console", "w"); stderr = fopen("/dev/console", "w");
fprintf(stderr, "init is running as PID %d\n", getpid()); printf("init is running as PID %d\n", getpid());
pid_t ret = fork(); pid_t ret = fork();

View File

@ -4,9 +4,11 @@
#include "arch/Timer.h" #include "arch/Timer.h"
#include "arch/x86_64/CPU.h" #include "arch/x86_64/CPU.h"
#include "arch/x86_64/IO.h" #include "arch/x86_64/IO.h"
#include "fs/devices/ConsoleDevice.h"
#include "memory/MemoryManager.h" #include "memory/MemoryManager.h"
#include "sys/Syscall.h" #include "sys/Syscall.h"
#include "thread/Scheduler.h" #include "thread/Scheduler.h"
#include "video/TextConsole.h"
#include <cpuid.h> #include <cpuid.h>
#include <luna/CString.h> #include <luna/CString.h>
#include <luna/CircularQueue.h> #include <luna/CircularQueue.h>
@ -124,7 +126,11 @@ void io_thread()
while (!scancode_queue.try_pop(scancode)) { kernel_sleep(10); } while (!scancode_queue.try_pop(scancode)) { kernel_sleep(10); }
char key; char key;
if (Keyboard::decode_scancode(scancode).try_set_value(key)) { kdbgln("Read key: %c", key); } if (Keyboard::decode_scancode(scancode).try_set_value(key))
{
TextConsole::putchar(key);
ConsoleDevice::did_press_key(key);
}
} }
} }

View File

@ -1,15 +1,26 @@
#include "fs/devices/ConsoleDevice.h" #include "fs/devices/ConsoleDevice.h"
#include "video/TextConsole.h" #include "video/TextConsole.h"
#include <luna/Buffer.h>
#include <luna/CString.h>
static Buffer g_console_input;
Result<SharedPtr<Device>> ConsoleDevice::create() Result<SharedPtr<Device>> ConsoleDevice::create()
{ {
return (SharedPtr<Device>)TRY(make_shared<ConsoleDevice>()); return (SharedPtr<Device>)TRY(make_shared<ConsoleDevice>());
} }
// FIXME: Read from the keyboard. Result<usize> ConsoleDevice::read(u8* buf, usize, usize length) const
Result<usize> ConsoleDevice::read(u8*, usize, usize) const
{ {
return 0; if (length > g_console_input.size()) length = g_console_input.size();
memcpy(buf, g_console_input.data(), length);
memmove(g_console_input.data(), g_console_input.data() + length, g_console_input.size() - length);
g_console_input.try_resize(g_console_input.size() - length).release_value();
return length;
} }
Result<usize> ConsoleDevice::write(const u8* buf, usize, usize length) Result<usize> ConsoleDevice::write(const u8* buf, usize, usize length)
@ -20,5 +31,10 @@ Result<usize> ConsoleDevice::write(const u8* buf, usize, usize length)
bool ConsoleDevice::blocking() const bool ConsoleDevice::blocking() const
{ {
return false; return g_console_input.size() == 0;
}
void ConsoleDevice::did_press_key(char key)
{
*g_console_input.slice_at_end(1).value() = (u8)key;
} }

View File

@ -11,6 +11,8 @@ class ConsoleDevice : public Device
Result<usize> write(const u8*, usize, usize) override; Result<usize> write(const u8*, usize, usize) override;
static void did_press_key(char key);
bool blocking() const override; bool blocking() const override;
virtual ~ConsoleDevice() = default; virtual ~ConsoleDevice() = default;

View File

@ -13,6 +13,9 @@ extern "C"
/* Open a file path and return a file descriptor to it. */ /* Open a file path and return a file descriptor to it. */
int open(const char* path, int flags, ...); int open(const char* path, int flags, ...);
/* Create a file and return a file descriptor to it. */
int creat(const char* path, mode_t mode);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -16,8 +16,10 @@ typedef struct
#define EOF -1 #define EOF -1
extern FILE* stdin;
extern FILE* stdout; extern FILE* stdout;
extern FILE* stderr; extern FILE* stderr;
#define stdin stdin
#define stdout stdout #define stdout stdout
#define stderr stderr #define stderr stderr

View File

@ -10,8 +10,9 @@
#include <stdint.h> #include <stdint.h>
#include <sys/types.h> #include <sys/types.h>
#define STDOUT_FILENO 0 #define STDIN_FILENO 0
#define STDERR_FILENO 1 #define STDOUT_FILENO 1
#define STDERR_FILENO 2
#ifdef __cplusplus #ifdef __cplusplus
extern "C" extern "C"

View File

@ -18,4 +18,9 @@ extern "C"
va_end(ap); va_end(ap);
__errno_return(rc, int); __errno_return(rc, int);
} }
int creat(const char* path, mode_t mode)
{
return open(path, O_WRONLY | O_CREAT | O_TRUNC, mode);
}
} }

View File

@ -5,6 +5,7 @@ extern "C"
{ {
void libc_init() void libc_init()
{ {
stdin = fdopen(STDIN_FILENO, "r");
stdout = fdopen(STDOUT_FILENO, "w"); stdout = fdopen(STDOUT_FILENO, "w");
stderr = fdopen(STDERR_FILENO, "w"); stderr = fdopen(STDERR_FILENO, "w");
} }

View File

@ -8,6 +8,7 @@
#include <sys/syscall.h> #include <sys/syscall.h>
#include <unistd.h> #include <unistd.h>
FILE* stdin = nullptr;
FILE* stderr = nullptr; FILE* stderr = nullptr;
FILE* stdout = nullptr; FILE* stdout = nullptr;