From a7288b8ecff0eeb2ede38fc471f26ec1fc140d57 Mon Sep 17 00:00:00 2001 From: Tulio A M Mendes Date: Tue, 10 Feb 2026 03:27:16 -0300 Subject: [PATCH] refactor: remove dead shell.c, integrate commands into kconsole shell.c became dead code after kconsole_enter() replaced shell_init() as the fallback when init_start() fails. Nobody called shell_init(). - Integrate useful shell commands into kconsole: ls, cat, clear, mem, sleep, ring3 (via arch_platform_usermode_test_start) - Remove src/kernel/shell.c and include/shell.h - Remove shell.h include from main.c - kconsole is now the single kernel-mode interactive console Passes: make, cppcheck, QEMU smoke test. --- include/shell.h | 6 -- src/kernel/kconsole.c | 73 +++++++++++++++++++-- src/kernel/main.c | 1 - src/kernel/shell.c | 144 ------------------------------------------ 4 files changed, 69 insertions(+), 155 deletions(-) delete mode 100644 include/shell.h delete mode 100644 src/kernel/shell.c diff --git a/include/shell.h b/include/shell.h deleted file mode 100644 index 2af8086..0000000 --- a/include/shell.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef SHELL_H -#define SHELL_H - -void shell_init(void); - -#endif diff --git a/src/kernel/kconsole.c b/src/kernel/kconsole.c index 53244b1..8e21a6d 100644 --- a/src/kernel/kconsole.c +++ b/src/kernel/kconsole.c @@ -1,6 +1,10 @@ #include "kconsole.h" #include "console.h" #include "utils.h" +#include "fs.h" +#include "heap.h" +#include "process.h" +#include "arch/arch_platform.h" #include "hal/system.h" #include "hal/cpu.h" @@ -8,16 +12,68 @@ static void kconsole_help(void) { kprintf("kconsole commands:\n"); - kprintf(" help - Show this list\n"); - kprintf(" dmesg - Show kernel log buffer\n"); - kprintf(" reboot - Restart system\n"); - kprintf(" halt - Halt the CPU\n"); + kprintf(" help - Show this list\n"); + kprintf(" clear - Clear screen\n"); + kprintf(" ls - List files\n"); + kprintf(" cat - Read file content\n"); + kprintf(" mem - Show memory stats\n"); + kprintf(" dmesg - Show kernel log buffer\n"); + kprintf(" sleep - Sleep for N ticks\n"); + kprintf(" ring3 - Run usermode syscall test\n"); + kprintf(" reboot - Restart system\n"); + kprintf(" halt - Halt the CPU\n"); } static void kconsole_exec(const char* cmd) { if (strcmp(cmd, "help") == 0) { kconsole_help(); } + else if (strcmp(cmd, "clear") == 0) { + kprintf("\033[2J\033[1;1H"); + } + else if (strcmp(cmd, "ls") == 0) { + if (!fs_root) { + kprintf("No filesystem mounted.\n"); + } else { + kprintf("Filesystem Mounted (InitRD).\n"); + kprintf("Try: cat test.txt\n"); + } + } + else if (strncmp(cmd, "cat ", 4) == 0) { + if (!fs_root) { + kprintf("No filesystem mounted.\n"); + } else { + const char* fname = cmd + 4; + fs_node_t* file = NULL; + if (fname[0] == '/') { + file = vfs_lookup(fname); + } else { + char abs[132]; + abs[0] = '/'; + abs[1] = 0; + strcpy(abs + 1, fname); + file = vfs_lookup(abs); + } + if (file) { + kprintf("Reading %s...\n", fname); + uint8_t* buf = (uint8_t*)kmalloc(file->length + 1); + if (buf) { + uint32_t sz = vfs_read(file, 0, file->length, buf); + buf[sz] = 0; + kprintf("%s\n", (char*)buf); + kfree(buf); + } else { + kprintf("OOM: File too big for heap.\n"); + } + } else { + kprintf("File not found.\n"); + } + } + } + else if (strcmp(cmd, "mem") == 0) { + kprintf("Memory Stats:\n"); + kprintf(" Total RAM: [TODO] MB\n"); + } else if (strcmp(cmd, "dmesg") == 0) { char buf[4096]; size_t n = klog_read(buf, sizeof(buf)); @@ -27,6 +83,15 @@ static void kconsole_exec(const char* cmd) { kprintf("(empty)\n"); } } + else if (strncmp(cmd, "sleep ", 6) == 0) { + int ticks = atoi(cmd + 6); + kprintf("Sleeping for %s ticks...\n", cmd + 6); + process_sleep(ticks); + kprintf("Woke up!\n"); + } + else if (strcmp(cmd, "ring3") == 0) { + arch_platform_usermode_test_start(); + } else if (strcmp(cmd, "reboot") == 0) { hal_system_reboot(); } diff --git a/src/kernel/main.c b/src/kernel/main.c index 609ae55..6876c4a 100644 --- a/src/kernel/main.c +++ b/src/kernel/main.c @@ -7,7 +7,6 @@ #include "vmm.h" #include "process.h" #include "keyboard.h" -#include "shell.h" #include "kconsole.h" #include "heap.h" #include "timer.h" diff --git a/src/kernel/shell.c b/src/kernel/shell.c deleted file mode 100644 index 6b82c0e..0000000 --- a/src/kernel/shell.c +++ /dev/null @@ -1,144 +0,0 @@ -#include "shell.h" -#include "keyboard.h" -#include "console.h" -#include "utils.h" -#include "pmm.h" -#include "process.h" -#include "fs.h" -#include "heap.h" - -#include "arch/arch_platform.h" -#include "hal/system.h" -#include "hal/cpu.h" - -#define MAX_CMD_LEN 256 -static char cmd_buffer[MAX_CMD_LEN]; -static int cmd_index = 0; - -void print_prompt(void) { - kprintf("\nAdrOS $> "); -} - -void execute_command(char* cmd) { - kprintf("\n"); - - if (strcmp(cmd, "help") == 0) { - kprintf("Available commands:\n"); - kprintf(" help - Show this list\n"); - kprintf(" clear - Clear screen\n"); - kprintf(" ls - List files (Dummy)\n"); - kprintf(" cat - Read file content\n"); - kprintf(" mem - Show memory stats\n"); - kprintf(" panic - Trigger kernel panic\n"); - kprintf(" ring3 - Run usermode syscall test\n"); - kprintf(" reboot - Restart system\n"); - kprintf(" sleep - Sleep for N ticks\n"); - kprintf(" dmesg - Show kernel log buffer\n"); - } - else if (strcmp(cmd, "ls") == 0) { - if (!fs_root) { - kprintf("No filesystem mounted.\n"); - } else { - kprintf("Filesystem Mounted (InitRD).\n"); - kprintf("Try: cat test.txt\n"); - } - } - else if (strncmp(cmd, "cat ", 4) == 0) { - if (!fs_root) { - kprintf("No filesystem mounted.\n"); - } else { - const char* fname = cmd + 4; - fs_node_t* file = NULL; - if (fname[0] == '/') { - file = vfs_lookup(fname); - } else { - char abs[132]; - abs[0] = '/'; - abs[1] = 0; - strcpy(abs + 1, fname); - file = vfs_lookup(abs); - } - if (file) { - kprintf("Reading %s...\n", fname); - uint8_t* buf = (uint8_t*)kmalloc(file->length + 1); - if (buf) { - uint32_t sz = vfs_read(file, 0, file->length, buf); - buf[sz] = 0; - kprintf("%s\n", (char*)buf); - kfree(buf); - } else { - kprintf("OOM: File too big for heap.\n"); - } - } else { - kprintf("File not found.\n"); - } - } - } - else if (strcmp(cmd, "clear") == 0) { - kprintf("\033[2J\033[1;1H"); - } - else if (strncmp(cmd, "sleep ", 6) == 0) { - int ticks = atoi(cmd + 6); - kprintf("Sleeping for %s ticks...\n", cmd + 6); - process_sleep(ticks); - kprintf("Woke up!\n"); - } - else if (strcmp(cmd, "mem") == 0) { - kprintf("Memory Stats:\n"); - kprintf(" Total RAM: [TODO] MB\n"); - } - else if (strcmp(cmd, "panic") == 0) { - hal_cpu_disable_interrupts(); - for(;;) { - hal_cpu_idle(); - } - } - else if (strcmp(cmd, "ring3") == 0) { - arch_platform_usermode_test_start(); - } - else if (strcmp(cmd, "dmesg") == 0) { - char dmesg_buf[4096]; - size_t n = klog_read(dmesg_buf, sizeof(dmesg_buf)); - if (n > 0) { - console_write(dmesg_buf); - } else { - kprintf("(empty)\n"); - } - } - else if (strcmp(cmd, "reboot") == 0) { - hal_system_reboot(); - } - else if (strlen(cmd) > 0) { - kprintf("Unknown command: %s\n", cmd); - } - - print_prompt(); -} - -void shell_callback(char c) { - if (c == '\n') { - cmd_buffer[cmd_index] = 0; - execute_command(cmd_buffer); - cmd_index = 0; - } - else if (c == '\b') { - if (cmd_index > 0) { - cmd_index--; - console_write("\b \b"); - } - } - else { - if (cmd_index < MAX_CMD_LEN - 1) { - cmd_buffer[cmd_index++] = c; - char str[2] = { c, 0 }; - console_write(str); - } - } -} - -void shell_init(void) { - kprintf("[SHELL] Starting Shell...\n"); - cmd_index = 0; - keyboard_set_callback(shell_callback); - print_prompt(); -} -- 2.43.0