fix: cmd arg work

This commit is contained in:
starnakin 2024-09-25 22:25:35 +02:00
parent 3a916908ef
commit 02059bff33

View File

@ -1,3 +1,5 @@
#include <stdint.h>
#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;
}