add basic stdio

This commit is contained in:
2025-03-14 20:18:26 +03:00
parent e37c8462a6
commit 294111bf41
5 changed files with 124 additions and 5 deletions
+5 -2
View File
@@ -1,8 +1,8 @@
# Define the object files # Define the object files
OBJS = bin/boot.o bin/kernel.o bin/idt.o bin/isr.o bin/screen.o bin/utils.o bin/string.o OBJS = bin/boot.o bin/kernel.o bin/idt.o bin/isr.o bin/screen.o bin/utils.o bin/string.o bin/stdio.o
# Define the compiler and assembler # Define the compiler and assembler
CC = gcc CC = gcc -D__is_katauos
AS = nasm AS = nasm
# Define the compiler flags # Define the compiler flags
@@ -42,6 +42,9 @@ bin/utils.o: src/utils.c
bin/string.o: src/string.c bin/string.o: src/string.c
$(CC) $(CFLAGS) -c $< -o $@ $(CC) $(CFLAGS) -c $< -o $@
bin/stdio.o: src/stdio.c
$(CC) $(CFLAGS) -c $< -o $@
clean: clean:
rm -rf bin/ rm -rf bin/
rm -f $(OUTPUT) rm -f $(OUTPUT)
+13
View File
@@ -0,0 +1,13 @@
#ifndef STDIO_H
#define STDIO_H
//TODO: these should be contained in newlib i guess
//but until i implement it let it be here yo
#define EOF (-1)
#define INT_MAX 2147483647
int printf(const char* __restrict, ...);
int putchar(int);
int puts(const char*);
#endif
+1 -1
View File
@@ -44,7 +44,7 @@ void isr_install() {
void isr0() void isr0()
{ {
terminal_writestring(exception_messages[0]); terminal_writestring(exception_messages[0]);
asm("hlt"); asm("hlt");
} }
void isr1() void isr1()
{ {
+6 -2
View File
@@ -1,5 +1,6 @@
#include "../include/isr.h" #include "../include/isr.h"
#include "../include/screen.h" #include "../include/screen.h"
#include "../include/stdio.h"
#include "../include/utils.h" #include "../include/utils.h"
/* Check if the compiler thinks you are targeting the wrong operating system. */ /* Check if the compiler thinks you are targeting the wrong operating system. */
@@ -16,9 +17,12 @@
void kmain() void kmain()
{ {
terminal_initialize(); terminal_initialize();
terminal_writestring("KatauOS booting up\n"); printf("KatauOS booting up\n");
isr_install(); isr_install();
terminal_writestring("Kernel init sequence completed\n"); printf("Kernel init sequence completed\n");
char yooo[256] = "heheh";
printf("test string: %s\n", yooo);
while(1) while(1)
{ {
+99
View File
@@ -0,0 +1,99 @@
#include "../include/stdio.h"
#include <stdbool.h>
#include <stdarg.h>
#include <stddef.h>
#include "../include/string.h"
#ifdef __is_katauos
#include "../include/screen.h"
#endif
int puts(const char* string) {
return printf("%s\n", string);
}
int putchar(int ic) {
#if defined(__is_katauos)
char c = (char) ic;
terminal_write(&c, sizeof(c));
#else
// TODO: Implement stdio and the write system call.
#endif
return ic;
}
static bool print(const char* data, size_t length) {
const unsigned char* bytes = (const unsigned char*) data;
for (size_t i = 0; i < length; i++)
if (putchar(bytes[i]) == EOF)
return false;
return true;
}
int printf(const char* restrict format, ...) {
va_list parameters;
va_start(parameters, format);
int written = 0;
while (*format != '\0') {
size_t maxrem = INT_MAX - written;
if (format[0] != '%' || format[1] == '%') {
if (format[0] == '%')
format++;
size_t amount = 1;
while (format[amount] && format[amount] != '%')
amount++;
if (maxrem < amount) {
// TODO: Set errno to EOVERFLOW.
return -1;
}
if (!print(format, amount))
return -1;
format += amount;
written += amount;
continue;
}
const char* format_begun_at = format++;
if (*format == 'c') {
format++;
char c = (char) va_arg(parameters, int /* char promotes to int */);
if (!maxrem) {
// TODO: Set errno to EOVERFLOW.
return -1;
}
if (!print(&c, sizeof(c)))
return -1;
written++;
} else if (*format == 's') {
format++;
const char* str = va_arg(parameters, const char*);
size_t len = strlen(str);
if (maxrem < len) {
// TODO: Set errno to EOVERFLOW.
return -1;
}
if (!print(str, len))
return -1;
written += len;
} else {
format = format_begun_at;
size_t len = strlen(format);
if (maxrem < len) {
// TODO: Set errno to EOVERFLOW.
return -1;
}
if (!print(format, len))
return -1;
written += len;
format += len;
}
}
va_end(parameters);
return written;
}