From 02059bff336f3a29f0b480539c5aef57c1594404 Mon Sep 17 00:00:00 2001 From: starnakin Date: Wed, 25 Sep 2024 22:25:35 +0200 Subject: [PATCH] fix: cmd arg work --- src/shell/exec.c | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/src/shell/exec.c b/src/shell/exec.c index 7f1ec9e..63d13f9 100644 --- a/src/shell/exec.c +++ b/src/shell/exec.c @@ -1,3 +1,5 @@ +#include + #include "alloc.h" #include "commands.h" #include "kprintf.h" @@ -12,19 +14,24 @@ struct shell_command { char name[16]; char description[256]; void (*fn)(char *arg); + uint8_t name_len; }; +#define ARRAY_SIZE(array) sizeof(array) / sizeof(array[0]) +#define COMMAND_ADDER(name, description, function) \ + {name, description, function, ARRAY_SIZE(name) - 1} + const struct shell_command commands[] = { - {"help", "Print this help menu", help_cmd}, - {"reboot", "Reboot the system", reboot_cmd}, - {"poweroff", "Shut down the system", poweroff_cmd}, - {"halt", "Stop all CPU functions", halt_cmd}, - {"stack", "Print the stack trace", stack_cmd}, - {"heap", "Print the heaps", heap_cmd}, - {"clear", "Clear the current terminal", clear_cmd}, - {"date", "Display the current time and date", date_cmd}, - {"merdella", "Surprise", merdella_cmd}, - // {"color", "Change the screen color", color}, + COMMAND_ADDER("help", "Print this help menu", help_cmd), + COMMAND_ADDER("reboot", "Reboot the system", reboot_cmd), + COMMAND_ADDER("poweroff", "Shut down the system", poweroff_cmd), + COMMAND_ADDER("halt", "Stop all CPU functions", halt_cmd), + COMMAND_ADDER("stack", "Print the stack trace", stack_cmd), + COMMAND_ADDER("heap", "Print the heaps", heap_cmd), + COMMAND_ADDER("clear", "Clear the current terminal", clear_cmd), + COMMAND_ADDER("date", "Display the current time and date", date_cmd), + COMMAND_ADDER("merdella", "Surprise", merdella_cmd), + COMMAND_ADDER("color", "Change the screen color", color_cmd), }; #define NB_CMDS (sizeof(commands) / sizeof(commands[0])) @@ -112,9 +119,15 @@ void shell_init() read_line(); bool invalid = true; for (unsigned i = 0; i < NB_CMDS; i++) { - if (!strcmp(commands[i].name, screen->line)) { - commands[i].fn(screen->line + - strlen(commands[i].name) + 1); + if (!strncmp(commands[i].name, screen->line, + commands[i].name_len)) { + commands[i].fn( + screen->line + commands[i].name_len + + (strlen(screen->line) > + commands[i] + .name_len)); // "reboot " => arg = "" + // not " " and "reboot" => + // "" without invalid read invalid = false; break; }