From e95c4b2c9fa7105c43034469fad5ff6e514079d6 Mon Sep 17 00:00:00 2001 From: Riyan Dhiman Date: Mon, 5 Aug 2024 14:57:16 +0530 Subject: [PATCH] added basic commands --- kernel/arch/devices/keyboard.c | 4 +- kernel/arch/i386/time.c | 2 +- kernel/arch/i386/tty.c | 84 +++++++++++++++++++++++++++++++++- kernel/mmu/alloc.c | 2 +- libc/include/string.h | 4 +- libc/string/memcmp.c | 15 ++++++ 6 files changed, 105 insertions(+), 6 deletions(-) diff --git a/kernel/arch/devices/keyboard.c b/kernel/arch/devices/keyboard.c index ebbe01a..904d0b0 100644 --- a/kernel/arch/devices/keyboard.c +++ b/kernel/arch/devices/keyboard.c @@ -104,8 +104,8 @@ void keyboard_handler(struct regs *r){ if(scancode == 0x2a || scancode == 0x36){shift = 1;return;} if(scancode == 0x3a){ caps = ~caps; return;} - if(scancode == 0x14 || scancode == 0xE0) { ctrl = 1; return;} - if(scancode == 0x1c){terminal_new_line();return;} + if(scancode == 0xE0) { ctrl = 1; return;} + if(scancode == 0x1c){terminal_process_command();return;} if(scancode == 0x0e){ terminal_backspace(); return; diff --git a/kernel/arch/i386/time.c b/kernel/arch/i386/time.c index 240f714..01f90db 100644 --- a/kernel/arch/i386/time.c +++ b/kernel/arch/i386/time.c @@ -55,5 +55,5 @@ struct kernel_time rtc_time() { void show_time() { struct kernel_time time = rtc_time(); - printf("current time: %d:%d:%d %d:%d:%d%d\n", time.hour, time.minute, time.second, time.day, time.month, time.century, time.year); + printf("current time: %d:%d:%d %d:%d:%d%d", time.hour, time.minute, time.second, time.day, time.month, time.century, time.year); } \ No newline at end of file diff --git a/kernel/arch/i386/tty.c b/kernel/arch/i386/tty.c index 17195d8..9a02aa8 100644 --- a/kernel/arch/i386/tty.c +++ b/kernel/arch/i386/tty.c @@ -2,9 +2,14 @@ #include #include #include - +#include +#include #include #include "vga.h" +#include + +#define PROMPT "cheaky>" +#define PROMPT_LENGTH 7 // Length of "cheaky>" static const size_t VGA_WIDTH = 80; static const size_t VGA_HEIGHT = 100; @@ -121,3 +126,80 @@ void terminal_write(const char* data, size_t size) { void terminal_writestring(const char* data) { terminal_write(data, strlen(data)); } + + + +char* parse_command(size_t y) { + if (y >= VGA_HEIGHT) { + return NULL; // Invalid row number + } + // Calculate the starting index for the row + size_t start_index = y * VGA_WIDTH; + + // Check if the row starts with the prompt + for (size_t i = 0; i < PROMPT_LENGTH; i++) { + printf(terminal_buffer[start_index + i]); + printf(vga_entry(PROMPT[i], terminal_color)); + if (terminal_buffer[start_index + i] != vga_entry(PROMPT[i], terminal_color)) { + return NULL; // Prompt not found at the start of the row + } + } + // Allocate memory for the string (max length is VGA_WIDTH - PROMPT_LENGTH) + char* result = (char*)kmalloc(VGA_WIDTH - PROMPT_LENGTH + 1); + if (result == NULL) { + return NULL; // Memory allocation failed + } + // Extract characters after the prompt + size_t j = 0; + for (size_t i = start_index + PROMPT_LENGTH; i < (y + 1) * VGA_WIDTH; i++) { + char c = terminal_buffer[i] & 0xFF; // Extract character from VGA entry + if (c == '\0' || c == ' ') { + break; // Stop at null terminator or space + } + result[j++] = c; + } + result[j] = '\0'; // Null-terminate the string + return result; +} + + + +enum Command { + CMD_VERSION, + CMD_REBOOT, + CMD_WHOAMI, + CMD_TIME, + CMD_UNKNOWN + // ... other commands ... +}; + +enum Command get_command(const char *str) { + if (strcmp(str, "version")) return CMD_VERSION; + if (strcmp(str, "reboot")) return CMD_REBOOT; + if (strcmp(str, "whoami")) return CMD_WHOAMI; + if (strcmp(str, "time")) return CMD_TIME; + return CMD_UNKNOWN; +} + +void terminal_process_command() { + char *command = parse_command(terminal_row); + terminal_new_line(); + switch (get_command(command)) { + case CMD_VERSION: + terminal_writestring("Kernel version 1.0"); + break; + case CMD_REBOOT: + terminal_writestring("Rebooting system..."); + // Add reboot logic here + break; + case CMD_WHOAMI: + terminal_writestring("Revan"); + break; + case CMD_TIME: + show_time(); break; + default: + terminal_writestring("bash: command '"); printf(command); terminal_writestring("' not recognised!!!"); + } + + terminal_new_line(); terminal_writestring("cheaky>"); +} \ No newline at end of file diff --git a/kernel/mmu/alloc.c b/kernel/mmu/alloc.c index 7f61463..0a7e8e6 100644 --- a/kernel/mmu/alloc.c +++ b/kernel/mmu/alloc.c @@ -64,7 +64,7 @@ void *ksbrk(uint16_t n) realsize = KMALLOC_MINSIZE; } - printf("Size required: %d\n", realsize); +// printf("Size required: %d\n", realsize); struct kmalloc_header *chunk, *other; chunk = (struct kmalloc_header*) &end; while(chunk->used || chunk->size < realsize) diff --git a/libc/include/string.h b/libc/include/string.h index 56b7250..7b2967a 100644 --- a/libc/include/string.h +++ b/libc/include/string.h @@ -9,9 +9,11 @@ extern "C" { #endif int memcmp(const void*, const void*, size_t); -void* memcpy(void* __restrict, const void* __restrict, size_t); +int strcmp(const void*, const void*); +void* memcpy(void* __restrict, const void* __restrict, size_t);; void* memmove(void*, const void*, size_t); void* memset(void*, int, size_t); +size_t strlen(const char*); #ifdef _cplusplus } diff --git a/libc/string/memcmp.c b/libc/string/memcmp.c index 901803b..babae84 100644 --- a/libc/string/memcmp.c +++ b/libc/string/memcmp.c @@ -11,4 +11,19 @@ int memcmp(const void* aptr, const void* bptr, size_t size) { return 1; } return 0; +} + +int strcmp(const void* aptr, const void* bptr) { + size_t alen = strlen(aptr); + size_t blen = strlen(bptr); + if(alen != blen) + return 0; + const unsigned char* a = (const unsigned char*) aptr; + const unsigned char* b = (const unsigned char*) bptr; + for(size_t i = 0; i < alen; i++) + { + if(a[i] != b[i]) + return 0; + } + return 1; } \ No newline at end of file