Made printf() good!

This commit is contained in:
2024-05-30 10:30:39 +02:00
parent f4e8435603
commit 75410d3a6c
5 changed files with 91 additions and 45 deletions
+45 -22
View File
@@ -3,69 +3,92 @@
#include <syscall.h>
#include <string.h>
#include <stdlib.h>
// TODO: Make a proper file descriptor table
#define STDOUT 1
#define stdin 0
#define stdout 1
#define stderr 2
/* --- Output --- */
void putc(char c) {
// write(STDOUT, &c, 1); TODO: This doesn't work and we need to use a static variable
// write(stdout, &c, 1); TODO: This doesn't work and we need to use a static variable
static char _c;
_c = c;
write(STDOUT, &_c, 1);
write(stdout, &_c, 1);
}
void print(const char* s) {
write(STDOUT, s, strlen(s));
write(stdout, s, strlen(s));
}
void clear() {
print("\x1b[J");
}
// TODO: Dynamically allocate memory and print once instead of calling putc for every character :/
// TODO: Support negative numbers
// TODO: Printing individual characters doesn't let escape sequences work :(
int printf(const char* fmt, ...) {
uint32_t* argPtr = (uint32_t*)&fmt + 1; // First arg after fmt
uint8_t state = 0;
for (uint32_t i = 0; fmt[i] != '\0'; i++) {
char c = fmt[i];
// Printing buffer
static char* buffer;
static uint32_t len;
uint32_t max = 256;
buffer = malloc(max);
len = 0;
uint8_t state = 0;
for (uint32_t i = 0; fmt[i] != '\0'; i++) {
if (len > max) {
max *= 2;
// Reallocate larger buffer TODO: Use realloc();
char* temp = malloc(max);
strcpy(temp, buffer);
free(buffer);
buffer = temp;
}
buffer[len] = '\0';
char c = fmt[i];
if (state == 0) {
if (c == '%') state = '%';
else putc(c);
else buffer[len++] = c;
} else if (state == '%') {
switch (c) {
case 'd': // Decimal number
print(itoa(*(uint32_t*)argPtr, 10));
argPtr++;
strcat(buffer, itoa(*(uint32_t*)argPtr++, 10));
len += strlen(&buffer[len]); // Add the remaining length
break;
case 'x': // Hex number
print(itoa(*(uint32_t*)argPtr, 16));
argPtr++;;
strcat(buffer, itoa(*(uint32_t*)argPtr++, 16));
len += strlen(&buffer[len]);
break;
case 's': // String
print(*(char**)argPtr);
argPtr++;
char* s = *(char**)argPtr++;
while (*s != '\0') {
buffer[len++] = *s++;
}
break;
case 'c': // Char
putc(*(char*)argPtr);
buffer[len++] = *(char*)argPtr;
argPtr++;
break;
default: // This looks insane
buffer[len++] = '%';
case '%': // Just print '%'
putc(c);
break;
default:
putc('%');
putc(c);
buffer[len++] = c;
break;
}
state = 0;
}
}
buffer[len] = '\0';
write(stdout, buffer, len);
free(buffer);
return 0;
}
+5 -4
View File
@@ -2,17 +2,18 @@
#define STDLIB_H
#include <stdint.h>
#include <syscall.h>
void* malloc(const uint32_t size) { // Use Syscall(3) - Malloc()
void* malloc(const uint32_t size) {
void* ptr = 0;
__asm__ volatile ("int $0x80" : : "a"(3), "b"(size));
__asm__ volatile ("int $0x80" : : "a"(SYS_MALLOC), "b"(size));
__asm__ volatile ("movl %%eax, %0" : "=r"(ptr));
return ptr;
}
void free(const void* ptr) { // Use Syscall(4) - Free()
__asm__ volatile ("int $0x80" : : "a"(4), "b"(ptr));
void free(const void* ptr) {
__asm__ volatile ("int $0x80" : : "a"(SYS_FREE), "b"(ptr));
}
#endif
+2 -1
View File
@@ -5,6 +5,7 @@
#include <interrupt/idt.h>
#include <screen/text.h>
#include <screen/gfxmode.h>
#include <terminal/terminal.h>
#include <stdbool.h>
#include <string.h>
@@ -133,7 +134,7 @@ __attribute__ ((interrupt)) void rtcHandler(intFrame_t* iframe) {
uint32_t x = gfxMode->xRes - len - 1;
char* str = &buf[0];
for (uint8_t i = 0; i != len; i++, x++) {
drawChar(str[i], x, 0, FG_COLOR, BG_COLOR);
drawChar(str[i], x, 0, terminal->fg, terminal->bg);
}
}
}
+1
View File
@@ -42,6 +42,7 @@
// Other addresses
#define KERNEL_LOC 0x100000
#define KERNEL_MALLOC_AREA 0x300000
#define MEMMAP_AREA 0x30000
#endif
+38 -18
View File
@@ -50,6 +50,26 @@ void main() {
setupTimer();
setupRtc();
setupKeyboard();
// Setup kernel malloc variables
mallocNode_t* kernelMallocListHead = 0;
uint32_t kernelMallocVirtualAddr = KERNEL_MALLOC_AREA;
uint32_t kernelMallocPhysicalAddr = (uint32_t)allocBlocks(1); // TODO: Kernel may use more than 4kB?
uint32_t kernelMallocPages = 1;
mapPage((void*)kernelMallocPhysicalAddr, (void*)kernelMallocVirtualAddr);
ptEntry_t* kernelMallocPage = getPage(kernelMallocVirtualAddr);
SET_ATTR(kernelMallocPage, PTE_READ_WRITE);
kernelMallocListHead = (mallocNode_t*)kernelMallocVirtualAddr;
kernelMallocListHead->size = PAGE_SIZE - sizeof(mallocNode_t);
kernelMallocListHead->free = true;
kernelMallocListHead->next = NULL;
mallocListHead = kernelMallocListHead;
mallocVirtualAddr = kernelMallocVirtualAddr;
mallocPhysicalAddr = kernelMallocPhysicalAddr;
mallocPages = kernelMallocPages;
// Screen setup
loadFont("term16n.fnt");
@@ -61,7 +81,7 @@ void main() {
// Run Interactive Shell Program
// TODO: Make it in another file
while (1) {
print(PROMPT);
printf(PROMPT);
char input[256];
read(input);
@@ -134,18 +154,18 @@ void main() {
getc(); // Wait until we get a 'q'
clear();
} else if (strcmp(input, "help") == 0) {
print("Available Commands:\n");
print(" clear | Clears the screen\n");
print(" echo | Prints a message to stdout\n");
print(" exit | Exits shell\n");
print(" gfx | Does a graphics test\n");
print(" help | Prints this message\n");
print(" ls | Lists all available files\n");
print(" memmap | Prints the memory map and info\n");
print(" reboot | Reboots the system\n");
print(" sleep | Sleeps for input number of seconds\n");
print(" soundtest | Plays a tune :)\n");
print(" test | Performs tests\n");
printf("Available Commands:\n");
printf(" clear | Clears the screen\n");
printf(" echo | Prints a message to stdout\n");
printf(" exit | Exits shell\n");
printf(" gfx | Does a graphics test\n");
printf(" help | Prints this message\n");
printf(" ls | Lists all available files\n");
printf(" memmap | Prints the memory map and info\n");
printf(" reboot | Reboots the system\n");
printf(" sleep | Sleeps for input number of seconds\n");
printf(" soundtest | Plays a tune :)\n");
printf(" test | Performs tests\n");
} else if (strcmp(input, "ls") == 0) {
printFiletable(filetable);
} else if (strcmp(input, "memmap") == 0) {
@@ -228,11 +248,11 @@ void main() {
flushTlbEntry(virtualAddr);
}
}
mallocListHead = NULL;
mallocVirtualAddr = 0;
mallocPhysicalAddr = 0;
mallocPages = 0;
mallocListHead = kernelMallocListHead;
mallocVirtualAddr = kernelMallocVirtualAddr;
mallocPhysicalAddr = kernelMallocPhysicalAddr;
mallocPages = kernelMallocPages;
} else if (strcmp(file->filetype, "tab") == 0) {
printFiletable(entryPoint);
} else if (strcmp(file->filetype, "fnt") == 0) {