Implement printf()
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
apio 2023-01-07 01:49:26 +01:00
parent 29bd8a69fa
commit 7e377ef712
Signed by: apio
GPG Key ID: B8A7D06E42258954
7 changed files with 53 additions and 28 deletions

View File

@ -4,24 +4,18 @@
void bye() void bye()
{ {
console_print("byeee!\n"); printf("byeee!\n");
} }
int main() int main()
{ {
atexit(bye); atexit(bye);
printf("Welcome to %s!\n", "Luna");
char buffer[64];
snprintf(buffer, sizeof(buffer), "Welcome to %s!\n", "Luna");
console_print(buffer);
time_t now = time(NULL); time_t now = time(NULL);
snprintf(buffer, sizeof(buffer), "Realtime clock: %ld s\n", now); printf("Realtime clock: %ld s\n", now);
console_print(buffer); for (int i = 0; i < atoi("8"); i++) { console_write(".", 1); }
for (int i = 0; i < atoi("8"); i++) { console_print("."); } console_write("\n", 1);
console_print("\n");
} }

View File

@ -17,7 +17,7 @@ set(SOURCES
src/thread/Scheduler.cpp src/thread/Scheduler.cpp
src/sys/Syscall.cpp src/sys/Syscall.cpp
src/sys/exit.cpp src/sys/exit.cpp
src/sys/console_print.cpp src/sys/console_write.cpp
src/sys/clock_gettime.cpp src/sys/clock_gettime.cpp
src/InitRD.cpp src/InitRD.cpp
src/ELF.cpp src/ELF.cpp

View File

@ -1,12 +0,0 @@
#include "memory/MemoryManager.h"
#include "sys/Syscall.h"
#include "video/TextConsole.h"
Result<u64> sys_console_print(Registers*, SyscallArgs args)
{
if (!MemoryManager::validate_userspace_string(args[0])) return err(EFAULT);
TextConsole::print((char*)args[0]);
return { 0 };
}

View File

@ -0,0 +1,15 @@
#include "memory/MemoryManager.h"
#include "sys/Syscall.h"
#include "video/TextConsole.h"
Result<u64> sys_console_write(Registers*, SyscallArgs args)
{
char* ptr = (char*)args[0];
usize size = (usize)args[1];
if (!MemoryManager::validate_user_read(ptr, size)) return err(EFAULT);
while (size--) { TextConsole::putchar(*ptr++); }
return { 0 };
}

View File

@ -49,8 +49,14 @@ extern "C"
/* Write up to max bytes of formatted output into a buffer. */ /* Write up to max bytes of formatted output into a buffer. */
int vsnprintf(char*, size_t, const char*, va_list); int vsnprintf(char*, size_t, const char*, va_list);
/* Write formatted output to standard output. */
int printf(const char*, ...);
/* Write a string followed by a newline to standard output. */
int puts(const char* s);
/* Output a message to the console. */ /* Output a message to the console. */
int console_print(const char* msg); int console_write(const char* msg, size_t size);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -2,6 +2,7 @@
#include <bits/errno-return.h> #include <bits/errno-return.h>
#include <luna/Format.h> #include <luna/Format.h>
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include <sys/syscall.h> #include <sys/syscall.h>
#include <unistd.h> #include <unistd.h>
@ -9,9 +10,9 @@ FILE* stderr = nullptr;
extern "C" extern "C"
{ {
int console_print(const char* str) int console_write(const char* str, size_t size)
{ {
long rc = syscall(SYS_console_print, str); long rc = syscall(SYS_console_write, str, size);
__errno_return(rc, int); __errno_return(rc, int);
} }
@ -48,4 +49,25 @@ extern "C"
return rc; return rc;
} }
int printf(const char* format, ...)
{
va_list ap;
va_start(ap, format);
int rc = (int)pure_cstyle_format(
format, [](char c, void*) { console_write(&c, 1); }, nullptr, ap);
va_end(ap);
return rc;
}
int puts(const char* s)
{
if (console_write(s, strlen(s)) < 0) return -1;
if (console_write("\n", 1) < 0) return -1;
return 0;
}
} }

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#define enumerate_syscalls(_e) _e(exit) _e(console_print) _e(clock_gettime) #define enumerate_syscalls(_e) _e(exit) _e(console_write) _e(clock_gettime)
enum Syscalls enum Syscalls
{ {