diff --git a/kernel.c b/kernel.c index 2f0881a..e40176e 100644 --- a/kernel.c +++ b/kernel.c @@ -11,33 +11,30 @@ #include "stdio.h" typedef struct { - char user_name[32]; - char user_hostname[32]; + char user_name[32]; + char user_hostname[32]; - unsigned int user_id; + unsigned int user_id; } user; void kmain(void) { - user current_user = { - .user_name = "admin", - .user_hostname = "soraos", - .user_id = 0 - }; + user current_user = {.user_name = "admin", .user_hostname = "soraos", .user_id = 0}; - __clear_screen(); + __clear_screen_colored(LIGHT_GREEN); - printf("SoraOS Kernel Initialized!\n"); - printf("\tWelcome to SoraOS!\n"); - printf("\tKernel Version: %s\n", __KERNEL_VERSION); - printf("\tCompiled on: %s at %s\n\n", __DATE__, __TIME__); + printf("SoraOS Kernel Initialized!\n"); + printf("\tWelcome to SoraOS!\n"); + printf("\tKernel Version: %s\n", __KERNEL_VERSION); + printf("\tCompiled on: %s at %s\n\n", __DATE__, __TIME__); + // TODO: CLI IDEA + int _count = printf("%s@%s:>", current_user.user_name, current_user.user_hostname); + scursor(_count, text_pos.line); - // TODO: CLI IDEA - int _count = printf("%s@%s:>", current_user.user_name, current_user.user_hostname); - scursor(_count, text_pos.line); - - // TODO: Enable user input handling - + // TODO: Enable user input handling + // char input_buffer[256]; + // scanf("%s", input_buffer); + // printf("\nYou entered: %s\n", input_buffer); // Infinite loop while (1) { diff --git a/stdio.h b/stdio.h index b7dec05..9378208 100644 --- a/stdio.h +++ b/stdio.h @@ -3,13 +3,23 @@ #include "types.h" #include -#include +#include #define VGA_WIDTH 80 #define VGA_HEIGHT 25 #define VGA_MEMORY_ADDRESS 0xB8000 +/* + * FOR INPUT + */ + +#define KEYBOARD_BUFFER_SIZE 256 + +static char _keyboard_buffer[KEYBOARD_BUFFER_SIZE]; +static uint16_t _kb_head = 0; +static uint16_t _kb_tail = 0; + /* IN VGA - Video Memory Arrya: * short * 1s byte (left most) -> TEXT COLOR @@ -343,7 +353,109 @@ void scursor(unsigned int column, unsigned int line) { __VGA_set_cursor_internal(column, line); } +static inline void __keyboard_buffer_push(char __c) { + _keyboard_buffer[_kb_head] = __c; + _kb_head = (_kb_head + 1) % KEYBOARD_BUFFER_SIZE; +} +static inline char __keyboard_buffer_pop() { + if (_kb_tail == _kb_head) { + return '\0'; + } + + char __c = _keyboard_buffer[_kb_tail]; + _kb_tail = (_kb_tail + 1) % KEYBOARD_BUFFER_SIZE; + return __c; +} + +static inline char __gets_internal(char *buffer, size_t max_length) { + size_t index = 0; + char __c; + + while (index < max_length - 1) { + __c = __keyboard_buffer_pop(); + if (__c == '\0') { + continue; + } + + if (__c == '\n' || __c == '\r') { + buffer[index] = '\0'; + break; + } + + buffer[index++] = __c; + } + + buffer[index] = '\0'; + return index; +} + +void gets(char *buffer, size_t max_length) { + __gets_internal(buffer, max_length); +} + +static inline int __scanf_internal(const char *fmt, va_list args) { + + char buffer[256]; + __gets_internal(buffer, sizeof(buffer)); + + const char* p = fmt; + char* in = buffer; + + int count = 0; + + while (*p) { + if (*p == '%') { + p++; + + if (*p == 'd') { + int* out = va_arg(args, int*); + int sign = 1; + int value = 0; + + if (*in == '-') { sign = -1; in++; } + + while (*in >= '0' && *in <= '9') { + value = value * 10 + (*in - '0'); + in++; + } + + *out = value * sign; + count++; + } + + else if (*p == 's') { + char* out = va_arg(args, char*); + while (*in == ' ') in++; // skip spaces + while (*in && *in != ' ') { + *out++ = *in++; + } + *out = '\0'; + count++; + } + + else if (*p == 'c') { + char* out = va_arg(args, char*); + *out = *in++; + count++; + } + } + + p++; + } + + va_end(args); + + return count; +} + +int scanf(const char *fmt, ...) { + va_list args; + va_start(args, fmt); + int result = __scanf_internal(fmt, args); + va_end(args); + return result; +}